opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Calender
  4. Date Range Picker
Back to Calender

Components / Calender

Date Range Picker

Free Calender React component — DateRangePickerCard. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.

Travel-style date range picker — first tap sets check-in, second sets check-out, days between fill in.

Preview

July 2026

Select a date range

SMTWTFS

Setup

How to use

Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge lucide-react
  2. 2

    Copy lib/cn.ts below. Skip if you already have cn().

  3. 3

    Copy the code below and create components/calender/date-range-picker-card.tsx in your project.

  4. 4

    Import and render:

    Example

    import { DateRangePickerCard } from "@/components/calender/date-range-picker-card";

    <DateRangePickerCard onChange={(start, end) => {}} />

lib/cn.ts

1
2
3
4
5
6
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

components/calender/date-range-picker-card.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"use client";
 
import {
forwardRef,
useMemo,
useState,
type ComponentPropsWithoutRef,
} from "react";
 
import { cn } from "@/lib/cn";
import { ChevronLeft, ChevronRight } from "lucide-react";
 
const WEEKDAYS = ["S", "M", "T", "W", "T", "F", "S"] as const;
 
type DayCell = Readonly<{ day: number; month: number; year: number }>;
 
function buildMonthCells(year: number, month: number): Array<DayCell | null> {
const firstDay = new Date(year, month, 1).getDay();
const daysInMonth = new Date(year, month + 1, 0).getDate();
const cells: Array<DayCell | null> = [];
 
for (let index = 0; index < firstDay; index += 1) {
cells.push(null);
}
for (let day = 1; day <= daysInMonth; day += 1) {
cells.push({ day, month, year });
}
return cells;
}
 
function toKey(cell: DayCell) {
return `${cell.year}-${cell.month}-${cell.day}`;
}
 
function formatCell(cell: DayCell) {
return new Date(cell.year, cell.month, cell.day).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
});
}
 
export type DateRangePickerCardProps = Readonly<
{
defaultYear?: number;
defaultMonth?: number;
onChange?: (start: Date | null, end: Date | null) => void;
} & ComponentPropsWithoutRef<"div">
>;
 
