@olenbetong/synergi-react 5.0.0 → 5.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/dist/styles/styles.css +1 -1
- package/dist/styles/styles.css.map +1 -1
- package/es/components/Checkbox/index.css +1 -1
- package/es/components/Checkbox/index.js +1 -9
- package/es/components/CodeEditor/CodeEditor.css +6 -6
- package/es/components/ColorCard/index.css +17 -28
- package/es/components/DateNavigator/index.css +5 -7
- package/es/components/FilePond.js +3 -12
- package/es/components/ImageEditor/ImageEditor.css +10 -10
- package/es/components/ImageEditor/ImageEditor.js +4 -4
- package/es/components/ImageEditor/ResponsiveButton.js +2 -2
- package/es/components/ImageEditor/plugins/cropPlugin/CropToolbar.js +1 -1
- package/es/components/ImageEditor/plugins/redactPlugin/RedactToolbar.js +1 -1
- package/es/components/ImageEditor/plugins/redactPlugin/useRedactTool.js +18 -14
- package/es/components/ImageEditor/useImageHistory.js +13 -1
- package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +3 -3
- package/es/components/MergeProgressDialog/useMergeJob.js +5 -4
- package/es/components/PageBanner/index.css +1 -1
- package/es/components/PdfViewer/PdfViewer.js +1 -1
- package/es/components/Portal/index.js +20 -20
- package/es/components/Spinner/index.css +1 -1
- package/es/components/SplitContainer/index.css +1 -1
- package/es/components/SplitContainer/index.js +3 -1
- package/es/components/Toolbar/Toolbar.css +137 -137
- package/es/components/Toolbar/index.js +6 -1
- package/es/components/ValueToggleGroup/index.css +2 -2
- package/es/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +7 -7
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +8 -0
- package/es/elements/ElementLookupDialog/HIDScanner/useHIDScanner.js +3 -1
- package/es/elements-ds/DescriptionInput.d.ts +16 -0
- package/es/elements-ds/DescriptionInput.js +41 -0
- package/es/elements-ds/DuplicateElementDamage.d.ts +40 -0
- package/es/elements-ds/DuplicateElementDamage.js +57 -0
- package/es/elements-ds/ElementLookup.d.ts +23 -0
- package/es/elements-ds/ElementLookup.js +35 -0
- package/es/elements-ds/ElementLookupDialog.d.ts +7 -0
- package/es/elements-ds/ElementLookupDialog.js +84 -0
- package/es/elements-ds/FormFields.d.ts +43 -0
- package/es/elements-ds/FormFields.js +67 -0
- package/es/elements-ds/HIDScanner.d.ts +7 -0
- package/es/elements-ds/HIDScanner.js +36 -0
- package/es/elements-ds/ManualForm.d.ts +7 -0
- package/es/elements-ds/ManualForm.js +70 -0
- package/es/elements-ds/ProjectSelect.d.ts +28 -0
- package/es/elements-ds/ProjectSelect.js +45 -0
- package/es/elements-ds/elements-ds.css +157 -0
- package/es/elements-ds/index.d.ts +12 -0
- package/es/elements-ds/index.js +14 -0
- package/es/index.css +1 -1
- package/es/useTranslation/index.js +5 -3
- package/package.json +16 -4
- package/src/chrome/AppShell/AppShell.test.tsx +48 -0
- package/src/chrome/AppShell/index.tsx +29 -29
- package/src/chrome/openReport.test.ts +50 -0
- package/src/chrome/openReport.ts +34 -34
- package/src/colorScheme.test.ts +188 -0
- package/src/colorScheme.ts +125 -125
- package/src/components/Checkbox/Checkbox.browser.test.tsx +116 -0
- package/src/components/Checkbox/index.scss +2 -2
- package/src/components/Checkbox/index.tsx +1 -10
- package/src/components/CodeEditor/CodeEditor.css +6 -6
- package/src/components/CodeEditor/CodeEditor.tsx +173 -173
- package/src/components/CodeEditor/index.ts +1 -1
- package/src/components/ColorCard/ColorCard.test.tsx +34 -0
- package/src/components/ColorCard/index.css +17 -28
- package/src/components/DateNavigator/DateNavigator.test.tsx +175 -0
- package/src/components/DateNavigator/index.css +5 -7
- package/src/components/DateNavigator/index.tsx +186 -186
- package/src/components/FilePond.browser.test.tsx +389 -0
- package/src/components/FilePond.tsx +303 -312
- package/src/components/ImageEditor/ImageEditor.browser.test.tsx +286 -0
- package/src/components/ImageEditor/ImageEditor.css +10 -10
- package/src/components/ImageEditor/ImageEditor.test.tsx +299 -0
- package/src/components/ImageEditor/ImageEditor.tsx +348 -348
- package/src/components/ImageEditor/ResponsiveButton.tsx +2 -2
- package/src/components/ImageEditor/index.ts +9 -9
- package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
- package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +42 -48
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.test.ts +225 -0
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -405
- package/src/components/ImageEditor/plugins/plugins.browser.test.ts +252 -0
- package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
- package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.test.ts +89 -0
- package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.ts +16 -13
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
- package/src/components/ImageEditor/plugins/zoomPlugin/useZoomPan.test.ts +154 -0
- package/src/components/ImageEditor/useActiveTool.ts +21 -21
- package/src/components/ImageEditor/useEdgeDetection.test.ts +96 -0
- package/src/components/ImageEditor/useEdgeDetection.ts +319 -319
- package/src/components/ImageEditor/useImageHistory.test.ts +409 -0
- package/src/components/ImageEditor/useImageHistory.ts +206 -197
- package/src/components/ImageEditor/useOpenCV.test.ts +208 -0
- package/src/components/ImageEditor/useOpenCV.ts +184 -184
- package/src/components/LinkedCardList/index.scss +95 -95
- package/src/components/MergeProgressDialog/MergeProgressDialogDs.browser.test.tsx +156 -0
- package/src/components/MergeProgressDialog/MergeProgressDialogDs.tsx +4 -3
- package/src/components/MergeProgressDialog/useMergeJob.test.ts +326 -0
- package/src/components/MergeProgressDialog/useMergeJob.ts +4 -3
- package/src/components/PageBanner/index.scss +10 -10
- package/src/components/PageBanner/index.tsx +80 -80
- package/src/components/PdfViewer/PdfViewer.browser.test.tsx +78 -0
- package/src/components/PdfViewer/PdfViewer.tsx +158 -158
- package/src/components/Portal/Portal.test.tsx +106 -0
- package/src/components/Portal/index.tsx +22 -25
- package/src/components/Sidebar/Sidebar.browser.test.tsx +57 -0
- package/src/components/Sidebar/Sidebar.test.tsx +123 -0
- package/src/components/Spinner/index.css +1 -1
- package/src/components/SplitContainer/SplitContainer.browser.test.tsx +304 -0
- package/src/components/SplitContainer/index.scss +5 -1
- package/src/components/SplitContainer/index.tsx +335 -333
- package/src/components/Toolbar/Toolbar.css +137 -137
- package/src/components/Toolbar/Toolbar.test.tsx +216 -0
- package/src/components/Toolbar/ToolbarBreadcrumbs.browser.test.tsx +137 -0
- package/src/components/Toolbar/index.tsx +270 -265
- package/src/components/ValueToggleGroup/ValueToggleGroup.test.tsx +112 -0
- package/src/components/ValueToggleGroup/index.css +2 -2
- package/src/designsystemet-web-types.d.ts +24 -24
- package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
- package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
- package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
- package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
- package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
- package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
- package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
- package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
- package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
- package/src/elements/ElementLookup.tsx +123 -123
- package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.browser.test.tsx +175 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +7 -7
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +8 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/engines.browser.test.ts +225 -0
- package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
- package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.test.ts +124 -0
- package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.ts +2 -1
- package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
- package/src/elements/ElementLookupDialog/getElement.test.ts +133 -0
- package/src/elements/ElementLookupDialog/index.tsx +226 -226
- package/src/elements/ElementLookupDialog/utils/Giai.test.ts +53 -0
- package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
- package/src/elements/ProjectSelect.tsx +130 -130
- package/src/elements/index.ts +15 -15
- package/src/elements-ds/DamageForm.browser.test.tsx +530 -0
- package/src/elements-ds/DescriptionInput.tsx +114 -0
- package/src/elements-ds/DuplicateElementDamage.tsx +185 -0
- package/src/elements-ds/ElementLookup.tsx +114 -0
- package/src/elements-ds/ElementLookupDialog.browser.test.tsx +207 -0
- package/src/elements-ds/ElementLookupDialog.tsx +194 -0
- package/src/elements-ds/FormFields.tsx +176 -0
- package/src/elements-ds/HIDScanner.tsx +53 -0
- package/src/elements-ds/ManualForm.tsx +129 -0
- package/src/elements-ds/ProjectSelect.tsx +120 -0
- package/src/elements-ds/elements-ds.css +157 -0
- package/src/elements-ds/index.ts +16 -0
- package/src/filter/index.ts +34 -34
- package/src/i18n.test.ts +31 -0
- package/src/i18n.ts +13 -13
- package/src/mui/MuiThemeProvider.tsx +135 -135
- package/src/test/fakeCamera.ts +98 -0
- package/src/test/fakeDataApi.ts +90 -0
- package/src/test/setup.browser.ts +21 -0
- package/src/test/setup.ts +23 -23
- package/src/useTranslation/index.ts +5 -3
- package/src/useTranslation/useTranslation.test.ts +128 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts +0 -1
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +0 -103
- package/es/test/setup.d.ts +0 -6
- package/es/test/setup.js +0 -6
|
@@ -1,265 +1,270 @@
|
|
|
1
|
-
import "./Toolbar.css";
|
|
2
|
-
|
|
3
|
-
import { MenuHamburgerIcon } from "@navikt/aksel-icons";
|
|
4
|
-
import { useEffect, useId, useLayoutEffect, useRef, useState } from "react";
|
|
5
|
-
import { createPortal } from "react-dom";
|
|
6
|
-
import { Link, type To } from "react-router";
|
|
7
|
-
import { getLocalizedString } from "../../i18n.js";
|
|
8
|
-
|
|
9
|
-
/*
|
|
10
|
-
* The site template (`ob.2026.application`) renders the toolbar and leaves two regions to the
|
|
11
|
-
* app. Both are filled through portals, so the app's React tree (router, contexts, state) is
|
|
12
|
-
* preserved inside them:
|
|
13
|
-
*
|
|
14
|
-
* - the app region (`#ob-toolbar-app`): breadcrumbs first, then any per-app controls;
|
|
15
|
-
* - the drawer slot (`#ob-toolbar-drawer`) at the far left of the toolbar, reserved for the
|
|
16
|
-
* toggle of the app's navigation drawer — the same edge the drawer slides in from.
|
|
17
|
-
*
|
|
18
|
-
* Everything else in the toolbar (brand, Apps button, user menu) is the site's, rendered by the
|
|
19
|
-
* chrome bundle. The phase 1 template rendered separate slots instead; those ids are kept as
|
|
20
|
-
* fallbacks until every site runs the current template.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
const APP_REGION_IDS = ["ob-toolbar-app", "ob-toolbar-breadcrumbs"];
|
|
24
|
-
const DRAWER_SLOT_IDS = ["ob-toolbar-drawer", "ob-toolbar-menu-toggle"];
|
|
25
|
-
|
|
26
|
-
function findTarget(ids: string[]) {
|
|
27
|
-
if (typeof document === "undefined") return null;
|
|
28
|
-
for (let id of ids) {
|
|
29
|
-
let element = document.getElementById(id);
|
|
30
|
-
if (element) return element;
|
|
31
|
-
}
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* The 2026 template reserves the drawer slot only for articles whose deploy wrote the
|
|
37
|
-
* `ToolbarDrawer` block (the build detects `ToolbarDrawerToggle` in the app). An app with a
|
|
38
|
-
* drawer that has not been deployed since renders on a toolbar without the slot: create it in
|
|
39
|
-
* front of the app region and drop the no-drawer layout class, so the toggle still mounts. The
|
|
40
|
-
* toolbar shifts once when that happens, which the block avoids on the next deploy.
|
|
41
|
-
*/
|
|
42
|
-
function createDrawerSlot() {
|
|
43
|
-
let toolbar = document.querySelector<HTMLElement>(".ob-toolbar");
|
|
44
|
-
let lead = toolbar?.querySelector<HTMLElement>(".ob-toolbar__lead");
|
|
45
|
-
if (!toolbar || !lead) return null;
|
|
46
|
-
|
|
47
|
-
let slot = document.createElement("div");
|
|
48
|
-
slot.id = DRAWER_SLOT_IDS[0];
|
|
49
|
-
slot.className = "ob-toolbar__drawer";
|
|
50
|
-
lead.prepend(slot);
|
|
51
|
-
toolbar.classList.remove("ob-toolbar--no-drawer");
|
|
52
|
-
return slot;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export type ToolbarPortalProps = {
|
|
56
|
-
children: React.ReactNode;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Renders its children into the toolbar's app region. Render `ToolbarBreadcrumbs` before any
|
|
61
|
-
* other `ToolbarPortal`, since the region lays its children out in mount order with the first
|
|
62
|
-
* one taking the free space. Renders nothing on a template without a toolbar.
|
|
63
|
-
*/
|
|
64
|
-
export function ToolbarPortal({ children }: ToolbarPortalProps) {
|
|
65
|
-
let target = findTarget(APP_REGION_IDS);
|
|
66
|
-
if (!target) return null;
|
|
67
|
-
return createPortal(children, target);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export type ToolbarDrawerToggleProps = {
|
|
71
|
-
/** Whether the drawer is open; sets `aria-expanded`. */
|
|
72
|
-
open: boolean;
|
|
73
|
-
onToggle: () => void;
|
|
74
|
-
/** Accessible name. Defaults to the localized "Toggle menu". */
|
|
75
|
-
label?: string;
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* The hamburger button for the app's navigation drawer, rendered into the toolbar's drawer slot.
|
|
80
|
-
* Styled by the site stylesheet (`.ob-toolbar__icon-button`) so it inherits the toolbar's
|
|
81
|
-
* contrast colour on every brand. Renders nothing on a template without a toolbar.
|
|
82
|
-
*/
|
|
83
|
-
export function ToolbarDrawerToggle({ open, onToggle, label }: ToolbarDrawerToggleProps) {
|
|
84
|
-
let target = findTarget(DRAWER_SLOT_IDS) ?? createDrawerSlot();
|
|
85
|
-
if (!target) return null;
|
|
86
|
-
|
|
87
|
-
return createPortal(
|
|
88
|
-
<button
|
|
89
|
-
type="button"
|
|
90
|
-
className="ob-toolbar__icon-button"
|
|
91
|
-
aria-label={label ?? getLocalizedString("Toggle menu")}
|
|
92
|
-
aria-expanded={open}
|
|
93
|
-
onClick={onToggle}
|
|
94
|
-
>
|
|
95
|
-
<MenuHamburgerIcon aria-hidden fontSize="1.5rem" />
|
|
96
|
-
</button>,
|
|
97
|
-
target,
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export type BreadcrumbItem = {
|
|
102
|
-
label: string;
|
|
103
|
-
/**
|
|
104
|
-
* A route inside the app, followed without reloading the page. Resolved through the router, so
|
|
105
|
-
* it is relative to the app's basename — the article root is `"/"`, not `"/my-article"`.
|
|
106
|
-
*
|
|
107
|
-
* Requires the breadcrumbs to be rendered inside a router, which the portal preserves.
|
|
108
|
-
*/
|
|
109
|
-
to?: To;
|
|
110
|
-
/**
|
|
111
|
-
* A plain URL, followed with a full page load. Use it to leave the app — the front page, or
|
|
112
|
-
* another article. `to` wins when both are given.
|
|
113
|
-
*/
|
|
114
|
-
href?: string;
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* A crumb the user can follow. `to` goes through the router; `href` is an ordinary link.
|
|
119
|
-
*
|
|
120
|
-
* Either way it renders a real anchor with a real `href`, so ctrl-click, middle-click, "open in
|
|
121
|
-
* new tab" and the status bar all keep working — `Link` only intercepts a plain left click.
|
|
122
|
-
*/
|
|
123
|
-
function BreadcrumbLink({ item }: { item: BreadcrumbItem }) {
|
|
124
|
-
if (item.to != null) return <Link to={item.to}>{item.label}</Link>;
|
|
125
|
-
if (item.href) return <a href={item.href}>{item.label}</a>;
|
|
126
|
-
|
|
127
|
-
return <span>{item.label}</span>;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export type ToolbarBreadcrumbsProps = {
|
|
131
|
-
items: BreadcrumbItem[];
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
function BreadcrumbsInner({ items }: ToolbarBreadcrumbsProps) {
|
|
135
|
-
let [firstVisible, setFirstVisible] = useState(0);
|
|
136
|
-
let [menuOpen, setMenuOpen] = useState(false);
|
|
137
|
-
|
|
138
|
-
let
|
|
139
|
-
let
|
|
140
|
-
let
|
|
141
|
-
let
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
//
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
let
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
let
|
|
197
|
-
let
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
if (open) placeMenu();
|
|
233
|
-
}}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
)
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
1
|
+
import "./Toolbar.css";
|
|
2
|
+
|
|
3
|
+
import { MenuHamburgerIcon } from "@navikt/aksel-icons";
|
|
4
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState } from "react";
|
|
5
|
+
import { createPortal } from "react-dom";
|
|
6
|
+
import { Link, type To } from "react-router";
|
|
7
|
+
import { getLocalizedString } from "../../i18n.js";
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
* The site template (`ob.2026.application`) renders the toolbar and leaves two regions to the
|
|
11
|
+
* app. Both are filled through portals, so the app's React tree (router, contexts, state) is
|
|
12
|
+
* preserved inside them:
|
|
13
|
+
*
|
|
14
|
+
* - the app region (`#ob-toolbar-app`): breadcrumbs first, then any per-app controls;
|
|
15
|
+
* - the drawer slot (`#ob-toolbar-drawer`) at the far left of the toolbar, reserved for the
|
|
16
|
+
* toggle of the app's navigation drawer — the same edge the drawer slides in from.
|
|
17
|
+
*
|
|
18
|
+
* Everything else in the toolbar (brand, Apps button, user menu) is the site's, rendered by the
|
|
19
|
+
* chrome bundle. The phase 1 template rendered separate slots instead; those ids are kept as
|
|
20
|
+
* fallbacks until every site runs the current template.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const APP_REGION_IDS = ["ob-toolbar-app", "ob-toolbar-breadcrumbs"];
|
|
24
|
+
const DRAWER_SLOT_IDS = ["ob-toolbar-drawer", "ob-toolbar-menu-toggle"];
|
|
25
|
+
|
|
26
|
+
function findTarget(ids: string[]) {
|
|
27
|
+
if (typeof document === "undefined") return null;
|
|
28
|
+
for (let id of ids) {
|
|
29
|
+
let element = document.getElementById(id);
|
|
30
|
+
if (element) return element;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The 2026 template reserves the drawer slot only for articles whose deploy wrote the
|
|
37
|
+
* `ToolbarDrawer` block (the build detects `ToolbarDrawerToggle` in the app). An app with a
|
|
38
|
+
* drawer that has not been deployed since renders on a toolbar without the slot: create it in
|
|
39
|
+
* front of the app region and drop the no-drawer layout class, so the toggle still mounts. The
|
|
40
|
+
* toolbar shifts once when that happens, which the block avoids on the next deploy.
|
|
41
|
+
*/
|
|
42
|
+
function createDrawerSlot() {
|
|
43
|
+
let toolbar = document.querySelector<HTMLElement>(".ob-toolbar");
|
|
44
|
+
let lead = toolbar?.querySelector<HTMLElement>(".ob-toolbar__lead");
|
|
45
|
+
if (!toolbar || !lead) return null;
|
|
46
|
+
|
|
47
|
+
let slot = document.createElement("div");
|
|
48
|
+
slot.id = DRAWER_SLOT_IDS[0];
|
|
49
|
+
slot.className = "ob-toolbar__drawer";
|
|
50
|
+
lead.prepend(slot);
|
|
51
|
+
toolbar.classList.remove("ob-toolbar--no-drawer");
|
|
52
|
+
return slot;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type ToolbarPortalProps = {
|
|
56
|
+
children: React.ReactNode;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Renders its children into the toolbar's app region. Render `ToolbarBreadcrumbs` before any
|
|
61
|
+
* other `ToolbarPortal`, since the region lays its children out in mount order with the first
|
|
62
|
+
* one taking the free space. Renders nothing on a template without a toolbar.
|
|
63
|
+
*/
|
|
64
|
+
export function ToolbarPortal({ children }: ToolbarPortalProps) {
|
|
65
|
+
let target = findTarget(APP_REGION_IDS);
|
|
66
|
+
if (!target) return null;
|
|
67
|
+
return createPortal(children, target);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type ToolbarDrawerToggleProps = {
|
|
71
|
+
/** Whether the drawer is open; sets `aria-expanded`. */
|
|
72
|
+
open: boolean;
|
|
73
|
+
onToggle: () => void;
|
|
74
|
+
/** Accessible name. Defaults to the localized "Toggle menu". */
|
|
75
|
+
label?: string;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The hamburger button for the app's navigation drawer, rendered into the toolbar's drawer slot.
|
|
80
|
+
* Styled by the site stylesheet (`.ob-toolbar__icon-button`) so it inherits the toolbar's
|
|
81
|
+
* contrast colour on every brand. Renders nothing on a template without a toolbar.
|
|
82
|
+
*/
|
|
83
|
+
export function ToolbarDrawerToggle({ open, onToggle, label }: ToolbarDrawerToggleProps) {
|
|
84
|
+
let target = findTarget(DRAWER_SLOT_IDS) ?? createDrawerSlot();
|
|
85
|
+
if (!target) return null;
|
|
86
|
+
|
|
87
|
+
return createPortal(
|
|
88
|
+
<button
|
|
89
|
+
type="button"
|
|
90
|
+
className="ob-toolbar__icon-button"
|
|
91
|
+
aria-label={label ?? getLocalizedString("Toggle menu")}
|
|
92
|
+
aria-expanded={open}
|
|
93
|
+
onClick={onToggle}
|
|
94
|
+
>
|
|
95
|
+
<MenuHamburgerIcon aria-hidden fontSize="1.5rem" />
|
|
96
|
+
</button>,
|
|
97
|
+
target,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type BreadcrumbItem = {
|
|
102
|
+
label: string;
|
|
103
|
+
/**
|
|
104
|
+
* A route inside the app, followed without reloading the page. Resolved through the router, so
|
|
105
|
+
* it is relative to the app's basename — the article root is `"/"`, not `"/my-article"`.
|
|
106
|
+
*
|
|
107
|
+
* Requires the breadcrumbs to be rendered inside a router, which the portal preserves.
|
|
108
|
+
*/
|
|
109
|
+
to?: To;
|
|
110
|
+
/**
|
|
111
|
+
* A plain URL, followed with a full page load. Use it to leave the app — the front page, or
|
|
112
|
+
* another article. `to` wins when both are given.
|
|
113
|
+
*/
|
|
114
|
+
href?: string;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A crumb the user can follow. `to` goes through the router; `href` is an ordinary link.
|
|
119
|
+
*
|
|
120
|
+
* Either way it renders a real anchor with a real `href`, so ctrl-click, middle-click, "open in
|
|
121
|
+
* new tab" and the status bar all keep working — `Link` only intercepts a plain left click.
|
|
122
|
+
*/
|
|
123
|
+
function BreadcrumbLink({ item }: { item: BreadcrumbItem }) {
|
|
124
|
+
if (item.to != null) return <Link to={item.to}>{item.label}</Link>;
|
|
125
|
+
if (item.href) return <a href={item.href}>{item.label}</a>;
|
|
126
|
+
|
|
127
|
+
return <span>{item.label}</span>;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export type ToolbarBreadcrumbsProps = {
|
|
131
|
+
items: BreadcrumbItem[];
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
function BreadcrumbsInner({ items }: ToolbarBreadcrumbsProps) {
|
|
135
|
+
let [firstVisible, setFirstVisible] = useState(0);
|
|
136
|
+
let [menuOpen, setMenuOpen] = useState(false);
|
|
137
|
+
// Bumped on resize, so the trail is measured again even when nothing was collapsed.
|
|
138
|
+
let [, setMeasuredWidth] = useState(0);
|
|
139
|
+
let navRef = useRef<HTMLElement>(null);
|
|
140
|
+
let listRef = useRef<HTMLOListElement>(null);
|
|
141
|
+
let buttonRef = useRef<HTMLButtonElement>(null);
|
|
142
|
+
let menuRef = useRef<HTMLUListElement>(null);
|
|
143
|
+
let menuId = useId();
|
|
144
|
+
|
|
145
|
+
// A new trail (the user navigated) is measured from scratch. Adjusted during render, so
|
|
146
|
+
// the previous page's collapse never paints on the new one.
|
|
147
|
+
let trail = items.map((item) => item.label).join("\n");
|
|
148
|
+
let [measuredTrail, setMeasuredTrail] = useState(trail);
|
|
149
|
+
if (measuredTrail !== trail) {
|
|
150
|
+
setMeasuredTrail(trail);
|
|
151
|
+
setFirstVisible(0);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Detect overflow: increment firstVisible until the list fits within the nav.
|
|
155
|
+
// Runs after every render so it keeps pushing items into overflow until the
|
|
156
|
+
// list stops overflowing or only the last item (current page) remains.
|
|
157
|
+
useLayoutEffect(() => {
|
|
158
|
+
let list = listRef.current;
|
|
159
|
+
if (!list) return;
|
|
160
|
+
|
|
161
|
+
// Against the list's own box, not the nav's: the nav also holds the separator the site
|
|
162
|
+
// stylesheet draws between the brand and the trail.
|
|
163
|
+
if (list.scrollWidth > list.clientWidth + 1 && firstVisible < items.length - 1) {
|
|
164
|
+
setFirstVisible((prev) => prev + 1);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// ResizeObserver resets firstVisible to 0 on container resize so that the
|
|
169
|
+
// overflow calculation starts fresh at the new container width. The width is kept in
|
|
170
|
+
// state as well: when nothing was collapsed, resetting firstVisible alone would not
|
|
171
|
+
// re-render, and a narrowed toolbar would clip the trail instead of collapsing it.
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
let nav = navRef.current;
|
|
174
|
+
if (!nav) return;
|
|
175
|
+
|
|
176
|
+
let width = nav.clientWidth;
|
|
177
|
+
let observer = new ResizeObserver(() => {
|
|
178
|
+
// The first callback reports the size the trail was just measured at.
|
|
179
|
+
if (nav.clientWidth === width) return;
|
|
180
|
+
width = nav.clientWidth;
|
|
181
|
+
setFirstVisible(0);
|
|
182
|
+
setMeasuredWidth(width);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
observer.observe(nav);
|
|
186
|
+
return () => observer.disconnect();
|
|
187
|
+
}, []);
|
|
188
|
+
|
|
189
|
+
/*
|
|
190
|
+
* The menu is a popover: it renders in the top layer, so the toolbar and the trail, which
|
|
191
|
+
* both clip their overflow, cannot cut it off, and the browser closes it on Escape and on a
|
|
192
|
+
* click outside. The top layer has no positioned ancestor, so it is placed under the button
|
|
193
|
+
* by hand, and pulled back in when it would run past the edge of a narrow screen.
|
|
194
|
+
*/
|
|
195
|
+
function placeMenu() {
|
|
196
|
+
let button = buttonRef.current;
|
|
197
|
+
let menu = menuRef.current;
|
|
198
|
+
if (!button || !menu) return;
|
|
199
|
+
|
|
200
|
+
let rect = button.getBoundingClientRect();
|
|
201
|
+
let margin = 8;
|
|
202
|
+
let left = Math.min(rect.left, window.innerWidth - menu.offsetWidth - margin);
|
|
203
|
+
menu.style.top = `${rect.bottom + 4}px`;
|
|
204
|
+
menu.style.left = `${Math.max(left, margin)}px`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
let hiddenItems = items.slice(0, firstVisible);
|
|
208
|
+
let visibleItems = items.slice(firstVisible);
|
|
209
|
+
|
|
210
|
+
return (
|
|
211
|
+
<nav className="ob-breadcrumbs" aria-label="Breadcrumb" ref={navRef}>
|
|
212
|
+
<ol className="ob-breadcrumbs__list" ref={listRef}>
|
|
213
|
+
{firstVisible > 0 && (
|
|
214
|
+
<li className="ob-breadcrumbs__item ob-breadcrumbs__overflow">
|
|
215
|
+
<button
|
|
216
|
+
ref={buttonRef}
|
|
217
|
+
type="button"
|
|
218
|
+
aria-label={getLocalizedString("Show hidden breadcrumbs")}
|
|
219
|
+
aria-expanded={menuOpen}
|
|
220
|
+
popoverTarget={menuId}
|
|
221
|
+
>
|
|
222
|
+
…
|
|
223
|
+
</button>
|
|
224
|
+
<ul
|
|
225
|
+
ref={menuRef}
|
|
226
|
+
id={menuId}
|
|
227
|
+
popover="auto"
|
|
228
|
+
className="ob-breadcrumbs__overflow-menu"
|
|
229
|
+
// Placed before it shows, so it never paints in the popover's default
|
|
230
|
+
// spot, and again once it has a width to keep it on the screen.
|
|
231
|
+
onBeforeToggle={(event) => {
|
|
232
|
+
if (event.newState === "open") placeMenu();
|
|
233
|
+
}}
|
|
234
|
+
onToggle={(event) => {
|
|
235
|
+
let open = event.newState === "open";
|
|
236
|
+
setMenuOpen(open);
|
|
237
|
+
if (open) placeMenu();
|
|
238
|
+
}}
|
|
239
|
+
>
|
|
240
|
+
{hiddenItems.map((item, index) => (
|
|
241
|
+
// oxlint-disable-next-line react/no-array-index-key -- stable list with no reordering
|
|
242
|
+
<li key={index} onClick={() => menuRef.current?.hidePopover()}>
|
|
243
|
+
<BreadcrumbLink item={item} />
|
|
244
|
+
</li>
|
|
245
|
+
))}
|
|
246
|
+
</ul>
|
|
247
|
+
</li>
|
|
248
|
+
)}
|
|
249
|
+
{visibleItems.map((item, index) => {
|
|
250
|
+
let actualIndex = firstVisible + index;
|
|
251
|
+
let isCurrent = actualIndex === items.length - 1;
|
|
252
|
+
return (
|
|
253
|
+
<li key={actualIndex} className="ob-breadcrumbs__item">
|
|
254
|
+
{isCurrent ? <span aria-current="page">{item.label}</span> : <BreadcrumbLink item={item} />}
|
|
255
|
+
</li>
|
|
256
|
+
);
|
|
257
|
+
})}
|
|
258
|
+
</ol>
|
|
259
|
+
</nav>
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** The app's breadcrumb trail, rendered into the toolbar's app region. The last item is the current page. */
|
|
264
|
+
export function ToolbarBreadcrumbs({ items }: ToolbarBreadcrumbsProps) {
|
|
265
|
+
return (
|
|
266
|
+
<ToolbarPortal>
|
|
267
|
+
<BreadcrumbsInner items={items} />
|
|
268
|
+
</ToolbarPortal>
|
|
269
|
+
);
|
|
270
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { fireEvent, render, screen } from "@testing-library/react";
|
|
2
|
+
import { createRef, useState } from "react";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { ValueToggleGroup, type ValueToggleOption } from "./index";
|
|
6
|
+
|
|
7
|
+
const periods: ValueToggleOption[] = [
|
|
8
|
+
{ value: "week", label: "Week" },
|
|
9
|
+
{ value: "month", label: "Month" },
|
|
10
|
+
{ value: "year", label: "Year" },
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
describe("ValueToggleGroup", () => {
|
|
14
|
+
it("is a radio group with one option per value", () => {
|
|
15
|
+
render(<ValueToggleGroup options={periods} value="month" />);
|
|
16
|
+
|
|
17
|
+
expect(screen.getByRole("radiogroup")).toBeInTheDocument();
|
|
18
|
+
expect(screen.getAllByRole("radio").map((radio) => (radio as HTMLInputElement).value)).toEqual([
|
|
19
|
+
"week",
|
|
20
|
+
"month",
|
|
21
|
+
"year",
|
|
22
|
+
]);
|
|
23
|
+
expect(screen.getByRole("radio", { name: "Month" })).toBeChecked();
|
|
24
|
+
expect(screen.getByRole("radio", { name: "Week" })).not.toBeChecked();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("labels an option with its value when it has no label", () => {
|
|
28
|
+
render(<ValueToggleGroup options={[{ value: 2025 }, { value: 2026 }]} />);
|
|
29
|
+
expect(screen.getByRole("radio", { name: "2026" })).toBeInTheDocument();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("reports the chosen value with its original type", () => {
|
|
33
|
+
let onChange = vi.fn();
|
|
34
|
+
render(<ValueToggleGroup options={[{ value: 2025 }, { value: 2026 }]} value={2025} onChange={onChange} />);
|
|
35
|
+
|
|
36
|
+
fireEvent.click(screen.getByRole("radio", { name: "2026" }));
|
|
37
|
+
|
|
38
|
+
expect(onChange).toHaveBeenCalledExactlyOnceWith(2026);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("shows what the app chose, not what was clicked, when controlled", () => {
|
|
42
|
+
let onChange = vi.fn();
|
|
43
|
+
let { rerender } = render(<ValueToggleGroup options={periods} value="week" onChange={onChange} />);
|
|
44
|
+
|
|
45
|
+
fireEvent.click(screen.getByRole("radio", { name: "Year" }));
|
|
46
|
+
expect(onChange).toHaveBeenCalledWith("year");
|
|
47
|
+
expect(screen.getByRole("radio", { name: "Week" })).toBeChecked();
|
|
48
|
+
|
|
49
|
+
rerender(<ValueToggleGroup options={periods} value="year" onChange={onChange} />);
|
|
50
|
+
expect(screen.getByRole("radio", { name: "Year" })).toBeChecked();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("follows the clicks itself when uncontrolled", () => {
|
|
54
|
+
let onChange = vi.fn();
|
|
55
|
+
render(<ValueToggleGroup options={periods} defaultValue="week" onChange={onChange} />);
|
|
56
|
+
expect(screen.getByRole("radio", { name: "Week" })).toBeChecked();
|
|
57
|
+
|
|
58
|
+
fireEvent.click(screen.getByRole("radio", { name: "Month" }));
|
|
59
|
+
|
|
60
|
+
expect(onChange).toHaveBeenCalledWith("month");
|
|
61
|
+
expect(screen.getByRole("radio", { name: "Month" })).toBeChecked();
|
|
62
|
+
expect(screen.getByRole("radio", { name: "Week" })).not.toBeChecked();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("works as a toggle with state in the app", () => {
|
|
66
|
+
function Period() {
|
|
67
|
+
let [period, setPeriod] = useState<string | number>("week");
|
|
68
|
+
return <ValueToggleGroup options={periods} value={period} onChange={setPeriod} />;
|
|
69
|
+
}
|
|
70
|
+
render(<Period />);
|
|
71
|
+
|
|
72
|
+
fireEvent.click(screen.getByRole("radio", { name: "Year" }));
|
|
73
|
+
|
|
74
|
+
expect(screen.getByRole("radio", { name: "Year" })).toBeChecked();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("cannot choose a disabled option", () => {
|
|
78
|
+
let onChange = vi.fn();
|
|
79
|
+
render(
|
|
80
|
+
<ValueToggleGroup
|
|
81
|
+
options={[...periods, { value: "all", label: "All time", disabled: true }]}
|
|
82
|
+
value="week"
|
|
83
|
+
onChange={onChange}
|
|
84
|
+
/>,
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
expect(screen.getByRole("radio", { name: "All time" })).toBeDisabled();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("keeps two groups on one page apart", () => {
|
|
91
|
+
render(
|
|
92
|
+
<>
|
|
93
|
+
<ValueToggleGroup options={periods} defaultValue="week" />
|
|
94
|
+
<ValueToggleGroup options={periods} defaultValue="year" />
|
|
95
|
+
</>,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
let [first, second] = screen.getAllByRole("radiogroup");
|
|
99
|
+
let firstName = first.querySelector("input")!.name;
|
|
100
|
+
let secondName = second.querySelector("input")!.name;
|
|
101
|
+
expect(firstName).not.toBe(secondName);
|
|
102
|
+
expect(screen.getAllByRole("radio", { checked: true })).toHaveLength(2);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("uses the name it is given and hands the fieldset to a ref", () => {
|
|
106
|
+
let ref = createRef<HTMLFieldSetElement>();
|
|
107
|
+
render(<ValueToggleGroup ref={ref} name="period" options={periods} />);
|
|
108
|
+
|
|
109
|
+
expect(screen.getAllByRole("radio").every((radio) => (radio as HTMLInputElement).name === "period")).toBe(true);
|
|
110
|
+
expect(ref.current).toBe(screen.getByRole("radiogroup"));
|
|
111
|
+
});
|
|
112
|
+
});
|