@djangocfg/ui-nextjs 2.1.283 → 2.1.285
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@djangocfg/ui-nextjs",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.285",
|
|
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",
|
|
@@ -85,11 +85,11 @@
|
|
|
85
85
|
"check": "tsc --noEmit"
|
|
86
86
|
},
|
|
87
87
|
"peerDependencies": {
|
|
88
|
-
"@djangocfg/api": "^2.1.
|
|
89
|
-
"@djangocfg/i18n": "^2.1.
|
|
90
|
-
"@djangocfg/nextjs": "^2.1.
|
|
91
|
-
"@djangocfg/ui-core": "^2.1.
|
|
92
|
-
"@djangocfg/ui-tools": "^2.1.
|
|
88
|
+
"@djangocfg/api": "^2.1.285",
|
|
89
|
+
"@djangocfg/i18n": "^2.1.285",
|
|
90
|
+
"@djangocfg/nextjs": "^2.1.285",
|
|
91
|
+
"@djangocfg/ui-core": "^2.1.285",
|
|
92
|
+
"@djangocfg/ui-tools": "^2.1.285",
|
|
93
93
|
"@types/react": "^19.1.0",
|
|
94
94
|
"@types/react-dom": "^19.1.0",
|
|
95
95
|
"consola": "^3.4.2",
|
|
@@ -112,7 +112,7 @@
|
|
|
112
112
|
"react-chartjs-2": "^5.3.0"
|
|
113
113
|
},
|
|
114
114
|
"devDependencies": {
|
|
115
|
-
"@djangocfg/typescript-config": "^2.1.
|
|
115
|
+
"@djangocfg/typescript-config": "^2.1.285",
|
|
116
116
|
"@radix-ui/react-dropdown-menu": "^2.1.16",
|
|
117
117
|
"@radix-ui/react-slot": "^1.2.4",
|
|
118
118
|
"@types/node": "^24.7.2",
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
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
|
+
}
|
|
@@ -27,17 +27,34 @@ import { useEffect, useState } from 'react';
|
|
|
27
27
|
import { Button } from '@djangocfg/ui-core/components';
|
|
28
28
|
import { useIsMobile } from '@djangocfg/ui-core/hooks';
|
|
29
29
|
import { cn } from '@djangocfg/ui-core/lib';
|
|
30
|
+
import { useForcedTheme } from './ForcedThemeContext';
|
|
30
31
|
import { useThemeContext } from './ThemeProvider';
|
|
31
32
|
|
|
33
|
+
export type ThemeLockBehavior = 'disable' | 'hide';
|
|
34
|
+
|
|
32
35
|
export interface ThemeToggleProps {
|
|
33
36
|
/** Custom className */
|
|
34
37
|
className?: string;
|
|
35
38
|
/** Size variant (auto-detects mobile if not specified) */
|
|
36
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;
|
|
37
48
|
}
|
|
38
49
|
|
|
39
|
-
export function ThemeToggle({
|
|
50
|
+
export function ThemeToggle({
|
|
51
|
+
className,
|
|
52
|
+
size = 'auto',
|
|
53
|
+
lockedBehavior = 'disable',
|
|
54
|
+
lockedTitle = 'Theme is set for this page',
|
|
55
|
+
}: ThemeToggleProps) {
|
|
40
56
|
const { resolvedTheme, toggleTheme } = useThemeContext();
|
|
57
|
+
const forcedTheme = useForcedTheme();
|
|
41
58
|
const [mounted, setMounted] = useState(false);
|
|
42
59
|
const isMobile = useIsMobile();
|
|
43
60
|
|
|
@@ -46,11 +63,20 @@ export function ThemeToggle({ className, size = 'auto' }: ThemeToggleProps) {
|
|
|
46
63
|
setMounted(true);
|
|
47
64
|
}, []);
|
|
48
65
|
|
|
66
|
+
const isLocked = Boolean(forcedTheme);
|
|
49
67
|
// Determine actual size based on prop and screen size
|
|
50
68
|
const actualSize = size === 'auto' ? (isMobile ? 'compact' : 'default') : size;
|
|
51
69
|
const buttonSize = actualSize === 'compact' ? 'h-8 w-8' : 'h-9 w-9';
|
|
52
70
|
const iconSize = actualSize === 'compact' ? 'h-3.5 w-3.5' : 'h-4 w-4';
|
|
53
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
|
+
|
|
54
80
|
if (!mounted) {
|
|
55
81
|
return (
|
|
56
82
|
<Button
|
|
@@ -69,15 +95,14 @@ export function ThemeToggle({ className, size = 'auto' }: ThemeToggleProps) {
|
|
|
69
95
|
<Button
|
|
70
96
|
variant="ghost"
|
|
71
97
|
size="icon"
|
|
72
|
-
onClick={toggleTheme}
|
|
73
|
-
|
|
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)}
|
|
74
103
|
>
|
|
75
|
-
{
|
|
76
|
-
|
|
77
|
-
) : (
|
|
78
|
-
<Moon className={iconSize} />
|
|
79
|
-
)}
|
|
80
|
-
<span className="sr-only">Toggle theme</span>
|
|
104
|
+
{showSun ? <Sun className={iconSize} /> : <Moon className={iconSize} />}
|
|
105
|
+
<span className="sr-only">{isLocked ? lockedTitle : 'Toggle theme'}</span>
|
|
81
106
|
</Button>
|
|
82
107
|
);
|
|
83
108
|
}
|
package/src/theme/index.ts
CHANGED
|
@@ -4,3 +4,6 @@ export { ThemeProvider, useThemeContext } from './ThemeProvider';
|
|
|
4
4
|
export type { Theme, ThemeProviderProps } from './ThemeProvider';
|
|
5
5
|
export { ThemeToggle } from './ThemeToggle';
|
|
6
6
|
export { ForceTheme } from './ForceTheme';
|
|
7
|
+
export { ThemeOverride, resolveForcedTheme } from './ThemeOverride';
|
|
8
|
+
export type { ThemeOverrideRule, ThemeOverrideProps, ForcedTheme } from './ThemeOverride';
|
|
9
|
+
export { ForcedThemeProvider, useForcedTheme } from './ForcedThemeContext';
|