# Chip

Compact label for tags, statuses, filters and selections, with seven color variants (`variant`) and two sizes (`size`). With `onclick` its content becomes a native `<button>`; with `selected` as well it is a toggle (`aria-pressed`) that takes the selected look. `closable` adds a separate close button that calls `onclose`. Long text is cut with an ellipsis when the chip has no room, or past `--chip-max-width`. Its state is exposed as `data-variant`, `data-size`, `data-selected` and `data-disabled` for app CSS.

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

Category: simple. Source: `src/lib/components/simple/navigation/Chip.svelte`.

## Examples

### Variants and sizes

Seven colors and two sizes. `prependIcon` takes an SVG path; `prependSnippet` takes any content, here a pulsing status dot.

```svelte
<script lang="ts">
	import { mdiCheck, mdiLightningBolt, mdiTagOutline } from '@mdi/js';
	import { Chip } from '@likable-hair/svelte';
</script>

<div class="rows">
	<div class="row">
		<Chip prependIcon={mdiTagOutline}>Default</Chip>
		<Chip variant="primary">VIP</Chip>
		<Chip variant="accent">
			{#snippet prependSnippet()}<span class="dot"></span>{/snippet}
			Online
		</Chip>
		<Chip variant="success" prependIcon={mdiCheck}>Paid</Chip>
		<Chip variant="warning">Pending</Chip>
		<Chip variant="error">Expired</Chip>
		<Chip variant="filled" prependIcon={mdiLightningBolt}>Pro</Chip>
	</div>
	<div class="row">
		<Chip size="sm">10′</Chip>
		<Chip size="sm" variant="primary">35′</Chip>
		<Chip size="sm" variant="success">Confirmed</Chip>
		<Chip size="sm" variant="error">Cancelled</Chip>
	</div>
</div>

<style>
	.rows {
		display: grid;
		gap: 14px;
	}

	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 8px;
	}

	.dot {
		width: 6px;
		height: 6px;
		border-radius: 50%;
		background: currentColor;
		animation: pulse 1.8s infinite;
	}

	@keyframes pulse {
		from {
			box-shadow: 0 0 0 0 color-mix(in srgb, currentColor 60%, transparent);
		}
		to {
			box-shadow: 0 0 0 8px transparent;
		}
	}

	@media (prefers-reduced-motion: reduce) {
		.dot {
			animation: none;
		}
	}
</style>
```

### Toggle chips

With `onclick` the chip is a `<button>`; adding `selected` makes it a toggle with `aria-pressed`, and the selected one takes the `filled` look. The parent keeps the selection.

```svelte
<script lang="ts">
	import { mdiContentCut, mdiHairDryerOutline, mdiPalette, mdiSpa } from '@mdi/js';
	import { Chip } from '@likable-hair/svelte';

	const services = [
		{ value: 'cut', label: 'Cut', icon: mdiContentCut },
		{ value: 'color', label: 'Color', icon: mdiPalette },
		{ value: 'blow-dry', label: 'Blow-dry', icon: mdiHairDryerOutline },
		{ value: 'treatment', label: 'Treatment', icon: mdiSpa }
	];

	let selected = $state<string[]>(['color']);

	function toggle(value: string) {
		selected = selected.includes(value)
			? selected.filter((v) => v !== value)
			: [...selected, value];
	}
</script>

<div class="toggle">
	<div class="row" role="group" aria-label="Services">
		{#each services as service (service.value)}
			<Chip
				prependIcon={service.icon}
				selected={selected.includes(service.value)}
				onclick={() => toggle(service.value)}
			>
				{service.label}
			</Chip>
		{/each}
	</div>
	<p>Selected: {selected.join(', ') || 'none'}</p>
</div>

<style>
	.toggle {
		display: grid;
		gap: 12px;
	}

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

	p {
		margin: 0;
		font-size: 13px;
	}
</style>
```

### Removable chips

`closable` adds a separate close button named by `closeLabel`; `onclose` receives its click. Text that does not fit is cut with an ellipsis: `--chip-max-width` sets a cap.

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

	const initial = ['Cut', 'Balayage', 'Keratin treatment with deep conditioning mask', 'Blow-dry'];
	let tags = $state([...initial]);
