# SplitButton

The action the moment calls for is the button; the others are in the caret's menu. The caller picks the default and the button never changes what a click does without its label saying so.

## The default action

`primary` names the default action; `size` matches the buttons beside it. The caller moves the default as the work moves on; the label follows.

## Disabled actions

A disabled action's `hint` says why, in the app tooltip; the menu's arrow keys skip it.

## Keyboard

The caret's menu works from the keyboard alone.

- `Enter`, `Space` or `↓` on the caret opens the menu on its first enabled item; `↑` on its last
- `↓` `↑` move between the enabled items, wrapping
- `Home` `End` go to the first and last
- `Esc` closes it and returns to the caret; `Tab` closes it

## Examples

```svelte
<script lang="ts">
  import SplitButton from '@qualia-studios/design-ui/components/SplitButton.svelte';
  import { SplitAction } from '@qualia-studios/design-ui/components/SplitButton.svelte';
  import { generalIcons } from '@qualia-studios/design-ui/icons';

  let last = $state('nothing yet');
  const did = (what: string) => () => (last = what);
  const actions: SplitAction[] = [
    { key: 'publish', label: 'Publish', onclick: did('Publish') },
    { key: 'review', label: 'Review', onclick: did('Review') },
    {
      key: 'download',
      label: 'Download',
      icon: generalIcons.download,
      onclick: did('Download'),
    },
  ];
  const gated: SplitAction[] = [
    {
      key: 'publish',
      label: 'Publish',
      disabled: true,
      hint: 'Review the episodes before publishing them',
      onclick: did('Publish'),
    },
    { key: 'review', label: 'Review', onclick: did('Review') },
    {
      key: 'download',
      label: 'Download',
      disabled: true,
      hint: 'No version yet: publish one to download it.',
      onclick: did('Download'),
    },
    { key: 'copy', label: 'Copy ID', onclick: did('Copy ID') },
  ];
  let primary = $state('publish');
</script>
```

### Example 1: Two sizes

Last run: nothing yet

```svelte
<SplitButton {actions} primary="publish" label="Dataset actions" />
<SplitButton
  {actions}
  primary="publish"
  label="Dataset actions"
  size="normal"
/>
```

### Example 2: Moving the default

Pick a default; the button's label follows.

```svelte
<SplitButton {actions} {primary} label="Dataset actions" />
{#each actions as a (a.key)}
  <label class="pick">
    <input type="radio" bind:group={primary} value={a.key} />
    {a.label}
  </label>
{/each}
```

### Example 3: Disabled actions with hints

Open the menu and hover a disabled action for why.

```svelte
<SplitButton actions={gated} primary="publish" label="Dataset actions" />
```

---

From the Qualia design guide: https://q-core-3b2732.gitlab.io/components/splitbutton. Every page as markdown: https://q-core-3b2732.gitlab.io/llms.txt
