# Select

The native `<select>`, restyled, and a multiselect with tags.

Source: https://www.zazz.sh/api/primitives/select/

## Installation

```bash
pnpm dlx zazz-ui add select
```

Also adds `fields`, `kbd`, `button`, `popover`, `menu`, which it depends on.

Run `zazz-ui init` first if the project has no `zazz/` folder yet.

## Examples

### Default

```html
<div data-ui="field">
  <label data-field-slot="label" for="f-sort">Sort by</label>
  <select data-ui="select" id="f-sort" name="sort">
    <button>
      <selectedcontent></selectedcontent>
    </button>
    <!-- Keep label in optgroup as a fallback -->
    <optgroup
      label="Sort by"
      style="
        --display: grid;
        --gap: 1px;
      "
    >
      <legend
        style="
          --display: inline-flex;
          --p: 2;
          --font-size: var(--font-size-eyebrow);
          --font-weight: var(--font-weight-eyebrow);
          --leading: var(--leading-eyebrow);
          --tracking: var(--tracking-eyebrow);
          --text-transform: uppercase;
          --text: var(--color-muted-foreground);
        "
      >
        Sort by
      </legend>
      <option value="recent" selected>Most recent</option>
      <option value="popular">Most popular</option>
      <option value="price-asc">Price: low to high</option>
      <option value="price-desc">Price: high to low</option>
    </optgroup>
  </select>
</div>
```

### Align

```html
<div
  style="
    --display: flex;
    --items: start;
    --gap: 4;
  "
>
  <div data-ui="field">
    <label data-field-slot="label" for="f-align-start">Start</label>
    <select data-ui="select" id="f-align-start" name="align-start" data-select-align="start">
      <button>
        <selectedcontent></selectedcontent>
      </button>
      <optgroup
        label="Sort by"
        style="
          --display: grid;
          --gap: 1px;
        "
      >
        <legend
          style="
            --display: inline-flex;
            --p: 2;
            --font-size: var(--font-size-eyebrow);
            --font-weight: var(--font-weight-eyebrow);
            --leading: var(--leading-eyebrow);
            --tracking: var(--tracking-eyebrow);
            --text-transform: uppercase;
            --text: var(--color-muted-foreground);
          "
        >
          Sort by
        </legend>
        <option value="recent" selected>Most recent</option>
        <option value="popular">Most popular</option>
        <option value="price-asc">Price: low to high</option>
        <option value="price-desc">Price: high to low</option>
      </optgroup>
    </select>
  </div>

  <div data-ui="field">
    <label data-field-slot="label" for="f-align-center">Center</label>
    <select data-ui="select" id="f-align-center" name="align-center" data-select-align="center">
      <button>
        <selectedcontent></selectedcontent>
      </button>
      <optgroup
        label="Sort by"
        style="
          --display: grid;
          --gap: 1px;
        "
      >
        <legend
          style="
            --display: inline-flex;
            --p: 2;
            --font-size: var(--font-size-eyebrow);
            --font-weight: var(--font-weight-eyebrow);
            --leading: var(--leading-eyebrow);
            --tracking: var(--tracking-eyebrow);
            --text-transform: uppercase;
            --text: var(--color-muted-foreground);
          "
        >
          Sort by
        </legend>
        <option value="recent" selected>Most recent</option>
        <option value="popular">Most popular</option>
        <option value="price-asc">Price: low to high</option>
        <option value="price-desc">Price: high to low</option>
      </optgroup>
    </select>
  </div>

  <div data-ui="field">
    <label data-field-slot="label" for="f-align-end">End</label>
    <select data-ui="select" id="f-align-end" name="align-end" data-select-align="end">
      <button>
        <selectedcontent></selectedcontent>
      </button>
      <optgroup
        label="Sort by"
        style="
          --display: grid;
          --gap: 1px;
        "
      >
        <legend
          style="
            --display: inline-flex;
            --p: 2;
            --font-size: var(--font-size-eyebrow);
            --font-weight: var(--font-weight-eyebrow);
            --leading: var(--leading-eyebrow);
            --tracking: var(--tracking-eyebrow);
            --text-transform: uppercase;
            --text: var(--color-muted-foreground);
          "
        >
          Sort by
        </legend>
        <option value="recent" selected>Most recent</option>
        <option value="popular">Most popular</option>
        <option value="price-asc">Price: low to high</option>
        <option value="price-desc">Price: high to low</option>
      </optgroup>
    </select>
  </div>
</div>
```

### Sides