</script>

<div class="row">
	{#each tags as tag (tag)}
		<Chip
			variant="primary"
			closable
			closeLabel="Remove {tag}"
			title={tag}
			onclose={() => (tags = tags.filter((t) => t !== tag))}
			--chip-max-width="200px"
		>
			{tag}
		</Chip>
	{:else}
		<Button size="sm" variant="secondary" onclick={() => (tags = [...initial])}>Restore</Button>
	{/each}
</div>

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

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `'neutral' \| 'primary' \| 'accent' \| 'success' \| 'warning' \| 'error' \| 'filled'` | `'neutral'` | Color. `filled` is the strongest, for a highlighted chip. |
| `size` | `'sm' \| 'md'` | `'md'` | Height, padding and text size: `sm` 22px, `md` 26px. |
| `selected` | `boolean` |  | Selected state of a toggle chip: with `onclick` it sets `aria-pressed`. When `true` the chip takes the selected look, by default the same as `filled`. |
| `disabled` | `boolean` | `false` | Disables the chip button and the close button. |
| `prependIcon` | `string` |  | SVG path of an icon before the text, for example from `@mdi/js`. |
| `closable` | `boolean` | `false` | Shows a close button after the text, which calls `onclose`. |
| `closeIcon` | `string` | `CLOSE_ICON` | SVG path of the close button icon. Defaults to the MDI `close` icon. |
| `closeLabel` | `string` |  | Accessible name of the close button. Defaults to "Remove", 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`. |
| `tabindex` | `number` |  | Native `tabindex` of the chip button and of the close button: `-1` keeps both out of the tab order. |
| `chipElement` (bindable) | `HTMLSpanElement` |  | The root `<span>` element. |

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

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `children` |  | Chip text. |
| `prependSnippet` |  | Replaces the icon before the text with any content (a status dot, an avatar). |
| `closeSnippet` | `{ close: (event: MouseEvent) => void; closeLabel: string }` | Replaces the close button. `close` calls `onclose`. Shown even without `closable`. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onclick` | `(event: MouseEvent) => void` | Called on click. With it the chip content becomes a native `<button>`. |
| `onclose` | `(event: MouseEvent) => void` | Called on a click on the close button. |

## CSS variables

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

| Per-instance variable | Default |
| --- | --- |
| `--chip-icon-size` | `--chip-default-icon-size`: `15px`<br>`--chip-default-sm-icon-size`: `14px` |
| `--chip-max-width` | `--chip-default-max-width`: `100%` |
| `--chip-height` | `--chip-default-height`: `26px`<br>`--chip-default-sm-height`: `22px` |
| `--chip-border-color` | `--chip-default-neutral-border-color`: `var(--global-color-border)`<br>`--chip-default-primary-border-color`: `color-mix(in oklab, var(--global-color-primary) 30%, transparent)`<br>`--chip-default-accent-border-color`: `color-mix(in oklab, var(--global-color-accent) 30%, transparent)`<br>`--chip-default-success-border-color`: `transparent`<br>`--chip-default-warning-border-color`: `transparent`<br>`--chip-default-error-border-color`: `transparent`<br>`--chip-default-filled-border-color`: `transparent` |
| `--chip-background` | `--chip-default-neutral-background`: `var(--global-color-surface-2)`<br>`--chip-default-primary-background`: `var(--global-color-primary-soft)`<br>`--chip-default-accent-background`: `var(--global-color-accent-soft)`<br>`--chip-default-success-background`: `var(--global-color-success-soft)`<br>`--chip-default-warning-background`: `var(--global-color-warning-soft)`<br>`--chip-default-error-background`: `var(--global-color-error-soft)`<br>`--chip-default-filled-background`: `var(--global-fill-gradient)` |
| `--chip-color` | `--chip-default-neutral-color`: `var(--global-color-text)`<br>`--chip-default-primary-color`: `var(--global-color-primary)`<br>`--chip-default-accent-color`: `var(--global-color-accent)`<br>`--chip-default-success-color`: `var(--global-color-success)`<br>`--chip-default-warning-color`: `var(--global-color-warning)`<br>`--chip-default-error-color`: `var(--global-color-error)`<br>`--chip-default-filled-color`: `var(--global-color-on-primary)` |
| `--chip-box-shadow` | `--chip-default-neutral-box-shadow`: `0 0 #0000`<br>`--chip-default-primary-box-shadow`: `0 0 #0000`<br>`--chip-default-accent-box-shadow`: `0 0 #0000`<br>`--chip-default-success-box-shadow`: `0 0 #0000`<br>`--chip-default-warning-box-shadow`: `0 0 #0000`<br>`--chip-default-error-box-shadow`: `0 0 #0000`<br>`--chip-default-filled-box-shadow`: `0 6px 16px -6px var(--global-color-primary-glow)` |
| `--chip-border-width` | `--chip-default-border-width`: `var(--global-border-width)` |
| `--chip-border-radius` | `--chip-default-border-radius`: `var(--global-radius-sm)` |
| `--chip-font-family` | `--chip-default-font-family`: `inherit` |
| `--chip-font-size` | `--chip-default-font-size`: `12.5px`<br>`--chip-default-sm-font-size`: `12px` |
| `--chip-font-weight` | `--chip-default-font-weight`: `500` |
| `--chip-selected-border-color` | `--chip-default-selected-border-color`: `var(--chip-default-filled-border-color)` |
| `--chip-selected-hover-border-color` | `--chip-default-selected-hover-border-color`: `var(--chip-default-filled-hover-border-color)` |
| `--chip-selected-background` | `--chip-default-selected-background`: `var(--chip-default-filled-background)` |
| `--chip-selected-color` | `--chip-default-selected-color`: `var(--chip-default-filled-color)` |
| `--chip-selected-box-shadow` | `--chip-default-selected-box-shadow`: `var(--chip-default-filled-box-shadow)` |
| `--chip-gap` | `--chip-default-gap`: `6px` |
| `--chip-padding` | `--chip-default-padding`: `0 10px`<br>`--chip-default-sm-padding`: `0 8px` |
| `--chip-close-margin-end` | `--chip-default-close-margin-end`: `4px` |
| `--chip-close-icon-size` | `--chip-default-close-icon-size`: `14px` |
| `--chip-close-size` | `--chip-default-close-size`: `18px` |
| `--chip-close-border-radius` | `--chip-default-close-border-radius`: `var(--global-radius-full)` |
| `--chip-close-color` | `--chip-default-close-color`: `color-mix(in oklab, currentColor 70%, transparent)` |
| `--chip-hover-border-color` | `--chip-default-neutral-hover-border-color`: `var(--global-color-border-strong)`<br>`--chip-default-primary-hover-border-color`: `color-mix(in oklab, var(--global-color-primary) 55%, transparent)`<br>`--chip-default-accent-hover-border-color`: `color-mix(in oklab, var(--global-color-accent) 55%, transparent)`<br>`--chip-default-success-hover-border-color`: `color-mix(in oklab, var(--global-color-success) 40%, transparent)`<br>`--chip-default-warning-hover-border-color`: `color-mix(in oklab, var(--global-color-warning) 40%, transparent)`<br>`--chip-default-error-hover-border-color`: `color-mix(in oklab, var(--global-color-error) 40%, transparent)`<br>`--chip-default-filled-hover-border-color`: `transparent` |
| `--chip-hover-background` |  |
| `--chip-hover-color` |  |
| `--chip-hover-transform` | `--chip-default-hover-transform`: `translateY(-1px)` |
| `--chip-close-hover-background` | `--chip-default-close-hover-background`: `color-mix(in oklab, currentColor 14%, transparent)` |
| `--chip-close-hover-color` | `--chip-default-close-hover-color`: `currentColor` |
| `--chip-focus-ring-width` | `--chip-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--chip-focus-ring-color` | `--chip-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--chip-focus-ring-offset` | `--chip-default-focus-ring-offset`: `var(--global-focus-ring-offset)` |
| `--chip-close-focus-ring-offset` | `--chip-default-close-focus-ring-offset`: `0px` |
| `--chip-disabled-opacity` | `--chip-default-disabled-opacity`: `0.4` |
