Skip to content

Extras

Flag

Bit.BlazorUI.Extras

BitFlag renders the flag of a country, named by a BitCountry, an ISO 3166-1 alpha-2 or alpha-3 code, a dialing code or an English name - alternative names, abbreviations and accents included. It draws the packaged image, the flat or shiny image set of the Bit.BlazorUI.Assets package at the size and density the screen needs, the Unicode emoji flag or a set of images of the page's own, in a frame that carries the size, the aspect ratio and the shape - a rounded corner, a circle, a border for the flags that would otherwise disappear into a white surface. It is decorative to assistive technologies until it is named, and an OnClick turns it into a button that answers the keyboard as well as the pointer.

Notes

To use this component, you need to install the Bit.BlazorUI.Extras(opens in a new tab) nuget package, as described in the Optional steps of the Getting started page.

The image sets its ImageSet parameter draws the flags out of ship in the Bit.BlazorUI.Assets(opens in a new tab) nuget package, which has to be installed as well for that parameter.

A flag stands for a country and never for a language: Spanish is not the flag of Spain to the larger part of the world that speaks it, and English belongs to no one flag at all. A language switcher wants the name of the language written in that language - a country, a region or a currency is what this component is for.

Usage

Every example is live. Open its code to see exactly what produced the component running underneath.

Basic

Country is the fastest way of naming the country - there is nothing to look up - and every country the packaged images cover is a field of BitCountries, so it can be written straight into the markup. The flag is drawn as a 16 pixel image out of the Extras package, at the medium icon size of the theme.

Naming the country

The country can also be named by any of the four ways one is written down, and the first of them that resolves wins: Iso2 and Iso3 are the ISO 3166-1 codes, Code is the dialing code a telephone number begins with, and Name is the full English name. All four are matched case insensitively, and a dialing code is read the way a number is written - "+31", "0031" and "31" all reach the Netherlands.

A page is handed a country name in whichever spelling its own data uses, and none of those spellings is a different country. So Name also answers to the alternative names and the abbreviations a country is as widely known by - "Czechia", "Türkiye", "Holland", "USA", "Burma", "DRC" - and to the accents, the punctuation and the spacing another source writes it with, so "Curaçao" and "Guinea-Bissau" reach the countries this table writes as "Curacao" and "Guinea Bissau". Iso2 answers to "UK" as well, which ISO 3166-1 reserves for the United Kingdom even though its own code is "GB".

Dialing codes are not unique: Canada and the United States both carry "1", so a flag named by one alone is the first country of the table carrying it. Where the difference matters, name the country by its ISO code.

Iso2="nl"
Iso3="NLD"
Code="+31"
Name="Netherlands"
Name="Holland" - an everyday alternative
Name="Czechia" - the name the country carries today
Name="Curaçao" - accents are ignored on both sides
Iso2="UK" - the code ISO reserves for the United Kingdom

Shape

The frame around the flag carries its shape: Rounded softens its corners and Circular clips it to a circle, cropping the flag rather than squashing it. Bordered draws a hairline around it - which is the only thing that keeps a mostly white flag, Japan's or Switzerland's, off a white surface - and Shadow lifts it off the page with the card shadow of the theme. The border is drawn inside the frame, so the border itself takes no room.

The packaged image draws each flag in its own proportions inside a square, so a shaped frame is cut to the flag rather than to that square: the corners, the border and the shadow are the flag's own, and a rounded, bordered or shadowed flag is as wide as the flag is at the height it was given.

Rounded
Circular
Bordered
Shadow
All three

Emoji

Emoji draws the flag as its Unicode emoji instead of as an image. The emoji is text, so it costs no request at all and stays crisp at any size, where the packaged image is only as sharp as its 16 pixels. What it looks like is the platform's to decide, though: the emoji fonts of Apple, Google and the Noto family draw the flags, while Windows draws the two letters of the country code side by side instead - which is why the image is what is drawn unless this asks otherwise.

