v1.5

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

pnpm dlx @create-ui/cli add country-flag

Usage

import { CountryFlag, CurrencyFlag } from "@/components/ui/country-flag"
<CountryFlag code="US" />
<CurrencyFlag code="EUR" />

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.

<CountryFlag code="TR" />
<CurrencyFlag code="USD" />

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.

<CountryFlag code="DE" className="size-5" />

Fallback

Without a fallback, an unknown code renders the dashed circle. Pass any node to replace it. Pass null to render nothing.

<CountryFlag code="XX" fallback={<span>?</span>} />
<CountryFlag code="XX" fallback={null} />

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:

<CountryFlag code="US" className="size-10 rounded-full" />

Data slots: the components set these for CSS targeting:

  • data-slot="country-flag" on CountryFlag.
  • data-slot="currency-flag" on CurrencyFlag.
  • data-slot="flag-fallback" on the fallback circle.
  • Phone Input: a complete phone field that uses CountryFlag in its country select.
  • Input Group: hosts the flag in a prefix or valueChildren slot for currency and country selects.
  • Select: pairs a flag with each option label.

API Reference

CountryFlag

Extends React.SVGProps<SVGSVGElement> minus children.

Props

PropTypeDefaultDescription
codestring-ISO 3166-1 alpha-2 country code (US, TR), plus EU. Case-insensitive.
fallbackReactNode-Rendered when the code is unknown. Checked with !== undefined, so null renders nothing.
classNamestring-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

PropTypeDefaultDescription
codestring-ISO 4217 currency code (USD, EUR). Case-insensitive. Covers the currencies in the lookup map only.
fallbackReactNode-Rendered when the currency is not in the map. null renders nothing.
classNamestring-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.