# ActivableButton

Toggle button that stays pressed: every click flips `active` and then calls `onclick`, and the state is announced with `aria-pressed`. It is a `Button` with its own look for the two states (neutral when off, primary tint when on), so `size`, `icon`, `loading`, `disabled` and the other Button props work the same way. Buttons with text and no icon show a dot that lights up when active (`dot={false}` hides it); icon-only buttons, as in a text editor toolbar, need an `aria-label`. To drive `active` from outside (for example from the editor state), pass it without `bind:` and update it in `onclick`. Its state is exposed as `data-active` for app CSS.

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

Category: composed. Source: `src/lib/components/composed/buttons/ActivableButton.svelte`.

## Examples

### Filters

Every click flips `active` (here with `bind:active`) and the dot lights up; the state is announced with `aria-pressed`. `size` and the other Button props work as usual.

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

	let onlyActive = $state(true);
	let favorites = $state(false);
</script>

<div class="demo">
	<div class="row">
		<ActivableButton bind:active={onlyActive}>Only active</ActivableButton>
		<ActivableButton bind:active={favorites}>Favorites</ActivableButton>
		<ActivableButton size="sm" dot={false}>No dot</ActivableButton>
		<ActivableButton disabled>Disabled</ActivableButton>
	</div>
	<p>Only active: <strong>{onlyActive}</strong> · Favorites: <strong>{favorites}</strong></p>
</div>

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

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

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

### Toolbar

Icon-only toggles, as in a text editor, need an `aria-label`. Here `active` is driven from outside, without `bind:`: `onclick` updates the formatting state and the buttons follow it.

```svelte
<script lang="ts">
	import { mdiFormatBold, mdiFormatItalic, mdiFormatUnderline, mdiStar } from '@mdi/js';
	import { ActivableButton } from '@likable-hair/svelte';

	let format = $state({ bold: true, italic: false, underline: false });
	let starred = $state(true);
</script>

<div class="row">
	<div class="toolbar" role="toolbar" aria-label="Formatting">
		<ActivableButton
			icon={mdiFormatBold}
			aria-label="Bold"
			size="sm"
			active={format.bold}
			onclick={() => (format.bold = !format.bold)}
		/>
		<ActivableButton
			icon={mdiFormatItalic}
			aria-label="Italic"
			size="sm"
			active={format.italic}
			onclick={() => (format.italic = !format.italic)}
		/>
		<ActivableButton
			icon={mdiFormatUnderline}
			aria-label="Underline"
			size="sm"
			active={format.underline}
			onclick={() => (format.underline = !format.underline)}
		/>
	</div>
	<ActivableButton icon={mdiStar} aria-label="Starred" bind:active={starred} />
	<p class:bold={format.bold} class:italic={format.italic} class:underline={format.underline}>
		Sample text
	</p>
</div>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 16px;
	}

	.toolbar {
		display: flex;
		gap: 4px;
	}

	p {
		margin: 0;
	}

	.bold {
		font-weight: 700;
	}

	.italic {
		font-style: italic;
	}

	.underline {
		text-decoration: underline;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `active` (bindable) | `boolean` | `false` | Pressed state. A click flips it before `onclick` runs. |
| `dot` | `boolean` | `true` | Shows the state dot before the text. It appears only on buttons with text and no icon. |

### From Button

It accepts the props, snippets and events of Button (`/components/button.md`), listed in the tables "From Button". Except `variant`, `buttonType`.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Height, padding and text size: `sm` 28px, `md` 34px, `lg` 40px. |
| `type` | `'button' \| 'submit' \| 'reset'` | `'button'` | Native `type`. Defaults to `button`, so it never submits a form by accident. Ignored with `href`. |
| `href` | `string` |  | Renders an `<a>` link to this URL instead of a `<button>`, with the same look. |
| `target` | `'_self' \| '_blank' \| '_parent' \| '_top' \| (string & {})` |  | Native `target` of the link (with `href`). With `_blank`, `rel` defaults to `noopener noreferrer`. |
| `rel` | `string` |  | Native `rel` of the link (with `href`). |
| `download` | `string \| boolean` |  | Native `download` of the link (with `href`). |
| `loading` | `boolean` | `false` | Shows a spinner in place of the icon (or before the text) and disables the button. |
| `icon` | `string` |  | SVG path of the icon, for example from `@mdi/js`. With text it sits on the left; alone it makes the button square. |
| `disabled` | `boolean` | `false` | Native `disabled`: the button gets neither focus nor events. A link (`href`) loses its `href` and gets `aria-disabled`. |
| `buttonElement` (bindable) | `HTMLButtonElement \| HTMLAnchorElement` |  | The native element: `<button>`, or `<a>` with `href`. |
| `appendIcon` | `string` |  | SVG path of an icon after the text. When the button is wider than its content, it sits at the right edge. |

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

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `dotSnippet` | `{ active: boolean }` | Replaces the state dot. |

### From Button

| Name | Parameters | Description |
| --- | --- | --- |
| `children` |  | Button label. |
| `iconSnippet` |  | Replaces the icon with any content (an image, an avatar, a custom SVG). Without `children` it makes the button square, like `icon`. |
| `loadingSnippet` |  | Replaces the loading spinner. |
| `appendSnippet` |  | Replaces `appendIcon` with any content after the text (a chevron, a counter, a shortcut). |

## CSS variables

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

It also takes the variables of Button (`/components/button.md`).

| Per-instance variable | Default |
| --- | --- |
| `--activable-button-background` | `--activable-button-default-background`: `var(--global-color-surface-2)` |
| `--activable-button-color` | `--activable-button-default-color`: `var(--global-color-text-2)` |
| `--activable-button-border-color` | `--activable-button-default-border-color`: `var(--global-color-border)` |
| `--activable-button-hover-background` | `--activable-button-default-hover-background`: `var(--global-color-surface-3)` |
| `--activable-button-hover-color` | `--activable-button-default-hover-color`: `var(--global-color-text)` |
| `--activable-button-hover-border-color` | `--activable-button-default-hover-border-color`: `var(--global-color-border-strong)` |
| `--activable-button-dot-color` | `--activable-button-default-dot-color`: `var(--global-color-text-3)` |
| `--activable-button-active-background` | `--activable-button-default-active-background`: `var(--global-color-primary-soft)` |
| `--activable-button-active-color` | `--activable-button-default-active-color`: `var(--global-color-primary)` |
| `--activable-button-active-border-color` | `--activable-button-default-active-border-color`: `color-mix(in oklab, var(--global-color-primary) 50%, transparent)` |
| `--activable-button-active-hover-background` | `--activable-button-default-active-hover-background`: `color-mix(in oklab, var(--global-color-primary) 18%, transparent)` |
| `--activable-button-active-hover-color` | `--activable-button-default-active-hover-color`: `var(--global-color-primary)` |
| `--activable-button-active-hover-border-color` | `--activable-button-default-active-hover-border-color`: `var(--global-color-primary)` |
| `--activable-button-active-dot-color` | `--activable-button-default-active-dot-color`: `var(--global-color-primary)` |
| `--activable-button-active-dot-box-shadow` | `--activable-button-default-active-dot-box-shadow`: `0 0 0 3px color-mix(in oklab, var(--global-color-primary) 25%, transparent)` |
| `--activable-button-dot-size` | `--activable-button-default-dot-size`: `7px` |