So on Windows every sample below is two letters rather than a flag, a subdivision is a plain black flag, and the shaped frame of the last one crops those letters the way it would crop the flag. That is the platform's emoji font rather than the component: the same markup draws the flags wherever the font has them.

It is built out of the country code rather than looked up, so it also answers for a code the packaged images do not cover, and for the three subdivisions Unicode recommends for interchange: "GB-ENG", "GB-SCT" and "GB-WLS".

A glyph is as wide as the font draws it rather than as wide as a picture, so the frame grows to fit it - unless something has asked the frame for a shape of its own, where it keeps its size and crops the glyph exactly the way it crops a picture.

Accessibility

A flag is decorative to assistive technologies until something names it: it nearly always sits beside the name of the very country it stands for, and an image that repeats the text beside it is noise to a screen reader. Alt names it where it carries meaning of its own, and AutoAlt is that written for you out of the country's own name. Title is the tooltip, with AutoTitle filling it in the same way - but a tooltip is a pointer affordance rather than an accessible name, so a flag that has to be named to everyone wants both.

Where the name lands depends on what is drawn. An image carries it in its own alt, and an empty alt is what marks the decorative one. The emoji flag and whatever a FallbackTemplate puts in its place are text rather than pictures, and text is read out wherever it is - so a named one has the frame take the img role and the name instead, and an unnamed one is taken out of the accessibility tree altogether rather than being announced as two stray letters or a question mark.

Decorative (the default) - beside the name it would repeat
Canada
AutoAlt - named "Canada"
Ships to Canada
Alt - named by the page
AutoTitle - hover the flag
A named fallback is announced as a picture called "Kosovo"

Src & FallbackTemplate

A country the table does not carry draws nothing at all rather than a broken image, and FallbackTemplate is what stands in its place - a letter pair, an icon, whatever the page would rather show. It also stands in for an image that failed to load, which is what Src makes possible: it points the flag at a set of images of the page's own, a vector set that stays sharp at any size or one covering a country the packaged images do not.

A Src that fails is stood in for by the packaged flag of the same country first, so a set of a page's own that turns out not to cover a country is answered with the flag that ships rather than with nothing; the template is what is left once there is no flag at all.

An unknown country draws nothing
...unless a FallbackTemplate stands in for it
Src points at an image of the page's own
...one that fails falls back to the packaged flag
...and one with no country to fall back to falls to the template

Grayscale & IsEnabled

Grayscale drains the color out of the flag while leaving it recognisable, which is how a flag says that the country is not the one in play - an unavailable locale, a region not shipped to. IsEnabled dims it with the disabled opacity of the theme instead, refuses the click and leaves the tab order - while still answering the pointer, so the tooltip saying why the country cannot be picked is still there to be read. The two are independent and read well together.

Normal
Grayscale
IsEnabled="false" - hover it for the reason why
Both

OnClick

OnClick turns the whole frame into a button: it takes the button role, joins the tab order, draws the focus ring of the theme and answers Enter and Space as well as the pointer - the Space is even kept from scrolling the page under it. A flag is drawn at the icon sizes of the theme, well under the 24 pixels WCAG 2.2 asks of a target, so a clickable one grows the area that answers the pointer to that size without growing the flag.

A button needs a name to be announced by, so give a clickable flag an Alt saying what the click does; without one it falls back to the name of the country it shows, since "Italy, button" is worth more than "button" on its own, and a button with neither is left without the role rather than becoming a control with nothing to read out. A disabled flag leaves the tab order and stops answering.


Selected: (none)

All countries

Every country the packaged images cover, out of BitCountries.All. The images load lazily by default, so a list this long only fetches the rows that are actually scrolled to; hover any of them for its name and ISO code.

Aspect ratio & fit

The packaged images are square and so is the frame they are drawn in, which is the shape a flag has beside anything else in a list. A set of the page's own pointed at with Src is usually drawn in the proportions of the flags themselves - 4:3 and 3:2 are what the vector sets of the world ship - and AspectRatio is what gives the frame that shape: the height is the one that stays and the width follows from the ratio, or the other way round where a Width is what was given.

