@siiway/ui-react 0.0.0-stage → 0.1.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 (233) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +20 -2
  3. package/dist/components/BrandMark.d.ts +11 -0
  4. package/dist/components/BrandMark.d.ts.map +1 -0
  5. package/dist/components/BrandMark.js +11 -0
  6. package/dist/components/BrandMark.js.map +1 -0
  7. package/dist/components/CheckboxField.d.ts +15 -0
  8. package/dist/components/CheckboxField.d.ts.map +1 -0
  9. package/dist/components/CheckboxField.js +23 -0
  10. package/dist/components/CheckboxField.js.map +1 -0
  11. package/dist/components/ColorPickerInput.d.ts +17 -0
  12. package/dist/components/ColorPickerInput.d.ts.map +1 -0
  13. package/dist/components/ColorPickerInput.js +97 -0
  14. package/dist/components/ColorPickerInput.js.map +1 -0
  15. package/dist/components/CommandPalette.d.ts +53 -0
  16. package/dist/components/CommandPalette.d.ts.map +1 -0
  17. package/dist/components/CommandPalette.js +239 -0
  18. package/dist/components/CommandPalette.js.map +1 -0
  19. package/dist/components/CopyButton.d.ts +8 -0
  20. package/dist/components/CopyButton.d.ts.map +1 -0
  21. package/dist/components/CopyButton.js +17 -0
  22. package/dist/components/CopyButton.js.map +1 -0
  23. package/dist/components/CopyIdTrigger.d.ts +31 -0
  24. package/dist/components/CopyIdTrigger.d.ts.map +1 -0
  25. package/dist/components/CopyIdTrigger.js +72 -0
  26. package/dist/components/CopyIdTrigger.js.map +1 -0
  27. package/dist/components/DataList.d.ts +78 -0
  28. package/dist/components/DataList.d.ts.map +1 -0
  29. package/dist/components/DataList.js +150 -0
  30. package/dist/components/DataList.js.map +1 -0
  31. package/dist/components/DetailHeader.d.ts +22 -0
  32. package/dist/components/DetailHeader.d.ts.map +1 -0
  33. package/dist/components/DetailHeader.js +48 -0
  34. package/dist/components/DetailHeader.js.map +1 -0
  35. package/dist/components/DurationInput.d.ts +25 -0
  36. package/dist/components/DurationInput.d.ts.map +1 -0
  37. package/dist/components/DurationInput.js +83 -0
  38. package/dist/components/DurationInput.js.map +1 -0
  39. package/dist/components/EmptyState.d.ts +14 -0
  40. package/dist/components/EmptyState.d.ts.map +1 -0
  41. package/dist/components/EmptyState.js +41 -0
  42. package/dist/components/EmptyState.js.map +1 -0
  43. package/dist/components/FormPage.d.ts +22 -0
  44. package/dist/components/FormPage.d.ts.map +1 -0
  45. package/dist/components/FormPage.js +65 -0
  46. package/dist/components/FormPage.js.map +1 -0
  47. package/dist/components/GetStartedPanel.d.ts +16 -0
  48. package/dist/components/GetStartedPanel.d.ts.map +1 -0
  49. package/dist/components/GetStartedPanel.js +96 -0
  50. package/dist/components/GetStartedPanel.js.map +1 -0
  51. package/dist/components/LabeledLine.d.ts +12 -0
  52. package/dist/components/LabeledLine.d.ts.map +1 -0
  53. package/dist/components/LabeledLine.js +6 -0
  54. package/dist/components/LabeledLine.js.map +1 -0
  55. package/dist/components/ListSearch.d.ts +15 -0
  56. package/dist/components/ListSearch.d.ts.map +1 -0
  57. package/dist/components/ListSearch.js +23 -0
  58. package/dist/components/ListSearch.js.map +1 -0
  59. package/dist/components/LoadingBar.d.ts +8 -0
  60. package/dist/components/LoadingBar.d.ts.map +1 -0
  61. package/dist/components/LoadingBar.js +55 -0
  62. package/dist/components/LoadingBar.js.map +1 -0
  63. package/dist/components/Outline.d.ts +23 -0
  64. package/dist/components/Outline.d.ts.map +1 -0
  65. package/dist/components/Outline.js +150 -0
  66. package/dist/components/Outline.js.map +1 -0
  67. package/dist/components/Overview.d.ts +103 -0
  68. package/dist/components/Overview.d.ts.map +1 -0
  69. package/dist/components/Overview.js +286 -0
  70. package/dist/components/Overview.js.map +1 -0
  71. package/dist/components/PageHeader.d.ts +19 -0
  72. package/dist/components/PageHeader.d.ts.map +1 -0
  73. package/dist/components/PageHeader.js +50 -0
  74. package/dist/components/PageHeader.js.map +1 -0
  75. package/dist/components/Pagination.d.ts +19 -0
  76. package/dist/components/Pagination.d.ts.map +1 -0
  77. package/dist/components/Pagination.js +47 -0
  78. package/dist/components/Pagination.js.map +1 -0
  79. package/dist/components/PasswordInput.d.ts +3 -0
  80. package/dist/components/PasswordInput.d.ts.map +1 -0
  81. package/dist/components/PasswordInput.js +16 -0
  82. package/dist/components/PasswordInput.js.map +1 -0
  83. package/dist/components/PrevNext.d.ts +16 -0
  84. package/dist/components/PrevNext.d.ts.map +1 -0
  85. package/dist/components/PrevNext.js +61 -0
  86. package/dist/components/PrevNext.js.map +1 -0
  87. package/dist/components/SearchButton.d.ts +16 -0
  88. package/dist/components/SearchButton.d.ts.map +1 -0
  89. package/dist/components/SearchButton.js +63 -0
  90. package/dist/components/SearchButton.js.map +1 -0
  91. package/dist/components/SearchField.d.ts +10 -0
  92. package/dist/components/SearchField.d.ts.map +1 -0
  93. package/dist/components/SearchField.js +67 -0
  94. package/dist/components/SearchField.js.map +1 -0
  95. package/dist/components/Segmented.d.ts +28 -0
  96. package/dist/components/Segmented.d.ts.map +1 -0
  97. package/dist/components/Segmented.js +74 -0
  98. package/dist/components/Segmented.js.map +1 -0
  99. package/dist/components/Select.d.ts +22 -0
  100. package/dist/components/Select.d.ts.map +1 -0
  101. package/dist/components/Select.js +50 -0
  102. package/dist/components/Select.js.map +1 -0
  103. package/dist/components/Settings.d.ts +103 -0
  104. package/dist/components/Settings.d.ts.map +1 -0
  105. package/dist/components/Settings.js +246 -0
  106. package/dist/components/Settings.js.map +1 -0
  107. package/dist/components/SideNav.d.ts +34 -0
  108. package/dist/components/SideNav.d.ts.map +1 -0
  109. package/dist/components/SideNav.js +99 -0
  110. package/dist/components/SideNav.js.map +1 -0
  111. package/dist/components/Skeletons.d.ts +37 -0
  112. package/dist/components/Skeletons.d.ts.map +1 -0
  113. package/dist/components/Skeletons.js +109 -0
  114. package/dist/components/Skeletons.js.map +1 -0
  115. package/dist/components/StatusPage.d.ts +37 -0
  116. package/dist/components/StatusPage.d.ts.map +1 -0
  117. package/dist/components/StatusPage.js +106 -0
  118. package/dist/components/StatusPage.js.map +1 -0
  119. package/dist/components/ThemeMenu.d.ts +11 -0
  120. package/dist/components/ThemeMenu.d.ts.map +1 -0
  121. package/dist/components/ThemeMenu.js +42 -0
  122. package/dist/components/ThemeMenu.js.map +1 -0
  123. package/dist/components/ThemeSwitch.d.ts +14 -0
  124. package/dist/components/ThemeSwitch.d.ts.map +1 -0
  125. package/dist/components/ThemeSwitch.js +17 -0
  126. package/dist/components/ThemeSwitch.js.map +1 -0
  127. package/dist/components/TopBar.d.ts +45 -0
  128. package/dist/components/TopBar.d.ts.map +1 -0
  129. package/dist/components/TopBar.js +145 -0
  130. package/dist/components/TopBar.js.map +1 -0
  131. package/dist/components/ViewBanner.d.ts +17 -0
  132. package/dist/components/ViewBanner.d.ts.map +1 -0
  133. package/dist/components/ViewBanner.js +14 -0
  134. package/dist/components/ViewBanner.js.map +1 -0
  135. package/dist/hooks/platform.d.ts +7 -0
  136. package/dist/hooks/platform.d.ts.map +1 -0
  137. package/dist/hooks/platform.js +19 -0
  138. package/dist/hooks/platform.js.map +1 -0
  139. package/dist/hooks/useConfirm.d.ts +23 -0
  140. package/dist/hooks/useConfirm.d.ts.map +1 -0
  141. package/dist/hooks/useConfirm.js +55 -0
  142. package/dist/hooks/useConfirm.js.map +1 -0
  143. package/dist/hooks/useCopy.d.ts +9 -0
  144. package/dist/hooks/useCopy.d.ts.map +1 -0
  145. package/dist/hooks/useCopy.js +14 -0
  146. package/dist/hooks/useCopy.js.map +1 -0
  147. package/dist/hooks/useDebouncedSearch.d.ts +8 -0
  148. package/dist/hooks/useDebouncedSearch.d.ts.map +1 -0
  149. package/dist/hooks/useDebouncedSearch.js +19 -0
  150. package/dist/hooks/useDebouncedSearch.js.map +1 -0
  151. package/dist/hooks/usePageClamp.d.ts +4 -0
  152. package/dist/hooks/usePageClamp.d.ts.map +1 -0
  153. package/dist/hooks/usePageClamp.js +17 -0
  154. package/dist/hooks/usePageClamp.js.map +1 -0
  155. package/dist/hooks/useSearchShortcut.d.ts +8 -0
  156. package/dist/hooks/useSearchShortcut.d.ts.map +1 -0
  157. package/dist/hooks/useSearchShortcut.js +38 -0
  158. package/dist/hooks/useSearchShortcut.js.map +1 -0
  159. package/dist/hooks/useThemeMode.d.ts +17 -0
  160. package/dist/hooks/useThemeMode.d.ts.map +1 -0
  161. package/dist/hooks/useThemeMode.js +62 -0
  162. package/dist/hooks/useThemeMode.js.map +1 -0
  163. package/dist/hooks/useToastMessage.d.ts +22 -0
  164. package/dist/hooks/useToastMessage.d.ts.map +1 -0
  165. package/dist/hooks/useToastMessage.js +26 -0
  166. package/dist/hooks/useToastMessage.js.map +1 -0
  167. package/dist/index.d.ts +45 -0
  168. package/dist/index.d.ts.map +1 -0
  169. package/dist/index.js +55 -0
  170. package/dist/index.js.map +1 -0
  171. package/dist/provider/UiProvider.d.ts +37 -0
  172. package/dist/provider/UiProvider.d.ts.map +1 -0
  173. package/dist/provider/UiProvider.js +42 -0
  174. package/dist/provider/UiProvider.js.map +1 -0
  175. package/dist/provider/canvas.d.ts +2 -0
  176. package/dist/provider/canvas.d.ts.map +1 -0
  177. package/dist/provider/canvas.js +15 -0
  178. package/dist/provider/canvas.js.map +1 -0
  179. package/dist/provider/context.d.ts +45 -0
  180. package/dist/provider/context.d.ts.map +1 -0
  181. package/dist/provider/context.js +39 -0
  182. package/dist/provider/context.js.map +1 -0
  183. package/dist/theme/createTheme.d.ts +13 -0
  184. package/dist/theme/createTheme.d.ts.map +1 -0
  185. package/dist/theme/createTheme.js +18 -0
  186. package/dist/theme/createTheme.js.map +1 -0
  187. package/package.json +49 -4
  188. package/src/components/BrandMark.tsx +28 -0
  189. package/src/components/CheckboxField.tsx +50 -0
  190. package/src/components/ColorPickerInput.tsx +190 -0
  191. package/src/components/CommandPalette.tsx +437 -0
  192. package/src/components/CopyButton.tsx +48 -0
  193. package/src/components/CopyIdTrigger.tsx +127 -0
  194. package/src/components/DataList.tsx +332 -0
  195. package/src/components/DetailHeader.tsx +102 -0
  196. package/src/components/DurationInput.tsx +133 -0
  197. package/src/components/EmptyState.tsx +67 -0
  198. package/src/components/FormPage.tsx +109 -0
  199. package/src/components/GetStartedPanel.tsx +132 -0
  200. package/src/components/LabeledLine.tsx +28 -0
  201. package/src/components/ListSearch.tsx +64 -0
  202. package/src/components/LoadingBar.tsx +81 -0
  203. package/src/components/Outline.tsx +208 -0
  204. package/src/components/Overview.tsx +448 -0
  205. package/src/components/PageHeader.tsx +80 -0
  206. package/src/components/Pagination.tsx +105 -0
  207. package/src/components/PasswordInput.tsx +35 -0
  208. package/src/components/PrevNext.tsx +98 -0
  209. package/src/components/SearchButton.tsx +90 -0
  210. package/src/components/SearchField.tsx +97 -0
  211. package/src/components/Segmented.tsx +136 -0
  212. package/src/components/Select.tsx +110 -0
  213. package/src/components/Settings.tsx +432 -0
  214. package/src/components/SideNav.tsx +188 -0
  215. package/src/components/Skeletons.tsx +233 -0
  216. package/src/components/StatusPage.tsx +188 -0
  217. package/src/components/ThemeMenu.tsx +83 -0
  218. package/src/components/ThemeSwitch.tsx +39 -0
  219. package/src/components/TopBar.tsx +280 -0
  220. package/src/components/ViewBanner.tsx +51 -0
  221. package/src/hooks/platform.ts +22 -0
  222. package/src/hooks/useConfirm.tsx +97 -0
  223. package/src/hooks/useCopy.ts +20 -0
  224. package/src/hooks/useDebouncedSearch.ts +19 -0
  225. package/src/hooks/usePageClamp.ts +20 -0
  226. package/src/hooks/useSearchShortcut.ts +49 -0
  227. package/src/hooks/useThemeMode.ts +98 -0
  228. package/src/hooks/useToastMessage.ts +38 -0
  229. package/src/index.ts +140 -0
  230. package/src/provider/UiProvider.tsx +95 -0
  231. package/src/provider/canvas.ts +17 -0
  232. package/src/provider/context.tsx +85 -0
  233. package/src/theme/createTheme.ts +29 -0
