@dinachi/cli 1.1.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +81 -0
- package/README.md +1 -0
- package/dist/index.js +698 -314
- package/package.json +5 -1
- package/templates/agent/agent.tsx +35 -18
- package/templates/alert/alert.tsx +345 -0
- package/templates/alert/index.ts +16 -0
- package/templates/ask-bar/ask-bar.tsx +1061 -0
- package/templates/ask-bar/index.ts +1 -0
- package/templates/autocomplete/autocomplete.tsx +0 -1
- package/templates/breadcrumb/breadcrumb.tsx +606 -0
- package/templates/breadcrumb/index.ts +9 -0
- package/templates/calendar/calendar.tsx +1155 -0
- package/templates/calendar/index.ts +15 -0
- package/templates/combobox/combobox.tsx +0 -1
- package/templates/command-items/command-items.tsx +69 -0
- package/templates/command-items/index.ts +1 -0
- package/templates/command-palette/command-palette.tsx +954 -0
- package/templates/command-palette/index.ts +1 -0
- package/templates/comparison-table/comparison-table.tsx +540 -0
- package/templates/comparison-table/index.ts +15 -0
- package/templates/data-table/data-table.tsx +2077 -0
- package/templates/data-table/index.ts +14 -0
- package/templates/date-picker/date-picker.tsx +527 -0
- package/templates/date-picker/index.ts +2 -0
- package/templates/date-range-picker/date-range-picker.tsx +504 -0
- package/templates/date-range-picker/index.ts +2 -0
- package/templates/detail-list/detail-list.tsx +419 -0
- package/templates/detail-list/index.ts +12 -0
- package/templates/heat-table/heat-table.tsx +613 -0
- package/templates/heat-table/index.ts +9 -0
- package/templates/menu/menu.tsx +8 -3
- package/templates/month-picker/index.ts +1 -0
- package/templates/month-picker/month-picker.tsx +351 -0
- package/templates/omnibar/index.ts +1 -0
- package/templates/omnibar/omnibar.tsx +1009 -0
- package/templates/pagination/index.ts +8 -0
- package/templates/pagination/pagination.tsx +389 -0
- package/templates/table/index.ts +13 -0
- package/templates/table/table.tsx +265 -0
- package/templates/time-slots/index.ts +1 -0
- package/templates/time-slots/time-slots.tsx +589 -0
- package/templates/week-strip/index.ts +1 -0
- package/templates/week-strip/week-strip.tsx +329 -0
- package/templates/year-view/index.ts +1 -0
- package/templates/year-view/year-view.tsx +362 -0
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { format, type Locale } from "date-fns";
|
|
5
|
+
import { cn } from "@/lib/utils";
|
|
6
|
+
import { changeAsAgent, useAgentControl, useAgentState, useHasAgent, useMergedRef } from "@/components/ui/agent";
|
|
7
|
+
|
|
8
|
+
/** Minutes since midnight, end exclusive. 10:45 to 12:00 is `{ start: 645, end: 720 }`. */
|
|
9
|
+
export interface TimeSlot {
|
|
10
|
+
start: number;
|
|
11
|
+
end: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface TimeSlotsChangeDetails {
|
|
15
|
+
reason: string;
|
|
16
|
+
event?: Event;
|
|
17
|
+
isCanceled: boolean;
|
|
18
|
+
cancel(): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function changeDetails(reason: string, event?: Event): TimeSlotsChangeDetails {
|
|
22
|
+
const details: TimeSlotsChangeDetails = {
|
|
23
|
+
reason,
|
|
24
|
+
event,
|
|
25
|
+
isCanceled: false,
|
|
26
|
+
cancel() {
|
|
27
|
+
details.isCanceled = true;
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
return details;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const WIDE = "(min-width: 768px)";
|
|
34
|
+
|
|
35
|
+
/** `horizontal` from 768px up, `vertical` below, unless one is asked for. */
|
|
36
|
+
function useLayout(layout: "auto" | "horizontal" | "vertical"): "horizontal" | "vertical" {
|
|
37
|
+
const wide = React.useSyncExternalStore(
|
|
38
|
+
(onChange) => {
|
|
39
|
+
const query = window.matchMedia?.(WIDE);
|
|
40
|
+
query?.addEventListener("change", onChange);
|
|
41
|
+
return () => query?.removeEventListener("change", onChange);
|
|
42
|
+
},
|
|
43
|
+
() => window.matchMedia?.(WIDE).matches ?? true,
|
|
44
|
+
() => true
|
|
45
|
+
);
|
|
46
|
+
return layout === "auto" ? (wide ? "horizontal" : "vertical") : layout;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const pad = (n: number) => String(n).padStart(2, "0");
|
|
50
|
+
/** 24-hour HH:MM, the form an agent reads and sends. */
|
|
51
|
+
const hhmm = (m: number) => `${pad(Math.floor(m / 60))}:${pad(m % 60)}`;
|
|
52
|
+
const length = (n: number) => (n < 60 ? `${n} min` : `${Math.floor(n / 60)} h${n % 60 ? ` ${n % 60} min` : ""}`);
|
|
53
|
+
|
|
54
|
+
/** Midnight reads 00:00 at either end of the day. */
|
|
55
|
+
const clock = (m: number) => hhmm(m % (24 * 60));
|
|
56
|
+
|
|
57
|
+
/** The calendar day and minutes since midnight `instant` falls on in `timeZone`, or locally if it isn't a zone. */
|
|
58
|
+
function wallClock(instant: Date, timeZone: string): { day: string; minutes: number } {
|
|
59
|
+
let parts: Intl.DateTimeFormatPart[] | null = null;
|
|
60
|
+
try {
|
|
61
|
+
parts = new Intl.DateTimeFormat("en-CA", { timeZone, year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", hourCycle: "h23" }).formatToParts(instant);
|
|
62
|
+
} catch {
|
|
63
|
+
// Not an IANA zone, such as "UK time": the device's own clock stands in.
|
|
64
|
+
}
|
|
65
|
+
if (!parts) return { day: `${instant.getFullYear()}-${pad(instant.getMonth() + 1)}-${pad(instant.getDate())}`, minutes: instant.getHours() * 60 + instant.getMinutes() };
|
|
66
|
+
const get = (type: string) => parts!.find((p) => p.type === type)?.value ?? "0";
|
|
67
|
+
return { day: `${get("year")}-${get("month")}-${get("day")}`, minutes: +get("hour") * 60 + +get("minute") };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** "10:45", "9", "2pm" or minutes, as minutes since midnight. */
|
|
71
|
+
function readTime(t: unknown): number | null {
|
|
72
|
+
if (typeof t === "number") return Number.isInteger(t) ? t : null;
|
|
73
|
+
const m = /^(\d{1,2})(?::(\d{2}))?\s*(?:([ap])\.?m\.?)?$/i.exec(String(t).trim());
|
|
74
|
+
if (!m) return null;
|
|
75
|
+
const [h, min] = [+m[1], +(m[2] ?? 0)];
|
|
76
|
+
if (min > 59 || (m[3] ? h < 1 || h > 12 : h > 24 || (h === 24 && min))) return null;
|
|
77
|
+
return ((m[3] ? h % 12 : h) + (m[3]?.toLowerCase() === "p" ? 12 : 0)) * 60 + min;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Hatched, so time that can't be booked reads at 3:1 without leaning on a fill.
|
|
81
|
+
const HATCH = "repeating-linear-gradient(135deg, color-mix(in srgb, var(--foreground) 55%, var(--background)) 0 1.5px, transparent 1.5px 6px)";
|
|
82
|
+
const BUSY_FILL = "color-mix(in srgb, var(--foreground) 8%, var(--background))";
|
|
83
|
+
const BUSY = `${HATCH}, ${BUSY_FILL}`;
|
|
84
|
+
/** Rows are 44px. A busy run spans several, so each pins the hatch to the list's origin and the stripes run on. */
|
|
85
|
+
const ROW_PX = 44;
|
|
86
|
+
const PAST = "color-mix(in srgb, var(--foreground) 4%, transparent)";
|
|
87
|
+
|
|
88
|
+
export interface TimeSlotsProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "defaultValue" | "onChange" | "dir"> {
|
|
89
|
+
/** The booking, in minutes since midnight. */
|
|
90
|
+
value?: TimeSlot | null;
|
|
91
|
+
defaultValue?: TimeSlot | null;
|
|
92
|
+
onValueChange?: (value: TimeSlot | null, details: TimeSlotsChangeDetails) => void;
|
|
93
|
+
/**
|
|
94
|
+
* Named beside the booking. Times are shown as given. When it is an IANA zone such as
|
|
95
|
+
* `Europe/London`, a `now` given as a Date is read in it.
|
|
96
|
+
*/
|
|
97
|
+
timeZone: string;
|
|
98
|
+
/** `range` books a span between two handles. `single` picks one time, held for `minDuration`. */
|
|
99
|
+
mode?: "range" | "single";
|
|
100
|
+
/** Where the day starts, in minutes. Defaults to 08:00. */
|
|
101
|
+
min?: number;
|
|
102
|
+
/** Where the day ends, in minutes. Defaults to 20:00. */
|
|
103
|
+
max?: number;
|
|
104
|
+
/** Minutes a booking snaps to. Defaults to 15. */
|
|
105
|
+
step?: number;
|
|
106
|
+
/** Taken time. A booking can't cross it. */
|
|
107
|
+
busy?: TimeSlot[];
|
|
108
|
+
/**
|
|
109
|
+
* The time now. Time before it is shaded and can't be booked. Minutes since midnight, or a
|
|
110
|
+
* Date read in `timeZone` and against `date`: a later day has no past, an earlier one is all past.
|
|
111
|
+
*/
|
|
112
|
+
now?: number | Date;
|
|
113
|
+
/** Defaults to one step. */
|
|
114
|
+
minDuration?: number;
|
|
115
|
+
maxDuration?: number;
|
|
116
|
+
/** The day being booked, named above the track. */
|
|
117
|
+
date?: Date;
|
|
118
|
+
/** How a time reads. Defaults to 24-hour HH:MM, with midnight as 00:00. */
|
|
119
|
+
formatTime?: (minutes: number) => string;
|
|
120
|
+
/** `horizontal`: a track to drag across. `vertical`: half-hour rows to tap. `auto` switches at 768px. */
|
|
121
|
+
layout?: "auto" | "horizontal" | "vertical";
|
|
122
|
+
/** A date-fns locale, for the day's name. */
|
|
123
|
+
locale?: Locale;
|
|
124
|
+
dir?: "ltr" | "rtl";
|
|
125
|
+
disabled?: boolean;
|
|
126
|
+
/** Submits the booking as HH:MM/HH:MM. */
|
|
127
|
+
name?: string;
|
|
128
|
+
required?: boolean;
|
|
129
|
+
/** Sits beside the readout, such as a Book button. */
|
|
130
|
+
children?: React.ReactNode;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A day as a track. Drag across free time to book it, drag either end to resize it. Busy
|
|
135
|
+
* time and time already past stop the drag. Below 768px the day turns into rows to tap.
|
|
136
|
+
*/
|
|
137
|
+
const TimeSlots = React.forwardRef<HTMLDivElement, TimeSlotsProps>((props, ref) => {
|
|
138
|
+
const {
|
|
139
|
+
value: valueProp,
|
|
140
|
+
defaultValue,
|
|
141
|
+
onValueChange,
|
|
142
|
+
timeZone,
|
|
143
|
+
mode = "range",
|
|
144
|
+
min = 8 * 60,
|
|
145
|
+
max = 20 * 60,
|
|
146
|
+
step: stepProp = 15,
|
|
147
|
+
busy = [],
|
|
148
|
+
now: nowProp,
|
|
149
|
+
minDuration = 0,
|
|
150
|
+
maxDuration = max - min,
|
|
151
|
+
date,
|
|
152
|
+
formatTime = clock,
|
|
153
|
+
layout: layoutProp = "auto",
|
|
154
|
+
locale,
|
|
155
|
+
dir,
|
|
156
|
+
disabled,
|
|
157
|
+
name,
|
|
158
|
+
required,
|
|
159
|
+
children,
|
|
160
|
+
className,
|
|
161
|
+
"aria-label": ariaLabel = "Booking",
|
|
162
|
+
...rest
|
|
163
|
+
} = props;
|
|
164
|
+
const hasAgent = useHasAgent();
|
|
165
|
+
const layout = useLayout(layoutProp);
|
|
166
|
+
const [value, change] = useAgentState<TimeSlot | null, TimeSlotsChangeDetails>(valueProp, defaultValue ?? null, onValueChange);
|
|
167
|
+
const root = React.useRef<HTMLDivElement>(null);
|
|
168
|
+
const track = React.useRef<HTMLDivElement>(null);
|
|
169
|
+
const list = React.useRef<HTMLDivElement>(null);
|
|
170
|
+
const describedId = React.useId();
|
|
171
|
+
const single = mode === "single";
|
|
172
|
+
|
|
173
|
+
const now = ((): number | undefined => {
|
|
174
|
+
if (!(nowProp instanceof Date)) return nowProp;
|
|
175
|
+
const wall = wallClock(nowProp, timeZone);
|
|
176
|
+
const day = date && `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
|
|
177
|
+
return !day || day === wall.day ? wall.minutes : day < wall.day ? 24 * 60 : undefined;
|
|
178
|
+
})();
|
|
179
|
+
|
|
180
|
+
const span = max - min;
|
|
181
|
+
// Everything a booking can touch sits on the step grid, so the track and an agent agree.
|
|
182
|
+
const step = Math.max(1, Math.round(stepProp));
|
|
183
|
+
const snapDown = (m: number) => min + Math.floor((m - min) / step) * step;
|
|
184
|
+
const snapUp = (m: number) => min + Math.ceil((m - min) / step) * step;
|
|
185
|
+
const top = snapDown(max);
|
|
186
|
+
const minDur = Math.max(step, snapUp(min + minDuration) - min);
|
|
187
|
+
const maxDur = single ? minDur : Math.max(minDur, snapDown(min + maxDuration) - min);
|
|
188
|
+
const blocks: [number, number][] = busy.map((b) => [snapDown(b.start), snapUp(b.end)]);
|
|
189
|
+
if (now !== undefined && now > min) blocks.push([min, snapUp(now)]);
|
|
190
|
+
const fits = (a: number, b: number) =>
|
|
191
|
+
a >= min && b <= top && b - a >= minDur && b - a <= maxDur && !blocks.some(([x, y]) => a < y && b > x);
|
|
192
|
+
/** The free stretch around a minute. */
|
|
193
|
+
const gap = (at: number): [number, number] => {
|
|
194
|
+
let lo = min;
|
|
195
|
+
let hi = top;
|
|
196
|
+
for (const [x, y] of blocks) {
|
|
197
|
+
if (y <= at) lo = Math.max(lo, y);
|
|
198
|
+
if (x > at) hi = Math.min(hi, x);
|
|
199
|
+
}
|
|
200
|
+
return [lo, hi];
|
|
201
|
+
};
|
|
202
|
+
/** The nearest start, from `from` in `dir`, where `len` fits. */
|
|
203
|
+
const seek = (from: number, len: number, way: number) => {
|
|
204
|
+
for (let s = from; s >= min && s + len <= top; s += way * step) if (fits(s, s + len)) return s;
|
|
205
|
+
return null;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const set = (next: TimeSlot | null, reason: string, event?: Event) => {
|
|
209
|
+
if (disabled || (next ? next.start === value?.start && next.end === value?.end : !value)) return;
|
|
210
|
+
change(next, changeDetails(reason, event));
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const read = !value ? "" : single ? `${formatTime(value.start)} ${timeZone}` : `${formatTime(value.start)} to ${formatTime(value.end)} ${timeZone}`;
|
|
214
|
+
const pct = (m: number) => `${((m - min) / span) * 100}%`;
|
|
215
|
+
|
|
216
|
+
const isRtl = () => root.current?.closest("[dir]")?.getAttribute("dir") === "rtl";
|
|
217
|
+
|
|
218
|
+
/* ---- horizontal: drag ---- */
|
|
219
|
+
const drag = React.useRef<{ anchor: number; lo: number; hi: number } | null>(null);
|
|
220
|
+
const at = (e: React.PointerEvent) => {
|
|
221
|
+
const r = track.current!.getBoundingClientRect();
|
|
222
|
+
const rtl = isRtl();
|
|
223
|
+
const x = (rtl ? r.right - e.clientX : e.clientX - r.left) / r.width;
|
|
224
|
+
return Math.max(min, Math.min(top, min + Math.round((x * span) / step) * step));
|
|
225
|
+
};
|
|
226
|
+
const onPointerDown = (e: React.PointerEvent) => {
|
|
227
|
+
if (disabled || e.button !== 0) return;
|
|
228
|
+
const edge = (e.target as Element).closest("[data-edge]")?.getAttribute("data-edge");
|
|
229
|
+
if (value && edge) {
|
|
230
|
+
const anchor = edge === "start" ? value.end : value.start;
|
|
231
|
+
const [lo, hi] = gap(edge === "start" ? anchor - 1 : anchor);
|
|
232
|
+
drag.current = { anchor, lo, hi };
|
|
233
|
+
} else {
|
|
234
|
+
const m = at(e);
|
|
235
|
+
// Near the end of a free stretch, the booking runs back from the press instead.
|
|
236
|
+
const back = !fits(m, m + minDur);
|
|
237
|
+
if (back && !fits(m - minDur, m)) return;
|
|
238
|
+
const [lo, hi] = gap(back ? m - 1 : m);
|
|
239
|
+
drag.current = { anchor: m, lo, hi };
|
|
240
|
+
set(back ? { start: m - minDur, end: m } : { start: m, end: m + minDur }, "drag", e.nativeEvent);
|
|
241
|
+
}
|
|
242
|
+
e.currentTarget.setPointerCapture?.(e.pointerId);
|
|
243
|
+
(e.currentTarget as HTMLElement).focus();
|
|
244
|
+
e.preventDefault();
|
|
245
|
+
};
|
|
246
|
+
const onPointerMove = (e: React.PointerEvent) => {
|
|
247
|
+
const d = drag.current;
|
|
248
|
+
if (!d) return;
|
|
249
|
+
if (single) {
|
|
250
|
+
const s = at(e);
|
|
251
|
+
if (fits(s, s + minDur)) set({ start: s, end: s + minDur }, "drag", e.nativeEvent);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const m = Math.max(d.lo, Math.min(d.hi, at(e)));
|
|
255
|
+
const len = Math.min(maxDur, Math.max(minDur, Math.abs(m - d.anchor)));
|
|
256
|
+
const next = m >= d.anchor ? { start: d.anchor, end: Math.min(d.hi, d.anchor + len) } : { start: Math.max(d.lo, d.anchor - len), end: d.anchor };
|
|
257
|
+
if (fits(next.start, next.end)) set(next, "drag", e.nativeEvent);
|
|
258
|
+
};
|
|
259
|
+
const endDrag = () => (drag.current = null);
|
|
260
|
+
|
|
261
|
+
/* ---- horizontal: keys ---- */
|
|
262
|
+
const onKeyDown = (e: React.KeyboardEvent) => {
|
|
263
|
+
if (disabled) return;
|
|
264
|
+
const rtl = isRtl();
|
|
265
|
+
const hour = Math.max(step, Math.round(60 / step) * step);
|
|
266
|
+
const n = { ArrowLeft: rtl ? step : -step, ArrowRight: rtl ? -step : step, ArrowDown: -step, ArrowUp: step, PageDown: -hour, PageUp: hour }[e.key];
|
|
267
|
+
// A value set from outside may be off the grid; the keys bring it back on.
|
|
268
|
+
const base = value && snapDown(value.start + step / 2);
|
|
269
|
+
const len = value ? Math.min(maxDur, Math.max(minDur, Math.round((value.end - value.start) / step) * step)) : minDur;
|
|
270
|
+
let start: number | null | undefined;
|
|
271
|
+
if (e.key === "Delete" || e.key === "Backspace") {
|
|
272
|
+
e.preventDefault();
|
|
273
|
+
return set(null, "keyboard", e.nativeEvent);
|
|
274
|
+
}
|
|
275
|
+
if (e.key === "Home") start = seek(min, len, 1);
|
|
276
|
+
else if (e.key === "End") start = seek(snapDown(top - len), len, -1);
|
|
277
|
+
else if (n === undefined) return;
|
|
278
|
+
else if (base === null) start = seek(min, len, 1);
|
|
279
|
+
else if (e.shiftKey && Math.abs(n) === step) {
|
|
280
|
+
e.preventDefault();
|
|
281
|
+
const end = base + len + n;
|
|
282
|
+
return fits(base, end) && set({ start: base, end }, "keyboard", e.nativeEvent);
|
|
283
|
+
} else start = seek(base + n, len, Math.sign(n));
|
|
284
|
+
e.preventDefault();
|
|
285
|
+
if (start != null) set({ start, end: start + len }, "keyboard", e.nativeEvent);
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/* ---- vertical: rows ---- */
|
|
289
|
+
const row = Math.ceil(30 / step) * step;
|
|
290
|
+
const rows: number[] = [];
|
|
291
|
+
for (let m = min; m < top; m += row) rows.push(m);
|
|
292
|
+
/** What tapping a row books: the row, stretched or cut to the allowed length. */
|
|
293
|
+
const rowEnd = (m: number) => Math.min(top, m + Math.min(maxDur, Math.max(row, minDur)));
|
|
294
|
+
const busyAt = (m: number) => busy.find((b) => m < b.end && m + row > b.start);
|
|
295
|
+
const pastAt = (m: number) => now !== undefined && m < now;
|
|
296
|
+
const onAt = (m: number) => !!value && m < value.end && m + row > value.start;
|
|
297
|
+
const press = (m: number, e: React.MouseEvent) => {
|
|
298
|
+
// A booking no longer than one tap makes is cleared by tapping it.
|
|
299
|
+
if (onAt(m) && value!.end - value!.start <= Math.max(row, minDur)) return set(null, "row-press", e.nativeEvent);
|
|
300
|
+
// A later row stretches the booking to it, if nothing is in the way.
|
|
301
|
+
if (value && m >= value.end && fits(value.start, Math.min(top, m + row))) {
|
|
302
|
+
return set({ start: value.start, end: Math.min(top, m + row) }, "row-press", e.nativeEvent);
|
|
303
|
+
}
|
|
304
|
+
if (fits(m, rowEnd(m))) set({ start: m, end: rowEnd(m) }, "row-press", e.nativeEvent);
|
|
305
|
+
};
|
|
306
|
+
const free = rows.filter((m) => fits(m, rowEnd(m)));
|
|
307
|
+
const rowStop = (value && rows.find((m) => m + row > value.start)) ?? free[0] ?? rows[0];
|
|
308
|
+
const onRowKey = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
|
309
|
+
const n = { ArrowUp: -1, ArrowDown: 1, Home: -rows.length, End: rows.length }[e.key];
|
|
310
|
+
if (n === undefined) return;
|
|
311
|
+
e.preventDefault();
|
|
312
|
+
const all = [...e.currentTarget.querySelectorAll<HTMLElement>("[data-slot=time-slots-row] button")];
|
|
313
|
+
const i = all.indexOf(document.activeElement as HTMLElement);
|
|
314
|
+
all[Math.max(0, Math.min(all.length - 1, i + n))]?.focus();
|
|
315
|
+
};
|
|
316
|
+
// Opens on the booking, or on now.
|
|
317
|
+
React.useLayoutEffect(() => {
|
|
318
|
+
const at = value?.start ?? now ?? min;
|
|
319
|
+
const el = list.current?.querySelector<HTMLElement>(`[data-minute="${rows.find((m) => m + row > at) ?? min}"]`)?.parentElement?.parentElement;
|
|
320
|
+
if (list.current && el) list.current.scrollTop = el.offsetTop - list.current.offsetTop;
|
|
321
|
+
}, [layout]);
|
|
322
|
+
|
|
323
|
+
/* ---- agent ---- */
|
|
324
|
+
useAgentControl(() => ({
|
|
325
|
+
kind: "slider",
|
|
326
|
+
element: root.current,
|
|
327
|
+
value: value ? (single ? hhmm(value.start) : [hhmm(value.start), hhmm(value.end)]) : undefined,
|
|
328
|
+
disabled,
|
|
329
|
+
actions: {
|
|
330
|
+
setValue: (wanted: unknown) => {
|
|
331
|
+
if (wanted === null || wanted === "") return changeAsAgent((_: null, d: TimeSlotsChangeDetails) => value && change(null, d), null);
|
|
332
|
+
const pair =
|
|
333
|
+
wanted && typeof wanted === "object" && !Array.isArray(wanted)
|
|
334
|
+
? [(wanted as TimeSlot).start, (wanted as TimeSlot).end]
|
|
335
|
+
: Array.isArray(wanted) ? [...wanted] : String(wanted).split(/\s*(?:to|until|-|–|\/)\s*/i);
|
|
336
|
+
// One time holds the booking's length from it.
|
|
337
|
+
if (single && pair.length === 1) {
|
|
338
|
+
const t = readTime(pair[0]);
|
|
339
|
+
if (t != null) pair.push(t + minDur);
|
|
340
|
+
}
|
|
341
|
+
// "2 to 3pm": the start takes the end's am or pm.
|
|
342
|
+
const mer = /[ap]\.?m\.?$/i.exec(String(pair[1]))?.[0];
|
|
343
|
+
if (mer && typeof pair[0] === "string" && !/[ap]/i.test(pair[0])) pair[0] += mer;
|
|
344
|
+
const [a, b] = pair.map(readTime);
|
|
345
|
+
if (pair.length !== 2 || a == null || b == null || b <= a)
|
|
346
|
+
return single ? `${JSON.stringify(wanted)} is not a time. Send "10:45".` : `${JSON.stringify(wanted)} is not a span of time. Send ["10:45", "12:00"].`;
|
|
347
|
+
if (a < min || b > top) return `The day runs ${hhmm(min)} to ${hhmm(top)}.`;
|
|
348
|
+
if ((a - min) % step || (b - min) % step) return `Times go in steps of ${step} min.`;
|
|
349
|
+
if (now !== undefined && a < now) return `${hhmm(a)} has passed. It is ${hhmm(now)}.`;
|
|
350
|
+
const hit = busy.find((x) => a < x.end && b > x.start);
|
|
351
|
+
if (hit) return `${hhmm(hit.start)} to ${hhmm(hit.end)} is busy.`;
|
|
352
|
+
if (!fits(a, b)) return `A booking runs ${length(minDur)} to ${length(maxDur)}.`;
|
|
353
|
+
return changeAsAgent((next: TimeSlot, d: TimeSlotsChangeDetails) => {
|
|
354
|
+
if (next.start !== value?.start || next.end !== value?.end) change(next, d);
|
|
355
|
+
}, { start: a, end: b });
|
|
356
|
+
},
|
|
357
|
+
},
|
|
358
|
+
}));
|
|
359
|
+
|
|
360
|
+
/* ---- render ---- */
|
|
361
|
+
const hours: number[] = [];
|
|
362
|
+
for (let h = Math.ceil((min + 1) / 60) * 60; h < max; h += 60) hours.push(h);
|
|
363
|
+
const every = span > 6 * 60 ? 120 : 60;
|
|
364
|
+
const ticks: number[] = [];
|
|
365
|
+
for (let t = Math.ceil(min / every) * every; t <= max; t += every) ticks.push(t);
|
|
366
|
+
const pastShown = now !== undefined && now > min;
|
|
367
|
+
const nowShown = pastShown && now < max;
|
|
368
|
+
const shownBusy = busy.filter((b) => b.end > min && b.start < max);
|
|
369
|
+
// The track draws busy and past time; this says it, since arrows skip it without a sound.
|
|
370
|
+
const taken = [
|
|
371
|
+
pastShown && `Past until ${formatTime(Math.min(now, max))}.`,
|
|
372
|
+
shownBusy.length > 0 && `Busy ${shownBusy.map((b) => `${formatTime(Math.max(min, b.start))} to ${formatTime(Math.min(max, b.end))}`).join(", ")}.`,
|
|
373
|
+
].filter(Boolean).join(" ");
|
|
374
|
+
|
|
375
|
+
return (
|
|
376
|
+
<div
|
|
377
|
+
{...rest}
|
|
378
|
+
ref={useMergedRef(ref, root)}
|
|
379
|
+
role="group"
|
|
380
|
+
aria-label={ariaLabel}
|
|
381
|
+
aria-disabled={disabled || undefined}
|
|
382
|
+
dir={dir}
|
|
383
|
+
data-slot="time-slots"
|
|
384
|
+
data-layout={layout}
|
|
385
|
+
className={cn("relative flex w-full flex-col", layout === "vertical" && "max-h-[28rem]", disabled && "opacity-50", className)}
|
|
386
|
+
>
|
|
387
|
+
{date && (
|
|
388
|
+
<div data-slot="time-slots-heading" className="mb-3.5 text-[0.9375rem] font-semibold">
|
|
389
|
+
{format(date, "EEEE d MMMM", { locale })}
|
|
390
|
+
</div>
|
|
391
|
+
)}
|
|
392
|
+
{layout === "horizontal" ? (
|
|
393
|
+
<>
|
|
394
|
+
<div
|
|
395
|
+
ref={track}
|
|
396
|
+
role="slider"
|
|
397
|
+
tabIndex={disabled ? -1 : 0}
|
|
398
|
+
aria-label={single ? "Time" : "Time span"}
|
|
399
|
+
aria-describedby={taken ? describedId : undefined}
|
|
400
|
+
aria-orientation="horizontal"
|
|
401
|
+
aria-valuemin={min}
|
|
402
|
+
aria-valuemax={max}
|
|
403
|
+
aria-valuenow={value?.start ?? min}
|
|
404
|
+
aria-valuetext={value ? (single ? read : `${read}, ${length(value.end - value.start)}`) : "No time picked"}
|
|
405
|
+
aria-disabled={disabled || undefined}
|
|
406
|
+
data-slot="time-slots-track"
|
|
407
|
+
data-agent-part={hasAgent ? "" : undefined}
|
|
408
|
+
onPointerDown={onPointerDown}
|
|
409
|
+
onPointerMove={onPointerMove}
|
|
410
|
+
onPointerUp={endDrag}
|
|
411
|
+
onPointerCancel={endDrag}
|
|
412
|
+
onKeyDown={onKeyDown}
|
|
413
|
+
className={cn(
|
|
414
|
+
"relative h-16 cursor-crosshair touch-none select-none rounded-[0.625rem] border bg-background",
|
|
415
|
+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
|
|
416
|
+
disabled && "cursor-not-allowed"
|
|
417
|
+
)}
|
|
418
|
+
>
|
|
419
|
+
{hours.map((h) => (
|
|
420
|
+
<span key={h} aria-hidden className="absolute inset-y-0 w-px bg-border" style={{ insetInlineStart: pct(h) }} />
|
|
421
|
+
))}
|
|
422
|
+
{pastShown && (
|
|
423
|
+
<span
|
|
424
|
+
aria-hidden
|
|
425
|
+
data-slot="time-slots-past"
|
|
426
|
+
className="absolute inset-y-0 start-0 cursor-not-allowed rounded-s-[0.5625rem]"
|
|
427
|
+
style={{ width: pct(Math.min(now, max)), background: PAST }}
|
|
428
|
+
/>
|
|
429
|
+
)}
|
|
430
|
+
{shownBusy.map((b, i) => (
|
|
431
|
+
<span
|
|
432
|
+
key={i}
|
|
433
|
+
aria-hidden
|
|
434
|
+
data-slot="time-slots-busy"
|
|
435
|
+
className="absolute inset-y-1.5 cursor-not-allowed rounded-md"
|
|
436
|
+
style={{ insetInlineStart: pct(Math.max(min, b.start)), width: pct(min + Math.min(max, b.end) - Math.max(min, b.start)), background: BUSY }}
|
|
437
|
+
/>
|
|
438
|
+
))}
|
|
439
|
+
{nowShown && (
|
|
440
|
+
<span aria-hidden data-slot="time-slots-now" className="absolute -inset-y-1 w-0.5 rounded-sm bg-destructive" style={{ insetInlineStart: pct(now) }} />
|
|
441
|
+
)}
|
|
442
|
+
{taken && (
|
|
443
|
+
<span id={describedId} hidden>
|
|
444
|
+
{taken}
|
|
445
|
+
</span>
|
|
446
|
+
)}
|
|
447
|
+
{value && (
|
|
448
|
+
<span
|
|
449
|
+
aria-hidden
|
|
450
|
+
data-slot="time-slots-pick"
|
|
451
|
+
className="absolute inset-y-1 rounded-lg bg-primary transition-[inset-inline-start,width] duration-[60ms] ease-linear motion-reduce:transition-none"
|
|
452
|
+
style={{ insetInlineStart: pct(value.start), width: pct(min + value.end - value.start) }}
|
|
453
|
+
>
|
|
454
|
+
{!single && (["start", "end"] as const).map((edge) => (
|
|
455
|
+
<span
|
|
456
|
+
key={edge}
|
|
457
|
+
data-edge={edge}
|
|
458
|
+
data-slot="time-slots-handle"
|
|
459
|
+
// Overhanging the ends, so a short booking keeps both handles.
|
|
460
|
+
className={cn("absolute inset-y-0 flex w-3 cursor-ew-resize items-center", edge === "start" ? "-start-1 justify-end" : "-end-1 justify-start")}
|
|
461
|
+
>
|
|
462
|
+
<span className="h-4 w-[3px] rounded-sm bg-primary-foreground/60" />
|
|
463
|
+
</span>
|
|
464
|
+
))}
|
|
465
|
+
</span>
|
|
466
|
+
)}
|
|
467
|
+
</div>
|
|
468
|
+
<div aria-hidden className="relative mt-1.5 h-[1.125rem] text-[0.6875rem] tabular-nums text-muted-foreground">
|
|
469
|
+
{ticks.map((t) => (
|
|
470
|
+
<span
|
|
471
|
+
key={t}
|
|
472
|
+
className={cn(
|
|
473
|
+
"absolute",
|
|
474
|
+
t === min ? "" : t === max ? "-translate-x-full rtl:translate-x-full" : "-translate-x-1/2 rtl:translate-x-1/2"
|
|
475
|
+
)}
|
|
476
|
+
style={{ insetInlineStart: pct(t) }}
|
|
477
|
+
>
|
|
478
|
+
{formatTime(t)}
|
|
479
|
+
</span>
|
|
480
|
+
))}
|
|
481
|
+
</div>
|
|
482
|
+
</>
|
|
483
|
+
) : (
|
|
484
|
+
<div ref={list} data-slot="time-slots-rows" onKeyDown={onRowKey} className="min-h-0 flex-1 overflow-y-auto">
|
|
485
|
+
{rows.map((m, i) => {
|
|
486
|
+
const past = pastAt(m);
|
|
487
|
+
const taken = !past && busyAt(m);
|
|
488
|
+
const on = onAt(m);
|
|
489
|
+
// Free but too short to book, and not a stretch of the booking either.
|
|
490
|
+
const held =
|
|
491
|
+
!on && !past && !taken && !fits(m, rowEnd(m)) && !(value && m >= value.end && fits(value.start, Math.min(top, m + row)));
|
|
492
|
+
// Neighbours in the same run join into one block.
|
|
493
|
+
const joins = (n: number) =>
|
|
494
|
+
n >= min && n < top && (on ? onAt(n) : !!taken && busyAt(n) === taken && !pastAt(n));
|
|
495
|
+
const up = (on || taken) && joins(m - row);
|
|
496
|
+
const down = (on || taken) && joins(m + row);
|
|
497
|
+
return (
|
|
498
|
+
<div key={m} data-slot="time-slots-row" className="grid h-11 grid-cols-[3.25rem_1fr]">
|
|
499
|
+
<span aria-hidden className={cn("pt-0.5 text-xs tabular-nums text-muted-foreground", past && "opacity-50")}>
|
|
500
|
+
{m % 60 ? "" : formatTime(m)}
|
|
501
|
+
</span>
|
|
502
|
+
<div className="relative border-t">
|
|
503
|
+
<button
|
|
504
|
+
type="button"
|
|
505
|
+
data-minute={m}
|
|
506
|
+
data-agent-part={hasAgent ? "" : undefined}
|
|
507
|
+
data-state={on ? "on" : past ? "past" : taken || held ? "busy" : "free"}
|
|
508
|
+
aria-label={`${formatTime(m)}, ${on ? "booked" : past ? "past" : taken ? "busy" : held ? "unavailable" : "free"}`}
|
|
509
|
+
aria-pressed={on}
|
|
510
|
+
aria-disabled={(!on && (past || !!taken || held)) || disabled || undefined}
|
|
511
|
+
tabIndex={m === rowStop ? 0 : -1}
|
|
512
|
+
onClick={(e) => (on || !(past || taken || held)) && press(m, e)}
|
|
513
|
+
className={cn(
|
|
514
|
+
"absolute inset-x-0 inset-y-[3px] rounded-lg px-2.5 text-start text-[0.8125rem] text-muted-foreground",
|
|
515
|
+
"hover:bg-accent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
|
516
|
+
"data-[state=on]:bg-primary data-[state=on]:text-primary-foreground",
|
|
517
|
+
"data-[state=busy]:cursor-not-allowed data-[state=past]:inset-0 data-[state=past]:cursor-not-allowed data-[state=past]:rounded-none",
|
|
518
|
+
up && "-top-px rounded-t-none",
|
|
519
|
+
down && "-bottom-px rounded-b-none"
|
|
520
|
+
)}
|
|
521
|
+
style={
|
|
522
|
+
on
|
|
523
|
+
? undefined
|
|
524
|
+
: past
|
|
525
|
+
? { background: PAST }
|
|
526
|
+
: taken || held
|
|
527
|
+
? { background: `${HATCH} 0 ${-(i * ROW_PX + (up ? -1 : 3))}px / 100% ${rows.length * ROW_PX}px no-repeat, ${BUSY_FILL}` }
|
|
528
|
+
: undefined
|
|
529
|
+
}
|
|
530
|
+
/>
|
|
531
|
+
{nowShown && !on && now >= m && now < m + row && (
|
|
532
|
+
<span
|
|
533
|
+
aria-hidden
|
|
534
|
+
data-slot="time-slots-now"
|
|
535
|
+
className="pointer-events-none absolute inset-x-0 h-0.5 -translate-y-1/2 rounded-sm bg-destructive"
|
|
536
|
+
style={{ top: `${((now - m) / row) * 100}%` }}
|
|
537
|
+
/>
|
|
538
|
+
)}
|
|
539
|
+
</div>
|
|
540
|
+
</div>
|
|
541
|
+
);
|
|
542
|
+
})}
|
|
543
|
+
</div>
|
|
544
|
+
)}
|
|
545
|
+
<div data-slot="time-slots-footer" className={cn("mt-3.5 flex items-center gap-2.5", layout === "vertical" && "mt-2 border-t pt-3")}>
|
|
546
|
+
{/* The slider announces its own value; rows have no value, so the readout speaks for them. */}
|
|
547
|
+
<p data-slot="time-slots-readout" aria-live={layout === "vertical" ? "polite" : undefined} className="min-h-5 flex-1 text-[0.8125rem]">
|
|
548
|
+
{value ? (
|
|
549
|
+
single ? (
|
|
550
|
+
read
|
|
551
|
+
) : (
|
|
552
|
+
<>
|
|
553
|
+
{read}
|
|
554
|
+
{layout === "vertical" ? <br /> : " "}
|
|
555
|
+
<span className="text-muted-foreground">
|
|
556
|
+
{layout === "horizontal" && "· "}
|
|
557
|
+
{length(value.end - value.start)}
|
|
558
|
+
</span>
|
|
559
|
+
</>
|
|
560
|
+
)
|
|
561
|
+
) : (
|
|
562
|
+
<span className="text-muted-foreground">
|
|
563
|
+
{layout === "horizontal"
|
|
564
|
+
? `${single ? "Press a free time" : "Drag across free time"}, or use the arrow keys`
|
|
565
|
+
: `Tap a free ${row === 30 ? "half hour" : length(row)}`}
|
|
566
|
+
</span>
|
|
567
|
+
)}
|
|
568
|
+
</p>
|
|
569
|
+
{children}
|
|
570
|
+
</div>
|
|
571
|
+
{(name || required) && (
|
|
572
|
+
<input
|
|
573
|
+
aria-hidden
|
|
574
|
+
tabIndex={-1}
|
|
575
|
+
name={name}
|
|
576
|
+
required={required}
|
|
577
|
+
disabled={disabled}
|
|
578
|
+
value={value ? (single ? hhmm(value.start) : `${hhmm(value.start)}/${hhmm(value.end)}`) : ""}
|
|
579
|
+
onChange={() => {}}
|
|
580
|
+
onFocus={() => root.current?.querySelector<HTMLElement>("[role=slider], [data-minute][tabindex='0']")?.focus()}
|
|
581
|
+
className="pointer-events-none absolute bottom-0 start-0 size-px opacity-0"
|
|
582
|
+
/>
|
|
583
|
+
)}
|
|
584
|
+
</div>
|
|
585
|
+
);
|
|
586
|
+
});
|
|
587
|
+
TimeSlots.displayName = "TimeSlots";
|
|
588
|
+
|
|
589
|
+
export { TimeSlots };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { WeekStrip, type WeekStripProps } from "./week-strip";
|