@dbcdk/react-components 0.0.178 → 0.0.179
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/README.md +20 -1
- package/dist/client.cjs +7 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +1 -0
- package/dist/hooks/useGetActiveTheme.cjs +13 -0
- package/dist/hooks/useGetActiveTheme.d.ts +8 -0
- package/dist/hooks/useGetActiveTheme.js +11 -0
- package/dist/hooks/useTheme.cjs +6 -4
- package/dist/hooks/useTheme.d.ts +2 -2
- package/dist/hooks/useTheme.js +6 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -72,7 +72,7 @@ export default function RootLayout({ children }: Readonly<{ children: ReactNode
|
|
|
72
72
|
|
|
73
73
|
### 4) Switching theme in your application
|
|
74
74
|
|
|
75
|
-
Example using `useTheme()
|
|
75
|
+
Example using `useTheme()` when you want to control the theme:
|
|
76
76
|
|
|
77
77
|
```tsx
|
|
78
78
|
'use client'
|
|
@@ -99,6 +99,25 @@ export default function Header() {
|
|
|
99
99
|
The hook updates `data-product-theme`, applies the selected product theme's
|
|
100
100
|
token overrides, and persists the selection.
|
|
101
101
|
|
|
102
|
+
If you only need to read the active theme state, use `useGetActiveTheme()`
|
|
103
|
+
instead of `useTheme()`:
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
'use client'
|
|
107
|
+
|
|
108
|
+
import { useGetActiveTheme } from '@dbcdk/react-components/client'
|
|
109
|
+
|
|
110
|
+
export function ThemeBadge() {
|
|
111
|
+
const { theme, colorScheme } = useGetActiveTheme()
|
|
112
|
+
|
|
113
|
+
return <span>{theme} / {colorScheme}</span>
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`useGetActiveTheme()` is intentionally read-only and takes no options. It
|
|
118
|
+
reports the current active theme state as established by `ThemeScript`,
|
|
119
|
+
persisted storage, or another mounted theme control.
|
|
120
|
+
|
|
102
121
|
---
|
|
103
122
|
|
|
104
123
|
## Using Storybook
|
package/dist/client.cjs
CHANGED
|
@@ -19,6 +19,7 @@ var Footer = require('./components/page-layout/components/footer/Footer');
|
|
|
19
19
|
var Input = require('./components/forms/input/Input');
|
|
20
20
|
var SearchBox = require('./components/search-box/SearchBox');
|
|
21
21
|
var useColorScheme = require('./hooks/useColorScheme');
|
|
22
|
+
var useGetActiveTheme = require('./hooks/useGetActiveTheme');
|
|
22
23
|
var useTheme = require('./hooks/useTheme');
|
|
23
24
|
var usePersistentState = require('./hooks/usePersistentState');
|
|
24
25
|
var Chip = require('./components/chip/Chip');
|
|
@@ -203,6 +204,12 @@ Object.keys(useColorScheme).forEach(function (k) {
|
|
|
203
204
|
get: function () { return useColorScheme[k]; }
|
|
204
205
|
});
|
|
205
206
|
});
|
|
207
|
+
Object.keys(useGetActiveTheme).forEach(function (k) {
|
|
208
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
209
|
+
enumerable: true,
|
|
210
|
+
get: function () { return useGetActiveTheme[k]; }
|
|
211
|
+
});
|
|
212
|
+
});
|
|
206
213
|
Object.keys(useTheme).forEach(function (k) {
|
|
207
214
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
208
215
|
enumerable: true,
|
package/dist/client.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export * from './components/page-layout/components/footer/Footer';
|
|
|
16
16
|
export * from './components/forms/input/Input';
|
|
17
17
|
export * from './components/search-box/SearchBox';
|
|
18
18
|
export * from './hooks/useColorScheme';
|
|
19
|
+
export * from './hooks/useGetActiveTheme';
|
|
19
20
|
export * from './hooks/useTheme';
|
|
20
21
|
export * from './hooks/usePersistentState';
|
|
21
22
|
export * from './components/chip/Chip';
|
package/dist/client.js
CHANGED
|
@@ -17,6 +17,7 @@ export * from './components/page-layout/components/footer/Footer';
|
|
|
17
17
|
export * from './components/forms/input/Input';
|
|
18
18
|
export * from './components/search-box/SearchBox';
|
|
19
19
|
export * from './hooks/useColorScheme';
|
|
20
|
+
export * from './hooks/useGetActiveTheme';
|
|
20
21
|
export * from './hooks/useTheme';
|
|
21
22
|
export * from './hooks/usePersistentState';
|
|
22
23
|
export * from './components/chip/Chip';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var useColorScheme = require('../hooks/useColorScheme');
|
|
5
|
+
var useTheme = require('./useTheme');
|
|
6
|
+
|
|
7
|
+
function useGetActiveTheme() {
|
|
8
|
+
const { theme } = useTheme.useTheme();
|
|
9
|
+
const { colorScheme } = useColorScheme.useColorScheme();
|
|
10
|
+
return { theme, colorScheme };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
exports.useGetActiveTheme = useGetActiveTheme;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ColorScheme } from '../utils/color-scheme.constants';
|
|
2
|
+
import type { ThemeId } from './useTheme';
|
|
3
|
+
export type { ColorScheme, ThemeId };
|
|
4
|
+
export interface UseGetActiveThemeResult {
|
|
5
|
+
theme: ThemeId | null;
|
|
6
|
+
colorScheme: ColorScheme | null;
|
|
7
|
+
}
|
|
8
|
+
export declare function useGetActiveTheme(): UseGetActiveThemeResult;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useColorScheme } from '../hooks/useColorScheme';
|
|
3
|
+
import { useTheme } from './useTheme';
|
|
4
|
+
|
|
5
|
+
function useGetActiveTheme() {
|
|
6
|
+
const { theme } = useTheme();
|
|
7
|
+
const { colorScheme } = useColorScheme();
|
|
8
|
+
return { theme, colorScheme };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { useGetActiveTheme };
|
package/dist/hooks/useTheme.cjs
CHANGED
|
@@ -47,15 +47,17 @@ function resolveInitialTheme(storageKey, defaultTheme) {
|
|
|
47
47
|
if (fromCookie) return fromCookie;
|
|
48
48
|
const fromStorage = localStorage_utils.readLocalStorage(storageKey);
|
|
49
49
|
if (fromStorage) return fromStorage;
|
|
50
|
-
return defaultTheme;
|
|
50
|
+
return defaultTheme != null ? defaultTheme : null;
|
|
51
51
|
}
|
|
52
52
|
function ensureResolved(defaultTheme, storageKey) {
|
|
53
53
|
if (resolved) return;
|
|
54
54
|
resolved = true;
|
|
55
55
|
const value = resolveInitialTheme(storageKey, defaultTheme);
|
|
56
|
-
applyTheme(value);
|
|
57
|
-
persistTheme(storageKey, value);
|
|
58
56
|
currentTheme = value;
|
|
57
|
+
if (value) {
|
|
58
|
+
applyTheme(value);
|
|
59
|
+
persistTheme(storageKey, value);
|
|
60
|
+
}
|
|
59
61
|
notify();
|
|
60
62
|
}
|
|
61
63
|
function ensureColorSchemeObserver() {
|
|
@@ -77,7 +79,7 @@ function switchThemeInternal(storageKey, id) {
|
|
|
77
79
|
resolved = true;
|
|
78
80
|
notify();
|
|
79
81
|
}
|
|
80
|
-
function useTheme(options) {
|
|
82
|
+
function useTheme(options = {}) {
|
|
81
83
|
const { defaultTheme, storageKey = theme_constants.THEME_STORAGE_KEY } = options;
|
|
82
84
|
react.useEffect(() => {
|
|
83
85
|
ensureColorSchemeObserver();
|
package/dist/hooks/useTheme.d.ts
CHANGED
|
@@ -2,10 +2,10 @@ import type { ThemeId } from '../styles/themes/types';
|
|
|
2
2
|
export type { ThemeId };
|
|
3
3
|
export interface UseThemeOptions {
|
|
4
4
|
/** The theme the app should fall back to when nothing is stored yet. */
|
|
5
|
-
defaultTheme
|
|
5
|
+
defaultTheme?: ThemeId;
|
|
6
6
|
storageKey?: string;
|
|
7
7
|
}
|
|
8
|
-
export declare function useTheme(options
|
|
8
|
+
export declare function useTheme(options?: UseThemeOptions): {
|
|
9
9
|
theme: ThemeId | null;
|
|
10
10
|
switchTheme: (id: ThemeId) => void;
|
|
11
11
|
};
|
package/dist/hooks/useTheme.js
CHANGED
|
@@ -45,15 +45,17 @@ function resolveInitialTheme(storageKey, defaultTheme) {
|
|
|
45
45
|
if (fromCookie) return fromCookie;
|
|
46
46
|
const fromStorage = readLocalStorage(storageKey);
|
|
47
47
|
if (fromStorage) return fromStorage;
|
|
48
|
-
return defaultTheme;
|
|
48
|
+
return defaultTheme != null ? defaultTheme : null;
|
|
49
49
|
}
|
|
50
50
|
function ensureResolved(defaultTheme, storageKey) {
|
|
51
51
|
if (resolved) return;
|
|
52
52
|
resolved = true;
|
|
53
53
|
const value = resolveInitialTheme(storageKey, defaultTheme);
|
|
54
|
-
applyTheme(value);
|
|
55
|
-
persistTheme(storageKey, value);
|
|
56
54
|
currentTheme = value;
|
|
55
|
+
if (value) {
|
|
56
|
+
applyTheme(value);
|
|
57
|
+
persistTheme(storageKey, value);
|
|
58
|
+
}
|
|
57
59
|
notify();
|
|
58
60
|
}
|
|
59
61
|
function ensureColorSchemeObserver() {
|
|
@@ -75,7 +77,7 @@ function switchThemeInternal(storageKey, id) {
|
|
|
75
77
|
resolved = true;
|
|
76
78
|
notify();
|
|
77
79
|
}
|
|
78
|
-
function useTheme(options) {
|
|
80
|
+
function useTheme(options = {}) {
|
|
79
81
|
const { defaultTheme, storageKey = THEME_STORAGE_KEY } = options;
|
|
80
82
|
useEffect(() => {
|
|
81
83
|
ensureColorSchemeObserver();
|