@sincpro/web-ui 2.2.0 → 2.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/README.md +113 -3
- package/dist/components/board/Board.js +1 -1
- package/dist/components/guide/Guide.d.ts +33 -0
- package/dist/components/guide/Guide.js +288 -0
- package/dist/components/guide/GuideSpot.d.ts +14 -0
- package/dist/components/guide/GuideSpot.js +13 -0
- package/dist/components/guide/index.d.ts +4 -0
- package/dist/components/guide/index.js +6 -0
- package/dist/components/guide/place.d.ts +44 -0
- package/dist/components/guide/place.js +69 -0
- package/dist/components/guide/target.d.ts +41 -0
- package/dist/components/guide/target.js +55 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +1 -0
- package/dist/components/listing/GroupingLevels.js +11 -9
- package/dist/components/listing/Listing.js +7 -5
- package/dist/components/listing/RecordCard.js +1 -1
- package/dist/components/listing/Row.js +8 -7
- package/dist/components/navigation/NavigationTree.js +36 -9
- package/dist/components/record/DirtyActions.d.ts +9 -7
- package/dist/components/record/DirtyActions.js +14 -9
- package/dist/components/record/ThumbBar.js +2 -1
- package/dist/components/search/CriteriaBuilder.js +3 -2
- package/dist/components/search/CriteriaChips.js +6 -5
- package/dist/components/search/JoinWord.js +1 -1
- package/dist/components/search/SearchBar.js +41 -25
- package/dist/components/shell/AppFrame.d.ts +10 -1
- package/dist/components/shell/AppFrame.js +53 -10
- package/dist/components/shell/SearchEntry.d.ts +29 -0
- package/dist/components/shell/SearchEntry.js +34 -0
- package/dist/components/shell/Sidebar.d.ts +12 -3
- package/dist/components/shell/Sidebar.js +38 -12
- package/dist/components/shell/TopBar.d.ts +12 -1
- package/dist/components/shell/TopBar.js +7 -5
- package/dist/components/shell/brandMark.d.ts +17 -0
- package/dist/components/shell/brandMark.js +9 -0
- package/dist/components/shell/index.d.ts +2 -0
- package/dist/components/shell/index.js +5 -0
- package/dist/foundations/copy.d.ts +19 -2
- package/dist/foundations/copy.es.js +13 -3
- package/dist/foundations/copy.js +13 -3
- package/dist/foundations/index.d.ts +1 -0
- package/dist/foundations/index.js +2 -0
- package/dist/foundations/shortcut.d.ts +18 -0
- package/dist/foundations/shortcut.js +20 -0
- package/dist/foundations/variants.js +19 -2
- package/dist/primitives/block/Block.js +1 -1
- package/dist/primitives/button/Button.d.ts +3 -1
- package/dist/primitives/button/Button.js +6 -6
- package/dist/primitives/choice/SegmentedControl.d.ts +4 -1
- package/dist/primitives/choice/SegmentedControl.js +35 -7
- package/dist/primitives/dialog/Dialog.d.ts +1 -1
- package/dist/primitives/dialog/Dialog.js +2 -2
- package/dist/primitives/field/CardNumberInput.js +1 -1
- package/dist/primitives/field/Checks.d.ts +14 -0
- package/dist/primitives/field/Checks.js +12 -4
- package/dist/primitives/field/DurationInput.js +1 -1
- package/dist/primitives/field/Field.js +7 -7
- package/dist/primitives/field/PatternInput.js +1 -1
- package/dist/primitives/field/RichTextEditor.js +2 -2
- package/dist/primitives/field/SignatureInput.js +1 -1
- package/dist/primitives/field/aria.d.ts +2 -2
- package/dist/primitives/field/aria.js +5 -5
- package/dist/primitives/field/index.d.ts +1 -1
- package/dist/primitives/field/index.js +1 -1
- package/dist/primitives/menu/Menu.d.ts +23 -7
- package/dist/primitives/menu/Menu.js +76 -22
- package/dist/primitives/overlay/Popover.js +2 -2
- package/dist/primitives/picker/Picker.js +3 -3
- package/dist/primitives/picker/TagsField.js +1 -1
- package/dist/primitives/table/EditableTable.js +2 -2
- package/dist/primitives/table/ReportTable.js +1 -1
- package/dist/primitives/table/Table.js +2 -2
- package/dist/primitives/tag/Tag.d.ts +9 -0
- package/dist/primitives/tag/Tag.js +14 -1
- package/dist/primitives/tag/Tags.js +6 -7
- package/dist/primitives/value/Metric.d.ts +5 -1
- package/dist/primitives/value/Metric.js +8 -6
- package/dist/tokens.css +202 -56
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -145,19 +145,129 @@ on `:root`, and every palette — light, dark, and a light subtree inside a dark
|
|
|
145
145
|
--brand-chrome: #10203f; /* the application bar */
|
|
146
146
|
--brand-chrome-raised: #1a2c52;
|
|
147
147
|
--brand-chrome-edge: #10203f;
|
|
148
|
+
--brand-ground: #f7f8fa; /* the page's paper */
|
|
149
|
+
--brand-surface: #ffffff; /* raised boxes, menus */
|
|
148
150
|
|
|
149
151
|
--brand-dark-accent: #e0b850; /* the same, for the dark palette */
|
|
150
152
|
--brand-dark-chrome: #0a1428;
|
|
153
|
+
--brand-dark-ground: #121417;
|
|
154
|
+
|
|
155
|
+
--brand-radius: 0.5; /* the corners: 0 square everywhere · 1 the system's · 1.5 rounder */
|
|
151
156
|
}
|
|
152
157
|
```
|
|
153
158
|
|
|
154
|
-
The full list is `
|
|
155
|
-
`
|
|
156
|
-
|
|
159
|
+
The full list is `ground`, `surface`, `line`, `line-strong`, `ink`, `muted`, `accent`,
|
|
160
|
+
`accent-hover`, `on-accent`, `primary`, `primary-hover`, `chrome`, `chrome-raised`, `chrome-edge`,
|
|
161
|
+
`chrome-accent` and `chrome-link`; `--brand-<token>` is the light palette's and
|
|
162
|
+
`--brand-dark-<token>` the dark's. One left unset keeps the system's value. Do not
|
|
157
163
|
override `--color-*` directly: those are redefined per theme, so an override on `:root` loses to
|
|
158
164
|
`[data-theme="light"]` and leaks into the dark palette. The system measures its own contrasts;
|
|
159
165
|
your brand's are checked by Storybook's a11y pass if you render your stories with it.
|
|
160
166
|
|
|
167
|
+
The corners are one knob: `--brand-radius` multiplies the whole radius scale (`mark`, `control`,
|
|
168
|
+
`box`, `panel`, `pill`), so the steps keep their proportion and a dropdown stays concentric with
|
|
169
|
+
its rows. `0` squares everything, the capsules too. A single step can still be set on its own —
|
|
170
|
+
`--brand-radius-control: 6px`. Both go on `:root`.
|
|
171
|
+
|
|
172
|
+
#### Customizing a component
|
|
173
|
+
|
|
174
|
+
The brand changes the whole product. To change **one component** — square buttons for a
|
|
175
|
+
till, a cooler menu, cards without a border — set its `--ui-*` tokens, on `:root` for the
|
|
176
|
+
whole application or on any element for a zone. Each one falls back to the system's token, so
|
|
177
|
+
setting nothing changes nothing, and each resolves on the element, so the dark theme still
|
|
178
|
+
follows:
|
|
179
|
+
|
|
180
|
+
```css
|
|
181
|
+
:root {
|
|
182
|
+
--ui-button-radius: 0;
|
|
183
|
+
--ui-popover-bg: var(--color-ground);
|
|
184
|
+
}
|
|
185
|
+
.till {
|
|
186
|
+
--ui-control-radius: 0; /* only inside this zone */
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
| Component | Tokens |
|
|
191
|
+
| ----------------------------------------------------------- | -------------------------------------------------------------------------------------- |
|
|
192
|
+
| `Button`, `ButtonLink` | `--ui-button-radius` |
|
|
193
|
+
| `IconButton`, `IconLink` | `--ui-icon-button-radius` |
|
|
194
|
+
| Inputs, selects, date and pickers' boxes | `--ui-control-radius`, `--ui-control-bg`, `--ui-control-border` |
|
|
195
|
+
| Menus, lists, popovers, the cuts panel | `--ui-popover-radius`, `--ui-popover-bg`, `--ui-popover-border`, `--ui-popover-shadow` |
|
|
196
|
+
| A row inside them (menu item, option) | `--ui-option-radius`, `--ui-option-active-bg` |
|
|
197
|
+
| `Dialog` | `--ui-dialog-radius`, `--ui-dialog-bg` |
|
|
198
|
+
| `Block`, `RecordCard`, `BoardTile`, `Metric` | `--ui-card-radius`, `--ui-card-bg`, `--ui-card-border` |
|
|
199
|
+
| `Tag`, `Tags`, the search bar's chips | `--ui-tag-radius` |
|
|
200
|
+
| `SearchBar`'s field | `--ui-search-radius` (and the control's bg and border) |
|
|
201
|
+
| The sidebar on the canvas | `--ui-sidebar-bg`, `--ui-sidebar-border` |
|
|
202
|
+
| `NavigationTree` | `--ui-nav-radius`, `--ui-nav-current-bg` |
|
|
203
|
+
| Table headers (`DataTable`, `EditableTable`, `ReportTable`) | `--ui-table-head-bg` |
|
|
204
|
+
| `Guide` (a guided tour): its card and its veil | `--ui-guide-radius`, `--ui-guide-bg`, `--ui-guide-scrim` |
|
|
205
|
+
|
|
206
|
+
What a token does not cover, a **part** does. Every customizable piece names its root
|
|
207
|
+
`data-ui="<component>"` and its parts `data-ui="<component>.<part>"`, with its variant beside
|
|
208
|
+
(`data-emphasis`, `data-size`, `data-surface`, `data-tone`) — stable names for your CSS, instead
|
|
209
|
+
of classes that change with the next refactor:
|
|
210
|
+
|
|
211
|
+
```css
|
|
212
|
+
[data-ui="button"][data-emphasis="primary"] {
|
|
213
|
+
letter-spacing: 0.02em;
|
|
214
|
+
}
|
|
215
|
+
[data-ui="popover"] {
|
|
216
|
+
backdrop-filter: blur(8px);
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
The parts: `button`, `icon-button`, `control`, `popover`, `menu`, `menu.trigger`,
|
|
221
|
+
`menu.section`, `menu.item`, `menu.fold`, `dialog`, `block`, `record-card`, `board.tile`,
|
|
222
|
+
`metric`, `metric.detail`, `tag`, `tags.item`, `search`, `search.field`, `search.chip`,
|
|
223
|
+
`search.panel`, `search.suggestions`, `search-entry`, `search-entry.keys`, `table`,
|
|
224
|
+
`table.head`, `topbar`, `topbar.brand`, `topbar.menus`, `topbar.actions`, `topbar.user`,
|
|
225
|
+
`frame`, `frame.bar`, `frame.sidebar`, `frame.main`, `sidebar`, `sidebar.brand`,
|
|
226
|
+
`sidebar.user`, `sidebar.search`, `sidebar.nav`, `sidebar.account`, `navigation`,
|
|
227
|
+
`navigation.item`, `dirty.blocked`, `guide`, `guide.veil`, `guide.spot`, `guide.card`,
|
|
228
|
+
`guide.progress`, `guide.title`, `guide.body`, `guide.actions`. A part that is one of many
|
|
229
|
+
carries `data-key` too (`navigation.item`). Put your rules in a cascade layer above web-ui's, or
|
|
230
|
+
outside any layer.
|
|
231
|
+
|
|
232
|
+
#### A guided tour: the screen says what each thing is
|
|
233
|
+
|
|
234
|
+
`Guide` walks a person through a screen: the rest dims, the thing being explained is cut out,
|
|
235
|
+
and a card beside it names it. Its stops are the anchors above, so no component needs a prop to
|
|
236
|
+
be pointed at:
|
|
237
|
+
|
|
238
|
+
```tsx
|
|
239
|
+
<Guide
|
|
240
|
+
open={open}
|
|
241
|
+
label="Recorrido del portal"
|
|
242
|
+
steps={[
|
|
243
|
+
{ id: "hola", title: "Bienvenida", body: "Un minuto para ver dónde está cada cosa." },
|
|
244
|
+
{ id: "buscar", target: { part: "sidebar.search" }, placement: "right", title: "Buscar", body: "…" },
|
|
245
|
+
{ id: "cuotas", target: { part: "navigation.item", key: "socios.cuotas" }, title: "Cuotas", body: "…" },
|
|
246
|
+
{ id: "cifras", target: { spot: "cifras" }, title: "Las cifras de hoy", body: "…" },
|
|
247
|
+
]}
|
|
248
|
+
onClose={(reason, index) => {
|
|
249
|
+
setOpen(false);
|
|
250
|
+
markSeen(reason); // "finish" | "skip" | "dismiss" — remembering it is the application's
|
|
251
|
+
}}
|
|
252
|
+
/>
|
|
253
|
+
|
|
254
|
+
<GuideSpot id="cifras">
|
|
255
|
+
<Metrics>…</Metrics>
|
|
256
|
+
</GuideSpot>
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
- **A target** is a part (`{ part }`), one item of a collection (`{ part, key }`), anything wrapped
|
|
260
|
+
in `GuideSpot` or carrying `data-guide` (`{ spot }`), a selector, or a function returning an
|
|
261
|
+
element. A step without one is centred.
|
|
262
|
+
- **`before()`** runs first — navigate, open a tab — and the step waits for its target to appear.
|
|
263
|
+
A target that never appears is skipped; the tour does not break when the screen changes.
|
|
264
|
+
- **On a phone**, a stop inside the frame's sidebar opens its drawer, and the next stop outside
|
|
265
|
+
closes it. Render the `Guide` inside the `AppFrame`.
|
|
266
|
+
- It is a modal dialog: focus on «Siguiente» (Enter walks it), → and ←, Escape to leave, and
|
|
267
|
+
every step announced. `onStepChange` and `startAt` let an application save and resume it.
|
|
268
|
+
- `placeCard`, `findTarget`, `selectorOf` and `boxOf` are exported: a tour of your own follows the
|
|
269
|
+
same rules.
|
|
270
|
+
|
|
161
271
|
#### Living beside a site's own CSS
|
|
162
272
|
|
|
163
273
|
The package's styles are in cascade layers (`@layer base`, `utilities`), and **CSS outside any
|
|
@@ -31,7 +31,7 @@ const SPAN = {
|
|
|
31
31
|
/** One tile of a `Board`: a titled surface around a chart, a list or a number. */
|
|
32
32
|
export function BoardTile({ title, description, actions, span = 1, state = READY, children, }) {
|
|
33
33
|
const copy = useUiCopy();
|
|
34
|
-
return (_jsxs("section", { "aria-label": title, ...(state.status === "loading" ? { "aria-busy": true } : {}), className: `flex min-w-0 flex-col gap-4 rounded-
|
|
34
|
+
return (_jsxs("section", { "aria-label": title, ...(state.status === "loading" ? { "aria-busy": true } : {}), "data-ui": "board.tile", className: `flex min-w-0 flex-col gap-4 rounded-ui-card border border-line-strong bg-ui-card p-4 ${SPAN[span]}`, children: [_jsxs("header", { className: "flex min-w-0 items-start justify-between gap-3", children: [_jsxs("div", { className: "min-w-0", children: [_jsx("h2", { className: "text-lead font-semibold text-ink", children: title }), description === undefined ? null : (_jsx("p", { className: "mt-0.5 text-meta text-muted", children: description }))] }), actions === undefined ? null : (_jsx("div", { className: "flex shrink-0 items-center gap-1", children: actions }))] }), state.status === "loading" ? (
|
|
35
35
|
/* A quiet block where the chart goes. It appears late, like every skeleton: a fast
|
|
36
36
|
read never shows it. */
|
|
37
37
|
_jsx("div", { className: "min-h-(--size-tile-body) animate-reveal-late rounded-control bg-ink/4", children: _jsx("span", { className: "sr-only", children: copy.failed.loading }) })) : state.status === "failed" ? (_jsx("div", { className: "flex min-h-(--size-tile-body) flex-col justify-center", children: _jsx(Failure, { heading: fillCopy(copy.board.tileFailed, { title }), state: state }) })) : (children)] }));
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type GuideSide } from "./place.js";
|
|
2
|
+
import { type GuideTarget } from "./target.js";
|
|
3
|
+
import { type ReactNode } from "react";
|
|
4
|
+
export type GuideStep = {
|
|
5
|
+
id: string;
|
|
6
|
+
/** What it points at. Without it the card is centred: a welcome, a goodbye. */
|
|
7
|
+
target?: GuideTarget;
|
|
8
|
+
title: string;
|
|
9
|
+
/** What it explains: text, a list, an image, a button that tries the thing. */
|
|
10
|
+
body: ReactNode;
|
|
11
|
+
/** The side of the target the card prefers; it flips when there is no room. `bottom` by
|
|
12
|
+
* default. */
|
|
13
|
+
placement?: GuideSide;
|
|
14
|
+
/** Runs before the step looks for its target: navigate, open a tab, expand a section. The
|
|
15
|
+
* tour waits for it, then for the target to appear. */
|
|
16
|
+
before?: () => void | Promise<void>;
|
|
17
|
+
};
|
|
18
|
+
/** `finish`: the last step's button. `skip`: «Saltar el recorrido». `dismiss`: Escape. */
|
|
19
|
+
export type GuideCloseReason = "finish" | "skip" | "dismiss";
|
|
20
|
+
export type GuideProps = {
|
|
21
|
+
steps: readonly GuideStep[];
|
|
22
|
+
open: boolean;
|
|
23
|
+
/** The tour wants to close; the application sets `open` to false — and remembers, if it
|
|
24
|
+
* wants to, that this person has seen it. */
|
|
25
|
+
onClose: (reason: GuideCloseReason, index: number) => void;
|
|
26
|
+
/** A step is on screen: to save how far someone got, to measure. */
|
|
27
|
+
onStepChange?: (index: number, step: GuideStep) => void;
|
|
28
|
+
/** Where it starts each time it opens — to resume a tour left halfway. */
|
|
29
|
+
startAt?: number;
|
|
30
|
+
/** The tour's name, for a screen reader: «Recorrido del portal». */
|
|
31
|
+
label?: string;
|
|
32
|
+
};
|
|
33
|
+
export declare function Guide({ steps, open, onClose, onStepChange, startAt, label, }: GuideProps): import("react").JSX.Element | null;
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { placeCard, } from "./place.js";
|
|
4
|
+
import { boxOf, findTarget, scrollable, } from "./target.js";
|
|
5
|
+
import { useSidebarDrawer } from "../shell/sidebarDrawer.js";
|
|
6
|
+
import { fillCopy, useUiCopy } from "../../foundations/copy.js";
|
|
7
|
+
import { SurfaceProvider } from "../../foundations/surface.js";
|
|
8
|
+
import { Button } from "../../primitives/button/Button.js";
|
|
9
|
+
import { useEffect, useId, useRef, useState, } from "react";
|
|
10
|
+
import { Dialog, Modal, ModalOverlay } from "react-aria-components";
|
|
11
|
+
/** How long a step waits for its target to appear before it is skipped: a list that loads,
|
|
12
|
+
* a screen the step's `before()` navigated to. Long enough for those, short enough that a
|
|
13
|
+
* stop that is not there is not a wait. */
|
|
14
|
+
const WAIT_FOR_TARGET = 1500;
|
|
15
|
+
const SPOT_PADDING = 0.375; // rem — the cut-out's air around the target (`p-1.5`)
|
|
16
|
+
const CARD_GAP = 0.75; // rem — between the cut-out and the card
|
|
17
|
+
const WINDOW_MARGIN = 1; // rem — the card never comes closer to the window's edge
|
|
18
|
+
const nextFrame = () => new Promise((resolve) => requestAnimationFrame(() => resolve()));
|
|
19
|
+
const prefersLessMotion = () => window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
20
|
+
/** Scrolled into view only when it is not already whole in it: a tour that scrolls a page
|
|
21
|
+
* that was fine moves the very thing it is about to explain. */
|
|
22
|
+
function bringIntoView(element) {
|
|
23
|
+
const box = boxOf(element);
|
|
24
|
+
const whole = box !== null &&
|
|
25
|
+
box.top >= 0 &&
|
|
26
|
+
box.left >= 0 &&
|
|
27
|
+
box.top + box.height <= window.innerHeight &&
|
|
28
|
+
box.left + box.width <= window.innerWidth;
|
|
29
|
+
if (whole)
|
|
30
|
+
return;
|
|
31
|
+
scrollable(element).scrollIntoView({
|
|
32
|
+
block: "center",
|
|
33
|
+
inline: "nearest",
|
|
34
|
+
behavior: prefersLessMotion() ? "auto" : "smooth",
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
38
|
+
const rounded = (box) => box === null
|
|
39
|
+
? null
|
|
40
|
+
: {
|
|
41
|
+
top: Math.round(box.top),
|
|
42
|
+
left: Math.round(box.left),
|
|
43
|
+
width: Math.round(box.width),
|
|
44
|
+
height: Math.round(box.height),
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* The target's box, the card's size and the window's, measured every frame while the tour is
|
|
48
|
+
* open — and set only when they change.
|
|
49
|
+
*
|
|
50
|
+
* Context: the things a target does while a tour points at it have no single event: a page
|
|
51
|
+
* scrolled into place, a drawer sliding in (a transform), a font loading, a list re-rendering
|
|
52
|
+
* the node. One `getBoundingClientRect` per frame follows all of them, and costs nothing next
|
|
53
|
+
* to a frame. A node React replaced is found again by the same target.
|
|
54
|
+
*/
|
|
55
|
+
function useLayout(active, element, target, card) {
|
|
56
|
+
const [layout, setLayout] = useState({
|
|
57
|
+
target: null,
|
|
58
|
+
card: null,
|
|
59
|
+
view: { width: 0, height: 0 },
|
|
60
|
+
});
|
|
61
|
+
/* Read, not depended on: steps written inline give a new `target` object every render, and
|
|
62
|
+
restarting the loop on each would measure for ever. */
|
|
63
|
+
const latestTarget = useRef(target);
|
|
64
|
+
latestTarget.current = target;
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (!active)
|
|
67
|
+
return;
|
|
68
|
+
let current = element;
|
|
69
|
+
let frame = 0;
|
|
70
|
+
const tick = () => {
|
|
71
|
+
const target = latestTarget.current;
|
|
72
|
+
if (current !== null && !current.isConnected && target !== undefined) {
|
|
73
|
+
current = findTarget(target).shown;
|
|
74
|
+
}
|
|
75
|
+
const node = card.current;
|
|
76
|
+
const next = {
|
|
77
|
+
target: current === null ? null : rounded(boxOf(current)),
|
|
78
|
+
card: node === null ? null : { width: node.offsetWidth, height: node.offsetHeight },
|
|
79
|
+
view: { width: window.innerWidth, height: window.innerHeight },
|
|
80
|
+
};
|
|
81
|
+
setLayout((previous) => (same(previous, next) ? previous : next));
|
|
82
|
+
frame = requestAnimationFrame(tick);
|
|
83
|
+
};
|
|
84
|
+
tick();
|
|
85
|
+
return () => cancelAnimationFrame(frame);
|
|
86
|
+
}, [active, element, card]);
|
|
87
|
+
return layout;
|
|
88
|
+
}
|
|
89
|
+
export function Guide({ steps, open, onClose, onStepChange, startAt = 0, label, }) {
|
|
90
|
+
const copy = useUiCopy();
|
|
91
|
+
const drawer = useSidebarDrawer();
|
|
92
|
+
const titleId = useId();
|
|
93
|
+
const bodyId = useId();
|
|
94
|
+
const card = useRef(null);
|
|
95
|
+
const [request, setRequest] = useState(null);
|
|
96
|
+
const [shown, setShown] = useState(null);
|
|
97
|
+
/* Bumped when the tour opens the drawer: the tour's own overlay mounts again ABOVE it, or
|
|
98
|
+
the drawer — the newest modal — would make the card inert. */
|
|
99
|
+
const [layer, setLayer] = useState(0);
|
|
100
|
+
/* The latest props, read by the async step without restarting it: an application that
|
|
101
|
+
writes its `steps` inline gives a new array every render. */
|
|
102
|
+
const latest = useRef({ steps, onClose, onStepChange, drawer });
|
|
103
|
+
latest.current = { steps, onClose, onStepChange, drawer };
|
|
104
|
+
const openedDrawer = useRef(false);
|
|
105
|
+
const returnTo = useRef(null);
|
|
106
|
+
/* Open: from `startAt`, remembering where the focus was. Closed: the drawer the tour opened
|
|
107
|
+
closes, and the focus goes back — a tour that went through the drawer remounted its
|
|
108
|
+
overlay, and the overlay's own memory of «where the focus was» went with it. */
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
if (!open)
|
|
111
|
+
return;
|
|
112
|
+
returnTo.current =
|
|
113
|
+
document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
114
|
+
const first = Math.min(Math.max(startAt, 0), Math.max(latest.current.steps.length - 1, 0));
|
|
115
|
+
setRequest({ index: first, dir: 1 });
|
|
116
|
+
return () => {
|
|
117
|
+
setRequest(null);
|
|
118
|
+
setShown(null);
|
|
119
|
+
if (openedDrawer.current)
|
|
120
|
+
latest.current.drawer?.close();
|
|
121
|
+
openedDrawer.current = false;
|
|
122
|
+
const back = returnTo.current;
|
|
123
|
+
requestAnimationFrame(() => {
|
|
124
|
+
const lost = document.activeElement === null || document.activeElement === document.body;
|
|
125
|
+
if (lost && back?.isConnected === true)
|
|
126
|
+
back.focus();
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
}, [open, startAt]);
|
|
130
|
+
const close = (reason) => latest.current.onClose(reason, shown?.index ?? request?.index ?? 0);
|
|
131
|
+
/* One step: run its `before`, wait for its target, open or close the drawer for it, bring
|
|
132
|
+
it into view — and only then show it, so the card never names one thing while the veil
|
|
133
|
+
cuts out another. */
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
if (!open || request === null)
|
|
136
|
+
return;
|
|
137
|
+
let cancelled = false;
|
|
138
|
+
const { steps: all } = latest.current;
|
|
139
|
+
const step = all[request.index];
|
|
140
|
+
if (step === undefined)
|
|
141
|
+
return;
|
|
142
|
+
const drawerElement = () => {
|
|
143
|
+
const id = latest.current.drawer?.id;
|
|
144
|
+
return id === undefined ? null : document.getElementById(id);
|
|
145
|
+
};
|
|
146
|
+
const seek = async (target) => {
|
|
147
|
+
const started = performance.now();
|
|
148
|
+
let openedHere = false;
|
|
149
|
+
while (!cancelled) {
|
|
150
|
+
const { shown: found, hidden } = findTarget(target);
|
|
151
|
+
const sheet = latest.current.drawer;
|
|
152
|
+
if (found !== null) {
|
|
153
|
+
const inDrawer = drawerElement()?.contains(found) === true;
|
|
154
|
+
if (!inDrawer && openedDrawer.current && sheet?.isOpen === true) {
|
|
155
|
+
sheet.close();
|
|
156
|
+
openedDrawer.current = false;
|
|
157
|
+
}
|
|
158
|
+
if (openedHere)
|
|
159
|
+
setLayer((n) => n + 1);
|
|
160
|
+
return found;
|
|
161
|
+
}
|
|
162
|
+
const inSidebar = hidden?.closest('[data-ui="frame.sidebar"]') != null;
|
|
163
|
+
if (inSidebar && sheet !== null && !sheet.isOpen && !openedHere) {
|
|
164
|
+
sheet.open();
|
|
165
|
+
openedDrawer.current = true;
|
|
166
|
+
openedHere = true;
|
|
167
|
+
}
|
|
168
|
+
if (performance.now() - started > WAIT_FOR_TARGET)
|
|
169
|
+
return null;
|
|
170
|
+
await nextFrame();
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
};
|
|
174
|
+
/* The step's target is not there: on to the next one in the direction of travel. Past
|
|
175
|
+
the last, the tour is done; before the first, nothing earlier is on screen, so it
|
|
176
|
+
stays where the person is. */
|
|
177
|
+
const skip = () => {
|
|
178
|
+
const next = request.index + request.dir;
|
|
179
|
+
if (next >= 0 && next < all.length) {
|
|
180
|
+
setRequest({ index: next, dir: request.dir });
|
|
181
|
+
}
|
|
182
|
+
else if (request.dir === 1) {
|
|
183
|
+
latest.current.onClose("finish", request.index);
|
|
184
|
+
}
|
|
185
|
+
else if (shown !== null) {
|
|
186
|
+
setRequest({ index: shown.index, dir: 1 });
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
void (async () => {
|
|
190
|
+
try {
|
|
191
|
+
await step.before?.();
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
if (!cancelled)
|
|
195
|
+
skip();
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (cancelled)
|
|
199
|
+
return;
|
|
200
|
+
const element = step.target === undefined ? null : await seek(step.target);
|
|
201
|
+
if (cancelled)
|
|
202
|
+
return;
|
|
203
|
+
if (step.target !== undefined && element === null) {
|
|
204
|
+
skip();
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (element !== null)
|
|
208
|
+
bringIntoView(element);
|
|
209
|
+
setShown({ index: request.index, element });
|
|
210
|
+
latest.current.onStepChange?.(request.index, step);
|
|
211
|
+
})();
|
|
212
|
+
return () => {
|
|
213
|
+
cancelled = true;
|
|
214
|
+
};
|
|
215
|
+
// `shown` is read for a skip backwards only; the step must not restart when it lands.
|
|
216
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
217
|
+
}, [open, request]);
|
|
218
|
+
/* The focus on the button that walks the tour, on every step: Enter, Enter, Enter. */
|
|
219
|
+
useEffect(() => {
|
|
220
|
+
if (shown === null)
|
|
221
|
+
return;
|
|
222
|
+
const frame = requestAnimationFrame(() => card.current?.querySelector('[data-emphasis="primary"]')?.focus());
|
|
223
|
+
return () => cancelAnimationFrame(frame);
|
|
224
|
+
}, [shown, layer]);
|
|
225
|
+
const step = shown === null ? undefined : steps[shown.index];
|
|
226
|
+
const layout = useLayout(open && shown !== null, shown?.element ?? null, step?.target, card);
|
|
227
|
+
if (!open || shown === null || step === undefined)
|
|
228
|
+
return null;
|
|
229
|
+
const index = shown.index;
|
|
230
|
+
const first = index === 0;
|
|
231
|
+
const last = index === steps.length - 1;
|
|
232
|
+
const go = (dir) => setRequest({ index: index + dir, dir });
|
|
233
|
+
const progress = fillCopy(copy.guide.progress, {
|
|
234
|
+
current: String(index + 1),
|
|
235
|
+
total: String(steps.length),
|
|
236
|
+
});
|
|
237
|
+
const rem = parseFloat(getComputedStyle(document.documentElement).fontSize) || 16;
|
|
238
|
+
const pad = SPOT_PADDING * rem;
|
|
239
|
+
const spot = layout.target === null
|
|
240
|
+
? null
|
|
241
|
+
: {
|
|
242
|
+
top: layout.target.top - pad,
|
|
243
|
+
left: layout.target.left - pad,
|
|
244
|
+
width: layout.target.width + 2 * pad,
|
|
245
|
+
height: layout.target.height + 2 * pad,
|
|
246
|
+
};
|
|
247
|
+
const place = layout.card === null
|
|
248
|
+
? null
|
|
249
|
+
: placeCard({
|
|
250
|
+
target: spot,
|
|
251
|
+
card: layout.card,
|
|
252
|
+
view: layout.view,
|
|
253
|
+
prefer: step.placement ?? "bottom",
|
|
254
|
+
gap: CARD_GAP * rem,
|
|
255
|
+
margin: WINDOW_MARGIN * rem,
|
|
256
|
+
});
|
|
257
|
+
const onKeyDown = (event) => {
|
|
258
|
+
if (event.target instanceof HTMLInputElement ||
|
|
259
|
+
event.target instanceof HTMLTextAreaElement)
|
|
260
|
+
return;
|
|
261
|
+
const rtl = getComputedStyle(event.currentTarget).direction === "rtl";
|
|
262
|
+
const forward = rtl ? "ArrowLeft" : "ArrowRight";
|
|
263
|
+
const backward = rtl ? "ArrowRight" : "ArrowLeft";
|
|
264
|
+
if (event.key === forward) {
|
|
265
|
+
event.preventDefault();
|
|
266
|
+
if (last)
|
|
267
|
+
close("finish");
|
|
268
|
+
else
|
|
269
|
+
go(1);
|
|
270
|
+
}
|
|
271
|
+
else if (event.key === backward && !first) {
|
|
272
|
+
event.preventDefault();
|
|
273
|
+
go(-1);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
return (_jsxs(ModalOverlay, { isOpen: true, isDismissable: false, onOpenChange: (next) => {
|
|
277
|
+
if (!next)
|
|
278
|
+
close("dismiss");
|
|
279
|
+
}, className: "fixed inset-0 z-60 print:hidden", "data-ui": "guide", children: [spot === null ? (_jsx("div", { "aria-hidden": true, "data-ui": "guide.veil", className: "absolute inset-0 bg-ui-guide-scrim" })) : (
|
|
280
|
+
/* The veil IS the cut-out's shadow: one box around the target throws a spread as
|
|
281
|
+
wide as the window, so the hole is exact, rounded, and glides from one target to
|
|
282
|
+
the next. */
|
|
283
|
+
_jsx("div", { "aria-hidden": true, "data-ui": "guide.spot", className: "pointer-events-none fixed rounded-box text-ui-guide-scrim shadow-guide-veil outline-2 outline-surface transition-[top,left,width,height] duration-(--duration-panel) ease-(--ease-out)", style: spot })), _jsx(Modal, { className: "fixed w-(--container-guide) outline-0 transition-[top,left] duration-(--duration-panel) ease-(--ease-out)", style: place === null
|
|
284
|
+
? { top: 0, left: 0, visibility: "hidden" }
|
|
285
|
+
: { top: place.top, left: place.left }, children: _jsx(Dialog, { ref: (node) => {
|
|
286
|
+
card.current = node;
|
|
287
|
+
}, "aria-label": label ?? copy.guide.label, "aria-describedby": `${titleId} ${bodyId}`, "data-ui": "guide.card", "data-side": place?.side, className: "rounded-ui-guide border border-line-strong bg-ui-guide p-4 text-ink shadow-window outline-0", children: _jsx(SurfaceProvider, { value: "canvas", children: _jsxs("div", { role: "presentation", onKeyDown: onKeyDown, className: "flex flex-col gap-2", children: [_jsx("p", { "data-ui": "guide.progress", className: "text-micro tabular-nums text-muted", children: progress }), _jsx("h2", { id: titleId, "data-ui": "guide.title", className: "text-lead font-semibold", children: step.title }), _jsx("div", { id: bodyId, "data-ui": "guide.body", className: "text-meta", children: step.body }), _jsxs("div", { "data-ui": "guide.actions", className: "flex items-center gap-2 pt-2", children: [last ? null : (_jsx(Button, { role: "ghost", size: "sm", onClick: () => close("skip"), children: copy.guide.skip })), _jsxs("span", { className: "ms-auto flex items-center gap-2", children: [first ? null : (_jsx(Button, { size: "sm", onClick: () => go(-1), children: copy.guide.back })), _jsx(Button, { role: "primary", size: "sm", onClick: () => (last ? close("finish") : go(1)), children: last ? copy.guide.finish : copy.guide.next })] })] }), _jsx("p", { className: "sr-only", "aria-live": "polite", children: fillCopy(copy.guide.step, { progress, title: step.title }) })] }) }) }) })] }, layer));
|
|
288
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Marks anything as a stop of a tour — `{ spot: "cifras" }` — without changing its layout.
|
|
4
|
+
*
|
|
5
|
+
* Context: a tour has to point at things the application composes — a row of `Metrics`, a
|
|
6
|
+
* `Block`, a group of buttons — and none of them should grow a prop for it. This is a wrapper
|
|
7
|
+
* with no box of its own (`display: contents`): inside a flex row or a grid its children stay
|
|
8
|
+
* exactly where they were, and the tour measures the union of their boxes. An element of the
|
|
9
|
+
* application's own can carry `data-guide` directly; this is for what it does not write.
|
|
10
|
+
*/
|
|
11
|
+
export declare function GuideSpot({ id, children }: {
|
|
12
|
+
id: string;
|
|
13
|
+
children: ReactNode;
|
|
14
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* Marks anything as a stop of a tour — `{ spot: "cifras" }` — without changing its layout.
|
|
4
|
+
*
|
|
5
|
+
* Context: a tour has to point at things the application composes — a row of `Metrics`, a
|
|
6
|
+
* `Block`, a group of buttons — and none of them should grow a prop for it. This is a wrapper
|
|
7
|
+
* with no box of its own (`display: contents`): inside a flex row or a grid its children stay
|
|
8
|
+
* exactly where they were, and the tour measures the union of their boxes. An element of the
|
|
9
|
+
* application's own can carry `data-guide` directly; this is for what it does not write.
|
|
10
|
+
*/
|
|
11
|
+
export function GuideSpot({ id, children }) {
|
|
12
|
+
return (_jsx("span", { "data-guide": id, className: "contents", children: children }));
|
|
13
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { Guide, type GuideCloseReason, type GuideProps, type GuideStep, } from "./Guide.js";
|
|
2
|
+
export { GuideSpot } from "./GuideSpot.js";
|
|
3
|
+
export { type Box as GuideBox, type CardPlacement as GuideCardPlacement, type GuideSide, type Size as GuideSize, placeCard, } from "./place.js";
|
|
4
|
+
export { boxOf, findTarget, type Found as GuideMatch, type GuideTarget, selectorOf, } from "./target.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/* A GUIDED TOUR — the screen explains what each thing is, where it is. */
|
|
2
|
+
export { Guide, } from "./Guide.js";
|
|
3
|
+
export { GuideSpot } from "./GuideSpot.js";
|
|
4
|
+
/* The rules underneath, for an application that draws a tour of its own. */
|
|
5
|
+
export { placeCard, } from "./place.js";
|
|
6
|
+
export { boxOf, findTarget, selectorOf, } from "./target.js";
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a tour's card goes, beside what it points at.
|
|
3
|
+
*
|
|
4
|
+
* Context: pure on purpose. The tour measures — the target's box, the card's size, the
|
|
5
|
+
* window — and this decides, so the one part of a tour that is easy to get subtly wrong (a
|
|
6
|
+
* card under the fold, a card covering what it explains, a card off the left edge on a
|
|
7
|
+
* phone) is held by tests that need no browser.
|
|
8
|
+
*
|
|
9
|
+
* 1. No target: the card is centred — a welcome, a goodbye.
|
|
10
|
+
* 2. The preferred side, then its opposite, then the other two: the first one where the
|
|
11
|
+
* whole card fits between the target and the window's margin wins.
|
|
12
|
+
* 3. Along that side the card is centred on the target, and clamped inside the margins:
|
|
13
|
+
* a target at the window's edge keeps its card in view.
|
|
14
|
+
* Final: no side fits (the target is most of the window — a whole list): the card goes to
|
|
15
|
+
* the bottom of the window, over the target, where it covers the least of its top.
|
|
16
|
+
*/
|
|
17
|
+
export type GuideSide = "top" | "bottom" | "left" | "right";
|
|
18
|
+
export type Box = {
|
|
19
|
+
top: number;
|
|
20
|
+
left: number;
|
|
21
|
+
width: number;
|
|
22
|
+
height: number;
|
|
23
|
+
};
|
|
24
|
+
export type Size = {
|
|
25
|
+
width: number;
|
|
26
|
+
height: number;
|
|
27
|
+
};
|
|
28
|
+
export type CardPlacement = {
|
|
29
|
+
top: number;
|
|
30
|
+
left: number;
|
|
31
|
+
/** Which side of the target it ended on; `center` without a target, `over` when no side had
|
|
32
|
+
* room. */
|
|
33
|
+
side: GuideSide | "center" | "over";
|
|
34
|
+
};
|
|
35
|
+
export declare function placeCard({ target, card, view, prefer, gap, margin, }: {
|
|
36
|
+
target: Box | null;
|
|
37
|
+
card: Size;
|
|
38
|
+
view: Size;
|
|
39
|
+
prefer?: GuideSide;
|
|
40
|
+
/** Between the target and the card. */
|
|
41
|
+
gap: number;
|
|
42
|
+
/** The card never comes closer than this to the window's edge. */
|
|
43
|
+
margin: number;
|
|
44
|
+
}): CardPlacement;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a tour's card goes, beside what it points at.
|
|
3
|
+
*
|
|
4
|
+
* Context: pure on purpose. The tour measures — the target's box, the card's size, the
|
|
5
|
+
* window — and this decides, so the one part of a tour that is easy to get subtly wrong (a
|
|
6
|
+
* card under the fold, a card covering what it explains, a card off the left edge on a
|
|
7
|
+
* phone) is held by tests that need no browser.
|
|
8
|
+
*
|
|
9
|
+
* 1. No target: the card is centred — a welcome, a goodbye.
|
|
10
|
+
* 2. The preferred side, then its opposite, then the other two: the first one where the
|
|
11
|
+
* whole card fits between the target and the window's margin wins.
|
|
12
|
+
* 3. Along that side the card is centred on the target, and clamped inside the margins:
|
|
13
|
+
* a target at the window's edge keeps its card in view.
|
|
14
|
+
* Final: no side fits (the target is most of the window — a whole list): the card goes to
|
|
15
|
+
* the bottom of the window, over the target, where it covers the least of its top.
|
|
16
|
+
*/
|
|
17
|
+
const ORDER = {
|
|
18
|
+
bottom: ["bottom", "top", "right", "left"],
|
|
19
|
+
top: ["top", "bottom", "right", "left"],
|
|
20
|
+
right: ["right", "left", "bottom", "top"],
|
|
21
|
+
left: ["left", "right", "bottom", "top"],
|
|
22
|
+
};
|
|
23
|
+
/** Between `min` and `max`; when the card is wider than the room, its start edge wins — the
|
|
24
|
+
* title is what must stay readable. */
|
|
25
|
+
function clamp(value, min, max) {
|
|
26
|
+
return Math.max(min, Math.min(value, max));
|
|
27
|
+
}
|
|
28
|
+
function fits(side, target, card, view, gap, margin) {
|
|
29
|
+
switch (side) {
|
|
30
|
+
case "bottom":
|
|
31
|
+
return target.top + target.height + gap + card.height <= view.height - margin;
|
|
32
|
+
case "top":
|
|
33
|
+
return target.top - gap - card.height >= margin;
|
|
34
|
+
case "right":
|
|
35
|
+
return target.left + target.width + gap + card.width <= view.width - margin;
|
|
36
|
+
case "left":
|
|
37
|
+
return target.left - gap - card.width >= margin;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function placeCard({ target, card, view, prefer = "bottom", gap, margin, }) {
|
|
41
|
+
const across = (value) => clamp(value, margin, view.width - margin - card.width);
|
|
42
|
+
const down = (value) => clamp(value, margin, view.height - margin - card.height);
|
|
43
|
+
if (target === null) {
|
|
44
|
+
return {
|
|
45
|
+
top: down((view.height - card.height) / 2),
|
|
46
|
+
left: across((view.width - card.width) / 2),
|
|
47
|
+
side: "center",
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const middleX = target.left + target.width / 2 - card.width / 2;
|
|
51
|
+
const middleY = target.top + target.height / 2 - card.height / 2;
|
|
52
|
+
const side = ORDER[prefer].find((one) => fits(one, target, card, view, gap, margin));
|
|
53
|
+
switch (side) {
|
|
54
|
+
case "bottom":
|
|
55
|
+
return { top: target.top + target.height + gap, left: across(middleX), side };
|
|
56
|
+
case "top":
|
|
57
|
+
return { top: target.top - gap - card.height, left: across(middleX), side };
|
|
58
|
+
case "right":
|
|
59
|
+
return { top: down(middleY), left: target.left + target.width + gap, side };
|
|
60
|
+
case "left":
|
|
61
|
+
return { top: down(middleY), left: target.left - gap - card.width, side };
|
|
62
|
+
case undefined:
|
|
63
|
+
return {
|
|
64
|
+
top: down(view.height - margin - card.height),
|
|
65
|
+
left: across(middleX),
|
|
66
|
+
side: "over",
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|