Number 68: Weekly hours editor
Specification
- Catalogue
- No. 68 of 110
- Section
- Pickers and booking
- Interaction
- Press, Layout Animation
- Element
- Picker, Toggle
- Preview
- Live component
- Imports
- react 19.3.1, framer-motion 12.40.0
- Added
Notes
A week of opening or booking hours. Each day switches between its hours and Unavailable, ranges are added and removed with a short blur slide, and a copy menu puts one day's hours on any others.
Coaches, clinics and local businesses set the hours their booking page offers. This is the owner's side of the booking rather than the visitor's, which is why it comes last in this set.
The day's switch is the whole start of the row, a 44px target, and its thumb moves with a little overshoot. Ranges and the Unavailable line enter from 8px to the left out of a 4px blur and leave to the right over 240ms cubic-bezier(.16, 1, .3, 1), and the others close up on a layout move. Times are native selects in half-hour steps, so a phone shows its own picker; each select only offers times that keep the range the right way round. The copy menu is a small popover of checkboxes that closes on Escape or a press outside and returns focus to its button. Crossfades only under reduced motion.
The hours are sample content.
Code
React and plain CSS"use client";
import { useEffect, useId, useRef, useState } from "react";
import { AnimatePresence, motion } from "framer-motion";
import type { LivePreviewProps } from "@/components/live/types";
import { EASE_ENTER } from "@/components/pieces/kit/physical-spring";
import { useSafeReducedMotion } from "@/components/pieces/kit/use-safe-reduced-motion";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "@/components/pieces/kit/pop.css";
import "./weekly-hours.css";
type Range = { id: number; from: number; to: number };
type Day = { name: string; on: boolean; ranges: Range[] };
/** Half-hour steps from 6am to 10pm, in minutes. */
const TIMES = Array.from({ length: 33 }, (_, i) => 360 + i * 30);
const label = (m: number) => `${Math.floor(m / 60)}:${String(m % 60).padStart(2, "0")}`;
let nextId = 100;
const START: Day[] = [
{ name: "Monday", on: true, ranges: [{ id: 1, from: 540, to: 1020 }] },
{ name: "Tuesday", on: true, ranges: [{ id: 2, from: 540, to: 1020 }] },
{ name: "Wednesday", on: true, ranges: [{ id: 3, from: 540, to: 780 }] },
{ name: "Thursday", on: true, ranges: [{ id: 4, from: 600, to: 1140 }] },
{ name: "Friday", on: true, ranges: [{ id: 5, from: 540, to: 960 }] },
{ name: "Saturday", on: false, ranges: [{ id: 6, from: 600, to: 840 }] },
{ name: "Sunday", on: false, ranges: [{ id: 7, from: 600, to: 840 }] },
];
function CopyMenu({ from, days, onApply }: { from: number; days: Day[]; onApply: (to: number[]) => void }) {
const [open, setOpen] = useState(false);
const [picked, setPicked] = useState<number[]>([]);
const trigger = useRef<HTMLButtonElement>(null);
const wrap = useRef<HTMLDivElement>(null);
const uid = useId();
useEffect(() => {
if (!open) return;
const onDown = (e: PointerEvent) => {
if (e.target instanceof Node && !wrap.current?.contains(e.target)) setOpen(false);
};
document.addEventListener("pointerdown", onDown);
return () => document.removeEventListener("pointerdown", onDown);
}, [open]);
const close = () => {
setOpen(false);
trigger.current?.focus();
};
return (
<div
ref={wrap}
className="wh-copy"
onKeyDown={(e) => {
if (e.key === "Escape" && open) {
e.stopPropagation();
close();
}
}}
>
<button ref={trigger} type="button" className="wh-icon" aria-label={`Copy ${days[from]!.name}'s hours`} aria-expanded={open} aria-controls={`${uid}-m`} onClick={() => (open ? close() : (setPicked([]), setOpen(true)))}>
<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<rect x="9" y="9" width="11" height="11" rx="2" />
<path d="M5 15V6a2 2 0 0 1 2-2h8" />
</svg>
</button>
<div id={`${uid}-m`} className="wh-menu pop" data-up={from >= 3 || undefined} data-closed={!open || undefined} role="group" aria-label={`Copy ${days[from]!.name}'s hours to`}>
<p className="wh-menu-title">Copy to</p>
{days.map((d, i) =>
i === from ? null : (
<label key={d.name} className="wh-check">
<input type="checkbox" checked={picked.includes(i)} onChange={(e) => setPicked((p) => (e.target.checked ? [...p, i] : p.filter((x) => x !== i)))} />
{d.name}
</label>
),
)}
<button
type="button"
className="wh-apply"
disabled={!picked.length}
onClick={() => {
onApply(picked);
close();
}}
>
Apply
</button>
</div>
</div>
);
}
function WeeklyHoursDemo() {
const reduce = useSafeReducedMotion();
const [days, setDays] = useState<Day[]>(START);
const [said, setSaid] = useState("");
const slide = reduce ? { duration: 0 } : { duration: 0.24, ease: EASE_ENTER };
const update = (i: number, fn: (d: Day) => Day) => setDays((all) => all.map((d, k) => (k === i ? fn(d) : d)));
const add = (i: number) =>
update(i, (d) => {
const last = d.ranges[d.ranges.length - 1];
const from = Math.min(last ? last.to + 60 : 540, 1260);
return { ...d, on: true, ranges: [...d.ranges, { id: nextId++, from, to: Math.min(from + 120, 1320) }] };
});
const copy = (from: number, to: number[]) => {
const src = days[from]!;
setDays((all) => all.map((d, k) => (to.includes(k) ? { ...d, on: src.on, ranges: src.ranges.map((r) => ({ ...r, id: nextId++ })) } : d)));
setSaid(`${src.name}'s hours copied to ${to.length} ${to.length === 1 ? "day" : "days"}`);
};
return (
<div className="wh">
<p className="wh-title">Booking hours</p>
<ul className="wh-days">
{days.map((d, i) => (
<li key={d.name} className="wh-day" data-on={d.on || undefined}>
<button type="button" role="switch" aria-checked={d.on} className="wh-switch" onClick={() => update(i, (x) => ({ ...x, on: !x.on }))}>
<span className="wh-track" aria-hidden="true">
<span className="wh-thumb" />
</span>
<span className="wh-name">{d.name.slice(0, 3)}</span>
<span className="sr-only">{d.name}</span>
</button>
<div className="wh-ranges">
<AnimatePresence initial={false} mode="popLayout">
{!d.on ? (
<motion.p key="off" className="wh-off" initial={{ opacity: 0, x: -8, filter: "blur(4px)" }} animate={{ opacity: 1, x: 0, filter: "blur(0px)" }} exit={{ opacity: 0, x: 8, filter: "blur(4px)" }} transition={slide}>
Unavailable
</motion.p>
) : (
d.ranges.map((r, k) => (
<motion.div key={r.id} layout={reduce ? false : "position"} className="wh-range" initial={{ opacity: 0, x: -8, filter: "blur(4px)" }} animate={{ opacity: 1, x: 0, filter: "blur(0px)" }} exit={{ opacity: 0, x: 8, filter: "blur(4px)" }} transition={slide}>
<select aria-label={`${d.name}, range ${k + 1}, from`} value={r.from} onChange={(e) => update(i, (x) => ({ ...x, ranges: x.ranges.map((y) => (y.id === r.id ? { ...y, from: Number(e.target.value) } : y)) }))}>
{TIMES.filter((t) => t < r.to).map((t) => (
<option key={t} value={t}>
{label(t)}
</option>
))}
</select>
<span className="wh-to" aria-hidden="true">
to
</span>
<select aria-label={`${d.name}, range ${k + 1}, to`} value={r.to} onChange={(e) => update(i, (x) => ({ ...x, ranges: x.ranges.map((y) => (y.id === r.id ? { ...y, to: Number(e.target.value) } : y)) }))}>
{TIMES.filter((t) => t > r.from).map((t) => (
<option key={t} value={t}>
{label(t)}
</option>
))}
</select>
{d.ranges.length > 1 && (
<button type="button" className="wh-icon" aria-label={`Remove ${label(r.from)} to ${label(r.to)} on ${d.name}`} onClick={() => update(i, (x) => ({ ...x, ranges: x.ranges.filter((y) => y.id !== r.id) }))}>
<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
<path d="M7 7l10 10M17 7 7 17" />
</svg>
</button>
)}
</motion.div>
))
)}
</AnimatePresence>
</div>
<div className="wh-tools">
<button type="button" className="wh-icon wh-add" aria-label={`Add hours on ${d.name}`} onClick={() => add(i)} disabled={d.on && d.ranges.length >= 3}>
<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
<path d="M12 5v14M5 12h14" />
</svg>
</button>
<CopyMenu from={i} days={days} onApply={(to) => copy(i, to)} />
</div>
</li>
))}
</ul>
<p className="sr-only" role="status">
{said}
</p>
</div>
);
}
// What a hovered card shows: Tuesday switched off, then a second range added to Monday.
const TOUR: Tour = [
{ click: ".wh-switch", nth: 1, wait: 900 },
{ click: ".wh-add", nth: 0, wait: 1100 },
];
export default function WeeklyHoursEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Weekly hours editor" tour={TOUR}>
<WeeklyHoursDemo />
</PieceStage>
);
}