@gemboss/ui 0.48.0 → 0.51.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 +457 -0
- package/README.md +280 -12
- package/catalog/editor.html +25 -12
- package/catalog/index.html +641 -99
- package/gemboss-ui.css +3 -3
- package/package.json +29 -2
- package/parts/components.css +3 -3
- package/parts/core.css +1 -1
- package/parts/shell.css +2 -2
- package/src/app-shell.css +1 -1
- package/src/assistant/chat.tsx +50 -6
- package/src/assistant/host.tsx +19 -7
- package/src/assistant/index.ts +1 -1
- package/src/assistant/sessions.tsx +2 -1
- package/src/assistant-chat.css +1 -1
- package/src/assistant.css +1 -1
- package/src/auth-extras.css +1 -0
- package/src/auth-footer.css +1 -0
- package/src/auth-footer.tsx +71 -0
- package/src/auth-signin-plain.js +1 -0
- package/src/auth-sso-plain.js +1 -1
- package/src/auth-steps.css +1 -0
- package/src/auth-steps.tsx +179 -0
- package/src/auth.css +1 -1
- package/src/auth.tsx +239 -14
- package/src/briefing-card.css +1 -1
- package/src/collections.css +1 -1
- package/src/create-shell.css +1 -0
- package/src/create-shell.tsx +221 -0
- package/src/dialog.tsx +3 -0
- package/src/editor/color-picker.tsx +183 -0
- package/src/editor/controls.tsx +49 -33
- package/src/editor/index.ts +8 -0
- package/src/editor/picker-grid.tsx +186 -0
- package/src/editor/picker.tsx +275 -0
- package/src/editor/popover.tsx +160 -0
- package/src/editor/swatch.ts +2 -1
- package/src/editor-color.css +1 -0
- package/src/editor-insert.css +1 -0
- package/src/editor-picker-grid.css +1 -0
- package/src/editor-picker.css +1 -0
- package/src/editor-plain.js +1 -1
- package/src/editor-popover.css +1 -0
- package/src/editor.css +1 -1
- package/src/empty-stage.css +1 -0
- package/src/empty-stage.tsx +31 -0
- package/src/icons.tsx +6 -0
- package/src/index-table.tsx +7 -4
- package/src/index.ts +19 -2
- package/src/kit-plain.css +1 -1
- package/src/kit-plain.js +1 -1
- package/src/language-switcher.tsx +10 -41
- package/src/list-page.css +1 -0
- package/src/list-page.tsx +239 -0
- package/src/menu-extras.css +1 -0
- package/src/menu-keys.ts +199 -0
- package/src/menu-layer.ts +2 -1
- package/src/menu-plain.js +1 -0
- package/src/menu.css +1 -0
- package/src/menu.tsx +83 -35
- package/src/pager-extras.css +1 -0
- package/src/pager.css +1 -0
- package/src/pager.tsx +88 -0
- package/src/portal.css +1 -1
- package/src/segmented.tsx +12 -2
- package/src/selection.tsx +3 -1
- package/src/session-rail-plain.js +1 -1
- package/src/settings-dialog.tsx +29 -2
- package/src/shell.css +1 -1
- package/src/stage-path.css +1 -0
- package/src/stage-path.tsx +89 -0
- package/src/stage.css +1 -0
- package/src/stage.tsx +52 -0
- package/src/starting-point.css +1 -0
- package/src/starting-point.tsx +93 -0
- package/src/status-list.css +1 -0
- package/src/status-list.tsx +65 -0
- package/src/status-matrix.css +1 -0
- package/src/status-matrix.tsx +131 -0
- package/src/team-chat.css +1 -1
- package/src/theme.tsx +13 -3
- package/src/translation-row.css +1 -0
- package/src/translation-row.tsx +113 -0
- package/src/ui.css +1 -1
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
// PICKER GRID (24/10/2026, picker standard slice 3): PickerList's search box, highlight and states for things chosen by their
|
|
3
|
+
// PICTURE (the shop's images, an icon library): tiles in as many columns as fit; a click selects, a double click or Enter uses at
|
|
4
|
+
// once where the caller has a "use" (the image picker's Use image); a tile being uploaded or that failed shows, cannot be chosen,
|
|
5
|
+
// and is said aloud from outside the grid (an option's children are presentational: an alert inside one is not announced).
|
|
6
|
+
// Keys, the WAI-ARIA grid combobox's: ↑ ↓ move by a row, to the tile in the next row nearest the column (the last row may be
|
|
7
|
+
// short; a row of tiles in flight is passed); in the search box ← → Home End are the caret's until ↑ or ↓ has stepped into the
|
|
8
|
+
// grid, and the grid's from then until the next keystroke typed (review of slice 3: with the box always keeping ← →, a keyboard
|
|
9
|
+
// reached only the highlighted tile's column). Without a box the grid has the focus and every key is the grid's.
|
|
10
|
+
// Its own part (`editor-picker-grid.css`); the shared pieces are editor-picker.css's.
|
|
11
|
+
import "../editor-picker.css";
|
|
12
|
+
import "../editor-picker-grid.css";
|
|
13
|
+
import React, { useEffect, useId, useRef, useState } from "react";
|
|
14
|
+
import { Icon } from "../icons";
|
|
15
|
+
import { PickNothing, PickSearch, typesFirst, usePickHighlight, type PickerSearch } from "./picker";
|
|
16
|
+
|
|
17
|
+
export interface PickerTile {
|
|
18
|
+
/** What `onSelect` / `onActivate` get, and what `selected` is compared with. */
|
|
19
|
+
key: string;
|
|
20
|
+
title: string;
|
|
21
|
+
/** A second line under the name (where the image came from). */
|
|
22
|
+
subtitle?: string;
|
|
23
|
+
/** The tile's tooltip, its title when left out: the image picker's file address, which tells two files of one name apart. */
|
|
24
|
+
tooltip?: string;
|
|
25
|
+
/** A photo: a URL, or null for one that could have a picture and has none (a grey square keeps its place). */
|
|
26
|
+
image?: string | null;
|
|
27
|
+
/** A drawn picture instead (an icon's SVG), centred in the tile. */
|
|
28
|
+
picture?: React.ReactNode;
|
|
29
|
+
/** The photo whole inside its square (a logo, a cut-out) instead of filling it; `checker`: on a checkerboard (a cut-out). */
|
|
30
|
+
fit?: "cover" | "contain";
|
|
31
|
+
checker?: boolean;
|
|
32
|
+
/** A tile not yet a choice: being uploaded (`busy`), or one that failed (`error`, said in red). Shown, not choosable. */
|
|
33
|
+
status?: { busy?: boolean; error?: string };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface PickerGridProps {
|
|
37
|
+
/** The grid's accessible name ("Your images", "Icons"). */
|
|
38
|
+
label: string;
|
|
39
|
+
tiles: readonly PickerTile[];
|
|
40
|
+
/** The tile now chosen: a check, `aria-selected`, and the one highlighted first. */
|
|
41
|
+
selected?: string | null;
|
|
42
|
+
/** A click, Space: the tile chosen (the image picker's selection; an icon library's pick). */
|
|
43
|
+
onSelect: (key: string) => void;
|
|
44
|
+
/** A double click, Enter: the tile used at once (the image picker's Use image). Without it they select. */
|
|
45
|
+
onActivate?: (key: string) => void;
|
|
46
|
+
search?: PickerSearch;
|
|
47
|
+
state?: "ready" | "loading" | "reloading" | "error";
|
|
48
|
+
error?: { message: string; onRetry?: () => void };
|
|
49
|
+
empty?: { title: string; body?: string; action?: React.ReactNode };
|
|
50
|
+
more?: { onClick: () => void; busy?: boolean; label?: string };
|
|
51
|
+
/** `photo`: square pictures, 120px wide at least, a name and a line (the default). `icon`: a 26px drawing over its name (two
|
|
52
|
+
* lines at most), 88px wide at least. */
|
|
53
|
+
size?: "photo" | "icon";
|
|
54
|
+
/** Skeleton tiles while loading (default 8). */
|
|
55
|
+
loadingTiles?: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function PickerGrid({ label, tiles, selected = null, onSelect, onActivate, search, state = "ready", error, empty, more, size = "photo", loadingTiles = 8 }: PickerGridProps) {
|
|
59
|
+
const base = useId();
|
|
60
|
+
const boxRef = useRef<HTMLInputElement>(null);
|
|
61
|
+
const gridRef = useRef<HTMLUListElement>(null);
|
|
62
|
+
const phase = state === "reloading" && tiles.length === 0 ? "loading" : state;
|
|
63
|
+
const shown = phase === "ready" || phase === "reloading" ? tiles : [];
|
|
64
|
+
const live = phase === "ready";
|
|
65
|
+
const choosable = (t: PickerTile | undefined) => !!t && !t.status;
|
|
66
|
+
const query = search?.value ?? "";
|
|
67
|
+
const tileId = (i: number) => `${base}-t${i}`;
|
|
68
|
+
const { active, highlight } = usePickHighlight(shown, choosable, selected, query, tileId);
|
|
69
|
+
// In the box: ↑ or ↓ has stepped into the grid, so ← → Home End move the highlight; a keystroke typed gives them back to the caret.
|
|
70
|
+
const [stepping, setStepping] = useState(false);
|
|
71
|
+
useEffect(() => { setStepping(false); }, [query]);
|
|
72
|
+
|
|
73
|
+
// Columns as laid out now (as many as fit): the tiles on the first row.
|
|
74
|
+
const columns = () => {
|
|
75
|
+
const els = gridRef.current ? [...gridRef.current.querySelectorAll<HTMLElement>("[role=option]")] : [];
|
|
76
|
+
const top = els[0]?.offsetTop;
|
|
77
|
+
const n = els.findIndex((e) => e.offsetTop !== top);
|
|
78
|
+
return Math.max(1, n < 0 ? els.length : n);
|
|
79
|
+
};
|
|
80
|
+
const move = (step: "up" | "down" | "left" | "right" | "first" | "last") => {
|
|
81
|
+
const can = shown.flatMap((t, i) => (choosable(t) ? [i] : []));
|
|
82
|
+
if (!can.length) return;
|
|
83
|
+
const to = (i: number | undefined) => { if (i !== undefined) highlight(shown[i]!.key, "keys"); };
|
|
84
|
+
if (step === "first" || active < 0) return to(can[0]);
|
|
85
|
+
if (step === "last") return to(can[can.length - 1]);
|
|
86
|
+
if (step === "right") return to(can.find((i) => i > active));
|
|
87
|
+
if (step === "left") return to([...can].reverse().find((i) => i < active));
|
|
88
|
+
// A row up or down: the next row holding a choice, its tile nearest this column (a short last row, a row of uploads).
|
|
89
|
+
const cols = columns(), col = active % cols, dir = step === "down" ? 1 : -1;
|
|
90
|
+
for (let row = Math.floor(active / cols) + dir; row >= 0 && row * cols < shown.length; row += dir) {
|
|
91
|
+
const inRow = can.filter((i) => Math.floor(i / cols) === row);
|
|
92
|
+
if (inRow.length) return to(inRow.reduce((a, b) => (Math.abs((b % cols) - col) < Math.abs((a % cols) - col) ? b : a)));
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const choose = (t: PickerTile | undefined, use: boolean) => {
|
|
96
|
+
if (!live || !choosable(t)) return;
|
|
97
|
+
if (use && onActivate) onActivate(t!.key); else onSelect(t!.key);
|
|
98
|
+
};
|
|
99
|
+
const clear = () => { search?.onChange(""); boxRef.current?.focus(); };
|
|
100
|
+
// As PickerList: `search.autoFocus`, the box takes the focus once, as the grid appears.
|
|
101
|
+
useEffect(() => { if (typesFirst(search)) boxRef.current?.focus(); }, []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
102
|
+
const keyDown = (e: React.KeyboardEvent, fromBox: boolean) => {
|
|
103
|
+
if (e.nativeEvent.isComposing || e.keyCode === 229) return;
|
|
104
|
+
// A keystroke typed gives ← → back to the caret now, in this event: the effect on the query lands a render later, and the
|
|
105
|
+
// next key can come first.
|
|
106
|
+
if (fromBox && (e.key.length === 1 || e.key === "Backspace" || e.key === "Delete")) { if (stepping) setStepping(false); return; }
|
|
107
|
+
const grid = !fromBox || stepping;
|
|
108
|
+
const go = (step: Parameters<typeof move>[0]) => { e.preventDefault(); move(step); };
|
|
109
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp") { if (fromBox) setStepping(true); go(e.key === "ArrowDown" ? "down" : "up"); }
|
|
110
|
+
else if (grid && e.key === "ArrowRight") go("right");
|
|
111
|
+
else if (grid && e.key === "ArrowLeft") go("left");
|
|
112
|
+
else if (grid && e.key === "Home") go("first");
|
|
113
|
+
else if (grid && e.key === "End") go("last");
|
|
114
|
+
else if (e.key === "Enter") { if (shown.length) e.preventDefault(); choose(shown[active], true); }
|
|
115
|
+
else if (!fromBox && e.key === " ") { e.preventDefault(); choose(shown[active], false); }
|
|
116
|
+
else if (fromBox && e.key === "Escape" && query) { e.preventDefault(); e.stopPropagation(); clear(); }
|
|
117
|
+
};
|
|
118
|
+
// The uploads in flight, said aloud from outside the grid (below).
|
|
119
|
+
const sending = tiles.filter((t) => t.status?.busy).map((t) => t.title);
|
|
120
|
+
|
|
121
|
+
const listId = `${base}-grid`;
|
|
122
|
+
const hasTiles = shown.length > 0;
|
|
123
|
+
const activeId = live && active >= 0 && shown[active] ? tileId(active) : undefined;
|
|
124
|
+
|
|
125
|
+
return (
|
|
126
|
+
<div className="gemboss-ei-pick">
|
|
127
|
+
{search ? <PickSearch search={search} boxRef={boxRef} listId={listId} hasList={hasTiles} activeId={activeId} onKeyDown={(e) => keyDown(e, true)} onClear={clear} /> : null}
|
|
128
|
+
{phase === "loading" || phase === "error" || !hasTiles ? (
|
|
129
|
+
<PickNothing phase={phase === "loading" ? "loading" : phase === "error" ? "error" : "empty"} error={error} empty={empty} query={query} onClear={clear}
|
|
130
|
+
skeleton={
|
|
131
|
+
<div className={`gemboss-ei-pickgrid${size === "icon" ? " is-icon" : ""}`} role="status" aria-label="Loading">
|
|
132
|
+
{Array.from({ length: loadingTiles }, (_, i) => (
|
|
133
|
+
<div key={i} className="gemboss-ei-pickgrid-skeleton" aria-hidden>
|
|
134
|
+
<span className="gemboss-ei-pickgrid-pic ui-skeleton__line" />
|
|
135
|
+
<span className="ui-skeleton__line" />
|
|
136
|
+
</div>
|
|
137
|
+
))}
|
|
138
|
+
</div>
|
|
139
|
+
} />
|
|
140
|
+
) : (
|
|
141
|
+
<>
|
|
142
|
+
<ul ref={gridRef} id={listId} className={`gemboss-ei-pickgrid${size === "icon" ? " is-icon" : ""}${live ? "" : " is-reloading"}`} role="listbox" aria-label={label}
|
|
143
|
+
aria-busy={live ? undefined : true} tabIndex={search ? -1 : 0} aria-activedescendant={search ? undefined : activeId}
|
|
144
|
+
onKeyDown={search ? undefined : (e) => keyDown(e, false)}>
|
|
145
|
+
{shown.map((t, i) => {
|
|
146
|
+
const chosen = t.key === selected;
|
|
147
|
+
const off = !live || !!t.status;
|
|
148
|
+
return (
|
|
149
|
+
<li key={t.key} id={tileId(i)} role="option" aria-selected={chosen} aria-disabled={off ? true : undefined} title={t.tooltip ?? t.title}
|
|
150
|
+
className={`gemboss-ei-pickgrid-tile${i === active ? " is-active" : ""}${t.status?.busy ? " is-busy" : ""}${t.status?.error ? " is-error" : ""}`} data-key={t.key}
|
|
151
|
+
onMouseDown={(e) => { if (search) e.preventDefault(); /* the search box keeps the focus */ }}
|
|
152
|
+
onMouseMove={() => { if (choosable(t) && i !== active) highlight(t.key, "pointer"); }}
|
|
153
|
+
onClick={() => choose(t, false)} onDoubleClick={() => choose(t, true)}>
|
|
154
|
+
{t.picture ? <span className="gemboss-ei-pickgrid-pic is-drawn" aria-hidden>{t.picture}</span>
|
|
155
|
+
: t.image ? <img className={`gemboss-ei-pickgrid-pic${t.fit === "contain" ? " is-whole" : ""}${t.checker ? " is-checker" : ""}`} src={t.image} alt="" loading="lazy" />
|
|
156
|
+
: <span className="gemboss-ei-pickgrid-pic" aria-hidden>{t.status?.error ? "!" : null}</span>}
|
|
157
|
+
<span className="gemboss-ei-pickgrid-name">{t.title}</span>
|
|
158
|
+
{t.status?.error || t.status?.busy || t.subtitle ? (
|
|
159
|
+
<span className="gemboss-ei-pickgrid-sub">
|
|
160
|
+
{t.status?.error ?? (t.status?.busy ? "Uploading…" : t.subtitle)}
|
|
161
|
+
</span>
|
|
162
|
+
) : null}
|
|
163
|
+
{chosen ? <Icon name="check" size={16} className="gemboss-ei-pickgrid-check" /> : null}
|
|
164
|
+
</li>
|
|
165
|
+
);
|
|
166
|
+
})}
|
|
167
|
+
</ul>
|
|
168
|
+
</>
|
|
169
|
+
)}
|
|
170
|
+
{/* Said aloud from outside the grid (an option's children are presentational) and kept across its states: each failure once,
|
|
171
|
+
a log ADDS (one alert string with every failure read the first again with the second: review of #5325); the uploads in
|
|
172
|
+
flight as one line. */}
|
|
173
|
+
<div className="ui-sr-only" role="log" aria-live="assertive">
|
|
174
|
+
{tiles.filter((t) => t.status?.error).map((t) => <p key={t.key}>{`${t.title}: ${t.status!.error}`}</p>)}
|
|
175
|
+
</div>
|
|
176
|
+
<p className="ui-sr-only" role="status">{sending.length ? `Uploading ${sending.join(", ")}…` : ""}</p>
|
|
177
|
+
{/* After the grid, or after "no results" / an empty grid: the next page may hold what this one did not (the first page of a
|
|
178
|
+
store's Shopify files, all held already or none matching, kept Load more out of reach: review of #5325). */}
|
|
179
|
+
{more && (phase === "ready" || phase === "reloading") ? (
|
|
180
|
+
<div className="gemboss-ei-pick-more">
|
|
181
|
+
<button type="button" className="ui-btn" disabled={more.busy} onClick={more.onClick}>{more.busy ? "Loading…" : (more.label ?? "Show more")}</button>
|
|
182
|
+
</div>
|
|
183
|
+
) : null}
|
|
184
|
+
</div>
|
|
185
|
+
);
|
|
186
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
// PICKER LIST (24/10/2026, Chris: the editor's pickers looked bare and each drew its own; "chuẩn hoá theo ui kit"). One body for
|
|
3
|
+
// every "choose one of these" popup in the editors: a link's product, page or section, a video, a font, a page. Before it, five
|
|
4
|
+
// pickers drew five lists in the admin's own CSS (rows, search box, empty and loading lines), each a little different.
|
|
5
|
+
//
|
|
6
|
+
// Polaris' pattern for picking from a searchable list is a Combobox over a Listbox, and this is that, at the kit's numbers:
|
|
7
|
+
// · the search box keeps the focus: ↑ ↓ move the highlighted row, Enter picks it, Escape clears the box first (the popup's own
|
|
8
|
+
// Escape closes it when the box is empty); with no search box the list takes the focus and the same keys work on it. An
|
|
9
|
+
// Enter or an Escape that ends an IME composition (Vietnamese, Chinese) is the typing's, not the list's (as SearchDialog);
|
|
10
|
+
// · the highlight is a ROW, kept by its key: a list that changes under it never leaves it on another row for a frame (review
|
|
11
|
+
// of #5290); the pointer moves it too, and it is painted only while the picker has the focus;
|
|
12
|
+
// · `.gemboss-ei-pick-row[role=option]`: a 40px picture (a photo, or an icon for a page or a section), the title (20px line,
|
|
13
|
+
// 650) with a status pill (`ui-tag`), a second line; the row now chosen has a check and `aria-selected`; one click picks;
|
|
14
|
+
// · the second line puts a NOTE first, in its own box the ellipsis never reaches (review of gemboss #5283: "Opens the popup"
|
|
15
|
+
// at the end of a long line was cut off), then the rest, cut with "…";
|
|
16
|
+
// · a row that cannot be picked is shown, says why (its note), and does nothing (`aria-disabled`);
|
|
17
|
+
// · states (the kit's collection states): loading keeps the rows' place with skeletons; RELOADING keeps the rows of the last
|
|
18
|
+
// answer dimmed and `aria-busy`, and nothing is picked from them (they answer another search: an Enter there wrote the
|
|
19
|
+
// previous query's first row, review of #5290); an error is the kit's danger notice with Try again; a search that finds
|
|
20
|
+
// nothing says so and offers Clear search; an empty list says what the caller gives it; "Show more" ends a long list.
|
|
21
|
+
// Its grid sibling, PickerGrid (picker-grid.tsx), draws the same search box, highlight and states (PickSearch, usePickHighlight,
|
|
22
|
+
// PickNothing, exported for it, not from the package).
|
|
23
|
+
// Its own part (`editor-picker.css`): only a surface that draws a picker loads it.
|
|
24
|
+
import "../editor-picker.css";
|
|
25
|
+
import React, { useEffect, useId, useMemo, useRef, useState } from "react";
|
|
26
|
+
import { Icon, type IconName } from "../icons";
|
|
27
|
+
|
|
28
|
+
export type PickerTone = "neutral" | "info" | "success" | "warning" | "critical";
|
|
29
|
+
|
|
30
|
+
export interface PickerItem {
|
|
31
|
+
/** What `onPick` gets, and what `current` is compared with. */
|
|
32
|
+
key: string;
|
|
33
|
+
title: string;
|
|
34
|
+
/** The title drawn as what it names: a font's name in its own face, at a size its shape reads at (`{ fontFamily, fontSize }`). */
|
|
35
|
+
titleStyle?: React.CSSProperties;
|
|
36
|
+
/** The rest of the second line (a path, a heading), cut with "…" when it does not fit. */
|
|
37
|
+
subtitle?: string;
|
|
38
|
+
/** What to know before picking, first on the second line and never cut ("Opens the popup", "Opens once it is active"). */
|
|
39
|
+
note?: string;
|
|
40
|
+
/** A short status beside the title, as a pill ("Draft", "Archived"). */
|
|
41
|
+
badge?: { label: string; tone?: PickerTone };
|
|
42
|
+
/** A photo: a URL, or null for a thing that could have one and has none (a grey square keeps its place). */
|
|
43
|
+
image?: string | null;
|
|
44
|
+
/** For a thing that has no photo (a page, a section): its icon in the same 40px tile. */
|
|
45
|
+
icon?: IconName;
|
|
46
|
+
/** A drawn picture of the caller's instead (an SVG the kit has no icon for: a cart, a product tag), in the same 40px tile. */
|
|
47
|
+
picture?: React.ReactNode;
|
|
48
|
+
/** The row opens a list of its own (the page picker's Products, Collections): a chevron at its end instead of the check,
|
|
49
|
+
* and picking it is the caller's step into that list. Current, it is marked by the chosen row's tint (and `aria-selected`). */
|
|
50
|
+
drill?: boolean;
|
|
51
|
+
/** Present: shown, not pickable, and this is said as its note (a locked row says why). */
|
|
52
|
+
disabledReason?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface PickerListProps {
|
|
56
|
+
/** The list's accessible name ("Sections", "Products"). */
|
|
57
|
+
label: string;
|
|
58
|
+
items: readonly PickerItem[];
|
|
59
|
+
/** The key now chosen: its row has the check (a drill row: the tint), and is the one highlighted first. */
|
|
60
|
+
current?: string | null;
|
|
61
|
+
onPick: (key: string) => void;
|
|
62
|
+
/** The search box above the list; leave it out for a short list. The caller filters `items` (or asks a server). `autoFocus`:
|
|
63
|
+
* the box takes the focus when the list appears, and is the `[data-autofocus]` the kit's Dialog and Popover focus on opening
|
|
64
|
+
* (every picker in the editors sets it: the search box is where a picker is used from). */
|
|
65
|
+
search?: PickerSearch;
|
|
66
|
+
/**
|
|
67
|
+
* `loading`: skeleton rows in the list's place (a first read). `reloading`: `items` are the last answer, kept on screen
|
|
68
|
+
* dimmed while the next one is read (a search being typed), and not pickable. `error`: `error.message`, Try again with
|
|
69
|
+
* `error.onRetry`.
|
|
70
|
+
*/
|
|
71
|
+
state?: "ready" | "loading" | "reloading" | "error";
|
|
72
|
+
error?: { message: string; onRetry?: () => void };
|
|
73
|
+
/** What an empty list says when nothing is searched ("This page has no sections yet."), and one action. */
|
|
74
|
+
empty?: { title: string; body?: string; action?: React.ReactNode };
|
|
75
|
+
/** More rows than these: "Show more" after the last one. */
|
|
76
|
+
more?: { onClick: () => void; busy?: boolean; label?: string };
|
|
77
|
+
/** Skeleton rows while loading (default 4). */
|
|
78
|
+
loadingRows?: number;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** @internal shared with picker-grid.tsx */
|
|
82
|
+
export type PickerSearch = { value: string; onChange: (value: string) => void; placeholder?: string; label?: string; autoFocus?: boolean };
|
|
83
|
+
|
|
84
|
+
/** @internal shared with picker-grid.tsx. `search.autoFocus` under a mouse only: under a finger (the kit's `(pointer: coarse)`, as
|
|
85
|
+
* §9.1's targets) a focused box raises the on-screen keyboard over the list the person came to tap (review, 24/10/2026). */
|
|
86
|
+
export const typesFirst = (search?: PickerSearch) =>
|
|
87
|
+
Boolean(search?.autoFocus) && !(typeof window !== "undefined" && window.matchMedia?.("(pointer: coarse)").matches);
|
|
88
|
+
|
|
89
|
+
/** The search box both pickers draw: the kit's grouped field (an icon in the box, as the admin's "Find a setting"), `gemboss-field`
|
|
90
|
+
* owning the look, here only the icon and the ✕ placed (text-fields-share-one-look.test.ts). A text box with a search keyboard, not
|
|
91
|
+
* type=search: it is a combobox, and the browser's own ✕ would sit beside the kit's. */
|
|
92
|
+
export function PickSearch({ search, boxRef, listId, hasList, activeId, onKeyDown, onClear }: {
|
|
93
|
+
search: PickerSearch; boxRef: React.RefObject<HTMLInputElement>; listId: string; hasList: boolean; activeId?: string;
|
|
94
|
+
onKeyDown: (e: React.KeyboardEvent) => void; onClear: () => void;
|
|
95
|
+
}) {
|
|
96
|
+
return (
|
|
97
|
+
<label className="gemboss-field gemboss-field--group gemboss-ei-pick-search">
|
|
98
|
+
<Icon name="search" size={16} className="gemboss-ei-pick-search-icon" />
|
|
99
|
+
<input ref={boxRef} type="text" inputMode="search" enterKeyHint="search" className="gemboss-field__bare" value={search.value} placeholder={search.placeholder ?? "Search"}
|
|
100
|
+
aria-label={search.label ?? search.placeholder ?? "Search"} role="combobox" aria-expanded={hasList} aria-controls={hasList ? listId : undefined}
|
|
101
|
+
aria-autocomplete="list" aria-activedescendant={activeId} autoComplete="off" spellCheck={false} data-autofocus={typesFirst(search) ? "" : undefined}
|
|
102
|
+
onChange={(e) => search.onChange(e.target.value)} onKeyDown={onKeyDown} />
|
|
103
|
+
{search.value ? (
|
|
104
|
+
<button type="button" className="gemboss-ei-pick-clear" aria-label="Clear search" onClick={onClear}>
|
|
105
|
+
<Icon name="close" size={16} />
|
|
106
|
+
</button>
|
|
107
|
+
) : null}
|
|
108
|
+
</label>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** What both pickers draw instead of their items: skeletons (given), the kit's error, a search that found nothing, an empty list. */
|
|
113
|
+
export function PickNothing({ phase, skeleton, error, empty, query, onClear }: {
|
|
114
|
+
phase: "loading" | "error" | "empty"; skeleton: React.ReactNode; error?: { message: string; onRetry?: () => void };
|
|
115
|
+
empty?: { title: string; body?: string; action?: React.ReactNode }; query: string; onClear: () => void;
|
|
116
|
+
}) {
|
|
117
|
+
if (phase === "loading") return <>{skeleton}</>;
|
|
118
|
+
if (phase === "error") {
|
|
119
|
+
// The kit's error (CollectionState's): a danger notice, never an empty list.
|
|
120
|
+
return (
|
|
121
|
+
<div className="ui-notice ui-notice--danger gemboss-ei-pick-error" role="alert">
|
|
122
|
+
<b>{error?.message ?? "This list could not be read."}</b>
|
|
123
|
+
{error?.onRetry ? <button type="button" className="ui-btn" onClick={error.onRetry}>Try again</button> : null}
|
|
124
|
+
</div>
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
return query.trim() ? (
|
|
128
|
+
<div className="ui-empty">
|
|
129
|
+
<p className="ui-empty__title">No results for “{query.trim()}”</p>
|
|
130
|
+
<p className="ui-empty__body">Check the spelling or search for something else.</p>
|
|
131
|
+
<button type="button" className="ui-btn" onClick={onClear}>Clear search</button>
|
|
132
|
+
{/* The caller's way forward too (the image picker's Upload): a library with nothing in it, searched, hid it (review of #5325). */}
|
|
133
|
+
{empty?.action ?? null}
|
|
134
|
+
</div>
|
|
135
|
+
) : (
|
|
136
|
+
<div className="ui-empty">
|
|
137
|
+
<p className="ui-empty__title">{empty?.title ?? "Nothing to choose yet."}</p>
|
|
138
|
+
{empty?.body ? <p className="ui-empty__body">{empty.body}</p> : null}
|
|
139
|
+
{empty?.action ?? null}
|
|
140
|
+
</div>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** @internal shared with picker-grid.tsx. The highlight both pickers keep: a KEY (a list that changes under it never leaves it on
|
|
145
|
+
* another item for a frame, review of #5290), forgotten when the search changes; unset or gone, the chosen item when it can be
|
|
146
|
+
* picked, else the first that can. It is scrolled into view when the keys (or the list) move it, never when the pointer does:
|
|
147
|
+
* a pointer resting on a half-shown tile scrolled the grid under itself, and the next tile under it moved it again (review of
|
|
148
|
+
* slice 3). */
|
|
149
|
+
export function usePickHighlight<T extends { key: string }>(shown: readonly T[], can: (t: T) => boolean, chosen: string | null, query: string, idOf: (i: number) => string) {
|
|
150
|
+
const [activeKey, setActiveKey] = useState<string | null>(null);
|
|
151
|
+
// The key the pointer put the highlight on: that one is not scrolled to (a flag alone stayed set when the pointer's move left
|
|
152
|
+
// the highlight where it was, and the next move by the keys or the list skipped its scroll: review of #5325).
|
|
153
|
+
const byPointer = useRef<string | null>(null);
|
|
154
|
+
useEffect(() => { setActiveKey(null); }, [query]);
|
|
155
|
+
const byKey = activeKey === null ? -1 : shown.findIndex((t) => t.key === activeKey && can(t));
|
|
156
|
+
const atChosen = shown.findIndex((t) => t.key === chosen && can(t));
|
|
157
|
+
const active = byKey >= 0 ? byKey : atChosen >= 0 ? atChosen : shown.findIndex(can);
|
|
158
|
+
useEffect(() => {
|
|
159
|
+
if (active >= 0 && byPointer.current !== shown[active]?.key) document.getElementById(idOf(active))?.scrollIntoView?.({ block: "nearest" });
|
|
160
|
+
byPointer.current = null;
|
|
161
|
+
}, [active]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
162
|
+
const highlight = (key: string, by: "keys" | "pointer") => { byPointer.current = by === "pointer" ? key : null; setActiveKey(key); };
|
|
163
|
+
return { active, highlight };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// A row that opens a list of its own ends with this.
|
|
167
|
+
const DRILL = <Icon name="chevronRight" size={16} className="gemboss-ei-pick-drill" />;
|
|
168
|
+
|
|
169
|
+
const TONE: Record<PickerTone, string> = { neutral: "ui-tag", info: "ui-tag ui-tag--accent", success: "ui-tag ui-tag--success", warning: "ui-tag ui-tag--warning", critical: "ui-tag ui-tag--danger" };
|
|
170
|
+
|
|
171
|
+
export function PickerList({ label, items, current = null, onPick, search, state = "ready", error, empty, more, loadingRows = 4 }: PickerListProps) {
|
|
172
|
+
const base = useId();
|
|
173
|
+
const boxRef = useRef<HTMLInputElement>(null);
|
|
174
|
+
// Reloading with nothing to keep is a first read: skeletons, not "nothing here".
|
|
175
|
+
const phase = state === "reloading" && items.length === 0 ? "loading" : state;
|
|
176
|
+
const shown = phase === "ready" || phase === "reloading" ? items : [];
|
|
177
|
+
const live = phase === "ready";
|
|
178
|
+
const pickable = useMemo(() => shown.map((it, i) => (it.disabledReason ? -1 : i)).filter((i) => i >= 0), [shown]);
|
|
179
|
+
const query = search?.value ?? "";
|
|
180
|
+
const optionId = (i: number) => `${base}-o${i}`;
|
|
181
|
+
const { active, highlight } = usePickHighlight(shown, (it) => !it.disabledReason, current, query, optionId);
|
|
182
|
+
|
|
183
|
+
const pick = (it: PickerItem | undefined) => { if (live && it && !it.disabledReason) onPick(it.key); };
|
|
184
|
+
const move = (by: 1 | -1 | "first" | "last") => {
|
|
185
|
+
if (!pickable.length) return;
|
|
186
|
+
const at = pickable.indexOf(active);
|
|
187
|
+
const next = by === "first" ? 0 : by === "last" ? pickable.length - 1 : at < 0 ? 0 : Math.min(pickable.length - 1, Math.max(0, at + by));
|
|
188
|
+
highlight(shown[pickable[next]!]!.key, "keys");
|
|
189
|
+
};
|
|
190
|
+
const clear = () => { search?.onChange(""); boxRef.current?.focus(); };
|
|
191
|
+
// The box carries `data-autofocus`, which the kit's Dialog and Popover focus as they open; this covers a list that appears in a
|
|
192
|
+
// container already open (a drill-down's new list). Once, as the list appears.
|
|
193
|
+
useEffect(() => { if (typesFirst(search)) boxRef.current?.focus(); }, []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
194
|
+
const keyDown = (e: React.KeyboardEvent, fromBox: boolean) => {
|
|
195
|
+
if (e.nativeEvent.isComposing || e.keyCode === 229) return;
|
|
196
|
+
if (e.key === "ArrowDown") { e.preventDefault(); move(1); }
|
|
197
|
+
else if (e.key === "ArrowUp") { e.preventDefault(); move(-1); }
|
|
198
|
+
else if (!fromBox && e.key === "Home") { e.preventDefault(); move("first"); }
|
|
199
|
+
else if (!fromBox && e.key === "End") { e.preventDefault(); move("last"); }
|
|
200
|
+
else if (e.key === "Enter" || (!fromBox && e.key === " ")) {
|
|
201
|
+
// Reloading: the rows answer another search; Enter waits for this one (nothing is written).
|
|
202
|
+
if (shown.length) e.preventDefault();
|
|
203
|
+
pick(shown[active]);
|
|
204
|
+
} else if (fromBox && e.key === "Escape" && query) { e.preventDefault(); e.stopPropagation(); clear(); }
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const listId = `${base}-list`;
|
|
208
|
+
const hasList = shown.length > 0;
|
|
209
|
+
const activeId = live && active >= 0 && shown[active] ? optionId(active) : undefined;
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
<div className="gemboss-ei-pick">
|
|
213
|
+
{search ? <PickSearch search={search} boxRef={boxRef} listId={listId} hasList={hasList} activeId={activeId} onKeyDown={(e) => keyDown(e, true)} onClear={clear} /> : null}
|
|
214
|
+
|
|
215
|
+
{phase === "loading" || phase === "error" || !hasList ? (
|
|
216
|
+
<PickNothing phase={phase === "loading" ? "loading" : phase === "error" ? "error" : "empty"} error={error} empty={empty} query={query} onClear={clear}
|
|
217
|
+
skeleton={
|
|
218
|
+
<div className="gemboss-ei-pick-skeleton" role="status" aria-label="Loading">
|
|
219
|
+
{Array.from({ length: loadingRows }, (_, i) => (
|
|
220
|
+
<div key={i} className="gemboss-ei-pick-skeleton-row" aria-hidden>
|
|
221
|
+
<span className="gemboss-ei-pick-media ui-skeleton__line" />
|
|
222
|
+
<span className="gemboss-ei-pick-skeleton-text"><span className="ui-skeleton__line" /><span className="ui-skeleton__line" /></span>
|
|
223
|
+
</div>
|
|
224
|
+
))}
|
|
225
|
+
</div>
|
|
226
|
+
} />
|
|
227
|
+
) : (
|
|
228
|
+
<>
|
|
229
|
+
<ul id={listId} className={`gemboss-ei-pick-list${live ? "" : " is-reloading"}`} role="listbox" aria-label={label} aria-busy={live ? undefined : true}
|
|
230
|
+
tabIndex={search ? -1 : 0} aria-activedescendant={search ? undefined : activeId} onKeyDown={search ? undefined : (e) => keyDown(e, false)}>
|
|
231
|
+
{shown.map((it, i) => {
|
|
232
|
+
const chosen = it.key === current;
|
|
233
|
+
const note = it.disabledReason ?? it.note;
|
|
234
|
+
return (
|
|
235
|
+
<li key={it.key} id={optionId(i)} role="option" aria-selected={chosen} aria-disabled={it.disabledReason || !live ? true : undefined}
|
|
236
|
+
className={`gemboss-ei-pick-row${i === active ? " is-active" : ""}`} data-key={it.key}
|
|
237
|
+
onMouseDown={(e) => e.preventDefault() /* the search box keeps the focus */}
|
|
238
|
+
onMouseMove={() => { if (!it.disabledReason && i !== active) highlight(it.key, "pointer"); }}
|
|
239
|
+
onClick={() => pick(it)}>
|
|
240
|
+
{it.image !== undefined ? (
|
|
241
|
+
it.image ? <img className="gemboss-ei-pick-media" src={it.image} alt="" loading="lazy" /> : <span className="gemboss-ei-pick-media" aria-hidden />
|
|
242
|
+
) : it.icon ? (
|
|
243
|
+
<span className="gemboss-ei-pick-media gemboss-ei-pick-media--icon" aria-hidden><Icon name={it.icon} size={20} /></span>
|
|
244
|
+
) : it.picture ? (
|
|
245
|
+
<span className="gemboss-ei-pick-media gemboss-ei-pick-media--icon" aria-hidden>{it.picture}</span>
|
|
246
|
+
) : null}
|
|
247
|
+
<span className="gemboss-ei-pick-text">
|
|
248
|
+
<span className="gemboss-ei-pick-head">
|
|
249
|
+
<span className="gemboss-ei-pick-title" style={it.titleStyle}>{it.title}</span>
|
|
250
|
+
{it.badge ? <span className={TONE[it.badge.tone ?? "neutral"]}>{it.badge.label}</span> : null}
|
|
251
|
+
</span>
|
|
252
|
+
{note || it.subtitle ? (
|
|
253
|
+
<span className="gemboss-ei-pick-sub">
|
|
254
|
+
{note ? <span className={`gemboss-ei-pick-note${it.disabledReason ? " is-blocked" : ""}`}>{note}</span> : null}
|
|
255
|
+
{it.subtitle ? <span className="gemboss-ei-pick-rest">{note ? `· ${it.subtitle}` : it.subtitle}</span> : null}
|
|
256
|
+
</span>
|
|
257
|
+
) : null}
|
|
258
|
+
</span>
|
|
259
|
+
{it.drill ? DRILL : chosen ? <Icon name="check" size={20} className="gemboss-ei-pick-check" /> : null}
|
|
260
|
+
</li>
|
|
261
|
+
);
|
|
262
|
+
})}
|
|
263
|
+
</ul>
|
|
264
|
+
</>
|
|
265
|
+
)}
|
|
266
|
+
{/* After the list, or after "no results" / an empty list: the next page may hold what this one did not (the first page of a
|
|
267
|
+
store's Shopify files, all held already or none matching, kept Load more out of reach: review of #5325). */}
|
|
268
|
+
{more && (phase === "ready" || phase === "reloading") ? (
|
|
269
|
+
<div className="gemboss-ei-pick-more">
|
|
270
|
+
<button type="button" className="ui-btn" disabled={more.busy} onClick={more.onClick}>{more.busy ? "Loading…" : (more.label ?? "Show more")}</button>
|
|
271
|
+
</div>
|
|
272
|
+
) : null}
|
|
273
|
+
</div>
|
|
274
|
+
);
|
|
275
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
// POPOVER (24/10/2026, Chris: the editor needs the small panels GemPages opens from a setting, "tham khảo cái gì hợp lý… ưu tiên ui
|
|
3
|
+
// ux dễ dùng"). A setting that needs more room than its row (a colour's picker, later a typography or a shadow) opens this beside
|
|
4
|
+
// the settings column, over the canvas, so the column does not grow and the canvas stays in view:
|
|
5
|
+
// · in the top layer (`popover="manual"`), like the kit menu's list and RepeaterList's layer, and in a portal on <body>: the
|
|
6
|
+
// panel's own rules (`.gemboss-ei-fld :is(button, input)`, a bundle panel's 44px floor) never reach inside it (review of
|
|
7
|
+
// #5296: they drew the picker's 12px bars 44 tall); where the browser has no popover API it opens in place, under its row;
|
|
8
|
+
// · BESIDE the column the anchor scrolls in, on the side with room (the canvas side), its top at the anchor's row and kept on
|
|
9
|
+
// screen; no room either side: under the anchor (`placeFloating`); placed again on a scroll or a resize outside it;
|
|
10
|
+
// `placement="below"`: always under the anchor (a trigger with no column beside it: the editor top bar's page picker);
|
|
11
|
+
// · at phone width (`max-width: 47.9975em`) a sheet along the bottom of the screen (editor-popover.css);
|
|
12
|
+
// · a head with its title, ← Back for a nested panel (`onBack`) and ✕; Escape steps back in a nested panel (as ← Back: the page
|
|
13
|
+
// picker's Products › back to its pages), else closes it (and only it), as does a press outside
|
|
14
|
+
// it and its anchor, or the focus moving into a frame (the canvas is an iframe: its presses never reach this document); the
|
|
15
|
+
// focus goes in on open, and back to what opened it (`returnTo`, else the anchor) on Escape or ✕ (a press outside leaves it
|
|
16
|
+
// where that press put it); placed again when it changes size (a list growing in it) and on a scroll that moves its row.
|
|
17
|
+
// Its own part (`editor-popover.css`); the surface is the shared `gemboss-popover-surface` (shell).
|
|
18
|
+
import "../editor-popover.css";
|
|
19
|
+
import React, { useEffect, useId, useLayoutEffect, useRef } from "react";
|
|
20
|
+
import { createPortal } from "react-dom";
|
|
21
|
+
import { Icon } from "../icons";
|
|
22
|
+
import { canFloatMenus, placeFloating } from "../menu-layer";
|
|
23
|
+
|
|
24
|
+
export interface PopoverProps {
|
|
25
|
+
open: boolean;
|
|
26
|
+
/** What it is placed against (its row) and the column that sits in. */
|
|
27
|
+
anchor: HTMLElement | null;
|
|
28
|
+
/** What gets the focus back on Escape or ✕: the button that opened it (default: the anchor). */
|
|
29
|
+
returnTo?: HTMLElement | null;
|
|
30
|
+
onClose: () => void;
|
|
31
|
+
title: string;
|
|
32
|
+
/** A nested panel's way back (Border › Color): a ← in the head. */
|
|
33
|
+
onBack?: () => void;
|
|
34
|
+
children: React.ReactNode;
|
|
35
|
+
/** Its width beside the column, in px (default 280). A sheet at phone width is the screen's. */
|
|
36
|
+
width?: number;
|
|
37
|
+
/** `beside` (default): beside the anchor's column, as a setting's panel. `below`: under the anchor, as a menu from a top bar. */
|
|
38
|
+
placement?: "beside" | "below";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const GAP = 8, EDGE = 8;
|
|
42
|
+
// A layout effect in the browser (placed before paint), a plain one on a server render (the editor catalog's), as team-chat.tsx.
|
|
43
|
+
const useIsoLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
|
|
44
|
+
const PHONE = "(max-width: 47.9975em)";
|
|
45
|
+
|
|
46
|
+
/** The column the anchor sits in: its nearest ancestor that scrolls up and down (the settings panel), whether or not it overflows
|
|
47
|
+
* now (a short panel is still the panel). */
|
|
48
|
+
function columnOf(el: HTMLElement): HTMLElement | null {
|
|
49
|
+
for (let p = el.parentElement; p && p !== document.body; p = p.parentElement) {
|
|
50
|
+
const oy = getComputedStyle(p).overflowY;
|
|
51
|
+
if (oy === "auto" || oy === "scroll") return p;
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The card around the scrolling column: the boxes that only pad or border it (each edge within 24px of the column's), up to
|
|
57
|
+
* the first that is wider (a layout). The popover sits beside the card, not over its edge (measured in the page editor: the
|
|
58
|
+
* settings body ends at 372, its card at 389). */
|
|
59
|
+
function cardOf(col: HTMLElement): HTMLElement {
|
|
60
|
+
const c = col.getBoundingClientRect();
|
|
61
|
+
let card = col;
|
|
62
|
+
for (let p = col.parentElement; p && p !== document.body; p = p.parentElement) {
|
|
63
|
+
const r = p.getBoundingClientRect();
|
|
64
|
+
// Only a box that HOLDS the column is its card: a zero box (`display: contents`) or a narrower wrapper is passed over, or the
|
|
65
|
+
// popover was placed against its rect, over the column (review of #5323).
|
|
66
|
+
if (r.left > c.left + 0.5 || r.right < c.right - 0.5) continue;
|
|
67
|
+
if (r.left < c.left - 24 || r.right > c.right + 24) break;
|
|
68
|
+
card = p;
|
|
69
|
+
}
|
|
70
|
+
return card;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Beside the column's card on the side with room (right first), top at the anchor's row, on screen; neither: under the anchor. */
|
|
74
|
+
function place(layer: HTMLElement, anchor: HTMLElement, width: number, placement: "beside" | "below") {
|
|
75
|
+
if (window.matchMedia?.(PHONE).matches) { layer.style.left = layer.style.top = layer.style.maxHeight = ""; layer.dataset.side = "sheet"; return; }
|
|
76
|
+
layer.style.width = `${width}px`;
|
|
77
|
+
if (placement === "below") { placeFloating(layer, anchor); return; }
|
|
78
|
+
const column = columnOf(anchor);
|
|
79
|
+
const col = (column ? cardOf(column) : anchor).getBoundingClientRect();
|
|
80
|
+
const r = anchor.getBoundingClientRect();
|
|
81
|
+
const vw = document.documentElement.clientWidth, vh = window.innerHeight;
|
|
82
|
+
const right = vw - col.right - GAP - EDGE, left = col.left - GAP - EDGE;
|
|
83
|
+
if (right < width && left < width) { placeFloating(layer, anchor); return; }
|
|
84
|
+
layer.style.maxHeight = `${vh - 2 * EDGE}px`;
|
|
85
|
+
const h = Math.min(layer.offsetHeight, vh - 2 * EDGE);
|
|
86
|
+
const top = Math.max(EDGE, Math.min(r.top, vh - EDGE - h));
|
|
87
|
+
const x = right >= width ? col.right + GAP : col.left - GAP - width;
|
|
88
|
+
layer.style.left = `${Math.round(x)}px`;
|
|
89
|
+
layer.style.top = `${Math.round(top)}px`;
|
|
90
|
+
layer.dataset.side = right >= width ? "right" : "left";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function Popover({ open, anchor, returnTo, onClose, title, onBack, children, width = 280, placement = "beside" }: PopoverProps) {
|
|
94
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
95
|
+
const titleId = useId();
|
|
96
|
+
const floating = canFloatMenus();
|
|
97
|
+
const close = useRef(onClose);
|
|
98
|
+
close.current = onClose;
|
|
99
|
+
|
|
100
|
+
useIsoLayoutEffect(() => {
|
|
101
|
+
const el = ref.current;
|
|
102
|
+
if (!open || !el || !anchor) return;
|
|
103
|
+
if (floating) {
|
|
104
|
+
if (el.getAttribute("popover") !== "manual") el.setAttribute("popover", "manual");
|
|
105
|
+
if (!el.matches(":popover-open")) el.showPopover();
|
|
106
|
+
}
|
|
107
|
+
const again = () => { if (floating) place(el, anchor, width, placement); };
|
|
108
|
+
again();
|
|
109
|
+
// The focus goes in (the first thing to use, else the layer), and comes back to the anchor however it closes.
|
|
110
|
+
(el.querySelector<HTMLElement>("[data-autofocus]") ?? el).focus({ preventScroll: true });
|
|
111
|
+
// A scroll that moves the anchor (the page, or a box the anchor is in): placed again. Others (a chat streaming beside it, a
|
|
112
|
+
// scroll inside it) leave it be, as the kit menu's closeDetachedMenus does.
|
|
113
|
+
const onScroll = (e: Event) => {
|
|
114
|
+
const t = e.target;
|
|
115
|
+
if (t instanceof Node && el.contains(t)) return;
|
|
116
|
+
if (t === document || t === document.documentElement || (t instanceof Node && t.contains(anchor))) again();
|
|
117
|
+
};
|
|
118
|
+
const onDown = (e: PointerEvent) => { const t = e.target as Node; if (!el.contains(t) && !anchor.contains(t)) close.current(); };
|
|
119
|
+
// The canvas is an iframe: a press in it never reaches this document, but the focus leaving for it does.
|
|
120
|
+
const onBlur = () => { setTimeout(() => { if (document.activeElement instanceof HTMLIFrameElement) close.current(); }, 0); };
|
|
121
|
+
const grows = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(again);
|
|
122
|
+
grows?.observe(el);
|
|
123
|
+
window.addEventListener("scroll", onScroll, true);
|
|
124
|
+
window.addEventListener("resize", again);
|
|
125
|
+
window.addEventListener("blur", onBlur);
|
|
126
|
+
document.addEventListener("pointerdown", onDown, true);
|
|
127
|
+
return () => {
|
|
128
|
+
grows?.disconnect();
|
|
129
|
+
window.removeEventListener("scroll", onScroll, true);
|
|
130
|
+
window.removeEventListener("resize", again);
|
|
131
|
+
window.removeEventListener("blur", onBlur);
|
|
132
|
+
document.removeEventListener("pointerdown", onDown, true);
|
|
133
|
+
if (floating && el.matches(":popover-open")) el.hidePopover();
|
|
134
|
+
};
|
|
135
|
+
}, [open, anchor, floating, width, placement]);
|
|
136
|
+
|
|
137
|
+
const shut = () => { onClose(); (returnTo ?? anchor)?.focus({ preventScroll: true }); };
|
|
138
|
+
const back = () => {
|
|
139
|
+
onBack?.();
|
|
140
|
+
// The panel it steps back to is drawn next, and what had the focus (← Back, a box) may be gone with it: the focus stays in the
|
|
141
|
+
// popover (its [data-autofocus], else the layer), never on <body>.
|
|
142
|
+
requestAnimationFrame(() => {
|
|
143
|
+
const el = ref.current;
|
|
144
|
+
if (el && !el.contains(document.activeElement)) (el.querySelector<HTMLElement>("[data-autofocus]") ?? el).focus({ preventScroll: true });
|
|
145
|
+
});
|
|
146
|
+
};
|
|
147
|
+
if (!open) return null;
|
|
148
|
+
const layer = (
|
|
149
|
+
<div ref={ref} className={`gemboss-ei-pop gemboss-popover-surface${floating ? "" : " gemboss-ei-pop--inline"}`} role="dialog" aria-labelledby={titleId}
|
|
150
|
+
tabIndex={-1} onKeyDown={(e) => { if (e.key === "Escape" && !e.nativeEvent.isComposing) { e.preventDefault(); e.stopPropagation(); if (onBack) back(); else shut(); } }}>
|
|
151
|
+
<div className="gemboss-ei-pop-head">
|
|
152
|
+
{onBack ? <button type="button" className="gemboss-ei-pop-btn" aria-label="Back" onClick={back}><Icon name="chevronLeft" size={16} /></button> : null}
|
|
153
|
+
<span className="gemboss-ei-pop-title" id={titleId}>{title}</span>
|
|
154
|
+
<button type="button" className="gemboss-ei-pop-btn" aria-label="Close" onClick={shut}><Icon name="close" size={16} /></button>
|
|
155
|
+
</div>
|
|
156
|
+
<div className="gemboss-ei-pop-body">{children}</div>
|
|
157
|
+
</div>
|
|
158
|
+
);
|
|
159
|
+
return floating && typeof document !== "undefined" ? createPortal(layer, document.body) : layer;
|
|
160
|
+
}
|
package/src/editor/swatch.ts
CHANGED
|
@@ -115,7 +115,8 @@ export function parseColor(value: string): Rgba | null {
|
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
const hex2 = (n: number) => clamp(Math.round(n), 0, 255).toString(16).padStart(2, "0");
|
|
118
|
-
|
|
118
|
+
/** `#rrggbb` of a colour's channels (rounded, clamped): the swatch's, pickSwatch's and the colour picker's one way of writing hex. */
|
|
119
|
+
export const hexOf = (c: Pick<Rgba, "r" | "g" | "b">) => `#${hex2(c.r)}${hex2(c.g)}${hex2(c.b)}`;
|
|
119
120
|
const alphaText = (a: number) => String(Math.round(a * 100) / 100);
|
|
120
121
|
|
|
121
122
|
/** A colour written as the field already writes it: a hex stays a hex, anything else becomes `rgb()`; with transparency `rgba()`. */
|