Fit decides what the picture does inside a frame that is not its own shape. It covers the frame and is cropped by default, which is what keeps a circular or rounded flag full of flag rather than of empty space; Contain fits the whole flag inside the frame instead and leaves the rest of it empty.

A 3:2 image in the square frame, cropped to it
AspectRatio="3/2" - the frame takes the shape of the flag
AspectRatio="4/3"
Fit="BitImageFit.Contain" - the whole flag inside the square frame

Loading & events

Loading says when the browser fetches the image. It holds off until the flag is about to be scrolled to, which is what a list of two hundred of them wants; Eager starts the fetch at once, which is only worth setting for a flag whose image has to be on the wire before the layout settles.

OnLoad and OnError are the browser's own events on the picture, so neither fires for the emoji flag, which is text and cannot fail. A Src that fails raises OnError once for the page's own image and once more if the packaged flag standing in for it fails as well. ImageAttributes puts attributes on the picture rather than on the frame, which is where a crossorigin or a referrerpolicy for a CDN belongs.

The flags below are only rendered when the button is pressed, so their images are fetched and their events fire in front of you - rather than long before this section was scrolled to. Every press renders them anew and starts the counts over.



Image sets

The packaged image is 16 pixels, which is all a flag at the icon sizes of the theme needs and which blurs past them. ImageSet draws the flag out of one of the two image sets of the Bit.BlazorUI.Assets package instead - Flat, the same artwork as the packaged image, and Shiny - each carrying every flag at 16, 24, 32, 48 and 64 pixels. Every size is offered to the browser, which fetches only the one the size of the flag and the density of the screen call for, so a large flag on a phone stays as sharp as a small one on a desktop.

The size is read off Width, Height and AspectRatio where they are written in px or rem, and off Size otherwise. A frame cut to the flag - rounded, circular, bordered or shadowed - is drawn from the one image whose flag covers it twice over. The images ship in that package, so it has to be installed; a country the sets do not cover, and an image that fails to load, fall back to the packaged flag.

ImageSize pins the flag to one of those sizes instead - Size16 to Size64 - where the page knows better than the size of the flag which image it wants: one it preloads, a known byte count for a long list, or a flag sized by a style of its own. The image is still scaled to the frame, so a small one drawn large is blurred. It only applies to an ImageSet, since the packaged image is 16 pixels and nothing else.

The image set is also cascaded: a CascadingValue of a BitFlagImageSet sets it for every flag inside, and one set on the flag itself wins over it - the last flag of the row below.

The packaged 16 pixel image
ImageSet="BitFlagImageSet.Flat"
ImageSet="BitFlagImageSet.Shiny"
ImageSize="BitFlagImageSize.Size16" - pinned to the 16 pixel image, scaled up
A frame cut to the flag of the set

Size

Size steps the flag through the icon sizes of the theme, and Medium is the 16 pixels the packaged images are drawn at - the size a flag with nothing set is. Width and Height take any CSS length instead and win over it; the images are square, so a height alone sets the width as well. The packaged image is raster, so a large one is those same 16 pixels scaled up: past these sizes, an ImageSet, the emoji flag or a Src of a vector set of the page's own is what stays sharp.

Small
Medium (the default)
Large
Height="3rem"
Width="4rem" Height="2rem"

Style & Class

Style and Class reach the root element - the frame the flag is drawn in - and Styles and Classes reach each part of the component by name: the Root, the Image, the Emoji the emoji flag is written in and the Fallback that stands in for a flag that is not there.

Style
Class
Styles
Classes

RTL

Dir renders the flag in a right-to-left direction, which is also cascaded, so a flag inside an RTL container inherits it. The flag itself is a picture and is never mirrored - only the row it sits in flips - which is what a flag has to do: a mirrored flag is a different flag.

ایران
عربستان سعودی
امارات متحده عربی

API

Every parameter, public member, sub-class and enum this component exposes.

BitFlag parameters