```html
<div
  style="
    --display: grid;
    --place-items: center;
    --grid-cols: 2;
    --gap: 4;
  "
>
  <div data-ui="field">
    <label data-field-slot="label" for="f-side-top">Top</label>
    <select data-ui="select" id="f-side-top" name="side-top" data-select-side="top">
      <button>
        <selectedcontent></selectedcontent>
      </button>
      <optgroup
        label="Sort by"
        style="
          --display: grid;
          --gap: 1px;
        "
      >
        <legend
          style="
            --display: inline-flex;
            --p: 2;
            --font-size: var(--font-size-eyebrow);
            --font-weight: var(--font-weight-eyebrow);
            --leading: var(--leading-eyebrow);
            --tracking: var(--tracking-eyebrow);
            --text-transform: uppercase;
            --text: var(--color-muted-foreground);
          "
        >
          Sort by
        </legend>
        <option value="recent" selected>Most recent</option>
        <option value="popular">Most popular</option>
        <option value="price-asc">Price: low to high</option>
        <option value="price-desc">Price: high to low</option>
      </optgroup>
    </select>
  </div>

  <div data-ui="field">
    <label data-field-slot="label" for="f-side-bottom">Bottom</label>
    <select data-ui="select" id="f-side-bottom" name="side-bottom" data-select-side="bottom">
      <button>
        <selectedcontent></selectedcontent>
      </button>
      <optgroup
        label="Sort by"
        style="
          --display: grid;
          --gap: 1px;
        "
      >
        <legend
          style="
            --display: inline-flex;
            --p: 2;
            --font-size: var(--font-size-eyebrow);
            --font-weight: var(--font-weight-eyebrow);
            --leading: var(--leading-eyebrow);
            --tracking: var(--tracking-eyebrow);
            --text-transform: uppercase;
            --text: var(--color-muted-foreground);
          "
        >
          Sort by
        </legend>
        <option value="recent" selected>Most recent</option>
        <option value="popular">Most popular</option>
        <option value="price-asc">Price: low to high</option>
        <option value="price-desc">Price: high to low</option>
      </optgroup>
    </select>
  </div>

  <div data-ui="field">
    <label data-field-slot="label" for="f-side-left">Left</label>
    <select data-ui="select" id="f-side-left" name="side-left" data-select-side="left">
      <button>
        <selectedcontent></selectedcontent>
      </button>
      <optgroup
        label="Sort by"
        style="
          --display: grid;
          --gap: 1px;
        "
      >
        <legend
          style="
            --display: inline-flex;
            --p: 2;
            --font-size: var(--font-size-eyebrow);
            --font-weight: var(--font-weight-eyebrow);
            --leading: var(--leading-eyebrow);
            --tracking: var(--tracking-eyebrow);
            --text-transform: uppercase;
            --text: var(--color-muted-foreground);
          "
        >
          Sort by
        </legend>
        <option value="recent" selected>Most recent</option>
        <option value="popular">Most popular</option>
        <option value="price-asc">Price: low to high</option>
        <option value="price-desc">Price: high to low</option>
      </optgroup>
    </select>
  </div>

  <div data-ui="field">
    <label data-field-slot="label" for="f-side-right">Right</label>
    <select data-ui="select" id="f-side-right" name="side-right" data-select-side="right">
      <button>
        <selectedcontent></selectedcontent>
      </button>
      <optgroup
        label="Sort by"
        style="
          --display: grid;
          --gap: 1px;
        "
      >
        <legend
          style="
            --display: inline-flex;
            --p: 2;
            --font-size: var(--font-size-eyebrow);
            --font-weight: var(--font-weight-eyebrow);
            --leading: var(--leading-eyebrow);
            --tracking: var(--tracking-eyebrow);
            --text-transform: uppercase;
            --text: var(--color-muted-foreground);
          "
        >
          Sort by
        </legend>
        <option value="recent" selected>Most recent</option>
        <option value="popular">Most popular</option>
        <option value="price-asc">Price: low to high</option>
        <option value="price-desc">Price: high to low</option>
      </optgroup>
    </select>
  </div>
</div>
```

### Multiple

```html
<div data-ui="field" style="--max-w: 96">
  <label data-field-slot="label" for="f-tags">Tags</label>
  <ui-multiselect
    data-multiselect-placeholder="Select tags"
    data-multiselect-side="bottom"
    data-multiselect-align="start"
  >
    <select data-ui="select" id="f-tags" name="tags" multiple>
      <option value="design" selected>Design</option>
      <option value="engineering" selected>Engineering</option>
      <option value="research">Research</option>
      <option value="marketing">Marketing</option>
      <option value="ops" disabled>Operations</option>
    </select>
  </ui-multiselect>
</div>
```

## Behavior

`primitives/select/multiselect.js`

`<ui-multiselect>`: a multi-select styled as a dropdown.
Light-DOM custom element that progressively enhances a real
`<select multiple data-ui="select">`. `appearance: base-select` does not
apply to multi-selects in stable engines, so the script hides the
select (which remains the form value carrier and source of truth) and
stamps a `[data-ui~="select"]`-look trigger button plus an anchored popover of
checkbox rows projected from the options. Checkbox changes write back to
`option.selected` and re-dispatch `change` on the select; external writes
and form resets flow the other way. Without JavaScript the native
multi-select listbox renders: fully functional and accessible.

The trigger reads `<first selection> (+N more)`.