@@ -0,0 +1,280 @@
1
+ // The strip across the top of a page, as in Vellum: the canvas with a
2
+ // hairline under it, the brand (and a crumb after it), links, then the app's
3
+ // actions (search, a ThemeMenu) at the end, all as compact 32px controls with
4
+ // a grey (not brand) selection.
5
+ //
6
+ // On a narrow screen it gives way in steps: the menu button that opens the
7
+ // app's navigation shows from 960px down, the links go at 720px, the crumb at
8
+ // 540px.
9
+
10
+ import {
11
+ Button,
12
+ Menu,
13
+ MenuItem,
14
+ MenuList,
15
+ MenuPopover,
16
+ MenuTrigger,
17
+ makeStyles,
18
+ mergeClasses,
19
+ tokens,
20
+ } from "@fluentui/react-components";
21
+ import { ChevronDown12Regular, Navigation20Regular } from "@fluentui/react-icons";
22
+ import type { ReactNode } from "react";
23
+ import { useCanvasClass } from "../provider/canvas.js";
24
+ import { UiLink, useUiNavigate, useUiStrings } from "../provider/context.js";
25
+ import { BrandMark } from "./BrandMark.js";
26
+
27
+ /** The top bar's height in pixels, for what sticks below it. */
28
+ export const TOP_BAR_HEIGHT = 52;
29
+
30
+ const useStyles = makeStyles({
31
+ root: {
32
+ display: "flex",
33
+ alignItems: "center",
34
+ gap: "8px",
35
+ height: `${TOP_BAR_HEIGHT}px`,
36
+ paddingInline: "16px 12px",
37
+ boxSizing: "border-box",
38
+ borderBottom: `1px solid ${tokens.colorNeutralStroke2}`,
39
+ "@media (max-width: 720px)": { gap: "4px", paddingInline: "8px" },
40
+ },
41
+ sticky: { position: "sticky", top: 0, zIndex: 100 },
42
+ brandGroup: {
43
+ display: "flex",
44
+ alignItems: "center",
45
+ gap: "8px",
46
+ minWidth: 0,
47
+ flexShrink: 1,
48
+ },
49
+ brand: {
50
+ display: "flex",
51
+ alignItems: "center",
52
+ gap: "10px",
53
+ minWidth: 0,
54
+ color: tokens.colorNeutralForeground1,
55
+ textDecoration: "none",
56
+ ":hover": { color: tokens.colorNeutralForeground1, textDecoration: "none" },
57
+ },
58
+ brandText: {
59
+ fontSize: tokens.fontSizeBase300,
60
+ fontWeight: tokens.fontWeightSemibold,
61
+ whiteSpace: "nowrap",
62
+ overflow: "hidden",
63
+ textOverflow: "ellipsis",
64
+ },
65
+ // Muted, like a path segment after the site's name.
66
+ crumb: {
67
+ gap: "8px",
68
+ color: tokens.colorNeutralForeground3,
69
+ ":hover": { color: tokens.colorNeutralForeground1 },
70
+ "@media (max-width: 540px)": { display: "none" },
71
+ },
72
+ crumbText: {
73
+ fontSize: tokens.fontSizeBase300,
74
+ whiteSpace: "nowrap",
75
+ overflow: "hidden",
76
+ textOverflow: "ellipsis",
77
+ },
78
+ separator: {
79
+ color: tokens.colorNeutralStroke1,
80
+ fontSize: tokens.fontSizeBase400,
81
+ userSelect: "none",
82
+ "@media (max-width: 540px)": { display: "none" },
83
+ },
84
+ // A wrapper, because Fluent's Button sets its own display at the same
85
+ // specificity as a class on it.
86
+ menuButton: {
87
+ display: "none",
88
+ "@media (max-width: 960px)": { display: "inline-flex" },
89
+ },
90
+ links: {
91
+ display: "flex",
92
+ alignItems: "center",
93
+ gap: "1px",
94
+ marginLeft: "12px",
95
+ "@media (max-width: 720px)": { display: "none" },
96
+ },
97
+ link: {
98
+ display: "inline-flex",
99
+ alignItems: "center",
100
+ gap: "4px",
101
+ minHeight: "32px",
102
+ paddingInline: "10px",
103
+ boxSizing: "border-box",
104
+ border: "none",
105
+ borderRadius: tokens.borderRadiusMedium,
106
+ background: "transparent",
107
+ color: tokens.colorNeutralForeground2,
108
+ textDecoration: "none",
109
+ fontFamily: "inherit",
110
+ fontSize: "13px",
111
+ fontWeight: tokens.fontWeightMedium,
112
+ whiteSpace: "nowrap",
113
+ cursor: "pointer",
114
+ ":hover": {
115
+ backgroundColor: tokens.colorNeutralBackground3,
116
+ color: tokens.colorNeutralForeground1,
117
+ textDecoration: "none",
118
+ },
119
+ ":focus-visible": {
120
+ outline: `2px solid ${tokens.colorStrokeFocus2}`,
121
+ outlineOffset: "-2px",
122
+ },
123
+ },
124
+ linkActive: {
125
+ backgroundColor: tokens.colorNeutralBackground3,
126
+ color: tokens.colorNeutralForeground1,
127
+ },
128
+ chevron: { color: tokens.colorNeutralForeground3 },
129
+ spacer: { flex: 1 },
130
+ actions: { display: "flex", alignItems: "center", gap: "2px" },
131
+ });
132
+
133
+ export interface TopBarBrand {
134
+ /** The site's (or, for a crumb, the section's) name. */
135
+ label: string;
136
+ /** Where it leads: the home page, the section's first page. */
137
+ to: string;
138
+ /** An icon URL, drawn as a BrandMark before the name. */
139
+ logo?: string;
140
+ }
141
+
142
+ export interface TopBarLink {
143
+ /** The link's text. */
144
+ label: ReactNode;
145
+ /** An in-app path, or a full URL, which opens in a new tab. */
146
+ to?: string;
147
+ /** The reader is in this part of the site. A menu is active when one of its items is. */
148
+ active?: boolean;
149
+ /** Makes the link a menu of these. */
150
+ items?: TopBarLink[];
151
+ }
152
+
153
+ export interface TopBarProps {
154
+ /** `{ label, to, logo? }`: the site's name, where it leads, an icon URL. */
155
+ brand: TopBarBrand;
156
+ /** A second step after the brand, such as the section or project. Hidden from 540px down. */
157
+ crumb?: TopBarBrand;
158
+ /**
159
+ * `{ label, to?, active?, items? }`. A full URL opens in a new tab; `items`
160
+ * makes a menu, active when one of its items is. Hidden from 720px down.
161
+ */
162
+ links?: TopBarLink[];
163
+ /** Shows a menu button from 960px down that calls this, to open the app's navigation. */
164
+ onMenu?: () => void;
165
+ /** The menu button's name. Defaults to the library's "Open navigation". */
166
+ menuLabel?: string;
167
+ /** Names the links for screen readers. Defaults to the library's "Primary". */
168
+ linksLabel?: string;
169
+ /** Keeps it at the top of the window as the page scrolls. */
170
+ sticky?: boolean;
171
+ /** The actions at the end: a SearchButton, a ThemeMenu, icon buttons. */
172
+ children?: ReactNode;
173
+ className?: string;
174
+ }
175
+
176
+ const EXTERNAL = /^[a-z]+:\/\//i;
177
+
178
+ export function TopBar({
179
+ brand,
180
+ crumb,
181
+ links,
182
+ onMenu,
183
+ menuLabel,
184
+ linksLabel,
185
+ sticky,
186
+ children,
187
+ className,
188
+ }: TopBarProps) {
189
+ const styles = useStyles();
190
+ const canvas = useCanvasClass();
191
+ const strings = useUiStrings();
192
+ return (
193
+ <header className={mergeClasses(styles.root, canvas, sticky && styles.sticky, className)}>
194
+ <div className={styles.brandGroup}>
195
+ <UiLink to={brand.to} className={styles.brand}>
196
+ <BrandMark iconUrl={brand.logo} size={24} />
197
+ <span className={styles.brandText}>{brand.label}</span>
198
+ </UiLink>
199
+ {crumb && (
200
+ <>
201
+ <span className={styles.separator} aria-hidden="true">
202
+ /
203
+ </span>
204
+ <UiLink to={crumb.to} className={mergeClasses(styles.brand, styles.crumb)}>
205
+ <BrandMark iconUrl={crumb.logo} size={20} />
206
+ <span className={styles.crumbText}>{crumb.label}</span>
207
+ </UiLink>
208
+ </>
209
+ )}
210
+ </div>
211
+ {links && links.length > 0 && (
212
+ <nav className={styles.links} aria-label={linksLabel ?? strings.primaryNavigation}>
213
+ {links.map((link, i) => (
214
+ <BarLink key={i} link={link} styles={styles} />
215
+ ))}
216
+ </nav>
217
+ )}
218
+ <div className={styles.spacer} />
219
+ <div className={styles.actions}>
220
+ {onMenu && (
221
+ <span className={styles.menuButton}>
222
+ <Button
223
+ appearance="subtle"
224
+ icon={<Navigation20Regular />}
225
+ aria-label={menuLabel ?? strings.openNavigation}
226
+ onClick={onMenu}
227
+ />
228
+ </span>
229
+ )}
230
+ {children}
231
+ </div>
232
+ </header>
233
+ );
234
+ }
235
+
236
+ function BarLink({ link, styles }: { link: TopBarLink; styles: ReturnType<typeof useStyles> }) {
237
+ const navigate = useUiNavigate();
238
+ if (link.items) {
239
+ const active = link.active ?? link.items.some((item) => item.active);
240
+ return (
241
+ <Menu>
242
+ <MenuTrigger disableButtonEnhancement>
243
+ <button type="button" className={mergeClasses(styles.link, active && styles.linkActive)}>
244
+ {link.label}
245
+ <ChevronDown12Regular className={styles.chevron} />
246
+ </button>
247
+ </MenuTrigger>
248
+ <MenuPopover>
249
+ <MenuList>
250
+ {link.items.map((item, i) => (
251
+ <MenuItem
252
+ key={i}
253
+ onClick={() => {
254
+ if (!item.to) return;
255
+ if (EXTERNAL.test(item.to)) window.open(item.to, "_blank", "noopener,noreferrer");
256
+ else navigate(item.to);
257
+ }}
258
+ >
259
+ {item.label}
260
+ </MenuItem>
261
+ ))}
262
+ </MenuList>
263
+ </MenuPopover>
264
+ </Menu>
265
+ );
266
+ }
267
+ const className = mergeClasses(styles.link, link.active && styles.linkActive);
268
+ if (link.to && EXTERNAL.test(link.to)) {
269
+ return (
270
+ <a href={link.to} target="_blank" rel="noopener noreferrer" className={className}>
271
+ {link.label}
272
+ </a>
273
+ );
274
+ }
275
+ return (
276
+ <UiLink to={link.to ?? "#"} className={className}>
277
+ {link.label}
278
+ </UiLink>
279
+ );
280
+ }
@@ -0,0 +1,51 @@
1
+ // The banner on app and team pages that says which authority a site admin is
2
+ // acting with (the site override or their own membership), with the button
3
+ // that switches. Built on MessageBar's own layout, so a long message moves
4
+ // the action under the text and the dismiss stays in the corner rather than
5
+ // wrapping onto a line of its own.
6
+
7
+ import { Button, MessageBar, MessageBarActions, MessageBarBody } from "@fluentui/react-components";
8
+ import { DismissRegular } from "@fluentui/react-icons";
9
+ import type { ReactNode } from "react";
10
+ import { useUiStrings } from "../provider/context.js";
11
+
12
+ interface Props {
13
+ /** The banner's colour and icon. */
14
+ intent: "info" | "warning";
15
+ /** The message. */
16
+ children: ReactNode;
17
+ /** A button at the end: the switch, e.g. "Switch to normal view". */
18
+ action?: { label: string; onClick: () => void };
19
+ /** Called by a close button, which shows only when this is set. */
20
+ onDismiss?: () => void;
21
+ }
22
+
23
+ export function ViewBanner({ intent, children, action, onDismiss }: Props) {
24
+ const strings = useUiStrings();
25
+ return (
26
+ <MessageBar intent={intent}>
27
+ <MessageBarBody>{children}</MessageBarBody>
28
+ {(action || onDismiss) && (
29
+ <MessageBarActions
30
+ containerAction={
31
+ onDismiss && (
32
+ <Button
33
+ appearance="transparent"
34
+ size="small"
35
+ icon={<DismissRegular />}
36
+ aria-label={strings.close}
37
+ onClick={onDismiss}
38
+ />
39
+ )
40
+ }
41
+ >
42
+ {action && (
43
+ <Button size="small" onClick={action.onClick}>
44
+ {action.label}
45
+ </Button>
46
+ )}
47
+ </MessageBarActions>
48
+ )}
49
+ </MessageBar>
50
+ );
51
+ }
@@ -0,0 +1,22 @@
1
+ import { useSyncExternalStore } from "react";
2
+
3
+ function subscribe() {
4
+ return () => {};
5
+ }
6
+
7
+ function isApple() {
8
+ return /Mac|iPhone|iPad/.test(navigator.platform);
9
+ }
10
+
11
+ function serverIsApple() {
12
+ return false;
13
+ }
14
+
15
+ /**
16
+ * Whether the reader is on an Apple device, where shortcuts use ⌘ rather than
17
+ * Ctrl. The server can't tell, so it renders Ctrl, and the browser corrects
18
+ * it without a hydration mismatch.
19
+ */
20
+ export function useIsApple(): boolean {
21
+ return useSyncExternalStore(subscribe, isApple, serverIsApple);
22
+ }
@@ -0,0 +1,97 @@
1
+ // Promise-based confirmation dialog for one-click destructive actions:
2
+ //
3
+ // const { confirm, confirmDialog } = useConfirm();
4
+ // if (!(await confirm({ title, body, confirmLabel }))) return;
5
+ // ...
6
+ // return <>{...}{confirmDialog}</>;
7
+ //
8
+ // One dialog per page, reused for every action on it.
9
+
10
+ import {
11
+ Button,
12
+ Dialog,
13
+ DialogActions,
14
+ DialogBody,
15
+ DialogContent,
16
+ DialogSurface,
17
+ DialogTitle,
18
+ makeStyles,
19
+ tokens,
20
+ } from "@fluentui/react-components";
21
+ import { useCallback, useRef, useState, type ReactNode } from "react";
22
+ import { useUiStrings } from "../provider/context.js";
23
+
24
+ const useStyles = makeStyles({
25
+ danger: {
26
+ background: tokens.colorPaletteRedBackground3,
27
+ ":hover": { background: tokens.colorPaletteRedForeground1 },
28
+ ":hover:active": { background: tokens.colorPaletteRedForeground1 },
29
+ },
30
+ });
31
+
32
+ /** What `confirm` asks. */
33
+ export interface ConfirmOptions {
34
+ /** The question, as the dialog's title. */
35
+ title: string;
36
+ /** More detail under the title. */
37
+ body?: ReactNode;
38
+ /** Label of the confirming button, e.g. "Remove". */
39
+ confirmLabel: string;
40
+ }
41
+
42
+ /**
43
+ * Asks a yes-or-no question in a dialog and waits for the answer. Render
44
+ * `confirmDialog` once, anywhere in the component.
45
+ */
46
+ export function useConfirm() {
47
+ const styles = useStyles();
48
+ const strings = useUiStrings();
49
+ const [options, setOptions] = useState<ConfirmOptions | null>(null);
50
+ const resolver = useRef<((ok: boolean) => void) | null>(null);
51
+
52
+ const confirm = useCallback((next: ConfirmOptions) => {
53
+ // A second request while one is open answers the first with "no".
54
+ resolver.current?.(false);
55
+ setOptions(next);
56
+ return new Promise<boolean>((resolve) => {
57
+ resolver.current = resolve;
58
+ });
59
+ }, []);
60
+
61
+ const close = (ok: boolean) => {
62
+ resolver.current?.(ok);
63
+ resolver.current = null;
64
+ setOptions(null);
65
+ };
66
+
67
+ const confirmDialog = (
68
+ <Dialog
69
+ open={options !== null}
70
+ onOpenChange={(_, d) => {
71
+ if (!d.open) close(false);
72
+ }}
73
+ >
74
+ <DialogSurface>
75
+ <DialogBody>
76
+ <DialogTitle>{options?.title}</DialogTitle>
77
+ {options?.body && <DialogContent>{options.body}</DialogContent>}
78
+ <DialogActions>
79
+ <Button onClick={() => close(false)}>{strings.cancel}</Button>
80
+ <Button appearance="primary" className={styles.danger} onClick={() => close(true)}>
81
+ {options?.confirmLabel}
82
+ </Button>
83
+ </DialogActions>
84
+ </DialogBody>
85
+ </DialogSurface>
86
+ </Dialog>
87
+ );
88
+
89
+ return {
90
+ /** Opens the dialog. Resolves to `true` when confirmed and `false` when
91
+ * cancelled or closed. A second call while it's open answers the first
92
+ * with `false`. */
93
+ confirm,
94
+ /** The dialog. Render it once. */
95
+ confirmDialog,
96
+ };
97
+ }
@@ -0,0 +1,20 @@
1
+ import { useState } from "react";
2
+
3
+ export type CopyState = "idle" | "copied" | "failed";
4
+
5
+ /** Copy text to the clipboard and report how it went for a moment, so the
6
+ * control can say "Copied" — or "Copy failed" when clipboard access is
7
+ * denied (permission, insecure context), rather than claiming success. */
8
+ export function useCopy(resetMs = 1500) {
9
+ const [state, setState] = useState<CopyState>("idle");
10
+ const copy = (text: string) =>
11
+ navigator.clipboard.writeText(text).then(
12
+ () => flash("copied"),
13
+ () => flash("failed"),
14
+ );
15
+ function flash(next: CopyState) {
16
+ setState(next);
17
+ setTimeout(() => setState("idle"), resetMs);
18
+ }
19
+ return { state, copy };
20
+ }
@@ -0,0 +1,19 @@
1
+ import { useEffect, useState } from "react";
2
+
3
+ /** A search box value plus its debounced, trimmed copy; resets the page when
4
+ * the search settles. */
5
+ export function useDebouncedSearch(onSettle?: () => void, delay = 250) {
6
+ const [query, setQuery] = useState("");
7
+ const [search, setSearch] = useState("");
8
+ useEffect(() => {
9
+ const id = setTimeout(() => {
10
+ setSearch(query.trim());
11
+ onSettle?.();
12
+ }, delay);
13
+ return () => clearTimeout(id);
14
+ // onSettle is a setter-style callback; re-running on its identity would
15
+ // restart the timer every render.
16
+ // eslint-disable-next-line react-hooks/exhaustive-deps
17
+ }, [query, delay]);
18
+ return { query, setQuery, search };
19
+ }
@@ -0,0 +1,20 @@
1
+ // Keep a paged list on a page that exists. Removing the last row on the last
2
+ // page (or filtering elsewhere) can leave `page` past the end, where the
3
+ // list would show as empty with no pager to get back; this steps back to
4
+ // the last page that still has rows.
5
+
6
+ import { useEffect } from "react";
7
+
8
+ export function usePageClamp(
9
+ page: number,
10
+ /** Total rows from the latest response; undefined while loading. */
11
+ total: number | undefined,
12
+ pageSize: number,
13
+ setPage: (page: number) => void,
14
+ ) {
15
+ useEffect(() => {
16
+ if (total === undefined) return;
17
+ const last = Math.max(1, Math.ceil(total / pageSize));
18
+ if (page > last) setPage(last);
19
+ }, [page, total, pageSize, setPage]);
20
+ }
@@ -0,0 +1,49 @@
1
+ // The keys that open search, as Prism and Vellum both have them: Ctrl K (⌘K
2
+ // on Apple devices) toggles it from anywhere, and with `slash`, "/" opens it
3
+ // when the reader isn't typing.
4
+
5
+ import { useEffect, type Dispatch, type SetStateAction } from "react";
6
+
7
+ export interface SearchShortcutOptions {
8
+ /** Also open on "/", as docs sites do. Defaults to false. */
9
+ slash?: boolean;
10
+ }
11
+
12
+ function typing(target: EventTarget | null): boolean {
13
+ if (!(target instanceof HTMLElement)) return false;
14
+ return (
15
+ target.isContentEditable ||
16
+ target.tagName === "INPUT" ||
17
+ target.tagName === "TEXTAREA" ||
18
+ target.tagName === "SELECT"
19
+ );
20
+ }
21
+
22
+ /** Pass the state setter of the search's `open`, e.g. from `useState(false)`. */
23
+ export function useSearchShortcut(
24
+ setOpen: Dispatch<SetStateAction<boolean>>,
25
+ { slash = false }: SearchShortcutOptions = {},
26
+ ): void {
27
+ useEffect(() => {
28
+ function onKey(e: KeyboardEvent) {
29
+ // An input method's keys belong to the text it's composing.
30
+ if (e.isComposing) return;
31
+ if ((e.ctrlKey || e.metaKey) && !e.altKey && e.key.toLowerCase() === "k") {
32
+ e.preventDefault();
33
+ setOpen((open) => !open);
34
+ } else if (
35
+ slash &&
36
+ e.key === "/" &&
37
+ !e.ctrlKey &&
38
+ !e.metaKey &&
39
+ !e.altKey &&
40
+ !typing(e.target)
41
+ ) {
42
+ e.preventDefault();
43
+ setOpen(true);
44
+ }
45
+ }
46
+ window.addEventListener("keydown", onKey);
47
+ return () => window.removeEventListener("keydown", onKey);
48
+ }, [setOpen, slash]);
49
+ }
@@ -0,0 +1,98 @@
1
+ // The person's light/dark choice and the scheme it draws in now. The choice is
2
+ // kept in localStorage under the key schemeBootScript reads, and follows
3
+ // other tabs; `system` follows the OS setting as it changes.
4
+ //
5
+ // On the server, and while hydrating, it gives `defaultMode` drawn light: the
6
+ // server can't see the stored choice or the OS. The client then moves to the
7
+ // real scheme. useSyncExternalStore makes that switch without a hydration
8
+ // mismatch.
9
+
10
+ import {
11
+ THEME_STORAGE_KEY,
12
+ isThemeMode,
13
+ resolveScheme,
14
+ type Scheme,
15
+ type ThemeMode,
16
+ } from "@siiway/ui-core";
17
+ import { useCallback, useSyncExternalStore } from "react";
18
+
19
+ const DARK_QUERY = "(prefers-color-scheme: dark)";
20
+
21
+ // setMode in this tab notifies these; other tabs arrive as `storage` events.
22
+ const listeners = new Set<() => void>();
23
+ // Where the choice lives when the browser blocks localStorage.
24
+ const memory = new Map<string, string>();
25
+
26
+ function subscribeMode(onChange: () => void) {
27
+ listeners.add(onChange);
28
+ window.addEventListener("storage", onChange);
29
+ return () => {
30
+ listeners.delete(onChange);
31
+ window.removeEventListener("storage", onChange);
32
+ };
33
+ }
34
+
35
+ function subscribeSystem(onChange: () => void) {
36
+ const query = window.matchMedia?.(DARK_QUERY);
37
+ query?.addEventListener("change", onChange);
38
+ return () => query?.removeEventListener("change", onChange);
39
+ }
40
+
41
+ function systemDark() {
42
+ return window.matchMedia?.(DARK_QUERY).matches ?? false;
43
+ }
44
+
45
+ function readStored(key: string): string | null {
46
+ try {
47
+ return window.localStorage.getItem(key);
48
+ } catch {
49
+ return memory.get(key) ?? null;
50
+ }
51
+ }
52
+
53
+ function serverStored() {
54
+ return null;
55
+ }
56
+
57
+ function serverDark() {
58
+ return false;
59
+ }
60
+
61
+ export interface ThemeModeOptions {
62
+ /** The `localStorage` key; give `schemeBootScript` the same. Defaults to `"siiway-theme"`. */
63
+ storageKey?: string;
64
+ /** Until a choice is stored, and on the server. Defaults to `"system"`. */
65
+ defaultMode?: ThemeMode;
66
+ }
67
+
68
+ export interface ThemeModeState {
69
+ /** The choice, for `<ThemeSwitch value>`. */
70
+ mode: ThemeMode;
71
+ /** Stores a new choice, for `<ThemeSwitch onChange>`. */
72
+ setMode: (mode: ThemeMode) => void;
73
+ /** What to draw in now, for `<UiProvider scheme>`. */
74
+ scheme: Scheme;
75
+ }
76
+
77
+ export function useThemeMode({
78
+ storageKey = THEME_STORAGE_KEY,
79
+ defaultMode = "system",
80
+ }: ThemeModeOptions = {}): ThemeModeState {
81
+ const stored = useSyncExternalStore(subscribeMode, () => readStored(storageKey), serverStored);
82
+ const dark = useSyncExternalStore(subscribeSystem, systemDark, serverDark);
83
+ const mode = isThemeMode(stored) ? stored : defaultMode;
84
+
85
+ const setMode = useCallback(
86
+ (next: ThemeMode) => {
87
+ try {
88
+ window.localStorage.setItem(storageKey, next);
89
+ } catch {
90
+ memory.set(storageKey, next);
91
+ }
92
+ for (const listener of listeners) listener();
93
+ },
94
+ [storageKey],
95
+ );
96
+
97
+ return { mode, setMode, scheme: resolveScheme(mode, dark) };
98
+ }