# Preact

> Tabler Flags for Preact offers country flags as tree-shakable components, so you can use them the same way as any other Preact component.

Add Tabler country flags to Preact apps with the official tree-shakable package. Render over 300 flags as components in four visual styles.

Source: https://docs.tabler.io/flags/libraries/preact

---

This is an alternative to the [CSS flags plugin](/flags/css) for Preact projects: instead of adding `flag`/`flag-country-*` classes to a `<span>`, you render a real Preact component. This gives you type-checked props, no separate CSS file to load, and tree-shaking - a page that only uses `FlagPoland` doesn't ship the SVG markup for the other 300 flags.

This package is pre-1.0: the API may still change before a 1.0 release.

## Installation

```shell
npm install @tabler/flags-preact
yarn add @tabler/flags-preact
pnpm install @tabler/flags-preact
bun install @tabler/flags-preact
```

`preact` is a peer dependency - the package reuses the Preact version already in your project instead of bundling its own.

## How to use

Every flag is exported as its own named component, following the pattern `Flag<CountryName>` - for example `FlagPoland`, `FlagUnitedStates` or `FlagEuropeanUnion`. A flag with an ISO code also has a short alias, `Flag<ISO>`, so `FlagPL` is the same component as `FlagPoland`. Import the one you need and render it like any other Preact component:

```jsx
import { FlagPoland } from '@tabler/flags-preact';

function Language() {
  return <FlagPoland variant="plain" size={32} />;
}
```

This works well when you know exactly which flags you want to show, and you write their names directly in your code - for example a fixed list of the languages your app supports.

### Rendering a flag by name at runtime

Sometimes the country isn't known ahead of time in your source code - for example, it's a country code coming from an API response, a user profile or a CMS field.

For this case, use the `Flag` component instead: pass the flag's slug or its lowercase ISO code as a `name` prop and it renders the right flag for you. The package also exports `flagsList`, an array of every slug, which is handy for a country select:

```jsx
import { Flag, flagsList } from '@tabler/flags-preact';

<Flag name="pl" size={24} />

{flagsList.map((slug) => (
  <Flag key={slug} name={slug} size={24} />
))}
```

The slug is the component name without the `Flag` prefix, in kebab-case - for example `FlagUnitedStates` is `"united-states"`. The ISO code is the two-letter ISO 3166-1 alpha-2 code in lower case, like `"us"`. Both work for any of the 300+ flags.

If `name` doesn't match a known slug or code, `Flag` renders nothing rather than throwing - useful when the data driving it isn't fully trusted (for example, a country code typed by hand in a CMS).

## Props

Both the named flag components (`FlagPoland`, `FlagPL`, ...) and the dynamic `Flag` component accept the same set of props. Quick reference:

| name      | type                                                 | default   | required    |
| --------- | ---------------------------------------------------- | --------- | ----------- |
| `variant` | `'rounded' \| 'plain' \| 'gradient' \| 'shiny'`      | `rounded` | no          |
| `size`    | `string \| number`                                   | `24`      | no          |
| `title`   | `string`                                             | -         | no          |
| `name`    | `string`                                             | -         | `Flag` only |

### `variant`

Every flag ships in four pre-baked visual styles. `rounded` is the default and matches the [CSS plugin](/flags/css): rounded corners and a thin inset border. `plain` is the bare flag, for when you draw your own frame. `gradient` and `shiny` add a soft shading on top of the flag. You choose explicitly; there is no automatic switch between them:

```jsx
<FlagPoland variant="rounded" />  {/* default, like the CSS plugin */}
<FlagPoland variant="plain" />
<FlagPoland variant="gradient" />
<FlagPoland variant="shiny" />
```

### `size`

Sets the rendered SVG **height**, in pixels. The width is calculated for you from the flag's fixed 5:4 aspect ratio (`width = size * 1.25`, rounded to 2 decimals), so a flag never looks stretched or squashed no matter what size you pick:

```jsx
<FlagPoland size={24} /> {/* 24px tall, 30px wide (the default) */}
<FlagPoland size={48} /> {/* 48px tall, 60px wide */}
```

If you need a specific width instead - for example to fit a fixed-width column - pass `width` directly. An explicit `width` prop overrides the calculated one, while `size` still controls the height:

```jsx
<FlagPoland size={24} width={40} /> {/* 24px tall, but exactly 40px wide */}
```

### `title`

Adds an accessible `<title>` element as the first child inside the SVG, which screen readers announce when they reach the flag. This does **not** happen automatically - without `title` (or an `aria-label` on a wrapping element), the flag is invisible to assistive technology, which is correct when it's purely decorative but wrong when it's the only way to tell the country:

```jsx
<FlagPoland title="Poland" />
```

See the [Accessibility](/flags/css#accessibility) section on the CSS plugin page for concrete guidance on when a flag needs this and when it should stay `aria-hidden` instead.

### Everything else

Any other prop you pass - `class`, `onClick`, `style`, `data-*`, `aria-*`, and so on - is forwarded as-is to the root `<svg>` element, exactly like it would be on a plain HTML element:

```jsx
<FlagPoland class="rounded shadow-sm" onClick={() => selectCountry('pl')} data-testid="flag-pl" />
```

Every rendered flag also gets the classes `tabler-flag` and `tabler-flag-<slug>`, so you can style all flags, or one flag, from your stylesheet.

### `name` (dynamic `Flag` component only)

Required when using `Flag` instead of a named import. Takes the flag's slug (`"poland"`) or its lowercase ISO code (`"pl"`) as a string. Any of the 300+ flags works here, not just the ones shown in the examples on this page - see the [repository README](https://github.com/tabler/tabler-flags#flags) for the full list. See [Rendering a flag by name at runtime](#rendering-a-flag-by-name-at-runtime) above.

## Available flags

There are over 300 flags, each with a `Flag<CountryName>` component and, where the flag has an ISO code, a `Flag<ISO>` alias. See the [repository README](https://github.com/tabler/tabler-flags#flags) for the full list of names, slugs and ISO codes.
