@ignite-ember/ui-react 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +217 -2
- package/dist/CopySource.d.ts +4 -0
- package/dist/CopySource.d.ts.map +1 -0
- package/dist/CopySource.js +36 -0
- package/dist/CopySource.js.map +1 -0
- package/dist/DataTable.d.ts +61 -0
- package/dist/DataTable.d.ts.map +1 -0
- package/dist/DataTable.js +29 -0
- package/dist/DataTable.js.map +1 -0
- package/dist/ErrorBoundary.d.ts +68 -0
- package/dist/ErrorBoundary.d.ts.map +1 -0
- package/dist/ErrorBoundary.js +63 -0
- package/dist/ErrorBoundary.js.map +1 -0
- package/dist/LiveRegion.d.ts +35 -0
- package/dist/LiveRegion.d.ts.map +1 -0
- package/dist/LiveRegion.js +70 -0
- package/dist/LiveRegion.js.map +1 -0
- package/dist/Markdown.d.ts +18 -0
- package/dist/Markdown.d.ts.map +1 -0
- package/dist/Markdown.js +52 -0
- package/dist/Markdown.js.map +1 -0
- package/dist/MarkdownInline.d.ts +44 -0
- package/dist/MarkdownInline.d.ts.map +1 -0
- package/dist/MarkdownInline.js +144 -0
- package/dist/MarkdownInline.js.map +1 -0
- package/dist/MenuIcon.d.ts +54 -0
- package/dist/MenuIcon.d.ts.map +1 -0
- package/dist/MenuIcon.js +96 -0
- package/dist/MenuIcon.js.map +1 -0
- package/dist/Mermaid.d.ts +38 -0
- package/dist/Mermaid.d.ts.map +1 -0
- package/dist/Mermaid.js +119 -0
- package/dist/Mermaid.js.map +1 -0
- package/dist/Monogram.d.ts +42 -0
- package/dist/Monogram.d.ts.map +1 -0
- package/dist/Monogram.js +88 -0
- package/dist/Monogram.js.map +1 -0
- package/dist/OverflowMenu.d.ts +55 -0
- package/dist/OverflowMenu.d.ts.map +1 -0
- package/dist/OverflowMenu.js +141 -0
- package/dist/OverflowMenu.js.map +1 -0
- package/dist/Shell.d.ts +54 -0
- package/dist/Shell.d.ts.map +1 -0
- package/dist/Shell.js +31 -0
- package/dist/Shell.js.map +1 -0
- package/dist/Skeleton.d.ts +32 -0
- package/dist/Skeleton.d.ts.map +1 -0
- package/dist/Skeleton.js +26 -0
- package/dist/Skeleton.js.map +1 -0
- package/dist/Split.d.ts +66 -0
- package/dist/Split.d.ts.map +1 -0
- package/dist/Split.js +133 -0
- package/dist/Split.js.map +1 -0
- package/dist/Toast.d.ts +87 -0
- package/dist/Toast.d.ts.map +1 -0
- package/dist/Toast.js +118 -0
- package/dist/Toast.js.map +1 -0
- package/dist/Tooltip.d.ts +15 -0
- package/dist/Tooltip.d.ts.map +1 -0
- package/dist/Tooltip.js +144 -0
- package/dist/Tooltip.js.map +1 -0
- package/dist/cx.d.ts +3 -0
- package/dist/cx.d.ts.map +1 -0
- package/dist/cx.js +5 -0
- package/dist/cx.js.map +1 -0
- package/dist/fenceInfo.d.ts +23 -0
- package/dist/fenceInfo.d.ts.map +1 -0
- package/dist/fenceInfo.js +19 -0
- package/dist/fenceInfo.js.map +1 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +82 -0
- package/dist/index.js.map +1 -0
- package/dist/primitives.d.ts +383 -0
- package/dist/primitives.d.ts.map +1 -0
- package/dist/primitives.js +340 -0
- package/dist/primitives.js.map +1 -0
- package/package.json +56 -4
- package/src/CopySource.tsx +52 -0
- package/src/DataTable.tsx +104 -0
- package/src/ErrorBoundary.tsx +102 -0
- package/src/LiveRegion.tsx +121 -0
- package/src/Markdown.tsx +71 -0
- package/src/MarkdownInline.tsx +164 -0
- package/src/MenuIcon.tsx +203 -0
- package/src/Mermaid.tsx +142 -0
- package/src/Monogram.tsx +103 -0
- package/src/OverflowMenu.tsx +352 -0
- package/src/Shell.tsx +86 -0
- package/src/Skeleton.tsx +68 -0
- package/src/Split.tsx +242 -0
- package/src/Toast.tsx +258 -0
- package/src/Tooltip.tsx +188 -0
- package/src/cx.ts +4 -0
- package/src/fenceInfo.ts +33 -0
- package/src/index.ts +153 -0
- package/src/primitives.tsx +887 -0
package/src/Mermaid.tsx
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mermaid diagrams.
|
|
3
|
+
*
|
|
4
|
+
* ```mermaid``` becomes a diagram, in a document exactly as it
|
|
5
|
+
* already does in chat.
|
|
6
|
+
*
|
|
7
|
+
* **A fence, deliberately.** This is what lets a document carry a
|
|
8
|
+
* diagram while staying markdown: the source is a code block, so it
|
|
9
|
+
* diffs a line at a time, chunks for search, survives the editor's
|
|
10
|
+
* round trip untouched, and reads as its own source in any other
|
|
11
|
+
* markdown tool. The alternative — embedding HTML, or a bespoke
|
|
12
|
+
* node type — would cost every one of those. ``fenceInfo`` is the
|
|
13
|
+
* helper that spots one.
|
|
14
|
+
*
|
|
15
|
+
* Shared by the chat and the document view rather than copied.
|
|
16
|
+
* There was one mermaid renderer living inside ``ChatItems`` and a
|
|
17
|
+
* document could not reach it, which is the usual way two renderers
|
|
18
|
+
* of the same thing start disagreeing about what it looks like.
|
|
19
|
+
*
|
|
20
|
+
* **`mermaid` is a peer dependency**, loaded on demand. An
|
|
21
|
+
* extension that never renders a diagram never downloads it; one
|
|
22
|
+
* that does must have it installed. This is the only export under
|
|
23
|
+
* the floor that asks anything of your package.json.
|
|
24
|
+
*
|
|
25
|
+
* ``SpecBlock`` used to live here and does not any more: it reaches
|
|
26
|
+
* into the app for the ``json-render`` renderer, so it could not be
|
|
27
|
+
* published without publishing that too. It is app code now.
|
|
28
|
+
*/
|
|
29
|
+
import { useEffect, useRef, useState } from "react";
|
|
30
|
+
|
|
31
|
+
import { CopySource } from "./CopySource.js";
|
|
32
|
+
|
|
33
|
+
type MermaidTheme = "default" | "dark";
|
|
34
|
+
|
|
35
|
+
interface MermaidConfig {
|
|
36
|
+
startOnLoad: boolean;
|
|
37
|
+
securityLevel: "strict";
|
|
38
|
+
theme: MermaidTheme;
|
|
39
|
+
themeVariables: { fontFamily: string; background: string };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface MermaidLib {
|
|
43
|
+
initialize: (config: MermaidConfig) => void;
|
|
44
|
+
render: (id: string, src: string) => Promise<{ svg: string }>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Mermaid draws into a scratch element by id, and the id becomes the
|
|
48
|
+
// SVG's own and scopes the <style> it injects. One counter for the
|
|
49
|
+
// page, so no two renders anywhere on it share one — not across
|
|
50
|
+
// blocks, not across React roots, not across an edit in flight.
|
|
51
|
+
let mermaidId = 0;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A mermaid theme matching igni's.
|
|
55
|
+
*
|
|
56
|
+
* ``default`` is the colourful light palette and ``dark`` its dark
|
|
57
|
+
* counterpart. Both run in colour — ``neutral``, which reads like
|
|
58
|
+
* the safe choice, is what made every diagram grey.
|
|
59
|
+
*/
|
|
60
|
+
function mermaidTheme(): MermaidTheme {
|
|
61
|
+
if (typeof document === "undefined") return "default";
|
|
62
|
+
const explicit = document.documentElement.dataset.theme;
|
|
63
|
+
if (explicit === "dark") return "dark";
|
|
64
|
+
if (explicit === "light") return "default";
|
|
65
|
+
if (typeof window !== "undefined" && window.matchMedia) {
|
|
66
|
+
return window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
67
|
+
? "dark"
|
|
68
|
+
: "default";
|
|
69
|
+
}
|
|
70
|
+
return "default";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Mermaid, configured for the theme in force right now.
|
|
75
|
+
*
|
|
76
|
+
* Mermaid is about 700KB, so it arrives only if a document has one;
|
|
77
|
+
* the bundler caches the module, so asking again costs nothing.
|
|
78
|
+
* Configured on every call rather than once, because the theme is
|
|
79
|
+
* read when it is configured — a diagram drawn after a switch to
|
|
80
|
+
* dark has to be drawn in dark.
|
|
81
|
+
*/
|
|
82
|
+
export async function loadMermaid(): Promise<MermaidLib> {
|
|
83
|
+
// Inline on purpose: `mermaid` is an optional peer dependency.
|
|
84
|
+
const lib = (await import("mermaid")).default as unknown as MermaidLib;
|
|
85
|
+
lib.initialize({
|
|
86
|
+
startOnLoad: false,
|
|
87
|
+
securityLevel: "strict",
|
|
88
|
+
theme: mermaidTheme(),
|
|
89
|
+
themeVariables: {
|
|
90
|
+
fontFamily: "var(--font-ui)",
|
|
91
|
+
background: "transparent",
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
return lib;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* A mermaid diagram.
|
|
99
|
+
*
|
|
100
|
+
* `source` is the text inside the fence. A diagram that does not
|
|
101
|
+
* parse is shown as its own source rather than as a blank box: it is
|
|
102
|
+
* usually a diagram somebody is halfway through writing.
|
|
103
|
+
*/
|
|
104
|
+
export function MermaidBlock({ source }: { source: string }) {
|
|
105
|
+
const frame = useRef<HTMLDivElement>(null);
|
|
106
|
+
const [error, setError] = useState<string | null>(null);
|
|
107
|
+
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
let cancelled = false;
|
|
110
|
+
setError(null);
|
|
111
|
+
void loadMermaid()
|
|
112
|
+
.then((lib) => lib.render(`mermaid-${++mermaidId}`, source))
|
|
113
|
+
.then(({ svg }) => {
|
|
114
|
+
if (cancelled || !frame.current) return;
|
|
115
|
+
frame.current.innerHTML = svg;
|
|
116
|
+
})
|
|
117
|
+
.catch((e: unknown) => {
|
|
118
|
+
if (cancelled) return;
|
|
119
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
120
|
+
});
|
|
121
|
+
return () => {
|
|
122
|
+
cancelled = true;
|
|
123
|
+
};
|
|
124
|
+
}, [source]);
|
|
125
|
+
|
|
126
|
+
if (error) {
|
|
127
|
+
// The source, not a blank box. A diagram that does not parse is
|
|
128
|
+
// usually a diagram somebody is halfway through writing, and
|
|
129
|
+
// hiding what they wrote is the least helpful response to that.
|
|
130
|
+
return (
|
|
131
|
+
<pre className="mermaid-error" title={`Mermaid render failed: ${error}`}>
|
|
132
|
+
{source}
|
|
133
|
+
</pre>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
return (
|
|
137
|
+
<div className="mermaid-diagram">
|
|
138
|
+
<CopySource source={source} />
|
|
139
|
+
<div ref={frame} />
|
|
140
|
+
</div>
|
|
141
|
+
);
|
|
142
|
+
}
|
package/src/Monogram.tsx
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two letters standing in for a face.
|
|
3
|
+
*
|
|
4
|
+
* Not an avatar. Nothing in the product stores an uploaded picture —
|
|
5
|
+
* there is no `avatar_url` on a user anywhere in the server — and a
|
|
6
|
+
* generic silhouette for everybody says strictly less than two
|
|
7
|
+
* letters of somebody's own name. The portal draws the same thing
|
|
8
|
+
* from the same source, so the two surfaces agree about what a person
|
|
9
|
+
* looks like.
|
|
10
|
+
*
|
|
11
|
+
* The colour is derived from the name rather than chosen, so the same
|
|
12
|
+
* person is the same colour on every machine and across restarts.
|
|
13
|
+
* That is the whole point: the monogram is a recognition aid, and one
|
|
14
|
+
* that changed colour per session would be decoration.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export function initialsOf(name: string): string {
|
|
18
|
+
const cleaned = (name || "").trim();
|
|
19
|
+
if (!cleaned) return "?";
|
|
20
|
+
|
|
21
|
+
// An address is not a name: "maya@acme.test" should read as M, not
|
|
22
|
+
// as MA from "maya" and "acme".
|
|
23
|
+
const local = cleaned.includes("@") ? cleaned.split("@")[0] : cleaned;
|
|
24
|
+
const words = local.split(/[\s._-]+/).filter(Boolean);
|
|
25
|
+
if (words.length === 0) return "?";
|
|
26
|
+
if (words.length === 1) return words[0].slice(0, 2).toUpperCase();
|
|
27
|
+
return (words[0][0] + words[words.length - 1][0]).toUpperCase();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A stable hue for a name. Same person, same colour, every time. */
|
|
31
|
+
export function hueOf(name: string): number {
|
|
32
|
+
let hash = 0;
|
|
33
|
+
for (const ch of name || "") {
|
|
34
|
+
hash = (hash * 31 + ch.charCodeAt(0)) % 360;
|
|
35
|
+
}
|
|
36
|
+
return hash;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The same colour, written as ``#rrggbb``.
|
|
41
|
+
*
|
|
42
|
+
* For the places that cannot take ``hsl()``. The collaborative
|
|
43
|
+
* caret is one: it validates against ``/^#[0-9a-fA-F]{6}$/`` and
|
|
44
|
+
* quietly renders anything else transparent — a cursor that is
|
|
45
|
+
* there, labelled with the right name, and invisible.
|
|
46
|
+
*
|
|
47
|
+
* Derived from the same hue as the monogram, so somebody's caret and
|
|
48
|
+
* their initials are the same colour.
|
|
49
|
+
*/
|
|
50
|
+
export function hexOf(
|
|
51
|
+
name: string,
|
|
52
|
+
saturation = 0.62,
|
|
53
|
+
lightness = 0.52,
|
|
54
|
+
): string {
|
|
55
|
+
const hue = hueOf(name) / 360;
|
|
56
|
+
const chroma = (1 - Math.abs(2 * lightness - 1)) * saturation;
|
|
57
|
+
const second = chroma * (1 - Math.abs(((hue * 6) % 2) - 1));
|
|
58
|
+
const base = lightness - chroma / 2;
|
|
59
|
+
const sixth = Math.floor(hue * 6) % 6;
|
|
60
|
+
const [r, g, b] = (
|
|
61
|
+
[
|
|
62
|
+
[chroma, second, 0],
|
|
63
|
+
[second, chroma, 0],
|
|
64
|
+
[0, chroma, second],
|
|
65
|
+
[0, second, chroma],
|
|
66
|
+
[second, 0, chroma],
|
|
67
|
+
[chroma, 0, second],
|
|
68
|
+
] as const
|
|
69
|
+
)[sixth];
|
|
70
|
+
const byte = (value: number) =>
|
|
71
|
+
Math.round((value + base) * 255)
|
|
72
|
+
.toString(16)
|
|
73
|
+
.padStart(2, "0");
|
|
74
|
+
return `#${byte(r)}${byte(g)}${byte(b)}`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Two letters standing in for a face.
|
|
79
|
+
*
|
|
80
|
+
* See the note at the top of this file. The colour comes from the
|
|
81
|
+
* name, so the same person is the same colour on every machine and
|
|
82
|
+
* across restarts.
|
|
83
|
+
*/
|
|
84
|
+
export function Monogram({ name, size = 18 }: { name: string; size?: number }) {
|
|
85
|
+
const hue = hueOf(name);
|
|
86
|
+
return (
|
|
87
|
+
<span
|
|
88
|
+
className="monogram"
|
|
89
|
+
aria-hidden="true"
|
|
90
|
+
style={{
|
|
91
|
+
width: size,
|
|
92
|
+
height: size,
|
|
93
|
+
fontSize: Math.round(size * 0.46),
|
|
94
|
+
// Fixed saturation and lightness so every generated colour
|
|
95
|
+
// clears contrast against white text, rather than hoping the
|
|
96
|
+
// hash lands somewhere readable.
|
|
97
|
+
background: `hsl(${hue} 42% 42%)`,
|
|
98
|
+
}}
|
|
99
|
+
>
|
|
100
|
+
{initialsOf(name)}
|
|
101
|
+
</span>
|
|
102
|
+
);
|
|
103
|
+
}
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { useCallback, useEffect, useId, useRef, useState } from "react";
|
|
2
|
+
import { MenuIcon, type MenuIconName } from "./MenuIcon.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A row of actions collapsed behind one button.
|
|
6
|
+
*
|
|
7
|
+
* For the case where the actions are real but secondary: four buttons
|
|
8
|
+
* across a header give each of them the same weight as the page
|
|
9
|
+
* title, and the one that deletes the thing ends up the same size as
|
|
10
|
+
* the one that exports it.
|
|
11
|
+
*
|
|
12
|
+
* Hiding them costs discoverability, so everything that made them
|
|
13
|
+
* usable as buttons has to survive the move — focus, the keyboard,
|
|
14
|
+
* and the names screen readers announce. That is most of what is
|
|
15
|
+
* below.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** One action in an {@link OverflowMenu}. */
|
|
19
|
+
export interface OverflowAction {
|
|
20
|
+
/** Stable across renders; the label changes on toggles. */
|
|
21
|
+
key: string;
|
|
22
|
+
/** What it is called. */
|
|
23
|
+
label: string;
|
|
24
|
+
/** What picking it does. Omitted when `items` makes it a submenu. */
|
|
25
|
+
onSelect?: () => void;
|
|
26
|
+
/** Variants of this action, opened as a submenu rather than run.
|
|
27
|
+
* Use it when one verb has several objects — export as HTML, as
|
|
28
|
+
* Markdown, as PDF — so the menu stays a list of things you can
|
|
29
|
+
* do rather than a list of every combination of them. */
|
|
30
|
+
items?: OverflowAction[];
|
|
31
|
+
/** Destructive — rendered apart from the rest and in the danger colour. */
|
|
32
|
+
danger?: boolean;
|
|
33
|
+
/** A toggle that is currently on. Announced as such, not just coloured. */
|
|
34
|
+
active?: boolean;
|
|
35
|
+
/** Whether it can be picked. Shown as it is, and kept out of the tab order. */
|
|
36
|
+
disabled?: boolean;
|
|
37
|
+
/** Why you would pick this. Shown on hover, same as the buttons had. */
|
|
38
|
+
title?: string;
|
|
39
|
+
/** The glyph beside the label.
|
|
40
|
+
*
|
|
41
|
+
* Optional. A menu of five verbs reads as five equal options, and
|
|
42
|
+
* picking the wrong one of Archive and Remove costs quite
|
|
43
|
+
* differently — the shape is recognised before the word is read. */
|
|
44
|
+
icon?: MenuIconName;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Direct children only — a submenu's own items belong to its list. */
|
|
48
|
+
const OWN_ITEMS =
|
|
49
|
+
":scope > [role='menuitem'], :scope > .overflow-sub-wrap > [role='menuitem']";
|
|
50
|
+
|
|
51
|
+
function enabledItems(root: HTMLElement | null): HTMLButtonElement[] {
|
|
52
|
+
return Array.from(
|
|
53
|
+
root?.querySelectorAll<HTMLButtonElement>(OWN_ITEMS) ?? [],
|
|
54
|
+
).filter((el) => !el.disabled);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Up/down within one list, wrapping. Returns whether it handled the key. */
|
|
58
|
+
function moveFocus(root: HTMLElement | null, key: string): boolean {
|
|
59
|
+
if (key !== "ArrowDown" && key !== "ArrowUp") return false;
|
|
60
|
+
const all = enabledItems(root);
|
|
61
|
+
if (!all.length) return false;
|
|
62
|
+
const at = all.indexOf(document.activeElement as HTMLButtonElement);
|
|
63
|
+
const step = key === "ArrowDown" ? 1 : -1;
|
|
64
|
+
// Wrapping, because a menu this short has no scroll to tell you
|
|
65
|
+
// that you have reached the end of it.
|
|
66
|
+
all[(at + step + all.length) % all.length]?.focus();
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A row of actions collapsed behind one button.
|
|
72
|
+
*
|
|
73
|
+
* See the note at the top of this file. Everything that made the
|
|
74
|
+
* buttons usable as buttons survives the move: focus, the keyboard,
|
|
75
|
+
* and the names screen readers announce.
|
|
76
|
+
*/
|
|
77
|
+
export function OverflowMenu({
|
|
78
|
+
actions,
|
|
79
|
+
label = "More actions",
|
|
80
|
+
}: {
|
|
81
|
+
actions: OverflowAction[];
|
|
82
|
+
/** What the trigger is called, for anyone who cannot see three dots. */
|
|
83
|
+
label?: string;
|
|
84
|
+
}) {
|
|
85
|
+
const [open, setOpen] = useState(false);
|
|
86
|
+
const trigger = useRef<HTMLButtonElement>(null);
|
|
87
|
+
const list = useRef<HTMLDivElement>(null);
|
|
88
|
+
const menuId = useId();
|
|
89
|
+
|
|
90
|
+
// Closing returns focus to the trigger. Without it a keyboard user
|
|
91
|
+
// who opens the menu and changes their mind is dropped at the top
|
|
92
|
+
// of the document with no way back to where they were.
|
|
93
|
+
const close = useCallback((restoreFocus = true) => {
|
|
94
|
+
setOpen(false);
|
|
95
|
+
if (restoreFocus) trigger.current?.focus();
|
|
96
|
+
}, []);
|
|
97
|
+
|
|
98
|
+
// Opening moves focus into the menu, which is what makes it a menu
|
|
99
|
+
// rather than a panel that happens to be visible: the next Tab or
|
|
100
|
+
// arrow key lands on an action instead of on whatever follows the
|
|
101
|
+
// trigger in the document.
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
if (open) enabledItems(list.current)[0]?.focus();
|
|
104
|
+
}, [open]);
|
|
105
|
+
|
|
106
|
+
const onKeyDown = (e: React.KeyboardEvent) => {
|
|
107
|
+
if (e.key === "Escape") {
|
|
108
|
+
// Only if no submenu is open — that one owns Escape first, and
|
|
109
|
+
// closes back to here rather than out of the menu entirely.
|
|
110
|
+
if (list.current?.querySelector(".overflow-sub")) return;
|
|
111
|
+
e.stopPropagation();
|
|
112
|
+
close();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (moveFocus(list.current, e.key)) e.preventDefault();
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
if (!actions.length) return null;
|
|
119
|
+
|
|
120
|
+
const safe = actions.filter((a) => !a.danger);
|
|
121
|
+
const destructive = actions.filter((a) => a.danger);
|
|
122
|
+
const dismiss = () => close(false);
|
|
123
|
+
|
|
124
|
+
return (
|
|
125
|
+
<div className="overflow-menu">
|
|
126
|
+
<button
|
|
127
|
+
ref={trigger}
|
|
128
|
+
type="button"
|
|
129
|
+
className="kb-icon-btn"
|
|
130
|
+
aria-haspopup="menu"
|
|
131
|
+
aria-expanded={open}
|
|
132
|
+
aria-controls={open ? menuId : undefined}
|
|
133
|
+
aria-label={label}
|
|
134
|
+
title={label}
|
|
135
|
+
onClick={() => (open ? close() : setOpen(true))}
|
|
136
|
+
>
|
|
137
|
+
<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true">
|
|
138
|
+
<circle cx="3" cy="8" r="1.4" fill="currentColor" />
|
|
139
|
+
<circle cx="8" cy="8" r="1.4" fill="currentColor" />
|
|
140
|
+
<circle cx="13" cy="8" r="1.4" fill="currentColor" />
|
|
141
|
+
</svg>
|
|
142
|
+
</button>
|
|
143
|
+
|
|
144
|
+
{open && (
|
|
145
|
+
<>
|
|
146
|
+
{/* Outside click closes it — the same backdrop the composer's
|
|
147
|
+
mode picker uses. No focus restore here: the click has
|
|
148
|
+
already put focus wherever the user aimed it, and pulling
|
|
149
|
+
it back to the trigger would undo that. */}
|
|
150
|
+
<div className="overflow-backdrop" onClick={dismiss} />
|
|
151
|
+
<div
|
|
152
|
+
ref={list}
|
|
153
|
+
id={menuId}
|
|
154
|
+
className="overflow-list"
|
|
155
|
+
role="menu"
|
|
156
|
+
aria-label={label}
|
|
157
|
+
onKeyDown={onKeyDown}
|
|
158
|
+
>
|
|
159
|
+
{safe.map((a) => (
|
|
160
|
+
<Entry key={a.key} action={a} onDone={dismiss} />
|
|
161
|
+
))}
|
|
162
|
+
{/* A separator, so the item that removes the thing is not
|
|
163
|
+
one slip of the wrist below the one that shares it. */}
|
|
164
|
+
{destructive.length > 0 && safe.length > 0 && (
|
|
165
|
+
<div className="overflow-sep" role="separator" />
|
|
166
|
+
)}
|
|
167
|
+
{destructive.map((a) => (
|
|
168
|
+
<Entry key={a.key} action={a} onDone={dismiss} />
|
|
169
|
+
))}
|
|
170
|
+
</div>
|
|
171
|
+
</>
|
|
172
|
+
)}
|
|
173
|
+
</div>
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function Entry({
|
|
178
|
+
action,
|
|
179
|
+
onDone,
|
|
180
|
+
}: {
|
|
181
|
+
action: OverflowAction;
|
|
182
|
+
onDone: () => void;
|
|
183
|
+
}) {
|
|
184
|
+
return action.items?.length ? (
|
|
185
|
+
<Submenu action={action} onDone={onDone} />
|
|
186
|
+
) : (
|
|
187
|
+
<Item action={action} onDone={onDone} />
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* An item that opens its own list beside it.
|
|
193
|
+
*
|
|
194
|
+
* Opens to the *left*. The parent menu is already pinned to the right
|
|
195
|
+
* edge of what it hangs off, so a submenu flying out to the right —
|
|
196
|
+
* the desktop default — would open past the window.
|
|
197
|
+
*/
|
|
198
|
+
function Submenu({
|
|
199
|
+
action,
|
|
200
|
+
onDone,
|
|
201
|
+
}: {
|
|
202
|
+
action: OverflowAction;
|
|
203
|
+
onDone: () => void;
|
|
204
|
+
}) {
|
|
205
|
+
const [open, setOpen] = useState(false);
|
|
206
|
+
const item = useRef<HTMLButtonElement>(null);
|
|
207
|
+
const list = useRef<HTMLDivElement>(null);
|
|
208
|
+
const menuId = useId();
|
|
209
|
+
|
|
210
|
+
useEffect(() => {
|
|
211
|
+
if (open) enabledItems(list.current)[0]?.focus();
|
|
212
|
+
}, [open]);
|
|
213
|
+
|
|
214
|
+
const back = () => {
|
|
215
|
+
setOpen(false);
|
|
216
|
+
item.current?.focus();
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
return (
|
|
220
|
+
<div
|
|
221
|
+
className="overflow-sub-wrap"
|
|
222
|
+
// Hover opens it, the way a desktop menu does. `onMouseLeave`
|
|
223
|
+
// rather than a timer: a submenu that lingers after the pointer
|
|
224
|
+
// has gone covers the items below the one that opened it.
|
|
225
|
+
onMouseEnter={() => setOpen(true)}
|
|
226
|
+
onMouseLeave={() => setOpen(false)}
|
|
227
|
+
>
|
|
228
|
+
<button
|
|
229
|
+
ref={item}
|
|
230
|
+
type="button"
|
|
231
|
+
role="menuitem"
|
|
232
|
+
className="popup-item overflow-item overflow-item-parent"
|
|
233
|
+
aria-haspopup="menu"
|
|
234
|
+
aria-expanded={open}
|
|
235
|
+
aria-controls={open ? menuId : undefined}
|
|
236
|
+
disabled={action.disabled}
|
|
237
|
+
title={action.title}
|
|
238
|
+
onClick={() => setOpen((on) => !on)}
|
|
239
|
+
onKeyDown={(e) => {
|
|
240
|
+
// Right opens and steps in, matching every other menu on
|
|
241
|
+
// the desktop. Enter and Space come free from `button`.
|
|
242
|
+
if (e.key === "ArrowRight" || e.key === "Enter" || e.key === " ") {
|
|
243
|
+
e.preventDefault();
|
|
244
|
+
e.stopPropagation();
|
|
245
|
+
setOpen(true);
|
|
246
|
+
}
|
|
247
|
+
}}
|
|
248
|
+
>
|
|
249
|
+
{action.icon && <MenuIcon name={action.icon} />}
|
|
250
|
+
{action.label}
|
|
251
|
+
<svg
|
|
252
|
+
className="overflow-chevron"
|
|
253
|
+
viewBox="0 0 16 16"
|
|
254
|
+
width="11"
|
|
255
|
+
height="11"
|
|
256
|
+
aria-hidden="true"
|
|
257
|
+
>
|
|
258
|
+
<path
|
|
259
|
+
d="M6 3.5 10.5 8 6 12.5"
|
|
260
|
+
fill="none"
|
|
261
|
+
stroke="currentColor"
|
|
262
|
+
strokeWidth="1.6"
|
|
263
|
+
strokeLinecap="round"
|
|
264
|
+
strokeLinejoin="round"
|
|
265
|
+
/>
|
|
266
|
+
</svg>
|
|
267
|
+
</button>
|
|
268
|
+
|
|
269
|
+
{open && (
|
|
270
|
+
<div
|
|
271
|
+
ref={list}
|
|
272
|
+
id={menuId}
|
|
273
|
+
className="overflow-list overflow-sub"
|
|
274
|
+
role="menu"
|
|
275
|
+
aria-label={action.label}
|
|
276
|
+
onKeyDown={(e) => {
|
|
277
|
+
// Escape and Left close *this* list and go back to the
|
|
278
|
+
// item that opened it, rather than dropping the whole
|
|
279
|
+
// menu — one level per keypress, like the trail does.
|
|
280
|
+
if (e.key === "Escape" || e.key === "ArrowLeft") {
|
|
281
|
+
e.preventDefault();
|
|
282
|
+
e.stopPropagation();
|
|
283
|
+
back();
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (moveFocus(list.current, e.key)) {
|
|
287
|
+
e.preventDefault();
|
|
288
|
+
e.stopPropagation();
|
|
289
|
+
}
|
|
290
|
+
}}
|
|
291
|
+
>
|
|
292
|
+
{action.items?.map((child) => (
|
|
293
|
+
<Item
|
|
294
|
+
key={child.key}
|
|
295
|
+
action={child}
|
|
296
|
+
onDone={onDone}
|
|
297
|
+
parent={action.label}
|
|
298
|
+
/>
|
|
299
|
+
))}
|
|
300
|
+
</div>
|
|
301
|
+
)}
|
|
302
|
+
</div>
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function Item({
|
|
307
|
+
action,
|
|
308
|
+
onDone,
|
|
309
|
+
parent,
|
|
310
|
+
}: {
|
|
311
|
+
action: OverflowAction;
|
|
312
|
+
onDone: () => void;
|
|
313
|
+
/** The submenu this sits under, if any. */
|
|
314
|
+
parent?: string;
|
|
315
|
+
}) {
|
|
316
|
+
return (
|
|
317
|
+
<button
|
|
318
|
+
type="button"
|
|
319
|
+
role="menuitem"
|
|
320
|
+
// A toggle is `menuitemradio`-ish, but `aria-pressed` on a
|
|
321
|
+
// menuitem is not read. `aria-current` is: it says "this is the
|
|
322
|
+
// state you are in" rather than leaving the state to the colour.
|
|
323
|
+
aria-current={action.active ? true : undefined}
|
|
324
|
+
// "PDF" alone says nothing once the submenu that gives it
|
|
325
|
+
// meaning has been navigated past. The full name travels with
|
|
326
|
+
// the item.
|
|
327
|
+
aria-label={parent ? `${parent} ${action.label}` : undefined}
|
|
328
|
+
className={[
|
|
329
|
+
"popup-item",
|
|
330
|
+
"overflow-item",
|
|
331
|
+
action.active ? "active" : "",
|
|
332
|
+
action.danger ? "overflow-item-danger" : "",
|
|
333
|
+
]
|
|
334
|
+
.filter(Boolean)
|
|
335
|
+
.join(" ")}
|
|
336
|
+
disabled={action.disabled}
|
|
337
|
+
title={action.title}
|
|
338
|
+
// Closed without pulling focus back to the trigger, because most
|
|
339
|
+
// of these actions replace the header the trigger lives in —
|
|
340
|
+
// restoring focus to an element that is about to unmount loses
|
|
341
|
+
// it on `document.body`. Where focus goes next belongs to the
|
|
342
|
+
// action. Dismissing without choosing still restores it.
|
|
343
|
+
onClick={() => {
|
|
344
|
+
onDone();
|
|
345
|
+
action.onSelect?.();
|
|
346
|
+
}}
|
|
347
|
+
>
|
|
348
|
+
{action.icon && <MenuIcon name={action.icon} />}
|
|
349
|
+
{action.label}
|
|
350
|
+
</button>
|
|
351
|
+
);
|
|
352
|
+
}
|
package/src/Shell.tsx
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page an extension fills.
|
|
3
|
+
*
|
|
4
|
+
* `.igni-root` set colour, type and `margin: 0`, and nothing about
|
|
5
|
+
* layout — no height, no `box-sizing`, nothing on `html`. So every
|
|
6
|
+
* full-frame extension opened by writing the same six lines of CSS,
|
|
7
|
+
* and wrote them differently. Two panels in one product then scroll
|
|
8
|
+
* differently, which is the kind of difference nobody can name and
|
|
9
|
+
* everybody notices.
|
|
10
|
+
*
|
|
11
|
+
* `min-height: 0` is the reason this is a published component rather
|
|
12
|
+
* than a recipe in a README. A flex child defaults to
|
|
13
|
+
* `min-height: auto`, which refuses to shrink below its content, so
|
|
14
|
+
* a scroll container nested in a flex column grows the page instead
|
|
15
|
+
* of scrolling. It is invisible until the content is long, it is the
|
|
16
|
+
* most-reported layout bug in flexbox, and the fix is one line that
|
|
17
|
+
* reads like it does nothing.
|
|
18
|
+
*/
|
|
19
|
+
import type { HTMLAttributes, ReactNode } from "react";
|
|
20
|
+
|
|
21
|
+
function cx(...parts: (string | false | null | undefined)[]): string {
|
|
22
|
+
return parts.filter(Boolean).join(" ");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** What an {@link AppShell} takes. */
|
|
26
|
+
export interface AppShellProps extends HTMLAttributes<HTMLDivElement> {
|
|
27
|
+
/** Fixed at the top. A `Toolbar`, usually. */
|
|
28
|
+
header?: ReactNode;
|
|
29
|
+
/** Fixed at the bottom. Status, or a form's actions. */
|
|
30
|
+
footer?: ReactNode;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Header, body, footer — with exactly one part allowed to grow.
|
|
35
|
+
*
|
|
36
|
+
* The body is a flex column itself, so a `Scroll` placed directly
|
|
37
|
+
* inside it fills what is left over rather than the whole frame.
|
|
38
|
+
*/
|
|
39
|
+
export function AppShell({
|
|
40
|
+
header,
|
|
41
|
+
footer,
|
|
42
|
+
children,
|
|
43
|
+
className,
|
|
44
|
+
...rest
|
|
45
|
+
}: AppShellProps) {
|
|
46
|
+
return (
|
|
47
|
+
<div {...rest} className={cx("ui-shell", className)}>
|
|
48
|
+
{header === undefined ? null : (
|
|
49
|
+
<div className="ui-shell-header">{header}</div>
|
|
50
|
+
)}
|
|
51
|
+
<div className="ui-shell-body">{children}</div>
|
|
52
|
+
{footer === undefined ? null : (
|
|
53
|
+
<div className="ui-shell-footer">{footer}</div>
|
|
54
|
+
)}
|
|
55
|
+
</div>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** What a {@link Scroll} takes. */
|
|
60
|
+
export interface ScrollProps extends HTMLAttributes<HTMLDivElement> {
|
|
61
|
+
/** Scroll sideways instead. A wide table, not a long list. */
|
|
62
|
+
axis?: "y" | "x";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The part that scrolls.
|
|
67
|
+
*
|
|
68
|
+
* `overscroll-behavior: contain`, so reaching the end does not chain
|
|
69
|
+
* the scroll to the host page behind the frame — an extension that
|
|
70
|
+
* scrolls igni when its own list bottoms out feels like a bug in
|
|
71
|
+
* igni.
|
|
72
|
+
*
|
|
73
|
+
* Not focusable by default. A scrollable region that holds no
|
|
74
|
+
* focusable children is unreachable by keyboard, so pass
|
|
75
|
+
* `tabIndex={0}` when that is the case; doing it unconditionally
|
|
76
|
+
* would put a stop in the tab order of every list that already has
|
|
77
|
+
* buttons in it.
|
|
78
|
+
*/
|
|
79
|
+
export function Scroll({ axis = "y", className, ...rest }: ScrollProps) {
|
|
80
|
+
return (
|
|
81
|
+
<div
|
|
82
|
+
{...rest}
|
|
83
|
+
className={cx(axis === "x" ? "ui-scroll-x" : "ui-scroll", className)}
|
|
84
|
+
/>
|
|
85
|
+
);
|
|
86
|
+
}
|