@skillcdn/console 0.4.6 → 0.4.7

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.
Files changed (131) hide show
  1. package/README.md +4 -4
  2. package/dist/api.d.ts +2 -2
  3. package/dist/api.d.ts.map +1 -1
  4. package/dist/api.js +2 -2
  5. package/dist/api.js.map +1 -1
  6. package/dist/cli/cli.d.ts +6 -0
  7. package/dist/cli/cli.d.ts.map +1 -1
  8. package/dist/cli/cli.js +60 -30
  9. package/dist/cli/cli.js.map +1 -1
  10. package/dist/cli/format.d.ts.map +1 -1
  11. package/dist/cli/format.js +1 -0
  12. package/dist/cli/format.js.map +1 -1
  13. package/dist/cli/help.js +5 -5
  14. package/dist/cli/help.js.map +1 -1
  15. package/dist/cli/keys.d.ts +10 -0
  16. package/dist/cli/keys.d.ts.map +1 -0
  17. package/dist/cli/keys.js +59 -0
  18. package/dist/cli/keys.js.map +1 -0
  19. package/dist/cli/main.js +2 -1
  20. package/dist/cli/main.js.map +1 -1
  21. package/dist/cli/terminal.d.ts +7 -0
  22. package/dist/cli/terminal.d.ts.map +1 -1
  23. package/dist/cli/terminal.js +39 -0
  24. package/dist/cli/terminal.js.map +1 -1
  25. package/dist/client.d.ts +7 -1
  26. package/dist/client.d.ts.map +1 -1
  27. package/dist/client.js +4 -1
  28. package/dist/client.js.map +1 -1
  29. package/dist/components/plugin-frame.d.ts.map +1 -1
  30. package/dist/components/plugin-frame.js +53 -10
  31. package/dist/components/plugin-frame.js.map +1 -1
  32. package/dist/components/sessions.d.ts +15 -0
  33. package/dist/components/sessions.d.ts.map +1 -0
  34. package/dist/components/sessions.js +17 -0
  35. package/dist/components/sessions.js.map +1 -0
  36. package/dist/components/shell.d.ts.map +1 -1
  37. package/dist/components/shell.js +198 -28
  38. package/dist/components/shell.js.map +1 -1
  39. package/dist/components/workspace.d.ts +2 -0
  40. package/dist/components/workspace.d.ts.map +1 -1
  41. package/dist/components/workspace.js +19 -4
  42. package/dist/components/workspace.js.map +1 -1
  43. package/dist/console.css +228 -21
  44. package/dist/console.d.ts +5 -2
  45. package/dist/console.d.ts.map +1 -1
  46. package/dist/console.js +106 -21
  47. package/dist/console.js.map +1 -1
  48. package/dist/data.d.ts +7 -1
  49. package/dist/data.d.ts.map +1 -1
  50. package/dist/data.js +26 -5
  51. package/dist/data.js.map +1 -1
  52. package/dist/i18n/en.d.ts +24 -0
  53. package/dist/i18n/en.d.ts.map +1 -1
  54. package/dist/i18n/en.js +21 -0
  55. package/dist/i18n/en.js.map +1 -1
  56. package/dist/i18n/index.d.ts +2 -2
  57. package/dist/i18n/index.d.ts.map +1 -1
  58. package/dist/i18n/index.js +7 -3
  59. package/dist/i18n/index.js.map +1 -1
  60. package/dist/i18n/ko.d.ts.map +1 -1
  61. package/dist/i18n/ko.js +23 -2
  62. package/dist/i18n/ko.js.map +1 -1
  63. package/dist/index.d.ts +1 -0
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +1 -0
  66. package/dist/index.js.map +1 -1
  67. package/dist/plugin/bridge.d.ts +3 -0
  68. package/dist/plugin/bridge.d.ts.map +1 -1
  69. package/dist/plugin/bridge.js +1 -0
  70. package/dist/plugin/bridge.js.map +1 -1
  71. package/dist/plugin/context.d.ts +9 -0
  72. package/dist/plugin/context.d.ts.map +1 -1
  73. package/dist/plugin/define.d.ts.map +1 -1
  74. package/dist/plugin/define.js +6 -3
  75. package/dist/plugin/define.js.map +1 -1
  76. package/dist/plugin/sandbox.d.ts.map +1 -1
  77. package/dist/plugin/sandbox.js +8 -1
  78. package/dist/plugin/sandbox.js.map +1 -1
  79. package/dist/router.d.ts +6 -0
  80. package/dist/router.d.ts.map +1 -1
  81. package/dist/router.js +5 -0
  82. package/dist/router.js.map +1 -1
  83. package/dist/routes.d.ts +3 -1
  84. package/dist/routes.d.ts.map +1 -1
  85. package/dist/routes.js +2 -0
  86. package/dist/routes.js.map +1 -1
  87. package/dist/schemas.d.ts +38 -0
  88. package/dist/schemas.d.ts.map +1 -1
  89. package/dist/schemas.js +37 -1
  90. package/dist/schemas.js.map +1 -1
  91. package/dist/tokens.d.ts.map +1 -1
  92. package/dist/tokens.js +1 -0
  93. package/dist/tokens.js.map +1 -1
  94. package/dist/vocabulary.d.ts +6 -0
  95. package/dist/vocabulary.d.ts.map +1 -1
  96. package/dist/vocabulary.js +7 -0
  97. package/dist/vocabulary.js.map +1 -1
  98. package/docs/specs/cli.md +3 -3
  99. package/docs/specs/plugins.md +7 -6
  100. package/docs/specs/rest.md +9 -5
  101. package/package.json +1 -1
  102. package/skills/building-a-plugin/SKILL.md +2 -2
  103. package/skills/planning-the-board/references/setting-up.md +1 -1
  104. package/src/api.ts +6 -0
  105. package/src/cli/cli.ts +81 -29
  106. package/src/cli/format.ts +1 -0
  107. package/src/cli/help.ts +5 -5
  108. package/src/cli/keys.ts +69 -0
  109. package/src/cli/main.ts +2 -1
  110. package/src/cli/terminal.ts +40 -0
  111. package/src/client.ts +14 -0
  112. package/src/components/plugin-frame.tsx +72 -17
  113. package/src/components/sessions.tsx +108 -0
  114. package/src/components/shell.tsx +265 -52
  115. package/src/components/workspace.tsx +46 -2
  116. package/src/console.tsx +181 -16
  117. package/src/data.ts +33 -5
  118. package/src/i18n/en.ts +25 -0
  119. package/src/i18n/index.ts +11 -3
  120. package/src/i18n/ko.ts +27 -2
  121. package/src/index.ts +1 -0
  122. package/src/plugin/bridge.ts +4 -0
  123. package/src/plugin/context.ts +9 -0
  124. package/src/plugin/define.ts +10 -2
  125. package/src/plugin/sandbox.ts +9 -1
  126. package/src/router.ts +7 -0
  127. package/src/routes.ts +3 -1
  128. package/src/schemas.ts +41 -0
  129. package/src/styles/console.css +228 -21
  130. package/src/tokens.ts +1 -0
  131. package/src/vocabulary.ts +9 -0
