@xforge-dev/forge-kit 0.22.0 → 0.22.1
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/theme-provider.js +8 -4
- package/dist/theme.d.ts +8 -0
- package/dist/theme.js +15 -0
- package/package.json +1 -1
package/dist/theme-provider.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
3
|
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
-
import { DEFAULT_THEME, THEME_COLOR_KEY, THEME_MODE_KEY, THEME_SYNC_KEY, isColorTheme, isThemeMode, parseThemeCookie,
|
|
4
|
+
import { DEFAULT_THEME, hostThemeCookieExpiry, THEME_COLOR_KEY, THEME_MODE_KEY, THEME_SYNC_KEY, isColorTheme, isThemeMode, parseThemeCookie, writeThemeCookie, } from './theme.js';
|
|
5
5
|
const Context = createContext(undefined);
|
|
6
6
|
function safeStorage() {
|
|
7
7
|
try {
|
|
@@ -39,7 +39,7 @@ export function storeForgeTheme(theme) {
|
|
|
39
39
|
storage?.setItem(THEME_MODE_KEY, theme.mode);
|
|
40
40
|
storage?.setItem(THEME_COLOR_KEY, theme.color);
|
|
41
41
|
if (readSync(storage))
|
|
42
|
-
|
|
42
|
+
writeThemeCookie(theme);
|
|
43
43
|
}
|
|
44
44
|
function apply(theme) {
|
|
45
45
|
const root = document.documentElement;
|
|
@@ -66,7 +66,7 @@ export function ForgeThemeProvider({ children, onChange }) {
|
|
|
66
66
|
setThemeState(next);
|
|
67
67
|
apply(next);
|
|
68
68
|
if (share && readSync(storage))
|
|
69
|
-
|
|
69
|
+
writeThemeCookie(next);
|
|
70
70
|
if (!same(local, next)) {
|
|
71
71
|
storage?.setItem(THEME_MODE_KEY, next.mode);
|
|
72
72
|
storage?.setItem(THEME_COLOR_KEY, next.color);
|
|
@@ -79,6 +79,10 @@ export function ForgeThemeProvider({ children, onChange }) {
|
|
|
79
79
|
const storage = safeStorage();
|
|
80
80
|
setSyncState(readSync(storage));
|
|
81
81
|
const adopt = () => {
|
|
82
|
+
// A stale host-only copy would shadow the shared cookie: drop it before reading.
|
|
83
|
+
const expiry = hostThemeCookieExpiry();
|
|
84
|
+
if (expiry)
|
|
85
|
+
document.cookie = expiry;
|
|
82
86
|
const next = resolveTheme(storage, document.cookie);
|
|
83
87
|
// The first app visited seeds the shared theme for the others.
|
|
84
88
|
const seed = readSync(storage) && !parseThemeCookie(document.cookie);
|
|
@@ -114,7 +118,7 @@ export function ForgeThemeProvider({ children, onChange }) {
|
|
|
114
118
|
setSyncState(sync);
|
|
115
119
|
// Turning sync on shares this app's current theme rather than jumping to another app's.
|
|
116
120
|
if (sync)
|
|
117
|
-
|
|
121
|
+
writeThemeCookie(current.current);
|
|
118
122
|
}, []);
|
|
119
123
|
const value = useMemo(() => ({ theme: theme.mode, setTheme, colorTheme: theme.color, setColorTheme, syncTheme, setSyncTheme }), [theme, setTheme, setColorTheme, syncTheme, setSyncTheme]);
|
|
120
124
|
return _jsx(Context.Provider, { value: value, children: children });
|
package/dist/theme.d.ts
CHANGED
|
@@ -67,6 +67,14 @@ export declare function isColorTheme(value: unknown): value is ForgeColorTheme;
|
|
|
67
67
|
export declare function parseThemeCookie(cookie: string): ForgeTheme | null;
|
|
68
68
|
/** The `document.cookie` assignment that stores `theme` for every Forge app. */
|
|
69
69
|
export declare function themeCookie(theme: ForgeTheme, domain?: string): string;
|
|
70
|
+
/**
|
|
71
|
+
* The `document.cookie` assignment that deletes a host-only theme cookie, or null where the theme cookie is host-only
|
|
72
|
+
* anyway (no shared domain). A host-only copy left over from before the shared domain was set sorts ahead of the
|
|
73
|
+
* shared cookie, so it would win every read and a new pick would never stick.
|
|
74
|
+
*/
|
|
75
|
+
export declare function hostThemeCookieExpiry(domain?: string): string | null;
|
|
76
|
+
/** Writes the shared theme cookie, dropping any stale host-only copy first. */
|
|
77
|
+
export declare function writeThemeCookie(theme: ForgeTheme, domain?: string): void;
|
|
70
78
|
/**
|
|
71
79
|
* Inline `<head>` script that applies the theme before first paint: the shared
|
|
72
80
|
* cookie when this app syncs, otherwise localStorage. Keep in step with
|
package/dist/theme.js
CHANGED
|
@@ -53,6 +53,21 @@ export function themeCookie(theme, domain) {
|
|
|
53
53
|
.filter(Boolean)
|
|
54
54
|
.join('; ');
|
|
55
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* The `document.cookie` assignment that deletes a host-only theme cookie, or null where the theme cookie is host-only
|
|
58
|
+
* anyway (no shared domain). A host-only copy left over from before the shared domain was set sorts ahead of the
|
|
59
|
+
* shared cookie, so it would win every read and a new pick would never stick.
|
|
60
|
+
*/
|
|
61
|
+
export function hostThemeCookieExpiry(domain) {
|
|
62
|
+
return forgeCookieOptions(domain).domain ? `${THEME_COOKIE}=; Path=/; Max-Age=0` : null;
|
|
63
|
+
}
|
|
64
|
+
/** Writes the shared theme cookie, dropping any stale host-only copy first. */
|
|
65
|
+
export function writeThemeCookie(theme, domain) {
|
|
66
|
+
const expiry = hostThemeCookieExpiry(domain);
|
|
67
|
+
if (expiry)
|
|
68
|
+
document.cookie = expiry;
|
|
69
|
+
document.cookie = themeCookie(theme, domain);
|
|
70
|
+
}
|
|
56
71
|
/**
|
|
57
72
|
* Inline `<head>` script that applies the theme before first paint: the shared
|
|
58
73
|
* cookie when this app syncs, otherwise localStorage. Keep in step with
|