Pick a number or a range by dragging, with steps, marks, the keyboard and the value over the thumb.
Price per night
Guests
253 stays match
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { formatCount } from "./ui/uicontext/components/domain/Format.ts"import { Slider } from "./ui/uicontext/components/Slider.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ panel: { display: "grid", gap: space.xxl, inlineSize: "min(100%, 24rem)", padding: space.xl, borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: radius.lg, backgroundColor: color.panel, }, results: { margin: 0, fontSize: font.sm, color: color.muted, fontVariantNumeric: "tabular-nums" },})// nightly prices of the listings a search foundconst prices = Array.from({ length: 480 }, (_, i) => 40 + ((i * 37) % 460))const money = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", maximumFractionDigits: 0 })const Message = UiSchema.TaggedEnum({ MovedPrice: { low: Schema.Number, high: Schema.Number }, MovedGuests: { guests: Schema.Number },})export const StayFilters = Html.component({ name: "StayFilters", model: Schema.Struct({ low: Schema.Number, high: Schema.Number, guests: Schema.Number }), message: Message,}).make({ init: () => ({ low: 80, high: 320, guests: 2 }), update: { MovedPrice: ($, { low, high }) => $.evolve({ low, high }), MovedGuests: ($, { guests }) => $.evolve({ guests }), }, view: (h, { low, high, guests }) => { const found = prices.filter((p) => p >= low && p <= high).length return h.div.attrs(sx(styles.panel)).children( Slider(h, { key: "price", label: "Price per night", value: [low, high], min: 0, max: 500, step: 10, minStepsBetween: 2, format: (n) => (n === 500 ? "$500+" : money.format(n)), onMove: ([lo, hi]) => Message.MovedPrice({ low: lo, high: hi }), }), Slider(h, { key: "guests", label: "Guests", value: guests, min: 1, max: 8, marks: [1, 2, 3, 4, 5, 6, 7, 8].map((n) => ({ value: n, label: n === 8 ? "8+" : String(n) })), format: (n) => formatCount(n, "guest"), onMove: (value) => Message.MovedGuests({ guests: value }), }), h.p.attrs({ role: "status", ...sx(styles.results) }).children(`${formatCount(found, "stay")} match`), ) },})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/sliderUsage
import { Slider } from "./ui/uicontext/components/Slider.ts"const Message = UiSchema.TaggedEnum({ MovedVolume: { volume: Schema.Number } })export const VolumeSlider = (h: Html.H<typeof Message.Type>, volume: number) => Slider(h, { label: "Volume", value: volume, format: (n) => `${n}%`, onMove: (value) => Message.MovedVolume({ volume: value }), })Examples
Marks, and saving on release
Labelled stops under the track. onMove follows the thumb; onCommit saves once the drag or key press is done.
Creativity
Saved: 0.4
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Slider } from "./ui/uicontext/components/Slider.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ stack: { display: "grid", gap: 8, inlineSize: "min(100%, 24rem)" }, saved: { margin: 0, fontSize: font.sm, color: color.muted },})const Message = UiSchema.TaggedEnum({ MovedCreativity: { value: Schema.Number }, CommittedCreativity: { value: Schema.Number } })// an agent's creativity: named stops, and the setting saves once, when you let goexport const AgentCreativity = Html.component({ name: "AgentCreativity", model: Schema.Struct({ value: Schema.Number, saved: Schema.Number }), message: Message,}).make({ init: () => ({ value: 0.4, saved: 0.4 }), update: { MovedCreativity: ($, { value }) => $.evolve({ value }), CommittedCreativity: ($, { value }) => $.evolve({ saved: value }), }, view: (h, { value, saved }) => h.div.attrs(sx(styles.stack)).children( Slider(h, { label: "Creativity", value, min: 0, max: 1, step: 0.1, largeStep: 0.5, marks: [ { value: 0, label: "Precise" }, { value: 0.5, label: "Balanced" }, { value: 1, label: "Creative" }, ], format: (n) => n.toFixed(1), onMove: (next) => Message.MovedCreativity({ value: next }), onCommit: (next) => Message.CommittedCreativity({ value: next }), }), h.p.attrs({ role: "status", ...sx(styles.saved) }).children(`Saved: ${saved.toFixed(1)}`), ),})Long labels, big numbers, disabled
Annual contract value for enterprise accounts in the EMEA region
Seats (set by your plan)
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Slider } from "./ui/uicontext/components/Slider.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ stack: { display: "grid", gap: 28, inlineSize: "min(100%, 24rem)" } })const compact = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", notation: "compact", maximumFractionDigits: 1 })const Message = UiSchema.TaggedEnum({ MovedBudget: { low: Schema.Number, high: Schema.Number } })export const SliderStates = Html.component({ name: "SliderStates", model: Schema.Struct({ low: Schema.Number, high: Schema.Number }), message: Message,}).make({ init: () => ({ low: 250000, high: 4500000 }), update: { MovedBudget: ($, { low, high }) => $.evolve({ low, high }) }, view: (h, { low, high }) => h.div.attrs(sx(styles.stack)).children( // big numbers stay short in the bubble and beside the label Slider(h, { key: "budget", label: "Annual contract value for enterprise accounts in the EMEA region", value: [low, high], min: 0, max: 10000000, step: 50000, format: (n) => compact.format(n), onMove: ([lo, hi]) => Message.MovedBudget({ low: lo, high: hi }), }), Slider(h, { key: "seats", label: "Seats (set by your plan)", value: 25, max: 50, disabled: true, onMove: () => Message.MovedBudget({ low, high }), }), ),})API reference
Options
| Name | Type | Default | Description |
|---|---|---|---|
key | string | — | Names the slider among others in the same view; needed for more than one. |
labelrequired | string | — | What it sets: "Volume", "Price". Shown above the track, and each thumb's name. |
hideLabel | boolean | — | Hide the label but keep it as the name, when the slider's meaning is plain from where it sits. |
valuerequired | number | Range | — | One number, or [low, high] for a range with two thumbs. The caller keeps it. |
min | number | 0 | 0 by default. |
max | number | 100 | 100 by default. |
step | number | 1 | What a key press and a drag move by; 1 by default. |
largeStep | number | — | What Shift with an arrow, Page Up and Page Down move by; ten steps by default. |
minStepsBetween | number | 0 | For a range: the fewest steps between the two thumbs; 0 by default. |
marks | SliderMark[] | — | Points along the track, as ticks, with a label under the ones that have one. |
format | (value: number) => string | — | A value as people read it: (n) => \${n}%\`, (n) => \$${n}\`. A grouped number by default. |
showValue | boolean | true | The value beside the label; true by default. |
bubble | "auto" | "never" | auto | The bubble over the thumb: while held or focused, and on hover (auto), or never; auto by default. |
disabled | boolean | — | — |
name | string | — | The form field the value submits under; a range submits two values under it. |
onMoverequired | (value: number | Range) => Message | — | Each value the person moves to, during a drag and per key. Store it and pass it back as value. |
onCommit | (value: number | Range) => Message | — | The value once the person lets go or presses a key: save it here, not on every move. |
style | Style | — | StyleX styles applied after the slider's own. |