Date range picker
A booking-style calendar: pick check-in and check-out, see the range fill as you hover, jump with quick presets, and slide between months.
"use client";
import { useRef, useState, type KeyboardEvent } from "react";
import "./date-range-picker.css";
const TODAY = new Date(2026, 11, 5); // set to new Date() in your app
const WEEKDAYS = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"];
const day = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate());
const add = (d: Date, n: number) =>
new Date(d.getFullYear(), d.getMonth(), d.getDate() + n);
const same = (a: Date | null, b: Date | null) =>
!!a && !!b && a.getTime() === b.getTime();
const fmt = (d: Date | null) =>
d
? d.toLocaleDateString("en-GB", { day: "numeric", month: "short" })
: "Add date";
/** Monday-first grid of 6 weeks for the month of `m`. */
function monthGrid(m: Date) {
const first = new Date(m.getFullYear(), m.getMonth(), 1);
const offset = (first.getDay() + 6) % 7;
return Array.from({ length: 42 }, (_, i) => add(first, i - offset));
}
/**
* DateRangePicker
* - First click sets the start, second click sets the end (clicking before the start restarts).
* - Roving focus over the grid: arrow keys move by day/week, Enter picks. Past dates are disabled.
*/
export function DateRangePicker({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [month, setMonth] = useState(
new Date(TODAY.getFullYear(), TODAY.getMonth(), 1),
);
const [start, setStart] = useState<Date | null>(add(TODAY, 7));
const [end, setEnd] = useState<Date | null>(add(TODAY, 11));
const [hover, setHover] = useState<Date | null>(null);
const [focus, setFocus] = useState<Date>(add(TODAY, 7));
const [dir, setDir] = useState(0);
const grid = useRef<HTMLDivElement>(null);
const pick = (d: Date) => {
if (!start || end || d < start) {
setStart(d);
setEnd(null);
} else if (!same(d, start)) setEnd(d);
};
const shift = (n: number) => {
setDir(n);
setMonth((m) => new Date(m.getFullYear(), m.getMonth() + n, 1));
};
const preset = (from: number, nights: number) => {
const s = add(TODAY, from);
setStart(s);
setEnd(add(s, nights));
setDir(0);
setMonth(new Date(s.getFullYear(), s.getMonth(), 1));
setFocus(s);
};
const onKey = (e: KeyboardEvent) => {
const map: Record<string, number> = {
ArrowLeft: -1,
ArrowRight: 1,
ArrowUp: -7,
ArrowDown: 7,
};
if (e.key in map) {
e.preventDefault();
const next = add(focus, map[e.key]);
if (next < TODAY) return;
setFocus(next);
if (next.getMonth() !== month.getMonth()) shift(next > focus ? 1 : -1);
requestAnimationFrame(() =>
grid.current
?.querySelector<HTMLButtonElement>('[tabindex="0"]')
?.focus(),
);
}
};
const rangeEnd = end ?? (start && hover && hover > start ? hover : null);
const nights =
start && end ? Math.round((end.getTime() - start.getTime()) / 86400000) : 0;
return (
<div className="gu-date" data-theme={theme}>
<div className="gu-date__fields">
<div data-active={!start || end ? "" : undefined}>
<small>Check-in</small>
<b>{fmt(start)}</b>
</div>
<div data-active={start && !end ? "" : undefined}>
<small>Check-out</small>
<b>{fmt(end)}</b>
</div>
</div>
<div className="gu-date__presets" role="group" aria-label="Quick picks">
<button
type="button"
onClick={() => preset((5 - TODAY.getDay() + 7) % 7 || 7, 2)}
>
This weekend
</button>
<button type="button" onClick={() => preset(7, 7)}>
A week, next week
</button>
<button type="button" onClick={() => preset(30, 3)}>
3 nights next month
</button>
</div>
<div className="gu-date__head">
<button
type="button"
onClick={() => shift(-1)}
disabled={month <= new Date(TODAY.getFullYear(), TODAY.getMonth(), 1)}
aria-label="Previous month"
>
‹
</button>
<h3 aria-live="polite">
{month.toLocaleDateString("en-GB", {
month: "long",
year: "numeric",
})}
</h3>
<button type="button" onClick={() => shift(1)} aria-label="Next month">
›
</button>
</div>
<div className="gu-date__week" aria-hidden="true">
{WEEKDAYS.map((w) => (
<span key={w}>{w}</span>
))}
</div>
<div
ref={grid}
key={month.toISOString()}
className="gu-date__grid"
data-dir={dir}
role="group"
aria-label="Choose dates"
onKeyDown={onKey}
onMouseLeave={() => setHover(null)}
>
{monthGrid(month).map((d) => {
const outside = d.getMonth() !== month.getMonth();
const past = d < day(TODAY);
const isStart = same(d, start);
const isEnd = same(d, rangeEnd);
const inRange = start && rangeEnd && d > start && d < rangeEnd;
return (
<button
key={d.toISOString()}
type="button"
className="gu-date__day"
disabled={past}
data-outside={outside ? "" : undefined}
data-today={same(d, TODAY) ? "" : undefined}
data-start={isStart ? "" : undefined}
data-end={isEnd ? "" : undefined}
data-range={inRange ? "" : undefined}
data-edge={
isStart && rangeEnd
? "start"
: isEnd && start
? "end"
: undefined
}
tabIndex={same(d, focus) ? 0 : -1}
aria-pressed={isStart || isEnd || !!inRange}
aria-label={
d.toLocaleDateString("en-GB", {
weekday: "long",
day: "numeric",
month: "long",
}) + (isStart ? ", check-in" : isEnd ? ", check-out" : "")
}
onMouseEnter={() => setHover(d)}
onFocus={() => setFocus(d)}
onClick={() => pick(d)}
>
{d.getDate()}
</button>
);
})}
</div>
<div className="gu-date__foot">
<span role="status">
{nights
? `${nights} nights · ${fmt(start)} – ${fmt(end)}`
: start
? "Now pick your check-out date"
: "Pick your check-in date"}
</span>
<button
type="button"
onClick={() => {
setStart(null);
setEnd(null);
}}
>
Clear
</button>
</div>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Date range picker
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A booking-style date range picker: check-in/check-out summary fields, quick preset chips, a month calendar with prev/next, a range band that follows your hover after picking the start, and a footer with the night count.
## Layout and look
- Card 370px, radius 26px, padding 18px.
- Fields: two halves in one bordered box (radius 16px), small muted label + bold date; the field you're about to set is tinted orange.
- Preset chips: 36px outline pills ("This weekend", "A week, next week", "3 nights next month").
- Header: month name centered (weight 900) between 40px round ‹ › buttons.
- Grid: Monday-first, 44px day cells; circles drawn in ::before; start and end are Ink circles with Cream numbers (inverted in dark); the band between is 16% orange and connects into the edges with half-cell gradients; today has a small orange dot; past days are struck through and disabled; days from other months are dimmed.
- Footer: "4 nights · 12 Dec – 16 Dec" and an orange "Clear" pill.
## Behavior and motion
1. Click 1 sets start; click 2 sets end; clicking before the start restarts.
2. With only a start, hovering later days previews the band.
3. Changing month slides the grid in ±24px (350ms cubic-bezier(0.16, 1, 0.3, 1)).
4. Keyboard: roving tabindex, arrows move ±1/±7 days (crossing months), Enter/Space picks.
## Accessibility
- Each day is a button labelled with the full date plus ", check-in"/", check-out"; aria-pressed marks selected days; the month title and footer are live.
- prefers-reduced-motion: no slide.
## Props
theme. Set TODAY to new Date() in your app.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### DateRangePicker.tsx
```tsx
"use client";
import { useRef, useState, type KeyboardEvent } from "react";
import "./date-range-picker.css";
const TODAY = new Date(2026, 11, 5); // set to new Date() in your app
const WEEKDAYS = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"];
const day = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate());
const add = (d: Date, n: number) =>
new Date(d.getFullYear(), d.getMonth(), d.getDate() + n);
const same = (a: Date | null, b: Date | null) =>
!!a && !!b && a.getTime() === b.getTime();
const fmt = (d: Date | null) =>
d
? d.toLocaleDateString("en-GB", { day: "numeric", month: "short" })
: "Add date";
/** Monday-first grid of 6 weeks for the month of `m`. */
function monthGrid(m: Date) {
const first = new Date(m.getFullYear(), m.getMonth(), 1);
const offset = (first.getDay() + 6) % 7;
return Array.from({ length: 42 }, (_, i) => add(first, i - offset));
}
/**
* DateRangePicker
* - First click sets the start, second click sets the end (clicking before the start restarts).
* - Roving focus over the grid: arrow keys move by day/week, Enter picks. Past dates are disabled.
*/
export function DateRangePicker({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [month, setMonth] = useState(
new Date(TODAY.getFullYear(), TODAY.getMonth(), 1),
);
const [start, setStart] = useState<Date | null>(add(TODAY, 7));
const [end, setEnd] = useState<Date | null>(add(TODAY, 11));
const [hover, setHover] = useState<Date | null>(null);
const [focus, setFocus] = useState<Date>(add(TODAY, 7));
const [dir, setDir] = useState(0);
const grid = useRef<HTMLDivElement>(null);
const pick = (d: Date) => {
if (!start || end || d < start) {
setStart(d);
setEnd(null);
} else if (!same(d, start)) setEnd(d);
};
const shift = (n: number) => {
setDir(n);
setMonth((m) => new Date(m.getFullYear(), m.getMonth() + n, 1));
};
const preset = (from: number, nights: number) => {
const s = add(TODAY, from);
setStart(s);
setEnd(add(s, nights));
setDir(0);
setMonth(new Date(s.getFullYear(), s.getMonth(), 1));
setFocus(s);
};
const onKey = (e: KeyboardEvent) => {
const map: Record<string, number> = {
ArrowLeft: -1,
ArrowRight: 1,
ArrowUp: -7,
ArrowDown: 7,
};
if (e.key in map) {
e.preventDefault();
const next = add(focus, map[e.key]);
if (next < TODAY) return;
setFocus(next);
if (next.getMonth() !== month.getMonth()) shift(next > focus ? 1 : -1);
requestAnimationFrame(() =>
grid.current
?.querySelector<HTMLButtonElement>('[tabindex="0"]')
?.focus(),
);
}
};
const rangeEnd = end ?? (start && hover && hover > start ? hover : null);
const nights =
start && end ? Math.round((end.getTime() - start.getTime()) / 86400000) : 0;
return (
<div className="gu-date" data-theme={theme}>
<div className="gu-date__fields">
<div data-active={!start || end ? "" : undefined}>
<small>Check-in</small>
<b>{fmt(start)}</b>
</div>
<div data-active={start && !end ? "" : undefined}>
<small>Check-out</small>
<b>{fmt(end)}</b>
</div>
</div>
<div className="gu-date__presets" role="group" aria-label="Quick picks">
<button
type="button"
onClick={() => preset((5 - TODAY.getDay() + 7) % 7 || 7, 2)}
>
This weekend
</button>
<button type="button" onClick={() => preset(7, 7)}>
A week, next week
</button>
<button type="button" onClick={() => preset(30, 3)}>
3 nights next month
</button>
</div>
<div className="gu-date__head">
<button
type="button"
onClick={() => shift(-1)}
disabled={month <= new Date(TODAY.getFullYear(), TODAY.getMonth(), 1)}
aria-label="Previous month"
>
‹
</button>
<h3 aria-live="polite">
{month.toLocaleDateString("en-GB", {
month: "long",
year: "numeric",
})}
</h3>
<button type="button" onClick={() => shift(1)} aria-label="Next month">
›
</button>
</div>
<div className="gu-date__week" aria-hidden="true">
{WEEKDAYS.map((w) => (
<span key={w}>{w}</span>
))}
</div>
<div
ref={grid}
key={month.toISOString()}
className="gu-date__grid"
data-dir={dir}
role="group"
aria-label="Choose dates"
onKeyDown={onKey}
onMouseLeave={() => setHover(null)}
>
{monthGrid(month).map((d) => {
const outside = d.getMonth() !== month.getMonth();
const past = d < day(TODAY);
const isStart = same(d, start);
const isEnd = same(d, rangeEnd);
const inRange = start && rangeEnd && d > start && d < rangeEnd;
return (
<button
key={d.toISOString()}
type="button"
className="gu-date__day"
disabled={past}
data-outside={outside ? "" : undefined}
data-today={same(d, TODAY) ? "" : undefined}
data-start={isStart ? "" : undefined}
data-end={isEnd ? "" : undefined}
data-range={inRange ? "" : undefined}
data-edge={
isStart && rangeEnd
? "start"
: isEnd && start
? "end"
: undefined
}
tabIndex={same(d, focus) ? 0 : -1}
aria-pressed={isStart || isEnd || !!inRange}
aria-label={
d.toLocaleDateString("en-GB", {
weekday: "long",
day: "numeric",
month: "long",
}) + (isStart ? ", check-in" : isEnd ? ", check-out" : "")
}
onMouseEnter={() => setHover(d)}
onFocus={() => setFocus(d)}
onClick={() => pick(d)}
>
{d.getDate()}
</button>
);
})}
</div>
<div className="gu-date__foot">
<span role="status">
{nights
? `${nights} nights · ${fmt(start)} – ${fmt(end)}`
: start
? "Now pick your check-out date"
: "Pick your check-in date"}
</span>
<button
type="button"
onClick={() => {
setStart(null);
setEnd(null);
}}
>
Clear
</button>
</div>
</div>
);
}
```
### date-range-picker.css
```css
/* DateRangePicker: change these colors to match your brand. */
.gu-date {
--card: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.12);
--range: rgb(255 90 31 / 0.16);
--accent: #ff5a1f;
--edge: #0e0e10;
--edge-text: #f3eee3;
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
display: grid;
gap: 14px;
width: min(100%, 370px);
padding: 18px;
border: 1px solid var(--line);
border-radius: 26px;
background: var(--card);
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-date[data-theme="dark"] {
--card: #18181b;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.12);
--range: rgb(255 90 31 / 0.22);
--edge: #f3eee3;
--edge-text: #0e0e10;
--focus: #d7ff3a;
}
.gu-date button {
font: inherit;
color: inherit;
cursor: pointer;
}
.gu-date button:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 1px;
z-index: 2;
}
.gu-date__fields {
display: grid;
grid-template-columns: 1fr 1fr;
overflow: hidden;
border: 1.5px solid var(--line);
border-radius: 16px;
}
.gu-date__fields div {
display: grid;
padding: 10px 14px;
transition: background-color 0.25s;
}
.gu-date__fields div + div {
border-left: 1.5px solid var(--line);
}
.gu-date__fields div[data-active] {
background: var(--range);
}
.gu-date__fields small {
color: var(--muted);
font-size: 0.72rem;
font-weight: 800;
}
.gu-date__fields b {
font-size: 1.05rem;
}
.gu-date__presets {
display: flex;
gap: 6px;
overflow-x: auto;
scrollbar-width: none;
}
.gu-date__presets button {
flex: none;
min-height: 36px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: none;
font-size: 0.8rem;
font-weight: 700;
}
.gu-date__presets button:hover {
border-color: var(--text);
}
.gu-date__head {
display: grid;
grid-template-columns: 40px 1fr 40px;
align-items: center;
}
.gu-date__head h3 {
margin: 0;
font-size: 1.05rem;
font-weight: 900;
text-align: center;
}
.gu-date__head button {
width: 40px;
height: 40px;
border: 0;
border-radius: 50%;
background: none;
font-size: 1.5rem;
line-height: 1;
}
.gu-date__head button:hover:not(:disabled) {
background: var(--line);
}
.gu-date__head button:disabled {
opacity: 0.25;
cursor: default;
}
.gu-date__week,
.gu-date__grid {
display: grid;
grid-template-columns: repeat(7, 1fr);
}
.gu-date__week span {
color: var(--muted);
font-size: 0.72rem;
font-weight: 700;
text-align: center;
}
.gu-date__grid {
row-gap: 2px;
animation: gu-date-in 0.35s var(--ease);
}
.gu-date__grid[data-dir="1"] {
animation-name: gu-date-next;
}
.gu-date__grid[data-dir="-1"] {
animation-name: gu-date-prev;
}
@keyframes gu-date-in {
from {
opacity: 0;
}
}
@keyframes gu-date-next {
from {
opacity: 0;
transform: translateX(24px);
}
}
@keyframes gu-date-prev {
from {
opacity: 0;
transform: translateX(-24px);
}
}
.gu-date__day {
position: relative;
height: 44px;
padding: 0;
border: 0;
background: none;
font-size: 0.9rem !important;
font-weight: 600 !important;
font-variant-numeric: tabular-nums;
isolation: isolate;
}
/* The circle lives in ::before; the range band in ::after */
.gu-date__day::before {
content: "";
position: absolute;
inset: 2px;
z-index: -1;
border-radius: 50%;
transition: background-color 0.15s;
}
.gu-date__day:hover:not(:disabled)::before {
background: var(--line);
}
.gu-date__day[data-outside] {
color: var(--muted);
opacity: 0.55;
}
.gu-date__day:disabled {
color: var(--muted);
opacity: 0.3;
text-decoration: line-through;
cursor: not-allowed;
}
.gu-date__day[data-today]::after {
content: "";
position: absolute;
bottom: 6px;
left: 50%;
width: 4px;
height: 4px;
margin-left: -2px;
border-radius: 50%;
background: var(--accent);
}
.gu-date__day[data-range] {
background: var(--range);
}
.gu-date__day[data-edge="start"] {
background: linear-gradient(90deg, transparent 50%, var(--range) 50%);
}
.gu-date__day[data-edge="end"] {
background: linear-gradient(90deg, var(--range) 50%, transparent 50%);
}
.gu-date__day[data-start]::before,
.gu-date__day[data-end]::before {
background: var(--edge) !important;
}
.gu-date__day[data-start],
.gu-date__day[data-end] {
color: var(--edge-text);
}
.gu-date__foot {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 0.85rem;
font-weight: 600;
}
.gu-date__foot button {
min-height: 40px;
padding: 0 14px;
border: 0;
border-radius: 999px;
background: var(--accent);
color: #0e0e10 !important;
font-weight: 800;
}
@media (prefers-reduced-motion: reduce) {
.gu-date__grid,
.gu-date__grid[data-dir="1"],
.gu-date__grid[data-dir="-1"] {
animation: none;
}
}
```
You might also want
Next piece: Depth cards heroOne-time code input
Components, intermediate
Tag input
Components, intermediate
Command menu
Components, intermediate