A compact row of exclusive choices with a sliding indicator.
Active issues
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { SegmentedControl } from "./ui/uicontext/components/SegmentedControl.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ toolbar: { display: "flex", flexWrap: "wrap", alignItems: "center", justifyContent: "space-between", gap: space.lg, inlineSize: "min(100%, 34rem)", }, heading: { margin: 0, fontSize: font.base, fontWeight: 600, color: color.fg },})const views = [ { value: "list", label: "List", icon: "☰" }, { value: "board", label: "Board", icon: "▦" }, { value: "timeline", label: "Timeline", icon: "▭" },]const Message = UiSchema.TaggedEnum({ PickedView: { value: Schema.String } })export const ViewSwitcher = Html.component({ name: "ViewSwitcher", model: Schema.Struct({ view: Schema.String }), message: Message,}).make({ init: () => ({ view: "board" }), update: { PickedView: ($, { value }) => $.evolve({ view: value }) }, view: (h, { view }) => h.div.attrs(sx(styles.toolbar)).children( h.h3.attrs(sx(styles.heading)).children("Active issues"), SegmentedControl(h, { label: "View", value: view, options: views, onPick: (value) => Message.PickedView({ value }) }), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/segmented-controlUsage
import { SegmentedControl } from "./ui/uicontext/components/SegmentedControl.ts"const Message = UiSchema.TaggedEnum({ PickedRange: { value: Schema.String } })export const RangeControl = (h: Html.H<typeof Message.Type>, range: string) => SegmentedControl(h, { label: "Range", value: range, options: [ { value: "day", label: "Day" }, { value: "week", label: "Week" }, { value: "month", label: "Month" }, ], onPick: (value) => Message.PickedRange({ value }), })Examples
In a field, full width
With fullWidth the segments share the field's width. The field's label names the group.
Appearance
System follows your device.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { FormField } from "./ui/uicontext/components/FormControls.ts"import { SegmentedControl } from "./ui/uicontext/components/SegmentedControl.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ field: { inlineSize: "min(100%, 22rem)" },})const Message = UiSchema.TaggedEnum({ PickedAppearance: { value: Schema.String } })export const AppearanceField = Html.component({ name: "AppearanceField", model: Schema.Struct({ appearance: Schema.String }), message: Message,}).make({ init: () => ({ appearance: "system" }), update: { PickedAppearance: ($, { value }) => $.evolve({ appearance: value }) }, view: (h, { appearance }) => FormField(h, { id: "appearance", label: "Appearance", description: "System follows your device.", group: true, style: styles.field }, (field) => SegmentedControl(h, { value: appearance, fullWidth: true, options: [ { value: "light", label: "Light" }, { value: "dark", label: "Dark" }, { value: "system", label: "System" }, ], field, onPick: (value) => Message.PickedAppearance({ value }), })),})Small, with a disabled segment
The arrow keys skip a disabled segment.
Runs this week: 1,284
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { SegmentedControl } from "./ui/uicontext/components/SegmentedControl.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ row: { display: "flex", flexWrap: "wrap", alignItems: "center", gap: 12 }, caption: { margin: 0, fontSize: font.sm, color: color.muted },})const ranges = [ { value: "day", label: "Day" }, { value: "week", label: "Week" }, { value: "month", label: "Month" }, // the plan covers a month of history { value: "year", label: "Year", disabled: true },]const Message = UiSchema.TaggedEnum({ PickedRange: { value: Schema.String } })export const UsageRange = Html.component({ name: "UsageRange", model: Schema.Struct({ range: Schema.String }), message: Message,}).make({ init: () => ({ range: "week" }), update: { PickedRange: ($, { value }) => $.evolve({ range: value }) }, view: (h, { range }) => h.div.attrs(sx(styles.row)).children( SegmentedControl(h, { label: "Range", size: "sm", value: range, options: ranges, onPick: (value) => Message.PickedRange({ value }) }), h.p.attrs(sx(styles.caption)).children(`Runs this ${range}: 1,284`), ),})API reference
Options
| Name | Type | Default | Description |
|---|---|---|---|
key | string | — | Names the control among others in the same view; needed for more than one |
valuerequired | string | null | — | The picked segment's value, or null for none. The caller keeps it. |
optionsrequired | Segment[] | — | Two to eight segments |
onPickrequired | (value: string) => Message | — | What a pick asks for: the value to store and pass back as value |
label | string | — | The accessible name of the group, such as "View"; leave it out inside a FormField |
size | "sm" | "md" | md | The control's height; md by default. |
fullWidth | boolean | — | Stretch to the container's width |
disabled | boolean | — | No segment can be picked. |
readOnly | boolean | — | Shows the pick and stays focusable, but no segment can be picked. |
name | string | — | The form field: the picked value submits under name |
field | FieldControlAttrs | — | A FormField's attributes (with group: true): its label names the control |
style | Style | — | StyleX styles applied after the control’s own. |
Accessibility
- A radio group and one Tab stop: the arrow keys move and pick, and screen readers hear "radio, 2 of 3".
- The group is named by
label; the sliding indicator and any icons are hidden from screen readers.