@datalayer/primer-addons 1.0.54 → 1.0.55
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/lib/components/appearance/ColorModeCircle.js +3 -1
- package/lib/components/toolbar/Toolbar.d.ts +14 -2
- package/lib/components/toolbar/Toolbar.js +102 -8
- package/lib/components/toolbar/ToolbarRenderer.d.ts +2 -0
- package/lib/components/toolbar/ToolbarRenderer.js +3 -1
- package/lib/components/toolbar/types.d.ts +6 -0
- package/package.json +1 -1
|
@@ -154,7 +154,9 @@ export function ColorModeCircle({ colorMode, onColorModeChange, size = 28, hover
|
|
|
154
154
|
bg: 'canvas.overlay',
|
|
155
155
|
border: '1px solid',
|
|
156
156
|
borderColor: 'border.default',
|
|
157
|
-
|
|
157
|
+
// The panel is an overlay, so its shape belongs to the active
|
|
158
|
+
// theme (Metal and Agent are deliberately rounder than VS Code).
|
|
159
|
+
borderRadius: 'var(--theme-radius-overlay, 12px)',
|
|
158
160
|
boxShadow: 'shadow.large',
|
|
159
161
|
opacity: isHoverOpen ? 1 : 0,
|
|
160
162
|
transform: isHoverOpen ? 'none' : isRight ? 'translateX(-6px)' : 'translateY(-6px)',
|
|
@@ -7,6 +7,18 @@ import type { ToolbarProps, ToolbarItem } from "./types";
|
|
|
7
7
|
* line first among equals — with every priority equal, the tail of the
|
|
8
8
|
* line, as a reader expects.
|
|
9
9
|
*/
|
|
10
|
-
export declare function overflowKeys(ordered: ToolbarItem[], widthOf: (key: string) => number, available: number, gap?: number, triggerWidth?: number
|
|
11
|
-
|
|
10
|
+
export declare function overflowKeys(ordered: ToolbarItem[], widthOf: (key: string) => number, available: number, gap?: number, triggerWidth?: number,
|
|
11
|
+
/**
|
|
12
|
+
* Whether an item is laid out on the line: one that draws nothing yet
|
|
13
|
+
* (a chip waiting for its data) is 0px wide and still takes its gap.
|
|
14
|
+
* Unsaid, an item 0px wide takes none.
|
|
15
|
+
*/
|
|
16
|
+
laidOut?: (key: string) => boolean): Set<string>;
|
|
17
|
+
/**
|
|
18
|
+
* The least room a line needs: the items that never leave it — pinned, spacers at their minimum, and those laid
|
|
19
|
+
* out empty, which still take their gap — and, when any item can leave, the menu's button with its gap.
|
|
20
|
+
* Exported for tests.
|
|
21
|
+
*/
|
|
22
|
+
export declare function minimumWidthOf(ordered: ToolbarItem[], widthOf: (key: string) => number, gap?: number, triggerWidth?: number, laidOut?: (key: string) => boolean): number;
|
|
23
|
+
export declare function Toolbar({ items, extraItems, className, disabled, ariaLabel, overflow, variant, gap, overflowAriaLabel, keyboardNavigation, onMinimumWidth, }: ToolbarProps): import("react").JSX.Element;
|
|
12
24
|
export default Toolbar;
|
|
@@ -48,7 +48,7 @@ import { useCallback, useLayoutEffect, useMemo, useRef, useState, } from "react"
|
|
|
48
48
|
import { ActionMenu, IconButton } from "@primer/react";
|
|
49
49
|
import { KebabHorizontalIcon } from "@primer/octicons-react";
|
|
50
50
|
import { Box } from "../box/Box";
|
|
51
|
-
import { ToolbarRenderer } from "./ToolbarRenderer";
|
|
51
|
+
import { SPACER_MIN_WIDTH, ToolbarRenderer } from "./ToolbarRenderer";
|
|
52
52
|
/** Space between two items, as the flex `gap` below sets it, by default. */
|
|
53
53
|
const ITEM_GAP = 2;
|
|
54
54
|
/** Room kept at the end of the line for the button opening the rest. */
|
|
@@ -81,11 +81,25 @@ function tidyDividers(list) {
|
|
|
81
81
|
* line first among equals — with every priority equal, the tail of the
|
|
82
82
|
* line, as a reader expects.
|
|
83
83
|
*/
|
|
84
|
-
export function overflowKeys(ordered, widthOf, available, gap = ITEM_GAP, triggerWidth = OVERFLOW_TRIGGER_WIDTH
|
|
84
|
+
export function overflowKeys(ordered, widthOf, available, gap = ITEM_GAP, triggerWidth = OVERFLOW_TRIGGER_WIDTH,
|
|
85
|
+
/**
|
|
86
|
+
* Whether an item is laid out on the line: one that draws nothing yet
|
|
87
|
+
* (a chip waiting for its data) is 0px wide and still takes its gap.
|
|
88
|
+
* Unsaid, an item 0px wide takes none.
|
|
89
|
+
*/
|
|
90
|
+
laidOut) {
|
|
85
91
|
const hidden = new Set();
|
|
86
|
-
|
|
92
|
+
// A spacer takes what is left, and never less than its minimum: that minimum, and its gap, are on the line
|
|
93
|
+
// whatever the room (the chat header's leading spacer, 8px and a gap, pushed its last button past the edge).
|
|
94
|
+
const widthOfItem = (item) => item.type === "spacer" ? SPACER_MIN_WIDTH : widthOf(item.key);
|
|
87
95
|
const used = () => {
|
|
88
|
-
|
|
96
|
+
// What the line holds, as the browser lays it out: a 0px item that is
|
|
97
|
+
// on the line takes its gap like any other (the Accountant's two empty
|
|
98
|
+
// chips pushed its last button 2px past the edge, 28 November 2026).
|
|
99
|
+
const shown = ordered.filter((item) => !hidden.has(item.key) &&
|
|
100
|
+
(item.type === "spacer" ||
|
|
101
|
+
widthOfItem(item) > 0 ||
|
|
102
|
+
laidOut?.(item.key) === true));
|
|
89
103
|
const total = shown.reduce((sum, item) => sum + widthOfItem(item), 0);
|
|
90
104
|
return (total +
|
|
91
105
|
Math.max(0, shown.length - 1) * gap +
|
|
@@ -106,7 +120,22 @@ export function overflowKeys(ordered, widthOf, available, gap = ITEM_GAP, trigge
|
|
|
106
120
|
}
|
|
107
121
|
return hidden;
|
|
108
122
|
}
|
|
109
|
-
|
|
123
|
+
/**
|
|
124
|
+
* The least room a line needs: the items that never leave it — pinned, spacers at their minimum, and those laid
|
|
125
|
+
* out empty, which still take their gap — and, when any item can leave, the menu's button with its gap.
|
|
126
|
+
* Exported for tests.
|
|
127
|
+
*/
|
|
128
|
+
export function minimumWidthOf(ordered, widthOf, gap = ITEM_GAP, triggerWidth = OVERFLOW_TRIGGER_WIDTH, laidOut) {
|
|
129
|
+
const width = (item) => (item.type === "spacer" ? SPACER_MIN_WIDTH : widthOf(item.key));
|
|
130
|
+
const stays = ordered.filter((item) => item.type === "spacer" ||
|
|
131
|
+
(item.pinned === true && width(item) > 0) ||
|
|
132
|
+
(width(item) === 0 && laidOut?.(item.key) === true));
|
|
133
|
+
const leaves = ordered.some((item) => !item.pinned && item.type !== "spacer" && width(item) > 0);
|
|
134
|
+
return (stays.reduce((sum, item) => sum + width(item), 0) +
|
|
135
|
+
Math.max(0, stays.length - 1) * gap +
|
|
136
|
+
(leaves ? gap + triggerWidth : 0));
|
|
137
|
+
}
|
|
138
|
+
export function Toolbar({ items, extraItems, className, disabled, ariaLabel = "Editor toolbar", overflow = true, variant = "bar", gap = ITEM_GAP, overflowAriaLabel, keyboardNavigation = true, onMinimumWidth, }) {
|
|
110
139
|
const allItems = useMemo(() => {
|
|
111
140
|
if (!extraItems?.length)
|
|
112
141
|
return items;
|
|
@@ -138,6 +167,8 @@ export function Toolbar({ items, extraItems, className, disabled, ariaLabel = "E
|
|
|
138
167
|
.sort((a, b) => (a.order ?? 100) - (b.order ?? 100)), [allItems]);
|
|
139
168
|
const containerRef = useRef(null);
|
|
140
169
|
const rowRef = useRef(null);
|
|
170
|
+
/** Room the line takes that the sum of its items does not account for. */
|
|
171
|
+
const unseen = useRef(0);
|
|
141
172
|
/*
|
|
142
173
|
* The width of every item, by key, measured while it was on the line.
|
|
143
174
|
*
|
|
@@ -148,6 +179,11 @@ export function Toolbar({ items, extraItems, className, disabled, ariaLabel = "E
|
|
|
148
179
|
*/
|
|
149
180
|
const widths = useRef(new Map());
|
|
150
181
|
const [hiddenKeys, setHiddenKeys] = useState(() => new Set());
|
|
182
|
+
// What is off the line now, for the measurement to compare its answer with.
|
|
183
|
+
const hiddenNow = useRef(hiddenKeys);
|
|
184
|
+
hiddenNow.current = hiddenKeys;
|
|
185
|
+
// The least room last told to the host.
|
|
186
|
+
const minimumNow = useRef(-1);
|
|
151
187
|
const [menuOpen, setMenuOpen] = useState(false);
|
|
152
188
|
const keys = useMemo(() => ordered.map((item) => item.key).join("\u0000"), [ordered]);
|
|
153
189
|
const measure = useCallback(() => {
|
|
@@ -166,12 +202,16 @@ export function Toolbar({ items, extraItems, className, disabled, ariaLabel = "E
|
|
|
166
202
|
*/
|
|
167
203
|
// Read off the wrappers' attribute rather than built into a selector: a
|
|
168
204
|
// key is the host's text, and `CSS.escape` is not everywhere (jsdom).
|
|
205
|
+
const onTheLine = new Set();
|
|
169
206
|
row
|
|
170
207
|
.querySelectorAll("[data-toolbar-key]")
|
|
171
208
|
.forEach((node) => {
|
|
172
209
|
const key = node.getAttribute("data-toolbar-key");
|
|
173
210
|
if (key !== null) {
|
|
174
211
|
widths.current.set(key, node.getBoundingClientRect().width);
|
|
212
|
+
if (getComputedStyle(node).display !== "none") {
|
|
213
|
+
onTheLine.add(key);
|
|
214
|
+
}
|
|
175
215
|
}
|
|
176
216
|
});
|
|
177
217
|
const style = getComputedStyle(container);
|
|
@@ -196,15 +236,40 @@ export function Toolbar({ items, extraItems, className, disabled, ariaLabel = "E
|
|
|
196
236
|
}
|
|
197
237
|
// An item never measured is assumed to fit; the next pass, once it has
|
|
198
238
|
// been on the line, knows better.
|
|
199
|
-
const
|
|
239
|
+
const widthOf = (key) => widths.current.get(key) ?? 0;
|
|
240
|
+
const laidOut = (key) => onTheLine.has(key);
|
|
241
|
+
/*
|
|
242
|
+
* The browser has the last word. Where the sum says the line fits and
|
|
243
|
+
* the line runs past the edge anyway (a gap or a margin the sum does
|
|
244
|
+
* not know of), the excess is remembered and taken off the room — kept
|
|
245
|
+
* until the items change, so that moving one into the menu, which ends
|
|
246
|
+
* the excess, does not bring it straight back.
|
|
247
|
+
*/
|
|
248
|
+
let next = overflowKeys(ordered, widthOf, available - unseen.current, gap, undefined, laidOut);
|
|
249
|
+
// Read on the line itself: it shrinks beside the menu's button, and what runs past it runs under that button.
|
|
250
|
+
// Only when the sum agrees with what is drawn: mid-change, the line can still show the items the sum moved.
|
|
251
|
+
const excess = row.scrollWidth - row.clientWidth;
|
|
252
|
+
const agrees = next.size === hiddenNow.current.size &&
|
|
253
|
+
[...next].every((key) => hiddenNow.current.has(key));
|
|
254
|
+
if (agrees && excess > 0.5) {
|
|
255
|
+
unseen.current += excess;
|
|
256
|
+
next = overflowKeys(ordered, widthOf, available - unseen.current, gap, undefined, laidOut);
|
|
257
|
+
}
|
|
258
|
+
// The least room the line needs, told to the host when it changes.
|
|
259
|
+
const minimum = Math.ceil(minimumWidthOf(ordered, widthOf, gap, undefined, laidOut));
|
|
260
|
+
if (minimum !== minimumNow.current) {
|
|
261
|
+
minimumNow.current = minimum;
|
|
262
|
+
onMinimumWidth?.(minimum);
|
|
263
|
+
}
|
|
200
264
|
setHiddenKeys((previous) => previous.size === next.size && [...next].every((key) => previous.has(key))
|
|
201
265
|
? previous
|
|
202
266
|
: next);
|
|
203
|
-
}, [ordered, gap]);
|
|
267
|
+
}, [ordered, gap, onMinimumWidth]);
|
|
204
268
|
useLayoutEffect(() => {
|
|
205
269
|
// A new set of items is shown whole first, so each one is measured on
|
|
206
270
|
// the line before anything is taken off it.
|
|
207
271
|
setHiddenKeys(new Set());
|
|
272
|
+
unseen.current = 0;
|
|
208
273
|
}, [keys, overflow]);
|
|
209
274
|
useLayoutEffect(() => {
|
|
210
275
|
if (!overflow) {
|
|
@@ -229,7 +294,36 @@ export function Toolbar({ items, extraItems, className, disabled, ariaLabel = "E
|
|
|
229
294
|
row
|
|
230
295
|
.querySelectorAll("[data-toolbar-key]")
|
|
231
296
|
.forEach((node) => observer.observe(node));
|
|
232
|
-
|
|
297
|
+
/*
|
|
298
|
+
* And what changes without changing size: a chip that waits for its data
|
|
299
|
+
* is put on the line still 0px wide, which takes a gap and resizes
|
|
300
|
+
* nothing — the Accountant's two empty chips pushed the agent chooser
|
|
301
|
+
* past the edge on first load, unheard (28 November 2026).
|
|
302
|
+
*/
|
|
303
|
+
let frame = 0;
|
|
304
|
+
const mutations = typeof MutationObserver === "undefined"
|
|
305
|
+
? null
|
|
306
|
+
: new MutationObserver(() => {
|
|
307
|
+
if (frame === 0) {
|
|
308
|
+
frame = requestAnimationFrame(() => {
|
|
309
|
+
frame = 0;
|
|
310
|
+
measure();
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
mutations?.observe(row, {
|
|
315
|
+
subtree: true,
|
|
316
|
+
childList: true,
|
|
317
|
+
attributes: true,
|
|
318
|
+
attributeFilter: ["style", "class", "hidden"],
|
|
319
|
+
});
|
|
320
|
+
return () => {
|
|
321
|
+
observer.disconnect();
|
|
322
|
+
mutations?.disconnect();
|
|
323
|
+
if (frame !== 0) {
|
|
324
|
+
cancelAnimationFrame(frame);
|
|
325
|
+
}
|
|
326
|
+
};
|
|
233
327
|
}, [measure, overflow, hiddenKeys]);
|
|
234
328
|
const onKeyDown = useCallback((event) => {
|
|
235
329
|
if (!keyboardNavigation) {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { ToolbarItem } from "./types";
|
|
2
|
+
/** The least a spacer takes on the line: the overflow counts it, so the two never disagree. */
|
|
3
|
+
export declare const SPACER_MIN_WIDTH = 8;
|
|
2
4
|
export interface ToolbarRendererProps {
|
|
3
5
|
items: ToolbarItem[];
|
|
4
6
|
disabled?: boolean;
|
|
@@ -15,6 +15,8 @@ import { Box } from "../box/Box";
|
|
|
15
15
|
import { ToolbarButton } from "./ToolbarButton";
|
|
16
16
|
import { ToolbarDropdown } from "./ToolbarDropdown";
|
|
17
17
|
import { ToolbarDivider } from "./ToolbarDivider";
|
|
18
|
+
/** The least a spacer takes on the line: the overflow counts it, so the two never disagree. */
|
|
19
|
+
export const SPACER_MIN_WIDTH = 8;
|
|
18
20
|
/**
|
|
19
21
|
* Sort items by order, then render each one according to its type.
|
|
20
22
|
*/
|
|
@@ -32,7 +34,7 @@ export function ToolbarRenderer({ items, disabled, size = "medium", showLabel =
|
|
|
32
34
|
case "divider":
|
|
33
35
|
return (_jsx(Fragment, { children: _jsx(ToolbarDivider, { orientation: direction === "column" ? "horizontal" : "vertical" }) }, item.key));
|
|
34
36
|
case "spacer":
|
|
35
|
-
return (_jsx(Box, { sx: { flex: "1 1 auto", minWidth:
|
|
37
|
+
return (_jsx(Box, { sx: { flex: "1 1 auto", minWidth: SPACER_MIN_WIDTH } }, item.key));
|
|
36
38
|
case "custom":
|
|
37
39
|
return _jsx(Fragment, { children: item.render() }, item.key);
|
|
38
40
|
default:
|
|
@@ -181,6 +181,12 @@ export interface ToolbarProps {
|
|
|
181
181
|
* default.
|
|
182
182
|
*/
|
|
183
183
|
keyboardNavigation?: boolean;
|
|
184
|
+
/**
|
|
185
|
+
* The least room the line needs, in pixels: what never leaves it (pinned items, spacers at their minimum, the
|
|
186
|
+
* gaps of items laid out empty) and, when anything can leave, the menu's button. Called when it changes, so a
|
|
187
|
+
* host can keep that room for the toolbar — what is beside it gives way instead of running under it.
|
|
188
|
+
*/
|
|
189
|
+
onMinimumWidth?: (width: number) => void;
|
|
184
190
|
}
|
|
185
191
|
/**
|
|
186
192
|
* Props for the extensible FloatingToolbar component.
|