# Button components

Three related button components:

- **`ixfx-button`** — a styled, icon-capable replacement for `<button>`
- **`ixfx-split-button`** — a button paired with a dropdown caret; supports a static popup menu, a switcher mode, a popover (slotted, by `popover-target`, or built just in time), and programmatic item control
- **`ixfx-radio-group`** — a group of radio-style selectable items; also supports single toggle mode

---

## `ixfx-button`

```html
<ixfx-button>Save</ixfx-button>
<ixfx-button icon="check">Commit</ixfx-button>
<ixfx-button icon="check" hide-label>Commit</ixfx-button>
<ixfx-button disabled>Unavailable</ixfx-button>
<ixfx-button subtle>Subtle</ixfx-button>
```

### Properties / attributes

| Attribute / Property | Type    | Default | Description                                        |
|---------------------|---------|---------|----------------------------------------------------|
| `disabled`          | boolean | `false` | Disables the button                                |
| `hidden`           | boolean | `false` | Hides the button (uses `display: none`)            |
| `icon`              | string  | —       | Registered icon name **or** raw `<svg>` string     |
| `hide-icon`         | boolean | `false` | Hides the icon even when `icon` is set             |
| `hide-label`        | boolean | `false` | Hides the text label (useful for icon-only buttons)|
| `type`              | `'button' \| 'submit' \| 'reset'` | `'button'` | Inside a `<form>` (including `ixfx-form`), `submit` submits it (after validation) and `reset` resets it. The button is form-associated, so this works even though its inner `<button>` is in a shadow root |
| `subtle`            | boolean | `false` | Renders the background at 40% opacity, raised to 70% on hover/focus |

### Slots

| Slot    | Description              |
|---------|--------------------------|
| default | Button label text / HTML |

### Events

`ixfx-button` does not fire custom events. Listen for the standard `click` on the element.

```js
document.querySelector('ixfx-button').addEventListener('click', () => { ... });
```

### Icons

Icon names resolve against the icon registry. Built-in names: `check`, `chevron-down`, `chevron-right`, `close`, `caret-right`. Register custom icons with `registerIcon(name, svgString)` from `src/icons/registry.ts`.

You can also pass a raw SVG string directly to `icon`:

```js
btn.icon = '<svg viewBox="0 0 24 24" fill="currentColor"><path d="..."/></svg>';
```

### CSS custom properties

| Property            | Default  | Description             |
|---------------------|----------|-------------------------|
| `--surface-3`       | `#ccc`   | Button background       |
| `--surface-3-text`  | `#000`   | Button text / icon fill |
| `--accent`          | `#666`   | Hover border colour     |
| `--border`          | `1px solid #999` | Border         |
| `--radius-xl`       | `8px`    | Border radius           |
| `--space-m`         | `8px`    | Vertical padding        |
| `--space-l`         | `12px`   | Horizontal padding      |
| `--text-m`          | `14px`   | Font size               |
| `--transition`      | `0.2s`   | Hover/active transition |
| `--disabled-opacity`| `0.5`    | Opacity when disabled   |
| `--ixfx-button-hide-icon`  | `inline-flex` (icon shown) | Set to `none` to hide the icon. Overridden by the `hide-icon` attribute. |
| `--ixfx-button-hide-label` | `inline` (label shown)     | Set to `none` to hide the label. Overridden by the `hide-label` attribute. |
| `--ixfx-button-outline`    | `none`                     | Additional outline around the button (e.g. `2px solid var(--accent)`) |
| `--ixfx-button-shade`      | `none`                     | Tinted overlay via box-shadow (e.g. `inset 0 0 8px 2px color-mix(in oklch, red 35%, transparent)`) |

`--ixfx-button-hide-icon`/`--ixfx-button-hide-label` let you hide the icon/label conditionally from plain CSS — handy for a media or container query — without touching the `hide-icon`/`hide-label` attributes:

```css
/* Hide button labels once the viewport gets narrow */
@media (max-width: 600px) {
  ixfx-button {
    --ixfx-button-hide-label: none;
  }
}
```

```css
/* Or react to a container's own width */
.toolbar { container-type: inline-size; }
@container (max-width: 200px) {
  ixfx-button { --ixfx-button-hide-label: none; }
}
```

The `hide-icon`/`hide-label` attributes always win when set — the CSS variables only apply when the attribute is absent.

### Responsive buttons

Use `width: 100%` to make a button fill its container's width. Combine this with a container query to hide the label when space is tight:

