API
Select
The native `<select>`, restyled, and a multiselect with tags.
Installation
pnpm dlx zazz-ui add selectnpx zazz-ui add selectyarn dlx zazz-ui add selectbunx zazz-ui add selectAlso adds fields, kbd, button, popover, menu, which it depends on. Run zazz-ui init first if the project has no zazz/ folder yet.
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Paint the right theme (background, form controls, scrollbars) before CSS loads -->
<meta name="color-scheme" content="light dark">
<!-- Zazz styles: base layers, then fields, kbd, button, popover, menu, select in cascade order -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_layers.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_variables.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_shadows.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_breakpoints.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_reset.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_typography.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_view-transitions.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/fields/fields.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/kbd/kbd.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/button/button.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/popover/popover.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/menu/menu.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/select/select.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-sm.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-md.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-lg.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-xl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-2xl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-disabled.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-active.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-focus-visible.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-focus-within.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-hover.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-checked.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-current.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-open.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-stuck.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-starting.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-sm.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-md.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-lg.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-xl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-2xl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-disabled.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-active.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-focus-visible.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-focus-within.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-hover.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-checked.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-current.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-open.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-stuck.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-starting.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-flow.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-grid.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-spacing.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-margin.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-sizing.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-typography.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-color.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-effects.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-box.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-pseudo.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_defaults.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-sr.css">
<!-- Import map: bare specifiers in Zazz scripts resolve to pinned, SRI-checked files -->
<script type="importmap">
{
"imports": {
"signal-polyfill": "https://cdn.jsdelivr.net/npm/signal-polyfill@0.2.2/dist/index.js"
},
"integrity": {
"https://cdn.jsdelivr.net/npm/signal-polyfill@0.2.2/dist/index.js": "sha384-0OqM7OXGT6Oz1z98u/geiFn/FUyz4iWexkXHCvbIGXRRDXl6bp6IKc0j78AQ6DEK"
}
}
</script>
<!-- Polyfill: Interest Invokers (interestfor) — native in Chromium only -->
<script type="module" src="https://cdn.jsdelivr.net/npm/invokers@2.2.2/dist/esm/production/interest.js" integrity="sha384-hR2BVNtsS7fIIMwOm+f7MY0JZfuByGhQQfevCBB6evFATKzBsTw0JzH/RxD94z2T" crossorigin="anonymous"></script>
<!-- Zazz behavior: side-effect modules by tag; the rest via module imports -->
<script type="module" src="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/dialog-lifecycle.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/menu/menu.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/select/multiselect.js"></script>
<!-- Theme: apply the persisted (or preferred) scheme before first paint -->
<script>
(() => {
try {
// A stored choice pins the scheme with data-theme; with no choice the
// page follows the system scheme.
const storedTheme = localStorage.getItem("theme");
if (storedTheme === "dark" || storedTheme === "light") {
document.documentElement.dataset.theme = storedTheme;
}
} catch {
/* no storage access: follow the system scheme */
}
})();
</script>A <head> that loads the base layers and select with its dependencies from jsDelivr, pinned to 0.14.2. To load the whole kit in two requests, see the CDN install.
Examples
Default
<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>/**
* select.css: Select (:where([data-ui~="select"]) on customizable <select>)
*
* @layer variables, reset, components
* @requires _layers.css, _variables.css, fields.css, _reset.css, popover.css (--ui-popover-*), button.css (--ui-button-*), menu.css (--ui-menu-*)
* @uses appearance: base-select: customizable select
* @uses ::picker(select), ::picker-icon, selectedcontent, option::checkmark
* @uses @starting-style + allow-discrete: picker open animation
* @uses anchor-size(width): match picker width to trigger
* @uses position-area, position-try-fallbacks: placement tokens come from
* popover.css's shared matrix (@supports gated)
* @uses Degrades to native OS dropdown where base-select is unsupported
* @uses ui-multiselect (multiselect.ts): JS-projected dropdown for
* <select multiple> (base-select has no stable multi-select);
* degrades to the native multi listbox without JS
* @uses --_touch-min (touch screens) — the trigger and every option row grow to the touch
* floor; the trigger's inline sides each gain half of what its height grew
* @uses corner-shape — superellipse corners; unsupported browsers keep plain
* rounded corners
* @tokens --ui-select-* (@layer variables); picker panel reuses --ui-popover-* (owned in popover.css)
* @consumedby autocomplete.css, combobox.css, command.css (--ui-option-*, --ui-select-picker-icon-*)
*/
@layer variables {
:root {
/* Box treatment aliases the shared --ui-field-* family; remap a --ui-select-*
token to diverge just the select. */
--ui-select-w: 100%;
--ui-select-min-w: 16ch;
--ui-select-pl: var(--ui-field-px);
/* room for the picker icon */
--ui-select-pr: calc(var(--spacing) * 2);
--ui-select-py: var(--ui-field-py);
--ui-select-font-size: var(--ui-field-font-size);
--ui-select-leading: var(--ui-field-leading);
--ui-select-border-width: var(--ui-field-border-width);
--ui-select-border-style: var(--ui-field-border-style);
--ui-select-border-color: var(--ui-field-border-color);
--ui-select-radius: var(--ui-field-radius);
--ui-select-corner-shape: var(--ui-field-corner-shape);
--ui-select-option-gap: var(--ui-field-option-gap);
/* Picker icon (trigger chevron) */
--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-picker-icon-color: currentColor;
/* Option (menu item): a ghost button rendered as a native <option>. Aliases
--ui-button-*; only --ui-option-bg/--ui-option-border-color diverge
(no fill or border). Radius bridges to --ui-menu-button-radius. */
--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-leading: var(--ui-button-leading);
--ui-option-gap: var(--ui-button-gap);
--ui-option-px: var(--ui-button-px);
--ui-option-py: var(--ui-button-py);
--ui-option-min-h: var(--ui-button-min-h);
--ui-option-icon-size: var(--ui-button-icon-size);
--ui-option-radius: var(--ui-menu-button-radius);
--ui-option-corner-shape: var(--ui-menu-button-corner-shape);
--ui-option-shadow: none;
--ui-option-bg: transparent;
--ui-option-bg--hover: var(--ui-button-bg--hover);
--ui-option-bg--active: var(--ui-button-bg--active);
--ui-option-text: var(--ui-button-text);
--ui-option-text--hover: var(--ui-button-text--hover);
--ui-option-text--active: var(--ui-button-text--active);
--ui-option-border-width: var(--ui-button-border-width);
--ui-option-border-style: var(--ui-button-border-style);
--ui-option-border-color: transparent;
--ui-option-border-color--hover: transparent;
--ui-option-border-color--active: transparent;
/* Option checkmark */
--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-checkmark-color: currentColor;
}
}
@layer reset {
/* ===========================================================================
SELECT: native <select> internals (::picker, <option>, ::picker-icon)
- In the reset layer so component overrides win.
- Both the select and ::picker(select) need appearance: base-select before
picker pseudo styles apply.
=========================================================================== */
:where([data-ui~="select"]),
:where([data-ui~="select"])::picker(select) {
appearance: base-select;
}
/* Inline-edge names for the placement matrix's origins, resolved on the
select (where the matrix sets them) so RTL mirrors them; see popover.css. */
:where([data-ui~="select"]) {
--_origin-inline-start: left;
--_origin-inline-end: right;
}
:where([data-ui~="select"]:dir(rtl)) {
--_origin-inline-start: right;
--_origin-inline-end: left;
}
:where([data-ui~="select"])::picker(select) {
transform-origin: var(--ui-popover-origin);
opacity: var(--ui-popover-opacity--start);
transform: scale(var(--ui-popover-scale--start)) translate3d(var(--ui-popover-translate--start));
transition:
opacity var(--default-transition-duration) var(--default-transition-timing-function),
transform var(--default-transition-duration) var(--default-transition-timing-function),
padding var(--default-transition-duration) var(--default-transition-timing-function),
block-size var(--default-transition-duration) var(--default-transition-timing-function),
inline-size var(--default-transition-duration) var(--default-transition-timing-function),
overlay var(--default-transition-duration) var(--default-transition-timing-function)
allow-discrete,
display var(--default-transition-duration) var(--default-transition-timing-function)
allow-discrete;
}
:where([data-ui~="select"]:open)::picker(select) {
opacity: var(--ui-popover-opacity--end);
transform: scale(var(--ui-popover-scale--end)) translate3d(var(--ui-popover-translate--end));
display: flex;
}
@starting-style {
:where([data-ui~="select"]:open)::picker(select) {
opacity: var(--ui-popover-opacity--start);
transform: scale(var(--ui-popover-scale--start))
translate3d(var(--ui-popover-translate--start));
}
}
:where(option)::checkmark {
content: "";
inline-size: var(--ui-option-checkmark-size);
block-size: var(--ui-option-checkmark-size);
background-color: var(--ui-option-checkmark-color);
mask: var(--ui-option-checkmark-mask) center / contain no-repeat;
}
:where([data-ui~="select"])::picker-icon {
content: "";
inline-size: var(--ui-select-picker-icon-size);
block-size: var(--ui-select-picker-icon-size);
background-color: var(--ui-select-picker-icon-color);
mask: var(--ui-select-picker-icon-mask) center / contain no-repeat;
}
}
@layer ui.components {
/* ===========================================================================
SELECT: :where([data-ui~="select"]) (customizable <select>, no JS)
- Needs a <button><selectedcontent></selectedcontent></button> child. Degrades
to the native OS dropdown where unsupported (the _reset.css chevron covers it).
=========================================================================== */
:where([data-ui~="select"]) {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--ui-select-option-gap);
--_w-len: var(--_w-resolved, var(--ui-select-w));
--_w-num: var(--_w-resolved, var(--ui-select-w));
inline-size: calc(var(--_w-len) + var(--_w-num) * var(--spacing));
--_min-w-len: var(--_min-w-resolved, var(--ui-select-min-w));
--_min-w-num: var(--_min-w-resolved, var(--ui-select-min-w));
min-inline-size: calc(var(--_min-w-len) + var(--_min-w-num) * var(--spacing));
--_h-len: var(--_h-resolved, var(--ui-field-h));
--_h-num: var(--_h-resolved, var(--ui-field-h));
/* never shorter than one line plus --ui-select-py and the border above and below (see input.css) */
--_py-len: var(--_py-resolved, var(--ui-select-py));
--_py-num: var(--_py-resolved, var(--ui-select-py));
block-size: max(
calc(var(--_h-len) + var(--_h-num) * var(--spacing)),
var(--_touch-min),
calc(
1lh + 2 *
(
var(--_py-len) + var(--_py-num) * var(--spacing) +
var(--_border-width-resolved, var(--ui-select-border-width))
)
)
);
/* half of what the touch floor added to the height, on each inline side */
--_touch-px: max(
0px,
(var(--_touch-min) - calc(var(--_h-len) + var(--_h-num) * var(--spacing))) / 2
);
--_pl-len: var(--_pl-resolved, var(--ui-select-pl));
--_pl-num: var(--_pl-resolved, var(--ui-select-pl));
padding-inline-start: calc(var(--_pl-len) + var(--_pl-num) * var(--spacing) + var(--_touch-px));
--_pr-len: var(--_pr-resolved, var(--ui-select-pr));
--_pr-num: var(--_pr-resolved, var(--ui-select-pr));
padding-inline-end: calc(var(--_pr-len) + var(--_pr-num) * var(--spacing) + var(--_touch-px));
font-size: var(--_font-size-resolved, var(--ui-select-font-size));
line-height: var(--_leading-resolved, var(--ui-select-leading));
text-align: start;
color: var(--_text-resolved, var(--ui-field-text));
background-color: var(--_bg-resolved, var(--ui-field-bg));
background-image: none;
border-width: var(--_border-width-resolved, var(--ui-select-border-width));
border-style: var(--_border-style-resolved, var(--ui-select-border-style));
border-color: var(--_border-color-resolved, var(--ui-select-border-color));
--_radius-len: var(--_radius-resolved, var(--ui-select-radius));
--_radius-num: var(--_radius-resolved, var(--ui-select-radius));
border-radius: calc(
var(--_radius-len) + var(--_radius-num) * var(--spacing) * var(--radius-multiplier)
);
corner-shape: var(--_corner-shape-resolved, var(--ui-select-corner-shape));
cursor: pointer;
/* the transparent outline keeps focus visible in forced-colors */
--_ring-offset-width: 0px;
--_ring-width: 0px;
--_focus-in: max(0, -1 * sign(var(--_focus-ring-w, 1px)));
--_ring-fill: color-mix(in oklch, var(--ui-field-ring-color) var(--ring-opacity), transparent);
--_focus-shadow:
0 0 0 calc(var(--_ring-offset-width) * (1 - var(--_focus-in))) var(--ring-offset-shadow-color),
0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * (1 - var(--_focus-in)))
var(--_ring-fill, var(--ring-shadow-color)),
inset 0 0 0 calc(var(--_ring-offset-width) * var(--_focus-in)) var(--ring-offset-shadow-color),
inset 0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * var(--_focus-in))
var(--_ring-fill, var(--ring-shadow-color));
box-shadow: var(--_focus-shadow, 0 0 #0000), var(--_shadow-resolved, 0 0 #0000);
outline: abs(var(--_focus-ring-w, var(--outline-width))) var(--outline-style) transparent;
outline-offset: calc(
var(--outline-offset) - var(--_focus-in) *
(2 * var(--outline-offset) + abs(var(--_focus-ring-w, var(--outline-width))))
);
transition: var(--default-transition);
}
@media (hover: hover) {
:where([data-ui~="select"]):hover {
border-color: var(--_border-color-resolved, var(--ui-field-border-color--hover));
}
}
/* :is() takes a forgiving list: an engine without :open keeps the focus ring */
:where([data-ui~="select"]):is(:focus-visible, :open) {
border-color: var(--_border-color-resolved, var(--ui-field-border-color--focus-within));
--_ring-offset-width: var(--ring-offset-width);
--_ring-width: abs(var(--_focus-ring-w, var(--ring-width)));
outline-color: transparent;
}
:where([data-ui~="select"]) > button {
block-size: 100%;
display: flex;
flex: 1;
align-items: center;
gap: var(--ui-field-gap);
min-inline-size: 0;
overflow: hidden;
}
:where([data-ui~="select"]) selectedcontent {
display: flex;
align-items: center;
block-size: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
:where([data-ui~="select"]) > button > svg,
:where([data-ui~="select"]) selectedcontent > svg {
flex-shrink: 0;
inline-size: var(--ui-field-icon-size);
block-size: var(--ui-field-icon-size);
color: var(--color-muted-foreground);
}
:where([data-ui~="select"]):open {
background-color: var(--_bg-resolved, var(--ui-field-bg--focus-within));
}
:where([data-ui~="select"]):open::picker-icon {
transform: rotate(-180deg);
}
/* Where anchor-size() is missing the declaration is dropped, so the panel
keeps max-content sizing. */
:where([data-ui~="select"])::picker(select) {
flex-direction: column;
gap: 1px;
inline-size: max-content;
max-inline-size: var(--ui-popover-max-w);
padding: var(--ui-popover-p);
margin-block: var(--ui-popover-my);
overflow: clip;
background-color: var(--color-popover);
color: var(--color-popover-foreground);
border: 1px solid var(--ui-popover-border-color);
--_radius-len: var(--_radius-resolved, var(--ui-popover-radius));
--_radius-num: var(--_radius-resolved, var(--ui-popover-radius));
border-radius: calc(
var(--_radius-len) + var(--_radius-num) * var(--spacing) * var(--radius-multiplier)
);
corner-shape: var(--_corner-shape-resolved, var(--ui-popover-corner-shape));
box-shadow: var(--ui-popover-shadow);
}
:where([data-ui~="select"])::picker(select) {
min-inline-size: anchor-size(width);
}
/* ===========================================================================
PICKER PLACEMENT: the placement tokens come from the matrix below (select
custom properties inherit into ::picker(select)); applying them is gated on
anchor support.
=========================================================================== */
/* The picker is implicitly anchored, so no anchor-name is needed; gated like
popover.css so unsupporting browsers keep the UA placement. */
@supports (anchor-name: --zazz) {
:where([data-ui~="select"])::picker(select) {
position-area: var(--ui-popover-position-area);
position-try-fallbacks: var(--ui-popover-position-try-fallbacks);
margin-inline: var(--ui-popover-mx);
}
}
/* Like [data-ui~="button"], sizing rides on min-block-size + padding-inline, so
every row is uniform; --ui-option-py only shows once a small --spacing drops
the min height below the label. */
:where([data-ui~="select"]) option {
display: flex;
align-items: center;
gap: var(--ui-option-gap);
min-block-size: max(var(--ui-option-min-h), var(--_touch-min));
padding-block: var(--ui-option-py);
padding-inline: var(--ui-option-px);
font-family: var(--ui-option-font-family);
font-size: var(--ui-option-font-size);
font-weight: var(--ui-option-font-weight);
line-height: var(--ui-option-leading);
color: var(--ui-option-text);
background-color: var(--ui-option-bg);
border: var(--ui-option-border-width) var(--ui-option-border-style)
var(--ui-option-border-color);
border-radius: var(--ui-option-radius);
corner-shape: var(--ui-option-corner-shape);
box-shadow: var(--ui-option-shadow);
cursor: pointer;
transition: var(--default-transition);
}
@media (hover: hover) {
:where([data-ui~="select"]) option:hover {
color: var(--ui-option-text--hover);
background-color: var(--ui-option-bg--hover);
border-color: var(--ui-option-border-color--hover);
}
}
:where([data-ui~="select"]) option:checked {
color: var(--ui-option-text--active);
background-color: var(--ui-option-bg--active);
border-color: var(--ui-option-border-color--active);
}
:where([data-ui~="select"]) option:disabled {
--ui-option-text: var(--color-muted-foreground);
cursor: default;
opacity: 0.5;
}
:where([data-ui~="select"]) option > svg {
flex-shrink: 0;
inline-size: var(--ui-option-icon-size);
block-size: var(--ui-option-icon-size);
color: var(--color-muted-foreground);
}
:where([data-ui~="select"]) option::checkmark {
order: 1;
margin-inline-start: auto;
color: currentcolor;
}
/* ===========================================================================
MULTISELECT: ui-multiselect (JS-projected dropdown over <select multiple>)
- The script hides the real select and stamps a trigger plus a popover of
checkbox rows (see multiselect.ts).
- Without JS the native multi listbox renders with minimal field styling.
=========================================================================== */
:where(ui-multiselect, [data-ui~="multiselect"]) {
anchor-scope: --ui-multiselect-trigger;
display: block;
--_w-len: var(--_w-resolved, var(--ui-select-w));
--_w-num: var(--_w-resolved, var(--ui-select-w));
inline-size: calc(var(--_w-len) + var(--_w-num) * var(--spacing));
--_min-w-len: var(--_min-w-resolved, var(--ui-select-min-w));
--_min-w-num: var(--_min-w-resolved, var(--ui-select-min-w));
min-inline-size: calc(var(--_min-w-len) + var(--_min-w-num) * var(--spacing));
}
/* The real select stays in the DOM as the form value carrier */
:where(ui-multiselect, [data-ui~="multiselect"])
select[multiple][data-multiselect-state~="enhanced"] {
display: none;
}
:where(ui-multiselect, [data-ui~="multiselect"])
select[multiple][data-ui~="select"]:not([data-multiselect-state~="enhanced"]) {
display: block;
block-size: auto;
padding-block: var(--spacing);
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="trigger"] {
anchor-name: --ui-multiselect-trigger;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="label"] {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="icon"] {
flex-shrink: 0;
inline-size: var(--ui-select-picker-icon-size);
block-size: var(--ui-select-picker-icon-size);
background-color: var(--ui-select-picker-icon-color);
mask: var(--ui-select-picker-icon-mask) center / contain no-repeat;
transition: var(--default-transition);
}
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="trigger"][aria-expanded="true"]
[data-multiselect-slot~="icon"] {
transform: rotate(-180deg);
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="panel"] {
position-anchor: --ui-multiselect-trigger;
/* layout stays ungated so the close fade keeps it */
flex-direction: column;
gap: 1px;
box-shadow: var(--ui-menu-shadow);
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="panel"]:popover-open {
display: flex;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="panel"] {
min-inline-size: anchor-size(width);
}
/* The checkbox is real but visually replaced by the shared checkmark glyph */
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"] {
display: flex;
align-items: center;
gap: var(--ui-option-gap);
min-block-size: max(var(--ui-option-min-h), var(--_touch-min));
padding-block: var(--ui-option-py);
padding-inline: var(--ui-option-px);
font-family: var(--ui-option-font-family);
font-size: var(--ui-option-font-size);
font-weight: var(--ui-option-font-weight);
line-height: var(--ui-option-leading);
color: var(--ui-option-text);
background-color: var(--ui-option-bg);
border: var(--ui-option-border-width) var(--ui-option-border-style)
var(--ui-option-border-color);
border-radius: var(--ui-option-radius);
corner-shape: var(--ui-option-corner-shape);
cursor: pointer;
transition: var(--default-transition);
}
@media (hover: hover) {
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"]:hover {
color: var(--ui-option-text--hover);
background-color: var(--ui-option-bg--hover);
}
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"]:has(:checked) {
color: var(--ui-option-text--active);
background-color: var(--ui-option-bg--active);
}
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="option"]:has(:disabled) {
color: var(--color-muted-foreground);
cursor: default;
opacity: 0.5;
}
/* The checkbox stays focusable and announced but invisible; keyboard focus
surfaces on the row via :focus-within */
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="option"]
input[type="checkbox"] {
position: absolute;
inline-size: 1px;
block-size: 1px;
overflow: clip;
clip-path: inset(50%);
white-space: nowrap;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"]:focus-within {
background-color: var(--ui-option-bg--hover);
outline: var(--outline-width) var(--outline-style) var(--ring-shadow-color);
outline-offset: calc(var(--outline-offset) * -1);
}
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="option"]:has(:checked)::after {
content: "";
order: 1;
margin-inline-start: auto;
inline-size: var(--ui-option-checkmark-size);
block-size: var(--ui-option-checkmark-size);
background-color: var(--ui-option-checkmark-color);
mask: var(--ui-option-checkmark-mask) center / contain no-repeat;
}
}
/* An invalid control remaps the --ui-field-* family on itself. */
@layer ui.components {
:where([data-ui~="select"]):disabled {
opacity: 0.5;
cursor: not-allowed;
}
:where([data-ui~="select"]):user-invalid {
--ui-field-bg: var(--color-destructive);
--ui-field-bg--hover: oklch(from var(--color-destructive) calc(l - 0.025) c h);
--ui-field-border-color: var(--color-destructive-foreground);
--ui-field-border-color--hover: var(--color-destructive-foreground);
--ui-field-border-color--focus-within: var(--color-destructive-foreground);
--ui-field-ring-color: var(--color-destructive-foreground);
border-color: var(--color-destructive-foreground);
}
}
/* Placement rows of the popover matrix for the native select, keyed on
data-select-side / data-select-align. */
@layer ui.components {
:where([data-ui~="select"])[data-select-side="bottom"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="bottom"][data-select-align="start"],
:where([data-ui~="select"]:not([data-select-side]):not([data-select-align])),
:where([data-ui~="select"]:not([data-select-side])[data-select-align="start"]) {
--ui-popover-position-area: block-end span-inline-end;
--ui-popover-origin: top var(--_origin-inline-start);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="bottom"][data-select-align="center"],
:where([data-ui~="select"]:not([data-select-side])[data-select-align="center"]) {
--ui-popover-position-area: block-end span-all;
--ui-popover-origin: top center;
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="bottom"][data-select-align="end"],
:where([data-ui~="select"]:not([data-select-side])[data-select-align="end"]) {
--ui-popover-position-area: block-end span-inline-start;
--ui-popover-origin: top var(--_origin-inline-end);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="top"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="top"][data-select-align="start"] {
--ui-popover-position-area: block-start span-inline-end;
--ui-popover-origin: bottom var(--_origin-inline-start);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="top"][data-select-align="center"] {
--ui-popover-position-area: block-start span-all;
--ui-popover-origin: bottom center;
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="top"][data-select-align="end"] {
--ui-popover-position-area: block-start span-inline-start;
--ui-popover-origin: bottom var(--_origin-inline-end);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="right"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="right"][data-select-align="start"] {
--ui-popover-position-area: inline-end span-block-end;
--ui-popover-origin: top var(--_origin-inline-start);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="right"][data-select-align="center"] {
--ui-popover-position-area: inline-end span-all;
--ui-popover-origin: center var(--_origin-inline-start);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="right"][data-select-align="end"] {
--ui-popover-position-area: inline-end span-block-start;
--ui-popover-origin: bottom var(--_origin-inline-start);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="left"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="left"][data-select-align="start"] {
--ui-popover-position-area: inline-start span-block-end;
--ui-popover-origin: top var(--_origin-inline-end);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="left"][data-select-align="center"] {
--ui-popover-position-area: inline-start span-all;
--ui-popover-origin: center var(--_origin-inline-end);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="left"][data-select-align="end"] {
--ui-popover-position-area: inline-start span-block-start;
--ui-popover-origin: bottom var(--_origin-inline-end);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
}// primitives/menu/menu.js
"use strict";
/**
* @fileoverview `<ui-menu>`: HTML web component for keyboard-enhanced menus.
* @description Light-DOM custom element that augments the CSS-only menu
* pattern (trigger + anchored `[data-menu-slot~="popover"]` panel) with
* arrow-key navigation. The attribute form `[data-ui~="menu"]` stays fully functional
* without JavaScript: the Popover API provides open/close, light dismiss,
* and focus return on its own.
*
* Keyboard behavior:
* - ArrowDown / ArrowUp on the closed trigger open the panel and focus the
* first / last item.
* - ArrowDown / ArrowUp inside the open panel move focus between items,
* wrapping around and skipping disabled items.
* - Home / End jump to the first / last item.
* - Escape and light dismiss are native Popover API behavior (no code here).
*
* The menu keeps the honest disclosure posture: items are plain links and
* buttons, and no `role="menu"` is claimed. Add the full ARIA menu contract
* yourself only if every item is an action and you implement the rest of the
* pattern (typeahead, close-on-activate).
*
* @example
* <ui-menu>
* <button data-ui="button" type="button" popovertarget="m1">Open</button>
* <div id="m1" data-menu-slot="popover" popover="auto">
* <menu>
* <li><a href="/docs" data-ui="button" data-button-variant="ghost" style="--justify: start">Docs</a></li>
* </menu>
* </div>
* </ui-menu>
*/
import { ZazzElement, defineZazzElement } from "../../base/zazz-element.js";
class UiMenu extends ZazzElement {
setup(signal) {
this.addEventListener("keydown", (event) => this.#onKeydown(event), { signal });
}
/**
* @description The menu's own panel: a direct child so nested menus keep
* their panels to themselves.
*
* @returns The panel element, or null when the markup is incomplete.
*/
#panel() {
const panel = this.querySelector(':scope > [data-menu-slot~="popover"]');
return panel instanceof HTMLElement ? panel : null;
}
/**
* @description Focusable items inside the panel, in DOM order.
*
* @param panel - The menu panel.
* @returns Enabled links and buttons the arrow keys move between.
*/
#items(panel) {
return Array.from(panel.querySelectorAll("a[href], button"))
.filter((node) => node instanceof HTMLElement)
.filter((item) => !item.hasAttribute("disabled") &&
item.getAttribute("aria-disabled") !== "true" &&
item.closest('ui-menu, [data-ui~="menu"]') === this);
}
/**
* @description Routes arrow-key, Home, and End presses: opens the panel from
* the trigger, or moves focus between items inside the open panel.
*
* @param event - The keydown event.
*/
#onKeydown(event) {
if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key))
return;
const target = event.target;
if (!(target instanceof HTMLElement))
return;
// Ignore keys that belong to a nested ui-menu
if (target.closest('ui-menu, [data-ui~="menu"]') !== this)
return;
const panel = this.#panel();
if (!panel)
return;
const isTrigger = target.parentElement === this &&
(target.hasAttribute("popovertarget") || target.hasAttribute("interestfor"));
const open = panel.matches(":popover-open");
if (isTrigger && !open && (event.key === "ArrowDown" || event.key === "ArrowUp")) {
event.preventDefault();
panel.showPopover();
const items = this.#items(panel);
items[event.key === "ArrowDown" ? 0 : items.length - 1]?.focus();
return;
}
if (!open || !panel.contains(target))
return;
const items = this.#items(panel);
if (items.length === 0)
return;
const index = items.indexOf(target);
let nextIndex;
switch (event.key) {
case "ArrowDown":
nextIndex = index === -1 ? 0 : (index + 1) % items.length;
break;
case "ArrowUp":
nextIndex = index === -1 ? items.length - 1 : (index - 1 + items.length) % items.length;
break;
case "Home":
nextIndex = 0;
break;
case "End":
nextIndex = items.length - 1;
break;
default:
return;
}
event.preventDefault();
items[nextIndex].focus();
}
}
defineZazzElement("ui-menu", UiMenu);
export { UiMenu };
// primitives/select/multiselect.js
"use strict";
/**
* @fileoverview `<ui-multiselect>`: a multi-select styled as a dropdown.
* @description 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).
*/
import { ZazzElement, defineZazzElement } from "../../base/zazz-element.js";
import { effect, state } from "../../base/signals.js";
/**
* @description Formats the trigger label from the selected option labels.
*
* @param labels - Labels of the selected options, in DOM order.
* @param placeholder - Text for an empty selection.
* @param moreTemplate - Overflow template; `{n}` is the remaining count.
* @returns The trigger text.
*/
function resolveTriggerLabel(labels, placeholder, moreTemplate) {
if (labels.length === 0)
return placeholder;
if (labels.length === 1)
return labels[0];
return `${labels[0]} ${moreTemplate.replace("{n}", String(labels.length - 1))}`;
}
let multiselectIdCounter = 0;
class UiMultiselect extends ZazzElement {
#stamped = [];
#select = null;
setup(signal) {
const select = this.querySelector("select[multiple]");
if (!(select instanceof HTMLSelectElement))
return;
this.#select = select;
const placeholder = this.getAttribute("data-multiselect-placeholder") ?? "Select…";
const moreTemplate = this.getAttribute("data-multiselect-label-more") ?? "(+{n} more)";
const { trigger, label, panel, checkboxes } = this.#stamp(select);
select.setAttribute("data-multiselect-state", "enhanced");
const selectedLabels = () => Array.from(select.selectedOptions).map((option) => option.label);
const selection = state(selectedLabels());
// Checkbox → option: the select stays the source of truth
checkboxes.forEach((checkbox, index) => {
checkbox.addEventListener("change", () => {
const option = select.options[index];
if (option)
option.selected = checkbox.checked;
selection.set(selectedLabels());
select.dispatchEvent(new Event("change", { bubbles: true }));
}, { signal });
});
// External writes and form resets → re-read (reset applies after the event)
select.addEventListener("change", () => selection.set(selectedLabels()), { signal });
select.form?.addEventListener("reset", () => queueMicrotask(() => selection.set(selectedLabels())), { signal });
// Mirror popover open state onto the trigger's aria-expanded
panel.addEventListener("toggle", (event) => {
trigger.setAttribute("aria-expanded", String(event.newState === "open"));
}, { signal });
// Output adapter: trigger label + row checked states together
effect(() => {
selection.get();
label.textContent = resolveTriggerLabel(selectedLabels(), placeholder, moreTemplate);
checkboxes.forEach((checkbox, index) => {
const option = select.options[index];
if (option && checkbox.checked !== option.selected)
checkbox.checked = option.selected;
});
}, { signal });
}
teardown() {
for (const node of this.#stamped)
node.remove();
this.#stamped = [];
this.#select?.removeAttribute("data-multiselect-state");
this.#select = null;
}
/**
* @description Builds the trigger button and checkbox panel from the
* select's options and inserts them after it.
*
* @param select - The enhanced select.
* @returns The stamped parts and the checkbox list (option order).
* @private
*/
#stamp(select) {
const panelId = `ui-multiselect-panel-${++multiselectIdCounter}`;
const trigger = document.createElement("button");
trigger.type = "button";
trigger.dataset.ui = "select";
trigger.setAttribute("data-multiselect-slot", "trigger");
trigger.setAttribute("popovertarget", panelId);
trigger.setAttribute("aria-expanded", "false");
const label = document.createElement("span");
label.setAttribute("data-multiselect-slot", "label");
const icon = document.createElement("span");
icon.setAttribute("data-multiselect-slot", "icon");
icon.setAttribute("aria-hidden", "true");
trigger.append(label, icon);
const panel = document.createElement("div");
panel.id = panelId;
panel.setAttribute("data-multiselect-slot", "panel");
panel.setAttribute("popover", "auto");
const side = this.getAttribute("data-multiselect-side");
const align = this.getAttribute("data-multiselect-align");
if (side)
panel.setAttribute("data-popover-side", side);
if (align)
panel.setAttribute("data-popover-align", align);
const checkboxes = [];
for (const [index, option] of Array.from(select.options).entries()) {
const row = document.createElement("label");
row.setAttribute("data-multiselect-slot", "option");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
// an id, not a name: the select submits, and Chrome flags a field with neither
checkbox.id = `${panelId}-${index}`;
checkbox.checked = option.selected;
checkbox.disabled = option.disabled;
row.append(checkbox, document.createTextNode(option.label));
panel.append(row);
checkboxes.push(checkbox);
}
select.after(trigger, panel);
this.#stamped = [trigger, panel];
return { trigger, label, panel, checkboxes };
}
}
defineZazzElement("ui-multiselect", UiMultiselect);
export { UiMultiselect, resolveTriggerLabel };Align
<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>/**
* select.css: Select (:where([data-ui~="select"]) on customizable <select>)
*
* @layer variables, reset, components
* @requires _layers.css, _variables.css, fields.css, _reset.css, popover.css (--ui-popover-*), button.css (--ui-button-*), menu.css (--ui-menu-*)
* @uses appearance: base-select: customizable select
* @uses ::picker(select), ::picker-icon, selectedcontent, option::checkmark
* @uses @starting-style + allow-discrete: picker open animation
* @uses anchor-size(width): match picker width to trigger
* @uses position-area, position-try-fallbacks: placement tokens come from
* popover.css's shared matrix (@supports gated)
* @uses Degrades to native OS dropdown where base-select is unsupported
* @uses ui-multiselect (multiselect.ts): JS-projected dropdown for
* <select multiple> (base-select has no stable multi-select);
* degrades to the native multi listbox without JS
* @uses --_touch-min (touch screens) — the trigger and every option row grow to the touch
* floor; the trigger's inline sides each gain half of what its height grew
* @uses corner-shape — superellipse corners; unsupported browsers keep plain
* rounded corners
* @tokens --ui-select-* (@layer variables); picker panel reuses --ui-popover-* (owned in popover.css)
* @consumedby autocomplete.css, combobox.css, command.css (--ui-option-*, --ui-select-picker-icon-*)
*/
@layer variables {
:root {
/* Box treatment aliases the shared --ui-field-* family; remap a --ui-select-*
token to diverge just the select. */
--ui-select-w: 100%;
--ui-select-min-w: 16ch;
--ui-select-pl: var(--ui-field-px);
/* room for the picker icon */
--ui-select-pr: calc(var(--spacing) * 2);
--ui-select-py: var(--ui-field-py);
--ui-select-font-size: var(--ui-field-font-size);
--ui-select-leading: var(--ui-field-leading);
--ui-select-border-width: var(--ui-field-border-width);
--ui-select-border-style: var(--ui-field-border-style);
--ui-select-border-color: var(--ui-field-border-color);
--ui-select-radius: var(--ui-field-radius);
--ui-select-corner-shape: var(--ui-field-corner-shape);
--ui-select-option-gap: var(--ui-field-option-gap);
/* Picker icon (trigger chevron) */
--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-picker-icon-color: currentColor;
/* Option (menu item): a ghost button rendered as a native <option>. Aliases
--ui-button-*; only --ui-option-bg/--ui-option-border-color diverge
(no fill or border). Radius bridges to --ui-menu-button-radius. */
--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-leading: var(--ui-button-leading);
--ui-option-gap: var(--ui-button-gap);
--ui-option-px: var(--ui-button-px);
--ui-option-py: var(--ui-button-py);
--ui-option-min-h: var(--ui-button-min-h);
--ui-option-icon-size: var(--ui-button-icon-size);
--ui-option-radius: var(--ui-menu-button-radius);
--ui-option-corner-shape: var(--ui-menu-button-corner-shape);
--ui-option-shadow: none;
--ui-option-bg: transparent;
--ui-option-bg--hover: var(--ui-button-bg--hover);
--ui-option-bg--active: var(--ui-button-bg--active);
--ui-option-text: var(--ui-button-text);
--ui-option-text--hover: var(--ui-button-text--hover);
--ui-option-text--active: var(--ui-button-text--active);
--ui-option-border-width: var(--ui-button-border-width);
--ui-option-border-style: var(--ui-button-border-style);
--ui-option-border-color: transparent;
--ui-option-border-color--hover: transparent;
--ui-option-border-color--active: transparent;
/* Option checkmark */
--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-checkmark-color: currentColor;
}
}
@layer reset {
/* ===========================================================================
SELECT: native <select> internals (::picker, <option>, ::picker-icon)
- In the reset layer so component overrides win.
- Both the select and ::picker(select) need appearance: base-select before
picker pseudo styles apply.
=========================================================================== */
:where([data-ui~="select"]),
:where([data-ui~="select"])::picker(select) {
appearance: base-select;
}
/* Inline-edge names for the placement matrix's origins, resolved on the
select (where the matrix sets them) so RTL mirrors them; see popover.css. */
:where([data-ui~="select"]) {
--_origin-inline-start: left;
--_origin-inline-end: right;
}
:where([data-ui~="select"]:dir(rtl)) {
--_origin-inline-start: right;
--_origin-inline-end: left;
}
:where([data-ui~="select"])::picker(select) {
transform-origin: var(--ui-popover-origin);
opacity: var(--ui-popover-opacity--start);
transform: scale(var(--ui-popover-scale--start)) translate3d(var(--ui-popover-translate--start));
transition:
opacity var(--default-transition-duration) var(--default-transition-timing-function),
transform var(--default-transition-duration) var(--default-transition-timing-function),
padding var(--default-transition-duration) var(--default-transition-timing-function),
block-size var(--default-transition-duration) var(--default-transition-timing-function),
inline-size var(--default-transition-duration) var(--default-transition-timing-function),
overlay var(--default-transition-duration) var(--default-transition-timing-function)
allow-discrete,
display var(--default-transition-duration) var(--default-transition-timing-function)
allow-discrete;
}
:where([data-ui~="select"]:open)::picker(select) {
opacity: var(--ui-popover-opacity--end);
transform: scale(var(--ui-popover-scale--end)) translate3d(var(--ui-popover-translate--end));
display: flex;
}
@starting-style {
:where([data-ui~="select"]:open)::picker(select) {
opacity: var(--ui-popover-opacity--start);
transform: scale(var(--ui-popover-scale--start))
translate3d(var(--ui-popover-translate--start));
}
}
:where(option)::checkmark {
content: "";
inline-size: var(--ui-option-checkmark-size);
block-size: var(--ui-option-checkmark-size);
background-color: var(--ui-option-checkmark-color);
mask: var(--ui-option-checkmark-mask) center / contain no-repeat;
}
:where([data-ui~="select"])::picker-icon {
content: "";
inline-size: var(--ui-select-picker-icon-size);
block-size: var(--ui-select-picker-icon-size);
background-color: var(--ui-select-picker-icon-color);
mask: var(--ui-select-picker-icon-mask) center / contain no-repeat;
}
}
@layer ui.components {
/* ===========================================================================
SELECT: :where([data-ui~="select"]) (customizable <select>, no JS)
- Needs a <button><selectedcontent></selectedcontent></button> child. Degrades
to the native OS dropdown where unsupported (the _reset.css chevron covers it).
=========================================================================== */
:where([data-ui~="select"]) {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--ui-select-option-gap);
--_w-len: var(--_w-resolved, var(--ui-select-w));
--_w-num: var(--_w-resolved, var(--ui-select-w));
inline-size: calc(var(--_w-len) + var(--_w-num) * var(--spacing));
--_min-w-len: var(--_min-w-resolved, var(--ui-select-min-w));
--_min-w-num: var(--_min-w-resolved, var(--ui-select-min-w));
min-inline-size: calc(var(--_min-w-len) + var(--_min-w-num) * var(--spacing));
--_h-len: var(--_h-resolved, var(--ui-field-h));
--_h-num: var(--_h-resolved, var(--ui-field-h));
/* never shorter than one line plus --ui-select-py and the border above and below (see input.css) */
--_py-len: var(--_py-resolved, var(--ui-select-py));
--_py-num: var(--_py-resolved, var(--ui-select-py));
block-size: max(
calc(var(--_h-len) + var(--_h-num) * var(--spacing)),
var(--_touch-min),
calc(
1lh + 2 *
(
var(--_py-len) + var(--_py-num) * var(--spacing) +
var(--_border-width-resolved, var(--ui-select-border-width))
)
)
);
/* half of what the touch floor added to the height, on each inline side */
--_touch-px: max(
0px,
(var(--_touch-min) - calc(var(--_h-len) + var(--_h-num) * var(--spacing))) / 2
);
--_pl-len: var(--_pl-resolved, var(--ui-select-pl));
--_pl-num: var(--_pl-resolved, var(--ui-select-pl));
padding-inline-start: calc(var(--_pl-len) + var(--_pl-num) * var(--spacing) + var(--_touch-px));
--_pr-len: var(--_pr-resolved, var(--ui-select-pr));
--_pr-num: var(--_pr-resolved, var(--ui-select-pr));
padding-inline-end: calc(var(--_pr-len) + var(--_pr-num) * var(--spacing) + var(--_touch-px));
font-size: var(--_font-size-resolved, var(--ui-select-font-size));
line-height: var(--_leading-resolved, var(--ui-select-leading));
text-align: start;
color: var(--_text-resolved, var(--ui-field-text));
background-color: var(--_bg-resolved, var(--ui-field-bg));
background-image: none;
border-width: var(--_border-width-resolved, var(--ui-select-border-width));
border-style: var(--_border-style-resolved, var(--ui-select-border-style));
border-color: var(--_border-color-resolved, var(--ui-select-border-color));
--_radius-len: var(--_radius-resolved, var(--ui-select-radius));
--_radius-num: var(--_radius-resolved, var(--ui-select-radius));
border-radius: calc(
var(--_radius-len) + var(--_radius-num) * var(--spacing) * var(--radius-multiplier)
);
corner-shape: var(--_corner-shape-resolved, var(--ui-select-corner-shape));
cursor: pointer;
/* the transparent outline keeps focus visible in forced-colors */
--_ring-offset-width: 0px;
--_ring-width: 0px;
--_focus-in: max(0, -1 * sign(var(--_focus-ring-w, 1px)));
--_ring-fill: color-mix(in oklch, var(--ui-field-ring-color) var(--ring-opacity), transparent);
--_focus-shadow:
0 0 0 calc(var(--_ring-offset-width) * (1 - var(--_focus-in))) var(--ring-offset-shadow-color),
0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * (1 - var(--_focus-in)))
var(--_ring-fill, var(--ring-shadow-color)),
inset 0 0 0 calc(var(--_ring-offset-width) * var(--_focus-in)) var(--ring-offset-shadow-color),
inset 0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * var(--_focus-in))
var(--_ring-fill, var(--ring-shadow-color));
box-shadow: var(--_focus-shadow, 0 0 #0000), var(--_shadow-resolved, 0 0 #0000);
outline: abs(var(--_focus-ring-w, var(--outline-width))) var(--outline-style) transparent;
outline-offset: calc(
var(--outline-offset) - var(--_focus-in) *
(2 * var(--outline-offset) + abs(var(--_focus-ring-w, var(--outline-width))))
);
transition: var(--default-transition);
}
@media (hover: hover) {
:where([data-ui~="select"]):hover {
border-color: var(--_border-color-resolved, var(--ui-field-border-color--hover));
}
}
/* :is() takes a forgiving list: an engine without :open keeps the focus ring */
:where([data-ui~="select"]):is(:focus-visible, :open) {
border-color: var(--_border-color-resolved, var(--ui-field-border-color--focus-within));
--_ring-offset-width: var(--ring-offset-width);
--_ring-width: abs(var(--_focus-ring-w, var(--ring-width)));
outline-color: transparent;
}
:where([data-ui~="select"]) > button {
block-size: 100%;
display: flex;
flex: 1;
align-items: center;
gap: var(--ui-field-gap);
min-inline-size: 0;
overflow: hidden;
}
:where([data-ui~="select"]) selectedcontent {
display: flex;
align-items: center;
block-size: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
:where([data-ui~="select"]) > button > svg,
:where([data-ui~="select"]) selectedcontent > svg {
flex-shrink: 0;
inline-size: var(--ui-field-icon-size);
block-size: var(--ui-field-icon-size);
color: var(--color-muted-foreground);
}
:where([data-ui~="select"]):open {
background-color: var(--_bg-resolved, var(--ui-field-bg--focus-within));
}
:where([data-ui~="select"]):open::picker-icon {
transform: rotate(-180deg);
}
/* Where anchor-size() is missing the declaration is dropped, so the panel
keeps max-content sizing. */
:where([data-ui~="select"])::picker(select) {
flex-direction: column;
gap: 1px;
inline-size: max-content;
max-inline-size: var(--ui-popover-max-w);
padding: var(--ui-popover-p);
margin-block: var(--ui-popover-my);
overflow: clip;
background-color: var(--color-popover);
color: var(--color-popover-foreground);
border: 1px solid var(--ui-popover-border-color);
--_radius-len: var(--_radius-resolved, var(--ui-popover-radius));
--_radius-num: var(--_radius-resolved, var(--ui-popover-radius));
border-radius: calc(
var(--_radius-len) + var(--_radius-num) * var(--spacing) * var(--radius-multiplier)
);
corner-shape: var(--_corner-shape-resolved, var(--ui-popover-corner-shape));
box-shadow: var(--ui-popover-shadow);
}
:where([data-ui~="select"])::picker(select) {
min-inline-size: anchor-size(width);
}
/* ===========================================================================
PICKER PLACEMENT: the placement tokens come from the matrix below (select
custom properties inherit into ::picker(select)); applying them is gated on
anchor support.
=========================================================================== */
/* The picker is implicitly anchored, so no anchor-name is needed; gated like
popover.css so unsupporting browsers keep the UA placement. */
@supports (anchor-name: --zazz) {
:where([data-ui~="select"])::picker(select) {
position-area: var(--ui-popover-position-area);
position-try-fallbacks: var(--ui-popover-position-try-fallbacks);
margin-inline: var(--ui-popover-mx);
}
}
/* Like [data-ui~="button"], sizing rides on min-block-size + padding-inline, so
every row is uniform; --ui-option-py only shows once a small --spacing drops
the min height below the label. */
:where([data-ui~="select"]) option {
display: flex;
align-items: center;
gap: var(--ui-option-gap);
min-block-size: max(var(--ui-option-min-h), var(--_touch-min));
padding-block: var(--ui-option-py);
padding-inline: var(--ui-option-px);
font-family: var(--ui-option-font-family);
font-size: var(--ui-option-font-size);
font-weight: var(--ui-option-font-weight);
line-height: var(--ui-option-leading);
color: var(--ui-option-text);
background-color: var(--ui-option-bg);
border: var(--ui-option-border-width) var(--ui-option-border-style)
var(--ui-option-border-color);
border-radius: var(--ui-option-radius);
corner-shape: var(--ui-option-corner-shape);
box-shadow: var(--ui-option-shadow);
cursor: pointer;
transition: var(--default-transition);
}
@media (hover: hover) {
:where([data-ui~="select"]) option:hover {
color: var(--ui-option-text--hover);
background-color: var(--ui-option-bg--hover);
border-color: var(--ui-option-border-color--hover);
}
}
:where([data-ui~="select"]) option:checked {
color: var(--ui-option-text--active);
background-color: var(--ui-option-bg--active);
border-color: var(--ui-option-border-color--active);
}
:where([data-ui~="select"]) option:disabled {
--ui-option-text: var(--color-muted-foreground);
cursor: default;
opacity: 0.5;
}
:where([data-ui~="select"]) option > svg {
flex-shrink: 0;
inline-size: var(--ui-option-icon-size);
block-size: var(--ui-option-icon-size);
color: var(--color-muted-foreground);
}
:where([data-ui~="select"]) option::checkmark {
order: 1;
margin-inline-start: auto;
color: currentcolor;
}
/* ===========================================================================
MULTISELECT: ui-multiselect (JS-projected dropdown over <select multiple>)
- The script hides the real select and stamps a trigger plus a popover of
checkbox rows (see multiselect.ts).
- Without JS the native multi listbox renders with minimal field styling.
=========================================================================== */
:where(ui-multiselect, [data-ui~="multiselect"]) {
anchor-scope: --ui-multiselect-trigger;
display: block;
--_w-len: var(--_w-resolved, var(--ui-select-w));
--_w-num: var(--_w-resolved, var(--ui-select-w));
inline-size: calc(var(--_w-len) + var(--_w-num) * var(--spacing));
--_min-w-len: var(--_min-w-resolved, var(--ui-select-min-w));
--_min-w-num: var(--_min-w-resolved, var(--ui-select-min-w));
min-inline-size: calc(var(--_min-w-len) + var(--_min-w-num) * var(--spacing));
}
/* The real select stays in the DOM as the form value carrier */
:where(ui-multiselect, [data-ui~="multiselect"])
select[multiple][data-multiselect-state~="enhanced"] {
display: none;
}
:where(ui-multiselect, [data-ui~="multiselect"])
select[multiple][data-ui~="select"]:not([data-multiselect-state~="enhanced"]) {
display: block;
block-size: auto;
padding-block: var(--spacing);
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="trigger"] {
anchor-name: --ui-multiselect-trigger;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="label"] {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="icon"] {
flex-shrink: 0;
inline-size: var(--ui-select-picker-icon-size);
block-size: var(--ui-select-picker-icon-size);
background-color: var(--ui-select-picker-icon-color);
mask: var(--ui-select-picker-icon-mask) center / contain no-repeat;
transition: var(--default-transition);
}
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="trigger"][aria-expanded="true"]
[data-multiselect-slot~="icon"] {
transform: rotate(-180deg);
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="panel"] {
position-anchor: --ui-multiselect-trigger;
/* layout stays ungated so the close fade keeps it */
flex-direction: column;
gap: 1px;
box-shadow: var(--ui-menu-shadow);
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="panel"]:popover-open {
display: flex;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="panel"] {
min-inline-size: anchor-size(width);
}
/* The checkbox is real but visually replaced by the shared checkmark glyph */
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"] {
display: flex;
align-items: center;
gap: var(--ui-option-gap);
min-block-size: max(var(--ui-option-min-h), var(--_touch-min));
padding-block: var(--ui-option-py);
padding-inline: var(--ui-option-px);
font-family: var(--ui-option-font-family);
font-size: var(--ui-option-font-size);
font-weight: var(--ui-option-font-weight);
line-height: var(--ui-option-leading);
color: var(--ui-option-text);
background-color: var(--ui-option-bg);
border: var(--ui-option-border-width) var(--ui-option-border-style)
var(--ui-option-border-color);
border-radius: var(--ui-option-radius);
corner-shape: var(--ui-option-corner-shape);
cursor: pointer;
transition: var(--default-transition);
}
@media (hover: hover) {
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"]:hover {
color: var(--ui-option-text--hover);
background-color: var(--ui-option-bg--hover);
}
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"]:has(:checked) {
color: var(--ui-option-text--active);
background-color: var(--ui-option-bg--active);
}
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="option"]:has(:disabled) {
color: var(--color-muted-foreground);
cursor: default;
opacity: 0.5;
}
/* The checkbox stays focusable and announced but invisible; keyboard focus
surfaces on the row via :focus-within */
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="option"]
input[type="checkbox"] {
position: absolute;
inline-size: 1px;
block-size: 1px;
overflow: clip;
clip-path: inset(50%);
white-space: nowrap;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"]:focus-within {
background-color: var(--ui-option-bg--hover);
outline: var(--outline-width) var(--outline-style) var(--ring-shadow-color);
outline-offset: calc(var(--outline-offset) * -1);
}
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="option"]:has(:checked)::after {
content: "";
order: 1;
margin-inline-start: auto;
inline-size: var(--ui-option-checkmark-size);
block-size: var(--ui-option-checkmark-size);
background-color: var(--ui-option-checkmark-color);
mask: var(--ui-option-checkmark-mask) center / contain no-repeat;
}
}
/* An invalid control remaps the --ui-field-* family on itself. */
@layer ui.components {
:where([data-ui~="select"]):disabled {
opacity: 0.5;
cursor: not-allowed;
}
:where([data-ui~="select"]):user-invalid {
--ui-field-bg: var(--color-destructive);
--ui-field-bg--hover: oklch(from var(--color-destructive) calc(l - 0.025) c h);
--ui-field-border-color: var(--color-destructive-foreground);
--ui-field-border-color--hover: var(--color-destructive-foreground);
--ui-field-border-color--focus-within: var(--color-destructive-foreground);
--ui-field-ring-color: var(--color-destructive-foreground);
border-color: var(--color-destructive-foreground);
}
}
/* Placement rows of the popover matrix for the native select, keyed on
data-select-side / data-select-align. */
@layer ui.components {
:where([data-ui~="select"])[data-select-side="bottom"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="bottom"][data-select-align="start"],
:where([data-ui~="select"]:not([data-select-side]):not([data-select-align])),
:where([data-ui~="select"]:not([data-select-side])[data-select-align="start"]) {
--ui-popover-position-area: block-end span-inline-end;
--ui-popover-origin: top var(--_origin-inline-start);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="bottom"][data-select-align="center"],
:where([data-ui~="select"]:not([data-select-side])[data-select-align="center"]) {
--ui-popover-position-area: block-end span-all;
--ui-popover-origin: top center;
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="bottom"][data-select-align="end"],
:where([data-ui~="select"]:not([data-select-side])[data-select-align="end"]) {
--ui-popover-position-area: block-end span-inline-start;
--ui-popover-origin: top var(--_origin-inline-end);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="top"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="top"][data-select-align="start"] {
--ui-popover-position-area: block-start span-inline-end;
--ui-popover-origin: bottom var(--_origin-inline-start);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="top"][data-select-align="center"] {
--ui-popover-position-area: block-start span-all;
--ui-popover-origin: bottom center;
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="top"][data-select-align="end"] {
--ui-popover-position-area: block-start span-inline-start;
--ui-popover-origin: bottom var(--_origin-inline-end);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="right"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="right"][data-select-align="start"] {
--ui-popover-position-area: inline-end span-block-end;
--ui-popover-origin: top var(--_origin-inline-start);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="right"][data-select-align="center"] {
--ui-popover-position-area: inline-end span-all;
--ui-popover-origin: center var(--_origin-inline-start);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="right"][data-select-align="end"] {
--ui-popover-position-area: inline-end span-block-start;
--ui-popover-origin: bottom var(--_origin-inline-start);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="left"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="left"][data-select-align="start"] {
--ui-popover-position-area: inline-start span-block-end;
--ui-popover-origin: top var(--_origin-inline-end);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="left"][data-select-align="center"] {
--ui-popover-position-area: inline-start span-all;
--ui-popover-origin: center var(--_origin-inline-end);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="left"][data-select-align="end"] {
--ui-popover-position-area: inline-start span-block-start;
--ui-popover-origin: bottom var(--_origin-inline-end);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
}// primitives/menu/menu.js
"use strict";
/**
* @fileoverview `<ui-menu>`: HTML web component for keyboard-enhanced menus.
* @description Light-DOM custom element that augments the CSS-only menu
* pattern (trigger + anchored `[data-menu-slot~="popover"]` panel) with
* arrow-key navigation. The attribute form `[data-ui~="menu"]` stays fully functional
* without JavaScript: the Popover API provides open/close, light dismiss,
* and focus return on its own.
*
* Keyboard behavior:
* - ArrowDown / ArrowUp on the closed trigger open the panel and focus the
* first / last item.
* - ArrowDown / ArrowUp inside the open panel move focus between items,
* wrapping around and skipping disabled items.
* - Home / End jump to the first / last item.
* - Escape and light dismiss are native Popover API behavior (no code here).
*
* The menu keeps the honest disclosure posture: items are plain links and
* buttons, and no `role="menu"` is claimed. Add the full ARIA menu contract
* yourself only if every item is an action and you implement the rest of the
* pattern (typeahead, close-on-activate).
*
* @example
* <ui-menu>
* <button data-ui="button" type="button" popovertarget="m1">Open</button>
* <div id="m1" data-menu-slot="popover" popover="auto">
* <menu>
* <li><a href="/docs" data-ui="button" data-button-variant="ghost" style="--justify: start">Docs</a></li>
* </menu>
* </div>
* </ui-menu>
*/
import { ZazzElement, defineZazzElement } from "../../base/zazz-element.js";
class UiMenu extends ZazzElement {
setup(signal) {
this.addEventListener("keydown", (event) => this.#onKeydown(event), { signal });
}
/**
* @description The menu's own panel: a direct child so nested menus keep
* their panels to themselves.
*
* @returns The panel element, or null when the markup is incomplete.
*/
#panel() {
const panel = this.querySelector(':scope > [data-menu-slot~="popover"]');
return panel instanceof HTMLElement ? panel : null;
}
/**
* @description Focusable items inside the panel, in DOM order.
*
* @param panel - The menu panel.
* @returns Enabled links and buttons the arrow keys move between.
*/
#items(panel) {
return Array.from(panel.querySelectorAll("a[href], button"))
.filter((node) => node instanceof HTMLElement)
.filter((item) => !item.hasAttribute("disabled") &&
item.getAttribute("aria-disabled") !== "true" &&
item.closest('ui-menu, [data-ui~="menu"]') === this);
}
/**
* @description Routes arrow-key, Home, and End presses: opens the panel from
* the trigger, or moves focus between items inside the open panel.
*
* @param event - The keydown event.
*/
#onKeydown(event) {
if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key))
return;
const target = event.target;
if (!(target instanceof HTMLElement))
return;
// Ignore keys that belong to a nested ui-menu
if (target.closest('ui-menu, [data-ui~="menu"]') !== this)
return;
const panel = this.#panel();
if (!panel)
return;
const isTrigger = target.parentElement === this &&
(target.hasAttribute("popovertarget") || target.hasAttribute("interestfor"));
const open = panel.matches(":popover-open");
if (isTrigger && !open && (event.key === "ArrowDown" || event.key === "ArrowUp")) {
event.preventDefault();
panel.showPopover();
const items = this.#items(panel);
items[event.key === "ArrowDown" ? 0 : items.length - 1]?.focus();
return;
}
if (!open || !panel.contains(target))
return;
const items = this.#items(panel);
if (items.length === 0)
return;
const index = items.indexOf(target);
let nextIndex;
switch (event.key) {
case "ArrowDown":
nextIndex = index === -1 ? 0 : (index + 1) % items.length;
break;
case "ArrowUp":
nextIndex = index === -1 ? items.length - 1 : (index - 1 + items.length) % items.length;
break;
case "Home":
nextIndex = 0;
break;
case "End":
nextIndex = items.length - 1;
break;
default:
return;
}
event.preventDefault();
items[nextIndex].focus();
}
}
defineZazzElement("ui-menu", UiMenu);
export { UiMenu };
// primitives/select/multiselect.js
"use strict";
/**
* @fileoverview `<ui-multiselect>`: a multi-select styled as a dropdown.
* @description 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).
*/
import { ZazzElement, defineZazzElement } from "../../base/zazz-element.js";
import { effect, state } from "../../base/signals.js";
/**
* @description Formats the trigger label from the selected option labels.
*
* @param labels - Labels of the selected options, in DOM order.
* @param placeholder - Text for an empty selection.
* @param moreTemplate - Overflow template; `{n}` is the remaining count.
* @returns The trigger text.
*/
function resolveTriggerLabel(labels, placeholder, moreTemplate) {
if (labels.length === 0)
return placeholder;
if (labels.length === 1)
return labels[0];
return `${labels[0]} ${moreTemplate.replace("{n}", String(labels.length - 1))}`;
}
let multiselectIdCounter = 0;
class UiMultiselect extends ZazzElement {
#stamped = [];
#select = null;
setup(signal) {
const select = this.querySelector("select[multiple]");
if (!(select instanceof HTMLSelectElement))
return;
this.#select = select;
const placeholder = this.getAttribute("data-multiselect-placeholder") ?? "Select…";
const moreTemplate = this.getAttribute("data-multiselect-label-more") ?? "(+{n} more)";
const { trigger, label, panel, checkboxes } = this.#stamp(select);
select.setAttribute("data-multiselect-state", "enhanced");
const selectedLabels = () => Array.from(select.selectedOptions).map((option) => option.label);
const selection = state(selectedLabels());
// Checkbox → option: the select stays the source of truth
checkboxes.forEach((checkbox, index) => {
checkbox.addEventListener("change", () => {
const option = select.options[index];
if (option)
option.selected = checkbox.checked;
selection.set(selectedLabels());
select.dispatchEvent(new Event("change", { bubbles: true }));
}, { signal });
});
// External writes and form resets → re-read (reset applies after the event)
select.addEventListener("change", () => selection.set(selectedLabels()), { signal });
select.form?.addEventListener("reset", () => queueMicrotask(() => selection.set(selectedLabels())), { signal });
// Mirror popover open state onto the trigger's aria-expanded
panel.addEventListener("toggle", (event) => {
trigger.setAttribute("aria-expanded", String(event.newState === "open"));
}, { signal });
// Output adapter: trigger label + row checked states together
effect(() => {
selection.get();
label.textContent = resolveTriggerLabel(selectedLabels(), placeholder, moreTemplate);
checkboxes.forEach((checkbox, index) => {
const option = select.options[index];
if (option && checkbox.checked !== option.selected)
checkbox.checked = option.selected;
});
}, { signal });
}
teardown() {
for (const node of this.#stamped)
node.remove();
this.#stamped = [];
this.#select?.removeAttribute("data-multiselect-state");
this.#select = null;
}
/**
* @description Builds the trigger button and checkbox panel from the
* select's options and inserts them after it.
*
* @param select - The enhanced select.
* @returns The stamped parts and the checkbox list (option order).
* @private
*/
#stamp(select) {
const panelId = `ui-multiselect-panel-${++multiselectIdCounter}`;
const trigger = document.createElement("button");
trigger.type = "button";
trigger.dataset.ui = "select";
trigger.setAttribute("data-multiselect-slot", "trigger");
trigger.setAttribute("popovertarget", panelId);
trigger.setAttribute("aria-expanded", "false");
const label = document.createElement("span");
label.setAttribute("data-multiselect-slot", "label");
const icon = document.createElement("span");
icon.setAttribute("data-multiselect-slot", "icon");
icon.setAttribute("aria-hidden", "true");
trigger.append(label, icon);
const panel = document.createElement("div");
panel.id = panelId;
panel.setAttribute("data-multiselect-slot", "panel");
panel.setAttribute("popover", "auto");
const side = this.getAttribute("data-multiselect-side");
const align = this.getAttribute("data-multiselect-align");
if (side)
panel.setAttribute("data-popover-side", side);
if (align)
panel.setAttribute("data-popover-align", align);
const checkboxes = [];
for (const [index, option] of Array.from(select.options).entries()) {
const row = document.createElement("label");
row.setAttribute("data-multiselect-slot", "option");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
// an id, not a name: the select submits, and Chrome flags a field with neither
checkbox.id = `${panelId}-${index}`;
checkbox.checked = option.selected;
checkbox.disabled = option.disabled;
row.append(checkbox, document.createTextNode(option.label));
panel.append(row);
checkboxes.push(checkbox);
}
select.after(trigger, panel);
this.#stamped = [trigger, panel];
return { trigger, label, panel, checkboxes };
}
}
defineZazzElement("ui-multiselect", UiMultiselect);
export { UiMultiselect, resolveTriggerLabel };Sides
<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>/**
* select.css: Select (:where([data-ui~="select"]) on customizable <select>)
*
* @layer variables, reset, components
* @requires _layers.css, _variables.css, fields.css, _reset.css, popover.css (--ui-popover-*), button.css (--ui-button-*), menu.css (--ui-menu-*)
* @uses appearance: base-select: customizable select
* @uses ::picker(select), ::picker-icon, selectedcontent, option::checkmark
* @uses @starting-style + allow-discrete: picker open animation
* @uses anchor-size(width): match picker width to trigger
* @uses position-area, position-try-fallbacks: placement tokens come from
* popover.css's shared matrix (@supports gated)
* @uses Degrades to native OS dropdown where base-select is unsupported
* @uses ui-multiselect (multiselect.ts): JS-projected dropdown for
* <select multiple> (base-select has no stable multi-select);
* degrades to the native multi listbox without JS
* @uses --_touch-min (touch screens) — the trigger and every option row grow to the touch
* floor; the trigger's inline sides each gain half of what its height grew
* @uses corner-shape — superellipse corners; unsupported browsers keep plain
* rounded corners
* @tokens --ui-select-* (@layer variables); picker panel reuses --ui-popover-* (owned in popover.css)
* @consumedby autocomplete.css, combobox.css, command.css (--ui-option-*, --ui-select-picker-icon-*)
*/
@layer variables {
:root {
/* Box treatment aliases the shared --ui-field-* family; remap a --ui-select-*
token to diverge just the select. */
--ui-select-w: 100%;
--ui-select-min-w: 16ch;
--ui-select-pl: var(--ui-field-px);
/* room for the picker icon */
--ui-select-pr: calc(var(--spacing) * 2);
--ui-select-py: var(--ui-field-py);
--ui-select-font-size: var(--ui-field-font-size);
--ui-select-leading: var(--ui-field-leading);
--ui-select-border-width: var(--ui-field-border-width);
--ui-select-border-style: var(--ui-field-border-style);
--ui-select-border-color: var(--ui-field-border-color);
--ui-select-radius: var(--ui-field-radius);
--ui-select-corner-shape: var(--ui-field-corner-shape);
--ui-select-option-gap: var(--ui-field-option-gap);
/* Picker icon (trigger chevron) */
--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-picker-icon-color: currentColor;
/* Option (menu item): a ghost button rendered as a native <option>. Aliases
--ui-button-*; only --ui-option-bg/--ui-option-border-color diverge
(no fill or border). Radius bridges to --ui-menu-button-radius. */
--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-leading: var(--ui-button-leading);
--ui-option-gap: var(--ui-button-gap);
--ui-option-px: var(--ui-button-px);
--ui-option-py: var(--ui-button-py);
--ui-option-min-h: var(--ui-button-min-h);
--ui-option-icon-size: var(--ui-button-icon-size);
--ui-option-radius: var(--ui-menu-button-radius);
--ui-option-corner-shape: var(--ui-menu-button-corner-shape);
--ui-option-shadow: none;
--ui-option-bg: transparent;
--ui-option-bg--hover: var(--ui-button-bg--hover);
--ui-option-bg--active: var(--ui-button-bg--active);
--ui-option-text: var(--ui-button-text);
--ui-option-text--hover: var(--ui-button-text--hover);
--ui-option-text--active: var(--ui-button-text--active);
--ui-option-border-width: var(--ui-button-border-width);
--ui-option-border-style: var(--ui-button-border-style);
--ui-option-border-color: transparent;
--ui-option-border-color--hover: transparent;
--ui-option-border-color--active: transparent;
/* Option checkmark */
--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-checkmark-color: currentColor;
}
}
@layer reset {
/* ===========================================================================
SELECT: native <select> internals (::picker, <option>, ::picker-icon)
- In the reset layer so component overrides win.
- Both the select and ::picker(select) need appearance: base-select before
picker pseudo styles apply.
=========================================================================== */
:where([data-ui~="select"]),
:where([data-ui~="select"])::picker(select) {
appearance: base-select;
}
/* Inline-edge names for the placement matrix's origins, resolved on the
select (where the matrix sets them) so RTL mirrors them; see popover.css. */
:where([data-ui~="select"]) {
--_origin-inline-start: left;
--_origin-inline-end: right;
}
:where([data-ui~="select"]:dir(rtl)) {
--_origin-inline-start: right;
--_origin-inline-end: left;
}
:where([data-ui~="select"])::picker(select) {
transform-origin: var(--ui-popover-origin);
opacity: var(--ui-popover-opacity--start);
transform: scale(var(--ui-popover-scale--start)) translate3d(var(--ui-popover-translate--start));
transition:
opacity var(--default-transition-duration) var(--default-transition-timing-function),
transform var(--default-transition-duration) var(--default-transition-timing-function),
padding var(--default-transition-duration) var(--default-transition-timing-function),
block-size var(--default-transition-duration) var(--default-transition-timing-function),
inline-size var(--default-transition-duration) var(--default-transition-timing-function),
overlay var(--default-transition-duration) var(--default-transition-timing-function)
allow-discrete,
display var(--default-transition-duration) var(--default-transition-timing-function)
allow-discrete;
}
:where([data-ui~="select"]:open)::picker(select) {
opacity: var(--ui-popover-opacity--end);
transform: scale(var(--ui-popover-scale--end)) translate3d(var(--ui-popover-translate--end));
display: flex;
}
@starting-style {
:where([data-ui~="select"]:open)::picker(select) {
opacity: var(--ui-popover-opacity--start);
transform: scale(var(--ui-popover-scale--start))
translate3d(var(--ui-popover-translate--start));
}
}
:where(option)::checkmark {
content: "";
inline-size: var(--ui-option-checkmark-size);
block-size: var(--ui-option-checkmark-size);
background-color: var(--ui-option-checkmark-color);
mask: var(--ui-option-checkmark-mask) center / contain no-repeat;
}
:where([data-ui~="select"])::picker-icon {
content: "";
inline-size: var(--ui-select-picker-icon-size);
block-size: var(--ui-select-picker-icon-size);
background-color: var(--ui-select-picker-icon-color);
mask: var(--ui-select-picker-icon-mask) center / contain no-repeat;
}
}
@layer ui.components {
/* ===========================================================================
SELECT: :where([data-ui~="select"]) (customizable <select>, no JS)
- Needs a <button><selectedcontent></selectedcontent></button> child. Degrades
to the native OS dropdown where unsupported (the _reset.css chevron covers it).
=========================================================================== */
:where([data-ui~="select"]) {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--ui-select-option-gap);
--_w-len: var(--_w-resolved, var(--ui-select-w));
--_w-num: var(--_w-resolved, var(--ui-select-w));
inline-size: calc(var(--_w-len) + var(--_w-num) * var(--spacing));
--_min-w-len: var(--_min-w-resolved, var(--ui-select-min-w));
--_min-w-num: var(--_min-w-resolved, var(--ui-select-min-w));
min-inline-size: calc(var(--_min-w-len) + var(--_min-w-num) * var(--spacing));
--_h-len: var(--_h-resolved, var(--ui-field-h));
--_h-num: var(--_h-resolved, var(--ui-field-h));
/* never shorter than one line plus --ui-select-py and the border above and below (see input.css) */
--_py-len: var(--_py-resolved, var(--ui-select-py));
--_py-num: var(--_py-resolved, var(--ui-select-py));
block-size: max(
calc(var(--_h-len) + var(--_h-num) * var(--spacing)),
var(--_touch-min),
calc(
1lh + 2 *
(
var(--_py-len) + var(--_py-num) * var(--spacing) +
var(--_border-width-resolved, var(--ui-select-border-width))
)
)
);
/* half of what the touch floor added to the height, on each inline side */
--_touch-px: max(
0px,
(var(--_touch-min) - calc(var(--_h-len) + var(--_h-num) * var(--spacing))) / 2
);
--_pl-len: var(--_pl-resolved, var(--ui-select-pl));
--_pl-num: var(--_pl-resolved, var(--ui-select-pl));
padding-inline-start: calc(var(--_pl-len) + var(--_pl-num) * var(--spacing) + var(--_touch-px));
--_pr-len: var(--_pr-resolved, var(--ui-select-pr));
--_pr-num: var(--_pr-resolved, var(--ui-select-pr));
padding-inline-end: calc(var(--_pr-len) + var(--_pr-num) * var(--spacing) + var(--_touch-px));
font-size: var(--_font-size-resolved, var(--ui-select-font-size));
line-height: var(--_leading-resolved, var(--ui-select-leading));
text-align: start;
color: var(--_text-resolved, var(--ui-field-text));
background-color: var(--_bg-resolved, var(--ui-field-bg));
background-image: none;
border-width: var(--_border-width-resolved, var(--ui-select-border-width));
border-style: var(--_border-style-resolved, var(--ui-select-border-style));
border-color: var(--_border-color-resolved, var(--ui-select-border-color));
--_radius-len: var(--_radius-resolved, var(--ui-select-radius));
--_radius-num: var(--_radius-resolved, var(--ui-select-radius));
border-radius: calc(
var(--_radius-len) + var(--_radius-num) * var(--spacing) * var(--radius-multiplier)
);
corner-shape: var(--_corner-shape-resolved, var(--ui-select-corner-shape));
cursor: pointer;
/* the transparent outline keeps focus visible in forced-colors */
--_ring-offset-width: 0px;
--_ring-width: 0px;
--_focus-in: max(0, -1 * sign(var(--_focus-ring-w, 1px)));
--_ring-fill: color-mix(in oklch, var(--ui-field-ring-color) var(--ring-opacity), transparent);
--_focus-shadow:
0 0 0 calc(var(--_ring-offset-width) * (1 - var(--_focus-in))) var(--ring-offset-shadow-color),
0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * (1 - var(--_focus-in)))
var(--_ring-fill, var(--ring-shadow-color)),
inset 0 0 0 calc(var(--_ring-offset-width) * var(--_focus-in)) var(--ring-offset-shadow-color),
inset 0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * var(--_focus-in))
var(--_ring-fill, var(--ring-shadow-color));
box-shadow: var(--_focus-shadow, 0 0 #0000), var(--_shadow-resolved, 0 0 #0000);
outline: abs(var(--_focus-ring-w, var(--outline-width))) var(--outline-style) transparent;
outline-offset: calc(
var(--outline-offset) - var(--_focus-in) *
(2 * var(--outline-offset) + abs(var(--_focus-ring-w, var(--outline-width))))
);
transition: var(--default-transition);
}
@media (hover: hover) {
:where([data-ui~="select"]):hover {
border-color: var(--_border-color-resolved, var(--ui-field-border-color--hover));
}
}
/* :is() takes a forgiving list: an engine without :open keeps the focus ring */
:where([data-ui~="select"]):is(:focus-visible, :open) {
border-color: var(--_border-color-resolved, var(--ui-field-border-color--focus-within));
--_ring-offset-width: var(--ring-offset-width);
--_ring-width: abs(var(--_focus-ring-w, var(--ring-width)));
outline-color: transparent;
}
:where([data-ui~="select"]) > button {
block-size: 100%;
display: flex;
flex: 1;
align-items: center;
gap: var(--ui-field-gap);
min-inline-size: 0;
overflow: hidden;
}
:where([data-ui~="select"]) selectedcontent {
display: flex;
align-items: center;
block-size: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
:where([data-ui~="select"]) > button > svg,
:where([data-ui~="select"]) selectedcontent > svg {
flex-shrink: 0;
inline-size: var(--ui-field-icon-size);
block-size: var(--ui-field-icon-size);
color: var(--color-muted-foreground);
}
:where([data-ui~="select"]):open {
background-color: var(--_bg-resolved, var(--ui-field-bg--focus-within));
}
:where([data-ui~="select"]):open::picker-icon {
transform: rotate(-180deg);
}
/* Where anchor-size() is missing the declaration is dropped, so the panel
keeps max-content sizing. */
:where([data-ui~="select"])::picker(select) {
flex-direction: column;
gap: 1px;
inline-size: max-content;
max-inline-size: var(--ui-popover-max-w);
padding: var(--ui-popover-p);
margin-block: var(--ui-popover-my);
overflow: clip;
background-color: var(--color-popover);
color: var(--color-popover-foreground);
border: 1px solid var(--ui-popover-border-color);
--_radius-len: var(--_radius-resolved, var(--ui-popover-radius));
--_radius-num: var(--_radius-resolved, var(--ui-popover-radius));
border-radius: calc(
var(--_radius-len) + var(--_radius-num) * var(--spacing) * var(--radius-multiplier)
);
corner-shape: var(--_corner-shape-resolved, var(--ui-popover-corner-shape));
box-shadow: var(--ui-popover-shadow);
}
:where([data-ui~="select"])::picker(select) {
min-inline-size: anchor-size(width);
}
/* ===========================================================================
PICKER PLACEMENT: the placement tokens come from the matrix below (select
custom properties inherit into ::picker(select)); applying them is gated on
anchor support.
=========================================================================== */
/* The picker is implicitly anchored, so no anchor-name is needed; gated like
popover.css so unsupporting browsers keep the UA placement. */
@supports (anchor-name: --zazz) {
:where([data-ui~="select"])::picker(select) {
position-area: var(--ui-popover-position-area);
position-try-fallbacks: var(--ui-popover-position-try-fallbacks);
margin-inline: var(--ui-popover-mx);
}
}
/* Like [data-ui~="button"], sizing rides on min-block-size + padding-inline, so
every row is uniform; --ui-option-py only shows once a small --spacing drops
the min height below the label. */
:where([data-ui~="select"]) option {
display: flex;
align-items: center;
gap: var(--ui-option-gap);
min-block-size: max(var(--ui-option-min-h), var(--_touch-min));
padding-block: var(--ui-option-py);
padding-inline: var(--ui-option-px);
font-family: var(--ui-option-font-family);
font-size: var(--ui-option-font-size);
font-weight: var(--ui-option-font-weight);
line-height: var(--ui-option-leading);
color: var(--ui-option-text);
background-color: var(--ui-option-bg);
border: var(--ui-option-border-width) var(--ui-option-border-style)
var(--ui-option-border-color);
border-radius: var(--ui-option-radius);
corner-shape: var(--ui-option-corner-shape);
box-shadow: var(--ui-option-shadow);
cursor: pointer;
transition: var(--default-transition);
}
@media (hover: hover) {
:where([data-ui~="select"]) option:hover {
color: var(--ui-option-text--hover);
background-color: var(--ui-option-bg--hover);
border-color: var(--ui-option-border-color--hover);
}
}
:where([data-ui~="select"]) option:checked {
color: var(--ui-option-text--active);
background-color: var(--ui-option-bg--active);
border-color: var(--ui-option-border-color--active);
}
:where([data-ui~="select"]) option:disabled {
--ui-option-text: var(--color-muted-foreground);
cursor: default;
opacity: 0.5;
}
:where([data-ui~="select"]) option > svg {
flex-shrink: 0;
inline-size: var(--ui-option-icon-size);
block-size: var(--ui-option-icon-size);
color: var(--color-muted-foreground);
}
:where([data-ui~="select"]) option::checkmark {
order: 1;
margin-inline-start: auto;
color: currentcolor;
}
/* ===========================================================================
MULTISELECT: ui-multiselect (JS-projected dropdown over <select multiple>)
- The script hides the real select and stamps a trigger plus a popover of
checkbox rows (see multiselect.ts).
- Without JS the native multi listbox renders with minimal field styling.
=========================================================================== */
:where(ui-multiselect, [data-ui~="multiselect"]) {
anchor-scope: --ui-multiselect-trigger;
display: block;
--_w-len: var(--_w-resolved, var(--ui-select-w));
--_w-num: var(--_w-resolved, var(--ui-select-w));
inline-size: calc(var(--_w-len) + var(--_w-num) * var(--spacing));
--_min-w-len: var(--_min-w-resolved, var(--ui-select-min-w));
--_min-w-num: var(--_min-w-resolved, var(--ui-select-min-w));
min-inline-size: calc(var(--_min-w-len) + var(--_min-w-num) * var(--spacing));
}
/* The real select stays in the DOM as the form value carrier */
:where(ui-multiselect, [data-ui~="multiselect"])
select[multiple][data-multiselect-state~="enhanced"] {
display: none;
}
:where(ui-multiselect, [data-ui~="multiselect"])
select[multiple][data-ui~="select"]:not([data-multiselect-state~="enhanced"]) {
display: block;
block-size: auto;
padding-block: var(--spacing);
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="trigger"] {
anchor-name: --ui-multiselect-trigger;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="label"] {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="icon"] {
flex-shrink: 0;
inline-size: var(--ui-select-picker-icon-size);
block-size: var(--ui-select-picker-icon-size);
background-color: var(--ui-select-picker-icon-color);
mask: var(--ui-select-picker-icon-mask) center / contain no-repeat;
transition: var(--default-transition);
}
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="trigger"][aria-expanded="true"]
[data-multiselect-slot~="icon"] {
transform: rotate(-180deg);
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="panel"] {
position-anchor: --ui-multiselect-trigger;
/* layout stays ungated so the close fade keeps it */
flex-direction: column;
gap: 1px;
box-shadow: var(--ui-menu-shadow);
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="panel"]:popover-open {
display: flex;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="panel"] {
min-inline-size: anchor-size(width);
}
/* The checkbox is real but visually replaced by the shared checkmark glyph */
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"] {
display: flex;
align-items: center;
gap: var(--ui-option-gap);
min-block-size: max(var(--ui-option-min-h), var(--_touch-min));
padding-block: var(--ui-option-py);
padding-inline: var(--ui-option-px);
font-family: var(--ui-option-font-family);
font-size: var(--ui-option-font-size);
font-weight: var(--ui-option-font-weight);
line-height: var(--ui-option-leading);
color: var(--ui-option-text);
background-color: var(--ui-option-bg);
border: var(--ui-option-border-width) var(--ui-option-border-style)
var(--ui-option-border-color);
border-radius: var(--ui-option-radius);
corner-shape: var(--ui-option-corner-shape);
cursor: pointer;
transition: var(--default-transition);
}
@media (hover: hover) {
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"]:hover {
color: var(--ui-option-text--hover);
background-color: var(--ui-option-bg--hover);
}
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"]:has(:checked) {
color: var(--ui-option-text--active);
background-color: var(--ui-option-bg--active);
}
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="option"]:has(:disabled) {
color: var(--color-muted-foreground);
cursor: default;
opacity: 0.5;
}
/* The checkbox stays focusable and announced but invisible; keyboard focus
surfaces on the row via :focus-within */
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="option"]
input[type="checkbox"] {
position: absolute;
inline-size: 1px;
block-size: 1px;
overflow: clip;
clip-path: inset(50%);
white-space: nowrap;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"]:focus-within {
background-color: var(--ui-option-bg--hover);
outline: var(--outline-width) var(--outline-style) var(--ring-shadow-color);
outline-offset: calc(var(--outline-offset) * -1);
}
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="option"]:has(:checked)::after {
content: "";
order: 1;
margin-inline-start: auto;
inline-size: var(--ui-option-checkmark-size);
block-size: var(--ui-option-checkmark-size);
background-color: var(--ui-option-checkmark-color);
mask: var(--ui-option-checkmark-mask) center / contain no-repeat;
}
}
/* An invalid control remaps the --ui-field-* family on itself. */
@layer ui.components {
:where([data-ui~="select"]):disabled {
opacity: 0.5;
cursor: not-allowed;
}
:where([data-ui~="select"]):user-invalid {
--ui-field-bg: var(--color-destructive);
--ui-field-bg--hover: oklch(from var(--color-destructive) calc(l - 0.025) c h);
--ui-field-border-color: var(--color-destructive-foreground);
--ui-field-border-color--hover: var(--color-destructive-foreground);
--ui-field-border-color--focus-within: var(--color-destructive-foreground);
--ui-field-ring-color: var(--color-destructive-foreground);
border-color: var(--color-destructive-foreground);
}
}
/* Placement rows of the popover matrix for the native select, keyed on
data-select-side / data-select-align. */
@layer ui.components {
:where([data-ui~="select"])[data-select-side="bottom"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="bottom"][data-select-align="start"],
:where([data-ui~="select"]:not([data-select-side]):not([data-select-align])),
:where([data-ui~="select"]:not([data-select-side])[data-select-align="start"]) {
--ui-popover-position-area: block-end span-inline-end;
--ui-popover-origin: top var(--_origin-inline-start);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="bottom"][data-select-align="center"],
:where([data-ui~="select"]:not([data-select-side])[data-select-align="center"]) {
--ui-popover-position-area: block-end span-all;
--ui-popover-origin: top center;
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="bottom"][data-select-align="end"],
:where([data-ui~="select"]:not([data-select-side])[data-select-align="end"]) {
--ui-popover-position-area: block-end span-inline-start;
--ui-popover-origin: top var(--_origin-inline-end);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="top"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="top"][data-select-align="start"] {
--ui-popover-position-area: block-start span-inline-end;
--ui-popover-origin: bottom var(--_origin-inline-start);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="top"][data-select-align="center"] {
--ui-popover-position-area: block-start span-all;
--ui-popover-origin: bottom center;
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="top"][data-select-align="end"] {
--ui-popover-position-area: block-start span-inline-start;
--ui-popover-origin: bottom var(--_origin-inline-end);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="right"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="right"][data-select-align="start"] {
--ui-popover-position-area: inline-end span-block-end;
--ui-popover-origin: top var(--_origin-inline-start);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="right"][data-select-align="center"] {
--ui-popover-position-area: inline-end span-all;
--ui-popover-origin: center var(--_origin-inline-start);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="right"][data-select-align="end"] {
--ui-popover-position-area: inline-end span-block-start;
--ui-popover-origin: bottom var(--_origin-inline-start);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="left"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="left"][data-select-align="start"] {
--ui-popover-position-area: inline-start span-block-end;
--ui-popover-origin: top var(--_origin-inline-end);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="left"][data-select-align="center"] {
--ui-popover-position-area: inline-start span-all;
--ui-popover-origin: center var(--_origin-inline-end);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="left"][data-select-align="end"] {
--ui-popover-position-area: inline-start span-block-start;
--ui-popover-origin: bottom var(--_origin-inline-end);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
}// primitives/menu/menu.js
"use strict";
/**
* @fileoverview `<ui-menu>`: HTML web component for keyboard-enhanced menus.
* @description Light-DOM custom element that augments the CSS-only menu
* pattern (trigger + anchored `[data-menu-slot~="popover"]` panel) with
* arrow-key navigation. The attribute form `[data-ui~="menu"]` stays fully functional
* without JavaScript: the Popover API provides open/close, light dismiss,
* and focus return on its own.
*
* Keyboard behavior:
* - ArrowDown / ArrowUp on the closed trigger open the panel and focus the
* first / last item.
* - ArrowDown / ArrowUp inside the open panel move focus between items,
* wrapping around and skipping disabled items.
* - Home / End jump to the first / last item.
* - Escape and light dismiss are native Popover API behavior (no code here).
*
* The menu keeps the honest disclosure posture: items are plain links and
* buttons, and no `role="menu"` is claimed. Add the full ARIA menu contract
* yourself only if every item is an action and you implement the rest of the
* pattern (typeahead, close-on-activate).
*
* @example
* <ui-menu>
* <button data-ui="button" type="button" popovertarget="m1">Open</button>
* <div id="m1" data-menu-slot="popover" popover="auto">
* <menu>
* <li><a href="/docs" data-ui="button" data-button-variant="ghost" style="--justify: start">Docs</a></li>
* </menu>
* </div>
* </ui-menu>
*/
import { ZazzElement, defineZazzElement } from "../../base/zazz-element.js";
class UiMenu extends ZazzElement {
setup(signal) {
this.addEventListener("keydown", (event) => this.#onKeydown(event), { signal });
}
/**
* @description The menu's own panel: a direct child so nested menus keep
* their panels to themselves.
*
* @returns The panel element, or null when the markup is incomplete.
*/
#panel() {
const panel = this.querySelector(':scope > [data-menu-slot~="popover"]');
return panel instanceof HTMLElement ? panel : null;
}
/**
* @description Focusable items inside the panel, in DOM order.
*
* @param panel - The menu panel.
* @returns Enabled links and buttons the arrow keys move between.
*/
#items(panel) {
return Array.from(panel.querySelectorAll("a[href], button"))
.filter((node) => node instanceof HTMLElement)
.filter((item) => !item.hasAttribute("disabled") &&
item.getAttribute("aria-disabled") !== "true" &&
item.closest('ui-menu, [data-ui~="menu"]') === this);
}
/**
* @description Routes arrow-key, Home, and End presses: opens the panel from
* the trigger, or moves focus between items inside the open panel.
*
* @param event - The keydown event.
*/
#onKeydown(event) {
if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key))
return;
const target = event.target;
if (!(target instanceof HTMLElement))
return;
// Ignore keys that belong to a nested ui-menu
if (target.closest('ui-menu, [data-ui~="menu"]') !== this)
return;
const panel = this.#panel();
if (!panel)
return;
const isTrigger = target.parentElement === this &&
(target.hasAttribute("popovertarget") || target.hasAttribute("interestfor"));
const open = panel.matches(":popover-open");
if (isTrigger && !open && (event.key === "ArrowDown" || event.key === "ArrowUp")) {
event.preventDefault();
panel.showPopover();
const items = this.#items(panel);
items[event.key === "ArrowDown" ? 0 : items.length - 1]?.focus();
return;
}
if (!open || !panel.contains(target))
return;
const items = this.#items(panel);
if (items.length === 0)
return;
const index = items.indexOf(target);
let nextIndex;
switch (event.key) {
case "ArrowDown":
nextIndex = index === -1 ? 0 : (index + 1) % items.length;
break;
case "ArrowUp":
nextIndex = index === -1 ? items.length - 1 : (index - 1 + items.length) % items.length;
break;
case "Home":
nextIndex = 0;
break;
case "End":
nextIndex = items.length - 1;
break;
default:
return;
}
event.preventDefault();
items[nextIndex].focus();
}
}
defineZazzElement("ui-menu", UiMenu);
export { UiMenu };
// primitives/select/multiselect.js
"use strict";
/**
* @fileoverview `<ui-multiselect>`: a multi-select styled as a dropdown.
* @description 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).
*/
import { ZazzElement, defineZazzElement } from "../../base/zazz-element.js";
import { effect, state } from "../../base/signals.js";
/**
* @description Formats the trigger label from the selected option labels.
*
* @param labels - Labels of the selected options, in DOM order.
* @param placeholder - Text for an empty selection.
* @param moreTemplate - Overflow template; `{n}` is the remaining count.
* @returns The trigger text.
*/
function resolveTriggerLabel(labels, placeholder, moreTemplate) {
if (labels.length === 0)
return placeholder;
if (labels.length === 1)
return labels[0];
return `${labels[0]} ${moreTemplate.replace("{n}", String(labels.length - 1))}`;
}
let multiselectIdCounter = 0;
class UiMultiselect extends ZazzElement {
#stamped = [];
#select = null;
setup(signal) {
const select = this.querySelector("select[multiple]");
if (!(select instanceof HTMLSelectElement))
return;
this.#select = select;
const placeholder = this.getAttribute("data-multiselect-placeholder") ?? "Select…";
const moreTemplate = this.getAttribute("data-multiselect-label-more") ?? "(+{n} more)";
const { trigger, label, panel, checkboxes } = this.#stamp(select);
select.setAttribute("data-multiselect-state", "enhanced");
const selectedLabels = () => Array.from(select.selectedOptions).map((option) => option.label);
const selection = state(selectedLabels());
// Checkbox → option: the select stays the source of truth
checkboxes.forEach((checkbox, index) => {
checkbox.addEventListener("change", () => {
const option = select.options[index];
if (option)
option.selected = checkbox.checked;
selection.set(selectedLabels());
select.dispatchEvent(new Event("change", { bubbles: true }));
}, { signal });
});
// External writes and form resets → re-read (reset applies after the event)
select.addEventListener("change", () => selection.set(selectedLabels()), { signal });
select.form?.addEventListener("reset", () => queueMicrotask(() => selection.set(selectedLabels())), { signal });
// Mirror popover open state onto the trigger's aria-expanded
panel.addEventListener("toggle", (event) => {
trigger.setAttribute("aria-expanded", String(event.newState === "open"));
}, { signal });
// Output adapter: trigger label + row checked states together
effect(() => {
selection.get();
label.textContent = resolveTriggerLabel(selectedLabels(), placeholder, moreTemplate);
checkboxes.forEach((checkbox, index) => {
const option = select.options[index];
if (option && checkbox.checked !== option.selected)
checkbox.checked = option.selected;
});
}, { signal });
}
teardown() {
for (const node of this.#stamped)
node.remove();
this.#stamped = [];
this.#select?.removeAttribute("data-multiselect-state");
this.#select = null;
}
/**
* @description Builds the trigger button and checkbox panel from the
* select's options and inserts them after it.
*
* @param select - The enhanced select.
* @returns The stamped parts and the checkbox list (option order).
* @private
*/
#stamp(select) {
const panelId = `ui-multiselect-panel-${++multiselectIdCounter}`;
const trigger = document.createElement("button");
trigger.type = "button";
trigger.dataset.ui = "select";
trigger.setAttribute("data-multiselect-slot", "trigger");
trigger.setAttribute("popovertarget", panelId);
trigger.setAttribute("aria-expanded", "false");
const label = document.createElement("span");
label.setAttribute("data-multiselect-slot", "label");
const icon = document.createElement("span");
icon.setAttribute("data-multiselect-slot", "icon");
icon.setAttribute("aria-hidden", "true");
trigger.append(label, icon);
const panel = document.createElement("div");
panel.id = panelId;
panel.setAttribute("data-multiselect-slot", "panel");
panel.setAttribute("popover", "auto");
const side = this.getAttribute("data-multiselect-side");
const align = this.getAttribute("data-multiselect-align");
if (side)
panel.setAttribute("data-popover-side", side);
if (align)
panel.setAttribute("data-popover-align", align);
const checkboxes = [];
for (const [index, option] of Array.from(select.options).entries()) {
const row = document.createElement("label");
row.setAttribute("data-multiselect-slot", "option");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
// an id, not a name: the select submits, and Chrome flags a field with neither
checkbox.id = `${panelId}-${index}`;
checkbox.checked = option.selected;
checkbox.disabled = option.disabled;
row.append(checkbox, document.createTextNode(option.label));
panel.append(row);
checkboxes.push(checkbox);
}
select.after(trigger, panel);
this.#stamped = [trigger, panel];
return { trigger, label, panel, checkboxes };
}
}
defineZazzElement("ui-multiselect", UiMultiselect);
export { UiMultiselect, resolveTriggerLabel };Multiple
<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>/**
* select.css: Select (:where([data-ui~="select"]) on customizable <select>)
*
* @layer variables, reset, components
* @requires _layers.css, _variables.css, fields.css, _reset.css, popover.css (--ui-popover-*), button.css (--ui-button-*), menu.css (--ui-menu-*)
* @uses appearance: base-select: customizable select
* @uses ::picker(select), ::picker-icon, selectedcontent, option::checkmark
* @uses @starting-style + allow-discrete: picker open animation
* @uses anchor-size(width): match picker width to trigger
* @uses position-area, position-try-fallbacks: placement tokens come from
* popover.css's shared matrix (@supports gated)
* @uses Degrades to native OS dropdown where base-select is unsupported
* @uses ui-multiselect (multiselect.ts): JS-projected dropdown for
* <select multiple> (base-select has no stable multi-select);
* degrades to the native multi listbox without JS
* @uses --_touch-min (touch screens) — the trigger and every option row grow to the touch
* floor; the trigger's inline sides each gain half of what its height grew
* @uses corner-shape — superellipse corners; unsupported browsers keep plain
* rounded corners
* @tokens --ui-select-* (@layer variables); picker panel reuses --ui-popover-* (owned in popover.css)
* @consumedby autocomplete.css, combobox.css, command.css (--ui-option-*, --ui-select-picker-icon-*)
*/
@layer variables {
:root {
/* Box treatment aliases the shared --ui-field-* family; remap a --ui-select-*
token to diverge just the select. */
--ui-select-w: 100%;
--ui-select-min-w: 16ch;
--ui-select-pl: var(--ui-field-px);
/* room for the picker icon */
--ui-select-pr: calc(var(--spacing) * 2);
--ui-select-py: var(--ui-field-py);
--ui-select-font-size: var(--ui-field-font-size);
--ui-select-leading: var(--ui-field-leading);
--ui-select-border-width: var(--ui-field-border-width);
--ui-select-border-style: var(--ui-field-border-style);
--ui-select-border-color: var(--ui-field-border-color);
--ui-select-radius: var(--ui-field-radius);
--ui-select-corner-shape: var(--ui-field-corner-shape);
--ui-select-option-gap: var(--ui-field-option-gap);
/* Picker icon (trigger chevron) */
--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-picker-icon-color: currentColor;
/* Option (menu item): a ghost button rendered as a native <option>. Aliases
--ui-button-*; only --ui-option-bg/--ui-option-border-color diverge
(no fill or border). Radius bridges to --ui-menu-button-radius. */
--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-leading: var(--ui-button-leading);
--ui-option-gap: var(--ui-button-gap);
--ui-option-px: var(--ui-button-px);
--ui-option-py: var(--ui-button-py);
--ui-option-min-h: var(--ui-button-min-h);
--ui-option-icon-size: var(--ui-button-icon-size);
--ui-option-radius: var(--ui-menu-button-radius);
--ui-option-corner-shape: var(--ui-menu-button-corner-shape);
--ui-option-shadow: none;
--ui-option-bg: transparent;
--ui-option-bg--hover: var(--ui-button-bg--hover);
--ui-option-bg--active: var(--ui-button-bg--active);
--ui-option-text: var(--ui-button-text);
--ui-option-text--hover: var(--ui-button-text--hover);
--ui-option-text--active: var(--ui-button-text--active);
--ui-option-border-width: var(--ui-button-border-width);
--ui-option-border-style: var(--ui-button-border-style);
--ui-option-border-color: transparent;
--ui-option-border-color--hover: transparent;
--ui-option-border-color--active: transparent;
/* Option checkmark */
--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-checkmark-color: currentColor;
}
}
@layer reset {
/* ===========================================================================
SELECT: native <select> internals (::picker, <option>, ::picker-icon)
- In the reset layer so component overrides win.
- Both the select and ::picker(select) need appearance: base-select before
picker pseudo styles apply.
=========================================================================== */
:where([data-ui~="select"]),
:where([data-ui~="select"])::picker(select) {
appearance: base-select;
}
/* Inline-edge names for the placement matrix's origins, resolved on the
select (where the matrix sets them) so RTL mirrors them; see popover.css. */
:where([data-ui~="select"]) {
--_origin-inline-start: left;
--_origin-inline-end: right;
}
:where([data-ui~="select"]:dir(rtl)) {
--_origin-inline-start: right;
--_origin-inline-end: left;
}
:where([data-ui~="select"])::picker(select) {
transform-origin: var(--ui-popover-origin);
opacity: var(--ui-popover-opacity--start);
transform: scale(var(--ui-popover-scale--start)) translate3d(var(--ui-popover-translate--start));
transition:
opacity var(--default-transition-duration) var(--default-transition-timing-function),
transform var(--default-transition-duration) var(--default-transition-timing-function),
padding var(--default-transition-duration) var(--default-transition-timing-function),
block-size var(--default-transition-duration) var(--default-transition-timing-function),
inline-size var(--default-transition-duration) var(--default-transition-timing-function),
overlay var(--default-transition-duration) var(--default-transition-timing-function)
allow-discrete,
display var(--default-transition-duration) var(--default-transition-timing-function)
allow-discrete;
}
:where([data-ui~="select"]:open)::picker(select) {
opacity: var(--ui-popover-opacity--end);
transform: scale(var(--ui-popover-scale--end)) translate3d(var(--ui-popover-translate--end));
display: flex;
}
@starting-style {
:where([data-ui~="select"]:open)::picker(select) {
opacity: var(--ui-popover-opacity--start);
transform: scale(var(--ui-popover-scale--start))
translate3d(var(--ui-popover-translate--start));
}
}
:where(option)::checkmark {
content: "";
inline-size: var(--ui-option-checkmark-size);
block-size: var(--ui-option-checkmark-size);
background-color: var(--ui-option-checkmark-color);
mask: var(--ui-option-checkmark-mask) center / contain no-repeat;
}
:where([data-ui~="select"])::picker-icon {
content: "";
inline-size: var(--ui-select-picker-icon-size);
block-size: var(--ui-select-picker-icon-size);
background-color: var(--ui-select-picker-icon-color);
mask: var(--ui-select-picker-icon-mask) center / contain no-repeat;
}
}
@layer ui.components {
/* ===========================================================================
SELECT: :where([data-ui~="select"]) (customizable <select>, no JS)
- Needs a <button><selectedcontent></selectedcontent></button> child. Degrades
to the native OS dropdown where unsupported (the _reset.css chevron covers it).
=========================================================================== */
:where([data-ui~="select"]) {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--ui-select-option-gap);
--_w-len: var(--_w-resolved, var(--ui-select-w));
--_w-num: var(--_w-resolved, var(--ui-select-w));
inline-size: calc(var(--_w-len) + var(--_w-num) * var(--spacing));
--_min-w-len: var(--_min-w-resolved, var(--ui-select-min-w));
--_min-w-num: var(--_min-w-resolved, var(--ui-select-min-w));
min-inline-size: calc(var(--_min-w-len) + var(--_min-w-num) * var(--spacing));
--_h-len: var(--_h-resolved, var(--ui-field-h));
--_h-num: var(--_h-resolved, var(--ui-field-h));
/* never shorter than one line plus --ui-select-py and the border above and below (see input.css) */
--_py-len: var(--_py-resolved, var(--ui-select-py));
--_py-num: var(--_py-resolved, var(--ui-select-py));
block-size: max(
calc(var(--_h-len) + var(--_h-num) * var(--spacing)),
var(--_touch-min),
calc(
1lh + 2 *
(
var(--_py-len) + var(--_py-num) * var(--spacing) +
var(--_border-width-resolved, var(--ui-select-border-width))
)
)
);
/* half of what the touch floor added to the height, on each inline side */
--_touch-px: max(
0px,
(var(--_touch-min) - calc(var(--_h-len) + var(--_h-num) * var(--spacing))) / 2
);
--_pl-len: var(--_pl-resolved, var(--ui-select-pl));
--_pl-num: var(--_pl-resolved, var(--ui-select-pl));
padding-inline-start: calc(var(--_pl-len) + var(--_pl-num) * var(--spacing) + var(--_touch-px));
--_pr-len: var(--_pr-resolved, var(--ui-select-pr));
--_pr-num: var(--_pr-resolved, var(--ui-select-pr));
padding-inline-end: calc(var(--_pr-len) + var(--_pr-num) * var(--spacing) + var(--_touch-px));
font-size: var(--_font-size-resolved, var(--ui-select-font-size));
line-height: var(--_leading-resolved, var(--ui-select-leading));
text-align: start;
color: var(--_text-resolved, var(--ui-field-text));
background-color: var(--_bg-resolved, var(--ui-field-bg));
background-image: none;
border-width: var(--_border-width-resolved, var(--ui-select-border-width));
border-style: var(--_border-style-resolved, var(--ui-select-border-style));
border-color: var(--_border-color-resolved, var(--ui-select-border-color));
--_radius-len: var(--_radius-resolved, var(--ui-select-radius));
--_radius-num: var(--_radius-resolved, var(--ui-select-radius));
border-radius: calc(
var(--_radius-len) + var(--_radius-num) * var(--spacing) * var(--radius-multiplier)
);
corner-shape: var(--_corner-shape-resolved, var(--ui-select-corner-shape));
cursor: pointer;
/* the transparent outline keeps focus visible in forced-colors */
--_ring-offset-width: 0px;
--_ring-width: 0px;
--_focus-in: max(0, -1 * sign(var(--_focus-ring-w, 1px)));
--_ring-fill: color-mix(in oklch, var(--ui-field-ring-color) var(--ring-opacity), transparent);
--_focus-shadow:
0 0 0 calc(var(--_ring-offset-width) * (1 - var(--_focus-in))) var(--ring-offset-shadow-color),
0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * (1 - var(--_focus-in)))
var(--_ring-fill, var(--ring-shadow-color)),
inset 0 0 0 calc(var(--_ring-offset-width) * var(--_focus-in)) var(--ring-offset-shadow-color),
inset 0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * var(--_focus-in))
var(--_ring-fill, var(--ring-shadow-color));
box-shadow: var(--_focus-shadow, 0 0 #0000), var(--_shadow-resolved, 0 0 #0000);
outline: abs(var(--_focus-ring-w, var(--outline-width))) var(--outline-style) transparent;
outline-offset: calc(
var(--outline-offset) - var(--_focus-in) *
(2 * var(--outline-offset) + abs(var(--_focus-ring-w, var(--outline-width))))
);
transition: var(--default-transition);
}
@media (hover: hover) {
:where([data-ui~="select"]):hover {
border-color: var(--_border-color-resolved, var(--ui-field-border-color--hover));
}
}
/* :is() takes a forgiving list: an engine without :open keeps the focus ring */
:where([data-ui~="select"]):is(:focus-visible, :open) {
border-color: var(--_border-color-resolved, var(--ui-field-border-color--focus-within));
--_ring-offset-width: var(--ring-offset-width);
--_ring-width: abs(var(--_focus-ring-w, var(--ring-width)));
outline-color: transparent;
}
:where([data-ui~="select"]) > button {
block-size: 100%;
display: flex;
flex: 1;
align-items: center;
gap: var(--ui-field-gap);
min-inline-size: 0;
overflow: hidden;
}
:where([data-ui~="select"]) selectedcontent {
display: flex;
align-items: center;
block-size: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
:where([data-ui~="select"]) > button > svg,
:where([data-ui~="select"]) selectedcontent > svg {
flex-shrink: 0;
inline-size: var(--ui-field-icon-size);
block-size: var(--ui-field-icon-size);
color: var(--color-muted-foreground);
}
:where([data-ui~="select"]):open {
background-color: var(--_bg-resolved, var(--ui-field-bg--focus-within));
}
:where([data-ui~="select"]):open::picker-icon {
transform: rotate(-180deg);
}
/* Where anchor-size() is missing the declaration is dropped, so the panel
keeps max-content sizing. */
:where([data-ui~="select"])::picker(select) {
flex-direction: column;
gap: 1px;
inline-size: max-content;
max-inline-size: var(--ui-popover-max-w);
padding: var(--ui-popover-p);
margin-block: var(--ui-popover-my);
overflow: clip;
background-color: var(--color-popover);
color: var(--color-popover-foreground);
border: 1px solid var(--ui-popover-border-color);
--_radius-len: var(--_radius-resolved, var(--ui-popover-radius));
--_radius-num: var(--_radius-resolved, var(--ui-popover-radius));
border-radius: calc(
var(--_radius-len) + var(--_radius-num) * var(--spacing) * var(--radius-multiplier)
);
corner-shape: var(--_corner-shape-resolved, var(--ui-popover-corner-shape));
box-shadow: var(--ui-popover-shadow);
}
:where([data-ui~="select"])::picker(select) {
min-inline-size: anchor-size(width);
}
/* ===========================================================================
PICKER PLACEMENT: the placement tokens come from the matrix below (select
custom properties inherit into ::picker(select)); applying them is gated on
anchor support.
=========================================================================== */
/* The picker is implicitly anchored, so no anchor-name is needed; gated like
popover.css so unsupporting browsers keep the UA placement. */
@supports (anchor-name: --zazz) {
:where([data-ui~="select"])::picker(select) {
position-area: var(--ui-popover-position-area);
position-try-fallbacks: var(--ui-popover-position-try-fallbacks);
margin-inline: var(--ui-popover-mx);
}
}
/* Like [data-ui~="button"], sizing rides on min-block-size + padding-inline, so
every row is uniform; --ui-option-py only shows once a small --spacing drops
the min height below the label. */
:where([data-ui~="select"]) option {
display: flex;
align-items: center;
gap: var(--ui-option-gap);
min-block-size: max(var(--ui-option-min-h), var(--_touch-min));
padding-block: var(--ui-option-py);
padding-inline: var(--ui-option-px);
font-family: var(--ui-option-font-family);
font-size: var(--ui-option-font-size);
font-weight: var(--ui-option-font-weight);
line-height: var(--ui-option-leading);
color: var(--ui-option-text);
background-color: var(--ui-option-bg);
border: var(--ui-option-border-width) var(--ui-option-border-style)
var(--ui-option-border-color);
border-radius: var(--ui-option-radius);
corner-shape: var(--ui-option-corner-shape);
box-shadow: var(--ui-option-shadow);
cursor: pointer;
transition: var(--default-transition);
}
@media (hover: hover) {
:where([data-ui~="select"]) option:hover {
color: var(--ui-option-text--hover);
background-color: var(--ui-option-bg--hover);
border-color: var(--ui-option-border-color--hover);
}
}
:where([data-ui~="select"]) option:checked {
color: var(--ui-option-text--active);
background-color: var(--ui-option-bg--active);
border-color: var(--ui-option-border-color--active);
}
:where([data-ui~="select"]) option:disabled {
--ui-option-text: var(--color-muted-foreground);
cursor: default;
opacity: 0.5;
}
:where([data-ui~="select"]) option > svg {
flex-shrink: 0;
inline-size: var(--ui-option-icon-size);
block-size: var(--ui-option-icon-size);
color: var(--color-muted-foreground);
}
:where([data-ui~="select"]) option::checkmark {
order: 1;
margin-inline-start: auto;
color: currentcolor;
}
/* ===========================================================================
MULTISELECT: ui-multiselect (JS-projected dropdown over <select multiple>)
- The script hides the real select and stamps a trigger plus a popover of
checkbox rows (see multiselect.ts).
- Without JS the native multi listbox renders with minimal field styling.
=========================================================================== */
:where(ui-multiselect, [data-ui~="multiselect"]) {
anchor-scope: --ui-multiselect-trigger;
display: block;
--_w-len: var(--_w-resolved, var(--ui-select-w));
--_w-num: var(--_w-resolved, var(--ui-select-w));
inline-size: calc(var(--_w-len) + var(--_w-num) * var(--spacing));
--_min-w-len: var(--_min-w-resolved, var(--ui-select-min-w));
--_min-w-num: var(--_min-w-resolved, var(--ui-select-min-w));
min-inline-size: calc(var(--_min-w-len) + var(--_min-w-num) * var(--spacing));
}
/* The real select stays in the DOM as the form value carrier */
:where(ui-multiselect, [data-ui~="multiselect"])
select[multiple][data-multiselect-state~="enhanced"] {
display: none;
}
:where(ui-multiselect, [data-ui~="multiselect"])
select[multiple][data-ui~="select"]:not([data-multiselect-state~="enhanced"]) {
display: block;
block-size: auto;
padding-block: var(--spacing);
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="trigger"] {
anchor-name: --ui-multiselect-trigger;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="label"] {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="icon"] {
flex-shrink: 0;
inline-size: var(--ui-select-picker-icon-size);
block-size: var(--ui-select-picker-icon-size);
background-color: var(--ui-select-picker-icon-color);
mask: var(--ui-select-picker-icon-mask) center / contain no-repeat;
transition: var(--default-transition);
}
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="trigger"][aria-expanded="true"]
[data-multiselect-slot~="icon"] {
transform: rotate(-180deg);
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="panel"] {
position-anchor: --ui-multiselect-trigger;
/* layout stays ungated so the close fade keeps it */
flex-direction: column;
gap: 1px;
box-shadow: var(--ui-menu-shadow);
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="panel"]:popover-open {
display: flex;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="panel"] {
min-inline-size: anchor-size(width);
}
/* The checkbox is real but visually replaced by the shared checkmark glyph */
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"] {
display: flex;
align-items: center;
gap: var(--ui-option-gap);
min-block-size: max(var(--ui-option-min-h), var(--_touch-min));
padding-block: var(--ui-option-py);
padding-inline: var(--ui-option-px);
font-family: var(--ui-option-font-family);
font-size: var(--ui-option-font-size);
font-weight: var(--ui-option-font-weight);
line-height: var(--ui-option-leading);
color: var(--ui-option-text);
background-color: var(--ui-option-bg);
border: var(--ui-option-border-width) var(--ui-option-border-style)
var(--ui-option-border-color);
border-radius: var(--ui-option-radius);
corner-shape: var(--ui-option-corner-shape);
cursor: pointer;
transition: var(--default-transition);
}
@media (hover: hover) {
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"]:hover {
color: var(--ui-option-text--hover);
background-color: var(--ui-option-bg--hover);
}
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"]:has(:checked) {
color: var(--ui-option-text--active);
background-color: var(--ui-option-bg--active);
}
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="option"]:has(:disabled) {
color: var(--color-muted-foreground);
cursor: default;
opacity: 0.5;
}
/* The checkbox stays focusable and announced but invisible; keyboard focus
surfaces on the row via :focus-within */
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="option"]
input[type="checkbox"] {
position: absolute;
inline-size: 1px;
block-size: 1px;
overflow: clip;
clip-path: inset(50%);
white-space: nowrap;
}
:where(ui-multiselect, [data-ui~="multiselect"]) [data-multiselect-slot~="option"]:focus-within {
background-color: var(--ui-option-bg--hover);
outline: var(--outline-width) var(--outline-style) var(--ring-shadow-color);
outline-offset: calc(var(--outline-offset) * -1);
}
:where(ui-multiselect, [data-ui~="multiselect"])
[data-multiselect-slot~="option"]:has(:checked)::after {
content: "";
order: 1;
margin-inline-start: auto;
inline-size: var(--ui-option-checkmark-size);
block-size: var(--ui-option-checkmark-size);
background-color: var(--ui-option-checkmark-color);
mask: var(--ui-option-checkmark-mask) center / contain no-repeat;
}
}
/* An invalid control remaps the --ui-field-* family on itself. */
@layer ui.components {
:where([data-ui~="select"]):disabled {
opacity: 0.5;
cursor: not-allowed;
}
:where([data-ui~="select"]):user-invalid {
--ui-field-bg: var(--color-destructive);
--ui-field-bg--hover: oklch(from var(--color-destructive) calc(l - 0.025) c h);
--ui-field-border-color: var(--color-destructive-foreground);
--ui-field-border-color--hover: var(--color-destructive-foreground);
--ui-field-border-color--focus-within: var(--color-destructive-foreground);
--ui-field-ring-color: var(--color-destructive-foreground);
border-color: var(--color-destructive-foreground);
}
}
/* Placement rows of the popover matrix for the native select, keyed on
data-select-side / data-select-align. */
@layer ui.components {
:where([data-ui~="select"])[data-select-side="bottom"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="bottom"][data-select-align="start"],
:where([data-ui~="select"]:not([data-select-side]):not([data-select-align])),
:where([data-ui~="select"]:not([data-select-side])[data-select-align="start"]) {
--ui-popover-position-area: block-end span-inline-end;
--ui-popover-origin: top var(--_origin-inline-start);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="bottom"][data-select-align="center"],
:where([data-ui~="select"]:not([data-select-side])[data-select-align="center"]) {
--ui-popover-position-area: block-end span-all;
--ui-popover-origin: top center;
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="bottom"][data-select-align="end"],
:where([data-ui~="select"]:not([data-select-side])[data-select-align="end"]) {
--ui-popover-position-area: block-end span-inline-start;
--ui-popover-origin: top var(--_origin-inline-end);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="top"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="top"][data-select-align="start"] {
--ui-popover-position-area: block-start span-inline-end;
--ui-popover-origin: bottom var(--_origin-inline-start);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="top"][data-select-align="center"] {
--ui-popover-position-area: block-start span-all;
--ui-popover-origin: bottom center;
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="top"][data-select-align="end"] {
--ui-popover-position-area: block-start span-inline-start;
--ui-popover-origin: bottom var(--_origin-inline-end);
--ui-popover-my: calc(var(--spacing) * 1.5);
--ui-popover-mx: 0;
}
:where([data-ui~="select"])[data-select-side="right"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="right"][data-select-align="start"] {
--ui-popover-position-area: inline-end span-block-end;
--ui-popover-origin: top var(--_origin-inline-start);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="right"][data-select-align="center"] {
--ui-popover-position-area: inline-end span-all;
--ui-popover-origin: center var(--_origin-inline-start);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="right"][data-select-align="end"] {
--ui-popover-position-area: inline-end span-block-start;
--ui-popover-origin: bottom var(--_origin-inline-start);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="left"]:not([data-select-align]),
:where([data-ui~="select"])[data-select-side="left"][data-select-align="start"] {
--ui-popover-position-area: inline-start span-block-end;
--ui-popover-origin: top var(--_origin-inline-end);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="left"][data-select-align="center"] {
--ui-popover-position-area: inline-start span-all;
--ui-popover-origin: center var(--_origin-inline-end);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
:where([data-ui~="select"])[data-select-side="left"][data-select-align="end"] {
--ui-popover-position-area: inline-start span-block-start;
--ui-popover-origin: bottom var(--_origin-inline-end);
--ui-popover-my: 0;
--ui-popover-mx: calc(var(--spacing) * 1.5);
}
}// primitives/menu/menu.js
"use strict";
/**
* @fileoverview `<ui-menu>`: HTML web component for keyboard-enhanced menus.
* @description Light-DOM custom element that augments the CSS-only menu
* pattern (trigger + anchored `[data-menu-slot~="popover"]` panel) with
* arrow-key navigation. The attribute form `[data-ui~="menu"]` stays fully functional
* without JavaScript: the Popover API provides open/close, light dismiss,
* and focus return on its own.
*
* Keyboard behavior:
* - ArrowDown / ArrowUp on the closed trigger open the panel and focus the
* first / last item.
* - ArrowDown / ArrowUp inside the open panel move focus between items,
* wrapping around and skipping disabled items.
* - Home / End jump to the first / last item.
* - Escape and light dismiss are native Popover API behavior (no code here).
*
* The menu keeps the honest disclosure posture: items are plain links and
* buttons, and no `role="menu"` is claimed. Add the full ARIA menu contract
* yourself only if every item is an action and you implement the rest of the
* pattern (typeahead, close-on-activate).
*
* @example
* <ui-menu>
* <button data-ui="button" type="button" popovertarget="m1">Open</button>
* <div id="m1" data-menu-slot="popover" popover="auto">
* <menu>
* <li><a href="/docs" data-ui="button" data-button-variant="ghost" style="--justify: start">Docs</a></li>
* </menu>
* </div>
* </ui-menu>
*/
import { ZazzElement, defineZazzElement } from "../../base/zazz-element.js";
class UiMenu extends ZazzElement {
setup(signal) {
this.addEventListener("keydown", (event) => this.#onKeydown(event), { signal });
}
/**
* @description The menu's own panel: a direct child so nested menus keep
* their panels to themselves.
*
* @returns The panel element, or null when the markup is incomplete.
*/
#panel() {
const panel = this.querySelector(':scope > [data-menu-slot~="popover"]');
return panel instanceof HTMLElement ? panel : null;
}
/**
* @description Focusable items inside the panel, in DOM order.
*
* @param panel - The menu panel.
* @returns Enabled links and buttons the arrow keys move between.
*/
#items(panel) {
return Array.from(panel.querySelectorAll("a[href], button"))
.filter((node) => node instanceof HTMLElement)
.filter((item) => !item.hasAttribute("disabled") &&
item.getAttribute("aria-disabled") !== "true" &&
item.closest('ui-menu, [data-ui~="menu"]') === this);
}
/**
* @description Routes arrow-key, Home, and End presses: opens the panel from
* the trigger, or moves focus between items inside the open panel.
*
* @param event - The keydown event.
*/
#onKeydown(event) {
if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key))
return;
const target = event.target;
if (!(target instanceof HTMLElement))
return;
// Ignore keys that belong to a nested ui-menu
if (target.closest('ui-menu, [data-ui~="menu"]') !== this)
return;
const panel = this.#panel();
if (!panel)
return;
const isTrigger = target.parentElement === this &&
(target.hasAttribute("popovertarget") || target.hasAttribute("interestfor"));
const open = panel.matches(":popover-open");
if (isTrigger && !open && (event.key === "ArrowDown" || event.key === "ArrowUp")) {
event.preventDefault();
panel.showPopover();
const items = this.#items(panel);
items[event.key === "ArrowDown" ? 0 : items.length - 1]?.focus();
return;
}
if (!open || !panel.contains(target))
return;
const items = this.#items(panel);
if (items.length === 0)
return;
const index = items.indexOf(target);
let nextIndex;
switch (event.key) {
case "ArrowDown":
nextIndex = index === -1 ? 0 : (index + 1) % items.length;
break;
case "ArrowUp":
nextIndex = index === -1 ? items.length - 1 : (index - 1 + items.length) % items.length;
break;
case "Home":
nextIndex = 0;
break;
case "End":
nextIndex = items.length - 1;
break;
default:
return;
}
event.preventDefault();
items[nextIndex].focus();
}
}
defineZazzElement("ui-menu", UiMenu);
export { UiMenu };
// primitives/select/multiselect.js
"use strict";
/**
* @fileoverview `<ui-multiselect>`: a multi-select styled as a dropdown.
* @description 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).
*/
import { ZazzElement, defineZazzElement } from "../../base/zazz-element.js";
import { effect, state } from "../../base/signals.js";
/**
* @description Formats the trigger label from the selected option labels.
*
* @param labels - Labels of the selected options, in DOM order.
* @param placeholder - Text for an empty selection.
* @param moreTemplate - Overflow template; `{n}` is the remaining count.
* @returns The trigger text.
*/
function resolveTriggerLabel(labels, placeholder, moreTemplate) {
if (labels.length === 0)
return placeholder;
if (labels.length === 1)
return labels[0];
return `${labels[0]} ${moreTemplate.replace("{n}", String(labels.length - 1))}`;
}
let multiselectIdCounter = 0;
class UiMultiselect extends ZazzElement {
#stamped = [];
#select = null;
setup(signal) {
const select = this.querySelector("select[multiple]");
if (!(select instanceof HTMLSelectElement))
return;
this.#select = select;
const placeholder = this.getAttribute("data-multiselect-placeholder") ?? "Select…";
const moreTemplate = this.getAttribute("data-multiselect-label-more") ?? "(+{n} more)";
const { trigger, label, panel, checkboxes } = this.#stamp(select);
select.setAttribute("data-multiselect-state", "enhanced");
const selectedLabels = () => Array.from(select.selectedOptions).map((option) => option.label);
const selection = state(selectedLabels());
// Checkbox → option: the select stays the source of truth
checkboxes.forEach((checkbox, index) => {
checkbox.addEventListener("change", () => {
const option = select.options[index];
if (option)
option.selected = checkbox.checked;
selection.set(selectedLabels());
select.dispatchEvent(new Event("change", { bubbles: true }));
}, { signal });
});
// External writes and form resets → re-read (reset applies after the event)
select.addEventListener("change", () => selection.set(selectedLabels()), { signal });
select.form?.addEventListener("reset", () => queueMicrotask(() => selection.set(selectedLabels())), { signal });
// Mirror popover open state onto the trigger's aria-expanded
panel.addEventListener("toggle", (event) => {
trigger.setAttribute("aria-expanded", String(event.newState === "open"));
}, { signal });
// Output adapter: trigger label + row checked states together
effect(() => {
selection.get();
label.textContent = resolveTriggerLabel(selectedLabels(), placeholder, moreTemplate);
checkboxes.forEach((checkbox, index) => {
const option = select.options[index];
if (option && checkbox.checked !== option.selected)
checkbox.checked = option.selected;
});
}, { signal });
}
teardown() {
for (const node of this.#stamped)
node.remove();
this.#stamped = [];
this.#select?.removeAttribute("data-multiselect-state");
this.#select = null;
}
/**
* @description Builds the trigger button and checkbox panel from the
* select's options and inserts them after it.
*
* @param select - The enhanced select.
* @returns The stamped parts and the checkbox list (option order).
* @private
*/
#stamp(select) {
const panelId = `ui-multiselect-panel-${++multiselectIdCounter}`;
const trigger = document.createElement("button");
trigger.type = "button";
trigger.dataset.ui = "select";
trigger.setAttribute("data-multiselect-slot", "trigger");
trigger.setAttribute("popovertarget", panelId);
trigger.setAttribute("aria-expanded", "false");
const label = document.createElement("span");
label.setAttribute("data-multiselect-slot", "label");
const icon = document.createElement("span");
icon.setAttribute("data-multiselect-slot", "icon");
icon.setAttribute("aria-hidden", "true");
trigger.append(label, icon);
const panel = document.createElement("div");
panel.id = panelId;
panel.setAttribute("data-multiselect-slot", "panel");
panel.setAttribute("popover", "auto");
const side = this.getAttribute("data-multiselect-side");
const align = this.getAttribute("data-multiselect-align");
if (side)
panel.setAttribute("data-popover-side", side);
if (align)
panel.setAttribute("data-popover-align", align);
const checkboxes = [];
for (const [index, option] of Array.from(select.options).entries()) {
const row = document.createElement("label");
row.setAttribute("data-multiselect-slot", "option");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
// an id, not a name: the select submits, and Chrome flags a field with neither
checkbox.id = `${panelId}-${index}`;
checkbox.checked = option.selected;
checkbox.disabled = option.disabled;
row.append(checkbox, document.createTextNode(option.label));
panel.append(row);
checkboxes.push(checkbox);
}
select.after(trigger, panel);
this.#stamped = [trigger, panel];
return { trigger, label, panel, checkboxes };
}
}
defineZazzElement("ui-multiselect", UiMultiselect);
export { UiMultiselect, resolveTriggerLabel };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 asdata-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 | Notes |
|---|---|---|---|
data-ui="multiselect" | Identity | free value | Identity token in data-ui. |
data-ui="select" | Identity | free value | Identity token in data-ui. |
<ui-multiselect> | Tag form | free value | Tag form; styled and, when scripted, registered under this name. |
data-select-align | Preset | center end start | Preset: a named bundle of values. |
data-select-side | Preset | bottom left right top | Preset: a named bundle of values. |
data-multiselect-slot | Slot | icon label option panel trigger | Slot: a part inside the primitive. |
data-multiselect-state | State | enhanced | State the kit's script writes; read it in selectors, never author it. |
data-multiselect-align | Config | free value | Configuration the kit's script reads. |
data-multiselect-label-more | Config | free value | Configuration the kit's script reads. |
data-multiselect-placeholder | Config | free value | Configuration the kit's script reads. |
data-multiselect-side | Config | free value | Configuration the kit's script reads. |
Requires: fields, popover, button, menu.
Hooks
Set a hook on :root, on a subtree, or inline on one element. A state hook (--…--hover) themes that state only; the primitive keeps its other states.
| 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% | — |