Name Type Default value Description
Alt string? null The alternative text of the flag image, which is also its accessible name. A flag with none is decorative and is not announced at all. Where OnClick makes the flag a button, this is the name of the button.
AspectRatio string? null The aspect ratio of the frame of the flag, as any CSS aspect-ratio value (e.g. "4/3" or "1"). The height stays and the width follows from the ratio - or the other way round where a Width is what was given - which is what gives a Src of a page's own the proportions the flag itself is drawn in.
AutoAlt bool false Names the flag to assistive technologies with the full name of the country it resolved to. An Alt of the page's own wins over it.
AutoTitle bool false Sets the tooltip of the flag to the full name of the country it resolved to. A Title of the page's own wins over it.
Bordered bool false Draws a hairline border around the flag, which is what keeps a mostly white flag off a white surface. It is drawn inside the frame, so it costs no layout.
Circular bool false Clips the flag into a circle. It wins over Rounded where both are set.
Classes BitFlagClassStyles? null Custom CSS classes for different parts of the flag.
Code string? null The dialing code of the country, read the way a telephone number is written: "+31", "0031" and "31" all reach the Netherlands. Dialing codes are not unique, and the first country of BitCountries.All carrying the code wins.
Country BitCountry? null The country to render the flag. It is taken exactly as given rather than looked up, so a country of the page's own is as valid as one out of BitCountries, and it wins over every other way of naming one.
Emoji bool false Renders the flag as its Unicode emoji instead of as an image, which costs no request and stays crisp at any size. Windows draws the two letters of the country code instead of the flag, and a subdivision as a plain black flag. It wins over Src.
FallbackTemplate RenderFragment? null What to render in place of the flag when there is none to draw - a country that resolved to nothing, or an image that failed to load after the packaged flag has stood in for it.
Fit BitImageFit? null How the flag image is scaled and cropped to fit the frame around it, which only matters where a Src of the page's own or an AspectRatio makes the two different shapes. Unset, the image covers the frame and the overflow is cropped.
Grayscale bool false Draws the flag in shades of grey, which is how a flag says the country is not the one in play while staying recognisable.
Height string? null The height of the flag, as any CSS length. It also sets the size the emoji flag is drawn at, and the width where no Width is set. It wins over Size.
ImageAttributes Dictionary<string, object> new Dictionary<string, object>() Additional HTML attributes to render on the img element rather than on the frame - a crossorigin or a referrerpolicy for a Src pointing at a CDN. The src, the alt and the loading of the flag itself still win over them, and a srcset among them only goes with a Src of the page's own.
ImageSet BitFlagImageSet? null Draws the flag out of the flat or the shiny image set of the Bit.BlazorUI.Assets package - which has to be installed for it - at whichever of 16, 24, 32, 48 and 64 pixels the size of the flag and the density of the screen call for, unless ImageSize picks one. The size is read off Width, Height and AspectRatio in px or rem, and off Size otherwise. Unset, the flag is the packaged 16 pixel image. It is also cascaded, so a CascadingValue of a BitFlagImageSet sets it for every flag inside; Src and Emoji win over it.
ImageSize BitFlagImageSize? null Pins the flag to one size of the image of its ImageSet instead of offering the browser every size of it. The image is still scaled to the frame, and a shaped frame is cut to the flag inside that image. It only applies to an ImageSet: without one, the flag is the packaged 16 pixel image.
Iso2 string? null The ISO 3166-1 alpha-2 code of the country, matched case insensitively. A code no country of BitCountries.All carries draws nothing rather than a broken image. The reserved "UK" is answered with the United Kingdom, whose own code is "GB".
Iso3 string? null The ISO 3166-1 alpha-3 code of the country, matched case insensitively.
Loading BitImageLoading? null How the browser should load the flag image. It defaults to loading lazily, which is what a list of two hundred flags wants.
Name string? null The full English name of the country, matched case insensitively against the whole name rather than part of it. The alternative names and abbreviations a country is as widely known by are answered too - "Czechia", "Türkiye", "Holland", "USA" - and so are the accents and the punctuation another source spells it with.
OnClick EventCallback<MouseEventArgs> The callback for when the flag is clicked. Setting it turns the flag into a button that joins the tab order, grows its target to the 24 pixels WCAG asks of one and answers Enter and Space as well as the pointer, named by its Alt or, without one, by the country it shows.
OnError EventCallback The callback for when the flag image fails to load. A Src of the page's own that failed raises it once for that image and once more if the packaged flag standing in for it fails as well.
OnLoad EventCallback The callback for when the flag image has loaded. It is the browser's own load event, so it never fires for the emoji flag, which is text.
Rounded bool false Rounds the corners of the flag. Circular wins over it where both are set.
Shadow bool false Draws the card shadow of the theme under the flag.
Size BitSize? null The size of the flag, out of the icon sizes of the theme. Medium is the 16 pixels the packaged images are drawn at, and past the sizes of the theme an ImageSet is what stays sharp. Width and Height win over it.
Src string? null The url of the image to render instead of the packaged flag image, for a set of images of the page's own. It wins over ImageSet and Emoji wins over it, and a source that fails falls back to the packaged flag of the same country and then to the FallbackTemplate.
Styles BitFlagClassStyles? null Custom CSS styles for different parts of the flag.
Title string? null The tooltip value of the flag element. A tooltip is a pointer affordance rather than an accessible name, so a flag that has to be named to everyone wants an Alt as well.
Width string? null The width of the flag, as any CSS length. The flag images are square, so a Height alone usually sets both. It wins over Size.

