All components
Date Input
Segmented date field with keyboard-editable month, day and year in locale order.
Start date
1234567891011121314151617181920212223242526import { DateInput } from "#components/ui/ui-sdk/date-input.tsx";import { indigoPalette } from "@stylexblocks/ui/themes/palettes.ts";
export function Example() { return ( // UI SDK's date input: ISO string values, segments are real inputs in // locale order; Control submits the value under `name` <DateInput.Root locale="en-US" defaultValue="2026-03-18" variant="outline" size="md" palette={indigoPalette} name="start" > <DateInput.Label>Start date</DateInput.Label> <DateInput.Control> <DateInput.SegmentGroup /> </DateInput.Control> </DateInput.Root> );}
// controlled: <DateInput.Root value={date} onValueChange={(value) => setDate(value)}>// range: <DateInput.Root min="2026-01-01" max="2026-12-31"> (out of range sets data-invalid)// date and time: <DateInput.Root granularity="minute">Variants
Start date
outlineStart date
subtleStart date
flushedSizes
Start date
xsStart date
smStart date
mdStart date
lgStart date
xlControlled
onValueChange fires with an ISO date once every segment is filled, and with null when one is cleared. Tab or ArrowLeft/Right moves between segments, ArrowUp/Down steps the focused one, typed digits fill it and jump ahead, Backspace clears.
Due date
Value: incompleteLocale order
Segment order and separators follow the locale: German shows day, month, year separated by dots.
Startdatum
Install
1npx stylexblocks add date-inputSource
components/ui/ui-sdk/date-input.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450import * as stylex from "@stylexjs/stylex";import { createContext, useContext, useId, useMemo, useState } from "react";import { mergeProps, renderElement, type PartProps } from "@ui-sdk/react/primitive";import { createDateInputController, DateInput as UiSdkDateInput, DateInputSegment, type DateInputController, type DateInputOptions, type DateInputSegmentData, type DateInputState,} from "@ui-sdk/react/date-input";import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";import { defineSlotRecipe, resolveSlotStyles } from "#lib/recipe-props.ts";import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
const dateInputRoot = stylex.create({ base: { display: 'flex', flexDirection: 'column', gap: spacing._1_5, width: '100%', },})
const dateInputLabel = stylex.create({ base: { fontSize: fontSizes.sm, lineHeight: lineHeights.sm, fontWeight: fontWeights.medium, alignSelf: 'flex-start', userSelect: 'none', opacity: { default: null, ':is([data-disabled])': 0.5, }, },})
/** The input-shaped box the segments sit in; mirrors the `input` recipe. */const dateInputControl = stylex.create({ base: { display: 'flex', alignItems: 'center', width: '100%', minWidth: 0, position: 'relative', height: 'var(--input-height)', cursor: { default: 'text', ':is([data-disabled])': 'not-allowed', }, color: semanticColors.fg, opacity: { default: null, ':is([data-disabled])': 0.5, }, '--focus-color': colorPalette.focusRing, '--error-color': semanticColors.borderError, borderColor: { default: null, ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)', }, },})
const dateInputControlSizes = stylex.create({ xs: { borderRadius: semanticRadii.control, paddingInline: spacing._2, '--input-height': sizes.controlXs, fontSize: fontSizes.xs, lineHeight: lineHeights.xs, }, sm: { borderRadius: semanticRadii.control, paddingInline: spacing._2_5, '--input-height': sizes.controlSm, fontSize: fontSizes.sm, lineHeight: lineHeights.sm, }, md: { borderRadius: semanticRadii.control, paddingInline: spacing._3, '--input-height': sizes.controlMd, fontSize: fontSizes.sm, lineHeight: lineHeights.sm, }, lg: { borderRadius: semanticRadii.control, paddingInline: spacing._4_5, '--input-height': sizes.controlLg, fontSize: fontSizes.md, lineHeight: lineHeights.md, }, xl: { borderRadius: semanticRadii.control, paddingInline: spacing._6, '--input-height': sizes.controlXl, fontSize: fontSizes.md, lineHeight: lineHeights.md, },})
const dateInputControlVariants = stylex.create({ outline: { backgroundColor: semanticColors.bg, borderWidth: '1px', borderStyle: 'solid', borderColor: { default: semanticColors.border, ':hover': semanticColors.borderEmphasized, ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)', }, outlineWidth: { default: null, ':focus-within': 'var(--focus-ring-width, 1px)', }, outlineOffset: { default: null, ':focus-within': '0px', }, outlineStyle: { default: null, ':focus-within': 'solid', }, outlineColor: { default: null, ':focus-within': 'var(--focus-color)', }, }, subtle: { backgroundColor: semanticColors.bgMuted, borderWidth: '1px', borderStyle: 'solid', borderColor: { default: 'transparent', ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)', }, outlineWidth: { default: null, ':focus-within': 'var(--focus-ring-width, 1px)', }, outlineOffset: { default: null, ':focus-within': '0px', }, outlineStyle: { default: null, ':focus-within': 'solid', }, outlineColor: { default: null, ':focus-within': 'var(--focus-color)', }, }, flushed: { backgroundColor: 'transparent', borderRadius: 0, paddingInline: 0, borderBottomWidth: '1px', borderBottomStyle: 'solid', borderBottomColor: { default: semanticColors.border, ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)', ':focus-within': 'var(--focus-color)', }, boxShadow: { default: null, ':focus-within': '0px 1px 0px 0px var(--focus-color)', }, },})
const dateInputSegmentGroup = stylex.create({ base: { display: 'inline-flex', alignItems: 'center', whiteSpace: 'nowrap', fontVariantNumeric: 'tabular-nums', },})
const dateInputSegment = stylex.create({ base: { display: 'inline-block', paddingInline: '1px', borderRadius: semanticRadii.l1, outline: 'none', textAlign: 'end', caretColor: 'transparent', userSelect: 'none', fontVariantNumeric: 'tabular-nums', color: { default: semanticColors.fg, ':is([data-placeholder-shown]):not(:focus)': semanticColors.fgMuted, ':is([data-type="literal"])': semanticColors.fgMuted, ':focus': colorPalette.contrast, }, backgroundColor: { default: null, ':focus': colorPalette.solid, }, },})
export type DateInputSize = keyof typeof dateInputControlSizes
export type DateInputVariant = keyof typeof dateInputControlVariants
const dateInputRecipe = defineSlotRecipe({ slots: { root: { styles: dateInputRoot }, label: { styles: dateInputLabel }, control: { styles: dateInputControl, sizes: dateInputControlSizes, variants: dateInputControlVariants, }, segmentGroup: { styles: dateInputSegmentGroup }, segment: { styles: dateInputSegment }, }, defaultVariants: { size: "md", variant: "outline" },});
// UI SDK's editable segments are real <input>s (number pad on phones,// typing before hydration), so they shed the UA input chrome and size to// their digits; an empty segment shows its placeholder through// `::placeholder` and marks itself `data-empty` (Ark: data-placeholder-shown)const segmentFix = stylex.create({ base: { appearance: "none", borderWidth: 0, borderStyle: "none", margin: 0, paddingBlock: 0, paddingInline: "1px", fontFamily: "inherit", fontSize: "inherit", fontWeight: "inherit", lineHeight: "inherit", textAlign: "end", backgroundColor: { default: "transparent", ":focus": colorPalette.solid, }, color: { default: semanticColors.fg, ":is([data-empty]):not(:focus)": semanticColors.fgMuted, ':is([data-type="literal"])': semanticColors.fgMuted, ":focus": colorPalette.contrast, }, width: { default: "calc(2ch + 2px)", '[data-type="year"]': "calc(4ch + 2px)", '[data-type="literal"]': "auto", }, boxSizing: "border-box", "::placeholder": { color: "inherit", opacity: 1 }, },});
const { withProvider, withContext } = createSlotRecipeContext(dateInputRecipe);
export type DateInputRootOwnProps = Omit<DateInputOptions, "controller"> & { controller?: DateInputController;};
interface RootContextValue { options: DateInputOptions; controller: DateInputController; labelId: string;}
const RootContext = createContext<RootContextValue | null>(null);const StateContext = createContext<DateInputState | null>(null);
function useRoot() { const ctx = useContext(RootContext); if (!ctx) throw new Error("DateInput parts must be rendered inside <DateInput.Root>"); return ctx;}
const OPTION_KEYS = [ "value", "defaultValue", "onValueChange", "granularity", "hourCycle", "min", "max", "unavailable", "locale", "disabled", "readOnly", "required", "name", "form", "debug",] as const;
function RootNode({ render, controller, ...props}: Omit<PartProps<"div">, "defaultValue"> & DateInputRootOwnProps) { const options: Record<string, unknown> = {}; const rest: Record<string, unknown> = {}; for (const [key, value] of Object.entries(props)) { if ((OPTION_KEYS as readonly string[]).includes(key)) options[key] = value; else rest[key] = value; } const [ownController] = useState(() => createDateInputController()); const labelId = useId(); const ctx: RootContextValue = { options: options as DateInputOptions, controller: controller ?? ownController, labelId, }; const element = renderElement( "div", mergeProps( { "data-disabled": options.disabled ? "" : undefined, "data-readonly": options.readOnly ? "" : undefined, }, rest, ), render as never, {}, ); return <RootContext.Provider value={ctx}>{element}</RootContext.Provider>;}
function LabelNode({ render, ...rest }: PartProps<"span">) { const { labelId, controller, options } = useRoot(); return renderElement( "span", mergeProps( { id: labelId, "data-disabled": options.disabled ? "" : undefined, // like a native label: a press focuses the first segment onClick: () => { if (!options.disabled) controller.focus(); }, }, rest, ), render as never, {}, );}
/** * Control — UI SDK's DateInput root: the input-shaped group that holds the * segments (a press on the box focuses the nearest one) and the hidden * input that submits the ISO value under `name`. */function ControlNode({ children, ...rest}: Omit<React.ComponentProps<typeof UiSdkDateInput>, keyof DateInputOptions>) { const { options, controller, labelId } = useRoot(); return ( <UiSdkDateInput aria-labelledby={labelId} {...rest} {...options} controller={controller}> {(state: DateInputState) => ( <StateContext.Provider value={state}> {(children as React.ReactNode) ?? <SegmentGroup />} </StateContext.Provider> )} </UiSdkDateInput> );}
const Segment = withContext( DateInputSegment, (v) => [...resolveSlotStyles(dateInputRecipe.slots.segment, v), segmentFix.base], "DateInputSegment",);
/** Renders the locale-ordered segments when given no children. */function SegmentGroupNode({ render, children, ...rest }: PartProps<"div">) { const state = useContext(StateContext); return renderElement( "div", mergeProps( { style: { unicodeBidi: "isolate" }, children: children ?? state?.segments.map((segment) => <Segment key={segment.key} segment={segment} />), }, rest, ), render as never, {}, );}
const SegmentGroup = withContext(SegmentGroupNode, "segmentGroup", "DateInputSegmentGroup");
/** Render-prop access to the segments and ISO value, like Ark's `DateInput.Context`. */function DateInputContextConsumer({ children,}: { children: (api: { segments: DateInputSegmentData[]; value: string | null }) => React.ReactNode;}) { const state = useContext(StateContext); const api = useMemo( () => ({ segments: state?.segments ?? [], value: state?.value ?? null }), [state], ); return <>{children(api)}</>;}
/** * DateInput — UI SDK's date-input primitive: a segmented date field (month * / day / year inputs in `locale` order; arrows step and wrap, typed digits * auto-advance, a separator or a pasted date fills ahead, Backspace clears) * wearing the date-input slot recipe. Values are ISO strings like * `"2026-03-18"`, reported once every segment holds something and as `null` * when one is emptied; `min`, `max` and `unavailable` mark the field * invalid, and `granularity` adds time segments. `<DateInput.Root variant * size>` resolves the recipe once; every part inherits it through context. * Control is the input-shaped box and renders SegmentGroup by default. * There is no HiddenInput part: Control submits the value under `name`. */export const DateInput = { Root: withProvider(RootNode, "root", "DateInputRoot"), Label: withContext(LabelNode, "label", "DateInputLabel"), Control: withContext(ControlNode, "control", "DateInputControl"), SegmentGroup, Segment, Context: DateInputContextConsumer,};
export { createDateInputController };
export type DateInputRootProps = React.ComponentProps<typeof DateInput.Root>;export type DateInputControlProps = React.ComponentProps<typeof DateInput.Control>;export type DateInputSegmentProps = React.ComponentProps<typeof DateInput.Segment>;