// Date range picker — first tap sets start, second tap sets end, range fills between.
export const DateRangePickerCard = forwardRef<
HTMLDivElement,
DateRangePickerCardProps
>(
(
{
className,
defaultYear,
defaultMonth,
onChange,
...props
},
ref,
) => {
const today = useMemo(() => new Date(), []);
const [viewYear, setViewYear] = useState(defaultYear ?? today.getFullYear());
const [viewMonth, setViewMonth] = useState(
defaultMonth ?? today.getMonth(),
);
const [start, setStart] = useState<DayCell | null>(null);
const [end, setEnd] = useState<DayCell | null>(null);
const [slideDirection, setSlideDirection] = useState(0);
 
const cells = useMemo(
() => buildMonthCells(viewYear, viewMonth),
[viewYear, viewMonth],
);
 
const monthLabel = new Date(viewYear, viewMonth, 1).toLocaleDateString(
"en-US",
{ month: "long", year: "numeric" },
);
 
const shiftMonth = (delta: number) => {
setSlideDirection(delta);
const next = new Date(viewYear, viewMonth + delta, 1);
setViewYear(next.getFullYear());
setViewMonth(next.getMonth());
};
 
const selectCell = (cell: DayCell) => {
if (!start || (start && end)) {
setStart(cell);
setEnd(null);
onChange?.(new Date(cell.year, cell.month, cell.day), null);
return;
}
 
const startTime = new Date(start.year, start.month, start.day).getTime();
const cellTime = new Date(cell.year, cell.month, cell.day).getTime();
 
if (cellTime < startTime) {
setEnd(start);
setStart(cell);
onChange?.(
new Date(cell.year, cell.month, cell.day),
new Date(start.year, start.month, start.day),
);
} else {
setEnd(cell);
onChange?.(
new Date(start.year, start.month, start.day),
new Date(cell.year, cell.month, cell.day),
);
}
};
 
const inRange = (cell: DayCell) => {
if (!start || !end) return false;
const time = new Date(cell.year, cell.month, cell.day).getTime();
const a = new Date(start.year, start.month, start.day).getTime();
const b = new Date(end.year, end.month, end.day).getTime();
const min = Math.min(a, b);
const max = Math.max(a, b);
return time >= min && time <= max;
};
 
const rangeLabel =
start && end
? `${formatCell(start)} – ${formatCell(end)}`
: start
? `${formatCell(start)} – pick end`
: "Select a date range";
 
return (
<div
ref={ref}
data-slot="date-range-picker-card"
className={cn(
"w-80 overflow-hidden rounded-2xl border border-neutral-200/80 bg-white p-4 font-sans shadow-lg shadow-black/5 select-none",
className,
)}
{...props}
>
<div className="mb-3 flex items-center justify-between">
<button
type="button"
aria-label="Previous month"
onClick={() => shiftMonth(-1)}
className="flex size-8 cursor-pointer items-center justify-center rounded-lg text-neutral-500 transition-all duration-200 ease-out hover:bg-neutral-100 active:scale-95"
>
<ChevronLeft size={16} strokeWidth={2} />
</button>
<p
key={monthLabel}
className="text-sm font-semibold text-neutral-900 opacity-100 starting:opacity-0 transition-all duration-300 ease-[cubic-bezier(0.25,0.46,0.45,0.94)]"
>
{monthLabel}
</p>
<button
type="button"
aria-label="Next month"
onClick={() => shiftMonth(1)}
className="flex size-8 cursor-pointer items-center justify-center rounded-lg text-neutral-500 transition-all duration-200 ease-out hover:bg-neutral-100 active:scale-95"
>
<ChevronRight size={16} strokeWidth={2} />
</button>
</div>
 
<p
key={rangeLabel}
className="mb-3 text-xs text-neutral-500 opacity-100 starting:opacity-0 transition-all duration-300 ease-out"
>
{rangeLabel}
</p>
 
<div className="mb-2 grid grid-cols-7 text-center text-[10px] font-medium text-neutral-400">
{WEEKDAYS.map((label, index) => (
<span key={`${label}-${index}`}>{label}</span>
))}
</div>
 
<div
key={`${viewYear}-${viewMonth}`}
className={cn(
"grid grid-cols-7 gap-y-1 opacity-100 transition-all duration-300 ease-[cubic-bezier(0.25,0.46,0.45,0.94)] starting:opacity-0",
slideDirection >= 0
? "starting:translate-x-2"
: "starting:-translate-x-2",
)}
>
{cells.map((cell, index) => {
if (!cell) {
return <span key={`empty-${index}`} className="h-9" />;
}
 
const key = toKey(cell);
const isStart = start && toKey(start) === key;
const isEnd = end && toKey(end) === key;
const isBetween = inRange(cell) && !isStart && !isEnd;
 
return (
<button
key={key}
type="button"
onClick={() => selectCell(cell)}
className={cn(
"mx-auto flex h-9 w-9 cursor-pointer items-center justify-center text-sm tabular-nums transition-all duration-200 ease-out active:scale-95",
isBetween &&
"w-full rounded-none bg-neutral-100 text-neutral-800",
(isStart || isEnd) &&
"scale-100 rounded-full bg-neutral-900 font-semibold text-white",
!isBetween &&
!isStart &&
!isEnd &&
"rounded-full text-neutral-700 hover:bg-neutral-100",
)}
>
{cell.day}
</button>
);
})}
</div>
</div>
);
},
);
 
DateRangePickerCard.displayName = "DateRangePickerCard";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/calender/date-range-picker-card.tsx

Sponsor spot · demo preview

Logo

Your brand name

Your tagline or category

A short pitch about your product or service goes here. This is a preview of how sponsor cards will look in this sidebar.

Your call to action

This slot is available. Sponsor Opensource UI and reach developers browsing components every day.