BitFlag public members

Name Type Default value Description
FocusAsync ValueTask Gives focus to the flag element. Only a flag the browser can focus takes it: one with an OnClick handler, or one given a TabIndex of its own.
FocusAsync(bool preventScroll) ValueTask Gives focus to the flag element, leaving the page scrolled where it is instead of bringing the flag into view.

BitComponentBase parameters

Name Type Default value Description
AriaLabel string? null Gets or sets the accessible label for the component, used by assistive technologies.
Class string? null Gets or sets the CSS class name(s) to apply to the rendered element.
Dir BitDir? null Gets or sets the text directionality for the component's content.
ForceAnimation bool false Gets or sets a value indicating whether the component's animations play at their full duration even when reduced motion is requested.
HtmlAttributes Dictionary<string, object> new Dictionary<string, object>() Captures additional HTML attributes to be applied to the rendered element, in addition to the component's parameters.
Id string? null Gets or sets the unique identifier for the component's root element.
IsEnabled bool true Gets or sets a value indicating whether the component is enabled and can respond to user interaction.
Style string? null Gets or sets the CSS style string to apply to the rendered element.
TabIndex string? null Gets or sets the tab order index for the component when navigating with the keyboard.
Visibility BitVisibility BitVisibility.Visible Gets or sets the visibility state (visible, hidden, or collapsed) of the component.

BitComponentBase public members

Name Type Default value Description
UniqueId Guid Guid.NewGuid() Gets the readonly unique identifier for the component's root element, assigned when the component instance is constructed.
RootElement ElementReference Gets the reference to the root HTML element associated with this component.

BitCountries properties

The table of countries the flag images and the country lookups of the library are built on. Every country is a shared BitCountry instance named after itself, so it can be written straight into markup, and the lookups below resolve any of the ways one is written down back to it - out of a dictionary rather than by scanning the table.

Name Type Default value Description
All BitCountry[] Every country the packaged flag images cover, in alphabetical order.
FindByIso2(string? iso2) BitCountry? The country carrying the ISO 3166-1 alpha-2 code, case insensitively and ignoring surrounding whitespace. "UK" is answered with the United Kingdom, whose own code is "GB".
FindByIso3(string? iso3) BitCountry? The country carrying the ISO 3166-1 alpha-3 code, case insensitively.
FindByName(string? name) BitCountry? The country carrying the English name, matched against the whole name rather than part of it. The alternative names and abbreviations a country is as widely known by are answered too, and the accents, punctuation and spacing another source spells it with are ignored.
FindByCode(string? code) BitCountry? The first country carrying the dialing code, read the way a telephone number is written: "+31", "00 31" and "31" all reach the Netherlands.
Find(string? value) BitCountry? The country a single value stands for, read as an alpha-2 code, an alpha-3 code, a name and a dialing code in that order.
HasFlag(string? iso2) bool Whether a packaged flag image ships for the given alpha-2 code, answered without asking the network for it.
GetEmoji(string? iso2) string? The Unicode emoji flag of the given alpha-2 code, built from the pair of regional indicator symbols its letters stand for rather than looked up - so it answers for codes the packaged images do not cover, and for "GB-ENG", "GB-SCT" and "GB-WLS".

