A number field you drag by its label, step with the arrow keys, or type maths into, with a Mixed state.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { NumberScrubber } from "./ui/uicontext/components/canvas/NumberScrubber.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ grid: { display: "grid", gridTemplateColumns: "repeat(2, minmax(0, 120px))", gap: 8 } })const Field = Schema.Literals(["w", "h", "rotation", "opacity"])const Message = UiSchema.TaggedEnum({ RequestedValue: { field: Field, value: Schema.Number } })// Drag a label sideways, use the arrow keys (Shift for 10), or type maths such as `+8` or `*2`.export const SizeFields = Html.component({ name: "SizeFields", model: Schema.Struct({ w: Schema.Number, h: Schema.Number, rotation: Schema.Number, opacity: Schema.Number }), message: Message,}).make({ init: () => ({ w: 342, h: 88, rotation: 0, opacity: 100 }), update: { RequestedValue: ($, { field, value }) => $.set({ ...$.model, [field]: value }) }, view: (h, m) => h.div.attrs(sx(styles.grid)).children( NumberScrubber(h, { key: "w", props: { label: "W", name: "Width", value: m.w, min: 1 }, onEmit: { RequestedValue: ({ value }) => Message.RequestedValue({ field: "w", value }), RequestedShift: ({ by }) => Message.RequestedValue({ field: "w", value: m.w + by }) } }), NumberScrubber(h, { key: "h", props: { label: "H", name: "Height", value: m.h, min: 1 }, onEmit: { RequestedValue: ({ value }) => Message.RequestedValue({ field: "h", value }), RequestedShift: ({ by }) => Message.RequestedValue({ field: "h", value: m.h + by }) } }), NumberScrubber(h, { key: "rotation", props: { label: "°", name: "Rotation", value: m.rotation, unit: "°", precision: 1 }, onEmit: { RequestedValue: ({ value }) => Message.RequestedValue({ field: "rotation", value }), RequestedShift: ({ by }) => Message.RequestedValue({ field: "rotation", value: m.rotation + by }) } }), NumberScrubber(h, { key: "opacity", props: { label: "%", name: "Opacity", value: m.opacity, min: 0, max: 100, unit: "%", precision: 0 }, onEmit: { RequestedValue: ({ value }) => Message.RequestedValue({ field: "opacity", value }), RequestedShift: ({ by }) => Message.RequestedValue({ field: "opacity", value: m.opacity + by }) } }), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/number-scrubberUsage
import { NumberScrubber } from "./ui/uicontext/components/canvas/NumberScrubber.ts"const Message = UiSchema.TaggedEnum({ RequestedWidth: { value: Schema.Number, live: Schema.Boolean } })export const WidthField = (h: Html.H<typeof Message.Type>, width: number) => NumberScrubber(h, { props: { label: "W", name: "Width", value: width, min: 1 }, onEmit: { RequestedValue: ({ value, live }) => Message.RequestedWidth({ value, live }), RequestedShift: ({ by, live }) => Message.RequestedWidth({ value: width + by, live }), }, })Examples
Mixed values
Several layers that differ: relative changes shift each one.
Widths: 320, 342, 360
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import { Schema } from "effect"import { NumberScrubber } from "./ui/uicontext/components/canvas/NumberScrubber.ts"const Message = UiSchema.TaggedEnum({ RequestedSet: { value: Schema.Number }, RequestedShift: { by: Schema.Number } })// Three cards with different widths: the field reads Mixed. `+8` or a scrub widens each by the same// amount; a plain number sets them all.export const MixedWidths = Html.component({ name: "MixedWidths", model: Schema.Struct({ widths: Schema.Array(Schema.Number) }), message: Message,}).make({ init: () => ({ widths: [320, 342, 360] }), update: { RequestedSet: ($, { value }) => $.evolve({ widths: (ws) => ws.map(() => value) }), RequestedShift: ($, { by }) => $.evolve({ widths: (ws) => ws.map((w) => Math.max(1, w + by)) }), }, view: (h, { widths }) => h.div.children( NumberScrubber(h, { props: { label: "W", name: "Width", value: widths.every((w) => w === widths[0]) ? widths[0]! : null, min: 1 }, onEmit: { RequestedValue: ({ value }) => Message.RequestedSet({ value }), RequestedShift: ({ by }) => Message.RequestedShift({ by }) }, }), h.p.children(`Widths: ${widths.join(", ")}`), ),})API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
labelrequired | string | — | The short label people drag: "W", "X", "°" |
namerequired | string | — | The field's accessible name: "Width" |
valuerequired | number | null | — | Null when the selected layers differ: the field reads Mixed |
min | number | — | — |
max | number | — | — |
step | number | 1 | How much one pixel of scrubbing and one arrow press change it; 1 by default |
unit | string | — | Shown after the number: "%", "°" |
precision | number | 2 | Decimals shown; 2 by default |
disabled | boolean | — | — |
Events
| Event | Payload | Description |
|---|---|---|
RequestedValue | { value: number; live: boolean } | Set the value; live while scrubbing, false when it's final |
RequestedShift | { by: number; live: boolean } | Shift each selected value by by (a mixed value, scrubbed or stepped); live as above |