OptionCard
@codeui/solid v0.3.0
Selectable cards with labels, descriptions, and schematic previews.
When to use
Use option cards for a single choice where a preview helps you compare options, such as colour mode, navigation, or snippet themes.
When to choose another component
Use SegmentedControl for short text choices. Use Switch for an independent boolean. Use Select for a long list of names.
Live demo
import { OptionCardGroup } from "@codeui/solid";
// A schematic in each option's own colours: the orb is the mode, the rim
// follows the text colour so it reads on either theme.
function Orb(props: { fill: string; half?: string }) {
return (
<svg width="44" height="44" viewBox="0 0 44 44" aria-hidden="true">
<circle cx="22" cy="22" r="18" fill={props.fill} />
{props.half && <path d="M22 4a18 18 0 0 1 0 36z" fill={props.half} />}
<circle
cx="22"
cy="22"
r="18"
fill="none"
stroke="currentColor"
stroke-opacity="0.18"
/>
</svg>
);
}
// The width of CodeImage's settings pane, which holds three 224px cards.
export default function ColourMode() {
return (
<div style={{ width: "100%", "max-width": "688px" }}>
<OptionCardGroup
label="Colour mode"
defaultValue="dark"
items={[
{
value: "dark",
label: "Dark",
description: "A darker workspace",
preview: <Orb fill="#2c2f4a" />,
badge: "Default",
},
{
value: "light",
label: "Light",
description: "A brighter workspace",
preview: <Orb fill="#f5f5f7" />,
},
{
value: "system",
label: "System",
description: "Follow your device",
preview: <Orb fill="#f5f5f7" half="#2c2f4a" />,
},
]}
/>
</div>
);
}OptionCardGroup renders the group label, description, error message, and one OptionCard per entry. OptionCard renders one item and needs a radio-group root. It does not create a group itself.
Anatomy
Tap a part to outline it in the demo.
- 01Root
optioncardclasses.root - 02Label
optioncard-labelclasses.label - 03Description
optioncard-descriptionclasses.description - 04ErrorMessage
optioncard-error-messageclasses.errorMessage - 05Item
optioncard-itemclasses.item - 06ItemInput
optioncard-item-inputclasses.itemInput - 07ItemControl
optioncard-item-controlclasses.itemControl - 08ItemIndicator
optioncard-item-indicatorclasses.itemIndicator - 09ItemLabel
optioncard-item-labelclasses.itemLabel - 10ItemDescription
optioncard-item-descriptionclasses.itemDescription - 11Preview
optioncard-previewclasses.preview - 12Badge
optioncard-badgeclasses.badge
| Export | Renders | classes key | data-cui | Role |
|---|---|---|---|---|
Root | div | root | optioncard | radiogroup |
Label | span | label | optioncard-label | Group name |
Description | div | description | optioncard-description | Group description |
ErrorMessage | div | errorMessage | optioncard-error-message | Validation message |
Item | div | item | optioncard-item | group |
ItemInput | input | itemInput | optioncard-item-input | Native radio |
ItemControl | div | itemControl | optioncard-item-control | Visual control |
ItemIndicator | div | itemIndicator | optioncard-item-indicator | Selection mark |
ItemLabel | label | itemLabel | optioncard-item-label | Input label |
ItemDescription | div; span in ready item | itemDescription | optioncard-item-description | Input description |
Preview | div | preview | optioncard-preview | Decorative |
Badge | span | badge | optioncard-badge | Status text |
The ready item renders ItemInput and ItemLabel. It does not render ItemControl or ItemIndicator. Preview, badge, and description render only when supplied. The label is the card title.
Variants
Card and tile
import { OptionCardGroup } from "@codeui/solid";
export default function Themes() {
return <OptionCardGroup label="Theme cards" variant="tile" defaultValue="window" items={[
{ value: "window", label: "Window", badge: "Default", preview: <svg width="96" height="68" viewBox="0 0 96 68" aria-hidden="true"><rect x="8" y="8" width="80" height="52" rx="8" fill="var(--cui-surface-canvas)"/><path d="M20 24h56M20 36h32M20 48h44" stroke="var(--cui-text-muted)" stroke-width="4"/></svg> },
{ value: "orb", label: "Orb", preview: <svg width="96" height="68" viewBox="0 0 96 68" aria-hidden="true"><circle cx="48" cy="34" r="24" fill="var(--cui-text-muted)"/><circle cx="42" cy="28" r="12" fill="var(--cui-surface-canvas)"/></svg> },
{ value: "list", label: "Aa list", preview: <span style={{ "font-size": "var(--cui-font-size-lg)" }} aria-hidden="true">Aa</span> },
]} />;
}card is the default. tile reduces label padding to 8px. Previews are noninteractive, decorative wells above the title; draw a schematic in the option's own colours, not a miniature of real content. Keep the choice understandable from its label alone. The Default badge sits on the title row, 8px after the title, and the row is 20px tall with or without it. It is supplied content, independent of selection.
Grid and column
import { OptionCardGroup } from "@codeui/solid";
export default function Vertical() {
return <OptionCardGroup label="Export appearance" orientation="vertical" defaultValue="window" items={[
{ value: "window", label: "Window", description: "Include window controls" },
{ value: "plain", label: "Plain", description: "Show the code alone" },
]} />;
}The horizontal root fills its container and lays the cards out three across, each at least 128px wide: three 224px columns with an 8px gap in a 688px settings pane, as in CodeImage. At 768px and narrower the cards stack in one column and each card puts its preview beside its text. Vertical orientation always uses one column. Orientation also controls arrow-key navigation.
States
Selected and controlled
import { OptionCardGroup, Stack, Text, type OptionCardEntry } from "@codeui/solid";
import { createSignal } from "solid-js";
type Mode = "dark" | "light" | "system";
export default function Controlled() {
const [mode, setMode] = createSignal<Mode>("system");
const items: readonly OptionCardEntry<Mode>[] = [{ value: "dark", label: "Dark" }, { value: "light", label: "Light" }, { value: "system", label: "System" }];
return <Stack><OptionCardGroup label="Colour mode" items={items} value={mode()} onChange={setMode} /><Text tone="muted">Saved mode: {mode()}</Text></Stack>;
}Use defaultValue for an initial selection or value with onChange for a controlled selection. OptionCardEntry<T> keeps values and the callback in the same string union. createOptionCardItems wraps an items accessor as { items } without changing entries.
A card has the field fill, a 12px radius, 12px padding and no border. The selected card rises one fill step and gets a 2px --cui-accent-focus outline inset 2px from its edge, and its title goes from weight 500 to 600. Hovering a card with a fine pointer raises the same fill step. Pressing a card scales it to 0.98 over 120ms with the ease-out curve; reduced motion keeps the fill change and drops the scale. Keyboard focus draws the 2px focus ring outside the card instead.
Disabled
import { OptionCardGroup, Stack } from "@codeui/solid";
const items = [{ value: "dark", label: "Dark" }, { value: "light", label: "Light" }, { value: "system", label: "System", disabled: true }];
export default function Disabled() {
return <Stack gap="6">
<OptionCardGroup label="Unavailable option" items={items} defaultValue="dark" />
<OptionCardGroup label="Locked appearance" items={items} defaultValue="dark" disabled />
</Stack>;
}An entry can disable one card. Group disabled disables all entries. readOnly prevents value changes without the disabled appearance.
Read-only
import { OptionCardGroup } from "@codeui/solid";
export default function ReadOnly() {
return <OptionCardGroup label="Managed colour mode" description="Your workspace administrator manages this preference." defaultValue="system" readOnly items={[
{ value: "dark", label: "Dark" }, { value: "light", label: "Light" }, { value: "system", label: "System" },
]} />;
}The managed value remains focusable, but user input cannot change it.
Required and invalid
import { OptionCardGroup } from "@codeui/solid";
export default function Invalid() {
return <OptionCardGroup label="Colour mode" required validationState="invalid" errorMessage="Choose a colour mode." items={[
{ value: "dark", label: "Dark" }, { value: "light", label: "Light" },
]} />;
}required enables native form validation. validationState="invalid" exposes the invalid state and displays errorMessage. The invalid border replaces the selected accent border.
Long labels
import { OptionCardGroup } from "@codeui/solid";
export default function LongLabels() {
return <OptionCardGroup label="Appearance" defaultValue="device" items={[
{ value: "device", label: "Match the colour mode selected in your device settings", description: "The workspace follows your device when its appearance changes.", badge: "Default" },
{ value: "fixed", label: "Keep the workspace dark throughout the day", description: "Exported snippets keep their own theme." },
]} />;
}Labels and descriptions wrap, including unbroken text. Cards stretch across each grid row. Keep previews at a consistent height so titles align. Do not truncate labels.
Ready API
OptionCardGroup
Renders a complete group from readonly OptionCardEntry<T>[]. Each entry has a required value and label, plus optional description, preview, badge, and disabled.
Prop
Type
Prop
Type
OptionCard
Renders one radio item inside a styled Root. Its variant defaults to the ready group's appearance when rendered by OptionCardGroup.
Prop
Type
Styled parts API
Import the parts from @codeui/solid/option-card/parts. These exports are direct named parts, with no namespace alias. The Kobalte parts retain their polymorphic as prop. Each styled radio part accepts class and variant; direct parts set their own variant rather than inheriting it from Root.
Badge
Renders the kit Badge for supplied status text. Its variant is the badge appearance, not the card layout.
Prop
Type
Prop
Type
Description
Wraps Kobalte RadioGroup.Description and connects the group description.
Prop
Type
ErrorMessage
Wraps Kobalte RadioGroup.ErrorMessage. It renders for invalid state or when force-mounted.
Prop
Type
Prop
Type
Item
Wraps Kobalte RadioGroup.Item. Supply a unique value and an optional disabled state.
Prop
Type
Prop
Type
ItemControl
Wraps Kobalte RadioGroup.ItemControl for a custom visual control.
Prop
Type
ItemDescription
Wraps Kobalte RadioGroup.ItemDescription and connects explanatory text to the input.
Prop
Type
ItemIndicator
Wraps Kobalte RadioGroup.ItemIndicator for content that appears when selected.
Prop
Type
Prop
Type
ItemInput
Wraps Kobalte RadioGroup.ItemInput. Keep this native radio in every custom item.
Prop
Type
ItemLabel
Wraps Kobalte RadioGroup.ItemLabel. Clicking this label selects its input.
Prop
Type
Label
Wraps Kobalte RadioGroup.Label and names the group.
Prop
Type
Preview
Renders a div with aria-hidden="true" and pointer-events disabled. Do not put focusable controls inside it.
Prop
Type
Root
Wraps Kobalte RadioGroup.Root and supplies the responsive grid.
Prop
Type
Prop
Type
useRadioGroupContext
Re-exports Kobalte's hook. Call it inside Root to read the radio group's context. It renders no DOM and accepts no props.
The inherited behavior follows the Kobalte RadioGroup contract.
Styles export
optionCardStyles
optionCardStyles is exported from @codeui/solid and @codeui/solid/option-card/parts. Each anatomy key is a function that returns a class string. OptionCardStyles describes those functions. OptionCardPart is the key union, and OptionCardClasses is a partial map of keys to class names.
Call optionCardStyles.itemLabel() and set data-variant="tile" on a custom label to select its tile padding. Kobalte sets data-orientation="vertical" on the root for the single-column layout.
Accessibility
Keyboard
| Key | Action |
|---|---|
| Tab | Enters the group once, at the selected enabled radio or the first enabled radio. The next Tab leaves. |
| Shift+Tab | Moves to the previous tab stop. |
| Right or Left arrow | Moves focus and selects the next or previous enabled option in horizontal orientation. |
| Down or Up arrow | Moves focus and selects the next or previous enabled option in vertical orientation. |
| Space | Selects the focused radio. |
ARIA
The root renders role="radiogroup" with its label and description connected through ARIA IDs. The item container renders role="group". Its visually hidden native input type="radio" exposes selection, disabled, required, and invalid state. Group validation uses aria-invalid, aria-required, and aria-disabled. An invalid error message joins the description IDs. Preview content is hidden from assistive technology.
Focus
Focus stays on the native input and shows a 2px ring around its card, only on :focus-visible. Selection moves focus within the group. Cards open no overlay and have no open or close focus behavior.
SSR notes
The group, inputs, labels, and supplied content render on the server. There is no portal or client-only preview. Keep item values, order, and initial selection identical during hydration. Create controlled state inside the component instance so requests do not share it.
Theming hooks
The card reads --cui-font-family-sans, --cui-font-size-sm, --cui-font-size-xs, --cui-text-default, --cui-text-muted, and --cui-text-hint. Errors read --cui-text-danger and --cui-action-danger-base.
Cards use --cui-surface-input, mixed 6% toward --cui-text-default for the hovered and selected step. The preview well is 68px tall with an 8px radius on --cui-surface-panel. Selection uses --cui-accent-focus; focus uses --cui-focus-width, --cui-focus-color, and --cui-focus-offset. Shape uses --cui-radius-md. Spacing reads --cui-space-0-5, --cui-space-1, --cui-space-2, --cui-space-3, --cui-space-5, --cui-space-8, --cui-space-10, --cui-space-16, --cui-space-24, and --cui-space-32. Text sets --cui-font-line-height-sm and --cui-font-line-height-xs, so the cards never inherit the page's leading. Press motion uses --cui-motion-duration-fast and --cui-motion-ease-out.
The optional badge uses the Badge theming hooks. Token defaults are described in Inkstone colour and space and shape.
Composition examples
Navigation choice
import { OptionCardGroup } from "@codeui/solid";
export default function Navigation() {
return <OptionCardGroup label="Navigation" defaultValue="accordion" items={[
{ value: "accordion", label: "Accordion", description: "One section open at a time", badge: "Default", preview: <svg width="96" height="68" viewBox="0 0 96 68" aria-hidden="true"><rect x="12" y="8" width="72" height="52" rx="8" fill="var(--cui-surface-canvas)"/><path d="M24 20h48M24 34h32M24 48h48" stroke="var(--cui-text-muted)" stroke-width="4"/></svg> },
{ value: "rail", label: "Rail", description: "Keep section shortcuts visible", preview: <svg width="96" height="68" viewBox="0 0 96 68" aria-hidden="true"><rect x="12" y="8" width="72" height="52" rx="8" fill="var(--cui-surface-canvas)"/><path d="M26 20v28M42 20h28M42 34h20M42 48h28" stroke="var(--cui-text-muted)" stroke-width="4"/></svg> },
]} />;
}Custom item layout
import { OptionCard } from "@codeui/solid";
import * as Cards from "@codeui/solid/option-card/parts";
export default function Parts() {
return <Cards.Root defaultValue="window"><Cards.Label>Snippet frame</Cards.Label>
<OptionCard value="window" label="Window" description="Include window controls" badge="Default" />
<Cards.Item value="plain"><Cards.ItemInput /><Cards.ItemLabel>Plain<Cards.ItemDescription as="span">Show the code alone</Cards.ItemDescription></Cards.ItemLabel></Cards.Item>
</Cards.Root>;
}Use OptionCard inside Root for a ready item alongside custom parts. Use OptionCardGroup when all options fit the entry shape. See Build settings for the complete dialog.
Related
- SegmentedControl uses compact text choices.
- Switch represents one boolean.
- Build settings combines cards with settings rows.