@djangocfg/ui-nextjs 2.1.404 → 2.1.408
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 +9 -3
- package/package.json +7 -7
- package/src/theme/index.ts +24 -7
- package/src/theme/ForceTheme.tsx +0 -116
- package/src/theme/ForcedThemeContext.tsx +0 -45
- package/src/theme/ThemeOverride.tsx +0 -163
- package/src/theme/ThemeProvider.tsx +0 -77
- package/src/theme/ThemeToggle.tsx +0 -108
package/README.md
CHANGED
|
@@ -6,10 +6,12 @@
|
|
|
6
6
|
|
|
7
7
|
# @djangocfg/ui-nextjs
|
|
8
8
|
|
|
9
|
-
Next-specific runtime for DjangoCFG:
|
|
9
|
+
Next-specific runtime for DjangoCFG: PWA install support and browser-storage hooks.
|
|
10
10
|
|
|
11
11
|
UI primitives (Button, Card, Sidebar, Pagination, Breadcrumb, etc.) live in [`@djangocfg/ui-core`](../ui-core) — they're framework-agnostic and don't belong here. Import them from there directly.
|
|
12
12
|
|
|
13
|
+
> **Theme moved.** `ThemeProvider`, `ThemeToggle`, `ForceTheme`, `useThemeContext` are framework-agnostic and now live in [`@djangocfg/ui-core`](../ui-core). The `@djangocfg/ui-nextjs/theme` subpath still re-exports them for backward compatibility, but new code should import from `@djangocfg/ui-core`.
|
|
14
|
+
|
|
13
15
|
**Part of [DjangoCFG](https://djangocfg.com).**
|
|
14
16
|
|
|
15
17
|
## Install
|
|
@@ -22,7 +24,7 @@ pnpm add @djangocfg/ui-nextjs
|
|
|
22
24
|
|
|
23
25
|
| Subpath | Purpose |
|
|
24
26
|
|---|---|
|
|
25
|
-
| `@djangocfg/ui-nextjs/theme` |
|
|
27
|
+
| `@djangocfg/ui-nextjs/theme` | Back-compat re-export of the theme runtime — now defined in `@djangocfg/ui-core` |
|
|
26
28
|
| `@djangocfg/ui-nextjs/pwa` | `PwaProvider`, `A2HSHint`, `useInstall`, `useIsPWA`, install detection helpers |
|
|
27
29
|
| `@djangocfg/ui-nextjs/hooks` | `useLocalStorage`, `useSessionStorage` and other Next-runtime-only hooks |
|
|
28
30
|
|
|
@@ -30,8 +32,12 @@ That's it. Anything else you might be looking for is in `@djangocfg/ui-core`.
|
|
|
30
32
|
|
|
31
33
|
## Theme
|
|
32
34
|
|
|
35
|
+
The theme runtime lives in `@djangocfg/ui-core` (it is framework-agnostic).
|
|
36
|
+
The `/theme` subpath here re-exports it so existing imports keep working.
|
|
37
|
+
|
|
33
38
|
```tsx
|
|
34
|
-
|
|
39
|
+
// Preferred — import directly from ui-core:
|
|
40
|
+
import { ThemeProvider, ThemeToggle } from '@djangocfg/ui-core';
|
|
35
41
|
|
|
36
42
|
<ThemeProvider>
|
|
37
43
|
{children}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@djangocfg/ui-nextjs",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.408",
|
|
4
4
|
"description": "Next.js UI component library with Radix UI primitives, Tailwind CSS styling, charts, and form components",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ui-components",
|
|
@@ -75,11 +75,11 @@
|
|
|
75
75
|
"check": "tsc --noEmit"
|
|
76
76
|
},
|
|
77
77
|
"peerDependencies": {
|
|
78
|
-
"@djangocfg/api": "^2.1.
|
|
79
|
-
"@djangocfg/i18n": "^2.1.
|
|
80
|
-
"@djangocfg/nextjs": "^2.1.
|
|
81
|
-
"@djangocfg/ui-core": "^2.1.
|
|
82
|
-
"@djangocfg/ui-tools": "^2.1.
|
|
78
|
+
"@djangocfg/api": "^2.1.408",
|
|
79
|
+
"@djangocfg/i18n": "^2.1.408",
|
|
80
|
+
"@djangocfg/nextjs": "^2.1.408",
|
|
81
|
+
"@djangocfg/ui-core": "^2.1.408",
|
|
82
|
+
"@djangocfg/ui-tools": "^2.1.408",
|
|
83
83
|
"@types/react": "^19.1.0",
|
|
84
84
|
"@types/react-dom": "^19.1.0",
|
|
85
85
|
"consola": "^3.4.2",
|
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
"react-chartjs-2": "^5.3.0"
|
|
103
103
|
},
|
|
104
104
|
"devDependencies": {
|
|
105
|
-
"@djangocfg/typescript-config": "^2.1.
|
|
105
|
+
"@djangocfg/typescript-config": "^2.1.408",
|
|
106
106
|
"@radix-ui/react-dropdown-menu": "^2.1.16",
|
|
107
107
|
"@radix-ui/react-slot": "^1.2.4",
|
|
108
108
|
"@types/node": "^24.7.2",
|
package/src/theme/index.ts
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Theme system — moved to @djangocfg/ui-core/theme (framework-agnostic).
|
|
3
|
+
* This module re-exports it for backward compatibility.
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
'use client';
|
|
2
7
|
|
|
3
|
-
export {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
export {
|
|
9
|
+
ThemeProvider,
|
|
10
|
+
useThemeContext,
|
|
11
|
+
ThemeToggle,
|
|
12
|
+
ForceTheme,
|
|
13
|
+
ThemeOverride,
|
|
14
|
+
resolveForcedTheme,
|
|
15
|
+
ForcedThemeProvider,
|
|
16
|
+
useForcedTheme,
|
|
17
|
+
} from '@djangocfg/ui-core/theme';
|
|
18
|
+
export type {
|
|
19
|
+
Theme,
|
|
20
|
+
ThemeProviderProps,
|
|
21
|
+
ThemeToggleProps,
|
|
22
|
+
ThemeLockBehavior,
|
|
23
|
+
ThemeOverrideRule,
|
|
24
|
+
ThemeOverrideProps,
|
|
25
|
+
ForcedTheme,
|
|
26
|
+
} from '@djangocfg/ui-core/theme';
|
package/src/theme/ForceTheme.tsx
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ForceTheme - Force a specific theme for a section
|
|
3
|
-
*
|
|
4
|
-
* Wraps content to override the global theme setting.
|
|
5
|
-
* Works by adding both the theme class and inline CSS variables
|
|
6
|
-
* to ensure proper theme application regardless of parent context.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
'use client';
|
|
10
|
-
|
|
11
|
-
import React, { ReactNode } from 'react';
|
|
12
|
-
|
|
13
|
-
import { cn } from '@djangocfg/ui-core/lib';
|
|
14
|
-
|
|
15
|
-
interface ForceThemeProps {
|
|
16
|
-
theme: 'light' | 'dark';
|
|
17
|
-
children: ReactNode;
|
|
18
|
-
className?: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// Dark theme CSS variables
|
|
22
|
-
const darkThemeVars = {
|
|
23
|
-
// Base HSL values
|
|
24
|
-
'--background': '0 0% 4%',
|
|
25
|
-
'--foreground': '0 0% 98%',
|
|
26
|
-
'--card': '0 0% 8%',
|
|
27
|
-
'--card-foreground': '0 0% 98%',
|
|
28
|
-
'--popover': '0 0% 12%',
|
|
29
|
-
'--popover-foreground': '0 0% 98%',
|
|
30
|
-
'--primary': '217 91% 60%',
|
|
31
|
-
'--primary-foreground': '0 0% 100%',
|
|
32
|
-
'--secondary': '0 0% 98%',
|
|
33
|
-
'--secondary-foreground': '0 0% 9%',
|
|
34
|
-
'--muted': '0 0% 10%',
|
|
35
|
-
'--muted-foreground': '0 0% 60%',
|
|
36
|
-
'--accent': '0 0% 15%',
|
|
37
|
-
'--accent-foreground': '0 0% 98%',
|
|
38
|
-
'--destructive': '0 84% 60%',
|
|
39
|
-
'--destructive-foreground': '0 0% 98%',
|
|
40
|
-
'--border': '0 0% 15%',
|
|
41
|
-
'--input': '0 0% 15%',
|
|
42
|
-
'--ring': '217 91% 60%',
|
|
43
|
-
// Tailwind color tokens (used by bg-*, text-*, etc)
|
|
44
|
-
'--color-background': 'hsl(0 0% 4%)',
|
|
45
|
-
'--color-foreground': 'hsl(0 0% 98%)',
|
|
46
|
-
'--color-card': 'hsl(0 0% 8%)',
|
|
47
|
-
'--color-card-foreground': 'hsl(0 0% 98%)',
|
|
48
|
-
'--color-primary': 'hsl(217 91% 60%)',
|
|
49
|
-
'--color-primary-foreground': 'hsl(0 0% 100%)',
|
|
50
|
-
'--color-secondary': 'hsl(0 0% 98%)',
|
|
51
|
-
'--color-secondary-foreground': 'hsl(0 0% 9%)',
|
|
52
|
-
'--color-muted': 'hsl(0 0% 10%)',
|
|
53
|
-
'--color-muted-foreground': 'hsl(0 0% 60%)',
|
|
54
|
-
'--color-accent': 'hsl(0 0% 15%)',
|
|
55
|
-
'--color-accent-foreground': 'hsl(0 0% 98%)',
|
|
56
|
-
'--color-destructive': 'hsl(0 84% 60%)',
|
|
57
|
-
'--color-destructive-foreground': 'hsl(0 0% 98%)',
|
|
58
|
-
'--color-border': 'hsl(0 0% 15%)',
|
|
59
|
-
'--color-input': 'hsl(0 0% 15%)',
|
|
60
|
-
'--color-ring': 'hsl(217 91% 60%)',
|
|
61
|
-
} as React.CSSProperties;
|
|
62
|
-
|
|
63
|
-
// Light theme CSS variables
|
|
64
|
-
const lightThemeVars = {
|
|
65
|
-
// Base HSL values
|
|
66
|
-
'--background': '0 0% 96%',
|
|
67
|
-
'--foreground': '0 0% 9%',
|
|
68
|
-
'--card': '0 0% 100%',
|
|
69
|
-
'--card-foreground': '0 0% 9%',
|
|
70
|
-
'--popover': '0 0% 100%',
|
|
71
|
-
'--popover-foreground': '0 0% 9%',
|
|
72
|
-
'--primary': '217 91% 60%',
|
|
73
|
-
'--primary-foreground': '0 0% 100%',
|
|
74
|
-
'--secondary': '0 0% 9%',
|
|
75
|
-
'--secondary-foreground': '0 0% 98%',
|
|
76
|
-
'--muted': '0 0% 96%',
|
|
77
|
-
'--muted-foreground': '0 0% 40%',
|
|
78
|
-
'--accent': '0 0% 92%',
|
|
79
|
-
'--accent-foreground': '0 0% 9%',
|
|
80
|
-
'--destructive': '0 84% 60%',
|
|
81
|
-
'--destructive-foreground': '0 0% 98%',
|
|
82
|
-
'--border': '0 0% 90%',
|
|
83
|
-
'--input': '0 0% 90%',
|
|
84
|
-
'--ring': '217 91% 60%',
|
|
85
|
-
// Tailwind color tokens (used by bg-*, text-*, etc)
|
|
86
|
-
'--color-background': 'hsl(0 0% 96%)',
|
|
87
|
-
'--color-foreground': 'hsl(0 0% 9%)',
|
|
88
|
-
'--color-card': 'hsl(0 0% 100%)',
|
|
89
|
-
'--color-card-foreground': 'hsl(0 0% 9%)',
|
|
90
|
-
'--color-primary': 'hsl(217 91% 60%)',
|
|
91
|
-
'--color-primary-foreground': 'hsl(0 0% 100%)',
|
|
92
|
-
'--color-secondary': 'hsl(0 0% 9%)',
|
|
93
|
-
'--color-secondary-foreground': 'hsl(0 0% 98%)',
|
|
94
|
-
'--color-muted': 'hsl(0 0% 96%)',
|
|
95
|
-
'--color-muted-foreground': 'hsl(0 0% 40%)',
|
|
96
|
-
'--color-accent': 'hsl(0 0% 92%)',
|
|
97
|
-
'--color-accent-foreground': 'hsl(0 0% 9%)',
|
|
98
|
-
'--color-destructive': 'hsl(0 84% 60%)',
|
|
99
|
-
'--color-destructive-foreground': 'hsl(0 0% 98%)',
|
|
100
|
-
'--color-border': 'hsl(0 0% 90%)',
|
|
101
|
-
'--color-input': 'hsl(0 0% 90%)',
|
|
102
|
-
'--color-ring': 'hsl(217 91% 60%)',
|
|
103
|
-
} as React.CSSProperties;
|
|
104
|
-
|
|
105
|
-
export function ForceTheme({ theme, children, className }: ForceThemeProps) {
|
|
106
|
-
const themeVars = theme === 'dark' ? darkThemeVars : lightThemeVars;
|
|
107
|
-
|
|
108
|
-
return (
|
|
109
|
-
<div
|
|
110
|
-
className={cn(theme, className)}
|
|
111
|
-
style={themeVars}
|
|
112
|
-
>
|
|
113
|
-
{children}
|
|
114
|
-
</div>
|
|
115
|
-
);
|
|
116
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ForcedThemeContext
|
|
3
|
-
*
|
|
4
|
-
* Published by `ThemeOverride` so downstream UI (theme switchers, toggles,
|
|
5
|
-
* indicators) can tell whether the current route forces a specific theme and
|
|
6
|
-
* adapt accordingly — typically by disabling or hiding their controls.
|
|
7
|
-
*
|
|
8
|
-
* Default value is `null` (no override) — consumers outside a `ThemeOverride`
|
|
9
|
-
* mount see the same shape as consumers on an unforced route, so there's no
|
|
10
|
-
* need to guard against the provider being absent.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
'use client';
|
|
14
|
-
|
|
15
|
-
import { createContext, useContext, type ReactNode } from 'react';
|
|
16
|
-
|
|
17
|
-
import type { ForcedTheme } from './ThemeOverride';
|
|
18
|
-
|
|
19
|
-
const ForcedThemeContext = createContext<ForcedTheme | null>(null);
|
|
20
|
-
|
|
21
|
-
interface ForcedThemeProviderProps {
|
|
22
|
-
value: ForcedTheme | null;
|
|
23
|
-
children: ReactNode;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function ForcedThemeProvider({ value, children }: ForcedThemeProviderProps) {
|
|
27
|
-
return (
|
|
28
|
-
<ForcedThemeContext.Provider value={value}>
|
|
29
|
-
{children}
|
|
30
|
-
</ForcedThemeContext.Provider>
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Read the currently-forced theme. `null` means the user is free to choose.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```tsx
|
|
39
|
-
* const forced = useForcedTheme();
|
|
40
|
-
* if (forced) return <p>Theme is locked to {forced} on this page.</p>;
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export function useForcedTheme(): ForcedTheme | null {
|
|
44
|
-
return useContext(ForcedThemeContext);
|
|
45
|
-
}
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ThemeOverride - Pathname-aware forced theme
|
|
3
|
-
*
|
|
4
|
-
* Applies a forced `light` or `dark` theme via `next-themes` while the current
|
|
5
|
-
* pathname matches any of the supplied rules, and restores the user's prior
|
|
6
|
-
* choice when the pathname no longer matches.
|
|
7
|
-
*
|
|
8
|
-
* Unlike `ForceTheme` (which scopes CSS vars to a wrapper div), this component
|
|
9
|
-
* mutates the real `next-themes` value — so the `html.dark` class is applied
|
|
10
|
-
* and every surface (navbar, footer, body, page) picks the new theme up
|
|
11
|
-
* consistently.
|
|
12
|
-
*
|
|
13
|
-
* The matcher is **dependency-free** and understands:
|
|
14
|
-
* - Exact match (`/` matches `/`)
|
|
15
|
-
* - Prefix match (`/docs` matches `/docs/getting-started`)
|
|
16
|
-
* - Glob wildcards (`*` for a single segment, `**` for any depth)
|
|
17
|
-
*
|
|
18
|
-
* Consumers pass the pathname already stripped of the locale prefix — matching
|
|
19
|
-
* is not locale-aware here.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```tsx
|
|
23
|
-
* <ThemeOverride
|
|
24
|
-
* pathname={pathname}
|
|
25
|
-
* rules={[
|
|
26
|
-
* { path: '/', theme: 'dark' },
|
|
27
|
-
* { path: ['/legal', '/legal/**'], theme: 'light' },
|
|
28
|
-
* ]}
|
|
29
|
-
* />
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
'use client';
|
|
34
|
-
|
|
35
|
-
import { useEffect, useMemo, useRef } from 'react';
|
|
36
|
-
|
|
37
|
-
import { useThemeContext } from './ThemeProvider';
|
|
38
|
-
|
|
39
|
-
import type { Theme } from './ThemeProvider';
|
|
40
|
-
|
|
41
|
-
export type ForcedTheme = Exclude<Theme, 'system'>;
|
|
42
|
-
|
|
43
|
-
export interface ThemeOverrideRule {
|
|
44
|
-
/** Path (or array of paths) to match against. Supports `*` and `**` globs. */
|
|
45
|
-
path: string | string[];
|
|
46
|
-
/** Theme to apply while the path matches. */
|
|
47
|
-
theme: ForcedTheme;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface ThemeOverrideProps {
|
|
51
|
-
/** Pathname stripped of locale prefix. Use `usePathnameWithoutLocale()` if you have it. */
|
|
52
|
-
pathname: string;
|
|
53
|
-
/** Rules evaluated top-to-bottom — first match wins. */
|
|
54
|
-
rules?: ThemeOverrideRule[];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Resolve the first matching rule's forced theme for a pathname, or `null`.
|
|
59
|
-
* Pure helper — use this to render a `ForcedThemeProvider` in the same tree
|
|
60
|
-
* where you mount `ThemeOverride`.
|
|
61
|
-
*/
|
|
62
|
-
export function resolveForcedTheme(
|
|
63
|
-
pathname: string,
|
|
64
|
-
rules?: ThemeOverrideRule[],
|
|
65
|
-
): ForcedTheme | null {
|
|
66
|
-
if (!rules || rules.length === 0) return null;
|
|
67
|
-
for (const rule of rules) {
|
|
68
|
-
if (matchesAny(pathname, rule.path)) return rule.theme;
|
|
69
|
-
}
|
|
70
|
-
return null;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// --- inlined matcher (no dep on @djangocfg/layouts) -------------------------
|
|
74
|
-
|
|
75
|
-
function matchGlob(pathname: string, pattern: string): boolean {
|
|
76
|
-
const pathParts = pathname.replace(/\/+$/, '').split('/').filter(Boolean);
|
|
77
|
-
const patternParts = pattern.replace(/\/+$/, '').split('/').filter(Boolean);
|
|
78
|
-
|
|
79
|
-
let pi = 0;
|
|
80
|
-
let ti = 0;
|
|
81
|
-
|
|
82
|
-
while (ti < patternParts.length && pi < pathParts.length) {
|
|
83
|
-
const pat = patternParts[ti];
|
|
84
|
-
if (pat === '**') {
|
|
85
|
-
if (ti === patternParts.length - 1) return true;
|
|
86
|
-
const next = patternParts[ti + 1];
|
|
87
|
-
while (pi < pathParts.length) {
|
|
88
|
-
if (pathParts[pi] === next || next === '*') break;
|
|
89
|
-
pi++;
|
|
90
|
-
}
|
|
91
|
-
ti++;
|
|
92
|
-
} else if (pat === '*') {
|
|
93
|
-
pi++;
|
|
94
|
-
ti++;
|
|
95
|
-
} else {
|
|
96
|
-
if (pathParts[pi] !== pat) return false;
|
|
97
|
-
pi++;
|
|
98
|
-
ti++;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (ti < patternParts.length) {
|
|
103
|
-
for (let i = ti; i < patternParts.length; i++) {
|
|
104
|
-
if (patternParts[i] !== '**') return false;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return pi === pathParts.length;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function matchOne(pathname: string, pattern: string): boolean {
|
|
112
|
-
const p = pathname.length > 1 ? pathname.replace(/\/+$/, '') : pathname;
|
|
113
|
-
const q = pattern.length > 1 ? pattern.replace(/\/+$/, '') : pattern;
|
|
114
|
-
if (q.includes('*')) return matchGlob(p, q);
|
|
115
|
-
return p === q || p.startsWith(q + '/');
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function matchesAny(pathname: string, path: string | string[]): boolean {
|
|
119
|
-
return Array.isArray(path) ? path.some((p) => matchOne(pathname, p)) : matchOne(pathname, path);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// --- component --------------------------------------------------------------
|
|
123
|
-
|
|
124
|
-
export function ThemeOverride({ pathname, rules }: ThemeOverrideProps) {
|
|
125
|
-
const { theme, setTheme } = useThemeContext();
|
|
126
|
-
|
|
127
|
-
// First rule whose path matches the current pathname. null = no override.
|
|
128
|
-
const forced = useMemo<ForcedTheme | null>(
|
|
129
|
-
() => resolveForcedTheme(pathname, rules),
|
|
130
|
-
[pathname, rules],
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
// Stash the user's own pick so we can restore it when they leave a forced route.
|
|
134
|
-
// We capture only non-override theme values (from renders where `forced === null`).
|
|
135
|
-
// Stored as a ref so it doesn't drive the effect.
|
|
136
|
-
const userThemeRef = useRef<string | undefined>(undefined);
|
|
137
|
-
|
|
138
|
-
// Keep userThemeRef in sync with what next-themes reports **while not forcing**.
|
|
139
|
-
// Every render where the route isn't overridden, the current `theme` IS the user's
|
|
140
|
-
// own pick — snapshot it. During a forced render we leave the ref alone so the
|
|
141
|
-
// original value survives until we hand control back.
|
|
142
|
-
useEffect(() => {
|
|
143
|
-
if (forced === null && theme) {
|
|
144
|
-
userThemeRef.current = theme;
|
|
145
|
-
}
|
|
146
|
-
}, [forced, theme]);
|
|
147
|
-
|
|
148
|
-
// Apply / revert the forced theme. Intentionally driven only by `forced` — the
|
|
149
|
-
// user may toggle the theme manually while forcing is off, and that's fine
|
|
150
|
-
// (the sync effect above records the new value).
|
|
151
|
-
useEffect(() => {
|
|
152
|
-
if (forced) {
|
|
153
|
-
setTheme(forced);
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
const restore = userThemeRef.current ?? 'system';
|
|
157
|
-
setTheme(restore);
|
|
158
|
-
// setTheme from next-themes is stable by ref; omit on purpose.
|
|
159
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
160
|
-
}, [forced]);
|
|
161
|
-
|
|
162
|
-
return null;
|
|
163
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ThemeProvider - Universal theme management
|
|
3
|
-
*
|
|
4
|
-
* Re-exports next-themes ThemeProvider with sensible defaults.
|
|
5
|
-
* Supports light, dark, and system themes with localStorage persistence.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```tsx
|
|
9
|
-
* // In app/layout.tsx
|
|
10
|
-
* import { ThemeProvider } from '@djangocfg/ui-nextjs';
|
|
11
|
-
*
|
|
12
|
-
* <ThemeProvider
|
|
13
|
-
* attribute="class"
|
|
14
|
-
* defaultTheme="system"
|
|
15
|
-
* enableSystem
|
|
16
|
-
* >
|
|
17
|
-
* {children}
|
|
18
|
-
* </ThemeProvider>
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
'use client';
|
|
23
|
-
|
|
24
|
-
import { ThemeProvider as NextThemesProvider, useTheme as useNextTheme } from 'next-themes';
|
|
25
|
-
|
|
26
|
-
import type { ThemeProviderProps as NextThemesProviderProps } from 'next-themes';
|
|
27
|
-
|
|
28
|
-
// Re-export types
|
|
29
|
-
export type Theme = 'light' | 'dark' | 'system';
|
|
30
|
-
export type ThemeProviderProps = NextThemesProviderProps;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* ThemeProvider wraps next-themes with sensible defaults
|
|
34
|
-
*/
|
|
35
|
-
export function ThemeProvider({
|
|
36
|
-
children,
|
|
37
|
-
attribute = 'class',
|
|
38
|
-
defaultTheme = 'system',
|
|
39
|
-
enableSystem = true,
|
|
40
|
-
disableTransitionOnChange = true,
|
|
41
|
-
...props
|
|
42
|
-
}: ThemeProviderProps) {
|
|
43
|
-
return (
|
|
44
|
-
<NextThemesProvider
|
|
45
|
-
attribute={attribute}
|
|
46
|
-
defaultTheme={defaultTheme}
|
|
47
|
-
enableSystem={enableSystem}
|
|
48
|
-
disableTransitionOnChange={disableTransitionOnChange}
|
|
49
|
-
{...props}
|
|
50
|
-
>
|
|
51
|
-
{children}
|
|
52
|
-
</NextThemesProvider>
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Hook to access theme context
|
|
58
|
-
* Returns theme, setTheme, resolvedTheme, systemTheme, and themes
|
|
59
|
-
*/
|
|
60
|
-
export function useThemeContext() {
|
|
61
|
-
const { theme, setTheme, resolvedTheme, systemTheme, themes } = useNextTheme();
|
|
62
|
-
|
|
63
|
-
const toggleTheme = () => {
|
|
64
|
-
// Toggle between light and dark (ignore system)
|
|
65
|
-
const currentResolved = resolvedTheme || 'light';
|
|
66
|
-
setTheme(currentResolved === 'light' ? 'dark' : 'light');
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
return {
|
|
70
|
-
theme: theme as Theme | undefined,
|
|
71
|
-
setTheme,
|
|
72
|
-
resolvedTheme: resolvedTheme as 'light' | 'dark' | undefined,
|
|
73
|
-
systemTheme: systemTheme as 'light' | 'dark' | undefined,
|
|
74
|
-
themes,
|
|
75
|
-
toggleTheme,
|
|
76
|
-
};
|
|
77
|
-
}
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ThemeToggle - Theme switcher component
|
|
3
|
-
*
|
|
4
|
-
* Switches between light and dark themes.
|
|
5
|
-
* Must be used within ThemeProvider.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```tsx
|
|
9
|
-
* import { ThemeToggle } from '@djangocfg/ui-nextjs';
|
|
10
|
-
*
|
|
11
|
-
* // Default
|
|
12
|
-
* <ThemeToggle />
|
|
13
|
-
*
|
|
14
|
-
* // Compact (for mobile/tight spaces)
|
|
15
|
-
* <ThemeToggle size="compact" />
|
|
16
|
-
*
|
|
17
|
-
* // Custom className
|
|
18
|
-
* <ThemeToggle className="ml-auto" />
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
'use client';
|
|
23
|
-
|
|
24
|
-
import { Moon, Sun } from 'lucide-react';
|
|
25
|
-
import { useEffect, useState } from 'react';
|
|
26
|
-
|
|
27
|
-
import { Button } from '@djangocfg/ui-core/components';
|
|
28
|
-
import { useIsMobile } from '@djangocfg/ui-core/hooks';
|
|
29
|
-
import { cn } from '@djangocfg/ui-core/lib';
|
|
30
|
-
import { useForcedTheme } from './ForcedThemeContext';
|
|
31
|
-
import { useThemeContext } from './ThemeProvider';
|
|
32
|
-
|
|
33
|
-
export type ThemeLockBehavior = 'disable' | 'hide';
|
|
34
|
-
|
|
35
|
-
export interface ThemeToggleProps {
|
|
36
|
-
/** Custom className */
|
|
37
|
-
className?: string;
|
|
38
|
-
/** Size variant (auto-detects mobile if not specified) */
|
|
39
|
-
size?: 'default' | 'compact' | 'auto';
|
|
40
|
-
/**
|
|
41
|
-
* What to do when the current route forces a theme via `ThemeOverride`.
|
|
42
|
-
* - `disable` (default): render the toggle disabled with a tooltip.
|
|
43
|
-
* - `hide`: render nothing.
|
|
44
|
-
*/
|
|
45
|
-
lockedBehavior?: ThemeLockBehavior;
|
|
46
|
-
/** Title shown when the control is locked. */
|
|
47
|
-
lockedTitle?: string;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function ThemeToggle({
|
|
51
|
-
className,
|
|
52
|
-
size = 'auto',
|
|
53
|
-
lockedBehavior = 'disable',
|
|
54
|
-
lockedTitle = 'Theme is set for this page',
|
|
55
|
-
}: ThemeToggleProps) {
|
|
56
|
-
const { resolvedTheme, toggleTheme } = useThemeContext();
|
|
57
|
-
const forcedTheme = useForcedTheme();
|
|
58
|
-
const [mounted, setMounted] = useState(false);
|
|
59
|
-
const isMobile = useIsMobile();
|
|
60
|
-
|
|
61
|
-
// Prevent hydration mismatch
|
|
62
|
-
useEffect(() => {
|
|
63
|
-
setMounted(true);
|
|
64
|
-
}, []);
|
|
65
|
-
|
|
66
|
-
const isLocked = Boolean(forcedTheme);
|
|
67
|
-
// Determine actual size based on prop and screen size
|
|
68
|
-
const actualSize = size === 'auto' ? (isMobile ? 'compact' : 'default') : size;
|
|
69
|
-
const buttonSize = actualSize === 'compact' ? 'h-8 w-8' : 'h-9 w-9';
|
|
70
|
-
const iconSize = actualSize === 'compact' ? 'h-3.5 w-3.5' : 'h-4 w-4';
|
|
71
|
-
|
|
72
|
-
// The icon always reflects what's on screen (forced or not) — that is
|
|
73
|
-
// `resolvedTheme`. When locked, it shows the forced theme's icon.
|
|
74
|
-
const showSun = mounted ? resolvedTheme === 'light' : true;
|
|
75
|
-
|
|
76
|
-
if (isLocked && lockedBehavior === 'hide') {
|
|
77
|
-
return null;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (!mounted) {
|
|
81
|
-
return (
|
|
82
|
-
<Button
|
|
83
|
-
variant="ghost"
|
|
84
|
-
size="icon"
|
|
85
|
-
className={cn(buttonSize, className)}
|
|
86
|
-
disabled
|
|
87
|
-
>
|
|
88
|
-
<Sun className={iconSize} />
|
|
89
|
-
<span className="sr-only">Toggle theme</span>
|
|
90
|
-
</Button>
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return (
|
|
95
|
-
<Button
|
|
96
|
-
variant="ghost"
|
|
97
|
-
size="icon"
|
|
98
|
-
onClick={isLocked ? undefined : toggleTheme}
|
|
99
|
-
disabled={isLocked}
|
|
100
|
-
title={isLocked ? lockedTitle : undefined}
|
|
101
|
-
aria-label={isLocked ? lockedTitle : 'Toggle theme'}
|
|
102
|
-
className={cn(buttonSize, isLocked && 'cursor-not-allowed opacity-60', className)}
|
|
103
|
-
>
|
|
104
|
-
{showSun ? <Sun className={iconSize} /> : <Moon className={iconSize} />}
|
|
105
|
-
<span className="sr-only">{isLocked ? lockedTitle : 'Toggle theme'}</span>
|
|
106
|
-
</Button>
|
|
107
|
-
);
|
|
108
|
-
}
|