<!-- generated by scripts/gen-docs.mjs from docs/system/flag.md. Edit the source and rerun. -->
# Flag

## Description
Shows a country by its flag at the size of the text: beside the country's name, or named on its own beside a code, with a grey box where there is no flag.

`import { Flag, countryName } from "@/components/cobalt/ui/flag"`, source `registry/cobalt/ui/flag.tsx`, status `ready`.
Foundation: `hand-rolled`. Traps checked: `trap/illustration-announced`, `trap/empty-value`, `trap/color-only-status`, `trap/numbers-shift`.

The flag is a 4:3 box 0.875em tall, so 12 by 16px beside 14px text, with a corner three quarters of `--r-xs` and the photo hairline `--outline-image`; it is never mirrored, since a flag reads the same in either direction. `trap/illustration-announced` is answered by `role="img"` and the country's name by default, and `decorative` beside the name (ARIA). `trap/empty-value` by the grey box that keeps the place of a code with no file or a file that fails (States). `trap/color-only-status` n/a: a flag marks a place, never a state. `trap/numbers-shift` by the explicit 16 by 12 size on the `img` and the box's own size, so a row does not move when the file arrives (D37).

### Foundation
Not applicable: hand-rolled.

| Given by the foundation | Added by the team |
|---|---|
| Not applicable: hand-rolled, an HTML `span` around an `img` | The box at the text's size, its corner from `--r-xs` and the hairline; the 161 flags of flag-icons' 4:3 set (MIT, `public/flags/LICENSE`, credited in `public/flags/CREDITS.md`), which the registry's `flags` item copies into the host's `public/flags/`; each country's English name written out in the file, `countryName`, the same on the server and in every browser, with a code the set lacks said as itself; a subdivision code such as `US-DE` drawing its country's flag; the grey box for a code with no file and for a file that fails; `decorative` |

## Examples
Domiciles beside their names, and two flags named on their own beside their codes.

Real uses, 0 call sites (`rg -n "<Flag\b" app registry --glob '!registry/cobalt/ui/*'`): the part is new. Fund Census draws every domicile with these flags and the code beside them, and the screener and record scenes, the data table, Select and Combobox examples here do the same.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/flag/default.tsx` | default | Flags at the size of the text, beside a country's name and named on their own beside a code |
| `docs/system/examples/flag/state-named.tsx` | state:named | Beside a code, the flag names the country to a screen reader and on hover |
| `docs/system/examples/flag/state-decorative.tsx` | state:decorative | Beside the country's name, so a screen reader hears it once |
| `docs/system/examples/flag/state-missing.tsx` | state:missing | A code with no flag keeps the grey box, so the column holds its line; a state draws its country's flag |
| `docs/system/examples/flag/in-table.tsx` | composition:Table | A domicile column where the flag leads the name in each row; in a narrow column the flag stands named beside its code |

Example: Flags at the size of the text, beside a country's name and named on their own beside a code (`docs/system/examples/flag/default.tsx`)

```tsx
import { Flag } from "@/components/cobalt/ui/flag";

const domiciles = [
  { code: "KY", name: "Cayman Islands", funds: 412 },
  { code: "LU", name: "Luxembourg", funds: 188 },
  { code: "IE", name: "Ireland", funds: 141 },
  { code: "GB", name: "United Kingdom", funds: 96 },
];

export default function Example() {
  return (
    <div className="flex w-full max-w-xs flex-col gap-4 text-sm">
      <ul aria-label="Funds by domicile" className="flex flex-col gap-2">
        {domiciles.map((d) => (
          <li key={d.code} className="flex items-center gap-2">
            {/* The name is written beside the flag, so the flag is not read again. */}
            <Flag code={d.code} decorative />
            <span className="min-w-0 flex-1 truncate">{d.name}</span>
            <span className="text-muted-foreground tabular-nums">{d.funds} funds</span>
          </li>
        ))}
      </ul>
      <p className="text-muted-foreground">
        Filed from <Flag code="US" /> <code>US</code> and <Flag code="LU" /> <code>LU</code>.
      </p>
    </div>
  );
}
```

Example: A domicile column where the flag leads the name in each row; in a narrow column the flag stands named beside its code (`docs/system/examples/flag/in-table.tsx`)

```tsx
import { Flag } from "@/components/cobalt/ui/flag";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/cobalt/ui/table";

