Date Picker
Date range picker card for booking and reporting filters: a labeled trigger opens a two-month range calendar in a popover, preset to a 20-day span built with date-fns.
import { addDays } from "date-fns"
import { DateRangePicker } from "@/components/controls/date-range-picker"
import { Card, CardContent } from "@/components/ui/card"
// The copy: change text here, not in the markup.
const COPY = {
label: "Pick a date",
placeholder: "Pick a date",
} as const
const START = new Date(new Date().getFullYear(), 0, 20)
/**
* A date range field in a card: a label and a button showing the chosen
* range, which opens a two-month calendar. It starts with January 20 to
* February 9 of the current year selected.
*
* The label and placeholder live in `COPY`; the start date is `START`.
*/
export default function DatePicker({
defaultOpen = false,
}: {
/** Opens the calendar on first render. Defaults to `false`. */
defaultOpen?: boolean
}) {
return (
<Card className="mx-auto w-full max-w-md">
<CardContent>
<DateRangePicker
label={COPY.label}
placeholder={COPY.placeholder}
defaultValue={{ from: START, to: addDays(START, 20) }}
defaultOpen={defaultOpen}
/>
</CardContent>
</Card>
)
}
"use client"
import { useId, useState, type ComponentProps, type ReactNode } from "react"
import { CalendarBlankIcon } from "@phosphor-icons/react"
import { format } from "date-fns"
import { type DateRange } from "react-day-picker"
import { Button } from "@/components/ui/button"
import { Calendar } from "@/components/ui/calendar"
import { Field, FieldLabel } from "@/components/ui/field"
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover"
type DateRangePickerProps = Omit<
ComponentProps<typeof Field>,
"children" | "defaultValue"
> & {
/** The field's label, tied to the button that opens the calendar. */
label: ReactNode
/** The button's text before any date is picked. */
placeholder: ReactNode
/** Controlled with value + onValueChange, or seeded with defaultValue. */
value?: DateRange
/** The range picked at first, when the picker keeps the value itself. */
defaultValue?: DateRange
/** Called with the new range on each pick; `undefined` once it is cleared. */
onValueChange?: (range: DateRange | undefined) => void
/** date-fns pattern for the trigger text. */
dateFormat?: string
/** How many months the calendar shows side by side; 2 by default. */
numberOfMonths?: number
/** Opens the calendar on first render. */
defaultOpen?: boolean
}
/**
* A labelled button that opens a calendar for picking a start and end date,
* and shows the picked range on the button. On a phone the months stack and
* the popup scrolls within the screen. Other props go to the Field.
*/
function DateRangePicker(pickerProps: DateRangePickerProps) {
const {
label,
placeholder,
value,
defaultValue,
onValueChange,
dateFormat = "LLL dd, y",
numberOfMonths = 2,
defaultOpen = false,
className,
...props
} = pickerProps
const id = useId()
const [uncontrolled, setUncontrolled] = useState(defaultValue)
const controlled = Object.hasOwn(pickerProps, "value")
const range = controlled ? value : uncontrolled
const select = (next: DateRange | undefined) => {
if (!controlled) setUncontrolled(next)
onValueChange?.(next)
}
return (
<Field data-slot="date-range-picker" className={className} {...props}>
<FieldLabel htmlFor={id}>{label}</FieldLabel>
<Popover defaultOpen={defaultOpen}>
<PopoverTrigger
render={
<Button
variant="outline"
id={id}
className="justify-start px-2.5 font-normal"
/>
}
>
<CalendarBlankIcon data-icon="inline-start" />
{range?.from ? (
range.to ? (
<>
{format(range.from, dateFormat)} -{" "}
{format(range.to, dateFormat)}
</>
) : (
format(range.from, dateFormat)
)
) : (
<span>{placeholder}</span>
)}
</PopoverTrigger>
{/* Two months stack on a phone: the popup keeps to the room the
screen has and scrolls, so neither month runs off the top. */}
<PopoverContent
className="max-h-(--available-height) w-auto overflow-y-auto p-0"
align="start"
>
<Calendar
mode="range"
defaultMonth={range?.from}
selected={range}
onSelect={select}
numberOfMonths={numberOfMonths}
/>
</PopoverContent>
</Popover>
</Field>
)
}
export { DateRangePicker }
Installation
pnpm dlx shadcn@latest add @01orbital/date-pickernpx shadcn@latest add @01orbital/date-pickeryarn dlx shadcn@latest add @01orbital/date-pickerbunx --bun shadcn@latest add @01orbital/date-pickerAPI reference
A date range field in a card: a label and a button showing the chosen range, which opens a two-month calendar. It starts with January 20 to February 9 of the current year selected.
The label and placeholder live in COPY; the start date is START.
Prop
Type
Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/controls/date-picker/index.tsx: change them there, not in the markup. START, also at the top of the file, holds its lists and settings.
Built from
npm: date-fns. The CLI installs it for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on