@dbcdk/react-components 0.0.196 → 0.0.198
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/components/json-viewer/JsonViewer.module.css +52 -0
- package/dist/components/json-viewer/JsonViewerShell.cjs +147 -143
- package/dist/components/json-viewer/JsonViewerShell.js +149 -145
- package/dist/components/line-viewer/LineViewer.cjs +15 -10
- package/dist/components/line-viewer/LineViewer.d.ts +1 -1
- package/dist/components/line-viewer/LineViewer.js +15 -10
- package/dist/components/line-viewer/LineViewer.module.css +58 -3
- package/dist/components/page-layout/PageLayout.module.css +1 -1
- package/dist/components/scroll-fade/ScrollFade.cjs +42 -0
- package/dist/components/scroll-fade/ScrollFade.d.ts +18 -0
- package/dist/components/scroll-fade/ScrollFade.js +36 -0
- package/dist/components/scroll-fade/ScrollFade.module.css +71 -0
- package/dist/components/search-box/SearchBox.cjs +39 -35
- package/dist/components/search-box/SearchBox.d.ts +2 -0
- package/dist/components/search-box/SearchBox.js +39 -35
- package/dist/components/table/Table.cjs +52 -23
- package/dist/components/table/Table.d.ts +1 -1
- package/dist/components/table/Table.js +53 -24
- package/dist/components/table/Table.module.css +4 -0
- package/dist/components/table/Table.types.d.ts +27 -0
- package/dist/components/table/TanstackTable.cjs +64 -64
- package/dist/components/table/TanstackTable.js +67 -67
- package/dist/components/table/columnLayout.utils.cjs +146 -0
- package/dist/components/table/columnLayout.utils.d.ts +44 -0
- package/dist/components/table/columnLayout.utils.js +141 -0
- package/dist/components/table/components/TableHeader.cjs +3 -1
- package/dist/components/table/components/TableHeader.d.ts +2 -1
- package/dist/components/table/components/TableHeader.js +3 -1
- package/dist/components/table/components/TableHeaderCell.cjs +29 -19
- package/dist/components/table/components/TableHeaderCell.d.ts +2 -1
- package/dist/components/table/components/TableHeaderCell.js +29 -19
- package/dist/components/table/hooks/useElementWidth.cjs +40 -0
- package/dist/components/table/hooks/useElementWidth.d.ts +7 -0
- package/dist/components/table/hooks/useElementWidth.js +38 -0
- package/dist/components/table/table.utils.cjs +24 -2
- package/dist/components/table/table.utils.d.ts +40 -2
- package/dist/components/table/table.utils.js +20 -2
- package/dist/components/table/tanstackTable.utils.cjs +33 -97
- package/dist/components/table/tanstackTable.utils.d.ts +24 -8
- package/dist/components/table/tanstackTable.utils.js +34 -99
- package/dist/components/xml-viewer/XmlViewer.cjs +15 -10
- package/dist/components/xml-viewer/XmlViewer.d.ts +1 -1
- package/dist/components/xml-viewer/XmlViewer.js +15 -10
- package/dist/components/xml-viewer/XmlViewer.module.css +58 -3
- package/dist/hooks/useColorScheme.cjs +15 -15
- package/dist/hooks/useColorScheme.d.ts +1 -0
- package/dist/hooks/useColorScheme.js +15 -15
- package/dist/hooks/useScrollOverflow.cjs +71 -0
- package/dist/hooks/useScrollOverflow.d.ts +23 -0
- package/dist/hooks/useScrollOverflow.js +69 -0
- package/dist/hooks/useTableSettings.cjs +2 -1
- package/dist/hooks/useTableSettings.d.ts +1 -0
- package/dist/hooks/useTableSettings.js +2 -1
- package/dist/hooks/useTheme.cjs +15 -15
- package/dist/hooks/useTheme.d.ts +3 -1
- package/dist/hooks/useTheme.js +15 -15
- package/dist/index.css +1 -0
- package/dist/utils/refs.utils.cjs +13 -0
- package/dist/utils/refs.utils.d.ts +7 -0
- package/dist/utils/refs.utils.js +11 -0
- package/package.json +1 -1
|
@@ -4,9 +4,9 @@ import { COLOR_SCHEME_STORAGE_KEY, isColorScheme } from '../utils/color-scheme.c
|
|
|
4
4
|
import { readCookie, writeCookie } from '../utils/cookie.utils';
|
|
5
5
|
import { readLocalStorage, writeLocalStorage } from '../utils/localStorage.utils';
|
|
6
6
|
|
|
7
|
-
function persistColorScheme(storageKey, id) {
|
|
8
|
-
writeLocalStorage(storageKey, id);
|
|
9
|
-
writeCookie(storageKey, id);
|
|
7
|
+
function persistColorScheme(storageKey, id, persistence) {
|
|
8
|
+
if (persistence !== "cookie") writeLocalStorage(storageKey, id);
|
|
9
|
+
if (persistence !== "localStorage") writeCookie(storageKey, id);
|
|
10
10
|
}
|
|
11
11
|
function getDomColorScheme() {
|
|
12
12
|
return document.documentElement.dataset.theme;
|
|
@@ -32,43 +32,43 @@ function getSnapshot() {
|
|
|
32
32
|
function getServerSnapshot() {
|
|
33
33
|
return null;
|
|
34
34
|
}
|
|
35
|
-
function resolveInitialColorScheme(storageKey, initialColorScheme) {
|
|
35
|
+
function resolveInitialColorScheme(storageKey, initialColorScheme, persistence) {
|
|
36
36
|
const fromDomAttribute = getDomColorScheme();
|
|
37
37
|
let resolvedColorScheme = isColorScheme(fromDomAttribute) ? fromDomAttribute : initialColorScheme;
|
|
38
|
-
const fromCookie = readCookie(storageKey);
|
|
38
|
+
const fromCookie = persistence !== "localStorage" ? readCookie(storageKey) : void 0;
|
|
39
39
|
if (isColorScheme(fromCookie)) {
|
|
40
40
|
resolvedColorScheme = fromCookie;
|
|
41
|
-
} else {
|
|
41
|
+
} else if (persistence !== "cookie") {
|
|
42
42
|
const fromStorage = readLocalStorage(storageKey);
|
|
43
43
|
if (isColorScheme(fromStorage)) resolvedColorScheme = fromStorage;
|
|
44
44
|
}
|
|
45
45
|
return resolvedColorScheme;
|
|
46
46
|
}
|
|
47
|
-
function ensureResolved(initialColorScheme, storageKey) {
|
|
47
|
+
function ensureResolved(initialColorScheme, storageKey, persistence) {
|
|
48
48
|
if (resolved) return;
|
|
49
49
|
resolved = true;
|
|
50
|
-
const value = resolveInitialColorScheme(storageKey, initialColorScheme);
|
|
50
|
+
const value = resolveInitialColorScheme(storageKey, initialColorScheme, persistence);
|
|
51
51
|
applyColorScheme(value);
|
|
52
|
-
persistColorScheme(storageKey, value);
|
|
52
|
+
persistColorScheme(storageKey, value, persistence);
|
|
53
53
|
currentColorScheme = value;
|
|
54
54
|
notify();
|
|
55
55
|
}
|
|
56
|
-
function switchColorSchemeInternal(storageKey, id) {
|
|
56
|
+
function switchColorSchemeInternal(storageKey, id, persistence) {
|
|
57
57
|
applyColorScheme(id);
|
|
58
|
-
persistColorScheme(storageKey, id);
|
|
58
|
+
persistColorScheme(storageKey, id, persistence);
|
|
59
59
|
currentColorScheme = id;
|
|
60
60
|
resolved = true;
|
|
61
61
|
notify();
|
|
62
62
|
return id;
|
|
63
63
|
}
|
|
64
64
|
function useColorScheme(initialColorScheme = "system", options = {}) {
|
|
65
|
-
const { storageKey = COLOR_SCHEME_STORAGE_KEY } = options;
|
|
65
|
+
const { storageKey = COLOR_SCHEME_STORAGE_KEY, persistence = "both" } = options;
|
|
66
66
|
useEffect(() => {
|
|
67
|
-
ensureResolved(initialColorScheme, storageKey);
|
|
68
|
-
}, [initialColorScheme, storageKey]);
|
|
67
|
+
ensureResolved(initialColorScheme, storageKey, persistence);
|
|
68
|
+
}, [initialColorScheme, storageKey, persistence]);
|
|
69
69
|
const colorScheme = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
70
70
|
function switchColorScheme(id) {
|
|
71
|
-
return switchColorSchemeInternal(storageKey, id);
|
|
71
|
+
return switchColorSchemeInternal(storageKey, id, persistence);
|
|
72
72
|
}
|
|
73
73
|
return { colorScheme, switchColorScheme };
|
|
74
74
|
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var react = require('react');
|
|
5
|
+
|
|
6
|
+
const NONE = {
|
|
7
|
+
top: false,
|
|
8
|
+
bottom: false,
|
|
9
|
+
left: false,
|
|
10
|
+
right: false,
|
|
11
|
+
scrollbarWidth: 0,
|
|
12
|
+
scrollbarHeight: 0
|
|
13
|
+
};
|
|
14
|
+
const THRESHOLD_PX = 1;
|
|
15
|
+
function measureScrollbars(el) {
|
|
16
|
+
const style = getComputedStyle(el);
|
|
17
|
+
const borderX = parseFloat(style.borderLeftWidth) + parseFloat(style.borderRightWidth) || 0;
|
|
18
|
+
const borderY = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth) || 0;
|
|
19
|
+
return {
|
|
20
|
+
scrollbarWidth: Math.max(0, el.offsetWidth - el.clientWidth - borderX),
|
|
21
|
+
scrollbarHeight: Math.max(0, el.offsetHeight - el.clientHeight - borderY)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function measure(el, scrollbars) {
|
|
25
|
+
const { scrollTop, scrollLeft, scrollHeight, scrollWidth, clientHeight, clientWidth } = el;
|
|
26
|
+
return {
|
|
27
|
+
top: scrollTop > THRESHOLD_PX,
|
|
28
|
+
bottom: scrollHeight - clientHeight - scrollTop > THRESHOLD_PX,
|
|
29
|
+
left: scrollLeft > THRESHOLD_PX,
|
|
30
|
+
right: scrollWidth - clientWidth - scrollLeft > THRESHOLD_PX,
|
|
31
|
+
...scrollbars
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function sameOverflow(a, b) {
|
|
35
|
+
return Object.keys(a).every((key) => a[key] === b[key]);
|
|
36
|
+
}
|
|
37
|
+
function useScrollOverflow(el) {
|
|
38
|
+
const [overflow, setOverflow] = react.useState(NONE);
|
|
39
|
+
react.useLayoutEffect(() => {
|
|
40
|
+
if (!el) return;
|
|
41
|
+
let scrollbars = measureScrollbars(el);
|
|
42
|
+
const update = () => {
|
|
43
|
+
const next = measure(el, scrollbars);
|
|
44
|
+
setOverflow((prev) => sameOverflow(prev, next) ? prev : next);
|
|
45
|
+
};
|
|
46
|
+
const updateLayout = () => {
|
|
47
|
+
scrollbars = measureScrollbars(el);
|
|
48
|
+
update();
|
|
49
|
+
};
|
|
50
|
+
update();
|
|
51
|
+
el.addEventListener("scroll", update, { passive: true });
|
|
52
|
+
const resizeObserver = typeof ResizeObserver !== "undefined" ? new ResizeObserver(updateLayout) : null;
|
|
53
|
+
resizeObserver == null ? void 0 : resizeObserver.observe(el);
|
|
54
|
+
const mutationObserver = typeof MutationObserver !== "undefined" ? new MutationObserver(updateLayout) : null;
|
|
55
|
+
mutationObserver == null ? void 0 : mutationObserver.observe(el, {
|
|
56
|
+
subtree: true,
|
|
57
|
+
childList: true,
|
|
58
|
+
characterData: true,
|
|
59
|
+
attributes: true,
|
|
60
|
+
attributeFilter: ["class", "style"]
|
|
61
|
+
});
|
|
62
|
+
return () => {
|
|
63
|
+
el.removeEventListener("scroll", update);
|
|
64
|
+
resizeObserver == null ? void 0 : resizeObserver.disconnect();
|
|
65
|
+
mutationObserver == null ? void 0 : mutationObserver.disconnect();
|
|
66
|
+
};
|
|
67
|
+
}, [el]);
|
|
68
|
+
return el ? overflow : NONE;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
exports.useScrollOverflow = useScrollOverflow;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Which edges of a scroll container have more content beyond them. */
|
|
2
|
+
export type ScrollOverflow = {
|
|
3
|
+
top: boolean;
|
|
4
|
+
bottom: boolean;
|
|
5
|
+
left: boolean;
|
|
6
|
+
right: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Space taken by classic (non-overlay) scrollbars, in pixels: the vertical
|
|
9
|
+
* scrollbar's width and the horizontal one's height. Lets edge hints stay
|
|
10
|
+
* clear of the scrollbars. 0 for overlay scrollbars.
|
|
11
|
+
*/
|
|
12
|
+
scrollbarWidth: number;
|
|
13
|
+
scrollbarHeight: number;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Tracks which edges of the scroll container `el` hide more content, so the
|
|
17
|
+
* caller can hint at it (e.g. with edge fades) without restyling the native
|
|
18
|
+
* scrollbars. Updates on scroll, on resize and when the content changes.
|
|
19
|
+
* Takes the element rather than a ref so it follows the element when it's
|
|
20
|
+
* replaced; keep it in state via a callback ref. All edges are `false` on the
|
|
21
|
+
* server and until the first client measurement.
|
|
22
|
+
*/
|
|
23
|
+
export declare function useScrollOverflow(el: HTMLElement | null): ScrollOverflow;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useState, useLayoutEffect } from 'react';
|
|
3
|
+
|
|
4
|
+
const NONE = {
|
|
5
|
+
top: false,
|
|
6
|
+
bottom: false,
|
|
7
|
+
left: false,
|
|
8
|
+
right: false,
|
|
9
|
+
scrollbarWidth: 0,
|
|
10
|
+
scrollbarHeight: 0
|
|
11
|
+
};
|
|
12
|
+
const THRESHOLD_PX = 1;
|
|
13
|
+
function measureScrollbars(el) {
|
|
14
|
+
const style = getComputedStyle(el);
|
|
15
|
+
const borderX = parseFloat(style.borderLeftWidth) + parseFloat(style.borderRightWidth) || 0;
|
|
16
|
+
const borderY = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth) || 0;
|
|
17
|
+
return {
|
|
18
|
+
scrollbarWidth: Math.max(0, el.offsetWidth - el.clientWidth - borderX),
|
|
19
|
+
scrollbarHeight: Math.max(0, el.offsetHeight - el.clientHeight - borderY)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function measure(el, scrollbars) {
|
|
23
|
+
const { scrollTop, scrollLeft, scrollHeight, scrollWidth, clientHeight, clientWidth } = el;
|
|
24
|
+
return {
|
|
25
|
+
top: scrollTop > THRESHOLD_PX,
|
|
26
|
+
bottom: scrollHeight - clientHeight - scrollTop > THRESHOLD_PX,
|
|
27
|
+
left: scrollLeft > THRESHOLD_PX,
|
|
28
|
+
right: scrollWidth - clientWidth - scrollLeft > THRESHOLD_PX,
|
|
29
|
+
...scrollbars
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function sameOverflow(a, b) {
|
|
33
|
+
return Object.keys(a).every((key) => a[key] === b[key]);
|
|
34
|
+
}
|
|
35
|
+
function useScrollOverflow(el) {
|
|
36
|
+
const [overflow, setOverflow] = useState(NONE);
|
|
37
|
+
useLayoutEffect(() => {
|
|
38
|
+
if (!el) return;
|
|
39
|
+
let scrollbars = measureScrollbars(el);
|
|
40
|
+
const update = () => {
|
|
41
|
+
const next = measure(el, scrollbars);
|
|
42
|
+
setOverflow((prev) => sameOverflow(prev, next) ? prev : next);
|
|
43
|
+
};
|
|
44
|
+
const updateLayout = () => {
|
|
45
|
+
scrollbars = measureScrollbars(el);
|
|
46
|
+
update();
|
|
47
|
+
};
|
|
48
|
+
update();
|
|
49
|
+
el.addEventListener("scroll", update, { passive: true });
|
|
50
|
+
const resizeObserver = typeof ResizeObserver !== "undefined" ? new ResizeObserver(updateLayout) : null;
|
|
51
|
+
resizeObserver == null ? void 0 : resizeObserver.observe(el);
|
|
52
|
+
const mutationObserver = typeof MutationObserver !== "undefined" ? new MutationObserver(updateLayout) : null;
|
|
53
|
+
mutationObserver == null ? void 0 : mutationObserver.observe(el, {
|
|
54
|
+
subtree: true,
|
|
55
|
+
childList: true,
|
|
56
|
+
characterData: true,
|
|
57
|
+
attributes: true,
|
|
58
|
+
attributeFilter: ["class", "style"]
|
|
59
|
+
});
|
|
60
|
+
return () => {
|
|
61
|
+
el.removeEventListener("scroll", update);
|
|
62
|
+
resizeObserver == null ? void 0 : resizeObserver.disconnect();
|
|
63
|
+
mutationObserver == null ? void 0 : mutationObserver.disconnect();
|
|
64
|
+
};
|
|
65
|
+
}, [el]);
|
|
66
|
+
return el ? overflow : NONE;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { useScrollOverflow };
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
var react = require('react');
|
|
5
|
+
var table_utils = require('../components/table/table.utils');
|
|
5
6
|
var tableSettings_utils = require('../components/table/tableSettings.utils');
|
|
6
7
|
var localStorage_utils = require('../utils/localStorage.utils');
|
|
7
8
|
|
|
@@ -27,7 +28,7 @@ const localStorageTableSettingsStorage = {
|
|
|
27
28
|
function useTableSettings({
|
|
28
29
|
storageKey,
|
|
29
30
|
tableColumns,
|
|
30
|
-
defaultViewMode =
|
|
31
|
+
defaultViewMode = table_utils.DEFAULT_VIEW_MODE,
|
|
31
32
|
defaultVisibleColumnIds,
|
|
32
33
|
defaultStriped = false,
|
|
33
34
|
storage = localStorageTableSettingsStorage
|
|
@@ -13,6 +13,7 @@ export declare const localStorageTableSettingsStorage: TableSettingsStorage;
|
|
|
13
13
|
export interface UseTableSettingsOptions<T = unknown> {
|
|
14
14
|
storageKey: string;
|
|
15
15
|
tableColumns?: ReadonlyArray<ColumnItem<T> | TableSettingsColumn>;
|
|
16
|
+
/** View mode until the user picks one. Defaults to `'wrapped'`; pass `'compact'` for even row heights. */
|
|
16
17
|
defaultViewMode?: ViewMode;
|
|
17
18
|
defaultVisibleColumnIds?: string[];
|
|
18
19
|
defaultStriped?: boolean;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { useMemo, useState, useEffect, useCallback } from 'react';
|
|
3
|
+
import { DEFAULT_VIEW_MODE } from '../components/table/table.utils';
|
|
3
4
|
import { isSettingsColumnHidden, getSettingsColumnId } from '../components/table/tableSettings.utils';
|
|
4
5
|
import { writeLocalStorage, readLocalStorage } from '../utils/localStorage.utils';
|
|
5
6
|
|
|
@@ -25,7 +26,7 @@ const localStorageTableSettingsStorage = {
|
|
|
25
26
|
function useTableSettings({
|
|
26
27
|
storageKey,
|
|
27
28
|
tableColumns,
|
|
28
|
-
defaultViewMode =
|
|
29
|
+
defaultViewMode = DEFAULT_VIEW_MODE,
|
|
29
30
|
defaultVisibleColumnIds,
|
|
30
31
|
defaultStriped = false,
|
|
31
32
|
storage = localStorageTableSettingsStorage
|
package/dist/hooks/useTheme.cjs
CHANGED
|
@@ -7,9 +7,9 @@ var cookie_utils = require('../utils/cookie.utils');
|
|
|
7
7
|
var localStorage_utils = require('../utils/localStorage.utils');
|
|
8
8
|
var theme_constants = require('../utils/theme.constants');
|
|
9
9
|
|
|
10
|
-
function persistTheme(storageKey, id) {
|
|
11
|
-
localStorage_utils.writeLocalStorage(storageKey, id);
|
|
12
|
-
cookie_utils.writeCookie(storageKey, id);
|
|
10
|
+
function persistTheme(storageKey, id, persistence) {
|
|
11
|
+
if (persistence !== "cookie") localStorage_utils.writeLocalStorage(storageKey, id);
|
|
12
|
+
if (persistence !== "localStorage") cookie_utils.writeCookie(storageKey, id);
|
|
13
13
|
}
|
|
14
14
|
function getDomTheme() {
|
|
15
15
|
return document.documentElement.dataset.productTheme;
|
|
@@ -40,23 +40,23 @@ function getSnapshot() {
|
|
|
40
40
|
function getServerSnapshot() {
|
|
41
41
|
return null;
|
|
42
42
|
}
|
|
43
|
-
function resolveInitialTheme(storageKey, defaultTheme) {
|
|
43
|
+
function resolveInitialTheme(storageKey, defaultTheme, persistence) {
|
|
44
44
|
const fromDom = getDomTheme();
|
|
45
45
|
if (fromDom) return fromDom;
|
|
46
|
-
const fromCookie = cookie_utils.readCookie(storageKey);
|
|
46
|
+
const fromCookie = persistence !== "localStorage" ? cookie_utils.readCookie(storageKey) : void 0;
|
|
47
47
|
if (fromCookie) return fromCookie;
|
|
48
|
-
const fromStorage = localStorage_utils.readLocalStorage(storageKey);
|
|
48
|
+
const fromStorage = persistence !== "cookie" ? localStorage_utils.readLocalStorage(storageKey) : void 0;
|
|
49
49
|
if (fromStorage) return fromStorage;
|
|
50
50
|
return defaultTheme != null ? defaultTheme : null;
|
|
51
51
|
}
|
|
52
|
-
function ensureResolved(defaultTheme, storageKey) {
|
|
52
|
+
function ensureResolved(defaultTheme, storageKey, persistence) {
|
|
53
53
|
if (resolved) return;
|
|
54
54
|
resolved = true;
|
|
55
|
-
const value = resolveInitialTheme(storageKey, defaultTheme);
|
|
55
|
+
const value = resolveInitialTheme(storageKey, defaultTheme, persistence);
|
|
56
56
|
currentTheme = value;
|
|
57
57
|
if (value) {
|
|
58
58
|
applyTheme(value);
|
|
59
|
-
persistTheme(storageKey, value);
|
|
59
|
+
persistTheme(storageKey, value, persistence);
|
|
60
60
|
}
|
|
61
61
|
notify();
|
|
62
62
|
}
|
|
@@ -72,22 +72,22 @@ function ensureColorSchemeObserver() {
|
|
|
72
72
|
attributeFilter: ["data-theme"]
|
|
73
73
|
});
|
|
74
74
|
}
|
|
75
|
-
function switchThemeInternal(storageKey, id) {
|
|
75
|
+
function switchThemeInternal(storageKey, id, persistence) {
|
|
76
76
|
applyTheme(id);
|
|
77
|
-
persistTheme(storageKey, id);
|
|
77
|
+
persistTheme(storageKey, id, persistence);
|
|
78
78
|
currentTheme = id;
|
|
79
79
|
resolved = true;
|
|
80
80
|
notify();
|
|
81
81
|
}
|
|
82
82
|
function useTheme(options = {}) {
|
|
83
|
-
const { defaultTheme, storageKey = theme_constants.THEME_STORAGE_KEY } = options;
|
|
83
|
+
const { defaultTheme, storageKey = theme_constants.THEME_STORAGE_KEY, persistence = "both" } = options;
|
|
84
84
|
react.useEffect(() => {
|
|
85
85
|
ensureColorSchemeObserver();
|
|
86
|
-
ensureResolved(defaultTheme, storageKey);
|
|
87
|
-
}, [defaultTheme, storageKey]);
|
|
86
|
+
ensureResolved(defaultTheme, storageKey, persistence);
|
|
87
|
+
}, [defaultTheme, storageKey, persistence]);
|
|
88
88
|
const theme = react.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
89
89
|
function switchTheme(id) {
|
|
90
|
-
switchThemeInternal(storageKey, id);
|
|
90
|
+
switchThemeInternal(storageKey, id, persistence);
|
|
91
91
|
}
|
|
92
92
|
return { theme, switchTheme };
|
|
93
93
|
}
|
package/dist/hooks/useTheme.d.ts
CHANGED
|
@@ -4,12 +4,14 @@ export interface UseThemeOptions {
|
|
|
4
4
|
/** The theme the app should fall back to when nothing is stored yet. */
|
|
5
5
|
defaultTheme?: ThemeId;
|
|
6
6
|
/**
|
|
7
|
-
* Storage key used for the
|
|
7
|
+
* Storage key used for the selected persistence store(s). Defaults to a
|
|
8
8
|
* shared key, so apps that run on the same origin (e.g. multiple products
|
|
9
9
|
* under one domain) should each pass their own value here — otherwise
|
|
10
10
|
* they'll unintentionally read and overwrite each other's persisted theme.
|
|
11
11
|
*/
|
|
12
12
|
storageKey?: string;
|
|
13
|
+
/** Where to read and persist the theme. Defaults to both stores. */
|
|
14
|
+
persistence?: 'both' | 'cookie' | 'localStorage';
|
|
13
15
|
}
|
|
14
16
|
export declare function useTheme(options?: UseThemeOptions): {
|
|
15
17
|
theme: ThemeId | null;
|
package/dist/hooks/useTheme.js
CHANGED
|
@@ -5,9 +5,9 @@ import { readCookie, writeCookie } from '../utils/cookie.utils';
|
|
|
5
5
|
import { readLocalStorage, writeLocalStorage } from '../utils/localStorage.utils';
|
|
6
6
|
import { THEME_STORAGE_KEY } from '../utils/theme.constants';
|
|
7
7
|
|
|
8
|
-
function persistTheme(storageKey, id) {
|
|
9
|
-
writeLocalStorage(storageKey, id);
|
|
10
|
-
writeCookie(storageKey, id);
|
|
8
|
+
function persistTheme(storageKey, id, persistence) {
|
|
9
|
+
if (persistence !== "cookie") writeLocalStorage(storageKey, id);
|
|
10
|
+
if (persistence !== "localStorage") writeCookie(storageKey, id);
|
|
11
11
|
}
|
|
12
12
|
function getDomTheme() {
|
|
13
13
|
return document.documentElement.dataset.productTheme;
|
|
@@ -38,23 +38,23 @@ function getSnapshot() {
|
|
|
38
38
|
function getServerSnapshot() {
|
|
39
39
|
return null;
|
|
40
40
|
}
|
|
41
|
-
function resolveInitialTheme(storageKey, defaultTheme) {
|
|
41
|
+
function resolveInitialTheme(storageKey, defaultTheme, persistence) {
|
|
42
42
|
const fromDom = getDomTheme();
|
|
43
43
|
if (fromDom) return fromDom;
|
|
44
|
-
const fromCookie = readCookie(storageKey);
|
|
44
|
+
const fromCookie = persistence !== "localStorage" ? readCookie(storageKey) : void 0;
|
|
45
45
|
if (fromCookie) return fromCookie;
|
|
46
|
-
const fromStorage = readLocalStorage(storageKey);
|
|
46
|
+
const fromStorage = persistence !== "cookie" ? readLocalStorage(storageKey) : void 0;
|
|
47
47
|
if (fromStorage) return fromStorage;
|
|
48
48
|
return defaultTheme != null ? defaultTheme : null;
|
|
49
49
|
}
|
|
50
|
-
function ensureResolved(defaultTheme, storageKey) {
|
|
50
|
+
function ensureResolved(defaultTheme, storageKey, persistence) {
|
|
51
51
|
if (resolved) return;
|
|
52
52
|
resolved = true;
|
|
53
|
-
const value = resolveInitialTheme(storageKey, defaultTheme);
|
|
53
|
+
const value = resolveInitialTheme(storageKey, defaultTheme, persistence);
|
|
54
54
|
currentTheme = value;
|
|
55
55
|
if (value) {
|
|
56
56
|
applyTheme(value);
|
|
57
|
-
persistTheme(storageKey, value);
|
|
57
|
+
persistTheme(storageKey, value, persistence);
|
|
58
58
|
}
|
|
59
59
|
notify();
|
|
60
60
|
}
|
|
@@ -70,22 +70,22 @@ function ensureColorSchemeObserver() {
|
|
|
70
70
|
attributeFilter: ["data-theme"]
|
|
71
71
|
});
|
|
72
72
|
}
|
|
73
|
-
function switchThemeInternal(storageKey, id) {
|
|
73
|
+
function switchThemeInternal(storageKey, id, persistence) {
|
|
74
74
|
applyTheme(id);
|
|
75
|
-
persistTheme(storageKey, id);
|
|
75
|
+
persistTheme(storageKey, id, persistence);
|
|
76
76
|
currentTheme = id;
|
|
77
77
|
resolved = true;
|
|
78
78
|
notify();
|
|
79
79
|
}
|
|
80
80
|
function useTheme(options = {}) {
|
|
81
|
-
const { defaultTheme, storageKey = THEME_STORAGE_KEY } = options;
|
|
81
|
+
const { defaultTheme, storageKey = THEME_STORAGE_KEY, persistence = "both" } = options;
|
|
82
82
|
useEffect(() => {
|
|
83
83
|
ensureColorSchemeObserver();
|
|
84
|
-
ensureResolved(defaultTheme, storageKey);
|
|
85
|
-
}, [defaultTheme, storageKey]);
|
|
84
|
+
ensureResolved(defaultTheme, storageKey, persistence);
|
|
85
|
+
}, [defaultTheme, storageKey, persistence]);
|
|
86
86
|
const theme = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
87
87
|
function switchTheme(id) {
|
|
88
|
-
switchThemeInternal(storageKey, id);
|
|
88
|
+
switchThemeInternal(storageKey, id, persistence);
|
|
89
89
|
}
|
|
90
90
|
return { theme, switchTheme };
|
|
91
91
|
}
|
package/dist/index.css
CHANGED
|
@@ -56,6 +56,7 @@
|
|
|
56
56
|
@import './components/pagination/Pagination.module.css';
|
|
57
57
|
@import './components/panel/Panel.module.css';
|
|
58
58
|
@import './components/popover/Popover.module.css';
|
|
59
|
+
@import './components/scroll-fade/ScrollFade.module.css';
|
|
59
60
|
@import './components/search-box/SearchBox.module.css';
|
|
60
61
|
@import './components/section-label/SectionLabel.module.css';
|
|
61
62
|
@import './components/segmented-progress-bar/SegmentedProgressBar.module.css';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Ref } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Combines refs into one callback ref. Returns a new function on every call,
|
|
4
|
+
* so memoize it (e.g. with `useMemo`) when a stable ref matters, such as when
|
|
5
|
+
* one of the refs sets state.
|
|
6
|
+
*/
|
|
7
|
+
export declare function mergeRefs<T>(...refs: Array<Ref<T> | undefined>): (node: T | null) => void;
|