const funds = [
  { fund: "Harbour Street Master Fund LP", code: "KY", domicile: "Cayman Islands", assets: "$48.2B" },
  { fund: "Calder Global Relative Value Fund", code: "IE", domicile: "Ireland", assets: "$31.7B" },
  { fund: "Northgate Credit Opportunities SICAV", code: "LU", domicile: "Luxembourg", assets: "$22.4B" },
  { fund: "Meridian Opportunities Fund II", code: "US-DE", domicile: "Delaware", assets: "$9.8B" },
];

export default function Example() {
  return (
    // The table's box is a container, so the column follows the room it gets, not the window.
    <div className="@container w-full max-w-xl">
      <Table>
      <TableHeader>
        <TableRow>
          <TableHead>Fund</TableHead>
          <TableHead>Domicile</TableHead>
          <TableHead align="end">Gross assets</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        {funds.map((f) => (
          <TableRow key={f.fund}>
            <TableHead scope="row">{f.fund}</TableHead>
            <TableCell>
              {/* Beside its name the flag is not read again; beside its code it is named, so a
                  screen reader and the tooltip say the place. */}
              <span className="hidden items-center gap-2 @md:inline-flex">
                <Flag code={f.code} decorative />
                {f.domicile}
              </span>
              <span className="inline-flex items-center gap-2 whitespace-nowrap @md:hidden">
                <Flag code={f.code} />
                {f.code}
              </span>
            </TableCell>
            <TableCell align="end" className="tabular-nums">
              {f.assets}
            </TableCell>
          </TableRow>
        ))}
      </TableBody>
      </Table>
    </div>
  );
}
```

## Variants
Not applicable: Flag has no variant axis. It takes the size of the text around it, so a flag in a table row and one in a heading match their words without a size prop.

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| named | rendered without `decorative` | read the country's name, or hover for it | the flag; its name in `aria-label` and in the system tooltip on hover, which mounts on the first hover or focus (`LazyTip`), and in a table is the table's one tooltip, so a column of flags holds no tooltip of its own (`data-tip` carries the name) | test |
| decorative | `decorative`, beside the same name in text | read the name beside it | the same flag, `aria-hidden` | test |
| missing | a code with no file in the set, or a file that fails to load | read the name or the code beside it | the grey box at the flag's size, `data-missing` | test |

Example: Beside a code, the flag names the country to a screen reader and on hover (`docs/system/examples/flag/state-named.tsx`)

```tsx
import { Flag } from "@/components/cobalt/ui/flag";

export default function Example() {
  return (
    <span className="inline-flex items-center gap-2 text-sm">
      <Flag code="LU" />
      <code>LU</code>
    </span>
  );
}
```

Example: Beside the country's name, so a screen reader hears it once (`docs/system/examples/flag/state-decorative.tsx`)

```tsx
import { Flag } from "@/components/cobalt/ui/flag";

export default function Example() {
  return (
    <span className="inline-flex items-center gap-2 text-sm">
      <Flag code="NO" decorative />
      Norway
    </span>
  );
}
```

Example: A code with no flag keeps the grey box, so the column holds its line; a state draws its country's flag (`docs/system/examples/flag/state-missing.tsx`)

```tsx
import { Flag } from "@/components/cobalt/ui/flag";

