Calendar
An animated React calendar built on react-day-picker v10 and shadcn/ui — a gliding selected-day pill, a today ring, range styles and months that slide in the direction you navigate — plus a date picker and a date range picker with presets, built with Radix Popover and Motion.
- shadcn API
- react-day-picker v10
- Keyboard grid
- date-fns
- prefers-reduced-motion
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
"use client";
import * as React from "react";
import { Calendar } from "@/components/ui/calendar";
// Fixed dates keep the server and client render identical (no hydration mismatch).
const TODAY = new Date(2026, 5, 12);
export default function CalendarBasicDemo() {
const [date, setDate] = React.useState<Date | undefined>(new Date(2026, 5, 16));
return (
<Calendar
mode="single"
selected={date}
onSelect={setDate}
today={TODAY}
defaultMonth={TODAY}
className="rounded-xl border bg-card/40"
/>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/calendar.json1. Install the dependencies.
npm install react-day-picker date-fns motion lucide-react2. Copy the source into
components/ui/calendar.tsx.calendar.tsx "use client"; import * as React from "react"; import { DayPicker, type ChevronProps, type DayButtonProps, type DayPickerProps, type RootProps, type WeekNumberProps, } from "react-day-picker"; import { format } from "date-fns"; import { ChevronDownIcon, ChevronLeftIcon, ChevronRightIcon } from "lucide-react"; import { motion, useReducedMotion, type Transition } from "motion/react"; import { buttonVariants, type ButtonProps } from "@/components/ui/button"; import { cn } from "@/lib/utils"; const pillSpring: Transition = { type: "spring", duration: 0.25, bounce: 0 }; /** Shared with the day buttons, so the selected pill can glide between days in single mode. */ const CalendarContext = React.createContext<{ pillId?: string }>({}); export type CalendarProps = DayPickerProps & { /** Button variant for the previous / next month buttons. */ buttonVariant?: ButtonProps["variant"]; }; /** * DuskUI Calendar — the shadcn/ui Calendar rebuilt on react-day-picker v10. * * Same API as shadcn (every DayPicker prop, plus `buttonVariant`) with: * - A solid mint pill for the selected day. In `mode="single"` it glides * from the old day to the new one (short, no overshoot). * - A neutral background for today, so it stays visible next to a selection. * - Range styles: solid caps on the start and end days joined by a neutral band. * - Month transitions: the weeks shift in slightly from the side you navigated * toward while the caption crossfades (react-day-picker's `animate`, on by default). * - Styled month / year dropdowns for `captionLayout="dropdown"`. * - Under `prefers-reduced-motion` months crossfade without sliding and the * pill jumps. * react-day-picker handles the grid semantics, roving focus and the full * keyboard model (arrows, Home/End, PageUp/PageDown, Shift+PageUp/Down). */ function Calendar({ className, classNames, showOutsideDays = true, captionLayout = "label", buttonVariant = "ghost", animate = true, formatters, components, ...props }: CalendarProps) { const id = React.useId(); const context = React.useMemo( () => ({ pillId: props.mode === "single" ? `${id}-pill` : undefined }), [props.mode, id] ); return ( <CalendarContext.Provider value={context}> <DayPicker showOutsideDays={showOutsideDays} captionLayout={captionLayout} // Under reduced motion the months crossfade without sliding (see the // motion-reduce classes on month_caption and weeks), so the markup is the // same on the server and the client. animate={animate} className={cn( "group/calendar w-fit p-3 [--cell-size:--spacing(9)]", className )} formatters={{ // Short month names from the calendar's own locale, identical on server and client. formatMonthDropdown: (date, dateLib) => (dateLib ? dateLib.format(date, "LLL") : format(date, "LLL")), ...formatters, }} classNames={{ root: "w-fit", months: "relative flex flex-col gap-4 md:flex-row", month: "flex w-full flex-col gap-4", nav: "absolute inset-x-0 top-0 flex w-full items-center justify-between gap-1", button_previous: cn( buttonVariants({ variant: buttonVariant }), "size-(--cell-size) p-0 text-muted-foreground select-none hover:text-foreground aria-disabled:pointer-events-none aria-disabled:opacity-40" ), button_next: cn( buttonVariants({ variant: buttonVariant }), "size-(--cell-size) p-0 text-muted-foreground select-none hover:text-foreground aria-disabled:pointer-events-none aria-disabled:opacity-40" ), month_caption: cn( "flex h-(--cell-size) w-full items-center justify-center px-(--cell-size)", "duration-200 ease-out [--tw-animation-fill-mode:forwards]", "motion-reduce:[--tw-enter-translate-x:0]! motion-reduce:[--tw-exit-translate-x:0]!", "[&.cal-caption-after-enter]:animate-in [&.cal-caption-after-enter]:fade-in-0 [&.cal-caption-after-enter]:slide-in-from-right-3", "[&.cal-caption-before-enter]:animate-in [&.cal-caption-before-enter]:fade-in-0 [&.cal-caption-before-enter]:slide-in-from-left-3", "[&.cal-caption-after-exit]:animate-out [&.cal-caption-after-exit]:fade-out-0 [&.cal-caption-after-exit]:slide-out-to-right-3", "[&.cal-caption-before-exit]:animate-out [&.cal-caption-before-exit]:fade-out-0 [&.cal-caption-before-exit]:slide-out-to-left-3" ), dropdowns: "flex h-(--cell-size) w-full items-center justify-center gap-1.5 text-sm font-medium", dropdown_root: cn( "relative rounded-md border border-input bg-input/30 shadow-xs transition-[border-color,box-shadow] duration-150", "has-focus:border-ring has-focus:ring-[3px] has-focus:ring-ring/50" ), dropdown: "absolute inset-0 cursor-pointer bg-popover opacity-0", caption_label: cn( "font-medium select-none", captionLayout === "label" ? "text-sm" : "flex h-8 items-center gap-1 rounded-md pr-1.5 pl-2.5 text-sm [&>svg]:size-3.5 [&>svg]:text-muted-foreground" ), month_grid: "w-full border-collapse", weekdays: "flex", weekday: "flex-1 rounded-md text-[0.8rem] font-normal text-muted-foreground select-none", weeks: cn( "duration-200 ease-out [--tw-animation-fill-mode:forwards]", "motion-reduce:[--tw-enter-translate-x:0]! motion-reduce:[--tw-exit-translate-x:0]!", "[&.cal-weeks-after-enter]:animate-in [&.cal-weeks-after-enter]:fade-in-0 [&.cal-weeks-after-enter]:slide-in-from-right-2", "[&.cal-weeks-before-enter]:animate-in [&.cal-weeks-before-enter]:fade-in-0 [&.cal-weeks-before-enter]:slide-in-from-left-2", "[&.cal-weeks-after-exit]:animate-out [&.cal-weeks-after-exit]:fade-out-0 [&.cal-weeks-after-exit]:slide-out-to-right-2", "[&.cal-weeks-before-exit]:animate-out [&.cal-weeks-before-exit]:fade-out-0 [&.cal-weeks-before-exit]:slide-out-to-left-2" ), week: "mt-1.5 flex w-full", week_number_header: "w-(--cell-size) select-none", week_number: "text-[0.8rem] text-muted-foreground select-none", day: cn( "group/day relative aspect-square h-full w-full p-0 text-center select-none", "[&:first-child[data-selected=true]_button]:rounded-l-md [&:last-child[data-selected=true]_button]:rounded-r-md" ), range_start: "rounded-l-md bg-accent", range_middle: "rounded-none bg-accent first:rounded-l-md last:rounded-r-md", range_end: "rounded-r-md bg-accent", today: "", outside: "text-muted-foreground/80 aria-selected:text-muted-foreground", disabled: "text-muted-foreground opacity-40", hidden: "invisible", // Single-token marker classes that react-day-picker adds while a month slides. weeks_after_enter: "cal-weeks-after-enter", weeks_before_enter: "cal-weeks-before-enter", weeks_after_exit: "cal-weeks-after-exit", weeks_before_exit: "cal-weeks-before-exit", caption_after_enter: "cal-caption-after-enter", caption_before_enter: "cal-caption-before-enter", caption_after_exit: "cal-caption-after-exit", caption_before_exit: "cal-caption-before-exit", ...classNames, }} components={{ Root: CalendarRoot, Chevron: CalendarChevron, DayButton: CalendarDayButton, WeekNumber: CalendarWeekNumber, ...components, }} {...props} /> </CalendarContext.Provider> ); } function CalendarRoot({ className, rootRef, ...props }: RootProps) { return <div data-slot="calendar" ref={rootRef} className={className} {...props} />; } function CalendarChevron({ className, orientation, disabled, style }: ChevronProps) { const Icon = orientation === "left" ? ChevronLeftIcon : orientation === "right" ? ChevronRightIcon : ChevronDownIcon; return <Icon aria-hidden data-disabled={disabled || undefined} style={style} className={cn("size-4", className)} />; } function CalendarWeekNumber({ children, week, ...props }: WeekNumberProps) { return ( <td data-week={week.weekNumber} {...props}> <div className="flex size-(--cell-size) items-center justify-center text-center">{children}</div> </td> ); } /** A day. Focus follows react-day-picker's roving focus; the selected pill is a separate layer. */ function CalendarDayButton({ className, day, modifiers, children, ...props }: DayButtonProps) { const { pillId } = React.useContext(CalendarContext); const reduceMotion = useReducedMotion() ?? false; const ref = React.useRef<HTMLButtonElement>(null); React.useEffect(() => { if (modifiers.focused) ref.current?.focus(); }, [modifiers.focused]); const selectedSingle = modifiers.selected && !modifiers.range_start && !modifiers.range_end && !modifiers.range_middle; // Only an in-month day may own the shared pill, so a date shown twice (as an // outside day of the next month) never renders two pills with one id. const sharedPill = selectedSingle && pillId && !day.outside ? pillId : undefined; return ( <button ref={ref} data-selected-single={selectedSingle || undefined} data-range-start={modifiers.range_start || undefined} data-range-end={modifiers.range_end || undefined} data-range-middle={modifiers.range_middle || undefined} data-today={(modifiers.today && !modifiers.outside && !modifiers.selected) || undefined} className={cn( "relative isolate flex aspect-square size-auto w-full min-w-(--cell-size) items-center justify-center rounded-md text-sm leading-none font-normal tabular-nums outline-none", "transition-[color,background-color,box-shadow] duration-150 ease-out motion-reduce:transition-none", "hover:bg-accent hover:text-accent-foreground", "focus-visible:z-20 focus-visible:ring-[3px] focus-visible:ring-ring/50", "disabled:pointer-events-none aria-disabled:pointer-events-none", // Today: a neutral background that stays visible next to a selection. "data-today:bg-accent data-today:text-accent-foreground", // Selected single / multiple day: the pill layer paints the background. "data-selected-single:text-primary-foreground data-selected-single:hover:bg-transparent data-selected-single:hover:text-primary-foreground", // Range caps and band. "data-range-start:bg-primary data-range-start:text-primary-foreground data-range-start:hover:bg-primary/90", "data-range-end:bg-primary data-range-end:text-primary-foreground data-range-end:hover:bg-primary/90", "data-range-middle:rounded-none data-range-middle:bg-transparent data-range-middle:text-foreground data-range-middle:hover:bg-white/5", className )} {...props} > {selectedSingle && ( <motion.span aria-hidden layoutId={sharedPill} transition={reduceMotion ? { duration: 0 } : pillSpring} className="absolute inset-0 -z-10 rounded-md bg-primary" /> )} {children} </button> ); } export { Calendar, CalendarDayButton };
Built for react-day-picker v10. Every DayPicker prop works, plus buttonVariant for the month buttons. Month transitions use DayPicker's animate prop, on by default; pass animate={false} to turn them off. To avoid hydration mismatches, pass fixed today and defaultMonth dates when a calendar is rendered on the server.
Usage
"use client"
import * as React from "react"
import { Calendar } from "@/components/ui/calendar"
export function Booking() {
const [date, setDate] = React.useState<Date | undefined>()
return <Calendar mode="single" selected={date} onSelect={setDate} />
}Examples
Single date
Pick a day and the mint pill glides over from the previous one. Today keeps a neutral background so you can always find it. Use the arrows (or PageUp / PageDown) to change month: the weeks slide in from the side you moved toward.
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
"use client";
import * as React from "react";
import { Calendar } from "@/components/ui/calendar";
// Fixed dates keep the server and client render identical (no hydration mismatch).
const TODAY = new Date(2026, 5, 12);
export default function CalendarBasicDemo() {
const [date, setDate] = React.useState<Date | undefined>(new Date(2026, 5, 16));
return (
<Calendar
mode="single"
selected={date}
onSelect={setDate}
today={TODAY}
defaultMonth={TODAY}
className="rounded-xl border bg-card/40"
/>
);
}Range
mode="range" draws solid caps on the first and last day joined by a tinted band. With numberOfMonths={2} the months sit side by side and stack on small screens.
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
"use client";
import * as React from "react";
import type { DateRange } from "react-day-picker";
import { Calendar } from "@/components/ui/calendar";
const TODAY = new Date(2026, 5, 12);
export default function CalendarRangeDemo() {
const [range, setRange] = React.useState<DateRange | undefined>({
from: new Date(2026, 5, 9),
to: new Date(2026, 5, 18),
});
return (
<Calendar
mode="range"
selected={range}
onSelect={setRange}
numberOfMonths={2}
today={TODAY}
defaultMonth={TODAY}
className="rounded-xl border bg-card/40"
/>
);
}Month & year dropdowns
captionLayout="dropdown" swaps the caption for native month and year selects, styled to match. Set startMonth and endMonth to bound them — handy for birthdays.
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
"use client";
import * as React from "react";
import { Calendar } from "@/components/ui/calendar";
const TODAY = new Date(2026, 5, 12);
export default function CalendarDropdownDemo() {
const [date, setDate] = React.useState<Date | undefined>(new Date(1994, 2, 21));
return (
<Calendar
mode="single"
selected={date}
onSelect={setDate}
captionLayout="dropdown"
startMonth={new Date(1940, 0)}
endMonth={new Date(2026, 11)}
today={TODAY}
defaultMonth={new Date(1994, 2)}
className="rounded-xl border bg-card/40"
/>
);
}Disabled days & week numbers
Pass any react-day-picker matcher to disabled — here past days and weekends — and showWeekNumber for a week column. Disabled days are skipped by keyboard navigation.
| Su | Mo | Tu | We | Th | Fr | Sa | |
|---|---|---|---|---|---|---|---|
23 | |||||||
24 | |||||||
25 | |||||||
26 | |||||||
27 |
Weekdays from today onward.
"use client";
import * as React from "react";
import { Calendar } from "@/components/ui/calendar";
const TODAY = new Date(2026, 5, 12);
export default function CalendarDisabledDemo() {
const [date, setDate] = React.useState<Date | undefined>(new Date(2026, 5, 17));
return (
<div className="flex flex-col items-center gap-3">
<Calendar
mode="single"
selected={date}
onSelect={setDate}
today={TODAY}
defaultMonth={TODAY}
// No past days and no weekends.
disabled={[{ before: TODAY }, { dayOfWeek: [0, 6] }]}
showWeekNumber
className="rounded-xl border bg-card/40 max-sm:[--cell-size:--spacing(8)]"
/>
<p className="text-sm text-muted-foreground">Weekdays from today onward.</p>
</div>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Date Picker
A button that opens the Calendar in a popover. The panel scales out of the trigger, focus moves into the grid, and picking a day closes it and rolls the formatted date into the trigger. Pass name to submit the date in a form as yyyy-MM-dd.
We deliver every day, including weekends.
"use client";
import * as React from "react";
import { DatePicker } from "@/components/dusk/calendar/date-picker";
const TODAY = new Date(2026, 5, 12);
export default function DatePickerDemo() {
const [date, setDate] = React.useState<Date | undefined>();
return (
<div className="grid w-full max-w-xs gap-2">
<span id="delivery-label" className="text-sm font-medium">
Delivery date
</span>
{/* Label + current value, so screen readers hear "Delivery date, June 15, 2026". */}
<DatePicker
id="delivery-date"
aria-labelledby="delivery-label delivery-date"
value={date}
onValueChange={setDate}
placeholder="Pick a delivery date"
disabledDays={{ before: TODAY }}
calendarProps={{ today: TODAY, defaultMonth: TODAY }}
name="deliveryDate"
/>
<p className="text-xs text-muted-foreground">We deliver every day, including weekends.</p>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/date-picker.json1. Install the dependencies.
npm install @radix-ui/react-popover react-day-picker date-fns motion lucide-react2. Copy the source into
components/dusk/calendar/date-picker.tsx.date-picker.tsx "use client"; import * as React from "react"; import * as PopoverPrimitive from "@radix-ui/react-popover"; import { format as formatDate } from "date-fns"; import { CalendarIcon } from "lucide-react"; import { AnimatePresence, motion, useReducedMotion, type Variants } from "motion/react"; import type { Matcher } from "react-day-picker"; import { Button } from "@/components/ui/button"; import { Calendar, type CalendarProps } from "@/components/ui/calendar"; import { Popover, PopoverTrigger } from "@/components/ui/popover"; import { cn } from "@/lib/utils"; export interface DatePickerProps extends Omit<React.ComponentProps<typeof Button>, "value" | "defaultValue" | "onChange" | "children" | "disabled"> { value?: Date; defaultValue?: Date; /** Called with the picked date, or undefined when it is cleared by clicking it again. */ onValueChange?: (date: Date | undefined) => void; /** Trigger text when no date is picked. */ placeholder?: string; /** date-fns format string for the trigger. Default "PPP" (e.g. June 12, 2026). */ dateFormat?: string; /** Days that can't be picked, e.g. `{ before: today }`. */ disabledDays?: Matcher | Matcher[]; /** Disable the whole picker. */ disabled?: boolean; /** Form field name. The date is submitted as yyyy-MM-dd in a hidden input. */ name?: string; /** Extra props for the Calendar (e.g. `startMonth`, `captionLayout`, `today`). */ calendarProps?: Omit<Extract<CalendarProps, { mode: "single" }>, "mode" | "selected" | "onSelect" | "required">; /** Classes for the floating panel. `className` styles the trigger. */ contentClassName?: string; } /** * A date picker: an outline trigger that opens the DuskUI Calendar in a * popover. The panel scales out of the trigger, the formatted date rolls in * when it changes, and the calendar's selected pill glides between days. The * popover closes on pick and focus returns to the trigger. Radix handles * focus, Escape and outside clicks; react-day-picker handles the keyboard * grid. Under reduced motion it fades only. */ export function DatePicker({ value: valueProp, defaultValue, onValueChange, placeholder = "Pick a date", dateFormat = "PPP", disabledDays, disabled, name, calendarProps, className, contentClassName, ...props }: DatePickerProps) { const reduceMotion = useReducedMotion() ?? false; const [uncontrolled, setUncontrolled] = React.useState<Date | undefined>(defaultValue); const [open, setOpen] = React.useState(false); const value = valueProp !== undefined ? valueProp : uncontrolled; const label = value ? formatDate(value, dateFormat) : placeholder; const select = (date: Date | undefined) => { if (valueProp === undefined) setUncontrolled(date); onValueChange?.(date); if (date) setOpen(false); }; const surface: Variants = reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1, transition: { duration: 0.15 } } } : { closed: { opacity: 0, scale: 0.96, y: -4, transition: { duration: 0.12, ease: "easeIn" } }, open: { opacity: 1, scale: 1, y: 0, transition: { type: "spring", duration: 0.25, bounce: 0 } }, }; return ( <Popover open={open} onOpenChange={setOpen}> <PopoverTrigger asChild> <Button type="button" variant="outline" disabled={disabled} className={cn( "w-full min-w-56 justify-start gap-2 px-3 font-normal hover:border-ring/40", open && "border-ring/60 ring-[3px] ring-ring/15", className )} {...props} > <CalendarIcon aria-hidden className={cn("text-muted-foreground transition-colors", (open || value) && "text-foreground")} /> <span className="relative flex min-w-0 flex-1 overflow-hidden"> <AnimatePresence mode="popLayout" initial={false}> <motion.span key={label} className={cn("truncate", !value && "text-muted-foreground")} initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: 6 }} animate={{ opacity: 1, y: 0 }} exit={reduceMotion ? { opacity: 0 } : { opacity: 0, y: -6 }} transition={{ type: "spring", duration: 0.25, bounce: 0 }} > {label} </motion.span> </AnimatePresence> </span> </Button> </PopoverTrigger> {name && <input type="hidden" name={name} value={value ? formatDate(value, "yyyy-MM-dd") : ""} />} <AnimatePresence> {open && ( // forceMount hands mounting to AnimatePresence so the exit animation can play. <PopoverPrimitive.Portal key="date-picker" forceMount> <PopoverPrimitive.Content forceMount asChild align="start" sideOffset={6}> <motion.div variants={surface} initial="closed" animate="open" exit="closed" className={cn( "z-50 w-auto overflow-hidden rounded-xl border bg-popover text-popover-foreground outline-none", "origin-(--radix-popover-content-transform-origin)", "shadow-lg", contentClassName )} > <Calendar {...calendarProps} mode="single" selected={value} onSelect={select} defaultMonth={calendarProps?.defaultMonth ?? value} disabled={disabledDays} autoFocus /> </motion.div> </PopoverPrimitive.Content> </PopoverPrimitive.Portal> )} </AnimatePresence> </Popover> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| value / defaultValue | Date | undefined | — | Controlled or initial date. |
| onValueChange | (date: Date | undefined) => void | — | Called when a date is picked (or cleared by clicking it again). |
| placeholder | string | "Pick a date" | Trigger text when empty. |
| dateFormat | string | "PPP" | date-fns format for the trigger. |
| disabledDays | Matcher | Matcher[] | — | Days that can't be picked, e.g. { before: today }. |
| name | string | — | Submits the date in a hidden input. |
| calendarProps | CalendarProps | — | Extra Calendar props such as today, defaultMonth, startMonth or captionLayout. |
| contentClassName | string | — | Classes for the popover panel. className styles the trigger. |
Date Range Picker
Two months side by side with quick ranges — Today, Last 7 days, This month and more. A pill glides to the active preset, choosing one slides the calendar to that range, and a footer counts the days with Clear and Done. Bring your own presets with presets.
"use client";
import * as React from "react";
import { subDays } from "date-fns";
import { DateRangePicker, type DateRange } from "@/components/dusk/calendar/date-range-picker";
const TODAY = new Date(2026, 5, 12);
export default function DateRangePickerDemo() {
const [range, setRange] = React.useState<DateRange | undefined>({ from: subDays(TODAY, 6), to: TODAY });
return (
<div className="grid w-full max-w-sm gap-2">
<span id="report-range" className="text-sm font-medium">
Report period
</span>
<DateRangePicker
id="report-range-trigger"
aria-labelledby="report-range report-range-trigger"
value={range}
onValueChange={setRange}
today={TODAY}
disabledDays={{ after: TODAY }}
/>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/date-range-picker.json1. Install the dependencies.
npm install @radix-ui/react-popover react-day-picker date-fns motion lucide-react2. Copy the source into
components/dusk/calendar/date-range-picker.tsx.date-range-picker.tsx "use client"; import * as React from "react"; import * as PopoverPrimitive from "@radix-ui/react-popover"; import { addMonths, differenceInCalendarDays, endOfMonth, format as formatDate, isAfter, isSameDay, startOfDay, startOfMonth, startOfYear, subDays, subMonths, } from "date-fns"; import { CalendarRangeIcon } from "lucide-react"; import { AnimatePresence, motion, useReducedMotion, type Transition, type Variants } from "motion/react"; import type { DateRange, Matcher } from "react-day-picker"; import { Button } from "@/components/ui/button"; import { Calendar, type CalendarProps } from "@/components/ui/calendar"; import { Popover, PopoverTrigger } from "@/components/ui/popover"; import { cn } from "@/lib/utils"; export type { DateRange }; export interface DateRangePreset { label: string; /** Returns the range for this preset, given today's date (start of day). */ getRange: (today: Date) => { from: Date; to: Date }; } export const defaultRangePresets: DateRangePreset[] = [ { label: "Today", getRange: (t) => ({ from: t, to: t }) }, { label: "Yesterday", getRange: (t) => ({ from: subDays(t, 1), to: subDays(t, 1) }) }, { label: "Last 7 days", getRange: (t) => ({ from: subDays(t, 6), to: t }) }, { label: "Last 30 days", getRange: (t) => ({ from: subDays(t, 29), to: t }) }, { label: "This month", getRange: (t) => ({ from: startOfMonth(t), to: t }) }, { label: "Last month", getRange: (t) => ({ from: startOfMonth(subMonths(t, 1)), to: endOfMonth(subMonths(t, 1)) }) }, { label: "Year to date", getRange: (t) => ({ from: startOfYear(t), to: t }) }, ]; export interface DateRangePickerProps extends Omit<React.ComponentProps<typeof Button>, "value" | "defaultValue" | "onChange" | "children" | "disabled"> { value?: DateRange; defaultValue?: DateRange; onValueChange?: (range: DateRange | undefined) => void; placeholder?: string; /** date-fns format string for each end of the range. Default "LLL d, y" (e.g. Jun 12, 2026). */ dateFormat?: string; /** Quick ranges in the side panel. Pass `[]` to hide the panel. */ presets?: DateRangePreset[]; /** Months shown side by side (stacked on small screens). Default 2. */ numberOfMonths?: number; /** "Today" for the presets and the calendar. Defaults to the current date when the picker opens. */ today?: Date; /** Days that can't be picked. */ disabledDays?: Matcher | Matcher[]; disabled?: boolean; /** Extra props for the Calendar. */ calendarProps?: Omit< Extract<CalendarProps, { mode: "range" }>, "mode" | "selected" | "onSelect" | "required" | "month" | "onMonthChange" | "numberOfMonths" >; /** Classes for the floating panel. `className` styles the trigger. */ contentClassName?: string; } const pillSpring: Transition = { type: "spring", stiffness: 520, damping: 38, mass: 0.6 }; /** The first visible month that still shows `range.to` in the last column. */ function monthFor(range: { from: Date; to: Date }, months: number) { const fromMonth = startOfMonth(range.from); const lastFit = addMonths(startOfMonth(range.to), -(months - 1)); return isAfter(lastFit, fromMonth) ? lastFit : fromMonth; } /** * A date range picker with presets. The trigger opens a popover with quick * ranges ("Last 7 days", "This month"…) next to a two-month DuskUI Calendar. * A highlight pill glides to the active preset, picking a preset slides the * calendar to that range in the right direction, and the trigger text rolls * to the new range. A footer shows the length of the range with Clear and * Done. Under reduced motion everything fades or jumps instead. */ export function DateRangePicker({ value: valueProp, defaultValue, onValueChange, placeholder = "Pick a date range", dateFormat = "LLL d, y", presets = defaultRangePresets, numberOfMonths = 2, today: todayProp, disabledDays, disabled, calendarProps, className, contentClassName, ...props }: DateRangePickerProps) { const id = React.useId(); const reduceMotion = useReducedMotion() ?? false; const [uncontrolled, setUncontrolled] = React.useState<DateRange | undefined>(defaultValue); const [open, setOpen] = React.useState(false); // Resolved when the popover opens (an event), so nothing reads the clock during render. const [today, setToday] = React.useState<Date | undefined>(todayProp); const [month, setMonth] = React.useState<Date | undefined>(undefined); const value = valueProp !== undefined ? valueProp : uncontrolled; const label = value?.from ? value.to && !isSameDay(value.from, value.to) ? `${formatDate(value.from, dateFormat)} – ${formatDate(value.to, dateFormat)}` : formatDate(value.from, dateFormat) : placeholder; const commit = (range: DateRange | undefined) => { if (valueProp === undefined) setUncontrolled(range); onValueChange?.(range); }; const handleOpenChange = (next: boolean) => { setOpen(next); if (!next) return; const now = startOfDay(todayProp ?? new Date()); setToday(now); setMonth(value?.from && value.to ? monthFor({ from: value.from, to: value.to }, numberOfMonths) : value?.from ?? addMonths(now, -(numberOfMonths - 1))); }; const applyPreset = (preset: DateRangePreset) => { if (!today) return; const range = preset.getRange(today); commit(range); setMonth(monthFor(range, numberOfMonths)); }; const activePreset = today && value?.from && value.to ? presets.find((p) => { const r = p.getRange(today); return isSameDay(r.from, value.from!) && isSameDay(r.to, value.to!); }) : undefined; const days = value?.from && value.to ? differenceInCalendarDays(value.to, value.from) + 1 : 0; const surface: Variants = reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1, transition: { duration: 0.15 } } } : { closed: { opacity: 0, scale: 0.96, y: -4, transition: { duration: 0.12, ease: "easeIn" } }, open: { opacity: 1, scale: 1, y: 0, transition: { type: "spring", duration: 0.25, bounce: 0 } }, }; return ( <Popover open={open} onOpenChange={handleOpenChange}> <PopoverTrigger asChild> <Button type="button" variant="outline" disabled={disabled} className={cn( "w-full min-w-64 justify-start gap-2 px-3 font-normal hover:border-ring/40", open && "border-ring/60 ring-[3px] ring-ring/15", className )} {...props} > <CalendarRangeIcon aria-hidden className={cn("text-muted-foreground transition-colors", (open || value?.from) && "text-foreground")} /> <span className="relative flex min-w-0 flex-1 overflow-hidden"> <AnimatePresence mode="popLayout" initial={false}> <motion.span key={label} className={cn("truncate", !value?.from && "text-muted-foreground")} initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: 6 }} animate={{ opacity: 1, y: 0 }} exit={reduceMotion ? { opacity: 0 } : { opacity: 0, y: -6 }} transition={{ type: "spring", duration: 0.25, bounce: 0 }} > {label} </motion.span> </AnimatePresence> </span> </Button> </PopoverTrigger> <AnimatePresence> {open && ( <PopoverPrimitive.Portal key="date-range-picker" forceMount> <PopoverPrimitive.Content forceMount asChild align="start" sideOffset={6} collisionPadding={8}> <motion.div variants={surface} initial="closed" animate="open" exit="closed" className={cn( "z-50 max-h-(--radix-popover-content-available-height) max-w-[calc(100vw-1rem)] overflow-auto rounded-xl border bg-popover text-popover-foreground outline-none", "origin-(--radix-popover-content-transform-origin)", "shadow-lg", contentClassName )} > <div className="flex flex-col sm:flex-row"> {presets.length > 0 && ( <div role="group" aria-label="Quick ranges" className="flex gap-1 overflow-x-auto border-b p-2 sm:w-40 sm:flex-col sm:overflow-visible sm:border-r sm:border-b-0" > {presets.map((preset) => { const active = preset === activePreset; return ( <button key={preset.label} type="button" aria-pressed={active} onClick={() => applyPreset(preset)} className={cn( "relative shrink-0 rounded-md px-2.5 py-1.5 text-left text-sm whitespace-nowrap text-muted-foreground outline-none", "transition-colors duration-150 hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50", active && "text-foreground" )} > {active && ( <motion.span aria-hidden layoutId={`${id}-preset`} transition={reduceMotion ? { duration: 0 } : pillSpring} className="absolute inset-0 rounded-md bg-accent" /> )} <span className="relative">{preset.label}</span> </button> ); })} </div> )} <div className="flex flex-col"> <Calendar {...calendarProps} mode="range" selected={value} onSelect={commit} month={month} onMonthChange={setMonth} numberOfMonths={numberOfMonths} today={calendarProps?.today ?? today} disabled={disabledDays} /> <div className="flex items-center justify-between gap-3 border-t px-3 py-2.5"> <p aria-live="polite" className="text-xs text-muted-foreground tabular-nums"> {days > 0 ? `${days} ${days === 1 ? "day" : "days"} selected` : value?.from ? "Pick an end date" : "Pick a start date"} </p> <div className="flex gap-2"> <Button type="button" size="sm" variant="ghost" aria-disabled={!value?.from || undefined} onClick={() => value?.from && commit(undefined)} > Clear </Button> <Button type="button" size="sm" onClick={() => setOpen(false)}> Done </Button> </div> </div> </div> </div> </motion.div> </PopoverPrimitive.Content> </PopoverPrimitive.Portal> )} </AnimatePresence> </Popover> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| value / defaultValue | DateRange | undefined | — | Controlled or initial range ({ from, to }). |
| onValueChange | (range: DateRange | undefined) => void | — | Called on every change. |
| presets | { label: string; getRange: (today: Date) => { from: Date; to: Date } }[] | defaultRangePresets | Quick ranges in the side panel. [] hides it. |
| numberOfMonths | number | 2 | Months shown side by side. |
| today | Date | — | Reference date for presets. Defaults to the current date when the picker opens. |
| dateFormat | string | "LLL d, y" | date-fns format for each end of the range. |
| disabledDays | Matcher | Matcher[] | — | Days that can't be picked. |
| calendarProps | CalendarProps | — | Extra Calendar props. |
API Reference
Calendar forwards every react-day-picker prop. The most used ones:
| Prop | Type | Default | Description |
|---|---|---|---|
| mode | "single" | "multiple" | "range" | — | Selection mode. In single mode the selected pill glides between days. |
| selected / onSelect | Date | Date[] | DateRange | — | The selection and its change handler. |
| today | Date | — | Overrides today. Pass a fixed date for server rendering. |
| month / defaultMonth / onMonthChange | Date | — | The displayed month, controlled or not. |
| numberOfMonths | number | 1 | Months shown at once. |
| captionLayout | "label" | "dropdown" | "dropdown-months" | "dropdown-years" | "label" | Caption text or month / year selects. |
| disabled | Matcher | Matcher[] | — | Days that can't be selected. |
| animate | boolean | true | Slide months in the navigation direction. Off under reduced motion. |
| showOutsideDays | boolean | true | Show days from the neighbouring months. |
| buttonVariant | Button variant | "ghost" | Variant of the previous / next month buttons. |
Accessibility
- react-day-picker renders a
role="grid"per month with labelled day buttons,aria-selectedand a roving tab stop, so only one day is in the tab order. - Full keyboard support: arrow keys move by day and week, Home / End to the week edges, PageUp / PageDown by month and Shift+PageUp / PageDown by year. Disabled days are skipped.
- The month caption is a polite live region, so changing month is announced. Month buttons have labels like "Go to the Next Month".
- The pickers use Radix Popover: focus moves into the calendar on open, Escape closes it and focus returns to the trigger.
- Under prefers-reduced-motion months swap instantly, the selected pill jumps and popovers only fade.