@@ -17,7 +17,8 @@ import type { Navigation } from "../router.js";
17
17
  import type { RestEvent, RestMe, RestPlugin, RestProject } from "../schemas.js";
18
18
  import { CONSOLE_TOKENS } from "../tokens.js";
19
19
  import { wordsForFailure } from "../use-action.js";
20
- import { CONSOLE_LANGUAGES } from "../vocabulary.js";
20
+ import { CONSOLE_LANGUAGES, languagesFirst } from "../vocabulary.js";
21
+ import { cx, Spinner } from "./ui.js";
21
22
  import { weekStartOf } from "./workspace.js";
22
23
 
23
24
  // A plugin's page or widget on the console's page (ADR-0054, docs/specs/plugins.md, "Pages"):
@@ -30,6 +31,11 @@ import { weekStartOf } from "./workspace.js";
30
31
  const MAX_PAGE_HEIGHT = 20_000;
31
32
  /** How often the plugin's own events are asked for while a frame listens for them. */
32
33
  const EVENTS_POLL_MS = 5_000;
34
+ /**
35
+ * How long after a frame has loaded, or said it is ready, the spinner stands in its place when
36
+ * the page has not grown: a page that draws nothing to measure, or says no height.
37
+ */
38
+ const DRAW_WAIT_MS = 5_000;
33
39
 
