Searching Puzzle Pieces

Type to search Puzzle Pieces.

Select

A listbox-pattern select: a trigger showing the current choice and an overlay panel of options, with typeahead, roving keyboard focus, group labels and dividers. Options are real children, not an array prop. The family wraps the @magic-spells/select-dropdown web component rather than reimplementing it.

Pick a countryUnited StatesCanadaMexicoBrazil

Value: —

Installation

Copy the family into your app:

puzzle add piece select

It has one runtime dependency — the web component it wraps:

npm install @magic-spells/select-dropdown

Then merge its stylesheet into your app's style entry. It is required, not cosmetic: the panel's at-rest opacity: 0 and the rule that reverses it are the whole show/hide mechanism, and the :not(:defined) guard is what keeps the un-upgraded option list out of a prerendered page.

/* app/styles/styles.css */
@import "@magic-spells/select-dropdown/css" layer(components);

Requires the pieces.css tokens merged into your app styles (see Introduction).

Usage

import Select from '@/components/ui/Select';
<Select value={ country } placeholder="Pick a country" @change={ setCountry }>
  <Select.Label>North America</Select.Label>
  <Select.Option value="us">United States</Select.Option>
  <Select.Option value="ca">Canada</Select.Option>
  <Select.Divider/>
  <Select.Option value="mx" disabled>Mexico (sold out)</Select.Option>
</Select>

One barrel import, dotted invocation. The members are thin — restyle them in your own app without touching the root, which is where the state contract lives.

Groups and dividers

Pick a cityNorth AmericaNew YorkTorontoEuropeLondonBerlinLisbon

Value: —

Disabled option

Choose a planFreeProTeam (invite only)Enterprise

Arrow keys skip it entirely

Disabled select

Select…United StatesCanada

Custom trigger

Sort byNewest firstOldest firstA to Z

Value: —

Controlled open

Pick a sizeSmallMediumLarge

Dynamic options

Pick oneApplePearPlum

Value: plum

Behavior

  • Controlled — the parent owns value and stores what @change reports; the callback is value-first and fires on a user pick only, never on a programmatic set
  • Keyboard — Enter/Space or arrow keys open the panel, arrows and typeahead move through the options, Home/End jump to the first/last enabled one, Escape closes, Tab closes and moves on. Focus is real DOM focus on the option (a roving tabindex)
  • Sticky value — the last applied value is re-asserted whenever the option list changes, so a re-rendered {#for} keeps the selection. If the selected value is gone from the new list the control clears back to the placeholder
  • The trigger slot wins over the stock chrome — a slot="trigger" element replaces the label and the chevron entirely, so it must carry its own accessible name. Give the element you want the selected label written into the select-label-text class. Keep it non-interactive: the trigger itself is the control
  • Optional-controlled open — omit open and the component owns its own state. Pass it and only a CHANGE acts; store what @show and @hide report so the next change is a fresh edge
  • Forms — pass name and the family renders a hidden input the component keeps in sync, so the control submits and a form reset restores the markup default

Migrating from 0.6

Select was a single component taking an options array, with an optionSnippet for custom rows. It is a family now, and options are children:

// 0.6
<Select options={ countries } value={ country } @change={ setCountry } />

// 0.7
<Select value={ country } @change={ setCountry }>
  {#for row in countries}
    <Select.Option key={ row.value } value={ row.value }>{ row.label }</Select.Option>
  {/for}
</Select>

The morph, position, indicator and size props are gone — the component has one placement mode and one caret.