```html
<div class="toolbar">
  <ixfx-button icon="check" style="width: 100%">Save</ixfx-button>
  <ixfx-button icon="close" style="width: 100%">Cancel</ixfx-button>
</div>
```

```css
.toolbar {
  container-type: inline-size;
}

@container (max-width: 120px) {
  ixfx-button {
    --ixfx-button-hide-label: none;
  }
}
```

The container query hides button labels once the container narrows past 120px. Resize the container to see labels appear/disappear.

---

## `ixfx-radio-group`

A group of radio-style selectable items, useful for selecting one option from a list. Also supports a **single toggle mode** when no `<option>` children are provided.

```html
<ixfx-radio-group name="waveform" value="sine">
  <option value="sine">Sine</option>
  <option value="square">Square</option>
  <option value="sawtooth">Saw</option>
  <option value="triangle">Tri</option>
</ixfx-radio-group>
```

### Single toggle mode

When no `<option>` elements are provided and text content is present, the component renders as a toggle button:

```html
<ixfx-radio-group name="toggle" checked>Enable Feature</ixfx-radio-group>
<ixfx-radio-group name="state" value="active" value-off="inactive" checked>Active/Inactive</ixfx-radio-group>
<ixfx-radio-group name="confirm" icon-name="check" icon-name-off="close" checked>On/Off</ixfx-radio-group>
```

### Properties / attributes

| Attribute / Property | Type                              | Default      | Description                                                    |
|---------------------|-----------------------------------|--------------|----------------------------------------------------------------|
| `value`             | string                            | `''`         | Currently selected value                                        |
| `checked`           | boolean                           | `false`      | For toggle mode: whether it is checked                         |
| `orientation`       | `'horizontal'` \| `'vertical'`    | `'horizontal'` | Layout direction                                             |
| `mode`              | `'compact'` \| `'max'`            | `'compact'`  | `compact`: items size to fit content. `max`: items stretch to fill container |
| `name`              | string                            | `''`         | Radio group name (used for native radio behavior)               |
| `value-off`         | string                            | `''`         | Value when toggle is in off state                              |
| `icon-name`         | string                            | `''`         | Icon name (from registry) shown when toggle is on / item selected |
| `icon-name-off`     | string                            | `''`         | Icon name shown when toggle is off                             |
| `hide-labels`       | boolean                           | `false`      | Hides item labels                                             |
| `wrap`              | boolean                           | `false`      | Wrap items onto additional rows (or columns if vertical) when space runs out. Rows sit flush unless `--item-spacing` is set |
| `items`             | `RadioItem[]`                     | `[]`         | Programmatically set items (alternative to `<option>` children) |

### Slots

| Slot      | Description                                      |
|-----------|--------------------------------------------------|
| default   | Text content for toggle mode                     |
| (children)| `<option>` elements defining radio items         |

### Events

| Event    | Detail                                      | Description                         |
|----------|---------------------------------------------|-------------------------------------|
| `change` | `{ checked: boolean, value: string \| undefined }` | Fired when selection changes |

```js
document.querySelector('ixfx-radio-group').addEventListener('change', e => {
  console.log(e.detail.value);   // e.g. 'sine'
  console.log(e.detail.checked);  // true
});
```

### Programmatic control

```js
const group = document.querySelector('ixfx-radio-group');

// Get/set selected value
group.value = 'square';

// For toggle mode
group.checked = true;

// Update items programmatically
group.items = [
  { value: 'alpha', label: 'Alpha' },
  { value: 'beta', label: 'Beta', disabled: true },
  { value: 'gamma', label: 'Gamma', iconName: 'check' },
];
```

### CSS custom properties

| Property               | Default                        | Description                              |
|------------------------|--------------------------------|------------------------------------------|
| `--surface-3`          | `transparent`                  | Background                               |
| `--border`             | `none`                         | Border                                   |
| `--radius-m`           | `4px`                          | Border radius                            |
| `--item-spacing`       | `0`                            | Gap between items                        |
| `--icon-label-spacing`  | `0.5rem`                       | Gap between icon and label               |
| `--icon-size`          | `1.2rem`                       | Icon dimensions                          |
| `--compact-padding`    | `0.5rem`                       | Padding in compact mode                  |
| `--space-m`            | `0.5rem`                       | Vertical item padding                    |
| `--space-l`            | `1rem`                         | Horizontal item padding                  |

### TypeScript types

```ts
import type { RadioGroupChangeDetail, RadioItem, RadioGroupMode } from 'ixfxfun-components';
```