34
40
  export interface PluginFrameProps {
35
41
  readonly plugin: RestPlugin;
@@ -231,6 +237,14 @@ export function PluginFrame(props: PluginFrameProps) {
231
237
  const [height, setHeight] = useState(480);
232
238
  /** Whether the console served the frame nothing to show: a plugin turned off since the page read it, say. */
233
239
  const [unavailable, setUnavailable] = useState(false);
240
+ /**
241
+ * Whether the plugin's page has yet to draw: a spinner stands in the frame's place until the
242
+ * page grows past the height it had before it drew anything, which it says first, or for a
243
+ * while after the frame loaded, so that a page that draws nothing is shown as it is.
244
+ */
245
+ const [drawing, setDrawing] = useState(true);
246
+ const drawWait = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
247
+ const emptyHeight = useRef<number | undefined>(undefined);
234
248
  const subscribed = useRef<Set<BridgeEventKind>>(new Set());
235
249
  const ready = useRef(false);
236
250
  const lastPluginEvent = useRef(0);
@@ -247,7 +261,22 @@ export function PluginFrame(props: PluginFrameProps) {
247
261
  [scoped, page, widget, project],
248
262
  );
249
263
  // biome-ignore lint/correctness/useExhaustiveDependencies: another address is another document
250
- useEffect(() => setUnavailable(false), [src]);
264
+ useEffect(() => {
265
+ setUnavailable(false);
266
+ setDrawing(true);
267
+ emptyHeight.current = undefined;
268
+ clearTimeout(drawWait.current);
269
+ }, [src]);
270
+ useEffect(() => () => clearTimeout(drawWait.current), []);
271
+ const drawn = useCallback(() => {
272
+ clearTimeout(drawWait.current);
273
+ setDrawing(false);
274
+ }, []);
275
+ /** The page is shown as it stands a while from now, whether or not it says a height by then. */
276
+ const drawnSoon = useCallback(() => {
277
+ clearTimeout(drawWait.current);
278
+ drawWait.current = setTimeout(() => setDrawing(false), DRAW_WAIT_MS);
279
+ }, []);
251
280
 
252
281
  const post = useCallback((message: BridgeToFrame, transfer?: Transferable[]) => {
253
282
  // The frame's origin is opaque, so the target is any: the frame itself checks the sender.
@@ -274,6 +303,7 @@ export function PluginFrame(props: PluginFrameProps) {
274
303
  return;
275
304
  case "ready":
276
305
  ready.current = true;
306
+ drawnSoon();
277
307
  post({
278
308
  v: PLUGIN_BRIDGE_VERSION,
279
309
  t: "init",
@@ -291,7 +321,8 @@ export function PluginFrame(props: PluginFrameProps) {
291
321
  },
292
322
  workspace: {
293
323
  name: me.workspace.name,
294
- languages: [...CONSOLE_LANGUAGES],
324
+ language: me.workspace.language ?? CONSOLE_LANGUAGES[0],
325
+ languages: languagesFirst(me.workspace.language ?? CONSOLE_LANGUAGES[0]),
295
326
  timeZone: me.workspace.timeZone ?? "UTC",
296
327
  weekStart: weekStartOf(me.workspace),
297
328
  firstDay: firstDayOfWeek(language, weekStartOf(me.workspace)),
@@ -326,6 +357,13 @@ export function PluginFrame(props: PluginFrameProps) {
326
357
  }
327
358
  return;
328
359
  case "resize":
360
+ // The first height is the empty page's, said before the page drew anything, its own
361
+ // padding included; a greater one is the page drawn.
362
+ if (emptyHeight.current === undefined) {
363
+ emptyHeight.current = message.height;
364
+ } else if (message.height > emptyHeight.current) {
365
+ drawn();
366
+ }
329
367
  // A widget fills the cells its size gives it (ADR-0059); a page is as tall as it asks.
330
368
  if (!isWidget) {
331
369
  setHeight(Math.max(40, Math.min(Math.ceil(message.height), MAX_PAGE_HEIGHT)));
@@ -453,20 +491,37 @@ export function PluginFrame(props: PluginFrameProps) {
453
491
  return <p className="sc-plugin-frame-unavailable">{words.frameUnavailable}</p>;
454
492
  }
455
493
  return (
456
- <iframe
457
- ref={frame}
458
- className={
459
- isWidget ? "sc-plugin-frame sc-plugin-frame-widget" : "sc-plugin-frame sc-plugin-frame-page"
460
- }
461
- src={src}
462
- title={props.title}
463
- // As the document's own policy says: scripts; forms firing their submit event, which the
464
- // policy's form-action 'none' keeps from going anywhere; and a window for a link a person
465
- // pressed in the frame, which the browser holds to the press (ADR-0078), opened out of the
466
- // sandbox so that the page it shows is a page as any.
467
- sandbox="allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox"
468
- referrerPolicy="origin"
494
+ <div
495
+ className={cx(
496
+ "sc-plugin-frame-box",
497
+ isWidget ? "sc-plugin-frame-box-widget" : "sc-plugin-frame-box-page",
498
+ drawing && "sc-plugin-frame-loading",
499
+ )}
469
500
  style={isWidget ? undefined : { height: `${height}px` }}
470
- />
501
+ >
502
+ <iframe
503
+ ref={frame}
504
+ className={
505
+ isWidget
506
+ ? "sc-plugin-frame sc-plugin-frame-widget"
507
+ : "sc-plugin-frame sc-plugin-frame-page"
508
+ }
509
+ src={src}
510
+ title={props.title}
511
+ // As the document's own policy says: scripts; forms firing their submit event, which the
512
+ // policy's form-action 'none' keeps from going anywhere; and a window for a link a person
513
+ // pressed in the frame, which the browser holds to the press (ADR-0078), opened out of the
514
+ // sandbox so that the page it shows is a page as any.
515
+ sandbox="allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox"
516
+ referrerPolicy="origin"
517
+ // Loaded, with its scripts run: a page that never says it is ready is shown as it is.
518
+ onLoad={drawnSoon}
519
+ />
520
+ {drawing && (
521
+ <div className="sc-plugin-frame-wait">
522
+ <Spinner label={pack.common.loading} />
523
+ </div>
524
+ )}
525
+ </div>
471
526
  );
472
527
  }
@@ -0,0 +1,108 @@
1
+ import type { ReactNode } from "react";
2
+ import type { RestSession } from "../api.js";
3
+ import { useWords } from "../i18n/index.js";
4
+ import { Badge, Button, cx, Time } from "./ui.js";
5
+
6
+ // The browsers a person is signed in on (board #309), on a page of the settings of their own: each
7
+ // with what it is, when it was last used, where it last came from and the day it signed in, the
8
+ // one they look from marked; the way to sign any other out, and all the others at once. Takes its
9
+ // data as props and nothing from the network.
10
+
11
+ export interface SessionListProps {
12
+ readonly sessions: readonly RestSession[];
13
+ /** How many days a session lasts without being used; left out, the page does not say. */
14
+ readonly idleDays?: number | undefined;
15
+ /** Called to end a session that is not the current one. Left out, sessions cannot be ended here. */
16
+ readonly onEnd?: ((session: RestSession) => void) | undefined;
17
+ /** Called to end every session but the current one. Left out, there is no such button. */
18
+ readonly onEndOthers?: (() => void) | undefined;
19
+ readonly busy?: boolean | undefined;
20
+ readonly empty?: ReactNode;
21
+ }
22
+
23
+ /** The platforms held in a hand, as the server names them: drawn as a phone, the rest as a screen. */
24
+ const HANDHELD: ReadonlySet<string> = new Set(["iOS", "Android"]);
25
+
26
+ function DeviceIcon(props: { readonly handheld: boolean }) {
27
+ return (
28
+ <span className="sc-session-icon">
29
+ <svg viewBox="0 0 20 20" aria-hidden="true">
30
+ {props.handheld ? (
31
+ <>
32
+ <rect x="5.5" y="2.5" width="9" height="15" rx="2" />
33
+ <path d="M9 14.75h2" />
34
+ </>
35
+ ) : (
36
+ <>
37
+ <rect x="2.5" y="3.5" width="15" height="10" rx="1.5" />
38
+ <path d="M7 16.5h6M10 13.5v3" />
39
+ </>
40
+ )}
41
+ </svg>
42
+ </span>
43
+ );
44
+ }
45
+
46
+ export function SessionList(props: SessionListProps) {
47
+ const words = useWords().sessions;
48
+ const others = props.sessions.filter((session) => !session.current);
49
+ const deviceOf = (session: RestSession): string =>
50
+ session.browser !== null && session.platform !== null
51
+ ? words.on(session.browser, session.platform)
52
+ : (session.browser ?? session.platform ?? words.unknownBrowser);
53
+ return (
54
+ <section className="sc-panel sc-sessions" aria-label={words.title}>
55
+ <div className="sc-section-head">
56
+ <h2 className="sc-section-title">{words.title}</h2>
57
+ {props.onEndOthers !== undefined && others.length > 0 && (
58
+ <Button size="sm" disabled={props.busy} onClick={props.onEndOthers}>
59
+ {words.endOthers}
60
+ </Button>
61
+ )}
62
+ </div>
63
+ <p className="sc-settings-hint">
64
+ {props.idleDays === undefined ? words.intro : words.introIdle(String(props.idleDays))}
65
+ </p>
66
+ {props.sessions.length === 0 ? (
67
+ (props.empty ?? null)
68
+ ) : (
69
+ <ul className="sc-session-list" aria-label={words.list}>
70
+ {props.sessions.map((session) => (
71
+ <li
72
+ key={session.id}
73
+ className={cx("sc-session", session.current && "sc-session-current")}
74
+ >
75
+ <DeviceIcon handheld={session.platform !== null && HANDHELD.has(session.platform)} />
76
+ <div className="sc-session-body">
77
+ <p className="sc-session-name">
78
+ <span>{deviceOf(session)}</span>
79
+ {session.current && <Badge tone="accent">{words.current}</Badge>}
80
+ </p>
81
+ <p className="sc-session-meta">
82
+ <span>
83
+ {words.lastActive} <Time iso={session.lastSeenAt} />
84
+ </span>
85
+ {session.address !== null && <span>{words.from(session.address)}</span>}
86
+ <span>
87
+ {words.signedIn} <Time iso={session.createdAt} precision="day" />
88
+ </span>
89
+ </p>
90
+ </div>
91
+ {!session.current && props.onEnd !== undefined && (
92
+ <Button
93
+ size="sm"
94
+ variant="ghost"
95
+ className="sc-session-end"
96
+ disabled={props.busy}
97
+ onClick={() => props.onEnd?.(session)}
98
+ >
99
+ {words.end}
100
+ </Button>
101
+ )}
102
+ </li>
103
+ ))}
104
+ </ul>
105
+ )}
106
+ </section>
107
+ );
108
+ }
@@ -1,4 +1,4 @@
1
- import { type ReactNode, useEffect, useRef } from "react";
1
+ import { Fragment, type ReactNode, useEffect, useId, useRef, useState } from "react";
2
2
  import type { RestPerson } from "../api.js";
3
3
  import { useWords } from "../i18n/index.js";
4
4
  import { Avatar, cx, personName } from "./ui.js";
@@ -69,30 +69,236 @@ export function isPlainClick(event: {
69
69
  return event.button === 0 && !event.metaKey && !event.ctrlKey && !event.shiftKey && !event.altKey;
70
70
  }
71
71
 
72
- /** A tab that opens a list of pages, as a group of several plugins does: a details, as the person's menu is. Closes once used. */
72
+ /** A page of a menu, by its name: followed in place for a plain click, and the menu closes. */
73
+ function MenuLink(props: {
74
+ readonly item: NavItem;
75
+ readonly onFollow: (href: string) => void;
76
+ readonly className?: string | undefined;
77
+ }) {
78
+ const { item } = props;
79
+ return (
80
+ <a
81
+ href={item.href}
82
+ className={cx("sc-menu-link", props.className)}
83
+ aria-current={item.current ? "page" : undefined}
84
+ onClick={(event) => {
85
+ if (isPlainClick(event)) {
86
+ event.preventDefault();
87
+ props.onFollow(item.href);
88
+ }
89
+ }}
90
+ >
91
+ {item.label}
92
+ </a>
93
+ );
94
+ }
95
+
96
+ /**
97
+ * The widest name a tab shows whole, in Latin letters: the stylesheet stops a wider one short
98
+ * with an ellipsis, about here, and the tab's title carries it whole. A plugin's name runs to
99
+ * 40 characters.
100
+ */
101
+ const TAB_LABEL_WHOLE = 28;
102
+
103
+ /** The characters a name runs about twice as wide in: Hangul, the Chinese characters, kana, the full-width forms. */
104
+ const WIDE =
105
+ /[\u1100-\u115f\u2e80-\ua4cf\uac00-\ud7a3\uf900-\ufaff\ufe30-\ufe4f\uff00-\uff60\uffe0-\uffe6]/u;
106
+
107
+ /** How wide a name runs, in Latin letters. */
108
+ function widthOf(label: string): number {
109
+ let width = 0;
110
+ for (const character of label) {
111
+ width += WIDE.test(character) ? 2 : 1;
112
+ }
113
+ return width;
114
+ }
115
+
116
+ /** A tab's name, as the stylesheet may stop it short, and the title that then says it whole. */
117
+ function tabLabel(label: string): { readonly title: string | undefined } {
118
+ return { title: widthOf(label) > TAB_LABEL_WHOLE ? label : undefined };
119
+ }
120
+
121
+ /** How close to the window's edge a list opened from a tab may come. */
122
+ const PANEL_GUTTER_PX = 8;
123
+ /** How far below its tab, off the header's line, a list opens. */
124
+ const PANEL_DROP_PX = 6;
125
+ /** How far a list's edge stands before the names in it: its own padding and a link's. */
126
+ const PANEL_INSET_PX = 14;
127
+
128
+ /** The least a list is let be tall where the window is short: a few of its pages, scrolled. */
129
+ const PANEL_SHORTEST_PX = 120;
130
+
131
+ /**
132
+ * Whether the browser shows a popover. One that does not, as Safari before 17, has the list
133
+ * shown and hidden by the tab itself, without the browser's ways of closing it.
134
+ */
135
+ const POPOVERS =
136
+ typeof HTMLElement === "undefined" || typeof HTMLElement.prototype.showPopover === "function";
137
+
138
+ /**
139
+ * Puts the list a tab opened under the tab, the names in it lined up with the tab's, within the
140
+ * page's width, the scrollbar left out, and no taller than the window has room for below it, a
141
+ * longer list scrolling within itself: the list is in the top layer, where nothing of the page
142
+ * places it.
143
+ */
144
+ function placeUnder(tab: HTMLElement | null, box: HTMLElement | null): void {
145
+ if (tab === null || box === null) {
146
+ return;
147
+ }
148
+ const at = tab.getBoundingClientRect();
149
+ const { clientWidth, clientHeight } = document.documentElement;
150
+ const room = clientWidth - box.offsetWidth - PANEL_GUTTER_PX;
151
+ const top = at.bottom + PANEL_DROP_PX;
152
+ box.style.left = `${Math.max(PANEL_GUTTER_PX, Math.min(at.left - PANEL_INSET_PX, room))}px`;
153
+ box.style.top = `${top}px`;
154
+ box.style.maxHeight = `${Math.max(PANEL_SHORTEST_PX, clientHeight - top - PANEL_GUTTER_PX)}px`;
155
+ }
156
+
157
+ /** Closes a list: the browser's popover, or the list the tab shows by hand where there is none. */
158
+ function hideList(box: HTMLElement | null, setOpen: (open: boolean) => void): void {
159
+ if (!POPOVERS) {
160
+ setOpen(false);
161
+ return;
162
+ }
163
+ if (box?.matches(":popover-open") === true) {
164
+ box.hidePopover();
165
+ }
166
+ }
167
+
168
+ /** How long a mouse rests on a tab before its list opens: one passing along the row opens nothing. */
169
+ const HOVER_OPEN_MS = 80;
170
+ /** How long a list stays once the mouse has left it and its tab: the moment it takes to cross from one to the other. */
171
+ const HOVER_LEAVE_MS = 200;
172
+
173
+ /** Whether a click is a mouse's, which had the list open under it before it clicked; a finger's, a pen's or a key's otherwise. */
174
+ function byMouse(event: Event): boolean {
175
+ return (
176
+ typeof PointerEvent !== "undefined" &&
177
+ event instanceof PointerEvent &&
178
+ event.pointerType === "mouse"
179
+ );
180
+ }
181
+
182
+ /**
183
+ * A tab that opens a list of pages, as a group of several plugins does. The list is a popover:
184
+ * in the top layer, out of the row of tabs, which scrolls sideways and clips whatever hangs out
185
+ * of it, and dismissed by a click elsewhere or the escape key, as the browser does it. A mouse
186
+ * resting on the tab opens it, and leaving the tab and the list closes it; a mouse's click on
187
+ * the tab, with the list open under it already, goes where the tab leads, the group's first
188
+ * page; a finger, a pen or a key opens and closes the list by a press. Placed under its tab
189
+ * when it opens; closed once used, when the row scrolls or the window changes, and when the
190
+ * window loses the focus, as it does to a click in a plugin's frame, which the page never sees.
191
+ */
73
192
  function TabGroup(props: { readonly item: NavItem; readonly onNavigate: (href: string) => void }) {
74
- const details = useRef<HTMLDetailsElement>(null);
193
+ const id = useId();
194
+ const tab = useRef<HTMLButtonElement>(null);
195
+ const panel = useRef<HTMLDivElement>(null);
196
+ const [open, setOpen] = useState(false);
197
+ const resting = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
198
+ const { item } = props;
199
+ const after = (ms: number, what: () => void) => {
200
+ clearTimeout(resting.current);
201
+ resting.current = setTimeout(what, ms);
202
+ };
203
+ // Shown here rather than by the browser, and placed at once, before anything is painted: the
204
+ // list has its width only once it shows, and a list placed before that, then again once the
205
+ // browser said it opened, could stand out of the window in between. The button stays its
206
+ // invoker, so that a click on it is not a click outside that closes it first.
207
+ const show = () => {
208
+ const box = panel.current;
209
+ if (box === null) {
210
+ return;
211
+ }
212
+ if (!POPOVERS) {
213
+ setOpen(true);
214
+ return;
215
+ }
216
+ if (!box.matches(":popover-open")) {
217
+ box.showPopover();
218
+ placeUnder(tab.current, box);
219
+ }
220
+ };
221
+ const hide = () => hideList(panel.current, setOpen);
75
222
  useEffect(() => {
76
- const onClick = (event: MouseEvent) => {
77
- const element = details.current;
78
- if (
79
- element?.open === true &&
80
- !(event.target instanceof Node && element.contains(event.target))
81
- ) {
82
- element.open = false;
223
+ const box = panel.current;
224
+ if (box === null) {
225
+ return;
226
+ }
227
+ const onToggle = (event: Event) => {
228
+ setOpen((event as ToggleEvent).newState === "open");
229
+ };
230
+ // Open, it is where its tab is; the row of tabs scrolling, or the window changing, would
231
+ // leave it where the tab no longer is, so either closes it, and so does the window losing
232
+ // the focus. A scroll within the list is the list's own. Heard from the start rather than
233
+ // once the page knows it opened, which comes a moment after it shows.
234
+ const close = (event: Event) => {
235
+ const shown = POPOVERS ? box.matches(":popover-open") : !box.hidden;
236
+ const within =
237
+ event.type === "scroll" && event.target instanceof Node && box.contains(event.target);
238
+ if (shown && !within) {
239
+ hideList(box, setOpen);
83
240
  }
84
241
  };
85
- document.addEventListener("click", onClick);
86
- return () => document.removeEventListener("click", onClick);
242
+ box.addEventListener("toggle", onToggle);
243
+ window.addEventListener("resize", close);
244
+ window.addEventListener("scroll", close, true);
245
+ window.addEventListener("blur", close);
246
+ return () => {
247
+ clearTimeout(resting.current);
248
+ box.removeEventListener("toggle", onToggle);
249
+ window.removeEventListener("resize", close);
250
+ window.removeEventListener("scroll", close, true);
251
+ window.removeEventListener("blur", close);
252
+ };
87
253
  }, []);
88
- const { item } = props;
254
+ useEffect(() => {
255
+ // Shown by the tab itself where the browser has no popover: placed now that it shows.
256
+ if (open && !POPOVERS) {
257
+ placeUnder(tab.current, panel.current);
258
+ }
259
+ }, [open]);
89
260
  return (
90
- <details className="sc-tab-group" ref={details}>
91
- <summary
261
+ <div
262
+ className="sc-tab-group"
263
+ // The list is a child of this box, wherever the browser draws it: a mouse over either is
264
+ // over the box, and leaving the box is leaving both.
265
+ onPointerEnter={(event) => {
266
+ if (event.pointerType === "mouse") {
267
+ after(HOVER_OPEN_MS, show);
268
+ }
269
+ }}
270
+ onPointerLeave={(event) => {
271
+ if (event.pointerType === "mouse") {
272
+ after(HOVER_LEAVE_MS, hide);
273
+ }
274
+ }}
275
+ >
276
+ <button
277
+ type="button"
278
+ ref={tab}
92
279
  className={cx("sc-tab", "sc-tab-summary", item.current && "sc-tab-current")}
93
280
  aria-current={item.current ? "page" : undefined}
281
+ aria-expanded={open}
282
+ aria-controls={id}
283
+ popoverTarget={id}
284
+ {...tabLabel(item.label)}
285
+ onClick={(event) => {
286
+ event.preventDefault();
287
+ clearTimeout(resting.current);
288
+ if (byMouse(event.nativeEvent)) {
289
+ hide();
290
+ props.onNavigate(item.href);
291
+ return;
292
+ }
293
+ const shown = POPOVERS ? panel.current?.matches(":popover-open") === true : open;
294
+ if (shown) {
295
+ hide();
296
+ } else {
297
+ show();
298
+ }
299
+ }}
94
300
  >
95
- {item.label}
301
+ <span className="sc-tab-label">{item.label}</span>
96
302
  <svg className="sc-menu-caret" viewBox="0 0 16 16" aria-hidden="true">
97
303
  <path
98
304
  d="M4 6l4 4 4-4"
@@ -103,31 +309,29 @@ function TabGroup(props: { readonly item: NavItem; readonly onNavigate: (href: s
103
309
  strokeLinejoin="round"
104
310
  />
105
311
  </svg>
106
- </summary>
107
- <div className="sc-menu-panel sc-tab-group-panel">
312
+ </button>
313
+ <div
314
+ id={id}
315
+ ref={panel}
316
+ popover="auto"
317
+ hidden={!POPOVERS && !open}
318
+ className="sc-menu-panel sc-tab-group-panel"
319
+ >
108
320
  <div className="sc-menu-list">
109
321
  {(item.children ?? []).map((child) => (
110
- <a
322
+ <MenuLink
111
323
  key={child.href}
112
- href={child.href}
113
- className="sc-menu-link"
114
- aria-current={child.current ? "page" : undefined}
115
- onClick={(event) => {
116
- if (isPlainClick(event)) {
117
- event.preventDefault();
118
- if (details.current !== null) {
119
- details.current.open = false;
120
- }
121
- props.onNavigate(child.href);
122
- }
324
+ item={child}
325
+ className="sc-group-link"
326
+ onFollow={(href) => {
327
+ hide();
328
+ props.onNavigate(href);
123
329
  }}
124
- >
125
- {child.label}
126
- </a>
330
+ />
127
331
  ))}
128
332
  </div>
129
333
  </div>
130
- </details>
334
+ </div>
131
335
  );
132
336
  }
133
337
 
@@ -147,6 +351,10 @@ function PersonMenu(props: {
147
351
  details.current.open = false;
148
352
  }
149
353
  };
354
+ const follow = (href: string) => {
355
+ close();
356
+ props.onNavigate(href);
357
+ };
150
358
  // A click elsewhere, or the escape key, closes it, as a menu is expected to.
151
359
  useEffect(() => {
152
360
  const onClick = (event: MouseEvent) => {
@@ -163,11 +371,19 @@ function PersonMenu(props: {
163
371
  details.current.open = false;
164
372
  }
165
373
  };
374
+ // A click in a plugin's frame is a click elsewhere the page never sees: the window loses the focus.
375
+ const onBlur = () => {
376
+ if (details.current !== null) {
377
+ details.current.open = false;
378
+ }
379
+ };
166
380
  document.addEventListener("click", onClick);
167
381
  document.addEventListener("keydown", onKey);
382
+ window.addEventListener("blur", onBlur);
168
383
  return () => {
169
384
  document.removeEventListener("click", onClick);
170
385
  document.removeEventListener("keydown", onKey);
386
+ window.removeEventListener("blur", onBlur);
171
387
  };
172
388
  }, []);
173
389
  const { person } = props;
@@ -193,23 +409,19 @@ function PersonMenu(props: {
193
409
  {person.name !== null && <span className="sc-muted">{person.login}</span>}
194
410
  </p>
195
411
  <nav className="sc-menu-list" aria-label={words.shell.workspace}>
196
- {props.items.map((item) => (
197
- <a
198
- key={item.href}
199
- href={item.href}
200
- className="sc-menu-link"
201
- aria-current={item.current ? "page" : undefined}
202
- onClick={(event) => {
203
- if (isPlainClick(event)) {
204
- event.preventDefault();
205
- close();
206
- props.onNavigate(item.href);
207
- }
208
- }}
209
- >
210
- {item.label}
211
- </a>
212
- ))}
412
+ {props.items.map((item) =>
413
+ item.children !== undefined && item.children.length > 0 ? (
414
+ // A group of plugins, one by one under the group's name: where a tab opens a list.
415
+ <Fragment key={item.href}>
416
+ <p className="sc-menu-heading">{item.label}</p>
417
+ {item.children.map((child) => (
418
+ <MenuLink key={child.href} item={child} onFollow={follow} />
419
+ ))}
420
+ </Fragment>
421
+ ) : (
422
+ <MenuLink key={item.href} item={item} onFollow={follow} />
423
+ ),
424
+ )}
213
425
  </nav>
214
426
  {props.languages.length > 1 && props.onLanguage !== undefined && (
215
427
  <fieldset className="sc-menu-languages">
@@ -369,9 +581,10 @@ export function Shell(props: ShellProps) {
369
581
  href={item.href}
370
582
  className={cx("sc-tab", item.current && "sc-tab-current")}
371
583
  aria-current={item.current ? "page" : undefined}
584
+ {...tabLabel(item.label)}
372
585
  onClick={follow(item.href)}
373
586
  >
374
- {item.label}
587
+ <span className="sc-tab-label">{item.label}</span>
375
588
  {item.count !== undefined && item.count > 0 && (
376
589
  <span className="sc-nav-count">{item.count}</span>
377
590
  )}