@zonsh/ui 0.2.0 → 0.5.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/LICENSE +22 -0
- package/README.md +19 -13
- package/dist/components/accordion.js +1 -1
- package/dist/components/alert-dialog.js +1 -1
- package/dist/components/ascii-field.d.ts +1 -1
- package/dist/components/ascii-field.js +2 -1
- package/dist/components/bottom-nav.d.ts +86 -0
- package/dist/components/bottom-nav.js +175 -0
- package/dist/components/breadcrumb.d.ts +9 -0
- package/dist/components/breadcrumb.js +6 -1
- package/dist/components/button-group.d.ts +1 -1
- package/dist/components/button-group.js +2 -2
- package/dist/components/button.js +1 -1
- package/dist/components/calendar.d.ts +1 -1
- package/dist/components/calendar.js +18 -3
- package/dist/components/carousel.d.ts +22 -3
- package/dist/components/carousel.js +25 -7
- package/dist/components/chart.d.ts +20 -0
- package/dist/components/chart.js +52 -3
- package/dist/components/code-block.d.ts +20 -10
- package/dist/components/code-block.js +54 -12
- package/dist/components/collapsible.js +3 -3
- package/dist/components/color-picker.d.ts +30 -8
- package/dist/components/color-picker.js +48 -27
- package/dist/components/combobox.d.ts +21 -3
- package/dist/components/combobox.js +79 -7
- package/dist/components/command.d.ts +23 -3
- package/dist/components/command.js +63 -18
- package/dist/components/conversation.d.ts +10 -1
- package/dist/components/conversation.js +24 -7
- package/dist/components/copy-button.d.ts +15 -6
- package/dist/components/copy-button.js +27 -10
- package/dist/components/data-table.d.ts +66 -6
- package/dist/components/data-table.js +376 -23
- package/dist/components/date-picker.d.ts +73 -4
- package/dist/components/date-picker.js +138 -7
- package/dist/components/dialog.d.ts +10 -1
- package/dist/components/dialog.js +6 -3
- package/dist/components/drawer.d.ts +10 -1
- package/dist/components/drawer.js +6 -3
- package/dist/components/feature-list.js +2 -2
- package/dist/components/field.d.ts +27 -8
- package/dist/components/field.js +49 -4
- package/dist/components/file-drop.d.ts +26 -4
- package/dist/components/file-drop.js +125 -64
- package/dist/components/filter-bar.d.ts +50 -3
- package/dist/components/filter-bar.js +71 -33
- package/dist/components/form.d.ts +4 -2
- package/dist/components/form.js +24 -2
- package/dist/components/graph.d.ts +41 -0
- package/dist/components/graph.js +106 -50
- package/dist/components/illustration.js +51 -4
- package/dist/components/input-group.d.ts +18 -7
- package/dist/components/input-group.js +41 -19
- package/dist/components/input.d.ts +2 -2
- package/dist/components/input.js +3 -3
- package/dist/components/kbd.d.ts +17 -0
- package/dist/components/kbd.js +8 -1
- package/dist/components/layout.d.ts +2 -1
- package/dist/components/layout.js +55 -11
- package/dist/components/lightbox.d.ts +100 -0
- package/dist/components/lightbox.js +457 -0
- package/dist/components/locale.d.ts +44 -0
- package/dist/components/locale.js +37 -0
- package/dist/components/logo.js +1 -1
- package/dist/components/menu.js +3 -1
- package/dist/components/menubar.js +60 -13
- package/dist/components/message.d.ts +3 -3
- package/dist/components/message.js +12 -8
- package/dist/components/meter.js +3 -1
- package/dist/components/money-input.d.ts +80 -0
- package/dist/components/money-input.js +253 -0
- package/dist/components/navigation-menu.js +4 -2
- package/dist/components/number-field.d.ts +21 -2
- package/dist/components/number-field.js +53 -28
- package/dist/components/otp-field.d.ts +14 -1
- package/dist/components/otp-field.js +10 -2
- package/dist/components/pagination.d.ts +13 -7
- package/dist/components/pagination.js +18 -7
- package/dist/components/pdf-viewer.d.ts +67 -2
- package/dist/components/pdf-viewer.js +88 -21
- package/dist/components/phone-input.d.ts +79 -0
- package/dist/components/phone-input.js +282 -0
- package/dist/components/popover.js +1 -1
- package/dist/components/progress.js +3 -1
- package/dist/components/prompt-input.d.ts +34 -9
- package/dist/components/prompt-input.js +37 -17
- package/dist/components/radio-group.js +7 -2
- package/dist/components/rating.d.ts +73 -0
- package/dist/components/rating.js +124 -0
- package/dist/components/reasoning.d.ts +16 -1
- package/dist/components/reasoning.js +10 -7
- package/dist/components/resizable.d.ts +10 -1
- package/dist/components/resizable.js +4 -1
- package/dist/components/response.d.ts +17 -5
- package/dist/components/response.js +58 -18
- package/dist/components/scroll-area.d.ts +1 -1
- package/dist/components/select.js +14 -3
- package/dist/components/separator.js +1 -1
- package/dist/components/sidebar.d.ts +20 -4
- package/dist/components/sidebar.js +70 -35
- package/dist/components/site-header.d.ts +46 -12
- package/dist/components/site-header.js +78 -19
- package/dist/components/skeleton.d.ts +1 -1
- package/dist/components/skeleton.js +1 -1
- package/dist/components/slider.js +3 -1
- package/dist/components/sortable.d.ts +178 -0
- package/dist/components/sortable.js +1282 -0
- package/dist/components/sources.d.ts +17 -2
- package/dist/components/sources.js +11 -4
- package/dist/components/spinner.d.ts +13 -1
- package/dist/components/spinner.js +5 -2
- package/dist/components/stat.d.ts +91 -0
- package/dist/components/stat.js +141 -0
- package/dist/components/stepper.d.ts +71 -0
- package/dist/components/stepper.js +101 -0
- package/dist/components/switch.d.ts +2 -2
- package/dist/components/switch.js +2 -2
- package/dist/components/table.d.ts +13 -3
- package/dist/components/table.js +231 -41
- package/dist/components/tabs.js +8 -4
- package/dist/components/tags-input.d.ts +18 -2
- package/dist/components/tags-input.js +66 -34
- package/dist/components/theme.d.ts +16 -2
- package/dist/components/theme.js +34 -8
- package/dist/components/time-field.d.ts +24 -4
- package/dist/components/time-field.js +32 -9
- package/dist/components/timeline.d.ts +18 -2
- package/dist/components/timeline.js +13 -4
- package/dist/components/tippy.d.ts +48 -0
- package/dist/components/tippy.js +648 -0
- package/dist/components/toast.d.ts +18 -2
- package/dist/components/toast.js +12 -3
- package/dist/components/toggle-group.js +1 -0
- package/dist/components/tool-call.d.ts +19 -8
- package/dist/components/tool-call.js +23 -7
- package/dist/components/toolbar.d.ts +1 -1
- package/dist/components/toolbar.js +10 -2
- package/dist/components/tooltip.d.ts +4 -2
- package/dist/components/tooltip.js +10 -4
- package/dist/components/tour.d.ts +127 -0
- package/dist/components/tour.js +673 -0
- package/dist/components/tree.js +45 -9
- package/dist/components/video-player.d.ts +10 -2
- package/dist/components/video-player.js +134 -38
- package/dist/illustration.d.ts +2 -0
- package/dist/illustration.js +7 -0
- package/dist/index.d.ts +11 -3
- package/dist/index.js +11 -3
- package/dist/lib/code-highlight.d.ts +3 -16
- package/dist/lib/code-highlight.js +160 -10
- package/dist/lib/code-theme.js +3 -3
- package/dist/lib/digit-roll.js +8 -8
- package/dist/lib/field-errors.d.ts +28 -0
- package/dist/lib/field-errors.js +24 -0
- package/dist/lib/field-value.d.ts +13 -0
- package/dist/lib/field-value.js +44 -0
- package/dist/lib/glide.d.ts +5 -4
- package/dist/lib/glide.js +5 -4
- package/dist/lib/graph-layout.js +11 -2
- package/dist/lib/money.d.ts +73 -0
- package/dist/lib/money.js +343 -0
- package/dist/lib/phone-countries.d.ts +1 -0
- package/dist/lib/phone-countries.js +281 -0
- package/dist/lib/phone.d.ts +90 -0
- package/dist/lib/phone.js +286 -0
- package/dist/lib/platform.d.ts +8 -3
- package/dist/lib/platform.js +13 -6
- package/dist/lib/row-index.d.ts +15 -0
- package/dist/lib/row-index.js +136 -0
- package/dist/lib/table-fit.d.ts +3 -2
- package/dist/lib/table-fit.js +52 -23
- package/dist/lib/theme-script.d.ts +15 -2
- package/dist/lib/theme-script.js +23 -3
- package/dist/lib/tippy/pixels.d.ts +47 -0
- package/dist/lib/tippy/pixels.js +187 -0
- package/dist/lib/tippy/sprite.d.ts +86 -0
- package/dist/lib/tippy/sprite.js +753 -0
- package/dist/messages/de.d.ts +12 -0
- package/dist/messages/de.js +326 -0
- package/dist/styles/expressive.css +60 -26
- package/dist/styles/index.css +11 -2
- package/dist/styles/material.css +47 -23
- package/dist/styles/parts/ai.css +4 -2
- package/dist/styles/parts/controls.css +85 -32
- package/dist/styles/parts/data.css +33 -5
- package/dist/styles/parts/feedback.css +110 -13
- package/dist/styles/parts/foundations.css +1 -1
- package/dist/styles/parts/navigation.css +230 -5
- package/dist/styles/parts/overlays.css +98 -11
- package/dist/styles/parts/pdf.css +13 -7
- package/dist/styles/tokens.css +28 -4
- package/dist/vite.d.ts +0 -50
- package/dist/vite.js +0 -100
- package/package.json +28 -33
- package/dist/components/code-highlight.d.ts +0 -13
- package/dist/components/code-highlight.js +0 -107
- package/dist/components/whiteboard.d.ts +0 -80
- package/dist/components/whiteboard.js +0 -389
- package/dist/styles/parts/whiteboard.css +0 -855
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { Popover as BasePopover } from "@base-ui/react/popover";
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
import { createPortal } from "react-dom";
|
|
6
|
+
import { cn } from "../lib/cn.js";
|
|
7
|
+
import { cssDuration } from "../lib/motion.js";
|
|
8
|
+
import { overlayArrow, overlayPopup, usePopupDir } from "./_overlay.js";
|
|
9
|
+
import { Button } from "./button.js";
|
|
10
|
+
import { useMessages } from "./locale.js";
|
|
11
|
+
const defaultMessages = {
|
|
12
|
+
back: "Back",
|
|
13
|
+
next: "Next",
|
|
14
|
+
done: "Done",
|
|
15
|
+
skip: "Skip tour",
|
|
16
|
+
progress: (step, count) => `Step ${step} of ${count}`,
|
|
17
|
+
};
|
|
18
|
+
/** The element of a target: `undefined` for a step without one, `null` for one that is missing. */
|
|
19
|
+
function resolve(target) {
|
|
20
|
+
if (target == null)
|
|
21
|
+
return undefined;
|
|
22
|
+
let el = null;
|
|
23
|
+
try {
|
|
24
|
+
el =
|
|
25
|
+
typeof target === "string"
|
|
26
|
+
? document.querySelector(target)
|
|
27
|
+
: typeof target === "function"
|
|
28
|
+
? target()
|
|
29
|
+
: target.current;
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
el = null; // a selector that does not parse
|
|
33
|
+
}
|
|
34
|
+
// Rendered and shown: an element under `display: none` has no boxes.
|
|
35
|
+
return el && el.isConnected && el.getClientRects().length > 0 ? el : null;
|
|
36
|
+
}
|
|
37
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
38
|
+
/** Around the target, the hole in the spotlight leaves this much room. */
|
|
39
|
+
const SPOTLIGHT_PADDING = 6;
|
|
40
|
+
/* ── Motion ────────────────────────────────────────────────────────────────── */
|
|
41
|
+
/** How long the popover and the spotlight take to move from one target to the next: 250ms, 0 under reduced motion. */
|
|
42
|
+
const glideMs = (el) => (cssDuration(el) * 5) / 3;
|
|
43
|
+
/** The brand curve as the element sees it, for scripted animations: entering, leaving, fading. */
|
|
44
|
+
const easeOf = (el) => getComputedStyle(el).getPropertyValue("--ease").trim() || "cubic-bezier(0.16, 1, 0.3, 1)";
|
|
45
|
+
/** A CSS cubic-bezier() as a function of progress, for the spotlight drawn each frame. */
|
|
46
|
+
function bezier(css) {
|
|
47
|
+
const [x1 = 0.16, y1 = 1, x2 = 0.3, y2 = 1] = (/cubic-bezier\(([^)]+)\)/.exec(css)?.[1] ?? "")
|
|
48
|
+
.split(",")
|
|
49
|
+
.map(Number)
|
|
50
|
+
.filter(Number.isFinite);
|
|
51
|
+
const at = (a, b, t) => 3 * a * t * (1 - t) ** 2 + 3 * b * t * t * (1 - t) + t ** 3;
|
|
52
|
+
return (x) => {
|
|
53
|
+
if (x <= 0)
|
|
54
|
+
return 0;
|
|
55
|
+
if (x >= 1)
|
|
56
|
+
return 1;
|
|
57
|
+
let lo = 0;
|
|
58
|
+
let hi = 1;
|
|
59
|
+
for (let i = 0; i < 18; i++) {
|
|
60
|
+
const mid = (lo + hi) / 2;
|
|
61
|
+
if (at(x1, x2, mid) < x)
|
|
62
|
+
lo = mid;
|
|
63
|
+
else
|
|
64
|
+
hi = mid;
|
|
65
|
+
}
|
|
66
|
+
return at(y1, y2, (lo + hi) / 2);
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** Whether all of the element is on screen. */
|
|
70
|
+
function inView(el) {
|
|
71
|
+
const r = el.getBoundingClientRect();
|
|
72
|
+
return r.top >= 0 && r.left >= 0 && r.bottom <= window.innerHeight && r.right <= window.innerWidth;
|
|
73
|
+
}
|
|
74
|
+
/** Call `done` once the element has stopped moving (the page has scrolled it into view), or after 1.5s. */
|
|
75
|
+
function whenSettled(el, done) {
|
|
76
|
+
let frame = 0;
|
|
77
|
+
let still = 0;
|
|
78
|
+
let last = "";
|
|
79
|
+
const start = performance.now();
|
|
80
|
+
const check = () => {
|
|
81
|
+
const r = el.getBoundingClientRect();
|
|
82
|
+
const at = `${Math.round(r.left)},${Math.round(r.top)}`;
|
|
83
|
+
still = at === last ? still + 1 : 0;
|
|
84
|
+
last = at;
|
|
85
|
+
if (still >= 5 || performance.now() - start > 1500)
|
|
86
|
+
done();
|
|
87
|
+
else
|
|
88
|
+
frame = requestAnimationFrame(check);
|
|
89
|
+
};
|
|
90
|
+
frame = requestAnimationFrame(check);
|
|
91
|
+
return () => cancelAnimationFrame(frame);
|
|
92
|
+
}
|
|
93
|
+
/** How long a guide may stay on after the tour ends, at most. */
|
|
94
|
+
const GUIDE_LINGER = 4000;
|
|
95
|
+
/** How long the tour waits, as it opens, for a guide to say where the first step goes. */
|
|
96
|
+
const GUIDE_WAIT = 100;
|
|
97
|
+
/** The bubble's tail: how long it is, how wide where it meets the bubble, and the room past its tip. */
|
|
98
|
+
const TAIL = { length: 12, base: 20, gap: 4 };
|
|
99
|
+
/**
|
|
100
|
+
* Tour — a guided walk through a product, one step at a time: each step a small popover in the
|
|
101
|
+
* menu material anchored to the thing it explains, with its title, a sentence or two, a mono
|
|
102
|
+
* "02 / 05", Back, Next (Done on the last) and Skip. A spotlight dims the page around the target;
|
|
103
|
+
* between steps the popover glides to the next target and the spotlight's hole moves with it, a
|
|
104
|
+
* far target scrolled into view first. The popover follows its target as the page scrolls and
|
|
105
|
+
* resizes. A step whose target is not on the page is skipped. The arrow keys move between steps;
|
|
106
|
+
* Escape skips. A `guide` can walk the tour alongside — a mascot such as Tippy.
|
|
107
|
+
*
|
|
108
|
+
* The rule people break: a tour that explains what the interface should say itself. Keep it to the
|
|
109
|
+
* few things people cannot find, and let them skip it at any step.
|
|
110
|
+
*/
|
|
111
|
+
export function Tour({ steps, open: openProp, defaultOpen = false, onOpenChange, step: stepProp, defaultStep = 0, onStepChange, onComplete, onSkip, spotlight = true, guide, labels, className, }) {
|
|
112
|
+
const text = useMessages("tour", defaultMessages, labels);
|
|
113
|
+
const [ownOpen, setOwnOpen] = React.useState(defaultOpen);
|
|
114
|
+
const [ownStep, setOwnStep] = React.useState(defaultStep);
|
|
115
|
+
const open = openProp ?? ownOpen;
|
|
116
|
+
const step = Math.min(Math.max(stepProp ?? ownStep, 0), Math.max(steps.length - 1, 0));
|
|
117
|
+
const popupDir = usePopupDir();
|
|
118
|
+
const setOpen = (next) => {
|
|
119
|
+
if (openProp === undefined)
|
|
120
|
+
setOwnOpen(next);
|
|
121
|
+
onOpenChange?.(next);
|
|
122
|
+
};
|
|
123
|
+
const setStep = (next) => {
|
|
124
|
+
if (stepProp === undefined)
|
|
125
|
+
setOwnStep(next);
|
|
126
|
+
onStepChange?.(next);
|
|
127
|
+
};
|
|
128
|
+
const latest = React.useRef({ setOpen, setStep });
|
|
129
|
+
latest.current = { setOpen, setStep };
|
|
130
|
+
/** Which way the tour is going, so a missing target is skipped in that direction. */
|
|
131
|
+
const direction = React.useRef(1);
|
|
132
|
+
const [placed, setPlaced] = React.useState(null);
|
|
133
|
+
const [shown, setShown] = React.useState(null);
|
|
134
|
+
const shownRef = React.useRef(shown);
|
|
135
|
+
shownRef.current = shown;
|
|
136
|
+
/** The popover has faded out while the page scrolls to the next target. */
|
|
137
|
+
const [away, setAway] = React.useState(false);
|
|
138
|
+
const popupRef = React.useRef(null);
|
|
139
|
+
const [popupEl, setPopupEl] = React.useState(null);
|
|
140
|
+
const setPopup = React.useCallback((el) => {
|
|
141
|
+
popupRef.current = el;
|
|
142
|
+
setPopupEl(el);
|
|
143
|
+
}, []);
|
|
144
|
+
/** The step's content, and a layer over it where the last step's fades out as the popover glides. */
|
|
145
|
+
const contentRef = React.useRef(null);
|
|
146
|
+
const ghostRef = React.useRef(null);
|
|
147
|
+
/** What the popover looked like as it set off: its height and its content. */
|
|
148
|
+
const departure = React.useRef(null);
|
|
149
|
+
/** Focus before the tour opened, to go back to. */
|
|
150
|
+
const returnTo = React.useRef(null);
|
|
151
|
+
/** Where a guide has put the step, when the popover is its speech bubble. */
|
|
152
|
+
const [placement, setPlacement] = React.useState(null);
|
|
153
|
+
/** Opening with a guide, the popover waits for it to place the first step, or to not. */
|
|
154
|
+
const [guideAnswered, setGuideAnswered] = React.useState(false);
|
|
155
|
+
/** Each time the tour opens, a new guide: one still playing out the last tour goes. */
|
|
156
|
+
const [session, setSession] = React.useState(0);
|
|
157
|
+
const bubbled = guide ? placement : null;
|
|
158
|
+
const bubble = bubbled !== null;
|
|
159
|
+
const bubbleRef = React.useRef(bubble);
|
|
160
|
+
bubbleRef.current = bubble;
|
|
161
|
+
/** The anchor the bubble has been put at: a new one is given a couple of frames to be measured. */
|
|
162
|
+
const [positioned, setPositioned] = React.useState(null);
|
|
163
|
+
const bubbleAnchor = bubbled?.anchor;
|
|
164
|
+
React.useEffect(() => {
|
|
165
|
+
if (!bubbleAnchor)
|
|
166
|
+
return;
|
|
167
|
+
let frame = requestAnimationFrame(() => {
|
|
168
|
+
frame = requestAnimationFrame(() => setPositioned(bubbleAnchor));
|
|
169
|
+
});
|
|
170
|
+
return () => cancelAnimationFrame(frame);
|
|
171
|
+
}, [bubbleAnchor]);
|
|
172
|
+
// Find the step's target; skip the steps whose targets are not on the page.
|
|
173
|
+
React.useLayoutEffect(() => {
|
|
174
|
+
if (!open) {
|
|
175
|
+
setPlaced(null);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const found = steps.map((s) => resolve(s.target));
|
|
179
|
+
const available = found.flatMap((el, i) => (el === null ? [] : [i]));
|
|
180
|
+
if (!available.length) {
|
|
181
|
+
latest.current.setOpen(false);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
let at = step;
|
|
185
|
+
if (found[at] === null) {
|
|
186
|
+
const ahead = available.filter((i) => (direction.current === 1 ? i > at : i < at));
|
|
187
|
+
const next = direction.current === 1 ? ahead[0] : ahead[ahead.length - 1];
|
|
188
|
+
// Nothing left that way: the nearest step the other way.
|
|
189
|
+
at = next ?? (direction.current === 1 ? available[available.length - 1] : available[0]);
|
|
190
|
+
if (at !== step)
|
|
191
|
+
latest.current.setStep(at);
|
|
192
|
+
}
|
|
193
|
+
const anchor = found[at] ?? null;
|
|
194
|
+
setPlaced((prev) => prev && prev.index === at && prev.anchor === anchor && prev.available.join() === available.join()
|
|
195
|
+
? prev
|
|
196
|
+
: { index: at, anchor, available });
|
|
197
|
+
}, [open, step, steps]);
|
|
198
|
+
// Remember where focus was, to return it when the tour ends; and start a guide afresh.
|
|
199
|
+
React.useLayoutEffect(() => {
|
|
200
|
+
if (!open)
|
|
201
|
+
return;
|
|
202
|
+
returnTo.current = document.activeElement;
|
|
203
|
+
setPlacement(null);
|
|
204
|
+
setGuideAnswered(false);
|
|
205
|
+
setSession((n) => n + 1);
|
|
206
|
+
}, [open]);
|
|
207
|
+
// Take the popover to the step: glide there when the target is in view; when it is not, scroll
|
|
208
|
+
// it into view first, with the popover faded out, and show it there once the page has stopped.
|
|
209
|
+
React.useEffect(() => {
|
|
210
|
+
if (!placed) {
|
|
211
|
+
setShown(null);
|
|
212
|
+
setAway(false);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const before = shownRef.current;
|
|
216
|
+
if (before && before.index === placed.index && before.anchor === placed.anchor) {
|
|
217
|
+
if (before.available !== placed.available)
|
|
218
|
+
setShown({ ...before, available: placed.available });
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const { anchor } = placed;
|
|
222
|
+
const moving = typeof window !== "undefined" && glideMs(document.documentElement) > 0;
|
|
223
|
+
const go = (how) => {
|
|
224
|
+
const popup = popupRef.current;
|
|
225
|
+
departure.current =
|
|
226
|
+
how === "glide" && popup
|
|
227
|
+
? { height: popup.offsetHeight, content: contentRef.current?.cloneNode(true) ?? null }
|
|
228
|
+
: null;
|
|
229
|
+
setAway(false);
|
|
230
|
+
setShown({ ...placed, how });
|
|
231
|
+
};
|
|
232
|
+
if (before && bubbleRef.current) {
|
|
233
|
+
// A guide's bubble goes back into its tail as the guide sets off, the page scrolls to a far
|
|
234
|
+
// target, and the next step waits for the guide to place it there.
|
|
235
|
+
setAway(true);
|
|
236
|
+
let stop = () => { };
|
|
237
|
+
const timer = window.setTimeout(() => {
|
|
238
|
+
if (anchor && !inView(anchor)) {
|
|
239
|
+
anchor.scrollIntoView({
|
|
240
|
+
block: "center",
|
|
241
|
+
inline: "nearest",
|
|
242
|
+
behavior: moving ? "smooth" : "auto",
|
|
243
|
+
});
|
|
244
|
+
if (moving) {
|
|
245
|
+
stop = whenSettled(anchor, () => go("rest"));
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
go("rest");
|
|
250
|
+
}, moving ? cssDuration(document.documentElement) : 0);
|
|
251
|
+
return () => {
|
|
252
|
+
window.clearTimeout(timer);
|
|
253
|
+
stop();
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
if (!anchor || inView(anchor)) {
|
|
257
|
+
go(before && moving ? "glide" : "instant");
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
anchor.scrollIntoView({ block: "center", inline: "nearest", behavior: moving ? "smooth" : "auto" });
|
|
261
|
+
if (!moving) {
|
|
262
|
+
go("instant");
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
if (before)
|
|
266
|
+
setAway(true);
|
|
267
|
+
return whenSettled(anchor, () => go(before ? "arrive" : "instant"));
|
|
268
|
+
}, [placed]);
|
|
269
|
+
// Settled: the glide or the arrival is over.
|
|
270
|
+
const how = shown?.how;
|
|
271
|
+
const shownIndex = shown?.index;
|
|
272
|
+
React.useEffect(() => {
|
|
273
|
+
if (how !== "glide" && how !== "arrive")
|
|
274
|
+
return;
|
|
275
|
+
const popup = popupRef.current;
|
|
276
|
+
const ms = popup ? (how === "glide" ? glideMs(popup) : cssDuration(popup)) : 0;
|
|
277
|
+
const settle = () => setShown((s) => (s && s.index === shownIndex ? { ...s, how: "rest" } : s));
|
|
278
|
+
const positioner = how === "glide" ? popup?.parentElement : null;
|
|
279
|
+
if (!positioner) {
|
|
280
|
+
const timer = window.setTimeout(settle, ms + 40);
|
|
281
|
+
return () => window.clearTimeout(timer);
|
|
282
|
+
}
|
|
283
|
+
// A glide is over when the positioner's move is. On a busy page its new place can land
|
|
284
|
+
// several frames after the step changed, so the move is waited for rather than timed from
|
|
285
|
+
// the step; a glide to the same place starts no move and settles on the longer fallback.
|
|
286
|
+
let timer = window.setTimeout(settle, ms * 2 + 500);
|
|
287
|
+
const ours = (event) => event.target === positioner && event.propertyName === "transform";
|
|
288
|
+
const onRun = (event) => {
|
|
289
|
+
if (!ours(event))
|
|
290
|
+
return;
|
|
291
|
+
window.clearTimeout(timer);
|
|
292
|
+
timer = window.setTimeout(settle, ms + 200);
|
|
293
|
+
};
|
|
294
|
+
const onEnd = (event) => {
|
|
295
|
+
if (ours(event))
|
|
296
|
+
settle();
|
|
297
|
+
};
|
|
298
|
+
positioner.addEventListener("transitionrun", onRun);
|
|
299
|
+
positioner.addEventListener("transitionend", onEnd);
|
|
300
|
+
return () => {
|
|
301
|
+
window.clearTimeout(timer);
|
|
302
|
+
positioner.removeEventListener("transitionrun", onRun);
|
|
303
|
+
positioner.removeEventListener("transitionend", onEnd);
|
|
304
|
+
};
|
|
305
|
+
}, [how, shownIndex]);
|
|
306
|
+
// The motion itself: the height eases from the last step's to this one's while the last step's
|
|
307
|
+
// content fades out over it and this one's fades in; or the popover appears where it arrived.
|
|
308
|
+
React.useLayoutEffect(() => {
|
|
309
|
+
const popup = popupRef.current;
|
|
310
|
+
if (!popup || (how !== "glide" && how !== "arrive"))
|
|
311
|
+
return;
|
|
312
|
+
const ms = glideMs(popup);
|
|
313
|
+
const easing = easeOf(popup);
|
|
314
|
+
// The height changes with the glide, on its curve, so the popover moves and resizes as one.
|
|
315
|
+
const move = easing;
|
|
316
|
+
const running = [];
|
|
317
|
+
const ghost = ghostRef.current;
|
|
318
|
+
if (how === "arrive") {
|
|
319
|
+
running.push(popup.animate([
|
|
320
|
+
{ opacity: 0, scale: 0.97 },
|
|
321
|
+
{ opacity: 1, scale: 1 },
|
|
322
|
+
], {
|
|
323
|
+
duration: cssDuration(popup),
|
|
324
|
+
easing,
|
|
325
|
+
}));
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
const from = departure.current;
|
|
329
|
+
departure.current = null;
|
|
330
|
+
if (from) {
|
|
331
|
+
const height = popup.offsetHeight;
|
|
332
|
+
if (Math.abs(height - from.height) > 0.5)
|
|
333
|
+
running.push(popup.animate([{ height: `${from.height}px` }, { height: `${height}px` }], {
|
|
334
|
+
duration: ms,
|
|
335
|
+
easing: move,
|
|
336
|
+
}));
|
|
337
|
+
if (ghost && from.content) {
|
|
338
|
+
// A picture of the last step, never read out or reached: no ids, nothing to focus.
|
|
339
|
+
const copy = from.content;
|
|
340
|
+
copy.removeAttribute("data-slot");
|
|
341
|
+
for (const el of [copy, ...copy.querySelectorAll("[id]")])
|
|
342
|
+
el.removeAttribute("id");
|
|
343
|
+
ghost.replaceChildren(copy);
|
|
344
|
+
running.push(ghost.animate([{ opacity: 1 }, { opacity: 0 }], { duration: ms * 0.5, fill: "forwards" }));
|
|
345
|
+
}
|
|
346
|
+
const content = contentRef.current;
|
|
347
|
+
if (content)
|
|
348
|
+
running.push(content.animate([{ opacity: 0 }, { opacity: 1 }], {
|
|
349
|
+
duration: ms * 0.65,
|
|
350
|
+
delay: ms * 0.35,
|
|
351
|
+
easing,
|
|
352
|
+
fill: "backwards",
|
|
353
|
+
}));
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return () => {
|
|
357
|
+
for (const a of running)
|
|
358
|
+
a.cancel();
|
|
359
|
+
ghost?.replaceChildren();
|
|
360
|
+
};
|
|
361
|
+
}, [how, shownIndex]);
|
|
362
|
+
// Move focus to the step so it is read out.
|
|
363
|
+
React.useEffect(() => {
|
|
364
|
+
if (shownIndex === undefined)
|
|
365
|
+
return;
|
|
366
|
+
const frame = requestAnimationFrame(() => popupRef.current?.focus({ preventScroll: true }));
|
|
367
|
+
return () => cancelAnimationFrame(frame);
|
|
368
|
+
}, [shownIndex]);
|
|
369
|
+
// Where the tour is (the step it is on), and what the popover shows: it may still be on its way,
|
|
370
|
+
// and as the tour ends it keeps its last step, where it was, while it fades out.
|
|
371
|
+
const index = placed?.index;
|
|
372
|
+
const available = placed?.available ?? [];
|
|
373
|
+
const position = index === undefined ? 0 : available.indexOf(index);
|
|
374
|
+
const leaving = React.useRef(null);
|
|
375
|
+
if (shown)
|
|
376
|
+
leaving.current = shown;
|
|
377
|
+
const view = shown ?? (open ? null : leaving.current);
|
|
378
|
+
const viewAvailable = view?.available ?? [];
|
|
379
|
+
const viewPosition = view ? viewAvailable.indexOf(view.index) : 0;
|
|
380
|
+
const last = viewPosition === viewAvailable.length - 1;
|
|
381
|
+
const current = view ? steps[view.index] : undefined;
|
|
382
|
+
const anchor = view?.anchor ?? null;
|
|
383
|
+
const gliding = how === "glide" && !bubble;
|
|
384
|
+
// Out of sight: faded while the page scrolls to a far target; as a guide's bubble, also while
|
|
385
|
+
// the guide is on its way, until it says the step; and, opening with a guide, until it answers.
|
|
386
|
+
const spoken = !bubbled || (bubbled.step === view?.index && bubbled.ready && positioned === bubbled.anchor);
|
|
387
|
+
const waiting = !!guide && open && !guideAnswered;
|
|
388
|
+
const hidden = away || waiting || !spoken;
|
|
389
|
+
const wasHidden = React.useRef(false);
|
|
390
|
+
React.useLayoutEffect(() => {
|
|
391
|
+
const popup = popupRef.current;
|
|
392
|
+
const before = wasHidden.current;
|
|
393
|
+
wasHidden.current = hidden;
|
|
394
|
+
if (!popup || hidden === before)
|
|
395
|
+
return;
|
|
396
|
+
const ms = cssDuration(popup);
|
|
397
|
+
if (!ms)
|
|
398
|
+
return;
|
|
399
|
+
// A bubble goes back into its tail, and comes out of it with a little overshoot, as if said;
|
|
400
|
+
// a popover fades.
|
|
401
|
+
const motion = hidden
|
|
402
|
+
? popup.animate(bubble
|
|
403
|
+
? [
|
|
404
|
+
{ opacity: 1, scale: 1 },
|
|
405
|
+
{ opacity: 0, scale: 0.6 },
|
|
406
|
+
]
|
|
407
|
+
: [{ opacity: 1 }, { opacity: 0 }], { duration: ms, easing: bubble ? "cubic-bezier(0.5, 0, 0.75, 0)" : easeOf(popup) })
|
|
408
|
+
: bubble
|
|
409
|
+
? popup.animate([
|
|
410
|
+
{ opacity: 0, scale: 0.4 },
|
|
411
|
+
{ opacity: 1, scale: 1.04, offset: 0.6 },
|
|
412
|
+
{ opacity: 1, scale: 1 },
|
|
413
|
+
], { duration: ms * 2, easing: "cubic-bezier(0.2, 0.7, 0.3, 1)" })
|
|
414
|
+
: null;
|
|
415
|
+
return () => motion?.cancel();
|
|
416
|
+
}, [hidden, bubble]);
|
|
417
|
+
// With a guide, the first step waits a moment for the guide to place it.
|
|
418
|
+
React.useEffect(() => {
|
|
419
|
+
if (!waiting || !shown)
|
|
420
|
+
return;
|
|
421
|
+
const timer = window.setTimeout(() => setGuideAnswered(true), GUIDE_WAIT);
|
|
422
|
+
return () => window.clearTimeout(timer);
|
|
423
|
+
}, [waiting, shown]);
|
|
424
|
+
const place = React.useCallback((next) => {
|
|
425
|
+
setPlacement(next);
|
|
426
|
+
setGuideAnswered(true);
|
|
427
|
+
}, []);
|
|
428
|
+
/** How the tour ended, for the guide. */
|
|
429
|
+
const ending = React.useRef(null);
|
|
430
|
+
const finish = (how) => {
|
|
431
|
+
ending.current = how;
|
|
432
|
+
if (how === "complete")
|
|
433
|
+
onComplete?.();
|
|
434
|
+
else
|
|
435
|
+
onSkip?.(index ?? step);
|
|
436
|
+
setOpen(false);
|
|
437
|
+
};
|
|
438
|
+
const go = (delta) => {
|
|
439
|
+
direction.current = delta;
|
|
440
|
+
const next = available[position + delta];
|
|
441
|
+
if (next === undefined) {
|
|
442
|
+
if (delta === 1)
|
|
443
|
+
finish("complete");
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
setStep(next);
|
|
447
|
+
};
|
|
448
|
+
const goRef = React.useRef(go);
|
|
449
|
+
goRef.current = go;
|
|
450
|
+
// The arrow keys move between steps, unless they are busy in a field.
|
|
451
|
+
React.useEffect(() => {
|
|
452
|
+
if (!open)
|
|
453
|
+
return;
|
|
454
|
+
const onKey = (event) => {
|
|
455
|
+
if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey)
|
|
456
|
+
return;
|
|
457
|
+
if (event.key !== "ArrowLeft" && event.key !== "ArrowRight")
|
|
458
|
+
return;
|
|
459
|
+
const target = event.target;
|
|
460
|
+
if (target?.closest("input, textarea, select, [contenteditable=''], [contenteditable=true], [role=slider], [role=tablist], [role=radiogroup], [role=listbox], [role=menu], [role=grid], [role=tree], [role=toolbar], [role=menubar], [data-slot=toggle-group]"))
|
|
461
|
+
return;
|
|
462
|
+
const rtl = popupRef.current ? getComputedStyle(popupRef.current).direction === "rtl" : false;
|
|
463
|
+
event.preventDefault();
|
|
464
|
+
goRef.current((event.key === "ArrowRight") !== rtl ? 1 : -1);
|
|
465
|
+
};
|
|
466
|
+
document.addEventListener("keydown", onKey);
|
|
467
|
+
return () => document.removeEventListener("keydown", onKey);
|
|
468
|
+
}, [open]);
|
|
469
|
+
// What the popover is placed against: the step's target, or a point in the middle of the screen
|
|
470
|
+
// for a step without one. Base UI takes a new anchor an effect later than the step's side and
|
|
471
|
+
// offset, so every anchor measures the step the tour is on now: a placement made in between
|
|
472
|
+
// lands on the new target, never on the last one with the new step's side and offset.
|
|
473
|
+
const anchorNow = React.useRef(null);
|
|
474
|
+
anchorNow.current = anchor;
|
|
475
|
+
const placeAgainst = React.useMemo(() => ({
|
|
476
|
+
getBoundingClientRect: () => anchorNow.current?.getBoundingClientRect() ??
|
|
477
|
+
new DOMRect(window.innerWidth / 2, window.innerHeight / 2, 0, 0),
|
|
478
|
+
// Followed as its scroll containers scroll and as it resizes.
|
|
479
|
+
contextElement: anchor ?? undefined,
|
|
480
|
+
}), [anchor]);
|
|
481
|
+
const popoverOpen = open && !!shown && !!current;
|
|
482
|
+
const offset = 10 + (spotlight ? SPOTLIGHT_PADDING : 0);
|
|
483
|
+
// The spotlight goes to the step the tour is on at once (it follows the target as the page
|
|
484
|
+
// scrolls), and fades out when the tour ends.
|
|
485
|
+
const lit = open && !!placed && spotlight;
|
|
486
|
+
const [lingering, setLingering] = React.useState(false);
|
|
487
|
+
const litAnchor = React.useRef(null);
|
|
488
|
+
if (lit)
|
|
489
|
+
litAnchor.current = placed?.anchor ?? null;
|
|
490
|
+
React.useEffect(() => {
|
|
491
|
+
if (lit)
|
|
492
|
+
setLingering(true);
|
|
493
|
+
}, [lit]);
|
|
494
|
+
// The guide comes with the tour and stays a moment after it ends, to play its way out.
|
|
495
|
+
const [guideStatus, setGuideStatus] = React.useState(null);
|
|
496
|
+
React.useEffect(() => {
|
|
497
|
+
if (!guide)
|
|
498
|
+
return;
|
|
499
|
+
if (popoverOpen) {
|
|
500
|
+
ending.current = null;
|
|
501
|
+
setGuideStatus("open");
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
if (open)
|
|
505
|
+
return;
|
|
506
|
+
setGuideStatus((s) => (s === "open" ? (ending.current ?? "closed") : s));
|
|
507
|
+
}, [guide, popoverOpen, open]);
|
|
508
|
+
React.useEffect(() => {
|
|
509
|
+
if (!guideStatus || guideStatus === "open")
|
|
510
|
+
return;
|
|
511
|
+
const timer = window.setTimeout(() => setGuideStatus(null), GUIDE_LINGER);
|
|
512
|
+
return () => window.clearTimeout(timer);
|
|
513
|
+
}, [guideStatus]);
|
|
514
|
+
const release = React.useCallback(() => setGuideStatus((s) => (s === "open" ? s : null)), []);
|
|
515
|
+
// It comes on with the popover, in the same frame, so it can place the first step before it shows.
|
|
516
|
+
const guideOn = popoverOpen ? "open" : guideStatus;
|
|
517
|
+
const lastShown = React.useRef({ step: 0, position: 0, count: 0, target: null });
|
|
518
|
+
if (shown)
|
|
519
|
+
lastShown.current = {
|
|
520
|
+
step: shown.index,
|
|
521
|
+
position: viewPosition,
|
|
522
|
+
count: viewAvailable.length,
|
|
523
|
+
target: anchor,
|
|
524
|
+
};
|
|
525
|
+
return (_jsxs(_Fragment, { children: [(lit || lingering) && (_jsx(Spotlight, { anchor: litAnchor.current, closing: !lit, onClosed: () => setLingering(false) })), guide &&
|
|
526
|
+
guideOn &&
|
|
527
|
+
createPortal(_jsx("div", { "aria-hidden": true, "data-slot": "tour-guide", className: "pointer-events-none fixed inset-0 z-50 overflow-clip", children: guide({
|
|
528
|
+
...lastShown.current,
|
|
529
|
+
popover: popupEl,
|
|
530
|
+
moving: guideOn === "open" &&
|
|
531
|
+
(!shown ||
|
|
532
|
+
shown.index !== index ||
|
|
533
|
+
away ||
|
|
534
|
+
(!bubble && (how === "glide" || how === "arrive"))),
|
|
535
|
+
status: guideOn,
|
|
536
|
+
release,
|
|
537
|
+
place,
|
|
538
|
+
}) }, session), document.body), _jsx(BasePopover.Root, { open: popoverOpen, modal: false, onOpenChange: (next, details) => {
|
|
539
|
+
// Clicks elsewhere and focus moving away leave the tour open; Escape skips it.
|
|
540
|
+
if (!next && details.reason === "escape-key")
|
|
541
|
+
finish("skip");
|
|
542
|
+
}, children: _jsx(BasePopover.Portal, { children: _jsx(BasePopover.Positioner, { anchor: bubbled ? bubbled.anchor : placeAgainst, side: bubbled ? (bubbled.side ?? "top") : anchor ? (current?.side ?? "bottom") : "bottom", align: bubbled ? (bubbled.align ?? "center") : anchor ? (current?.align ?? "center") : "center", sideOffset: bubble ? TAIL.length + TAIL.gap : anchor ? offset : ({ positioner }) => -positioner.height / 2, collisionPadding: 12,
|
|
543
|
+
// A guide has placed its bubble where it fits: it is shifted along to stay on screen,
|
|
544
|
+
// never flipped to the other side of the guide's head.
|
|
545
|
+
collisionAvoidance: bubble ? { side: "none", align: "shift", fallbackAxisSide: "none" } : undefined, arrowPadding: bubble ? 18 : 14, dir: popupDir, "data-tour-glide": gliding ? "" : undefined, className: "z-50 outline-none [--tour-glide:calc(var(--duration)*5/3)] data-tour-glide:transition-transform data-tour-glide:duration-(--tour-glide) data-tour-glide:ease-(--ease)", children: _jsxs(BasePopover.Popup, { ref: setPopup, "data-slot": "tour-content", "data-step": view?.index, "data-tour-glide": gliding ? "" : undefined, "data-tour-bubble": bubble ? "" : undefined, "data-tour-hidden": hidden ? "" : undefined, initialFocus: popupRef, finalFocus: () => (returnTo.current?.isConnected ? returnTo.current : false), className: cn("relative flex w-80 max-w-(--available-width) flex-col gap-3 p-4 text-sm data-tour-glide:overflow-clip", "data-tour-hidden:pointer-events-none data-tour-hidden:opacity-0 data-tour-hidden:transition-none", overlayPopup, className), children: [bubble ? (_jsx(BasePopover.Arrow, { "data-slot": "tour-tail", className: tail, children: _jsx(BubbleTail, {}) })) : (anchor && (_jsx(BasePopover.Arrow, { "data-slot": "tour-arrow", className: cn(overlayArrow, "transition-opacity duration-(--duration) ease-(--ease)", gliding && "opacity-0") }))), _jsxs("div", { className: "relative", children: [current && (_jsxs("div", { ref: contentRef, "data-slot": "tour-step", className: "flex flex-col gap-3", children: [_jsxs("div", { className: "flex flex-col gap-1.5", children: [_jsxs("span", { "data-slot": "tour-progress", dir: "ltr", className: "self-start font-mono text-[12px] leading-none text-muted-foreground tabular-nums", children: [_jsxs("span", { "aria-hidden": true, children: [_jsx("span", { className: "text-foreground", children: pad(viewPosition + 1) }), " /", " ", pad(viewAvailable.length)] }), _jsx("span", { className: "sr-only", children: text.progress(viewPosition + 1, viewAvailable.length) })] }), _jsx(BasePopover.Title, { "data-slot": "tour-title", className: "m-0 font-body text-[15px] leading-snug font-medium tracking-normal text-balance text-foreground", children: current.title }), current.body && (_jsx(BasePopover.Description, { "data-slot": "tour-body", className: "m-0 text-[13px] leading-normal text-pretty text-muted-foreground", children: current.body }))] }), _jsxs("div", { "data-slot": "tour-footer", className: "flex flex-wrap items-center gap-2 pt-1", children: [!last && (_jsx(Button, { variant: "ghost", size: "sm", className: "-ms-2 text-muted-foreground", onClick: () => finish("skip"), children: text.skip })), _jsxs("div", { className: "ms-auto flex items-center gap-2", children: [viewPosition > 0 && (_jsx(Button, { variant: "secondary", size: "sm", onClick: () => go(-1), children: text.back })), _jsx(Button, { variant: "primary", size: "sm", "data-slot": "tour-next", onClick: () => go(1), children: last ? text.done : text.next })] })] })] })), _jsx("div", { ref: ghostRef, "aria-hidden": true, inert: true, className: "pointer-events-none absolute inset-x-0 top-0" })] })] }) }) }) })] }));
|
|
546
|
+
}
|
|
547
|
+
/* ── The bubble's tail ─────────────────────────────────────────────────────── */
|
|
548
|
+
/*
|
|
549
|
+
* The tail of a guide's bubble, drawn pointing down from the bubble's bottom edge and turned for
|
|
550
|
+
* the other sides. It is the bubble's own surface: the same fill, and its edge is the bubble's edge
|
|
551
|
+
* bent down to a point and back — the 1px border (`--field-edge`) and the fine ring outside it
|
|
552
|
+
* (`--control-edge`) — so there is no seam where it meets the bubble. Its base sits on the inner
|
|
553
|
+
* line of the bubble's border; for left and right it is turned a quarter about its middle, which
|
|
554
|
+
* is why those offsets take (base - length) / 2.
|
|
555
|
+
*/
|
|
556
|
+
const tail = cn("absolute block h-3 w-5 overflow-visible", "data-[side=top]:top-full", "data-[side=bottom]:bottom-full data-[side=bottom]:rotate-180", "data-[side=left]:left-[calc(100%-4px)] data-[side=left]:-rotate-90", "data-[side=right]:right-[calc(100%-4px)] data-[side=right]:rotate-90");
|
|
557
|
+
/** One side of the tail and back, along the middle of the bubble's border line: out of it flat, to a point. */
|
|
558
|
+
const TAIL_EDGE = `M0 0.5C5 0.5 8 5 10 ${TAIL.length}C12 5 15 0.5 ${TAIL.base} 0.5`;
|
|
559
|
+
function BubbleTail() {
|
|
560
|
+
const clip = `tour-tail-${React.useId().replace(/[^\w-]/g, "")}`;
|
|
561
|
+
return (_jsxs("svg", { "aria-hidden": true, width: TAIL.base, height: TAIL.length, viewBox: `0 0 ${TAIL.base} ${TAIL.length}`, className: "block overflow-visible", children: [_jsx("clipPath", { id: clip, children: _jsx("rect", { x: -4, y: 2, width: TAIL.base + 8, height: TAIL.length + 4 }) }), _jsx("path", { d: TAIL_EDGE, fill: "none", stroke: "var(--control-edge)", strokeWidth: 3, strokeLinejoin: "round", clipPath: `url(#${clip})` }), _jsx("path", { d: `${TAIL_EDGE}V-1H0Z`, fill: "var(--control-menu)" }), _jsx("path", { d: TAIL_EDGE, fill: "none", stroke: "var(--field-edge)", strokeWidth: 1, strokeLinejoin: "round" })] }));
|
|
562
|
+
}
|
|
563
|
+
/* ── Spotlight ─────────────────────────────────────────────────────────────── */
|
|
564
|
+
/** A rectangle with rounded corners, drawn the other way round so it cuts a hole (evenodd). */
|
|
565
|
+
function hole(x, y, w, h, r) {
|
|
566
|
+
const k = Math.max(0, Math.min(r, w / 2, h / 2));
|
|
567
|
+
return (`M${x + k} ${y}H${x + w - k}A${k} ${k} 0 0 1 ${x + w} ${y + k}V${y + h - k}` +
|
|
568
|
+
`A${k} ${k} 0 0 1 ${x + w - k} ${y + h}H${x + k}A${k} ${k} 0 0 1 ${x} ${y + h - k}` +
|
|
569
|
+
`V${y + k}A${k} ${k} 0 0 1 ${x + k} ${y}Z`);
|
|
570
|
+
}
|
|
571
|
+
const mix = (a, b, t) => ({
|
|
572
|
+
x: a.x + (b.x - a.x) * t,
|
|
573
|
+
y: a.y + (b.y - a.y) * t,
|
|
574
|
+
w: a.w + (b.w - a.w) * t,
|
|
575
|
+
h: a.h + (b.h - a.h) * t,
|
|
576
|
+
r: a.r + (b.r - a.r) * t,
|
|
577
|
+
});
|
|
578
|
+
const round = (n) => Math.round(n * 10) / 10;
|
|
579
|
+
/**
|
|
580
|
+
* The page dimmed around the target, as the backdrop of a dialog is. When the target changes, the
|
|
581
|
+
* hole moves from the last one to it on the glide's clock and curve, changing its size and its
|
|
582
|
+
* corners on the way (a step without a target closes it to the middle of the screen), drawn every
|
|
583
|
+
* frame while it moves. At rest nothing runs: it is drawn again, once in the next frame, when the
|
|
584
|
+
* page scrolls or resizes or the target or the page changes size — the events the popover follows
|
|
585
|
+
* too. The dim takes clicks; the hole lets them through to the target.
|
|
586
|
+
*/
|
|
587
|
+
function Spotlight({ anchor, closing, onClosed, }) {
|
|
588
|
+
const svgRef = React.useRef(null);
|
|
589
|
+
const pathRef = React.useRef(null);
|
|
590
|
+
/** The hole as it was last drawn, to move on from. */
|
|
591
|
+
const drawn = React.useRef(null);
|
|
592
|
+
const closed = React.useRef(onClosed);
|
|
593
|
+
closed.current = onClosed;
|
|
594
|
+
React.useEffect(() => {
|
|
595
|
+
const path = pathRef.current;
|
|
596
|
+
const svg = svgRef.current;
|
|
597
|
+
if (!path || !svg)
|
|
598
|
+
return;
|
|
599
|
+
const radius = anchor ? parseFloat(getComputedStyle(anchor).borderTopLeftRadius) || 0 : 0;
|
|
600
|
+
const from = drawn.current;
|
|
601
|
+
const ms = glideMs(svg);
|
|
602
|
+
const ease = bezier(easeOf(svg));
|
|
603
|
+
const start = performance.now();
|
|
604
|
+
let last = "";
|
|
605
|
+
let frame = 0;
|
|
606
|
+
let gliding = !!from && ms > 0;
|
|
607
|
+
const draw = (now) => {
|
|
608
|
+
frame = 0;
|
|
609
|
+
const w = window.innerWidth;
|
|
610
|
+
const h = window.innerHeight;
|
|
611
|
+
const p = SPOTLIGHT_PADDING;
|
|
612
|
+
let to;
|
|
613
|
+
if (anchor) {
|
|
614
|
+
const r = anchor.getBoundingClientRect();
|
|
615
|
+
to = { x: r.left - p, y: r.top - p, w: r.width + p * 2, h: r.height + p * 2, r: radius + p };
|
|
616
|
+
}
|
|
617
|
+
else
|
|
618
|
+
to = { x: w / 2, y: h / 2, w: 0, h: 0, r: 0 };
|
|
619
|
+
const t = gliding ? ease((now - start) / ms) : 1;
|
|
620
|
+
const box = t < 1 && from ? mix(from, to, t) : to;
|
|
621
|
+
if (t >= 1)
|
|
622
|
+
gliding = false;
|
|
623
|
+
drawn.current = box;
|
|
624
|
+
let d = `M0 0H${w}V${h}H0Z`;
|
|
625
|
+
if (box.w >= 1 && box.h >= 1)
|
|
626
|
+
d += hole(round(box.x), round(box.y), round(box.w), round(box.h), round(box.r));
|
|
627
|
+
if (d !== last) {
|
|
628
|
+
path.setAttribute("d", d);
|
|
629
|
+
last = d;
|
|
630
|
+
}
|
|
631
|
+
if (gliding)
|
|
632
|
+
frame = requestAnimationFrame(draw);
|
|
633
|
+
};
|
|
634
|
+
/** Draw again in the next frame, once however many events ask for it. */
|
|
635
|
+
const redraw = () => {
|
|
636
|
+
if (!frame)
|
|
637
|
+
frame = requestAnimationFrame(draw);
|
|
638
|
+
};
|
|
639
|
+
draw(start);
|
|
640
|
+
// Scrolls of any scroller (capture), the window's size, the target's and the page's.
|
|
641
|
+
window.addEventListener("scroll", redraw, { capture: true, passive: true });
|
|
642
|
+
window.addEventListener("resize", redraw);
|
|
643
|
+
const sizes = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(redraw);
|
|
644
|
+
if (anchor)
|
|
645
|
+
sizes?.observe(anchor);
|
|
646
|
+
sizes?.observe(document.documentElement);
|
|
647
|
+
return () => {
|
|
648
|
+
cancelAnimationFrame(frame);
|
|
649
|
+
window.removeEventListener("scroll", redraw, { capture: true });
|
|
650
|
+
window.removeEventListener("resize", redraw);
|
|
651
|
+
sizes?.disconnect();
|
|
652
|
+
};
|
|
653
|
+
}, [anchor]);
|
|
654
|
+
// Fading out as the tour ends, then off the page.
|
|
655
|
+
React.useEffect(() => {
|
|
656
|
+
const svg = svgRef.current;
|
|
657
|
+
if (!closing || !svg)
|
|
658
|
+
return;
|
|
659
|
+
const ms = cssDuration(svg);
|
|
660
|
+
if (!ms) {
|
|
661
|
+
closed.current();
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
const fade = svg.animate([{ opacity: 1 }, { opacity: 0 }], {
|
|
665
|
+
duration: ms,
|
|
666
|
+
easing: easeOf(svg),
|
|
667
|
+
fill: "forwards",
|
|
668
|
+
});
|
|
669
|
+
fade.onfinish = () => closed.current();
|
|
670
|
+
return () => fade.cancel();
|
|
671
|
+
}, [closing]);
|
|
672
|
+
return createPortal(_jsx("svg", { ref: svgRef, "aria-hidden": true, "data-slot": "tour-spotlight", "data-closing": closing ? "" : undefined, className: "pointer-events-none fixed inset-0 z-40 size-full transition-opacity duration-(--duration) ease-(--ease) starting:opacity-0", children: _jsx("path", { ref: pathRef, fillRule: "evenodd", className: cn("fill-black/40 dark:fill-black/60", !closing && "pointer-events-auto") }) }), document.body);
|
|
673
|
+
}
|