"use client";
import { ChevronLeft, ChevronRight } from "lucide-react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { useEffect, useId, useRef, useState, type KeyboardEvent } from "react";
import { cn } from "@/lib/utils";
const MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const DAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const stamp = (d: Date) => d.getFullYear() * 10000 + d.getMonth() * 100 + d.getDate();
const addDays = (d: Date, n: number) => new Date(d.getFullYear(), d.getMonth(), d.getDate() + n);
const monday = (d: Date) => addDays(d, -((d.getDay() + 6) % 7));
type WeekStripProps = {
value?: Date;
defaultValue?: Date;
onChange?: (date: Date) => void;
today?: Date;
/** How many dots (0–3) to show under a day. */
markers?: (date: Date) => number;
className?: string;
};
const DEMO_TODAY = new Date(2026, 2, 14);
export function WeekStrip({ value, defaultValue, onChange, today = DEMO_TODAY, markers, className }: WeekStripProps) {
const [inner, setInner] = useState(defaultValue ?? today);
const selected = value ?? inner;
const [start, setStart] = useState(() => monday(selected));
const [direction, setDirection] = useState(1);
const strip = useRef<HTMLDivElement>(null);
const moveFocus = useRef(false);
const reduce = useReducedMotion();
const uid = useId();
const days = Array.from({ length: 7 }, (_, i) => addDays(start, i));
const mid = days[3];
useEffect(() => {
if (!moveFocus.current) return;
moveFocus.current = false;
strip.current?.querySelector<HTMLButtonElement>(`[data-week="${stamp(start)}"] [data-day="${stamp(selected)}"]`)?.focus();
}, [selected, start]);
function select(day: Date, viaKeyboard = false) {
const week = monday(day);
if (stamp(week) !== stamp(start)) {
setDirection(stamp(week) > stamp(start) ? 1 : -1);
setStart(week);
}
moveFocus.current = viaKeyboard;
if (value === undefined) setInner(day);
onChange?.(day);
}
function shift(n: number) {
setDirection(n);
setStart(addDays(start, 7 * n));
}
function onKeyDown(e: KeyboardEvent<HTMLDivElement>) {
const moves: Record<string, Date> = {
ArrowLeft: addDays(selected, -1),
ArrowRight: addDays(selected, 1),
Home: monday(selected),
End: addDays(monday(selected), 6),
PageUp: addDays(selected, -7),
PageDown: addDays(selected, 7),
};
if (e.key in moves) {
e.preventDefault();
select(moves[e.key], true);
}
}
const inView = days.some((d) => stamp(d) === stamp(selected));
return (
<div className={cn("w-full", className)}>
<div className="flex items-center gap-1">
<p className="flex-1 text-sm font-semibold text-foreground" aria-live="polite">
{MONTHS[mid.getMonth()]} <span className="font-normal text-muted-foreground">{mid.getFullYear()}</span>
</p>
{stamp(monday(today)) !== stamp(start) && (
<button
type="button"
onClick={() => select(today)}
className="mr-1 h-7 rounded-lg px-2 text-xs font-medium text-primary transition-colors hover:bg-primary/10"
>
Today
</button>
)}
{[-1, 1].map((d) => (
<button
key={d}
type="button"
aria-label={d < 0 ? "Previous week" : "Next week"}
onClick={() => shift(d)}
className="flex size-7 items-center justify-center rounded-lg text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
>
{d < 0 ? <ChevronLeft className="size-4" /> : <ChevronRight className="size-4" />}
</button>
))}
</div>
<div ref={strip} role="radiogroup" aria-label="Day" onKeyDown={onKeyDown} className="relative mt-3 overflow-hidden rounded-2xl bg-muted p-1">
<AnimatePresence mode="popLayout" initial={false} custom={direction}>
<motion.div
key={stamp(start)}
data-week={stamp(start)}
custom={direction}
variants={{
enter: (d: number) => (reduce ? { opacity: 0 } : { x: `${d * 100}%`, opacity: 0 }),
center: { x: 0, opacity: 1 },
exit: (d: number) => (reduce ? { opacity: 0 } : { x: `${d * -100}%`, opacity: 0 }),
}}
initial="enter"
animate="center"
exit="exit"
transition={{ type: "spring", stiffness: 320, damping: 34 }}
className="grid grid-cols-7 gap-1"
>
{days.map((day, i) => {
const on = stamp(day) === stamp(selected);
const isToday = stamp(day) === stamp(today);
const dots = Math.min(3, markers?.(day) ?? 0);
return (
<button
key={stamp(day)}
type="button"
role="radio"
data-day={stamp(day)}
aria-checked={on}
aria-label={`${DAYS[day.getDay()]} ${MONTHS[day.getMonth()]} ${day.getDate()}${dots ? `, ${dots} events` : ""}`}
tabIndex={on || (!inView && i === 0) ? 0 : -1}
onClick={() => select(day)}
className="relative flex h-[76px] flex-col items-center justify-center gap-1 rounded-xl outline-offset-2"
>
{on && (
<motion.span
layoutId={`${uid}-pill-${stamp(start)}`}
transition={{ type: "spring", stiffness: 440, damping: 32 }}
className="absolute inset-0 rounded-xl bg-background shadow-sm ring-1 ring-border"
/>
)}
<span className={cn("relative text-[11px] font-medium uppercase", on ? "text-primary" : "text-muted-foreground")}>
{DAYS[day.getDay()]}
</span>
<span
className={cn(
"relative flex size-7 items-center justify-center rounded-full text-base tabular-nums",
on ? "font-semibold text-foreground" : "text-foreground/80",
isToday && "bg-primary font-semibold text-primary-foreground",
)}
>
{day.getDate()}
</span>
<span className="relative flex h-1 gap-0.5" aria-hidden>
{Array.from({ length: dots }, (_, n) => (
<span key={n} className={cn("size-1 rounded-full", on ? "bg-primary" : "bg-muted-foreground/50")} />
))}
</span>
</button>
);
})}
</motion.div>
</AnimatePresence>
</div>
</div>
);
}
const sessions = ["Morning swim", "Design critique", "Lunch with Noor", "Sprint planning", "Investor update", "Yoga", "1:1 with Jonas"];
const times = ["7:30", "10:00", "12:30", "14:00", "16:30"];
// Deterministic "events" derived from the date, so the demo needs no data.
const countFor = (d: Date) => (d.getDate() * 7 + d.getMonth() * 3) % 4;
export default function CalendarWeekStripDemo() {
const [day, setDay] = useState(new Date(2026, 2, 12));
const count = countFor(day);
return (
<div className="w-full max-w-md rounded-2xl border border-border bg-card p-5 shadow-sm">
<WeekStrip value={day} onChange={setDay} markers={countFor} />
<div className="mt-4 h-[132px] overflow-hidden">
<AnimatePresence mode="popLayout" initial={false}>
<motion.ul
key={stamp(day)}
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ type: "spring", stiffness: 400, damping: 34 }}
className="space-y-1.5"
>
{Array.from({ length: count }, (_, i) => (
<li key={i} className="flex h-10 items-center gap-3 rounded-xl bg-muted/60 px-3 text-sm">
<span className="w-10 text-xs font-medium text-muted-foreground tabular-nums">{times[(day.getDate() + i * 2) % 5]}</span>
<span className="h-4 w-0.5 rounded-full bg-primary" />
<span className="text-card-foreground">{sessions[(day.getDate() + i * 3) % sessions.length]}</span>
</li>
))}
{count === 0 && (
<li className="flex h-[132px] items-center justify-center rounded-xl border border-dashed border-border text-sm text-muted-foreground">
Nothing planned. Enjoy the quiet.
</li>
)}
</motion.ul>
</AnimatePresence>
</div>
</div>
);
}