Components
Date Picker
A date field with a calendar behind a button: type the date, or open the calendar and pick a day. It can pick a range too.
Introduction
Date Picker is a date field and a calendar in one. People who know the date type it, part by part. People who need to see the days, to find a Friday or to stay from the 10th to the 14th, open the calendar under the field and pick. Both fill the same value.
- Built on react-aria's DatePicker: the segments are the ones of
DateField, the panel is a popover with aCalendarin it, and focus goes into the calendar when it opens and back to the button when it closes. - Pick one date, or a range with
selectionMode="range", which has two sets of segments and a calendar that joins the days into a band. - The same limits as the calendar:
minValue,maxValue, a list of dates, days of the week, or a function of your own. Days that are not allowed cannot be typed or picked. - The calendar opens on the picked month, or on today, and can show more than one month, other first days of the week, the days around the month, and a Today button.
- The value is plain text such as
"2026-03-15", and it works in a form.
For a range with ready-made ranges such as Last 7 days, see DateRangePicker, which is this component in range mode. For a date the user does not need to see in a calendar, such as a date of birth, DateField alone is enough. For a calendar that is always on the page, use Calendar. It is a Client Component, so a function such as isDateUnavailable must be defined in a client file.
Installation
centerUI has no CLI or registry yet: components are copied into your project. Date Picker also needs @internationalized/date, which react-aria uses for dates.
# bun
bun add react-aria-components @internationalized/date tailwind-variants tailwind-merge clsx
# pnpm
pnpm add react-aria-components @internationalized/date tailwind-variants tailwind-merge clsx
# npm
npm install react-aria-components @internationalized/date tailwind-variants tailwind-merge clsxManual installation
Copy these files into your project and keep the paths, or adjust the imports. It builds on Calendar, Button and the popover styles, and on the icons.
1. Add Calendar
Its component and its styles, as described on its page. Date Picker draws the inside of the calendar with them.
src/components/ui/calendar/calendar.tsxShow code
"use client";
import {
type CalendarDate,
type DateValue,
getLocalTimeZone,
parseDate,
today,
} from "@internationalized/date";
import { useContext, useMemo, useSyncExternalStore } from "react";
import {
Calendar as AriaCalendar,
RangeCalendar as AriaRangeCalendar,
CalendarCell,
CalendarGrid,
CalendarGridBody,
CalendarGridHeader,
CalendarHeaderCell,
CalendarStateContext,
Heading,
RangeCalendarStateContext,
} from "react-aria-components";
import { ChevronLeftIcon, ChevronRightIcon } from "@/components/icons";
import { Button } from "@/components/ui/button/button";
import { cn } from "@/lib/cn";
import { calendarStyles } from "@/types/calendar-styles";
import {
type DateRangePresetInput,
resolveDateRangePreset,
} from "@/types/date-range-presets";
/** A date as text, `"2026-03-15"`. */
export type CalendarDateText = string;
/** A range of dates: from the first to the last, as text. */
export interface CalendarRange {
start: CalendarDateText;
end: CalendarDateText;
}
interface BaseProps {
/** Names the calendar for screen readers, such as "Departure date". */
"aria-label"?: string;
/** The earliest date that can be picked, `"2026-03-01"`. Earlier days are dimmed and cannot be reached. */
minValue?: CalendarDateText;
/** The latest date that can be picked. */
maxValue?: CalendarDateText;
/** Dates that cannot be picked, such as days that are booked. They are struck through, but can still be moved onto. */
unavailableDates?: CalendarDateText[];
/** Days of the week that cannot be picked, from 0 (Sunday) to 6 (Saturday). `[0, 6]` is the weekend. */
disabledWeekdays?: number[];
/** Decides for each date if it cannot be picked, for rules the two props above cannot say. Called with `"2026-03-15"`. */
isDateUnavailable?: (date: CalendarDateText) => boolean;
/** The month to show first, as a date of that month, when nothing is picked. Defaults to today. */
defaultFocusedValue?: CalendarDateText;
/** The first day of the week. Defaults to the one of the user's language. */
firstDayOfWeek?: "sun" | "mon" | "tue" | "wed" | "thu" | "fri" | "sat";
/** How many months are shown side by side, from 1 to 3. Defaults to 1. */
visibleMonths?: 1 | 2 | 3;
/** What the arrows move by: every month shown (`visible`), or one month at a time (`single`). Defaults to `visible`. */
pageBehavior?: "visible" | "single";
/** The days of the neighbouring months that fill the first and last week. Defaults to `false`. */
showOutsideDays?: boolean;
/** A "Today" button under the days, which moves to the month of today. Defaults to `false`. */
showTodayButton?: boolean;
/** The text of that button. Defaults to "Today". */
todayLabel?: string;
/** Nothing can be picked or moved. */
isDisabled?: boolean;
/** Days can be looked at and moved between, but not picked. */
isReadOnly?: boolean;
/** Focus a day when it is first shown. */
autoFocus?: boolean;
/** How big the days are. Defaults to `md`. */
size?: "sm" | "md" | "lg";
/** A card with a border and padding, or nothing around the days, to sit in a popover or a panel. Defaults to `card`. */
variant?: "card" | "plain";
className?: string;
}
interface SingleProps {
/** One day (the default), several days, or a range from one day to another. */
selectionMode?: "single";
/** The picked date as text, `"2026-03-15"`, or `null` for none. For a controlled calendar. */
value?: CalendarDateText | null;
/** The date to start with, for an uncontrolled calendar. */
defaultValue?: CalendarDateText | null;
onChange?: (value: CalendarDateText | null) => void;
}
interface MultipleProps {
selectionMode: "multiple";
/** The picked dates as text, in any order. For a controlled calendar. */
value?: CalendarDateText[];
defaultValue?: CalendarDateText[];
onChange?: (value: CalendarDateText[]) => void;
}
interface RangeProps {
selectionMode: "range";
/** Ranges offered in a list beside the days, to pick in one press: the names of ready-made ones, or ranges of your own. Ready-made ranges are worked out from today. */
presets?: DateRangePresetInput[];
/** A line under the days that says the range and how many days it has. Defaults to `false`. */
showSummary?: boolean;
/** A "Clear" button under the days. Defaults to `false`. */
showClearButton?: boolean;
/** The text of that button. Defaults to "Clear". */
clearLabel?: string;
/** Let the range run across days that are unavailable, which stay out of it. Defaults to `false`. */
allowsNonContiguousRanges?: boolean;
/** The picked range, `{ start: "2026-03-10", end: "2026-03-14" }`, or `null` for none. For a controlled calendar. */
value?: CalendarRange | null;
defaultValue?: CalendarRange | null;
onChange?: (value: CalendarRange | null) => void;
}
export type CalendarProps = BaseProps &
(SingleProps | MultipleProps | RangeProps);
function toDate(text: string | null | undefined): CalendarDate | null {
if (!text) return null;
try {
return parseDate(text);
} catch {
return null;
}
}
const toDates = (list: string[] | undefined) =>
(list ?? []).map(toDate).filter((d): d is CalendarDate => d !== null);
function toRange(range: CalendarRange | null | undefined) {
const start = toDate(range?.start);
const end = toDate(range?.end);
return start && end ? { start, end } : null;
}
// Jumps to the month of today. The calendar it is in provides its state.
function TodayButton({ children }: { children: string }) {
const single = useContext(CalendarStateContext);
const range = useContext(RangeCalendarStateContext);
const state = single ?? range;
return (
<Button
// Not one of the arrows: a button of the calendar's own must say so.
slot={null}
variant="ghost"
size="sm"
onPress={() => state?.setFocusedDate(today(getLocalTimeZone()))}
>
{children}
</Button>
);
}
const summaryFormat = new Intl.DateTimeFormat(undefined, {
dateStyle: "medium",
});
// What the range is and how long: "Mar 10, 2026 to Mar 14, 2026 · 5 days".
function RangeSummary({ className }: { className: string }) {
const state = useContext(RangeCalendarStateContext);
const value = state?.value;
if (!value?.start || !value.end) {
return <span className={className}>Pick the first and the last day</span>;
}
const days = value.end.compare(value.start) + 1;
const zone = getLocalTimeZone();
return (
<span className={className}>
{summaryFormat.format(value.start.toDate(zone))} to{" "}
{summaryFormat.format(value.end.toDate(zone))}
<span className="text-muted-fg">
{" "}
· {days} {days === 1 ? "day" : "days"}
</span>
</span>
);
}
// Forgets the picked range.
function ClearButton({ children }: { children: string }) {
const state = useContext(RangeCalendarStateContext);
return (
<Button
// Not one of the arrows: a button of the calendar's own must say so.
slot={null}
variant="ghost"
size="sm"
isDisabled={!state?.value}
onPress={() => state?.setValue(null)}
>
{children}
</Button>
);
}
/**
* A list of ready-made ranges to pick in one press, for a range calendar or a
* range picker. It is given the range now picked, to mark the one that matches,
* and what to do when one is pressed.
*/
export function RangePresetList({
presets,
value,
min,
max,
onPick,
className,
buttonClassName,
}: {
presets: DateRangePresetInput[];
value: { start: DateValue; end: DateValue } | null;
min?: DateValue;
max?: DateValue;
onPick: (range: { start: CalendarDate; end: CalendarDate }) => void;
className: string;
buttonClassName: string;
}) {
const now = today(getLocalTimeZone());
return (
// biome-ignore lint/a11y/useSemanticElements: a fieldset would bring a border and a legend of its own
<div role="group" aria-label="Quick ranges" className={className}>
{presets.map((preset) => {
const { label, start, end } = resolveDateRangePreset(preset, now);
const isOutside =
(min !== undefined && start.compare(min) < 0) ||
(max !== undefined && end.compare(max) > 0);
const isActive =
value !== null &&
value.start.compare(start) === 0 &&
value.end.compare(end) === 0;
return (
<Button
key={label}
// Not one of the arrows, nor the button that opens a picker.
slot={null}
variant={isActive ? "secondary" : "ghost"}
size="sm"
className={buttonClassName}
aria-pressed={isActive}
isDisabled={isOutside}
onPress={() => onPick({ start, end })}
>
{label}
</Button>
);
})}
</div>
);
}
// The presets of a range calendar: they set the calendar it is in.
function CalendarPresets({
presets,
min,
max,
className,
buttonClassName,
}: {
presets: DateRangePresetInput[];
min?: DateValue;
max?: DateValue;
className: string;
buttonClassName: string;
}) {
const state = useContext(RangeCalendarStateContext);
const value = state?.value;
return (
<RangePresetList
presets={presets}
value={
value?.start && value.end
? { start: value.start, end: value.end }
: null
}
min={min}
max={max}
className={className}
buttonClassName={buttonClassName}
onPick={(range) => {
state?.setValue(range);
state?.setFocusedDate(range.start);
}}
/>
);
}
type CalendarStyles = ReturnType<typeof calendarStyles>;
/**
* The inside of a calendar: the header with its arrows, the grids of days, and
* the Today button. `Calendar` and the panel of `DatePicker` both draw it, each
* inside the react-aria calendar it belongs to.
*/
export function CalendarBody({
styles,
visibleMonths,
showTodayButton,
todayLabel,
showSummary = false,
showClearButton = false,
clearLabel = "Clear",
}: {
styles: CalendarStyles;
visibleMonths: number;
showTodayButton: boolean;
todayLabel: string;
showSummary?: boolean;
showClearButton?: boolean;
clearLabel?: string;
}) {
return (
<>
<header className={styles.header()}>
<Button slot="previous" variant="ghost" isIcon size="sm">
<ChevronLeftIcon />
</Button>
<Heading className={styles.heading()} />
<Button slot="next" variant="ghost" isIcon size="sm">
<ChevronRightIcon />
</Button>
</header>
<div className={styles.months()}>
{Array.from({ length: visibleMonths }, (_, i) => (
<CalendarGrid
// biome-ignore lint/suspicious/noArrayIndexKey: the months are the positions
key={i}
offset={{ months: i }}
className={styles.grid()}
>
<CalendarGridHeader>
{(day) => (
<CalendarHeaderCell className={styles.headerCell()}>
{day}
</CalendarHeaderCell>
)}
</CalendarGridHeader>
<CalendarGridBody>
{(date) => <CalendarCell date={date} className={styles.cell()} />}
</CalendarGridBody>
</CalendarGrid>
))}
</div>
{showTodayButton || showSummary || showClearButton ? (
<div className={styles.footer()}>
{showSummary ? <RangeSummary className={styles.summary()} /> : null}
{showClearButton ? <ClearButton>{clearLabel}</ClearButton> : null}
{showTodayButton ? <TodayButton>{todayLabel}</TodayButton> : null}
</div>
) : null}
</>
);
}
/**
* Turns the ways to say that a date is not allowed into one function for
* react-aria: a list of dates, days of the week, and a rule of your own.
*/
export function createUnavailable(
unavailableDates: string[] | undefined,
disabledWeekdays: number[] | undefined,
isDateUnavailable: ((date: string) => boolean) | undefined,
) {
const blocked = new Set(unavailableDates);
return (date: DateValue) => {
const text = date.toString();
return (
blocked.has(text) ||
Boolean(disabledWeekdays?.includes(date.toDate("UTC").getUTCDay())) ||
Boolean(isDateUnavailable?.(text))
);
};
}
/**
* A calendar to pick a date, several dates, or a range of dates from: the days
* of a month in a grid, with arrows to go to the next or the previous. Built on
* react-aria's Calendar, so everything works with the keyboard (arrow keys move
* by a day or a week, Page Up and Down by a month, Home and End to the start and
* end of the week), screen readers announce each day, and the days and months
* are written in the user's language and calendar.
*
* Dates are plain text, `"2026-03-15"`, which is what a form and a database
* want. A date has no time and no time zone.
*
* @example
* <Calendar aria-label="Date of birth" value={date} onChange={setDate} />
* <Calendar selectionMode="range" aria-label="Stay" onChange={setStay} />
*/
export function Calendar(props: CalendarProps) {
const {
minValue,
maxValue,
unavailableDates,
disabledWeekdays,
isDateUnavailable,
defaultFocusedValue,
firstDayOfWeek,
visibleMonths = 1,
pageBehavior,
showOutsideDays = false,
showTodayButton = false,
todayLabel = "Today",
size = "md",
variant,
className,
...rest
} = props;
const isRange = props.selectionMode === "range";
const styles = calendarStyles({
variant,
size,
mode: isRange ? "range" : "single",
showOutsideDays,
});
// False on the server and while the page hydrates, true after. The days and
// their names are written for the user's language and time zone, which the
// server does not know, so they are drawn in the browser only.
const isClient = useSyncExternalStore(
() => () => {},
() => true,
() => false,
);
const min = useMemo(() => toDate(minValue) ?? undefined, [minValue]);
const max = useMemo(() => toDate(maxValue) ?? undefined, [maxValue]);
const focus = useMemo(
() => toDate(defaultFocusedValue) ?? undefined,
[defaultFocusedValue],
);
const unavailable = createUnavailable(
unavailableDates,
disabledWeekdays,
isDateUnavailable,
);
const shared = {
"aria-label": rest["aria-label"],
minValue: min,
maxValue: max,
isDateUnavailable: unavailable,
defaultFocusedValue: focus,
firstDayOfWeek,
visibleDuration: { months: visibleMonths },
pageBehavior,
isDisabled: rest.isDisabled,
isReadOnly: rest.isReadOnly,
autoFocus: rest.autoFocus,
className: cn(styles.root(), className),
};
const body = (
<CalendarBody
styles={styles}
visibleMonths={visibleMonths}
showTodayButton={showTodayButton}
todayLabel={todayLabel}
/>
);
if (!isClient) {
return (
<div
aria-hidden="true"
className={cn(styles.root(), styles.placeholder(), className)}
/>
);
}
if (props.selectionMode === "range") {
return (
<AriaRangeCalendar
{...shared}
{...(props.value !== undefined
? { value: toRange(props.value) }
: { defaultValue: toRange(props.defaultValue) })}
onChange={(next) =>
props.onChange?.(
next
? { start: next.start.toString(), end: next.end.toString() }
: null,
)
}
allowsNonContiguousRanges={props.allowsNonContiguousRanges}
>
<div className={styles.panel()}>
{props.presets?.length ? (
<CalendarPresets
presets={props.presets}
min={min}
max={max}
className={styles.presets()}
buttonClassName={styles.preset()}
/>
) : null}
<div className={styles.main()}>
<CalendarBody
styles={styles}
visibleMonths={visibleMonths}
showTodayButton={showTodayButton}
todayLabel={todayLabel}
showSummary={props.showSummary}
showClearButton={props.showClearButton}
clearLabel={props.clearLabel}
/>
</div>
</div>
</AriaRangeCalendar>
);
}
if (props.selectionMode === "multiple") {
return (
<AriaCalendar
{...shared}
selectionMode="multiple"
{...(props.value !== undefined
? { value: toDates(props.value) }
: { defaultValue: toDates(props.defaultValue) })}
onChange={(next) => props.onChange?.(next.map((d) => d.toString()))}
>
{body}
</AriaCalendar>
);
}
return (
<AriaCalendar
{...shared}
{...(props.value !== undefined
? { value: toDate(props.value) }
: { defaultValue: toDate(props.defaultValue) })}
onChange={(next) => props.onChange?.(next ? next.toString() : null)}
>
{body}
</AriaCalendar>
);
}2. Add the styles
src/types/date-picker-styles.tsShow code
import { tv } from "tailwind-variants";
// The frame holds the segments (month, day, year) and, at its end, the button
// that opens the calendar. The focus ring wraps all of it, and the segment that
// has focus is highlighted. The panel with the calendar is a Popover's.
export const datePickerStyles = tv({
slots: {
frame: [
"flex w-full items-center gap-2 rounded-md text-fg transition-colors",
"focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2 focus-within:ring-offset-bg",
"group-data-invalid/date-picker:border-danger",
"group-data-disabled/date-picker:opacity-50",
"[&_svg]:shrink-0",
],
input: "flex min-w-0 flex-1 items-center",
segment: [
"rounded-[4px] px-0.5 tabular-nums caret-transparent outline-none",
"data-placeholder:text-muted-fg",
"data-focused:bg-primary data-focused:text-primary-fg data-focused:data-placeholder:text-primary-fg",
"data-[type=literal]:px-0",
"data-disabled:cursor-not-allowed",
],
// The dash between the two dates of a range.
dash: "shrink-0 text-muted-fg",
trigger: "shrink-0",
dialog: "p-3 outline-none",
// The ready-made ranges beside the calendar (a range picker's presets): in a
// column on the left of it, or in a row above it on a narrow screen.
panel: "flex flex-col gap-3 sm:flex-row",
presets:
"flex shrink-0 flex-row flex-wrap gap-1 border-b pb-3 sm:w-36 sm:flex-col sm:flex-nowrap sm:border-r sm:border-b-0 sm:pr-3 sm:pb-0",
preset: "justify-start",
},
variants: {
variant: {
outline: { frame: "border bg-transparent" },
secondary: { frame: "bg-secondary text-secondary-fg" },
ghost: { frame: "bg-transparent focus-within:bg-muted hover:bg-muted" },
},
size: {
sm: { frame: "h-8 pr-1 pl-2.5 text-sm" },
md: { frame: "h-9 pr-1.5 pl-3 text-sm" },
lg: { frame: "h-11 pr-2 pl-3.5 text-base" },
},
},
defaultVariants: { variant: "outline", size: "md" },
});3. Add the Date Picker file
src/components/ui/date-picker/date-picker.tsxShow code
"use client";
import {
type CalendarDate,
type DateValue,
parseDate,
} from "@internationalized/date";
import {
type ReactNode,
useContext,
useMemo,
useSyncExternalStore,
} from "react";
import {
Calendar as AriaCalendar,
DatePicker as AriaDatePicker,
type DatePickerProps as AriaDatePickerProps,
DateRangePicker as AriaDateRangePicker,
RangeCalendar as AriaRangeCalendar,
DateInput,
DateRangePickerStateContext,
DateSegment,
Dialog,
FieldError,
Group,
Label,
Popover,
Text,
type ValidationResult,
} from "react-aria-components";
import { CalendarIcon } from "@/components/icons";
import { Button } from "@/components/ui/button/button";
import {
CalendarBody,
type CalendarDateText,
type CalendarRange,
createUnavailable,
RangePresetList,
} from "@/components/ui/calendar/calendar";
import { cn } from "@/lib/cn";
import { calendarStyles } from "@/types/calendar-styles";
import { datePickerStyles } from "@/types/date-picker-styles";
import type { DateRangePresetInput } from "@/types/date-range-presets";
import { popoverStyles } from "@/types/popover-styles";
export {
type DateRangePreset,
type DateRangePresetInput,
type DateRangePresetKey,
defaultDateRangePresets,
} from "@/types/date-range-presets";
type Placement = "bottom start" | "bottom end" | "top start" | "top end";
interface BaseProps
extends Pick<
AriaDatePickerProps<DateValue>,
| "isDisabled"
| "isReadOnly"
| "isInvalid"
| "isRequired"
| "autoFocus"
| "name"
| "form"
| "aria-label"
| "aria-labelledby"
| "isOpen"
| "defaultOpen"
| "onOpenChange"
| "shouldCloseOnSelect"
| "onFocus"
| "onBlur"
> {
label?: ReactNode;
/** Help text under the field. */
description?: ReactNode;
/** Shown when the field is invalid. A function receives the validation result. */
errorMessage?: string | ((validation: ValidationResult) => string);
/** The earliest date that can be picked, such as `"2026-01-01"`. */
minValue?: CalendarDateText;
/** The latest date that can be picked. */
maxValue?: CalendarDateText;
/** Dates that cannot be picked, such as days that are booked. */
unavailableDates?: CalendarDateText[];
/** Days of the week that cannot be picked, from 0 (Sunday) to 6 (Saturday). */
disabledWeekdays?: number[];
/** Decides for each date if it cannot be picked. Called with `"2026-03-15"`. */
isDateUnavailable?: (date: CalendarDateText) => boolean;
/** The date an empty field, and its calendar, start from. Defaults to today. */
placeholderValue?: CalendarDateText;
/** Show the month and the day with two digits, such as 03/05 instead of 3/5. */
showLeadingZeros?: boolean;
/** Replaces the calendar icon of the button. */
icon?: ReactNode;
/** Which side of the field the calendar opens on, and which edge it lines up with. Defaults to `bottom start`. */
placement?: Placement;
/** How many months the calendar shows side by side, from 1 to 3. Defaults to 1. */
visibleMonths?: 1 | 2 | 3;
/** The first day of the week. Defaults to the one of the user's language. */
firstDayOfWeek?: "sun" | "mon" | "tue" | "wed" | "thu" | "fri" | "sat";
/** What the arrows of the calendar move by. Defaults to `visible`. */
pageBehavior?: "visible" | "single";
/** Days of the neighbouring months in the calendar. Defaults to `false`. */
showOutsideDays?: boolean;
/** A "Today" button in the calendar. Defaults to `false`. */
showTodayButton?: boolean;
/** The text of that button. Defaults to "Today". */
todayLabel?: string;
variant?: "outline" | "secondary" | "ghost";
size?: "sm" | "md" | "lg";
className?: string;
}
interface SingleProps {
/** One date (the default), or a range from one date to another. */
selectionMode?: "single";
/** The date as text, `"2026-03-15"`, or `null` while it is not complete. For a controlled picker. */
value?: CalendarDateText | null;
/** The date to start with, for an uncontrolled picker. */
defaultValue?: CalendarDateText | null;
onChange?: (value: CalendarDateText | null) => void;
}
interface RangeProps {
selectionMode: "range";
/** Ranges offered in a list beside the calendar, to pick in one press: the names of ready-made ones, or ranges of your own. Ready-made ranges are worked out from today. */
presets?: DateRangePresetInput[];
/** The name of the form field that holds the start, such as `"from"`. A range is sent as two fields, this and `endName`. */
startName?: string;
/** The name of the form field that holds the end, such as `"to"`. */
endName?: string;
/** The range, `{ start: "2026-03-10", end: "2026-03-14" }`, or `null` while it is not complete. */
value?: CalendarRange | null;
defaultValue?: CalendarRange | null;
onChange?: (value: CalendarRange | null) => void;
}
export type DatePickerProps = BaseProps & (SingleProps | RangeProps);
/** The props of a range picker. */
export type DatePickerRangeProps = Extract<
DatePickerProps,
{ selectionMode: "range" }
>;
function toDate(text: string | null | undefined) {
if (!text) return null;
try {
return parseDate(text);
} catch {
return null;
}
}
function toRange(range: CalendarRange | null | undefined) {
const start = toDate(range?.start);
const end = toDate(range?.end);
return start && end ? { start, end } : null;
}
// The ready-made ranges beside the calendar. Pressing one fills the picker and
// closes the calendar. It reads and sets the picker through its state.
function Presets({
presets,
min,
max,
className,
buttonClassName,
}: {
presets: DateRangePresetInput[];
min?: CalendarDate;
max?: CalendarDate;
className: string;
buttonClassName: string;
}) {
const state = useContext(DateRangePickerStateContext);
const picked = state?.value;
return (
<RangePresetList
presets={presets}
value={
picked?.start && picked.end
? { start: picked.start, end: picked.end }
: null
}
min={min}
max={max}
className={className}
buttonClassName={buttonClassName}
onPick={(range) => {
state?.setValue(range);
state?.setOpen(false);
}}
/>
);
}
/**
* A field for a date with a calendar behind a button: the user can type the date
* in the field, part by part, or open the calendar and pick a day. Set
* `selectionMode="range"` to pick two dates, from one to another. It is a
* `DateField` and a `Calendar` in one, built on react-aria's DatePicker, so the
* keyboard, the screen reader announcements, the language of the days and
* months, and form submission (`name`) all come from there.
*
* The value is plain text, `"2026-03-15"`, the same as `DateField` and
* `Calendar`, and `{ start, end }` of two of them for a range. It is `null`
* until every part is filled. A date has no time and no time zone.
*
* @example
* <DatePicker label="Check-in" defaultValue="2026-03-15" onChange={setCheckIn} />
* <DatePicker selectionMode="range" label="Stay" onChange={setStay} />
*/
export function DatePicker(props: DatePickerProps) {
const {
label,
description,
errorMessage,
minValue,
maxValue,
unavailableDates,
disabledWeekdays,
isDateUnavailable,
placeholderValue,
showLeadingZeros,
icon,
placement = "bottom start",
visibleMonths = 1,
firstDayOfWeek,
pageBehavior,
showOutsideDays = false,
showTodayButton = false,
todayLabel = "Today",
variant,
size = "md",
className,
...rest
} = props;
const isRange = props.selectionMode === "range";
const styles = datePickerStyles({ variant, size });
const calendarClasses = calendarStyles({
variant: "plain",
size: "md",
mode: isRange ? "range" : "single",
showOutsideDays,
});
// False on the server and while the page hydrates, true after.
const isClient = useSyncExternalStore(
() => () => {},
() => true,
() => false,
);
const min = useMemo(() => toDate(minValue) ?? undefined, [minValue]);
const max = useMemo(() => toDate(maxValue) ?? undefined, [maxValue]);
const placeholder = useMemo(
() => toDate(placeholderValue) ?? undefined,
[placeholderValue],
);
const unavailable = createUnavailable(
unavailableDates,
disabledWeekdays,
isDateUnavailable,
);
// What `AriaDatePicker` and `AriaDateRangePicker` share.
const shared = {
isDisabled: rest.isDisabled,
isReadOnly: rest.isReadOnly,
isInvalid: rest.isInvalid,
isRequired: rest.isRequired,
autoFocus: rest.autoFocus,
name: rest.name,
form: rest.form,
"aria-label": rest["aria-label"],
"aria-labelledby": rest["aria-labelledby"],
isOpen: rest.isOpen,
defaultOpen: rest.defaultOpen,
onOpenChange: rest.onOpenChange,
shouldCloseOnSelect: rest.shouldCloseOnSelect,
onFocus: rest.onFocus,
onBlur: rest.onBlur,
minValue: min,
maxValue: max,
placeholderValue: placeholder,
isDateUnavailable: unavailable,
granularity: "day" as const,
shouldForceLeadingZeros: showLeadingZeros,
firstDayOfWeek,
// Show a problem while the user types, not only on submit.
validationBehavior: "aria" as const,
className: cn("group/date-picker flex w-full flex-col gap-1.5", className),
};
const labelEl = label ? (
<Label className="text-sm font-medium group-data-required/date-picker:after:ml-0.5 group-data-required/date-picker:after:text-danger group-data-required/date-picker:after:content-['*']">
{label}
</Label>
) : null;
const trigger = (
<Button
variant="ghost"
isIcon
size={size === "lg" ? "sm" : "xs"}
className={styles.trigger()}
>
{icon ?? <CalendarIcon />}
</Button>
);
const below = (
<>
{description ? (
<Text slot="description" className="text-sm text-muted-fg">
{description}
</Text>
) : null}
<FieldError className="text-sm text-danger">{errorMessage}</FieldError>
</>
);
const panel = (
<Popover
placement={placement}
className={popoverStyles({ size: "auto" }).popover()}
>
<Dialog className={styles.dialog()}>
<div className={styles.panel()}>
{props.selectionMode === "range" && props.presets?.length ? (
<Presets
presets={props.presets}
min={min}
max={max}
className={styles.presets()}
buttonClassName={styles.preset()}
/>
) : null}
{isRange ? (
<AriaRangeCalendar
visibleDuration={{ months: visibleMonths }}
pageBehavior={pageBehavior}
className={calendarClasses.root()}
>
<CalendarBody
styles={calendarClasses}
visibleMonths={visibleMonths}
showTodayButton={showTodayButton}
todayLabel={todayLabel}
/>
</AriaRangeCalendar>
) : (
<AriaCalendar
visibleDuration={{ months: visibleMonths }}
pageBehavior={pageBehavior}
className={calendarClasses.root()}
>
<CalendarBody
styles={calendarClasses}
visibleMonths={visibleMonths}
showTodayButton={showTodayButton}
todayLabel={todayLabel}
/>
</AriaCalendar>
)}
</div>
</Dialog>
</Popover>
);
const segments = (slot?: "start" | "end") =>
isClient ? (
<DateInput slot={slot} className={styles.input()}>
{(segment) => (
<DateSegment segment={segment} className={styles.segment()} />
)}
</DateInput>
) : (
// The segments are in the order and the language of the user, which the
// server does not know, so they are drawn in the browser only and the
// frame keeps its place until then.
<div aria-hidden="true" className={styles.input()} />
);
if (props.selectionMode === "range") {
return (
<AriaDateRangePicker
{...shared}
startName={props.startName}
endName={props.endName}
{...(props.value !== undefined
? { value: toRange(props.value) }
: { defaultValue: toRange(props.defaultValue) })}
onChange={(next) =>
props.onChange?.(
next
? { start: next.start.toString(), end: next.end.toString() }
: null,
)
}
>
{labelEl}
<Group className={styles.frame()}>
{segments("start")}
<span aria-hidden="true" className={styles.dash()}>
–
</span>
{segments("end")}
{trigger}
</Group>
{below}
{panel}
</AriaDateRangePicker>
);
}
return (
<AriaDatePicker
{...shared}
{...(props.value !== undefined
? { value: toDate(props.value) }
: { defaultValue: toDate(props.defaultValue) })}
onChange={(next) => props.onChange?.(next ? next.toString() : null)}
>
{labelEl}
<Group className={styles.frame()}>
{segments()}
{trigger}
</Group>
{below}
{panel}
</AriaDatePicker>
);
}4. Use it
import { DatePicker } from "@/components/ui/date-picker/date-picker";Anatomy
A framed row of segments with a button at its end, and a calendar in a popover.
<DatePicker
label="Check-in"
description="You can change it later."
defaultValue="2026-03-15"
minValue="2026-03-01"
/>| Part | Rendered as | Notes |
|---|---|---|
| Label | label | Names the field. Clicking it focuses the first segment. |
| Segments | spinbutton | Month, day and year, in the order of the user's language, to type the date. A range has two sets. |
| Button | button | A calendar icon that opens the calendar. Replace the icon with the icon prop. |
| Calendar | popover, dialog | A Calendar in a popover under the field. Picking a day closes it and fills the field. |
| Description / error | text under the field | Help text, replaced by the error message when the field is invalid. |
Without a label, give the picker an aria-label so a screen reader can name it.
Value
The date is text with the year first, whatever order the field shows.
// A date, as text: year, month, day
"2026-03-15"
// A range, with selectionMode="range"
{ start: "2026-03-10", end: "2026-03-14" }
null // not complete yetThe value is null until every part is filled, so an unfinished date is never reported as a wrong one.
Keyboard
Everything can be done without a mouse.
| Key | What it does |
|---|---|
| Type digits | Fill the segment that has focus. Focus moves to the next one when it is full. |
| Arrow Up, Arrow Down | Add or subtract one from the segment. |
| Arrow Left, Arrow Right | Move to the previous or the next segment. |
| Enter, Space, Arrow Down | On the button, open the calendar. Alt and Arrow Down open it from a segment. |
| Escape | Close the calendar without changing the date, and go back to the button. |
| Arrows, Page Up, Page Down | In the calendar, move by a day, a week, a month. Enter picks the day. |
Examples
Usage
Type a date, or press the calendar button and pick a day.
Label
Description
Default value
Start with a date, written as text. The calendar opens on its month.
Range
selectionMode="range" picks two dates. Type them in the two sets of segments, or press the first and the last day in the calendar. The value is { start, end }.
March 10, 2026 to March 14, 2026
Controlled
Keep the date in your own state with value and onChange. The value is null while the date is not complete.
Check-in on March 15, 2026
In a form
With a name, the date is submitted as one field, in the form 2026-03-15.
Minimum and maximum
Days outside the range are dimmed in the calendar and marked invalid when typed.
Your own rule
isDateUnavailable is called with each date as text. Here nothing is delivered on the 13th.
More than one month
visibleMonths shows up to three months in the calendar, which suits a range that crosses a month.
Placement
Which side of the field the calendar opens on, and which edge it lines up with. It flips when there is no room.
The calendar
The calendar takes the same options as Calendar: firstDayOfWeek, showOutsideDays, showTodayButton, pageBehavior.
Leading zeros
showLeadingZeros writes the month and the day with two digits.
Icon
A calendar by default.
Open from outside
Own the open state with isOpen and onOpenChange, to open the calendar from another button or to know when it is open.
Closed
Sizes
Variants
Disabled and read only
A disabled picker cannot be used at all. A read only one shows its date and can be focused, but not changed.
Validation
For a rule of your own, set isInvalid with an errorMessage. The field gets a red border and the message shows under it.
Component API
Date Picker takes the props below. The type of value follows selectionMode: text for a date, and { start, end } for a range.
| Prop | Type | Default |
|---|---|---|
| selectionMode | "single" | "range" | "single" |
| label | ReactNode | - |
| description | ReactNode | - |
| errorMessage | string | (validation) => string | - |
| value | string | null | { start, end } | null | - |
| defaultValue | same as value | - |
| onChange | (value) => void | - |
| presets | (DateRangePresetKey | { label, value })[] (range only) | - |
| startName, endName | string (range only) | - |
| minValue | string | - |
| maxValue | string | - |
| unavailableDates | string[] | - |
| disabledWeekdays | number[] | - |
| isDateUnavailable | (date: string) => boolean | - |
| placeholderValue | string | today |
| showLeadingZeros | boolean | the language's |
| icon | ReactNode | a calendar |
| placement | "bottom start" | "bottom end" | "top start" | "top end" | "bottom start" |
| visibleMonths | 1 | 2 | 3 | 1 |
| firstDayOfWeek | "sun" | "mon" | ... | "sat" | the user's language |
| pageBehavior | "visible" | "single" | "visible" |
| showOutsideDays | boolean | false |
| showTodayButton | boolean | false |
| todayLabel | string | "Today" |
| isOpen | boolean | - |
| defaultOpen | boolean | false |
| onOpenChange | (isOpen: boolean) => void | - |
| shouldCloseOnSelect | boolean | true |
| name | string | - |
| variant | "outline" | "secondary" | "ghost" | "outline" |
| size | "sm" | "md" | "lg" | "md" |
| autoFocus | boolean | false |
| isDisabled | boolean | false |
| isReadOnly | boolean | false |
| isInvalid | boolean | false |
| isRequired | boolean | false |
| aria-label | string | - (when there is no label) |