Forms

Date Picker

Typed date segments plus a calendar popover — fully keyboard accessible and locale aware.

react-aria-componentslucide-react@internationalized/date

Preview

Follow-up date
Close date
Expected signing date.

Installation

Components are copied into your project — no runtime package.

  1. Install the following dependencies:

    terminal
    npm install react-aria-components lucide-react @internationalized/date
  2. Create a date-picker.tsx file and paste the following code into it.

    src/components/ui/date-picker.tsx
    "use client";
    
    import { CalendarDays } from "lucide-react";
    import {
      Button as AriaButton,
      DateInput,
      DatePicker as AriaDatePicker,
      type DatePickerProps as AriaDatePickerProps,
      DateSegment,
      type DateSegmentProps,
      type DateValue,
      Dialog,
      Group,
      Popover,
    } from "react-aria-components";
    import { cx } from "@/utils/cx";
    import { DateCalendar } from "./date-calendar";
    import { Description, ErrorMessage, Label, fieldBase } from "./field";
    
    export const segment =
      "rounded-[4px] px-0.5 tabular text-fg outline-none data-placeholder:text-fg-muted data-focused:bg-accent data-focused:text-white data-[type=literal]:px-0 data-[type=literal]:text-fg-muted data-disabled:text-fg-disabled";
    
    /**
     * Date/time segment. Node and browsers ship different ICU data: Node formats "9:00 AM"
     * with a narrow no-break space (U+202F), Chrome with a plain space, which breaks hydration.
     * Normalising literal separators keeps server and client output identical.
     */
    export function Segment({ segment: seg }: { segment: DateSegmentProps["segment"] }) {
      return (
        <DateSegment segment={seg} className={segment}>
          {seg.type === "literal" ? seg.text.replace(/[\u202f\u00a0]/g, " ") : undefined}
        </DateSegment>
      );
    }
    
    export interface DatePickerProps<T extends DateValue> extends Omit<AriaDatePickerProps<T>, "className" | "children"> {
      label?: string;
      description?: string;
      errorMessage?: string;
      className?: string;
    }
    
    /** Typed date segments plus a calendar popover. Fully keyboard accessible. */
    export function DatePicker<T extends DateValue>({ label, description, errorMessage, className, ...props }: DatePickerProps<T>) {
      return (
        <AriaDatePicker {...props} isInvalid={props.isInvalid ?? Boolean(errorMessage)} className={cx("group flex flex-col gap-2", className)}>
          {label && <Label>{label}</Label>}
          <Group className={cx(fieldBase, "flex h-10 items-center gap-2 pl-3 pr-1 group-data-open:border-accent group-data-open:shadow-focus")}>
            <DateInput className="flex flex-1 text-base">{(seg) => <Segment segment={seg} />}</DateInput>
            <AriaButton className="inline-flex size-8 items-center justify-center rounded-sm text-fg-muted outline-none data-hovered:bg-muted data-hovered:text-fg data-focus-visible:shadow-focus">
              <CalendarDays className="size-4" aria-hidden />
            </AriaButton>
          </Group>
          {description && !errorMessage && <Description>{description}</Description>}
          <ErrorMessage>{errorMessage}</ErrorMessage>
          <Popover offset={8} placement="bottom start" className="rounded-md bg-surface p-3 shadow-popover outline-none">
            <Dialog className="outline-none">
              <DateCalendar />
            </Dialog>
          </Popover>
        </AriaDatePicker>
      );
    }
  3. Create a date-calendar.tsx file and paste the following code into it.

    src/components/ui/date-calendar.tsx
    "use client";
    
    import { ChevronLeft, ChevronRight } from "lucide-react";
    import {
      Button as AriaButton,
      Calendar as AriaCalendar,
      CalendarCell,
      CalendarGrid,
      CalendarGridBody,
      CalendarGridHeader,
      CalendarHeaderCell,
      type CalendarProps,
      type DateValue,
      Heading,
      RangeCalendar as AriaRangeCalendar,
      type RangeCalendarProps,
    } from "react-aria-components";
    import { cx } from "@/utils/cx";
    
    const nav = "inline-flex size-8 cursor-default items-center justify-center rounded-sm text-fg-secondary outline-none data-hovered:bg-muted data-hovered:text-fg data-focus-visible:shadow-focus data-disabled:text-fg-disabled [&_svg]:size-4";
    
    function Header() {
      return (
        <header className="mb-3 flex items-center justify-between">
          <AriaButton slot="previous" className={nav}>
            <ChevronLeft />
          </AriaButton>
          <Heading className="text-base font-medium text-fg" />
          <AriaButton slot="next" className={nav}>
            <ChevronRight />
          </AriaButton>
        </header>
      );
    }
    
    function Grid({ range }: { range?: boolean }) {
      return (
        <CalendarGrid className="w-full border-separate border-spacing-y-0.5" weekdayStyle="short">
          <CalendarGridHeader>{(day) => <CalendarHeaderCell className="pb-1.5 text-xs font-medium uppercase text-fg-muted">{day.slice(0, 2)}</CalendarHeaderCell>}</CalendarGridHeader>
          <CalendarGridBody>
            {(date) => (
              <CalendarCell
                date={date}
                className={cx(
                  "relative flex size-9 cursor-default items-center justify-center rounded-sm text-base tabular text-fg outline-none",
                  "data-hovered:bg-muted data-outside-month:text-fg-disabled data-unavailable:text-fg-disabled data-unavailable:line-through data-disabled:text-fg-disabled",
                  "data-focus-visible:shadow-focus",
                  "[&[data-today]:not([data-selected])]:after:absolute [&[data-today]:not([data-selected])]:after:bottom-1 [&[data-today]:not([data-selected])]:after:size-1 [&[data-today]:not([data-selected])]:after:rounded-full [&[data-today]:not([data-selected])]:after:bg-accent",
                  range
                    ? "data-selected:rounded-none data-selected:bg-accent-soft data-selected:text-accent-text data-selection-start:rounded-l-sm data-selection-start:bg-accent data-selection-start:text-white data-selection-end:rounded-r-sm data-selection-end:bg-accent data-selection-end:text-white"
                    : "data-selected:bg-linear-to-b data-selected:from-accent-light data-selected:to-accent data-selected:font-medium data-selected:text-white data-selected:shadow-button-primary",
                )}
              />
            )}
          </CalendarGridBody>
        </CalendarGrid>
      );
    }
    
    /** Month grid for picking one date. Today has an accent dot. */
    export function DateCalendar<T extends DateValue>({ className, ...props }: Omit<CalendarProps<T>, "className"> & { className?: string }) {
      return (
        <AriaCalendar {...props} className={cx("w-fit", className)}>
          <Header />
          <Grid />
        </AriaCalendar>
      );
    }
    
    /** Month grid for picking a start and end date. */
    export function DateRangeCalendar<T extends DateValue>({ className, ...props }: Omit<RangeCalendarProps<T>, "className"> & { className?: string }) {
      return (
        <AriaRangeCalendar {...props} className={cx("w-fit", className)}>
          <Header />
          <Grid range />
        </AriaRangeCalendar>
      );
    }
  4. Create a field.tsx file and paste the following code into it.

    src/components/ui/field.tsx
    "use client";
    
    import type { ReactNode } from "react";
    import { FieldError, Label as AriaLabel, Text } from "react-aria-components";
    import { cx } from "@/utils/cx";
    
    /** Shared field chrome: 40px tall, radius 12, 1px border-strong, accent border + 3px ring on focus. */
    export const fieldBase =
      "w-full rounded-md border border-line-strong bg-surface text-base text-fg outline-none transition-[border,box-shadow] " +
      "placeholder:text-fg-muted data-hovered:border-fg-disabled " +
      "data-focused:border-accent data-focused:shadow-focus data-focus-within:border-accent data-focus-within:shadow-focus " +
      "data-invalid:border-error data-invalid:data-focused:shadow-focus-error " +
      "data-disabled:border-line data-disabled:bg-subtle data-disabled:text-fg-disabled data-disabled:placeholder:text-fg-disabled";
    
    export function Label({ className, children }: { className?: string; children: ReactNode }) {
      return <AriaLabel className={cx("text-sm font-medium text-fg", className)}>{children}</AriaLabel>;
    }
    
    export function Description({ children }: { children: ReactNode }) {
      return (
        <Text slot="description" className="text-sm text-fg-muted">
          {children}
        </Text>
      );
    }
    
    export function ErrorMessage({ children }: { children?: ReactNode }) {
      return <FieldError className="text-sm text-error-text">{children}</FieldError>;
    }
  5. Add the theme tokens and the cx helper from Installation if you haven’t yet, then update the import paths to match your project setup.