Country Flag
CountryFlag and CurrencyFlag render an SVG flag from a country or currency code at run time, with a dashed-circle fallback for unknown codes.
Description
CountryFlag turns a runtime country code such as US or TR into an SVG flag. CurrencyFlag turns a currency code such as USD or EUR into the flag of the matching country (EUR shows the EU flag). Both are leaf SVG elements, so they take no children.
Use them when the code is data: a select value, a currency picker, a table row, a language list. When the country is known while you write the code, skip these components and import the named flag from @create-ui/assets/flags (for example Turkey). That imports only the flag you use.
Both components look up the flag in one table, so every flag in that table ships with the component. This is a trade-off for dynamic codes.
If the code is not found, the component renders a dashed circle (FlagFallback) so layouts do not shift.
Installation
Usage
Examples
Country and currency codes
The two components expect different codes. CountryFlag takes an ISO 3166-1 alpha-2 country code (and EU). CurrencyFlag takes an ISO 4217 currency code. Lookup is case-insensitive.
Passing a currency code to CountryFlag, or a country code to CurrencyFlag, renders the fallback. There is no error or console warning, so check the code format first when a flag looks like an empty circle.
Size
There is no size prop. A flag is size-6 by default; set another size with className.
Fallback
Without a fallback, an unknown code renders the dashed circle. Pass any node to replace it. Pass null to render nothing.
In a currency field
CurrencyFlag sits in the prefix slot of an InputGroupSelect. The flag is decorative, so the currency code next to it carries the meaning.
Accessibility
Flags are decorative. Both components render the SVG with aria-hidden="true", and the fallback circle is also aria-hidden. Screen readers skip them.
Put the meaning in adjacent text, for example the country name or the currency code beside the flag. A flag on its own conveys nothing to assistive technology.
The components have no keyboard interaction and no focusable parts.
Styling
Tailwind override: pass className to resize or restyle the SVG. It is merged after the default size-6 shrink-0:
Data slots: the components set these for CSS targeting:
data-slot="country-flag"onCountryFlag.data-slot="currency-flag"onCurrencyFlag.data-slot="flag-fallback"on the fallback circle.
Related Components
- Phone Input: a complete phone field that uses
CountryFlagin its country select. - Input Group: hosts the flag in a
prefixorvalueChildrenslot for currency and country selects. - Select: pairs a flag with each option label.
API Reference
CountryFlag
Extends React.SVGProps<SVGSVGElement> minus children.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | - | ISO 3166-1 alpha-2 country code (US, TR), plus EU. Case-insensitive. |
fallback | ReactNode | - | Rendered when the code is unknown. Checked with !== undefined, so null renders nothing. |
className | string | - | Tailwind classes merged after size-6 shrink-0. |
CurrencyFlag
Extends React.SVGProps<SVGSVGElement> minus children. Same props as CountryFlag, with a different code format.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | - | ISO 4217 currency code (USD, EUR). Case-insensitive. Covers the currencies in the lookup map only. |
fallback | ReactNode | - | Rendered when the currency is not in the map. null renders nothing. |
className | string | - | Tailwind classes merged after size-6 shrink-0. |
FlagFallback
The dashed circle used when no flag matches. It takes no props. Render it yourself when you need the same placeholder elsewhere.