Attributes on `<ui-multiselect>`:
- `data-multiselect-placeholder`: trigger text when nothing is selected.
- `data-multiselect-label-more`: overflow template, default `"(+{n} more)"`.
- `data-multiselect-side` / `data-multiselect-align`: forwarded to the stamped panel as `data-popover-*` (popover
  placement matrix).

Stamped parts: `multiselect-trigger` (a `[data-ui~="select"]`-identity button),
`multiselect-icon` (chevron), `multiselect-panel` (`[popover]`),
`multiselect-option` (label + checkbox per option).

## Attributes

| Attribute | Kind | Values |
| --- | --- | --- |
| `data-ui="multiselect"` | identity | free value |
| `data-ui="select"` | identity | free value |
| `<ui-multiselect>` | tag | free value |
| `data-select-align` | preset | `center` `end` `start` |
| `data-select-side` | preset | `bottom` `left` `right` `top` |
| `data-multiselect-slot` | slot | `icon` `label` `option` `panel` `trigger` |
| `data-multiselect-state` | state | `enhanced` |
| `data-multiselect-align` | config | free value |
| `data-multiselect-label-more` | config | free value |
| `data-multiselect-placeholder` | config | free value |
| `data-multiselect-side` | config | free value |

Requires: fields, popover, button, menu.

## Hooks

| Hook | Default | State |
| --- | --- | --- |
| `--ui-option-bg` | `transparent` | — |
| `--ui-option-bg--active` | `var(--ui-button-bg--active)` | active |
| `--ui-option-bg--hover` | `var(--ui-button-bg--hover)` | hover |
| `--ui-option-border-color` | `transparent` | — |
| `--ui-option-border-color--active` | `transparent` | active |
| `--ui-option-border-color--hover` | `transparent` | hover |
| `--ui-option-border-style` | `var(--ui-button-border-style)` | — |
| `--ui-option-border-width` | `var(--ui-button-border-width)` | — |
| `--ui-option-checkmark-color` | `currentColor` | — |
| `--ui-option-checkmark-mask` | `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 256 256'%3E%3Crect width='256' height='256' fill='none'/%3E%3Cpolyline points='40 144 96 200 224 72' fill='none' stroke='currentColor' stroke-linecap='round' stroke-linejoin='round' stroke-width='16'/%3E%3C/svg%3E")` | — |
| `--ui-option-checkmark-size` | `var(--ui-field-icon-size)` | — |
| `--ui-option-corner-shape` | `var(--ui-menu-button-corner-shape)` | — |
| `--ui-option-font-family` | `var(--ui-button-font-family)` | — |
| `--ui-option-font-size` | `var(--ui-button-font-size)` | — |
| `--ui-option-font-weight` | `var(--ui-button-font-weight)` | — |
| `--ui-option-gap` | `var(--ui-button-gap)` | — |
| `--ui-option-icon-size` | `var(--ui-button-icon-size)` | — |
| `--ui-option-leading` | `var(--ui-button-leading)` | — |
| `--ui-option-min-h` | `var(--ui-button-min-h)` | — |
| `--ui-option-px` | `var(--ui-button-px)` | — |
| `--ui-option-py` | `var(--ui-button-py)` | — |
| `--ui-option-radius` | `var(--ui-menu-button-radius)` | — |
| `--ui-option-shadow` | `none` | — |
| `--ui-option-text` | `var(--ui-button-text)` | — |
| `--ui-option-text--active` | `var(--ui-button-text--active)` | active |
| `--ui-option-text--hover` | `var(--ui-button-text--hover)` | hover |
| `--ui-select-border-color` | `var(--ui-field-border-color)` | — |
| `--ui-select-border-style` | `var(--ui-field-border-style)` | — |
| `--ui-select-border-width` | `var(--ui-field-border-width)` | — |
| `--ui-select-corner-shape` | `var(--ui-field-corner-shape)` | — |
| `--ui-select-font-size` | `var(--ui-field-font-size)` | — |
| `--ui-select-leading` | `var(--ui-field-leading)` | — |
| `--ui-select-min-w` | `16ch` | — |
| `--ui-select-option-gap` | `var(--ui-field-option-gap)` | — |
| `--ui-select-picker-icon-color` | `currentColor` | — |
| `--ui-select-picker-icon-mask` | `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 256 256'%3E%3Crect width='256' height='256' fill='none'/%3E%3Cpolyline points='80 176 128 224 176 176' fill='none' stroke='currentColor' stroke-linecap='round' stroke-linejoin='round' stroke-width='16'/%3E%3Cpolyline points='80 80 128 32 176 80' fill='none' stroke='currentColor' stroke-linecap='round' stroke-linejoin='round' stroke-width='16'/%3E%3C/svg%3E")` | — |
| `--ui-select-picker-icon-size` | `var(--ui-field-icon-size)` | — |
| `--ui-select-pl` | `var(--ui-field-px)` | — |
| `--ui-select-pr` | `calc(var(--spacing) * 2)` | — |
| `--ui-select-py` | `var(--ui-field-py)` | — |
| `--ui-select-radius` | `var(--ui-field-radius)` | — |
| `--ui-select-w` | `100%` | — |
