Date picker
Also called datepicker, calendar picker, or date range picker
A form control that opens a small calendar so people can pick a date, or a range of dates, by clicking days.
import { useState } from 'react';
import { Popover } from '@base-ui/react/popover';
const WEEKDAYS = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
const MONTH_YEAR = { month: 'long', year: 'numeric' } as const;
const FULL_DATE = { weekday: 'short', month: 'short', day: 'numeric' } as const;
export function PublishDatePicker() {
const today = new Date(new Date().setHours(0, 0, 0, 0));
const [page, setPage] = useState(0); // months after the current one
const [selected, setSelected] = useState<Date>();
const [y, m] = [today.getFullYear(), today.getMonth() + page];
const count = new Date(y, m + 1, 0).getDate(); // days in this month
const days = Array.from({ length: count }, (_, i) => new Date(y, m, i + 1));
const blanks = new Date(y, m, 1).getDay(); // empty cells before the 1st
return (
<Popover.Root>
<Popover.Trigger className='h-10 w-56 rounded-lg border px-3 text-left text-sm'>
{selected?.toLocaleDateString('en-US', FULL_DATE) ?? 'Pick a date'}
</Popover.Trigger>
<Popover.Portal>
<Popover.Positioner align='start' sideOffset={6}>
<Popover.Popup className='grid w-72 grid-cols-7 gap-1 rounded-xl border bg-background p-3 text-center text-sm shadow-lg [&>span]:text-xs [&>span]:text-muted-foreground'>
<button
onClick={() => setPage(page - 1)}
aria-label='Previous month'
>
‹
</button>
<Popover.Title className='col-span-5 font-medium'>
{new Date(y, m).toLocaleDateString('en-US', MONTH_YEAR)}
</Popover.Title>
<button onClick={() => setPage(page + 1)} aria-label='Next month'>
›
</button>
{WEEKDAYS.map((weekday) => (
<span key={weekday}>{weekday}</span>
))}
{blanks > 0 && <i style={{ gridColumn: `span ${blanks}` }} />}
{days.map((day) => (
// Popover.Close picks the day and closes the calendar in one click.
<Popover.Close
key={day.getDate()}
disabled={day < today}
onClick={() => setSelected(day)}
data-selected={+day === +(selected ?? 0) || undefined}
className='h-9 rounded-md tabular-nums enabled:not-data-selected:hover:bg-muted disabled:opacity-30 data-selected:bg-blue-600 data-selected:text-white'
>
{day.getDate()}
</Popover.Close>
))}
</Popover.Popup>
</Popover.Positioner>
</Popover.Portal>
</Popover.Root>
);
}Add a date range picker to the booking form: clicking the field opens a one-month calendar below it, the first click sets check-in, the second sets check-out, and the nights between get a light blue highlight. Disable past dates, slide the month grid 24px with a fade over 200ms when paging, and support arrow keys, Page Up/Page Down, Enter, and Escape.
Paste into Layerwise or any AI coding tool.
What is Date picker?
A date picker pairs a text field with a calendar grid. Clicking the field opens the calendar right below it, arrows page between months, and clicking a day fills in the field. A range picker takes two clicks, a start and an end, and shades the days in between.
Use it when the day matters in context, such as booking a stay, scheduling a meeting, or filtering a report by date range. For a birthday or another date far in the past, separate month, day, and year fields are faster than paging back through decades of calendars.
Let people type the date as well, and show the expected format. Disable days that cannot be chosen, like past dates or fully booked nights. A complete picker works from the keyboard: arrow keys move between days, Page Up and Page Down change the month, Enter picks, and Escape closes.
The browser’s built-in <input type="date"> is accessible and works well on phones. Reach for a custom picker when you need ranges, custom styling, or extra information on each day, such as prices.
Related terms
All termsPopover
A small floating panel anchored to the button that opened it, often with an arrow, holding a few controls and closing when you click outside.
Combobox
A text field with a dropdown list that filters as you type, so people can pick one option from a long list quickly.
Direction-aware transition
A transition that slides new content in from the side you are heading toward, so going forward and going back move in opposite directions.
Inline validation
Form checking that happens right next to each field as you fill it in, showing an error or a checkmark before you press Submit.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.