@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
package/src/colorScheme.ts
CHANGED
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
import { useCallback, useEffect, useState } from "react";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Color scheme preference (system / light / dark).
|
|
5
|
-
*
|
|
6
|
-
* The preference lives in the `ob-color-scheme` cookie so the article template
|
|
7
|
-
* can read it server side and render `data-color-scheme` on `<html>` and
|
|
8
|
-
* `<body>` before the first paint. The site chrome bundle
|
|
9
|
-
* (`@olenbetong/synergi-chrome`) owns the same cookie for the toolbar user menu
|
|
10
|
-
* and app menu toggles, so all of them write the cookie, apply the attributes,
|
|
11
|
-
* and dispatch `COLOR_SCHEME_CHANGE_EVENT` to keep every toggle on the page in sync.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
export type ColorSchemePreference = "auto" | "light" | "dark";
|
|
15
|
-
|
|
16
|
-
const COOKIE_NAME = "ob-color-scheme";
|
|
17
|
-
const COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
|
|
18
|
-
const DEFAULT_PREFERENCE: ColorSchemePreference = "auto";
|
|
19
|
-
const PREFERENCES: ColorSchemePreference[] = ["auto", "light", "dark"];
|
|
20
|
-
|
|
21
|
-
/** Dispatched on `window` whenever the preference changes. */
|
|
22
|
-
export const COLOR_SCHEME_CHANGE_EVENT = "ob:colorschemechange";
|
|
23
|
-
|
|
24
|
-
function isColorSchemePreference(value: unknown): value is ColorSchemePreference {
|
|
25
|
-
return typeof value === "string" && PREFERENCES.includes(value as ColorSchemePreference);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function getColorSchemePreference(): ColorSchemePreference {
|
|
29
|
-
if (typeof document === "undefined") return DEFAULT_PREFERENCE;
|
|
30
|
-
|
|
31
|
-
let match = document.cookie.match(new RegExp(`(?:^|;\\s*)${COOKIE_NAME}=([^;]*)`));
|
|
32
|
-
let value = match ? decodeURIComponent(match[1]) : null;
|
|
33
|
-
|
|
34
|
-
return isColorSchemePreference(value) ? value : DEFAULT_PREFERENCE;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function setColorSchemePreference(preference: ColorSchemePreference) {
|
|
38
|
-
if (typeof document === "undefined" || !isColorSchemePreference(preference)) return;
|
|
39
|
-
|
|
40
|
-
let secure = window.location.protocol === "https:" ? ";secure" : "";
|
|
41
|
-
document.cookie = `${COOKIE_NAME}=${preference};path=/;max-age=${COOKIE_MAX_AGE};samesite=lax${secure}`;
|
|
42
|
-
|
|
43
|
-
/*
|
|
44
|
-
* Both elements are updated: `<html>` so the browser applies `color-scheme`
|
|
45
|
-
* to scrollbars and native controls, `<body>` because Designsystemet declares
|
|
46
|
-
* the light tokens on `:root` as well (`:root, [data-color-scheme="light"]`).
|
|
47
|
-
*/
|
|
48
|
-
document.documentElement.dataset.colorScheme = preference;
|
|
49
|
-
if (document.body) document.body.dataset.colorScheme = preference;
|
|
50
|
-
|
|
51
|
-
window.dispatchEvent(new CustomEvent(COLOR_SCHEME_CHANGE_EVENT, { detail: preference }));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** Reads the stored preference and keeps it in sync with other toggles on the page. */
|
|
55
|
-
export function useColorSchemePreference(): [ColorSchemePreference, (value: ColorSchemePreference) => void] {
|
|
56
|
-
let [preference, setPreference] = useState<ColorSchemePreference>(() => getColorSchemePreference());
|
|
57
|
-
|
|
58
|
-
useEffect(() => {
|
|
59
|
-
function update() {
|
|
60
|
-
setPreference(getColorSchemePreference());
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
update();
|
|
64
|
-
|
|
65
|
-
window.addEventListener(COLOR_SCHEME_CHANGE_EVENT, update);
|
|
66
|
-
// Cookies fire no change event, so also pick up changes made in another tab.
|
|
67
|
-
document.addEventListener("visibilitychange", update);
|
|
68
|
-
|
|
69
|
-
return () => {
|
|
70
|
-
window.removeEventListener(COLOR_SCHEME_CHANGE_EVENT, update);
|
|
71
|
-
document.removeEventListener("visibilitychange", update);
|
|
72
|
-
};
|
|
73
|
-
}, []);
|
|
74
|
-
|
|
75
|
-
let setValue = useCallback((value: ColorSchemePreference) => {
|
|
76
|
-
setColorSchemePreference(value);
|
|
77
|
-
setPreference(value);
|
|
78
|
-
}, []);
|
|
79
|
-
|
|
80
|
-
return [preference, setValue];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** The scheme Designsystemet is rendering with right now: the preference, with `auto` resolved. */
|
|
84
|
-
export function getResolvedColorScheme(): "light" | "dark" {
|
|
85
|
-
if (typeof document === "undefined") return "light";
|
|
86
|
-
|
|
87
|
-
let scheme = document.body?.dataset.colorScheme ?? document.documentElement?.dataset.colorScheme ?? "light";
|
|
88
|
-
|
|
89
|
-
if (scheme === "auto") {
|
|
90
|
-
return globalThis.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return scheme === "dark" ? "dark" : "light";
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Tracks the color scheme Designsystemet is rendering with. The article template
|
|
98
|
-
* sets `data-color-scheme` on `<body>`, so watch that attribute as well as the
|
|
99
|
-
* OS preference (used when the scheme is `auto`).
|
|
100
|
-
*/
|
|
101
|
-
export function useResolvedColorScheme(): "light" | "dark" {
|
|
102
|
-
let [scheme, setScheme] = useState<"light" | "dark">(() => getResolvedColorScheme());
|
|
103
|
-
|
|
104
|
-
useEffect(() => {
|
|
105
|
-
function update() {
|
|
106
|
-
setScheme(getResolvedColorScheme());
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
update();
|
|
110
|
-
|
|
111
|
-
let observer = new MutationObserver(update);
|
|
112
|
-
observer.observe(document.body, { attributes: true, attributeFilter: ["data-color-scheme"] });
|
|
113
|
-
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["data-color-scheme"] });
|
|
114
|
-
|
|
115
|
-
let media = globalThis.matchMedia?.("(prefers-color-scheme: dark)");
|
|
116
|
-
media?.addEventListener("change", update);
|
|
117
|
-
|
|
118
|
-
return () => {
|
|
119
|
-
observer.disconnect();
|
|
120
|
-
media?.removeEventListener("change", update);
|
|
121
|
-
};
|
|
122
|
-
}, []);
|
|
123
|
-
|
|
124
|
-
return scheme;
|
|
125
|
-
}
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Color scheme preference (system / light / dark).
|
|
5
|
+
*
|
|
6
|
+
* The preference lives in the `ob-color-scheme` cookie so the article template
|
|
7
|
+
* can read it server side and render `data-color-scheme` on `<html>` and
|
|
8
|
+
* `<body>` before the first paint. The site chrome bundle
|
|
9
|
+
* (`@olenbetong/synergi-chrome`) owns the same cookie for the toolbar user menu
|
|
10
|
+
* and app menu toggles, so all of them write the cookie, apply the attributes,
|
|
11
|
+
* and dispatch `COLOR_SCHEME_CHANGE_EVENT` to keep every toggle on the page in sync.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export type ColorSchemePreference = "auto" | "light" | "dark";
|
|
15
|
+
|
|
16
|
+
const COOKIE_NAME = "ob-color-scheme";
|
|
17
|
+
const COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
|
|
18
|
+
const DEFAULT_PREFERENCE: ColorSchemePreference = "auto";
|
|
19
|
+
const PREFERENCES: ColorSchemePreference[] = ["auto", "light", "dark"];
|
|
20
|
+
|
|
21
|
+
/** Dispatched on `window` whenever the preference changes. */
|
|
22
|
+
export const COLOR_SCHEME_CHANGE_EVENT = "ob:colorschemechange";
|
|
23
|
+
|
|
24
|
+
function isColorSchemePreference(value: unknown): value is ColorSchemePreference {
|
|
25
|
+
return typeof value === "string" && PREFERENCES.includes(value as ColorSchemePreference);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getColorSchemePreference(): ColorSchemePreference {
|
|
29
|
+
if (typeof document === "undefined") return DEFAULT_PREFERENCE;
|
|
30
|
+
|
|
31
|
+
let match = document.cookie.match(new RegExp(`(?:^|;\\s*)${COOKIE_NAME}=([^;]*)`));
|
|
32
|
+
let value = match ? decodeURIComponent(match[1]) : null;
|
|
33
|
+
|
|
34
|
+
return isColorSchemePreference(value) ? value : DEFAULT_PREFERENCE;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function setColorSchemePreference(preference: ColorSchemePreference) {
|
|
38
|
+
if (typeof document === "undefined" || !isColorSchemePreference(preference)) return;
|
|
39
|
+
|
|
40
|
+
let secure = window.location.protocol === "https:" ? ";secure" : "";
|
|
41
|
+
document.cookie = `${COOKIE_NAME}=${preference};path=/;max-age=${COOKIE_MAX_AGE};samesite=lax${secure}`;
|
|
42
|
+
|
|
43
|
+
/*
|
|
44
|
+
* Both elements are updated: `<html>` so the browser applies `color-scheme`
|
|
45
|
+
* to scrollbars and native controls, `<body>` because Designsystemet declares
|
|
46
|
+
* the light tokens on `:root` as well (`:root, [data-color-scheme="light"]`).
|
|
47
|
+
*/
|
|
48
|
+
document.documentElement.dataset.colorScheme = preference;
|
|
49
|
+
if (document.body) document.body.dataset.colorScheme = preference;
|
|
50
|
+
|
|
51
|
+
window.dispatchEvent(new CustomEvent(COLOR_SCHEME_CHANGE_EVENT, { detail: preference }));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Reads the stored preference and keeps it in sync with other toggles on the page. */
|
|
55
|
+
export function useColorSchemePreference(): [ColorSchemePreference, (value: ColorSchemePreference) => void] {
|
|
56
|
+
let [preference, setPreference] = useState<ColorSchemePreference>(() => getColorSchemePreference());
|
|
57
|
+
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
function update() {
|
|
60
|
+
setPreference(getColorSchemePreference());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
update();
|
|
64
|
+
|
|
65
|
+
window.addEventListener(COLOR_SCHEME_CHANGE_EVENT, update);
|
|
66
|
+
// Cookies fire no change event, so also pick up changes made in another tab.
|
|
67
|
+
document.addEventListener("visibilitychange", update);
|
|
68
|
+
|
|
69
|
+
return () => {
|
|
70
|
+
window.removeEventListener(COLOR_SCHEME_CHANGE_EVENT, update);
|
|
71
|
+
document.removeEventListener("visibilitychange", update);
|
|
72
|
+
};
|
|
73
|
+
}, []);
|
|
74
|
+
|
|
75
|
+
let setValue = useCallback((value: ColorSchemePreference) => {
|
|
76
|
+
setColorSchemePreference(value);
|
|
77
|
+
setPreference(value);
|
|
78
|
+
}, []);
|
|
79
|
+
|
|
80
|
+
return [preference, setValue];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The scheme Designsystemet is rendering with right now: the preference, with `auto` resolved. */
|
|
84
|
+
export function getResolvedColorScheme(): "light" | "dark" {
|
|
85
|
+
if (typeof document === "undefined") return "light";
|
|
86
|
+
|
|
87
|
+
let scheme = document.body?.dataset.colorScheme ?? document.documentElement?.dataset.colorScheme ?? "light";
|
|
88
|
+
|
|
89
|
+
if (scheme === "auto") {
|
|
90
|
+
return globalThis.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return scheme === "dark" ? "dark" : "light";
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Tracks the color scheme Designsystemet is rendering with. The article template
|
|
98
|
+
* sets `data-color-scheme` on `<body>`, so watch that attribute as well as the
|
|
99
|
+
* OS preference (used when the scheme is `auto`).
|
|
100
|
+
*/
|
|
101
|
+
export function useResolvedColorScheme(): "light" | "dark" {
|
|
102
|
+
let [scheme, setScheme] = useState<"light" | "dark">(() => getResolvedColorScheme());
|
|
103
|
+
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
function update() {
|
|
106
|
+
setScheme(getResolvedColorScheme());
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
update();
|
|
110
|
+
|
|
111
|
+
let observer = new MutationObserver(update);
|
|
112
|
+
observer.observe(document.body, { attributes: true, attributeFilter: ["data-color-scheme"] });
|
|
113
|
+
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["data-color-scheme"] });
|
|
114
|
+
|
|
115
|
+
let media = globalThis.matchMedia?.("(prefers-color-scheme: dark)");
|
|
116
|
+
media?.addEventListener("change", update);
|
|
117
|
+
|
|
118
|
+
return () => {
|
|
119
|
+
observer.disconnect();
|
|
120
|
+
media?.removeEventListener("change", update);
|
|
121
|
+
};
|
|
122
|
+
}, []);
|
|
123
|
+
|
|
124
|
+
return scheme;
|
|
125
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import { createRef, useState } from "react";
|
|
3
|
+
import { userEvent } from "vitest/browser";
|
|
4
|
+
import { describe, expect, it, vi } from "vitest";
|
|
5
|
+
|
|
6
|
+
import { Checkbox } from "./index";
|
|
7
|
+
|
|
8
|
+
/** The box the user sees and clicks; the native input is hidden behind it. */
|
|
9
|
+
function box(container: HTMLElement) {
|
|
10
|
+
return container.querySelector<HTMLElement>(".ObCheckbox-box")!;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function Controlled({ onChange }: { onChange: (checked: boolean) => void }) {
|
|
14
|
+
let [checked, setChecked] = useState(false);
|
|
15
|
+
return (
|
|
16
|
+
<Checkbox
|
|
17
|
+
label="Send invoice"
|
|
18
|
+
value={checked}
|
|
19
|
+
onChange={(event) => {
|
|
20
|
+
let next = (event.target as HTMLInputElement).checked;
|
|
21
|
+
onChange(next);
|
|
22
|
+
setChecked(next);
|
|
23
|
+
}}
|
|
24
|
+
/>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe("Checkbox", () => {
|
|
29
|
+
it("is labelled and reflects its value", () => {
|
|
30
|
+
let { rerender } = render(<Checkbox label="Send invoice" value={false} onChange={() => {}} />);
|
|
31
|
+
expect(screen.getByRole("checkbox", { name: "Send invoice" })).not.toBeChecked();
|
|
32
|
+
|
|
33
|
+
rerender(<Checkbox label="Send invoice" value onChange={() => {}} />);
|
|
34
|
+
expect(screen.getByRole("checkbox", { name: "Send invoice" })).toBeChecked();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("toggles once per click on the box", async () => {
|
|
38
|
+
let onChange = vi.fn();
|
|
39
|
+
let { container } = render(<Controlled onChange={onChange} />);
|
|
40
|
+
|
|
41
|
+
await userEvent.click(box(container));
|
|
42
|
+
expect(onChange.mock.calls).toEqual([[true]]);
|
|
43
|
+
expect(screen.getByRole("checkbox")).toBeChecked();
|
|
44
|
+
expect(screen.getByRole("checkbox")).toHaveFocus();
|
|
45
|
+
|
|
46
|
+
await userEvent.click(box(container));
|
|
47
|
+
expect(onChange.mock.calls).toEqual([[true], [false]]);
|
|
48
|
+
expect(screen.getByRole("checkbox")).not.toBeChecked();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("toggles once per click on the label text", async () => {
|
|
52
|
+
let onChange = vi.fn();
|
|
53
|
+
render(<Controlled onChange={onChange} />);
|
|
54
|
+
|
|
55
|
+
await userEvent.click(screen.getByText("Send invoice"));
|
|
56
|
+
|
|
57
|
+
expect(onChange.mock.calls).toEqual([[true]]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("toggles with Space from the keyboard", async () => {
|
|
61
|
+
let onChange = vi.fn();
|
|
62
|
+
render(<Controlled onChange={onChange} />);
|
|
63
|
+
|
|
64
|
+
screen.getByRole("checkbox").focus();
|
|
65
|
+
await userEvent.keyboard(" ");
|
|
66
|
+
|
|
67
|
+
expect(onChange.mock.calls).toEqual([[true]]);
|
|
68
|
+
expect(screen.getByRole("checkbox")).toBeChecked();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("ignores clicks when disabled", async () => {
|
|
72
|
+
let onChange = vi.fn();
|
|
73
|
+
let { container } = render(<Checkbox label="Send invoice" value={false} disabled onChange={onChange} />);
|
|
74
|
+
|
|
75
|
+
await userEvent.click(box(container), { force: true });
|
|
76
|
+
|
|
77
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
78
|
+
expect(screen.getByRole("checkbox")).toBeDisabled();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("toggles once inside an outer label that points at it", async () => {
|
|
82
|
+
// The customer list in partner-administration wraps each row in a label for the checkbox.
|
|
83
|
+
let onChange = vi.fn();
|
|
84
|
+
function Row() {
|
|
85
|
+
let [checked, setChecked] = useState(false);
|
|
86
|
+
return (
|
|
87
|
+
<label htmlFor="customer-check-12">
|
|
88
|
+
<span>Olen Betong AS</span>
|
|
89
|
+
<Checkbox
|
|
90
|
+
id="customer-check-12"
|
|
91
|
+
value={checked}
|
|
92
|
+
onChange={() => {
|
|
93
|
+
onChange();
|
|
94
|
+
setChecked((value) => !value);
|
|
95
|
+
}}
|
|
96
|
+
/>
|
|
97
|
+
</label>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
let { container } = render(<Row />);
|
|
101
|
+
|
|
102
|
+
await userEvent.click(screen.getByText("Olen Betong AS"));
|
|
103
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
104
|
+
expect(screen.getByRole("checkbox")).toBeChecked();
|
|
105
|
+
|
|
106
|
+
await userEvent.click(box(container));
|
|
107
|
+
expect(onChange).toHaveBeenCalledTimes(2);
|
|
108
|
+
expect(screen.getByRole("checkbox")).not.toBeChecked();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("hands the native input to a ref", () => {
|
|
112
|
+
let ref = createRef<HTMLInputElement>();
|
|
113
|
+
render(<Checkbox ref={ref} value onChange={() => {}} />);
|
|
114
|
+
expect(ref.current).toBe(screen.getByRole("checkbox"));
|
|
115
|
+
});
|
|
116
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
.ObCheckbox-root {
|
|
2
|
-
--check-width: 2rem;
|
|
2
|
+
--check-width: var(--ds-size-8, 2rem);
|
|
3
3
|
--bg-color: var(--brand-dark, rgba(42, 65, 50, 0.5));
|
|
4
4
|
--bg-color-hover: var(--brand-primary, rgba(42, 65, 50, 0.8));
|
|
5
5
|
--bg-color-selected: var(--brand-light);
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
|
|
76
76
|
.ObCheckbox-label {
|
|
77
77
|
display: flex;
|
|
78
|
-
gap: 0.5rem;
|
|
78
|
+
gap: var(--ds-size-2, 0.5rem);
|
|
79
79
|
align-items: center;
|
|
80
80
|
|
|
81
81
|
overflow: visible;
|
|
@@ -36,7 +36,6 @@ export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(function Che
|
|
|
36
36
|
) {
|
|
37
37
|
let inputId = useId();
|
|
38
38
|
inputId = id ?? inputId;
|
|
39
|
-
let inputIdRef = `#${inputId}`;
|
|
40
39
|
let input = useRef<HTMLInputElement>(null);
|
|
41
40
|
|
|
42
41
|
// oxlint-disable-next-line react-hooks/exhaustive-deps -- Ref object
|
|
@@ -48,14 +47,6 @@ export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(function Che
|
|
|
48
47
|
}
|
|
49
48
|
}, []);
|
|
50
49
|
|
|
51
|
-
function handleClick() {
|
|
52
|
-
let elem = input.current;
|
|
53
|
-
if (elem) {
|
|
54
|
-
elem.focus();
|
|
55
|
-
elem.click();
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
50
|
function handleInputChanged(evt: any) {
|
|
60
51
|
if (!disabled) {
|
|
61
52
|
onChange(evt);
|
|
@@ -88,7 +79,7 @@ export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(function Che
|
|
|
88
79
|
ref={input}
|
|
89
80
|
{...other}
|
|
90
81
|
/>
|
|
91
|
-
<label htmlFor={
|
|
82
|
+
<label htmlFor={inputId} className="ObCheckbox-label" onKeyDown={handleKeyDown}>
|
|
92
83
|
<div className="ObCheckbox-box">
|
|
93
84
|
<Mark className="ObCheckbox-mark" />
|
|
94
85
|
</div>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
.ObCodeEditor-root {
|
|
2
|
-
min-height: 10rem;
|
|
3
|
-
border: 1px solid var(--ds-color-neutral-border-subtle);
|
|
4
|
-
border-radius: var(--ds-border-radius-md);
|
|
5
|
-
overflow: hidden;
|
|
6
|
-
}
|
|
1
|
+
.ObCodeEditor-root {
|
|
2
|
+
min-height: 10rem;
|
|
3
|
+
border: 1px solid var(--ds-color-neutral-border-subtle);
|
|
4
|
+
border-radius: var(--ds-border-radius-md);
|
|
5
|
+
overflow: hidden;
|
|
6
|
+
}
|