Select Item
Use selectItem on a <div> placed inside a selectList. It reads the select context to set aria-selected and handle click-to-toggle. In single mode the item becomes selected; in multiple mode it toggles its value in the array.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | number | null | null | The option value compared against and written to the select state. |
color | ThemeColor | "neutral" | Text and resting background tone. |
accentColor | ThemeColor | "primary" | Selected state and focus tone. |
Customization
Must see the source of patch at the bottom of each patch page to understand the structure then code it still code as html native element.
There are four levels of customization, in increasing order of effort:
- Patch props. Each patch exposes a small, stable set of props—typically fewer than five. Lowest friction.
- Context attributes. Use
dataTone,dataSize, anddataDensityon a container to shift tone, size, or density for an entire subtree without touching individual elements. - Inline override. Native-wins merge strategy: any property set directly on the element overrides the patch value.
- Create a variant. Clone a similar patch and edit it. Use this only when you need a reusable custom version.
Formulas
Unit - U = fontSize / 4 - convert final values with themeSpacing(n).
Size - n = intrinsic text lines, w = wrapping level, d = density factor:
height = (n * 6 + 2 * d * w) * U
paddingBlock = d * w * U
paddingInline = ceil(3 / w) * d * w * U
radius = d * w * U
Base density d = 1.5:
| U | w=0 | w=1 | w=2 | w=3 |
|---|---|---|---|---|
height (n = 1) | 6 | 9 | 12 | 15 |
| paddingBlock | 0 | 1.5 | 3 | 4.5 |
| paddingInline | 3 | 4.5 | 6 | 4.5 |
| radius | 0 | 1.5 | 3 | 4.5 |
Tone - K = N / 2 where N is the palette length. For N = 18, K = 9.
| Role | Shift | n=0 |
|---|---|---|
| Background | parent +/- n | 0 |
| Text | bg + K | 6 |
| Border | bg + K/2 | 3 |
| Hover | bg + 2K/3 | 4 |
| Selected / Focus | above +/- K/3 | 2-4 |
State shift range: K/3 <= delta <= 2K/3.
<div class="blocks">
<div class="block active" data-tab="0">
import type { PartialElement } from "@domphy/core";
import {
type ThemeColor,
themeColor,
themeDensity,
themeSize,
themeSpacing,
} from "@domphy/theme";
/**
* A single selectable option row (`role="option"`) for use inside a `selectList`. Reads the
* `select` context to reflect/toggle selection: it sets `aria-selected` from the bound value and
* toggles the value (single or multiple) on click. Styles hover/selected/focus states.
*
* @hostTag div
* @param props.accentColor - Theme color tone for the selected/focus state. Defaults to `"primary"`.
* @param props.color - Theme color tone for text/background. Defaults to `"neutral"`.
* @param props.value - The option value compared against and written to the select state.
* Defaults to `null`.
* @example { div: "Option A", $: [selectItem({ value: "a" })] }
*/
function selectItem(
props: {
accentColor?: ThemeColor;
color?: ThemeColor;
value?: number | string;
} = {},
): PartialElement {
const { accentColor = "primary", color = "neutral", value = null } = props;
const partial: PartialElement = {
role: "option",
_onInit: (node) => {
if (node.tagName !== "div") {
console.warn(`"selectItem" patch must use div tag`);
}
const select = node.getContext("select");
if (select) {
const state = select.value;
node.attributes.set("ariaSelected", (listener) => {
const val = state.get(listener);
return select.multiple ? val.includes(value) : val === value;
});
node.addEvent("click", () => {
const val = state.get();
if (select.multiple) {
val.includes(value)
? state.set(val.filter((v: number | string) => v !== value))
: state.set(val.concat([value]));
} else {
val !== value && state.set(value);
}
});
}
},
style: {
cursor: "pointer",
display: "flex",
alignItems: "center",
fontSize: (listener) => themeSize(listener, "inherit"),
height: (listener) => themeSpacing(6 + themeDensity(listener) * 2),
paddingInline: (listener) => themeSpacing(themeDensity(listener) * 3),
border: "none",
outline: "none",
color: (listener) => themeColor(listener, "shift-9", color),
backgroundColor: (listener) => themeColor(listener, "inherit", color),
"&:hover:not([disabled]):not([aria-selected=true])": {
backgroundColor: (listener) => themeColor(listener, "shift-2", color),
},
"&[aria-selected=true]": {
backgroundColor: (listener) =>
themeColor(listener, "shift-6", accentColor),
color: (listener) => themeColor(listener, "shift-11"),
},
"&:focus-visible": {
outline: (listener) =>
`${themeSpacing(0.5)} solid ${themeColor(listener, "shift-6", accentColor)}`,
outlineOffset: `-${themeSpacing(0.5)}`,
},
},
};
return partial;
}
export { selectItem };
</div>
<div class="block" data-tab="1">
import {
type DomphyElement,
type PartialElement,
toState,
type ValueOrState,
} from "@domphy/core";
import {
type ThemeColor,
themeColor,
themeDensity,
themeSize,
themeSpacing,
} from "@domphy/theme";
/**
* Container for a list of `selectItem`s that owns the selection state. It exposes a `select`
* context (`{ value, multiple }`) consumed by child items, and injects hidden `<input>`(s)
* carrying the selected value(s) under `name` for form submission.
*
* @hostTag div
* @param props.multiple - Whether multiple selection is allowed; also sets the default empty
* value (`[]` vs `null`). Defaults to `false`.
* @param props.value - Bound selection value(s). Accepts a value or reactive state of an array of
* `number | string | null`, or a single `number | string | null`. Defaults to `[]` when
* `multiple`, otherwise `null`.
* @param props.color - Theme color tone for the background. Defaults to `"neutral"`.
* @param props.name - Name attribute for the hidden inputs (form field name).
* @example { div: [{ div: "A", $: [selectItem({ value: "a" })] }], $: [selectList({ name: "pick" })] }
*/
function selectList(
props: {
multiple?: boolean;
value?: ValueOrState<
Array<number | string | null> | number | string | null
>;
color?: ThemeColor;
name?: string;
} = {},
): PartialElement {
const { color = "neutral", multiple = false } = props;
const state = toState(props.value ?? (multiple ? [] : null));
const inputs: DomphyElement<"div"> = {
div: (listener) => {
const val = state.get(listener);
const vals = Array.isArray(val) ? val : [val];
return vals.map((v) => ({
input: null,
name: props.name,
value: v || "",
}));
},
hidden: true,
};
const partial: PartialElement = {
dataTone: "shift-17",
_context: {
select: {
value: state,
multiple,
},
},
_onInit: (node) => {
if (node.tagName !== "div") {
console.warn(`"selectList" patch must use a div tag`);
}
node.children.insert(inputs);
},
style: {
display: "flex",
flexDirection: "column",
paddingBlock: (listener) => themeSpacing(themeDensity(listener) * 2),
paddingInline: (listener) => themeSpacing(themeDensity(listener) * 2),
fontSize: (listener) => themeSize(listener, "inherit"),
backgroundColor: (listener) => themeColor(listener, "inherit", color),
},
};
return partial;
}
export { selectList };
</div>
</div>