# AlertBanner

Message banner inside the page, with a colored icon, a title, a description, an optional action on the right (`appendSnippet`) and an optional close button. Four variants: `info`, `success`, `warning`, `error`. It is announced by screen readers when it appears: `role="alert"` for warnings and errors, `role="status"` for the others (pass `role` to change it). The close button only calls `onclose`: the app removes the banner. Its variant is exposed as `data-variant` for app CSS.

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

Category: simple. Source: `src/lib/components/simple/notifiers/AlertBanner.svelte`.

## Examples

### Variants

Four variants with their own color and icon. `closable` adds a close button that calls `onclose`, where the app removes the banner; `appendSnippet` puts an action on the right.

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

	let dismissed = $state<string[]>([]);
	const dismiss = (id: string) => (dismissed = [...dismissed, id]);
</script>

<div class="stack">
	{#if !dismissed.includes('update')}
		<AlertBanner
			title="New version available"
			description="Aurora UI 5.0 follows the light or dark mode of the device."
			closable
			onclose={() => dismiss('update')}
		/>
	{/if}
	{#if !dismissed.includes('confirmed')}
		<AlertBanner
			variant="success"
			title="Appointment confirmed"
			description="We sent Giulia a reminder by text message."
			closable
			onclose={() => dismiss('confirmed')}
		/>
	{/if}
	<AlertBanner variant="warning" title="Low stock" description="3 products are below the minimum level.">
		{#snippet appendSnippet()}
			<Button variant="secondary" size="sm">Reorder</Button>
		{/snippet}
	</AlertBanner>
	{#if !dismissed.includes('payment')}
		<AlertBanner
			variant="error"
			title="Payment failed"
			description="The card was declined by the issuer."
			closable
			onclose={() => dismiss('payment')}
		/>
	{/if}
	{#if dismissed.length}
		<div>
			<Button buttonType="text" size="sm" onclick={() => (dismissed = [])}>Show all again</Button>
		</div>
	{/if}
</div>

<style>
	.stack {
		display: grid;
		gap: 12px;
		width: 100%;
	}
</style>
```

### Custom content and colors

`children` replaces the title and the description with any content. `icon=""` removes the icon box. The colors are CSS variables, here for a promotional banner in the accent color.

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

<div class="stack">
	<AlertBanner variant="warning" icon="">
		<strong>Before you close the day:</strong>
		<ul>
			<li>2 appointments are still unpaid</li>
			<li>the cash count is missing</li>
		</ul>
	</AlertBanner>
	<AlertBanner
		icon={mdiGiftOutline}
		title="Spring offer"
		description="Clients who book a colour this month get a free gloss."
		--alert-banner-background="var(--global-color-accent-soft)"
		--alert-banner-border-color="color-mix(in oklab, var(--global-color-accent) 30%, transparent)"
		--alert-banner-icon-background="var(--global-color-accent)"
		--alert-banner-icon-color="var(--global-color-on-primary)"
	/>
</div>

<style>
	.stack {
		display: grid;
		gap: 12px;
		width: 100%;
		font-size: 13px;
	}

	ul {
		margin: 4px 0 0;
		padding-left: 18px;
		color: var(--global-color-text-2);
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `'info' \| 'success' \| 'warning' \| 'error'` | `'info'` | Color and default icon. |
| `title` | `string` |  | Bold first line. |
| `description` | `string` |  | Text under the title. Line breaks are kept. |
| `icon` | `string` |  | SVG path of the icon. Defaults to an icon for each variant; an empty string removes it. |
| `closable` | `boolean` | `false` | Shows a close button that calls `onclose`. |
| `closeLabel` | `string` |  | Accessible name of the close button. Defaults to "Dismiss", in the language of `locale`. |
| `locale` | `string` |  | BCP 47 locale of the texts: `it` and `en` are built in, other languages fall back to English. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `class` | `{ container?: string; icon?: string; body?: string; title?: string; description?: string; }` | `{}` | Extra classes for each part: the root, the icon box, the text block, the title and the description. |

It also accepts the native attributes and events of `<div>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `children` |  | Replaces the title and the description. |
| `titleSnippet` | `{ title: string \| undefined }` | Replaces the title. |
| `descriptionSnippet` | `{ description: string \| undefined }` | Replaces the description. |
| `iconSnippet` | `{ variant: 'info' \| 'success' \| 'warning' \| 'error' }` | Replaces the icon inside the colored box. |
| `appendSnippet` |  | Content on the right, for example an action button. |
| `closeSnippet` | `{ close: (event: MouseEvent) => void; closeLabel: string }` | Replaces the close button. `close` calls `onclose`. Shown even without `closable`. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onclose` | `(event: MouseEvent) => void` | Called on a click on the close button. Remove the banner here. |

## CSS variables

For a single instance, pass them as style props: `<AlertBanner --alert-banner-background="tomato">`. The `--alert-banner-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--alert-banner-gap` | `--alert-banner-default-gap`: `12px` |
| `--alert-banner-width` | `--alert-banner-default-width`: `100%` |
| `--alert-banner-padding` | `--alert-banner-default-padding`: `12px 14px` |
| `--alert-banner-border-width` | `--alert-banner-default-border-width`: `var(--global-border-width)` |
| `--alert-banner-border-color` | `--alert-banner-default-info-border-color`: `color-mix(in oklab, var(--global-color-primary) 30%, transparent)`<br>`--alert-banner-default-success-border-color`: `color-mix(in oklab, var(--global-color-success) 30%, transparent)`<br>`--alert-banner-default-warning-border-color`: `color-mix(in oklab, var(--global-color-warning) 30%, transparent)`<br>`--alert-banner-default-error-border-color`: `color-mix(in oklab, var(--global-color-error) 30%, transparent)` |
| `--alert-banner-border-radius` | `--alert-banner-default-border-radius`: `var(--global-radius-lg)` |
| `--alert-banner-background` | `--alert-banner-default-info-background`: `var(--global-color-primary-soft)`<br>`--alert-banner-default-success-background`: `var(--global-color-success-soft)`<br>`--alert-banner-default-warning-background`: `var(--global-color-warning-soft)`<br>`--alert-banner-default-error-background`: `var(--global-color-error-soft)` |
| `--alert-banner-box-shadow` | `--alert-banner-default-box-shadow`: `0 0 #0000` |
| `--alert-banner-line-height` | `--alert-banner-default-line-height`: `1.45` |
| `--alert-banner-icon-size` | `--alert-banner-default-icon-size`: `16px` |
| `--alert-banner-icon-box-size` | `--alert-banner-default-icon-box-size`: `28px` |
| `--alert-banner-icon-border-radius` | `--alert-banner-default-icon-border-radius`: `var(--global-radius-sm)` |
| `--alert-banner-icon-background` | `--alert-banner-default-info-icon-background`: `var(--global-color-primary)`<br>`--alert-banner-default-success-icon-background`: `var(--global-color-success)`<br>`--alert-banner-default-warning-icon-background`: `var(--global-color-warning)`<br>`--alert-banner-default-error-icon-background`: `var(--global-color-error)` |
| `--alert-banner-icon-color` | `--alert-banner-default-info-icon-color`: `var(--global-color-on-primary)`<br>`--alert-banner-default-success-icon-color`: `var(--global-color-on-success)`<br>`--alert-banner-default-warning-icon-color`: `var(--global-color-on-warning)`<br>`--alert-banner-default-error-icon-color`: `var(--global-color-on-error)` |
| `--alert-banner-title-color` | `--alert-banner-default-title-color`: `var(--global-color-text)` |
| `--alert-banner-title-font-size` | `--alert-banner-default-title-font-size`: `14px` |
| `--alert-banner-title-font-weight` | `--alert-banner-default-title-font-weight`: `600` |
| `--alert-banner-description-color` | `--alert-banner-default-description-color`: `var(--global-color-text-2)` |
| `--alert-banner-description-font-size` | `--alert-banner-default-description-font-size`: `13px` |
| `--alert-banner-close-icon-size` | `--alert-banner-default-close-icon-size`: `18px` |
| `--alert-banner-close-size` | `--alert-banner-default-close-size`: `28px` |
| `--alert-banner-close-border-radius` | `--alert-banner-default-close-border-radius`: `var(--global-radius-sm)` |
| `--alert-banner-close-color` | `--alert-banner-default-close-color`: `var(--global-color-text-3)` |
| `--alert-banner-close-hover-background` | `--alert-banner-default-close-hover-background`: `color-mix(in oklab, var(--global-color-text) 8%, transparent)` |
| `--alert-banner-close-hover-color` | `--alert-banner-default-close-hover-color`: `var(--global-color-text)` |
| `--alert-banner-focus-ring-width` | `--alert-banner-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--alert-banner-focus-ring-color` | `--alert-banner-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--alert-banner-focus-ring-offset` | `--alert-banner-default-focus-ring-offset`: `var(--global-focus-ring-offset)` |
