@siiway/ui-react 0.0.0 → 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.
Files changed (258) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -1
  3. package/dist/components/AppShell.d.ts +29 -0
  4. package/dist/components/AppShell.d.ts.map +1 -0
  5. package/dist/components/AppShell.js +220 -0
  6. package/dist/components/AppShell.js.map +1 -0
  7. package/dist/components/AuthShell.d.ts +21 -0
  8. package/dist/components/AuthShell.d.ts.map +1 -0
  9. package/dist/components/AuthShell.js +53 -0
  10. package/dist/components/AuthShell.js.map +1 -0
  11. package/dist/components/BrandMark.d.ts +11 -0
  12. package/dist/components/BrandMark.d.ts.map +1 -0
  13. package/dist/components/BrandMark.js +11 -0
  14. package/dist/components/BrandMark.js.map +1 -0
  15. package/dist/components/ButtonLink.d.ts +11 -0
  16. package/dist/components/ButtonLink.d.ts.map +1 -0
  17. package/dist/components/ButtonLink.js +15 -0
  18. package/dist/components/ButtonLink.js.map +1 -0
  19. package/dist/components/CheckboxField.d.ts +15 -0
  20. package/dist/components/CheckboxField.d.ts.map +1 -0
  21. package/dist/components/CheckboxField.js +23 -0
  22. package/dist/components/CheckboxField.js.map +1 -0
  23. package/dist/components/ColorPickerInput.d.ts +17 -0
  24. package/dist/components/ColorPickerInput.d.ts.map +1 -0
  25. package/dist/components/ColorPickerInput.js +97 -0
  26. package/dist/components/ColorPickerInput.js.map +1 -0
  27. package/dist/components/CommandPalette.d.ts +65 -0
  28. package/dist/components/CommandPalette.d.ts.map +1 -0
  29. package/dist/components/CommandPalette.js +269 -0
  30. package/dist/components/CommandPalette.js.map +1 -0
  31. package/dist/components/CopyButton.d.ts +27 -0
  32. package/dist/components/CopyButton.d.ts.map +1 -0
  33. package/dist/components/CopyButton.js +20 -0
  34. package/dist/components/CopyButton.js.map +1 -0
  35. package/dist/components/CopyIdTrigger.d.ts +31 -0
  36. package/dist/components/CopyIdTrigger.d.ts.map +1 -0
  37. package/dist/components/CopyIdTrigger.js +72 -0
  38. package/dist/components/CopyIdTrigger.js.map +1 -0
  39. package/dist/components/DataList.d.ts +78 -0
  40. package/dist/components/DataList.d.ts.map +1 -0
  41. package/dist/components/DataList.js +150 -0
  42. package/dist/components/DataList.js.map +1 -0
  43. package/dist/components/DetailHeader.d.ts +26 -0
  44. package/dist/components/DetailHeader.d.ts.map +1 -0
  45. package/dist/components/DetailHeader.js +49 -0
  46. package/dist/components/DetailHeader.js.map +1 -0
  47. package/dist/components/DurationInput.d.ts +25 -0
  48. package/dist/components/DurationInput.d.ts.map +1 -0
  49. package/dist/components/DurationInput.js +83 -0
  50. package/dist/components/DurationInput.js.map +1 -0
  51. package/dist/components/EmptyState.d.ts +14 -0
  52. package/dist/components/EmptyState.d.ts.map +1 -0
  53. package/dist/components/EmptyState.js +41 -0
  54. package/dist/components/EmptyState.js.map +1 -0
  55. package/dist/components/FormPage.d.ts +22 -0
  56. package/dist/components/FormPage.d.ts.map +1 -0
  57. package/dist/components/FormPage.js +65 -0
  58. package/dist/components/FormPage.js.map +1 -0
  59. package/dist/components/GetStartedPanel.d.ts +16 -0
  60. package/dist/components/GetStartedPanel.d.ts.map +1 -0
  61. package/dist/components/GetStartedPanel.js +96 -0
  62. package/dist/components/GetStartedPanel.js.map +1 -0
  63. package/dist/components/LabeledLine.d.ts +12 -0
  64. package/dist/components/LabeledLine.d.ts.map +1 -0
  65. package/dist/components/LabeledLine.js +6 -0
  66. package/dist/components/LabeledLine.js.map +1 -0
  67. package/dist/components/ListSearch.d.ts +15 -0
  68. package/dist/components/ListSearch.d.ts.map +1 -0
  69. package/dist/components/ListSearch.js +23 -0
  70. package/dist/components/ListSearch.js.map +1 -0
  71. package/dist/components/LoadingBar.d.ts +8 -0
  72. package/dist/components/LoadingBar.d.ts.map +1 -0
  73. package/dist/components/LoadingBar.js +55 -0
  74. package/dist/components/LoadingBar.js.map +1 -0
  75. package/dist/components/LoadingPage.d.ts +6 -0
  76. package/dist/components/LoadingPage.d.ts.map +1 -0
  77. package/dist/components/LoadingPage.js +25 -0
  78. package/dist/components/LoadingPage.js.map +1 -0
  79. package/dist/components/MenuPanel.d.ts +34 -0
  80. package/dist/components/MenuPanel.d.ts.map +1 -0
  81. package/dist/components/MenuPanel.js +98 -0
  82. package/dist/components/MenuPanel.js.map +1 -0
  83. package/dist/components/Outline.d.ts +23 -0
  84. package/dist/components/Outline.d.ts.map +1 -0
  85. package/dist/components/Outline.js +150 -0
  86. package/dist/components/Outline.js.map +1 -0
  87. package/dist/components/Overview.d.ts +103 -0
  88. package/dist/components/Overview.d.ts.map +1 -0
  89. package/dist/components/Overview.js +286 -0
  90. package/dist/components/Overview.js.map +1 -0
  91. package/dist/components/PageHeader.d.ts +19 -0
  92. package/dist/components/PageHeader.d.ts.map +1 -0
  93. package/dist/components/PageHeader.js +41 -0
  94. package/dist/components/PageHeader.js.map +1 -0
  95. package/dist/components/Pagination.d.ts +19 -0
  96. package/dist/components/Pagination.d.ts.map +1 -0
  97. package/dist/components/Pagination.js +47 -0
  98. package/dist/components/Pagination.js.map +1 -0
  99. package/dist/components/PasswordInput.d.ts +3 -0
  100. package/dist/components/PasswordInput.d.ts.map +1 -0
  101. package/dist/components/PasswordInput.js +29 -0
  102. package/dist/components/PasswordInput.js.map +1 -0
  103. package/dist/components/PrevNext.d.ts +16 -0
  104. package/dist/components/PrevNext.d.ts.map +1 -0
  105. package/dist/components/PrevNext.js +61 -0
  106. package/dist/components/PrevNext.js.map +1 -0
  107. package/dist/components/SearchButton.d.ts +16 -0
  108. package/dist/components/SearchButton.d.ts.map +1 -0
  109. package/dist/components/SearchButton.js +63 -0
  110. package/dist/components/SearchButton.js.map +1 -0
  111. package/dist/components/SearchField.d.ts +10 -0
  112. package/dist/components/SearchField.d.ts.map +1 -0
  113. package/dist/components/SearchField.js +67 -0
  114. package/dist/components/SearchField.js.map +1 -0
  115. package/dist/components/Segmented.d.ts +28 -0
  116. package/dist/components/Segmented.d.ts.map +1 -0
  117. package/dist/components/Segmented.js +74 -0
  118. package/dist/components/Segmented.js.map +1 -0
  119. package/dist/components/Select.d.ts +22 -0
  120. package/dist/components/Select.d.ts.map +1 -0
  121. package/dist/components/Select.js +50 -0
  122. package/dist/components/Select.js.map +1 -0
  123. package/dist/components/Settings.d.ts +103 -0
  124. package/dist/components/Settings.d.ts.map +1 -0
  125. package/dist/components/Settings.js +246 -0
  126. package/dist/components/Settings.js.map +1 -0
  127. package/dist/components/SideNav.d.ts +47 -0
  128. package/dist/components/SideNav.d.ts.map +1 -0
  129. package/dist/components/SideNav.js +125 -0
  130. package/dist/components/SideNav.js.map +1 -0
  131. package/dist/components/Skeletons.d.ts +37 -0
  132. package/dist/components/Skeletons.d.ts.map +1 -0
  133. package/dist/components/Skeletons.js +109 -0
  134. package/dist/components/Skeletons.js.map +1 -0
  135. package/dist/components/StatusPage.d.ts +49 -0
  136. package/dist/components/StatusPage.d.ts.map +1 -0
  137. package/dist/components/StatusPage.js +110 -0
  138. package/dist/components/StatusPage.js.map +1 -0
  139. package/dist/components/ThemeMenu.d.ts +13 -0
  140. package/dist/components/ThemeMenu.d.ts.map +1 -0
  141. package/dist/components/ThemeMenu.js +18 -0
  142. package/dist/components/ThemeMenu.js.map +1 -0
  143. package/dist/components/ThemeSwitch.d.ts +17 -0
  144. package/dist/components/ThemeSwitch.d.ts.map +1 -0
  145. package/dist/components/ThemeSwitch.js +25 -0
  146. package/dist/components/ThemeSwitch.js.map +1 -0
  147. package/dist/components/TopBar.d.ts +49 -0
  148. package/dist/components/TopBar.d.ts.map +1 -0
  149. package/dist/components/TopBar.js +146 -0
  150. package/dist/components/TopBar.js.map +1 -0
  151. package/dist/components/ViewBanner.d.ts +17 -0
  152. package/dist/components/ViewBanner.d.ts.map +1 -0
  153. package/dist/components/ViewBanner.js +14 -0
  154. package/dist/components/ViewBanner.js.map +1 -0
  155. package/dist/hooks/platform.d.ts +7 -0
  156. package/dist/hooks/platform.d.ts.map +1 -0
  157. package/dist/hooks/platform.js +19 -0
  158. package/dist/hooks/platform.js.map +1 -0
  159. package/dist/hooks/useConfirm.d.ts +23 -0
  160. package/dist/hooks/useConfirm.d.ts.map +1 -0
  161. package/dist/hooks/useConfirm.js +55 -0
  162. package/dist/hooks/useConfirm.js.map +1 -0
  163. package/dist/hooks/useCopy.d.ts +12 -0
  164. package/dist/hooks/useCopy.d.ts.map +1 -0
  165. package/dist/hooks/useCopy.js +39 -0
  166. package/dist/hooks/useCopy.js.map +1 -0
  167. package/dist/hooks/useDebouncedSearch.d.ts +8 -0
  168. package/dist/hooks/useDebouncedSearch.d.ts.map +1 -0
  169. package/dist/hooks/useDebouncedSearch.js +19 -0
  170. package/dist/hooks/useDebouncedSearch.js.map +1 -0
  171. package/dist/hooks/usePageClamp.d.ts +4 -0
  172. package/dist/hooks/usePageClamp.d.ts.map +1 -0
  173. package/dist/hooks/usePageClamp.js +17 -0
  174. package/dist/hooks/usePageClamp.js.map +1 -0
  175. package/dist/hooks/useSearchShortcut.d.ts +8 -0
  176. package/dist/hooks/useSearchShortcut.d.ts.map +1 -0
  177. package/dist/hooks/useSearchShortcut.js +38 -0
  178. package/dist/hooks/useSearchShortcut.js.map +1 -0
  179. package/dist/hooks/useThemeMode.d.ts +29 -0
  180. package/dist/hooks/useThemeMode.d.ts.map +1 -0
  181. package/dist/hooks/useThemeMode.js +111 -0
  182. package/dist/hooks/useThemeMode.js.map +1 -0
  183. package/dist/hooks/useToastMessage.d.ts +22 -0
  184. package/dist/hooks/useToastMessage.d.ts.map +1 -0
  185. package/dist/hooks/useToastMessage.js +26 -0
  186. package/dist/hooks/useToastMessage.js.map +1 -0
  187. package/dist/index.d.ts +50 -0
  188. package/dist/index.d.ts.map +1 -0
  189. package/dist/index.js +60 -0
  190. package/dist/index.js.map +1 -0
  191. package/dist/provider/UiProvider.d.ts +60 -0
  192. package/dist/provider/UiProvider.d.ts.map +1 -0
  193. package/dist/provider/UiProvider.js +144 -0
  194. package/dist/provider/UiProvider.js.map +1 -0
  195. package/dist/provider/canvas.d.ts +7 -0
  196. package/dist/provider/canvas.d.ts.map +1 -0
  197. package/dist/provider/canvas.js +30 -0
  198. package/dist/provider/canvas.js.map +1 -0
  199. package/dist/provider/context.d.ts +69 -0
  200. package/dist/provider/context.d.ts.map +1 -0
  201. package/dist/provider/context.js +84 -0
  202. package/dist/provider/context.js.map +1 -0
  203. package/dist/theme/createTheme.d.ts +13 -0
  204. package/dist/theme/createTheme.d.ts.map +1 -0
  205. package/dist/theme/createTheme.js +18 -0
  206. package/dist/theme/createTheme.js.map +1 -0
  207. package/package.json +42 -2
  208. package/src/components/AppShell.tsx +337 -0
  209. package/src/components/AuthShell.tsx +104 -0
  210. package/src/components/BrandMark.tsx +28 -0
  211. package/src/components/ButtonLink.tsx +40 -0
  212. package/src/components/CheckboxField.tsx +50 -0
  213. package/src/components/ColorPickerInput.tsx +190 -0
  214. package/src/components/CommandPalette.tsx +479 -0
  215. package/src/components/CopyButton.tsx +76 -0
  216. package/src/components/CopyIdTrigger.tsx +127 -0
  217. package/src/components/DataList.tsx +332 -0
  218. package/src/components/DetailHeader.tsx +108 -0
  219. package/src/components/DurationInput.tsx +133 -0
  220. package/src/components/EmptyState.tsx +67 -0
  221. package/src/components/FormPage.tsx +109 -0
  222. package/src/components/GetStartedPanel.tsx +132 -0
  223. package/src/components/LabeledLine.tsx +28 -0
  224. package/src/components/ListSearch.tsx +64 -0
  225. package/src/components/LoadingBar.tsx +81 -0
  226. package/src/components/LoadingPage.tsx +41 -0
  227. package/src/components/MenuPanel.tsx +163 -0
  228. package/src/components/Outline.tsx +208 -0
  229. package/src/components/Overview.tsx +448 -0
  230. package/src/components/PageHeader.tsx +69 -0
  231. package/src/components/Pagination.tsx +105 -0
  232. package/src/components/PasswordInput.tsx +50 -0
  233. package/src/components/PrevNext.tsx +98 -0
  234. package/src/components/SearchButton.tsx +90 -0
  235. package/src/components/SearchField.tsx +97 -0
  236. package/src/components/Segmented.tsx +136 -0
  237. package/src/components/Select.tsx +110 -0
  238. package/src/components/Settings.tsx +432 -0
  239. package/src/components/SideNav.tsx +258 -0
  240. package/src/components/Skeletons.tsx +233 -0
  241. package/src/components/StatusPage.tsx +212 -0
  242. package/src/components/ThemeMenu.tsx +51 -0
  243. package/src/components/ThemeSwitch.tsx +49 -0
  244. package/src/components/TopBar.tsx +293 -0
  245. package/src/components/ViewBanner.tsx +51 -0
  246. package/src/hooks/platform.ts +22 -0
  247. package/src/hooks/useConfirm.tsx +97 -0
  248. package/src/hooks/useCopy.ts +39 -0
  249. package/src/hooks/useDebouncedSearch.ts +19 -0
  250. package/src/hooks/usePageClamp.ts +20 -0
  251. package/src/hooks/useSearchShortcut.ts +49 -0
  252. package/src/hooks/useThemeMode.ts +168 -0
  253. package/src/hooks/useToastMessage.ts +38 -0
  254. package/src/index.ts +156 -0
  255. package/src/provider/UiProvider.tsx +256 -0
  256. package/src/provider/canvas.ts +31 -0
  257. package/src/provider/context.tsx +145 -0
  258. package/src/theme/createTheme.ts +29 -0