---

## `ixfx-split-button`

A button split into a **primary action area** (left) and a **caret** (right) that opens a popup menu. Extends `ixfx-button` so all button properties and CSS variables apply.

```html
<ixfx-split-button>
  Save
  <ixfx-menu-container slot="menu">
    <ixfx-menu-item command="save-draft" label="Save as Draft"></ixfx-menu-item>
    <ixfx-menu-item command="save-copy"  label="Save a Copy"></ixfx-menu-item>
    <ixfx-menu-separator></ixfx-menu-separator>
    <ixfx-menu-item command="export"     label="Export…"></ixfx-menu-item>
  </ixfx-menu-container>
</ixfx-split-button>
```

### Properties / attributes

Inherits all `ixfx-button` properties, plus:

| Attribute / Property | Type            | Default     | Description                                                     |
|---------------------|-----------------|-------------|-----------------------------------------------------------------|
| `mode`              | `'default'` \| `'switcher'` | `'default'` | See [Modes](#modes)                        |
| `default-value`     | string          | `''`        | Command to pre-select in switcher mode                          |
| `items`             | `PopupItem[]`   | —           | Directly set popup items (ignored when `itemsProvider` is set)  |
| `itemsProvider`     | `() => PopupItem[] \| Promise<PopupItem[]>` | — | Called on each caret click; return value replaces all other item sources |
| `popover-target`    | string          | `''`        | Id of a `popover` element the caret toggles instead of the menu (see [Popover](#popover-instead-of-a-menu)) |
| `popoverProvider`   | `(popover: HTMLElement) => void \| Promise<void>` | — | Builds popover content just before each show |
| `registry`          | `CommandRegistry` | —         | Optional command registry forwarded to `ixfx-menu-trigger`      |
| `keyboardManager`   | `KeyboardManager` | —         | Optional keyboard manager forwarded to `ixfx-menu-trigger`      |

### Slots

| Slot      | Description                                                                   |
|-----------|-------------------------------------------------------------------------------|
| default   | Primary button label                                                          |
| `popover` | Content of a popover the caret toggles instead of the menu |
| `menu`    | `<ixfx-menu-container>` + items for the popup (default mode); or `<ixfx-menu-item>` elements as the data source in switcher mode |

### Events

| Event          | Detail                       | Description                                                                 |
|----------------|------------------------------|-----------------------------------------------------------------------------|
| `menu-command` | `{ command: string }`        | Fired when a menu item is selected, or when the primary button is clicked in switcher mode |

```js
element.addEventListener('menu-command', e => {
  console.log(e.detail.command); // e.g. 'save-draft'
});
```

The primary area does **not** fire `click` with a command — handle `menu-command` for both modes.

---

### Modes

#### `default` mode (the default)

Left side fires a plain `click`. Right caret opens the popup.

```html
<ixfx-split-button icon="check">
  Commit
  <ixfx-menu-container slot="menu">
    <ixfx-menu-item command="commit-push"  label="Commit & Push"></ixfx-menu-item>
    <ixfx-menu-item command="commit-only"  label="Commit Only"></ixfx-menu-item>
    <ixfx-menu-item command="amend"        label="Amend Last Commit"></ixfx-menu-item>
  </ixfx-menu-container>
</ixfx-split-button>
```

#### `switcher` mode

Selecting from the popup **replaces** the primary action and fires `menu-command` immediately. The replaced item returns to the popup. The caret shows a right-facing chevron to signal "this changes what the button does".

```html
<ixfx-split-button mode="switcher" default-value="save">
  <ixfx-menu-item slot="menu" command="save"       label="Save"></ixfx-menu-item>
  <ixfx-menu-item slot="menu" command="save-draft" label="Save as Draft"></ixfx-menu-item>
  <ixfx-menu-item slot="menu" command="export"     label="Export…"></ixfx-menu-item>
</ixfx-split-button>
```

Icons on switcher items flow through to the primary button:

```html
<ixfx-split-button mode="switcher" default-value="commit-push">
  <ixfx-menu-item slot="menu" command="commit-push" label="Commit & Push" icon-name="check"></ixfx-menu-item>
  <ixfx-menu-item slot="menu" command="commit-only" label="Commit Only"   icon-name="check"></ixfx-menu-item>
  <ixfx-menu-item slot="menu" command="amend"       label="Amend"                          ></ixfx-menu-item>
</ixfx-split-button>
```

---

### Programmatic control

All properties are writable at runtime. Changes trigger re-render automatically.

#### Set popup items directly

```js
import type { PopupItem } from 'ixfxfun-components';

const btn = document.querySelector('ixfx-split-button');

btn.items = [
  { command: 'staging', label: 'Deploy to Staging', icon: 'check' },
  { command: 'prod',    label: 'Deploy to Production' },
  { command: 'preview', label: 'Preview build' },
];
```

`items` works in both `default` and `switcher` modes. In switcher mode the first item (or `default-value` if matched) becomes the active selection.

#### Dynamic / async items via callback

```js
btn.itemsProvider = async () => {
  const data = await fetch('/api/deploy-targets').then(r => r.json());
  return data.map(t => ({ command: t.id, label: t.name }));
};
```

The popup shows a "Loading…" placeholder while the promise resolves. `itemsProvider` takes priority over `items` and declarative slot content.

#### Popover instead of a menu

The caret can toggle a native popover instead of the menu (works in both modes; the primary button is unaffected). Popover mode is on when any of these is present, and takes precedence over menu items:

```html
<!-- 1. Slotted content: shown in a panel anchored under the caret (`part="popover"`) -->
<ixfx-split-button>
  Share
  <div slot="popover">Anyone with the link can view</div>
</ixfx-split-button>

<!-- 2. An existing popover element anywhere in the same tree -->
<ixfx-split-button popover-target="share-pop">Share</ixfx-split-button>
<div id="share-pop" popover>…</div>
```

```js
// 3. Build the content just in time. Runs each time the popover is about to show
//    (awaited), with the panel — or the `popover-target` element — as the argument.
btn.popoverProvider = async (popover) => {
  const people = await fetch('/api/people').then(r => r.json());
  popover.replaceChildren(...people.map(p => Object.assign(document.createElement('div'), { textContent: p.name })));
};
```

`popoverProvider` can be combined with either form; on its own it fills the built-in panel. The caret gets `aria-haspopup="dialog"` and `aria-expanded`, ArrowDown opens it, and clicking the caret again closes it. A `popover-target` element positions itself (the caret is passed as the popover `source`, so CSS anchor positioning can use it implicitly). Width of the built-in panel: `--ixfx-split-button-popover-max-width`.

#### Change mode at runtime

```js
btn.mode = 'switcher'; // or 'default'
```

When switching to `switcher` and `items` is set, items are re-applied for the new mode automatically.

#### Select a command in switcher mode

```js
const found = btn.selectCommand('staging'); // returns false if command not in items
```

This changes the primary button label/icon immediately without opening the popup and without firing `menu-command`.

#### Change label and icon

```js
btn.textContent = 'Publish';  // updates the default slot
btn.icon = 'check';
btn.hideLabel = true;
btn.disabled = true;
```

#### Invoke primary action

```js
btn.invoke(); // Invokes the primary button as if clicked
```

In default mode, this fires a `click` event. In switcher mode, it fires `menu-command` with the currently selected command.

---

### Responsive behaviour

When the primary area is narrower than its label, the label truncates with a CSS ellipsis instead of wrapping, overflowing, or being hidden. The caret always stays visible. No JavaScript is involved — pure CSS `text-overflow` on the label.

```html
<ixfx-split-button icon="check" style="width: 100%">
  Publish this document to the team workspace
  <ixfx-menu-container slot="menu">
    <ixfx-menu-item command="publish-now" label="Publish Now"></ixfx-menu-item>
  </ixfx-menu-container>
</ixfx-split-button>
```

To keep the button from shrinking below a given width, set the `--ixfx-split-button-min-width` custom property:

```css
ixfx-split-button {
  --ixfx-split-button-min-width: 200px;
}
```

Below that width the button stops shrinking (and overflows its container instead of squeezing further).

To force icon-only at all times, set `hide-label`:

```html
<ixfx-split-button icon="check" hide-label>Commit</ixfx-split-button>
```

---

### CSS parts

| Part      | Element                   |
|-----------|---------------------------|
| `primary` | The left primary `<button>` |
| `caret`   | The right caret `<button>`  |

```css
ixfx-split-button::part(primary) { font-weight: bold; }
ixfx-split-button::part(caret)   { background: hotpink; }
```

---

### TypeScript types

```ts
import type {
  PopupItem,           // { command, label, icon?, disabled? }
  SwitcherItem,        // { command, label, icon }
  ItemsProvider,       // () => PopupItem[] | Promise<PopupItem[]>
  SplitButtonMode,     // 'default' | 'switcher'
} from 'ixfxfun-components';
```