export default function Example() {
  return (
    <ul aria-label="Domiciles" className="flex flex-col gap-2 text-sm">
      <li className="flex items-center gap-2">
        {/* XK, Kosovo, has no file in the set: the box stays, named by the country. */}
        <Flag code="XK" decorative />
        Kosovo
      </li>
      <li className="flex items-center gap-2">
        {/* A subdivision draws the country's flag; the name says which part. */}
        <Flag code="US-DE" decorative />
        Delaware, United States
      </li>
    </ul>
  );
}
```

### State precedence
- Named and decorative: decorative wins. The flag is hidden and the name beside it is read.
- Missing and named: both show. The grey box keeps the place and still carries the country's name.
- Missing and decorative: both show. The grey box keeps the place and the name beside it is read.

### Motion
Not applicable: no motion. The flag replaces nothing; the box is drawn before the file arrives.

## Props

<!-- Props generated from registry/cobalt/ui/flag.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `code` | `string` |  |  |
| `base?` | `string` | `"/flags"` | Where the host serves the flag files, without a trailing slash. |
| `decorative?` | `boolean` | `false` | For a flag beside the same name in text, so it is not read twice. |
| `name?` | `string` |  | The accessible name and the tooltip; by default the country's name in English. |

Also accepts every native `<span>` attribute.

`code` is the country as an ISO 3166-1 alpha-2 code in any case, with a subdivision after a hyphen if it has one, such as `KY` or `US-DE`; the flag is the country's. `name` replaces the accessible name and the tooltip, which by default is the country's English name, with the subdivision first, such as "DE, United States". `decorative` hides the flag. `base` is the address the host serves the flag files from, `/flags` by default. Every native `span` attribute except `children` and `title` passes through. `countryName(code)` is the name `Flag` uses, for a label beside it.

## Usage

### When to use
- A row, an option or a fact names a country or a place in one: a fund's domicile, an office's country, a country to choose.
- A column of codes such as KY and LU, where the flag finds a country faster than two letters.

### When not to use
- The choice is a language, not a country. Use Select instead, with each language named in itself, such as "Español", since one language is spoken in many countries and one country speaks many.
- The mark names a person. Use Avatar instead.
- The mark is a category, such as a fund's type. Use Tag instead, which takes its hue from the key.

### Rules
- `rule/flag-named-or-decorative`: When the country's name is written beside a flag, give the flag `decorative`, and when only a code or nothing is beside it, leave it named, instead of the same state everywhere, because a named flag beside its name is read twice and a hidden flag beside a code leaves "KY" unexplained. Evidence: principle wcag: 1.1.1 Non-text content, an image beside its own text alternative is decorative; single use docs/system/examples/flag/default.tsx:18. Check: test `flag.test.tsx` renders `aria-hidden` for a decorative flag and the country's name otherwise.
  - Don't: `<Flag code="KY" /> Cayman Islands`
  - Do: `<Flag code="KY" decorative /> Cayman Islands`
- `rule/flag-code-not-name`: When drawing a flag, pass the ISO 3166-1 code, such as `code="KY"`, instead of the country's name, because the file and the name are both found by the code and a name finds nothing, so the grey box shows. Evidence: principle platform: ISO 3166-1 alpha-2 is the code the flag files and the names share. Check: test `flag.test.tsx` shows the grey box and no `img` for a code the set lacks.
  - Don't: `<Flag code="Cayman Islands" />`
  - Do: `<Flag code="KY" />`

### Content
- `rule/flag-name-is-the-place`: When `name` is set, write the place as the product shows it, such as "Delaware, United States", with no prefix such as "Flag of", because the role already says image and a screen reader would read "image, Flag of Delaware". Evidence: principle wcag: 1.1.1 Non-text content, the alternative says what the image shows, not that it is an image. Check: review of every `name` on a `Flag`.
  - Don't: `<Flag code="US-DE" name="Flag of Delaware" />`
  - Do: `<Flag code="US-DE" name="Delaware, United States" />`

### Anti-slop
- `rule/flag-no-emoji`: When showing a country's flag, use `Flag` instead of a regional-indicator emoji such as 🇰🇾, because Windows draws those as two letters and their size follows the emoji font rather than the text. Evidence: principle platform: Windows renders a flag emoji as its two regional-indicator letters. Check: review.
  - Don't: `<span>🇰🇾 Cayman Islands</span>`
  - Do: `<span className="inline-flex items-center gap-2"><Flag code="KY" decorative />Cayman Islands</span>`

### Limits
Not applicable: a flag is one box at the text's size with no text of its own; the name beside it is the host's, which wraps or truncates as the host sets.

## Accessibility
Rests on the HTML `span` and `img`.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the flag | passes over it; a flag is not focusable | the next focusable element | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| root, named | img | `aria-label`, the country's name or `name` | none | the name and "image" | test |
| root, decorative | none, `aria-hidden="true"` | none | none | nothing; the name beside it is read once | test |
| root, with a blank `name` | none, `aria-hidden="true"` | none | none | nothing, never an image with an empty name | test |
| image | none, `alt=""` | none | none | nothing | test |

## Tokens
| Part | State | Token |
|---|---|---|
| box | all | `--muted` under the flag and for a missing one, `--outline-image` for the hairline, `--r-xs` for the corner |

## Related
- Avatar: the picture of a person.
- Tag: a mark for a category, coloured by its key.
- Select: a country list leads each option with its flag (`docs/system/examples/select/with-flags.tsx`).
- DataTable: a domicile column holds the flag and the code or the name.
