# FlagIcon

Flag of a country, from the `flag-icons` set: 4:3 by default, square with `square`. `alpha2` is the ISO 3166-1 alpha-2 code in any case (`IT`, `it`); the set also has a few other regions, such as `eu`, `un` and `gb-sct`. The flag is as tall as `--flag-icon-size` (by default a bit taller than the surrounding text). Without `title` it is decorative and hidden from screen readers. The flags are SVG files of the `flag-icons` package, loaded by its CSS: by default Vite inlines the smaller ones (about 320 KB) in the app's CSS; to download each flag only when it is shown, set `build: { assetsInlineLimit: (file) => (file.includes('/flag-icons/') ? false : undefined) }` in `vite.config`.

```ts
import { FlagIcon } from '@likable-hair/svelte';
```

Category: simple. Source: `src/lib/components/simple/media/FlagIcon.svelte`.

## Examples

### Flags

`alpha2` is the ISO code of the country. Flags are 4:3 by default and square with `square`; `--flag-icon-size` sets the height, which otherwise follows the text. With `title` the flag has an accessible name, otherwise it is decorative.

```svelte
<script lang="ts">
	import { FlagIcon } from '@likable-hair/svelte';
</script>

<div class="demo">
	<div class="row">
		{#each ['IT', 'FR', 'DE', 'ES', 'GB', 'JP', 'BR', 'CA'] as code (code)}
			<FlagIcon alpha2={code} title={code} --flag-icon-size="26px" />
		{/each}
	</div>
	<div class="row">
		{#each ['it', 'fr', 'de', 'es', 'gb', 'jp', 'br', 'ca'] as code (code)}
			<FlagIcon alpha2={code} square --flag-icon-size="26px" />
		{/each}
	</div>
	<p>Shipping to <FlagIcon alpha2="CH" /> Switzerland and <FlagIcon alpha2="AT" /> Austria.</p>
</div>

<style>
	.demo {
		display: flex;
		flex-direction: column;
		gap: 16px;
	}

	.row {
		display: flex;
		flex-wrap: wrap;
		gap: 12px;
	}

	p {
		margin: 0;
	}
</style>
```

### Round flags in chips

A square flag with `--flag-icon-border-radius: 50%` becomes round, for example in a language picker built with `Chip` and its `prependSnippet`.

```svelte
<script lang="ts">
	import { Chip, FlagIcon } from '@likable-hair/svelte';

	const LANGUAGES = [
		{ code: 'it', flag: 'it', name: 'Italiano' },
		{ code: 'en', flag: 'gb', name: 'English' },
		{ code: 'es', flag: 'es', name: 'Español' }
	];

	let language = $state('it');
</script>

<div class="demo">
	{#each LANGUAGES as lang (lang.code)}
		<Chip selected={language === lang.code} onclick={() => (language = lang.code)}>
			{#snippet prependSnippet()}
				<FlagIcon
					alpha2={lang.flag}
					square
					--flag-icon-size="18px"
					--flag-icon-border-radius="50%"
				/>
			{/snippet}
			{lang.name}
		</Chip>
	{/each}
</div>

<style>
	.demo {
		display: flex;
		flex-wrap: wrap;
		gap: 8px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `alpha2` (required) | `string` |  | ISO 3166-1 alpha-2 code of the country, in any case (`IT`, `it`). The aliases of version 4 still work: `uk`, `el`, `xs`, `xi`, `ac`, `ta`. |
| `square` | `boolean` | `false` | Shows the square (1:1) version of the flag instead of the 4:3 one. |
| `title` | `string` |  | Accessible name, for example the country name. Without it the flag is decorative and hidden from screen readers. |
| `class` | `string` | `''` | Extra classes on the `<span>` element. |

## CSS variables

For a single instance, pass them as style props: `<FlagIcon --flag-icon-size="…">`. The `--flag-icon-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--flag-icon-size` | `--flag-icon-default-size`: `1.2em` |
| `--flag-icon-border-radius` | `--flag-icon-default-border-radius`: `calc(var(--global-radius-xs) * 0.75)` |
| `--flag-icon-box-shadow` | `--flag-icon-default-box-shadow`: `0 0 0 1px var(--global-color-border)` |
