---
title: "Date Input · UI SDK — StyleX Blocks"
description: "Segmented date field with keyboard-editable month, day and year in locale order."
url: "https://stylexblocks.com/components/ui-sdk/date-input/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Date Input

Segmented date field with keyboard-editable month, day and year in locale order.

- Behavior engine: UI SDK
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/date-input.md), [Base UI](https://stylexblocks.com/components/base-ui/date-input.md)
- Category: Form
- Variants: outline, subtle, flushed
- Sizes: xs, sm, md, lg, xl
- HTML page: https://stylexblocks.com/components/ui-sdk/date-input/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style ui-sdk
```

Then add the item as source you own:

```sh
npx stylexblocks add date-input
```

Equivalent: `npx shadcn add @stylexblocks/date-input`. Registry item JSON: https://stylexblocks.com/r/ui-sdk/date-input.json

## Usage

```tsx
import { 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">
```

## Source

`components/ui/ui-sdk/date-input.tsx`

```tsx
import * 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>;
```