@@ -0,0 +1,163 @@
1
+ // The panel of an account or preferences menu: Prism's account menu (name and
2
+ // email, the theme and language switches, sign out) and the theme menus of
3
+ // Vellum and ThemeMenu. A popover, not a Fluent Menu, since its rows hold
4
+ // controls such as a segmented switch.
5
+
6
+ import {
7
+ PopoverSurface,
8
+ Text,
9
+ makeStyles,
10
+ mergeClasses,
11
+ tokens,
12
+ type PopoverSurfaceProps,
13
+ } from "@fluentui/react-components";
14
+ import type { MouseEventHandler, ReactElement, ReactNode } from "react";
15
+
16
+ const useStyles = makeStyles({
17
+ // PopoverSurface brings a drop-shadow filter (sized for an arrow this
18
+ // panel doesn't have) and a transparent border; this has one soft shadow
19
+ // and a hairline edge instead, like the rest of the floating surfaces.
20
+ panel: {
21
+ padding: "4px",
22
+ minWidth: "220px",
23
+ boxSizing: "border-box",
24
+ borderRadius: tokens.borderRadiusLarge,
25
+ border: `1px solid ${tokens.colorNeutralStroke2}`,
26
+ filter: "none",
27
+ boxShadow: tokens.shadow8,
28
+ background: tokens.colorNeutralBackground1,
29
+ fontSize: "13px",
30
+ },
31
+ header: {
32
+ display: "flex",
33
+ flexDirection: "column",
34
+ padding: "8px 8px 10px",
35
+ minWidth: 0,
36
+ },
37
+ subtitle: { color: tokens.colorNeutralForeground3 },
38
+ divider: {
39
+ height: "1px",
40
+ margin: "4px -4px",
41
+ background: tokens.colorNeutralStroke2,
42
+ },
43
+ row: {
44
+ display: "flex",
45
+ alignItems: "center",
46
+ justifyContent: "space-between",
47
+ gap: "12px",
48
+ minHeight: "36px",
49
+ padding: "0 4px 0 8px",
50
+ color: tokens.colorNeutralForeground2,
51
+ },
52
+ button: {
53
+ display: "flex",
54
+ alignItems: "center",
55
+ justifyContent: "space-between",
56
+ gap: "12px",
57
+ width: "100%",
58
+ minHeight: "34px",
59
+ padding: "0 8px",
60
+ border: "none",
61
+ borderRadius: tokens.borderRadiusMedium,
62
+ background: "transparent",
63
+ color: tokens.colorNeutralForeground2,
64
+ fontFamily: "inherit",
65
+ fontSize: "13px",
66
+ textAlign: "start",
67
+ cursor: "pointer",
68
+ "& svg": { fontSize: "16px", flexShrink: 0, color: tokens.colorNeutralForeground3 },
69
+ ":hover": {
70
+ background: tokens.colorNeutralBackground3,
71
+ color: tokens.colorNeutralForeground1,
72
+ },
73
+ ":focus-visible": {
74
+ outline: `2px solid ${tokens.colorStrokeFocus2}`,
75
+ outlineOffset: "-2px",
76
+ },
77
+ ":disabled": { cursor: "default", opacity: 0.6 },
78
+ ":disabled:hover": { background: "transparent", color: tokens.colorNeutralForeground2 },
79
+ },
80
+ });
81
+
82
+ /** The panel, in place of Fluent's `PopoverSurface` inside a `Popover`. It
83
+ * takes `PopoverSurface`'s props. */
84
+ export function MenuPanel({ className, ...rest }: PopoverSurfaceProps) {
85
+ const styles = useStyles();
86
+ return <PopoverSurface {...rest} className={mergeClasses(styles.panel, className)} />;
87
+ }
88
+
89
+ /** The panel's top, such as the person's name and email. */
90
+ export function MenuPanelHeader({
91
+ title,
92
+ subtitle,
93
+ }: {
94
+ /** In semibold, such as the person's name. */
95
+ title: ReactNode;
96
+ /** A line under it, such as their email. */
97
+ subtitle?: ReactNode;
98
+ }) {
99
+ const styles = useStyles();
100
+ return (
101
+ <div className={styles.header}>
102
+ <Text weight="semibold" size={200} truncate wrap={false}>
103
+ {title}
104
+ </Text>
105
+ {subtitle != null && (
106
+ <Text size={200} truncate wrap={false} className={styles.subtitle}>
107
+ {subtitle}
108
+ </Text>
109
+ )}
110
+ </div>
111
+ );
112
+ }
113
+
114
+ /** A line across the panel, between groups of rows. */
115
+ export function MenuPanelDivider() {
116
+ const styles = useStyles();
117
+ return <div role="separator" className={styles.divider} />;
118
+ }
119
+
120
+ /** A label with a control at the end, such as the theme switch. */
121
+ export function MenuPanelRow({
122
+ label,
123
+ children,
124
+ }: {
125
+ /** What the control sets, such as "Theme". */
126
+ label: ReactNode;
127
+ /** The control, named on its own, as ThemeSwitch's and Segmented's `label`
128
+ * name them: the row's label is only shown, not tied to it. */
129
+ children: ReactNode;
130
+ }) {
131
+ const styles = useStyles();
132
+ return (
133
+ <div className={styles.row}>
134
+ <span>{label}</span>
135
+ {children}
136
+ </div>
137
+ );
138
+ }
139
+
140
+ /** A row that does something when clicked, such as signing out. */
141
+ export function MenuPanelButton({
142
+ icon,
143
+ onClick,
144
+ disabled,
145
+ children,
146
+ }: {
147
+ /** At the end of the row. */
148
+ icon?: ReactElement;
149
+ /** What the row does when clicked. */
150
+ onClick: MouseEventHandler<HTMLButtonElement>;
151
+ /** Greys the row out and ignores clicks, such as while signing out. */
152
+ disabled?: boolean;
153
+ /** The row's text. */
154
+ children: ReactNode;
155
+ }) {
156
+ const styles = useStyles();
157
+ return (
158
+ <button type="button" className={styles.button} onClick={onClick} disabled={disabled}>
159
+ <span>{children}</span>
160
+ {icon}
161
+ </button>
162
+ );
163
+ }
@@ -0,0 +1,208 @@
1
+ // "On this page": the page's headings beside it, with the one being read
2
+ // marked as the page scrolls. A click scrolls to the heading and puts it in
3
+ // the URL's hash, without going through the router (which would race the
4
+ // scroll). To keep it in view as the page scrolls, give it a className that
5
+ // makes it sticky and scroll on its own; it then keeps the marked heading in
6
+ // sight within itself.
7
+
8
+ import { Text, makeStyles, mergeClasses, tokens } from "@fluentui/react-components";
9
+ import {
10
+ useCallback,
11
+ useEffect,
12
+ useMemo,
13
+ useRef,
14
+ useState,
15
+ type MouseEvent,
16
+ type ReactNode,
17
+ } from "react";
18
+ import { useUiStrings } from "../provider/context.js";
19
+ import { TOP_BAR_HEIGHT } from "./TopBar.js";
20
+
21
+ const useStyles = makeStyles({
22
+ root: { fontSize: "13px", boxSizing: "border-box" },
23
+ heading: {
24
+ display: "block",
25
+ margin: 0,
26
+ padding: "0 0 8px 12px",
27
+ fontSize: "12px",
28
+ fontWeight: tokens.fontWeightRegular,
29
+ lineHeight: "16px",
30
+ color: tokens.colorNeutralForeground3,
31
+ },
32
+ list: {
33
+ margin: 0,
34
+ padding: 0,
35
+ listStyle: "none",
36
+ borderLeft: `1px solid ${tokens.colorNeutralStroke2}`,
37
+ },
38
+ link: {
39
+ display: "block",
40
+ marginLeft: "-1px",
41
+ padding: "4px 12px",
42
+ lineHeight: "18px",
43
+ borderLeft: "2px solid transparent",
44
+ color: tokens.colorNeutralForeground3,
45
+ textDecoration: "none",
46
+ cursor: "pointer",
47
+ ":hover": { color: tokens.colorNeutralForeground1 },
48
+ "& code": {
49
+ fontFamily: tokens.fontFamilyMonospace,
50
+ fontSize: "0.92em",
51
+ backgroundColor: tokens.colorNeutralBackground3,
52
+ padding: "1px 5px",
53
+ borderRadius: tokens.borderRadiusSmall,
54
+ },
55
+ },
56
+ active: {
57
+ borderLeftColor: tokens.colorNeutralForeground1,
58
+ color: tokens.colorNeutralForeground1,
59
+ fontWeight: tokens.fontWeightMedium,
60
+ },
61
+ });
62
+
63
+ export interface OutlineItem {
64
+ /** The heading's element id. */
65
+ id: string;
66
+ /** The heading's text. */
67
+ label: ReactNode;
68
+ /** The heading's level: 2 for an h2. Deeper levels are indented. */
69
+ depth: number;
70
+ /** The headings under this one, listed after it. */
71
+ children?: OutlineItem[];
72
+ }
73
+
74
+ export interface OutlineProps {
75
+ /** The page's headings, in order: `{ id, label, depth, children? }`. */
76
+ items: OutlineItem[];
77
+ /** The heading above the list. Defaults to the library's "On this page". */
78
+ label?: string;
79
+ /** The headings' language, when it isn't the page's. */
80
+ lang?: string;
81
+ /** To keep it in view, make it sticky and let it scroll on its own here. */
82
+ className?: string;
83
+ }
84
+
85
+ export function Outline({ items, label, lang, className }: OutlineProps) {
86
+ const styles = useStyles();
87
+ const strings = useUiStrings();
88
+ const rootRef = useRef<HTMLElement>(null);
89
+ const links = useRef(new Map<string, HTMLAnchorElement>());
90
+ const flat = useMemo(() => flatten(items), [items]);
91
+ const ids = useMemo(() => flat.map((item) => item.id), [flat]);
92
+ const top = Math.min(...flat.map((item) => item.depth));
93
+ const [active, snap] = useActiveHeading(ids);
94
+ const name = label ?? strings.onThisPage;
95
+
96
+ // Keep the marked heading in sight, when the outline scrolls on its own.
97
+ useEffect(() => {
98
+ const link = active ? links.current.get(active) : undefined;
99
+ const root = rootRef.current;
100
+ if (!link || !root) return;
101
+ const at = link.getBoundingClientRect();
102
+ const box = root.getBoundingClientRect();
103
+ if (at.top < box.top + 24 || at.bottom > box.bottom - 24) {
104
+ link.scrollIntoView({ block: "nearest", behavior: "smooth" });
105
+ }
106
+ }, [active]);
107
+
108
+ const go = useCallback(
109
+ (id: string) => (e: MouseEvent<HTMLAnchorElement>) => {
110
+ e.preventDefault();
111
+ snap(id);
112
+ document.getElementById(id)?.scrollIntoView({ behavior: "smooth", block: "start" });
113
+ const url = new URL(window.location.href);
114
+ url.hash = id;
115
+ window.history.replaceState(window.history.state, "", url);
116
+ },
117
+ [snap],
118
+ );
119
+
120
+ // A page without headings keeps the outline's place in the layout, empty.
121
+ if (!flat.length) return <div className={mergeClasses(styles.root, className)} />;
122
+
123
+ return (
124
+ <nav ref={rootRef} aria-label={name} className={mergeClasses(styles.root, className)}>
125
+ <Text as="h2" className={styles.heading}>
126
+ {name}
127
+ </Text>
128
+ <ul className={styles.list} lang={lang}>
129
+ {flat.map((item) => (
130
+ <li key={item.id} style={{ paddingLeft: `${(item.depth - top) * 12}px` }}>
131
+ <a
132
+ href={`#${item.id}`}
133
+ ref={(el) => {
134
+ if (el) links.current.set(item.id, el);
135
+ else links.current.delete(item.id);
136
+ }}
137
+ aria-current={active === item.id ? "location" : undefined}
138
+ className={mergeClasses(styles.link, active === item.id && styles.active)}
139
+ onClick={go(item.id)}
140
+ >
141
+ {item.label}
142
+ </a>
143
+ </li>
144
+ ))}
145
+ </ul>
146
+ </nav>
147
+ );
148
+ }
149
+
150
+ function flatten(items: OutlineItem[]): OutlineItem[] {
151
+ return items.flatMap((item) => [item, ...flatten(item.children ?? [])]);
152
+ }
153
+
154
+ // The heading being read: the topmost one in the upper part of the window,
155
+ // or, between headings, the last one above it. A click marks its heading at
156
+ // once and holds the mark while the smooth scroll runs, which would
157
+ // otherwise move it through the headings in between.
158
+ function useActiveHeading(ids: string[]): [string | null, (id: string) => void] {
159
+ const key = ids.join("\n");
160
+ // The mark belongs to one page's headings; on another page, it's their first.
161
+ const [mark, setMark] = useState<{ key: string; id: string | null }>({
162
+ key,
163
+ id: ids[0] ?? null,
164
+ });
165
+ const active = mark.key === key ? mark.id : (ids[0] ?? null);
166
+ const holdUntil = useRef(0);
167
+
168
+ useEffect(() => {
169
+ const headings = key
170
+ .split("\n")
171
+ .map((id) => document.getElementById(id))
172
+ .filter((el): el is HTMLElement => !!el);
173
+ if (!headings.length) return;
174
+ const visible = new Map<string, IntersectionObserverEntry>();
175
+ const observer = new IntersectionObserver(
176
+ (entries) => {
177
+ if (Date.now() < holdUntil.current) return;
178
+ for (const entry of entries) {
179
+ if (entry.isIntersecting) visible.set(entry.target.id, entry);
180
+ else visible.delete(entry.target.id);
181
+ }
182
+ let id: string | undefined;
183
+ if (visible.size) {
184
+ id = [...visible.values()].sort(
185
+ (a, b) => a.boundingClientRect.top - b.boundingClientRect.top,
186
+ )[0]!.target.id;
187
+ } else {
188
+ for (const h of headings) if (h.getBoundingClientRect().top < 100) id = h.id;
189
+ }
190
+ if (id) setMark({ key, id });
191
+ },
192
+ // Below the top bar, down to a little under half the window.
193
+ { rootMargin: `-${TOP_BAR_HEIGHT + 12}px 0px -55% 0px`, threshold: [0, 1] },
194
+ );
195
+ for (const h of headings) observer.observe(h);
196
+ return () => observer.disconnect();
197
+ }, [key]);
198
+
199
+ const snap = useCallback(
200
+ (id: string) => {
201
+ setMark({ key, id });
202
+ holdUntil.current = Date.now() + 700;
203
+ },
204
+ [key],
205
+ );
206
+
207
+ return [active, snap];
208
+ }