@bigpixelrocket/uiok 0.4.0 → 0.6.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 +63 -1
- package/dist/button/TextLink.d.ts +26 -3
- package/dist/button/TextLink.js +5 -3
- package/dist/button/button.css +7 -0
- package/dist/dialog/ModalDialog.js +8 -2
- package/dist/dialog/SectionedDialog.d.ts +37 -0
- package/dist/dialog/SectionedDialog.js +157 -0
- package/dist/dialog/dialog.css +102 -0
- package/dist/dialog/index.d.ts +4 -2
- package/dist/dialog/index.js +4 -2
- package/dist/frame/AppRoot.d.ts +1 -1
- package/dist/frame/AppRoot.js +2 -1
- package/dist/frame/Dock.d.ts +1 -1
- package/dist/frame/Dock.js +2 -1
- package/dist/frame/FilterChips.d.ts +5 -2
- package/dist/frame/FilterChips.js +35 -2
- package/dist/frame/FilterChips.logic.d.ts +24 -0
- package/dist/frame/FilterChips.logic.js +34 -0
- package/dist/frame/PageParts.d.ts +4 -3
- package/dist/frame/PageParts.js +10 -3
- package/dist/frame/SideBySide.d.ts +32 -0
- package/dist/frame/SideBySide.js +50 -0
- package/dist/frame/Tree.d.ts +9 -3
- package/dist/frame/Tree.js +8 -5
- package/dist/frame/WindowFrame.d.ts +1 -1
- package/dist/frame/WindowFrame.js +3 -1
- package/dist/frame/frame.css +74 -5
- package/dist/frame/index.d.ts +4 -2
- package/dist/frame/index.js +4 -2
- package/dist/grid/grid.css +1 -0
- package/dist/icon/ProductMark.d.ts +57 -0
- package/dist/icon/ProductMark.js +26 -0
- package/dist/icon/icon.css +53 -0
- package/dist/icon/index.d.ts +3 -1
- package/dist/icon/index.js +3 -1
- package/dist/keyboard/Keyboard.logic.js +20 -3
- package/dist/keychord/KeyChord.d.ts +17 -1
- package/dist/keychord/KeyChord.js +16 -1
- package/dist/keychord/ShortcutIndex.d.ts +49 -0
- package/dist/keychord/ShortcutIndex.js +28 -0
- package/dist/keychord/index.d.ts +8 -2
- package/dist/keychord/index.js +8 -2
- package/dist/keychord/keychord.css +101 -0
- package/dist/labels/dialog.d.ts +2 -0
- package/dist/labels/dialog.js +2 -1
- package/dist/labels/keys.d.ts +5 -0
- package/dist/labels/keys.js +2 -1
- package/dist/labels/settings.d.ts +6 -0
- package/dist/labels/settings.js +1 -0
- package/dist/listing/Listing.d.ts +95 -7
- package/dist/listing/Listing.js +57 -13
- package/dist/listing/ListingScope.d.ts +7 -0
- package/dist/listing/ListingScope.js +8 -0
- package/dist/listing/PaneMessage.d.ts +7 -4
- package/dist/listing/PaneMessage.js +11 -4
- package/dist/listing/Timeline.d.ts +51 -0
- package/dist/listing/Timeline.js +28 -0
- package/dist/listing/index.d.ts +4 -2
- package/dist/listing/index.js +3 -1
- package/dist/listing/listing.css +268 -25
- package/dist/log/Ansi.logic.d.ts +27 -0
- package/dist/log/Ansi.logic.js +203 -0
- package/dist/log/LogWell.d.ts +11 -2
- package/dist/log/LogWell.js +15 -3
- package/dist/log/index.d.ts +1 -0
- package/dist/log/index.js +1 -0
- package/dist/log/log.css +80 -0
- package/dist/menu/Menu.d.ts +133 -0
- package/dist/menu/Menu.js +351 -0
- package/dist/menu/Menu.logic.d.ts +83 -0
- package/dist/menu/Menu.logic.js +92 -0
- package/dist/menu/index.d.ts +6 -1
- package/dist/menu/index.js +6 -1
- package/dist/menu/menu.css +57 -0
- package/dist/palette/Palette.d.ts +3 -2
- package/dist/palette/Palette.js +8 -4
- package/dist/palette/PaletteRows.d.ts +138 -0
- package/dist/palette/PaletteRows.js +76 -0
- package/dist/palette/index.d.ts +5 -2
- package/dist/palette/index.js +5 -2
- package/dist/pane/Pane.d.ts +42 -6
- package/dist/pane/Pane.js +92 -5
- package/dist/pane/PaneFrame.d.ts +22 -1
- package/dist/pane/PaneFrame.js +42 -5
- package/dist/pane/PaneKeys.d.ts +5 -1
- package/dist/pane/PaneKeys.js +28 -3
- package/dist/pane/PaneKeys.logic.d.ts +25 -0
- package/dist/pane/PaneKeys.logic.js +30 -0
- package/dist/pane/index.d.ts +2 -2
- package/dist/pane/index.js +1 -1
- package/dist/pane/usePaneKeys.d.ts +8 -1
- package/dist/pane/usePaneKeys.js +32 -12
- package/dist/shell/SettingsDialog.d.ts +20 -2
- package/dist/shell/SettingsDialog.js +13 -3
- package/dist/shell/index.d.ts +1 -1
- package/dist/styles.css +18 -3
- package/dist/surface/FactList.d.ts +54 -0
- package/dist/surface/FactList.js +33 -0
- package/dist/surface/FactTable.d.ts +44 -0
- package/dist/surface/FactTable.js +33 -0
- package/dist/surface/index.d.ts +7 -2
- package/dist/surface/index.js +7 -2
- package/dist/surface/surface.css +139 -2
- package/dist/tabs/PageTabs.d.ts +6 -0
- package/dist/tabs/PageTabs.js +1 -1
- package/dist/tabs/tabs.css +14 -0
- package/dist/terminal/TerminalBox.d.ts +30 -0
- package/dist/terminal/TerminalBox.js +17 -0
- package/dist/terminal/ghostty/core.d.ts +12 -4
- package/dist/terminal/ghostty/core.js +12 -4
- package/dist/terminal/index.d.ts +6 -2
- package/dist/terminal/index.js +6 -2
- package/dist/terminal/terminal.css +29 -0
- package/dist/text/Code.d.ts +15 -0
- package/dist/text/Code.js +12 -0
- package/dist/text/index.d.ts +6 -1
- package/dist/text/index.js +6 -1
- package/dist/text/text.css +21 -0
- package/dist/themes/themes.generated.css +20 -0
- package/dist/themes/tokens.d.ts +1 -0
- package/dist/themes/tokens.js +16 -2
- package/package.json +1 -1
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState } from 'react';
|
|
3
|
+
import { createPortal } from 'react-dom';
|
|
4
|
+
import { ICON_MD } from '../icon/sizes.js';
|
|
5
|
+
import { KeyChord } from '../keychord/KeyChord.js';
|
|
6
|
+
import { CurrentDot } from '../listing/CurrentDot.js';
|
|
7
|
+
import { describedTooltip } from '../tooltip/Tooltip.logic.js';
|
|
8
|
+
import { menuKeyAction, placeMenu } from './Menu.logic.js';
|
|
9
|
+
import { movedPickableCursor, pickableCursor } from './MenuQuery.logic.js';
|
|
10
|
+
/** Scrolls a menu's item at `position` into the menu's view, the least it takes; jsdom has no scrollIntoView. */
|
|
11
|
+
const revealItem = (itemId, position) => {
|
|
12
|
+
if (position === null)
|
|
13
|
+
return;
|
|
14
|
+
document.getElementById(`${itemId}-${position}`)?.scrollIntoView?.({ block: 'nearest' });
|
|
15
|
+
};
|
|
16
|
+
const isRow = (entry) => entry.kind === undefined || entry.kind === 'item' || entry.kind === 'submenu';
|
|
17
|
+
const isItem = (entry) => entry.kind === undefined || entry.kind === 'item';
|
|
18
|
+
const pickableRows = (rows) => rows.map((row) => ({ key: row.id, taken: row.disabled === true }));
|
|
19
|
+
/** Whether an element reads right to left, where a submenu stands on the left. */
|
|
20
|
+
const isRtl = (element) => getComputedStyle(element).direction === 'rtl';
|
|
21
|
+
const NO_BOX = { left: 0, top: 0, right: 0, bottom: 0 };
|
|
22
|
+
/**
|
|
23
|
+
* How long the pointer may cross other items on its way into an open
|
|
24
|
+
* submenu, in milliseconds, before the item it stays on takes the cursor
|
|
25
|
+
* and the submenu gives way.
|
|
26
|
+
*/
|
|
27
|
+
const SUBMENU_GRACE_MS = 250;
|
|
28
|
+
/**
|
|
29
|
+
* The surface a menu or its submenu is drawn on, through a portal on the
|
|
30
|
+
* document's body (or the element shown full screen), placed after layout,
|
|
31
|
+
* when its size is known, and placed again when the window resizes, the
|
|
32
|
+
* cursor's item kept in view.
|
|
33
|
+
*/
|
|
34
|
+
function MenuSurface({ surfaceRef, id, label, labelledBy, entries, cursor, itemId, expanded, compact, at, anchor, besideId, onKeyDown, onBlur, onMouseMove, onMouseLeave, dir, onRowClick, onRowMove }) {
|
|
35
|
+
const cursorRef = useRef(cursor);
|
|
36
|
+
useLayoutEffect(() => {
|
|
37
|
+
cursorRef.current = cursor;
|
|
38
|
+
});
|
|
39
|
+
// Placed again when its menu moves or turns direction: a submenu is given
|
|
40
|
+
// its menu's point and anchor too, so it follows the item it stands beside,
|
|
41
|
+
// and turns sides with the direction, which places a menu itself nowhere new.
|
|
42
|
+
const turn = besideId === undefined ? undefined : dir;
|
|
43
|
+
useLayoutEffect(() => {
|
|
44
|
+
const menu = surfaceRef.current;
|
|
45
|
+
if (menu === null)
|
|
46
|
+
return;
|
|
47
|
+
const from = () => {
|
|
48
|
+
if (besideId !== undefined) {
|
|
49
|
+
const item = document.getElementById(besideId);
|
|
50
|
+
return {
|
|
51
|
+
beside: item?.getBoundingClientRect() ?? NO_BOX,
|
|
52
|
+
rtl: item !== null && isRtl(item)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return at !== undefined ? { point: at } : { box: anchor?.getBoundingClientRect() ?? NO_BOX };
|
|
56
|
+
};
|
|
57
|
+
const place = () => {
|
|
58
|
+
const size = { width: menu.offsetWidth, height: menu.offsetHeight };
|
|
59
|
+
const viewport = { width: window.innerWidth, height: window.innerHeight };
|
|
60
|
+
const spot = placeMenu(from(), size, viewport);
|
|
61
|
+
menu.style.left = `${spot.left}px`;
|
|
62
|
+
menu.style.top = `${spot.top}px`;
|
|
63
|
+
};
|
|
64
|
+
// A window that shrinks under a tall menu can hide the cursor's item.
|
|
65
|
+
const replace = () => {
|
|
66
|
+
place();
|
|
67
|
+
revealItem(itemId, cursorRef.current);
|
|
68
|
+
};
|
|
69
|
+
place();
|
|
70
|
+
// A menu taller than the window scrolls; it opens with its cursor in view.
|
|
71
|
+
revealItem(itemId, cursorRef.current);
|
|
72
|
+
window.addEventListener('resize', replace);
|
|
73
|
+
return () => window.removeEventListener('resize', replace);
|
|
74
|
+
}, [at, anchor, besideId, turn, itemId, surfaceRef]);
|
|
75
|
+
let index = -1;
|
|
76
|
+
return createPortal(_jsx("div", { "aria-activedescendant": cursor === null ? undefined : `${itemId}-${cursor}`, "aria-label": label, "aria-labelledby": labelledBy, className: compact ? 'folder-menu folder-menu-compact menu-popup' : 'folder-menu menu-popup', "data-native-keyboard": true, "data-overlay": true, dir: dir, id: id, onBlur: onBlur, onContextMenu: (event) => event.preventDefault(), onKeyDown: onKeyDown,
|
|
77
|
+
// A press on an item must not take the focus from the menu, which holds the keys.
|
|
78
|
+
onMouseDown: (event) => event.preventDefault(), onMouseLeave: onMouseLeave, onMouseMove: onMouseMove, ref: surfaceRef, role: "menu", tabIndex: -1, children: entries.map((entry, entryIndex) => {
|
|
79
|
+
if (entry.kind === 'separator') {
|
|
80
|
+
return (_jsx("div", { className: "folder-menu-separator", role: "separator" }, `separator-${entryIndex}`));
|
|
81
|
+
}
|
|
82
|
+
if (entry.kind === 'heading') {
|
|
83
|
+
return (_jsx("div", { className: "entry-menu-heading", role: "presentation", children: entry.label }, `heading-${entryIndex}`));
|
|
84
|
+
}
|
|
85
|
+
index += 1;
|
|
86
|
+
const position = index;
|
|
87
|
+
const Icon = entry.icon;
|
|
88
|
+
const submenu = entry.kind === 'submenu';
|
|
89
|
+
const open = submenu && expanded?.id === entry.id;
|
|
90
|
+
const classes = [
|
|
91
|
+
'folder-menu-option',
|
|
92
|
+
position === cursor ? 'cursor' : '',
|
|
93
|
+
!submenu && entry.danger === true ? 'danger' : ''
|
|
94
|
+
]
|
|
95
|
+
.filter(Boolean)
|
|
96
|
+
.join(' ');
|
|
97
|
+
return (_jsxs("div", { "aria-checked": submenu ? undefined : entry.checked, "aria-controls": open ? expanded.menu : undefined, "aria-disabled": entry.disabled === true || undefined, "aria-expanded": submenu ? open : undefined, "aria-haspopup": submenu ? 'menu' : undefined, className: classes, id: `${itemId}-${position}`, onClick: () => onRowClick(entry), onMouseMove: () => onRowMove(entry), role: submenu || entry.checked === undefined ? 'menuitem' : 'menuitemcheckbox', ...describedTooltip(entry.tip), children: [_jsx("span", { "aria-hidden": true, className: "row-icon", children: Icon === undefined ? entry.mark : _jsx(Icon, { size: ICON_MD }) }), _jsxs("span", { className: "menu-label", children: [_jsx("span", { className: "menu-label-text", children: entry.label }), submenu || entry.current === undefined || entry.current === null ? null : (_jsx(CurrentDot, { label: entry.current }))] }), submenu || entry.chord === undefined ? null : _jsx(KeyChord, { chord: entry.chord })] }, entry.id));
|
|
98
|
+
}) }), document.fullscreenElement ?? document.body);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* A menu of verbs or places, opened at a point (a secondary click's) or
|
|
102
|
+
* dropped from an element (a crumb's ⌄, an Actions button): each item its
|
|
103
|
+
* glyph, its words, and its chord as key caps; a destructive one in the
|
|
104
|
+
* danger tone; a setting with its check; the current place with its dot;
|
|
105
|
+
* separators and group headings between them; and items that open a
|
|
106
|
+
* submenu beside them, one level deep. It is the menu look
|
|
107
|
+
* (`.folder-menu`) drawn through a portal on the document's body (or the
|
|
108
|
+
* element shown full screen), so no pane or dialog clips it, placed inside
|
|
109
|
+
* the window (`placeMenu`); React still carries its events to its owner.
|
|
110
|
+
*
|
|
111
|
+
* It takes the focus and holds the keys while open (`menuKeyAction`: ↑ ↓,
|
|
112
|
+
* Home, End, Return and Space, → and ←, Escape and Tab), which travel no
|
|
113
|
+
* further, so neither a dialog nor the page hears them; `data-native-keyboard`
|
|
114
|
+
* keeps the browser-key guard and a page's own key handlers out
|
|
115
|
+
* (`isNativeKeyboardTarget`), and `data-overlay` marks it an open overlay
|
|
116
|
+
* while it shows. A press on it never takes the focus from it; a press or
|
|
117
|
+
* a focus anywhere else closes it. A menu taller than the window less its
|
|
118
|
+
* margins scrolls, and the keys keep the cursor's item in view.
|
|
119
|
+
*
|
|
120
|
+
* A submenu opens beside its item (on the inline-end side, or the other
|
|
121
|
+
* when the window has no room there) when the pointer rests on the item or
|
|
122
|
+
* presses it, showing without the keys, or with the keys and its cursor on
|
|
123
|
+
* its first item that can run on → (← in a right-to-left page), Return,
|
|
124
|
+
* or Space. ← goes back to its item, and Escape closes the submenu alone;
|
|
125
|
+
* Tab closes the whole menu. The pointer crossing other items on its way
|
|
126
|
+
* into the submenu leaves it open for a moment, and moving over the
|
|
127
|
+
* submenu gives it the keys.
|
|
128
|
+
*/
|
|
129
|
+
export function Menu({ label, entries, at, anchor = null, initial = null, compact = false, dir: dirGiven, onPick, onClose }) {
|
|
130
|
+
const rootRef = useRef(null);
|
|
131
|
+
const originRef = useRef(null);
|
|
132
|
+
const [dirFound, setDirFound] = useState(undefined);
|
|
133
|
+
const dir = dirGiven ?? dirFound;
|
|
134
|
+
const subRef = useRef(null);
|
|
135
|
+
const itemId = useId();
|
|
136
|
+
const subItemId = useId();
|
|
137
|
+
const [pointed, setPointed] = useState(initial);
|
|
138
|
+
const [subState, setSub] = useState(null);
|
|
139
|
+
const grace = useRef(null);
|
|
140
|
+
const rows = entries.filter(isRow);
|
|
141
|
+
const pickable = pickableRows(rows);
|
|
142
|
+
const cursor = pickableCursor(pickable, pointed);
|
|
143
|
+
const cursorRow = cursor === null ? null : (rows[cursor] ?? null);
|
|
144
|
+
const subPlace = subState === null
|
|
145
|
+
? -1
|
|
146
|
+
: rows.findIndex((row) => row.kind === 'submenu' && row.id === subState.id && !row.disabled);
|
|
147
|
+
const subEntry = subPlace === -1 ? null : rows[subPlace];
|
|
148
|
+
// The submenu shown: none once its item is disabled or gone from `entries`.
|
|
149
|
+
const sub = subEntry === null ? null : subState;
|
|
150
|
+
const subRows = subEntry === null ? [] : subEntry.entries.filter(isItem);
|
|
151
|
+
const subPickable = pickableRows(subRows);
|
|
152
|
+
const subCursor = sub === null || sub.pointed === null ? null : pickableCursor(subPickable, sub.pointed);
|
|
153
|
+
const subCursorRow = subCursor === null ? null : (subRows[subCursor] ?? null);
|
|
154
|
+
const keysInSub = subEntry !== null && sub?.keys === true;
|
|
155
|
+
const onCloseRef = useRef(onClose);
|
|
156
|
+
const subCursorRef = useRef(subCursor);
|
|
157
|
+
useLayoutEffect(() => {
|
|
158
|
+
onCloseRef.current = onClose;
|
|
159
|
+
subCursorRef.current = subCursor;
|
|
160
|
+
});
|
|
161
|
+
// Drawn on the body, it reads in the direction of what opened it, read
|
|
162
|
+
// again when the anchor changes or a given direction is taken away (the
|
|
163
|
+
// marker the fallback reads mounts only then).
|
|
164
|
+
useLayoutEffect(() => {
|
|
165
|
+
const opener = anchor ?? originRef.current;
|
|
166
|
+
if (opener !== null)
|
|
167
|
+
setDirFound(isRtl(opener) ? 'rtl' : 'ltr');
|
|
168
|
+
}, [anchor, dirGiven]);
|
|
169
|
+
// An item disabled or gone takes its submenu with it.
|
|
170
|
+
useEffect(() => {
|
|
171
|
+
if (subState !== null && subEntry === null)
|
|
172
|
+
setSub(null);
|
|
173
|
+
}, [subState, subEntry]);
|
|
174
|
+
// The keys stay where the menu holds them, the menu or its submenu, and
|
|
175
|
+
// come back to it when it moves (the anchor may have taken them). Keys
|
|
176
|
+
// taking over a submenu the pointer opened bring its cursor into view.
|
|
177
|
+
useLayoutEffect(() => {
|
|
178
|
+
(keysInSub ? subRef : rootRef).current?.focus({ preventScroll: true });
|
|
179
|
+
if (keysInSub)
|
|
180
|
+
revealItem(subItemId, subCursorRef.current);
|
|
181
|
+
}, [keysInSub, at, anchor, subItemId]);
|
|
182
|
+
// A press outside closes it, but one on its anchor, which toggles it.
|
|
183
|
+
useEffect(() => {
|
|
184
|
+
const listener = (event) => {
|
|
185
|
+
const target = event.target;
|
|
186
|
+
if (!(target instanceof Node))
|
|
187
|
+
return;
|
|
188
|
+
if (rootRef.current?.contains(target) ||
|
|
189
|
+
subRef.current?.contains(target) ||
|
|
190
|
+
anchor?.contains(target))
|
|
191
|
+
return;
|
|
192
|
+
onCloseRef.current('pointer');
|
|
193
|
+
};
|
|
194
|
+
document.addEventListener('mousedown', listener, true);
|
|
195
|
+
return () => document.removeEventListener('mousedown', listener, true);
|
|
196
|
+
}, [anchor]);
|
|
197
|
+
useEffect(() => () => {
|
|
198
|
+
if (grace.current !== null)
|
|
199
|
+
window.clearTimeout(grace.current.timer);
|
|
200
|
+
}, []);
|
|
201
|
+
const clearGrace = () => {
|
|
202
|
+
if (grace.current === null)
|
|
203
|
+
return;
|
|
204
|
+
window.clearTimeout(grace.current.timer);
|
|
205
|
+
grace.current = null;
|
|
206
|
+
};
|
|
207
|
+
const run = (item) => {
|
|
208
|
+
if (item !== null && item.kind !== 'submenu' && item.disabled !== true)
|
|
209
|
+
onPick(item.id);
|
|
210
|
+
};
|
|
211
|
+
// The keys go back to the menu before the submenu goes, so its unmount never drops the focus.
|
|
212
|
+
const closeSub = () => {
|
|
213
|
+
clearGrace();
|
|
214
|
+
if (subRef.current?.contains(document.activeElement)) {
|
|
215
|
+
rootRef.current?.focus({ preventScroll: true });
|
|
216
|
+
}
|
|
217
|
+
setSub(null);
|
|
218
|
+
};
|
|
219
|
+
// With the keys, its cursor starts on its first item that can run; for the pointer, on none.
|
|
220
|
+
const openSub = (item, keys) => {
|
|
221
|
+
clearGrace();
|
|
222
|
+
const items = item.entries.filter(isItem);
|
|
223
|
+
const first = keys ? pickableCursor(pickableRows(items), null) : null;
|
|
224
|
+
setPointed(item.id);
|
|
225
|
+
setSub({ id: item.id, keys, pointed: first === null ? null : (items[first]?.id ?? null) });
|
|
226
|
+
};
|
|
227
|
+
// The pointer rests on a row of the menu: it takes the cursor, and a submenu's opens.
|
|
228
|
+
const pointAt = (row) => {
|
|
229
|
+
clearGrace();
|
|
230
|
+
if (sub?.id === row.id)
|
|
231
|
+
return;
|
|
232
|
+
if (sub !== null)
|
|
233
|
+
closeSub();
|
|
234
|
+
if (row.disabled === true)
|
|
235
|
+
return;
|
|
236
|
+
if (row.kind === 'submenu')
|
|
237
|
+
openSub(row, false);
|
|
238
|
+
else
|
|
239
|
+
setPointed(row.id);
|
|
240
|
+
};
|
|
241
|
+
const within = (to) => to instanceof Node &&
|
|
242
|
+
(rootRef.current?.contains(to) === true ||
|
|
243
|
+
subRef.current?.contains(to) === true ||
|
|
244
|
+
anchor?.contains(to) === true);
|
|
245
|
+
const onBlur = (event) => {
|
|
246
|
+
if (!within(event.relatedTarget))
|
|
247
|
+
onClose('pointer');
|
|
248
|
+
};
|
|
249
|
+
const take = (event) => {
|
|
250
|
+
const action = menuKeyAction(event, isRtl(event.currentTarget));
|
|
251
|
+
if (action === null)
|
|
252
|
+
return null;
|
|
253
|
+
event.preventDefault();
|
|
254
|
+
event.stopPropagation();
|
|
255
|
+
return action;
|
|
256
|
+
};
|
|
257
|
+
const onMenuKey = (event) => {
|
|
258
|
+
const action = take(event);
|
|
259
|
+
if (action === null)
|
|
260
|
+
return;
|
|
261
|
+
switch (action.kind) {
|
|
262
|
+
case 'close':
|
|
263
|
+
// Escape closes a submenu the pointer opened before the menu.
|
|
264
|
+
if (action.all || sub === null)
|
|
265
|
+
onClose('keyboard');
|
|
266
|
+
else
|
|
267
|
+
closeSub();
|
|
268
|
+
return;
|
|
269
|
+
case 'back':
|
|
270
|
+
if (sub !== null)
|
|
271
|
+
closeSub();
|
|
272
|
+
return;
|
|
273
|
+
case 'open':
|
|
274
|
+
case 'run':
|
|
275
|
+
if (cursorRow?.kind === 'submenu')
|
|
276
|
+
openSub(cursorRow, true);
|
|
277
|
+
else if (action.kind === 'run')
|
|
278
|
+
run(cursorRow);
|
|
279
|
+
return;
|
|
280
|
+
default: {
|
|
281
|
+
const next = action.kind === 'move'
|
|
282
|
+
? movedPickableCursor(pickable, cursor, action.delta)
|
|
283
|
+
: movedPickableCursor(pickable, null, action.to === 'first' ? 1 : -1);
|
|
284
|
+
if (sub !== null)
|
|
285
|
+
closeSub();
|
|
286
|
+
setPointed(next === null ? null : (rows[next]?.id ?? null));
|
|
287
|
+
// Only the keys scroll: an item under the pointer is already in view.
|
|
288
|
+
revealItem(itemId, next);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
const onSubKey = (event) => {
|
|
293
|
+
const action = take(event);
|
|
294
|
+
if (action === null || sub === null)
|
|
295
|
+
return;
|
|
296
|
+
switch (action.kind) {
|
|
297
|
+
case 'close':
|
|
298
|
+
if (action.all)
|
|
299
|
+
onClose('keyboard');
|
|
300
|
+
else
|
|
301
|
+
closeSub();
|
|
302
|
+
return;
|
|
303
|
+
case 'back':
|
|
304
|
+
closeSub();
|
|
305
|
+
return;
|
|
306
|
+
case 'open':
|
|
307
|
+
return;
|
|
308
|
+
case 'run':
|
|
309
|
+
run(subCursorRow);
|
|
310
|
+
return;
|
|
311
|
+
default: {
|
|
312
|
+
const next = action.kind === 'move'
|
|
313
|
+
? movedPickableCursor(subPickable, subCursor, action.delta)
|
|
314
|
+
: movedPickableCursor(subPickable, null, action.to === 'first' ? 1 : -1);
|
|
315
|
+
setSub({ ...sub, pointed: next === null ? null : (subRows[next]?.id ?? null) });
|
|
316
|
+
revealItem(subItemId, next);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
const subMenuId = `${subItemId}-menu`;
|
|
321
|
+
return (_jsxs(_Fragment, { children: [dirGiven === undefined && anchor === null ? _jsx("span", { hidden: true, ref: originRef }) : null, _jsx(MenuSurface, { anchor: anchor, at: at, compact: compact, cursor: cursor, dir: dir, entries: entries, expanded: subEntry === null ? null : { id: subEntry.id, menu: subMenuId }, itemId: itemId, label: label, onBlur: onBlur, onKeyDown: onMenuKey, onMouseLeave: clearGrace, onRowClick: (row) => {
|
|
322
|
+
if (row.kind === 'submenu')
|
|
323
|
+
pointAt(row);
|
|
324
|
+
else
|
|
325
|
+
run(row);
|
|
326
|
+
}, onRowMove: (row) => {
|
|
327
|
+
if (sub === null || row.id === sub.id) {
|
|
328
|
+
pointAt(row);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
// On its way into the submenu the pointer may cross other items:
|
|
332
|
+
// the one it stays on takes the cursor once the moment passes.
|
|
333
|
+
if (grace.current !== null) {
|
|
334
|
+
grace.current.to = row;
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
grace.current = {
|
|
338
|
+
to: row,
|
|
339
|
+
timer: window.setTimeout(() => {
|
|
340
|
+
const to = grace.current?.to;
|
|
341
|
+
grace.current = null;
|
|
342
|
+
if (to !== undefined)
|
|
343
|
+
pointAt(to);
|
|
344
|
+
}, SUBMENU_GRACE_MS)
|
|
345
|
+
};
|
|
346
|
+
}, surfaceRef: rootRef }), subEntry === null ? null : (_jsx(MenuSurface, { anchor: anchor, at: at, besideId: `${itemId}-${subPlace}`, compact: compact, cursor: subCursor, dir: dir, entries: subEntry.entries, expanded: null, id: subMenuId, itemId: subItemId, labelledBy: `${itemId}-${subPlace}`, onBlur: onBlur, onKeyDown: onSubKey, onMouseLeave: clearGrace, onMouseMove: clearGrace, onRowClick: run, onRowMove: (row) => {
|
|
347
|
+
clearGrace();
|
|
348
|
+
if (row.disabled !== true && sub !== null)
|
|
349
|
+
setSub({ ...sub, pointed: row.id, keys: true });
|
|
350
|
+
}, surfaceRef: subRef }))] }));
|
|
351
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** A point in window coordinates: where a secondary click landed. */
|
|
2
|
+
export interface MenuPoint {
|
|
3
|
+
readonly x: number;
|
|
4
|
+
readonly y: number;
|
|
5
|
+
}
|
|
6
|
+
/** A box in window coordinates: what a menu drops from (a crumb, a button). */
|
|
7
|
+
export interface MenuBox {
|
|
8
|
+
readonly left: number;
|
|
9
|
+
readonly top: number;
|
|
10
|
+
readonly right: number;
|
|
11
|
+
readonly bottom: number;
|
|
12
|
+
}
|
|
13
|
+
/** A size in pixels. */
|
|
14
|
+
export interface MenuSize {
|
|
15
|
+
readonly width: number;
|
|
16
|
+
readonly height: number;
|
|
17
|
+
}
|
|
18
|
+
/** The room a menu keeps from the window's edges, and the gap it leaves under what it drops from, in pixels. */
|
|
19
|
+
export declare const MENU_MARGIN = 4;
|
|
20
|
+
/**
|
|
21
|
+
* Where a menu opens from: a point (a secondary click's), a box it drops
|
|
22
|
+
* from (a crumb, a button), or the box of the item whose submenu it is,
|
|
23
|
+
* which it stands beside (on the item's inline-end side, the right unless
|
|
24
|
+
* `rtl`).
|
|
25
|
+
*/
|
|
26
|
+
export type MenuFrom = {
|
|
27
|
+
readonly point: MenuPoint;
|
|
28
|
+
} | {
|
|
29
|
+
readonly box: MenuBox;
|
|
30
|
+
} | {
|
|
31
|
+
readonly beside: MenuBox;
|
|
32
|
+
readonly rtl?: boolean;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Where a menu stands, its top-left corner in window coordinates. Opened at
|
|
36
|
+
* a point, it hangs down and right from it, moved in to stay inside the
|
|
37
|
+
* window, and opens upward from the point when there is no room below.
|
|
38
|
+
* Dropped from a box, it starts under the box's leading edge, a margin
|
|
39
|
+
* below, and stands over the box instead when there is room above and none
|
|
40
|
+
* below. A submenu stands beside its item, on the item's inline-end side,
|
|
41
|
+
* its first item level with it (a margin above the item's top, the menu's
|
|
42
|
+
* own padding), and on the other side when the window has no room on that
|
|
43
|
+
* one; when neither side has room it keeps to the side with more. Every
|
|
44
|
+
* way it stays inside the window. A menu its stylesheet caps at the
|
|
45
|
+
* window's height less both margins (`.menu-popup`) measures at that cap,
|
|
46
|
+
* so it starts at the top margin.
|
|
47
|
+
*/
|
|
48
|
+
export declare function placeMenu(from: MenuFrom, size: MenuSize, viewport: MenuSize): {
|
|
49
|
+
readonly left: number;
|
|
50
|
+
readonly top: number;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* What a key pressed in an open menu does. `open` and `back` are the arrows
|
|
54
|
+
* along the line: into the cursor's submenu, and out of a submenu to its
|
|
55
|
+
* item. `close` closes one level (Escape: a submenu, else the menu), or
|
|
56
|
+
* with `all` the whole menu (Tab).
|
|
57
|
+
*/
|
|
58
|
+
export type MenuKeyAction = {
|
|
59
|
+
readonly kind: 'move';
|
|
60
|
+
readonly delta: number;
|
|
61
|
+
} | {
|
|
62
|
+
readonly kind: 'edge';
|
|
63
|
+
readonly to: 'first' | 'last';
|
|
64
|
+
} | {
|
|
65
|
+
readonly kind: 'run';
|
|
66
|
+
} | {
|
|
67
|
+
readonly kind: 'open';
|
|
68
|
+
} | {
|
|
69
|
+
readonly kind: 'back';
|
|
70
|
+
} | {
|
|
71
|
+
readonly kind: 'close';
|
|
72
|
+
readonly all: boolean;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The menu's keys, as a desktop menu's: ↑ and ↓ move the cursor over the
|
|
76
|
+
* items that can run, Home and End to the first and the last, Return and
|
|
77
|
+
* Space run the cursor's item (or open its submenu), → opens the cursor's
|
|
78
|
+
* submenu and ← goes back out of one (the other way round when `rtl`, so
|
|
79
|
+
* the arrow points where the submenu stands), Escape closes one level and
|
|
80
|
+
* Tab the whole menu. A chord with a modifier is not the menu's (null),
|
|
81
|
+
* and neither is any other key.
|
|
82
|
+
*/
|
|
83
|
+
export declare function menuKeyAction(event: Pick<KeyboardEvent, 'key' | 'metaKey' | 'ctrlKey' | 'altKey' | 'shiftKey'>, rtl?: boolean): MenuKeyAction | null;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** The room a menu keeps from the window's edges, and the gap it leaves under what it drops from, in pixels. */
|
|
2
|
+
export const MENU_MARGIN = 4;
|
|
3
|
+
/** Clamps `start` so `extent` fits between the margins of `room`; the margin wins where it cannot. */
|
|
4
|
+
const clamp = (start, extent, room) => Math.max(MENU_MARGIN, Math.min(start, room - MENU_MARGIN - extent));
|
|
5
|
+
/**
|
|
6
|
+
* Where a menu stands, its top-left corner in window coordinates. Opened at
|
|
7
|
+
* a point, it hangs down and right from it, moved in to stay inside the
|
|
8
|
+
* window, and opens upward from the point when there is no room below.
|
|
9
|
+
* Dropped from a box, it starts under the box's leading edge, a margin
|
|
10
|
+
* below, and stands over the box instead when there is room above and none
|
|
11
|
+
* below. A submenu stands beside its item, on the item's inline-end side,
|
|
12
|
+
* its first item level with it (a margin above the item's top, the menu's
|
|
13
|
+
* own padding), and on the other side when the window has no room on that
|
|
14
|
+
* one; when neither side has room it keeps to the side with more. Every
|
|
15
|
+
* way it stays inside the window. A menu its stylesheet caps at the
|
|
16
|
+
* window's height less both margins (`.menu-popup`) measures at that cap,
|
|
17
|
+
* so it starts at the top margin.
|
|
18
|
+
*/
|
|
19
|
+
export function placeMenu(from, size, viewport) {
|
|
20
|
+
const room = (top) => top + size.height <= viewport.height - MENU_MARGIN;
|
|
21
|
+
if ('beside' in from) {
|
|
22
|
+
const { beside: item, rtl = false } = from;
|
|
23
|
+
const after = item.right;
|
|
24
|
+
const before = item.left - size.width;
|
|
25
|
+
const fitsAfter = after + size.width <= viewport.width - MENU_MARGIN;
|
|
26
|
+
const fitsBefore = before >= MENU_MARGIN;
|
|
27
|
+
const roomAfter = viewport.width - item.right;
|
|
28
|
+
const roomBefore = item.left;
|
|
29
|
+
// Its own side while it fits there; the other while that fits; else the roomier one.
|
|
30
|
+
const startsAfter = rtl
|
|
31
|
+
? !fitsBefore && (fitsAfter || roomAfter > roomBefore)
|
|
32
|
+
: fitsAfter || (!fitsBefore && roomAfter >= roomBefore);
|
|
33
|
+
return {
|
|
34
|
+
left: clamp(startsAfter ? after : before, size.width, viewport.width),
|
|
35
|
+
top: clamp(item.top - MENU_MARGIN, size.height, viewport.height)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
if ('point' in from) {
|
|
39
|
+
const { x, y } = from.point;
|
|
40
|
+
const top = room(y) || y - size.height < MENU_MARGIN ? y : y - size.height;
|
|
41
|
+
return {
|
|
42
|
+
left: clamp(x, size.width, viewport.width),
|
|
43
|
+
top: clamp(top, size.height, viewport.height)
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const { box } = from;
|
|
47
|
+
const below = box.bottom + MENU_MARGIN;
|
|
48
|
+
const above = box.top - MENU_MARGIN - size.height;
|
|
49
|
+
const top = room(below) || above < MENU_MARGIN ? below : above;
|
|
50
|
+
return {
|
|
51
|
+
left: clamp(box.left, size.width, viewport.width),
|
|
52
|
+
top: clamp(top, size.height, viewport.height)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The menu's keys, as a desktop menu's: ↑ and ↓ move the cursor over the
|
|
57
|
+
* items that can run, Home and End to the first and the last, Return and
|
|
58
|
+
* Space run the cursor's item (or open its submenu), → opens the cursor's
|
|
59
|
+
* submenu and ← goes back out of one (the other way round when `rtl`, so
|
|
60
|
+
* the arrow points where the submenu stands), Escape closes one level and
|
|
61
|
+
* Tab the whole menu. A chord with a modifier is not the menu's (null),
|
|
62
|
+
* and neither is any other key.
|
|
63
|
+
*/
|
|
64
|
+
export function menuKeyAction(event, rtl = false) {
|
|
65
|
+
if (event.metaKey || event.ctrlKey || event.altKey)
|
|
66
|
+
return null;
|
|
67
|
+
if (event.key === 'Tab')
|
|
68
|
+
return { kind: 'close', all: true };
|
|
69
|
+
if (event.shiftKey)
|
|
70
|
+
return null;
|
|
71
|
+
switch (event.key) {
|
|
72
|
+
case 'ArrowUp':
|
|
73
|
+
return { kind: 'move', delta: -1 };
|
|
74
|
+
case 'ArrowDown':
|
|
75
|
+
return { kind: 'move', delta: 1 };
|
|
76
|
+
case 'ArrowRight':
|
|
77
|
+
return { kind: rtl ? 'back' : 'open' };
|
|
78
|
+
case 'ArrowLeft':
|
|
79
|
+
return { kind: rtl ? 'open' : 'back' };
|
|
80
|
+
case 'Home':
|
|
81
|
+
return { kind: 'edge', to: 'first' };
|
|
82
|
+
case 'End':
|
|
83
|
+
return { kind: 'edge', to: 'last' };
|
|
84
|
+
case 'Enter':
|
|
85
|
+
case ' ':
|
|
86
|
+
return { kind: 'run' };
|
|
87
|
+
case 'Escape':
|
|
88
|
+
return { kind: 'close', all: false };
|
|
89
|
+
default:
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
}
|
package/dist/menu/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Menus: `Menu`, a menu of verbs or places opened at a point or dropped
|
|
3
|
+
* from an element, its items able to open one level of submenus, with
|
|
4
|
+
* `placeMenu` and `menuKeyAction` under it; and for
|
|
5
|
+
* filtered menus: the fuzzy matcher (`matchQuery`), the rules every filtered
|
|
3
6
|
* menu and palette shares (a refused keystroke, the cursor's moves, the trail
|
|
4
7
|
* of levels opened in place), the visual bell's beat, and the menu look in
|
|
5
8
|
* menu.css under `.listing-root`.
|
|
@@ -7,3 +10,5 @@
|
|
|
7
10
|
export { hasNegativeTerms, hasQueryTerms, matchQuery, type FuzzyMatch } from './fuzzyMatch.js';
|
|
8
11
|
export { enteredLevel, leftLevel, menuQueryRefused, movedPickableCursor, moveMenuCursor, pickableCursor, returnedCursor, trailLevel, type LevelReturn, type LevelStep, type PickableRow } from './MenuQuery.logic.js';
|
|
9
12
|
export { useBellBeat } from './useBellBeat.js';
|
|
13
|
+
export { Menu, type MenuEntry, type MenuHeadingEntry, type MenuItemEntry, type MenuLeafEntry, type MenuProps, type MenuSeparatorEntry, type MenuSubmenuEntry } from './Menu.js';
|
|
14
|
+
export { MENU_MARGIN, menuKeyAction, placeMenu, type MenuBox, type MenuFrom, type MenuKeyAction, type MenuPoint, type MenuSize } from './Menu.logic.js';
|
package/dist/menu/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Menus: `Menu`, a menu of verbs or places opened at a point or dropped
|
|
3
|
+
* from an element, its items able to open one level of submenus, with
|
|
4
|
+
* `placeMenu` and `menuKeyAction` under it; and for
|
|
5
|
+
* filtered menus: the fuzzy matcher (`matchQuery`), the rules every filtered
|
|
3
6
|
* menu and palette shares (a refused keystroke, the cursor's moves, the trail
|
|
4
7
|
* of levels opened in place), the visual bell's beat, and the menu look in
|
|
5
8
|
* menu.css under `.listing-root`.
|
|
@@ -7,3 +10,5 @@
|
|
|
7
10
|
export { hasNegativeTerms, hasQueryTerms, matchQuery } from './fuzzyMatch.js';
|
|
8
11
|
export { enteredLevel, leftLevel, menuQueryRefused, movedPickableCursor, moveMenuCursor, pickableCursor, returnedCursor, trailLevel } from './MenuQuery.logic.js';
|
|
9
12
|
export { useBellBeat } from './useBellBeat.js';
|
|
13
|
+
export { Menu } from './Menu.js';
|
|
14
|
+
export { MENU_MARGIN, menuKeyAction, placeMenu } from './Menu.logic.js';
|
package/dist/menu/menu.css
CHANGED
|
@@ -250,6 +250,12 @@
|
|
|
250
250
|
background: var(--text-on-surface);
|
|
251
251
|
}
|
|
252
252
|
|
|
253
|
+
/* Right to left, a submenu opens on the left, so the chevron points there.
|
|
254
|
+
:dir() follows the nearest direction, as the menu's own placement does. */
|
|
255
|
+
.folder-menu-option[aria-haspopup='menu']:dir(rtl)::after {
|
|
256
|
+
transform: scaleX(-1);
|
|
257
|
+
}
|
|
258
|
+
|
|
253
259
|
/* A plain separator between groups of rows that need no name: a hairline
|
|
254
260
|
in the border colour with a small space above and below, as a desktop
|
|
255
261
|
menu draws one. */
|
|
@@ -361,3 +367,54 @@
|
|
|
361
367
|
transform: translateX(2px);
|
|
362
368
|
}
|
|
363
369
|
}
|
|
370
|
+
|
|
371
|
+
/* ---- A menu opened at a point or dropped from an element (Menu) ----
|
|
372
|
+
|
|
373
|
+
The menu surface on the document's body, fixed where placeMenu puts it
|
|
374
|
+
(the component sets left and top), above dialogs, since a dialog's own
|
|
375
|
+
controls open menus too. As wide as its longest item, within reason, and
|
|
376
|
+
never taller than the window less placeMenu's margin at either edge
|
|
377
|
+
(MENU_MARGIN, --space-2): a longer menu scrolls, and the keys bring the
|
|
378
|
+
cursor's item into view. The cap holds its padding and border too
|
|
379
|
+
(border-box), and no entry shrinks to fit it (a row keeps its height, a
|
|
380
|
+
separator its line): the menu scrolls instead. */
|
|
381
|
+
.folder-menu.menu-popup {
|
|
382
|
+
position: fixed;
|
|
383
|
+
box-sizing: border-box;
|
|
384
|
+
top: 0;
|
|
385
|
+
left: 0;
|
|
386
|
+
z-index: var(--z-dialog-popover);
|
|
387
|
+
width: auto;
|
|
388
|
+
min-width: 180px;
|
|
389
|
+
max-width: min(480px, calc(100vw - 2 * var(--space-4)));
|
|
390
|
+
max-height: calc(100vh - 2 * var(--space-2));
|
|
391
|
+
overflow-y: auto;
|
|
392
|
+
overscroll-behavior: contain;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
.folder-menu.menu-popup > * {
|
|
396
|
+
flex-shrink: 0;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/* Its icons in the quiet tone, at a weight the cursor row's and a
|
|
400
|
+
destructive item's rules above outrank. */
|
|
401
|
+
:where(.menu-popup) .folder-menu-option > .row-icon {
|
|
402
|
+
display: inline-flex;
|
|
403
|
+
align-items: center;
|
|
404
|
+
color: var(--text-dim);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/* An item's words and the current one's dot after them; long words give
|
|
408
|
+
way with an ellipsis, the dot never. */
|
|
409
|
+
.menu-label {
|
|
410
|
+
display: flex;
|
|
411
|
+
align-items: center;
|
|
412
|
+
min-width: 0;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
.menu-label-text {
|
|
416
|
+
min-width: 0;
|
|
417
|
+
overflow: hidden;
|
|
418
|
+
text-overflow: ellipsis;
|
|
419
|
+
white-space: nowrap;
|
|
420
|
+
}
|
|
@@ -21,8 +21,9 @@ export interface PaletteProps {
|
|
|
21
21
|
* answers its own keys first. A press inside never takes the focus from the
|
|
22
22
|
* field. The caller draws the query row (`PaletteQuery`), the list, any
|
|
23
23
|
* notes, and the legend inside, and owns filtering and the cursor
|
|
24
|
-
* (`matchQuery`, `menuQueryRefused`, `moveMenuCursor`).
|
|
25
|
-
* listing scope (`.listing-root`)
|
|
24
|
+
* (`matchQuery`, `menuQueryRefused`, `moveMenuCursor`). The box carries
|
|
25
|
+
* the listing scope (`.listing-root`), so rows and listings drawn in it keep
|
|
26
|
+
* their look wherever the palette mounts.
|
|
26
27
|
*/
|
|
27
28
|
export declare function Palette({ label, className, busy, onClose, children }: PaletteProps): ReactElement;
|
|
28
29
|
/**
|