BitCountry properties

Represents the basic information of a specific country. BitCountries holds one shared instance per country, and its Find methods resolve any of the four values below back to it.

Name Type Default value Description
Name string The full name of the country.
Code string The dialing code of the country, written without its leading plus sign. It is not unique: Canada and the United States both carry "1".
Iso2 string The ISO 3166-1 alpha-2 code of the country, which is what the flag image and the emoji flag are keyed by.
Iso3 string The ISO 3166-1 alpha-3 code of the country.
Emoji string? The flag of the country as a Unicode emoji, built from the pair of regional indicator symbols its Iso2 stands for. Null where the Iso2 is not two ASCII letters.

BitFlagClassStyles properties

Custom CSS classes/styles for the different parts of the BitFlag component.

Name Type Default value Description
Root string? null Custom CSS classes/styles for the root element of the flag, which is the frame carrying the size, the shape and the border.
Image string? null Custom CSS classes/styles for the img element, which is only rendered while the flag is drawn as an image.
Emoji string? null Custom CSS classes/styles for the element the emoji flag is written in.
Fallback string? null Custom CSS classes/styles for the element wrapping the FallbackTemplate.

BitSize enum

Name Value Description
Small 0 The small size.
Medium 1 The medium size, which is the 16 pixels the packaged flag images are drawn at.
Large 2 The large size.

BitFlagImageSet enum

Name Value Description
Flat 0 The flat artwork, in the same style as the packaged 16 pixel image.
Shiny 1 The shiny artwork, with a gloss and a soft edge over the flag.

BitFlagImageSize enum

Name Value Description
Size16 0 The 16 pixel image.
Size24 1 The 24 pixel image.
Size32 2 The 32 pixel image.
Size48 3 The 48 pixel image.
Size64 4 The 64 pixel image.

BitImageFit enum

Name Value Description
None 0 Neither the image nor the frame are scaled. Whatever of the image does not fit is cropped away from the right and the bottom.
Center 1 The image is not scaled, and is centered within the frame with the overflow cropped.
CenterContain 2 The image keeps its aspect ratio, is scaled down where needed so that all of it fits, and is centered in the frame.
CenterCover 3 The image keeps its aspect ratio, is scaled up where needed so that it covers the frame, and is centered in it.
Contain 4 The image keeps its aspect ratio and is fully contained within the frame. Nothing is cropped, and whatever of the frame it does not reach is left empty.
Cover 5 The image keeps its aspect ratio and fills the frame, with whatever falls outside it cropped away. This is what a flag with no Fit set does.
Fill 6 The image is stretched to fill the frame exactly, at the cost of distorting the flag where the two shapes disagree.
ScaleDown 7 The image is contained within the frame but never scaled up, so one smaller than the frame keeps its natural size.

BitImageLoading enum

Name Value Description
Eager 0 Tells the browser to load the image as soon as the element is processed.
Lazy 1 Tells the browser to hold off on loading the image until it estimates that it will be needed imminently. This is what a flag with no Loading set does.

BitVisibility enum

Name Value Description
Visible 0 The content of the component is visible.
Hidden 1 The content of the component is hidden, but the space it takes on the page remains (visibility:hidden).
Collapsed 2 The component is hidden (display:none).

BitDir enum

Name Value Description
Ltr 0 Ltr (left to right) is to be used for languages that are written from the left to the right (like English).
Rtl 1 Rtl (right to left) is to be used for languages that are written from the right to the left (like Arabic).
Auto 2 Auto lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element.

Feedback

Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.