cookie-prefs 0.1.2 → 0.2.0
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 +50 -7
- package/dist/index.cjs +19 -15
- package/dist/index.d.cts +3 -11
- package/dist/index.d.ts +3 -11
- package/dist/index.js +20 -15
- package/dist/next/index.cjs +4 -5
- package/dist/next/index.d.cts +1 -1
- package/dist/next/index.d.ts +1 -1
- package/dist/next/index.js +5 -6
- package/dist/react/index.cjs +18 -9
- package/dist/react/index.d.cts +37 -12
- package/dist/react/index.d.ts +37 -12
- package/dist/react/index.js +18 -10
- package/dist/script/index.cjs +14 -0
- package/dist/script/index.d.cts +12 -0
- package/dist/script/index.d.ts +12 -0
- package/dist/script/index.js +12 -0
- package/dist/theme/color/index.cjs +53 -0
- package/dist/theme/color/index.d.cts +32 -0
- package/dist/theme/color/index.d.ts +32 -0
- package/dist/theme/color/index.js +51 -0
- package/dist/theme/next/index.cjs +13 -0
- package/dist/theme/next/index.d.cts +20 -0
- package/dist/theme/next/index.d.ts +20 -0
- package/dist/theme/next/index.js +11 -0
- package/dist/theme/server/index.cjs +24 -0
- package/dist/theme/server/index.d.cts +41 -0
- package/dist/theme/server/index.d.ts +41 -0
- package/dist/theme/server/index.js +22 -0
- package/dist/{types-CQ_6o7nc.d.cts → types-BITLlZeg.d.cts} +12 -0
- package/dist/{types-CQ_6o7nc.d.ts → types-BITLlZeg.d.ts} +12 -0
- package/package.json +97 -2
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/script/index.ts
|
|
4
|
+
var REFLECTION = /* @__PURE__ */ Symbol.for("cookie-prefs.reflection.1");
|
|
5
|
+
var SOURCE = '(function(P){var r=document.documentElement,C="; "+document.cookie,k=0,j,p,t,n,i,e;for(P=JSON.parse(P);p=P[k++];)try{t=p[2];n=C.split("; "+p[0]+"=")[1];if(n!=null)try{n=decodeURIComponent(n.split(";")[0].replace(/^"(.*)"$/,"$1"));if(t===1){t=parseFloat(n);n=t===+n&&isFinite(t)?""+t:null}else if(t)n={}.hasOwnProperty.call(t,n)?t[n]:null}catch(e){n=null}if(n==null)n=p[3];for(j=0;i=p[1][j++];)if(i=="class"){for(e in t)r.classList.remove(t[e]);n!=null&&r.classList.add(n)}else i[0]=="-"?n==null?r.style.removeProperty(i):r.style.setProperty(i,n):n==null?r.removeAttribute(i):r.setAttribute(i,n)}catch(e){}})(';
|
|
6
|
+
function prefsScript(prefs) {
|
|
7
|
+
const config = Object.values(prefs).map((handle) => handle[REFLECTION]).filter((r) => !!r);
|
|
8
|
+
return config.length ? SOURCE + JSON.stringify(JSON.stringify(config)).replace(
|
|
9
|
+
/[<>&\u2028\u2029]/g,
|
|
10
|
+
(c) => "\\u" + c.charCodeAt(0).toString(16).padStart(4, "0")
|
|
11
|
+
) + ")" : "";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
exports.prefsScript = prefsScript;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PrefsRecord } from '../index.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Source of a blocking inline script that applies every `.reflect()` declaration to
|
|
5
|
+
* `<html>` from `document.cookie` before first paint. Returns `''` when no pref
|
|
6
|
+
* reflects. The output is fixed code plus escaped JSON config, safe to inline.
|
|
7
|
+
* @example
|
|
8
|
+
* <script nonce={nonce} dangerouslySetInnerHTML={{ __html: prefsScript(prefs) }} />
|
|
9
|
+
*/
|
|
10
|
+
declare function prefsScript(prefs: PrefsRecord): string;
|
|
11
|
+
|
|
12
|
+
export { prefsScript };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PrefsRecord } from '../index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Source of a blocking inline script that applies every `.reflect()` declaration to
|
|
5
|
+
* `<html>` from `document.cookie` before first paint. Returns `''` when no pref
|
|
6
|
+
* reflects. The output is fixed code plus escaped JSON config, safe to inline.
|
|
7
|
+
* @example
|
|
8
|
+
* <script nonce={nonce} dangerouslySetInnerHTML={{ __html: prefsScript(prefs) }} />
|
|
9
|
+
*/
|
|
10
|
+
declare function prefsScript(prefs: PrefsRecord): string;
|
|
11
|
+
|
|
12
|
+
export { prefsScript };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/script/index.ts
|
|
2
|
+
var REFLECTION = /* @__PURE__ */ Symbol.for("cookie-prefs.reflection.1");
|
|
3
|
+
var SOURCE = '(function(P){var r=document.documentElement,C="; "+document.cookie,k=0,j,p,t,n,i,e;for(P=JSON.parse(P);p=P[k++];)try{t=p[2];n=C.split("; "+p[0]+"=")[1];if(n!=null)try{n=decodeURIComponent(n.split(";")[0].replace(/^"(.*)"$/,"$1"));if(t===1){t=parseFloat(n);n=t===+n&&isFinite(t)?""+t:null}else if(t)n={}.hasOwnProperty.call(t,n)?t[n]:null}catch(e){n=null}if(n==null)n=p[3];for(j=0;i=p[1][j++];)if(i=="class"){for(e in t)r.classList.remove(t[e]);n!=null&&r.classList.add(n)}else i[0]=="-"?n==null?r.style.removeProperty(i):r.style.setProperty(i,n):n==null?r.removeAttribute(i):r.setAttribute(i,n)}catch(e){}})(';
|
|
4
|
+
function prefsScript(prefs) {
|
|
5
|
+
const config = Object.values(prefs).map((handle) => handle[REFLECTION]).filter((r) => !!r);
|
|
6
|
+
return config.length ? SOURCE + JSON.stringify(JSON.stringify(config)).replace(
|
|
7
|
+
/[<>&\u2028\u2029]/g,
|
|
8
|
+
(c) => "\\u" + c.charCodeAt(0).toString(16).padStart(4, "0")
|
|
9
|
+
) + ")" : "";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { prefsScript };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var theme = require('cookie-prefs/theme');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
7
|
+
|
|
8
|
+
// src/theme/color/index.tsx
|
|
9
|
+
var MARK = "cookie-prefs";
|
|
10
|
+
var OURS = `meta[property="${MARK}"]`;
|
|
11
|
+
var FIRST = 'meta[name="theme-color"]';
|
|
12
|
+
var SOURCE = `(function(C){var n=document.scripts,i=n.length,t;while(i--)if(!(t=n[i].text).indexOf('(function(k,l,a,m,d,f,s,c){')){(function(k,l,a,m,d,f,s,c){var x,h,y;if(!f){x=('; '+document.cookie).split('; '+k+'=')[1];try{x=x!=null?decodeURIComponent(x.split(';')[0].replace(/^"(.*)"$/,'$1')):localStorage.getItem(l)}catch(e){x=null}f=x!=null&&{}.hasOwnProperty.call(m,x)?x:d}if(f=='system'&&s)f=matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light';if(x={}.hasOwnProperty.call(C,f)&&C[f]){h=document.querySelector('` + OURS + "')||document.createElement('meta');h.name='theme-color';h.content=x;h.setAttribute('property','" + MARK + "');y=document.querySelector('" + FIRST + "');y?y!=h&&y.parentNode.insertBefore(h,y):document.head.appendChild(h)}}).apply(0,JSON.parse(JSON.parse(t.slice(t.indexOf('JSON.parse(')+11,-2))));break}})(JSON.parse(";
|
|
13
|
+
var colorScript = (colors) => SOURCE + JSON.stringify(JSON.stringify(colors)).replace(
|
|
14
|
+
/[<>&\u2028\u2029]/g,
|
|
15
|
+
(c) => "\\u" + c.charCodeAt(0).toString(16).padStart(4, "0")
|
|
16
|
+
) + "))";
|
|
17
|
+
var set = (color) => {
|
|
18
|
+
let meta = document.querySelector(OURS);
|
|
19
|
+
if (!color) return meta?.remove();
|
|
20
|
+
meta ||= document.createElement("meta");
|
|
21
|
+
meta.name = "theme-color";
|
|
22
|
+
meta.content = color;
|
|
23
|
+
meta.setAttribute("property", MARK);
|
|
24
|
+
const first = document.querySelector(FIRST);
|
|
25
|
+
if (!first) document.head.append(meta);
|
|
26
|
+
else if (first != meta) first.before(meta);
|
|
27
|
+
};
|
|
28
|
+
var useLayout = typeof document == "undefined" ? react.useEffect : react.useLayoutEffect;
|
|
29
|
+
function ThemeColor({ colors, nonce }) {
|
|
30
|
+
const { theme: theme$1, resolvedTheme, forcedTheme, systemTheme } = theme.useTheme();
|
|
31
|
+
let name = forcedTheme || resolvedTheme;
|
|
32
|
+
if (name == "system" && systemTheme) name = systemTheme;
|
|
33
|
+
const color = theme$1 == null ? void 0 : {}.hasOwnProperty.call(colors, name) ? colors[name] || "" : "";
|
|
34
|
+
const [first] = react.useState(() => ({ __html: colorScript(colors) }));
|
|
35
|
+
useLayout(() => {
|
|
36
|
+
if (color != null) {
|
|
37
|
+
set(color);
|
|
38
|
+
return set;
|
|
39
|
+
}
|
|
40
|
+
}, [color]);
|
|
41
|
+
const server = typeof document == "undefined";
|
|
42
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
43
|
+
"script",
|
|
44
|
+
{
|
|
45
|
+
type: server ? "text/javascript" : "text/plain",
|
|
46
|
+
nonce: server ? nonce : void 0,
|
|
47
|
+
suppressHydrationWarning: true,
|
|
48
|
+
dangerouslySetInnerHTML: first
|
|
49
|
+
}
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
exports.ThemeColor = ThemeColor;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ReactElement } from 'react';
|
|
2
|
+
|
|
3
|
+
/** Props of `ThemeColor`. */
|
|
4
|
+
interface ThemeColorProps {
|
|
5
|
+
/**
|
|
6
|
+
* Theme name to the `<meta name="theme-color">` value (any CSS colour), keyed like
|
|
7
|
+
* the provider's `themes` (custom themes and `value` keys too). Looked up for the theme
|
|
8
|
+
* shown: `forcedTheme` first, `'system'` resolved to `'light'` or `'dark'`. A theme
|
|
9
|
+
* without a colour leaves the page's own theme-color tags in charge.
|
|
10
|
+
*/
|
|
11
|
+
colors: Readonly<Record<string, string>>;
|
|
12
|
+
/** CSP nonce for the inline script, as passed to `ThemeProvider`. */
|
|
13
|
+
nonce?: string | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Keeps `<meta name="theme-color">` (the browser UI colour on mobile and of installed
|
|
17
|
+
* apps) in sync with the theme shown by `ThemeProvider`: before hydration through an
|
|
18
|
+
* inline script, afterwards through `useTheme()`, so it follows `setTheme`, the OS
|
|
19
|
+
* scheme for `'system'`, `forcedTheme` and other tabs. Render it once, anywhere inside
|
|
20
|
+
* `ThemeProvider`. It owns one tag and places it before the page's other theme-color
|
|
21
|
+
* tags (Next.js `viewport.themeColor`, for example), which stay in place and apply to
|
|
22
|
+
* themes without a colour.
|
|
23
|
+
* @example
|
|
24
|
+
* <ThemeProvider attribute="class">
|
|
25
|
+
* <ThemeColor colors={{ light: '#ffffff', dark: '#0a0a0a' }} />
|
|
26
|
+
* {children}
|
|
27
|
+
* </ThemeProvider>
|
|
28
|
+
*/
|
|
29
|
+
declare function ThemeColor({ colors, nonce }: ThemeColorProps): ReactElement;
|
|
30
|
+
|
|
31
|
+
export { ThemeColor };
|
|
32
|
+
export type { ThemeColorProps };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ReactElement } from 'react';
|
|
2
|
+
|
|
3
|
+
/** Props of `ThemeColor`. */
|
|
4
|
+
interface ThemeColorProps {
|
|
5
|
+
/**
|
|
6
|
+
* Theme name to the `<meta name="theme-color">` value (any CSS colour), keyed like
|
|
7
|
+
* the provider's `themes` (custom themes and `value` keys too). Looked up for the theme
|
|
8
|
+
* shown: `forcedTheme` first, `'system'` resolved to `'light'` or `'dark'`. A theme
|
|
9
|
+
* without a colour leaves the page's own theme-color tags in charge.
|
|
10
|
+
*/
|
|
11
|
+
colors: Readonly<Record<string, string>>;
|
|
12
|
+
/** CSP nonce for the inline script, as passed to `ThemeProvider`. */
|
|
13
|
+
nonce?: string | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Keeps `<meta name="theme-color">` (the browser UI colour on mobile and of installed
|
|
17
|
+
* apps) in sync with the theme shown by `ThemeProvider`: before hydration through an
|
|
18
|
+
* inline script, afterwards through `useTheme()`, so it follows `setTheme`, the OS
|
|
19
|
+
* scheme for `'system'`, `forcedTheme` and other tabs. Render it once, anywhere inside
|
|
20
|
+
* `ThemeProvider`. It owns one tag and places it before the page's other theme-color
|
|
21
|
+
* tags (Next.js `viewport.themeColor`, for example), which stay in place and apply to
|
|
22
|
+
* themes without a colour.
|
|
23
|
+
* @example
|
|
24
|
+
* <ThemeProvider attribute="class">
|
|
25
|
+
* <ThemeColor colors={{ light: '#ffffff', dark: '#0a0a0a' }} />
|
|
26
|
+
* {children}
|
|
27
|
+
* </ThemeProvider>
|
|
28
|
+
*/
|
|
29
|
+
declare function ThemeColor({ colors, nonce }: ThemeColorProps): ReactElement;
|
|
30
|
+
|
|
31
|
+
export { ThemeColor };
|
|
32
|
+
export type { ThemeColorProps };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useTheme } from 'cookie-prefs/theme';
|
|
3
|
+
import { useState, useEffect, useLayoutEffect } from 'react';
|
|
4
|
+
import { jsx } from 'react/jsx-runtime';
|
|
5
|
+
|
|
6
|
+
// src/theme/color/index.tsx
|
|
7
|
+
var MARK = "cookie-prefs";
|
|
8
|
+
var OURS = `meta[property="${MARK}"]`;
|
|
9
|
+
var FIRST = 'meta[name="theme-color"]';
|
|
10
|
+
var SOURCE = `(function(C){var n=document.scripts,i=n.length,t;while(i--)if(!(t=n[i].text).indexOf('(function(k,l,a,m,d,f,s,c){')){(function(k,l,a,m,d,f,s,c){var x,h,y;if(!f){x=('; '+document.cookie).split('; '+k+'=')[1];try{x=x!=null?decodeURIComponent(x.split(';')[0].replace(/^"(.*)"$/,'$1')):localStorage.getItem(l)}catch(e){x=null}f=x!=null&&{}.hasOwnProperty.call(m,x)?x:d}if(f=='system'&&s)f=matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light';if(x={}.hasOwnProperty.call(C,f)&&C[f]){h=document.querySelector('` + OURS + "')||document.createElement('meta');h.name='theme-color';h.content=x;h.setAttribute('property','" + MARK + "');y=document.querySelector('" + FIRST + "');y?y!=h&&y.parentNode.insertBefore(h,y):document.head.appendChild(h)}}).apply(0,JSON.parse(JSON.parse(t.slice(t.indexOf('JSON.parse(')+11,-2))));break}})(JSON.parse(";
|
|
11
|
+
var colorScript = (colors) => SOURCE + JSON.stringify(JSON.stringify(colors)).replace(
|
|
12
|
+
/[<>&\u2028\u2029]/g,
|
|
13
|
+
(c) => "\\u" + c.charCodeAt(0).toString(16).padStart(4, "0")
|
|
14
|
+
) + "))";
|
|
15
|
+
var set = (color) => {
|
|
16
|
+
let meta = document.querySelector(OURS);
|
|
17
|
+
if (!color) return meta?.remove();
|
|
18
|
+
meta ||= document.createElement("meta");
|
|
19
|
+
meta.name = "theme-color";
|
|
20
|
+
meta.content = color;
|
|
21
|
+
meta.setAttribute("property", MARK);
|
|
22
|
+
const first = document.querySelector(FIRST);
|
|
23
|
+
if (!first) document.head.append(meta);
|
|
24
|
+
else if (first != meta) first.before(meta);
|
|
25
|
+
};
|
|
26
|
+
var useLayout = typeof document == "undefined" ? useEffect : useLayoutEffect;
|
|
27
|
+
function ThemeColor({ colors, nonce }) {
|
|
28
|
+
const { theme, resolvedTheme, forcedTheme, systemTheme } = useTheme();
|
|
29
|
+
let name = forcedTheme || resolvedTheme;
|
|
30
|
+
if (name == "system" && systemTheme) name = systemTheme;
|
|
31
|
+
const color = theme == null ? void 0 : {}.hasOwnProperty.call(colors, name) ? colors[name] || "" : "";
|
|
32
|
+
const [first] = useState(() => ({ __html: colorScript(colors) }));
|
|
33
|
+
useLayout(() => {
|
|
34
|
+
if (color != null) {
|
|
35
|
+
set(color);
|
|
36
|
+
return set;
|
|
37
|
+
}
|
|
38
|
+
}, [color]);
|
|
39
|
+
const server = typeof document == "undefined";
|
|
40
|
+
return /* @__PURE__ */ jsx(
|
|
41
|
+
"script",
|
|
42
|
+
{
|
|
43
|
+
type: server ? "text/javascript" : "text/plain",
|
|
44
|
+
nonce: server ? nonce : void 0,
|
|
45
|
+
suppressHydrationWarning: true,
|
|
46
|
+
dangerouslySetInnerHTML: first
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { ThemeColor };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var cookiePrefs = require('cookie-prefs');
|
|
4
|
+
var next = require('cookie-prefs/next');
|
|
5
|
+
var server = require('cookie-prefs/theme/server');
|
|
6
|
+
|
|
7
|
+
// src/theme/next/index.ts
|
|
8
|
+
async function getTheme(options = {}) {
|
|
9
|
+
const raw = cookiePrefs.definePrefs({ theme: cookiePrefs.pref.string().options({ name: options.storageKey ?? "theme" }) });
|
|
10
|
+
return server.readTheme({ [raw.theme.name]: (await next.getPrefs(raw)).theme }, options);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
exports.getTheme = getTheme;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ThemeOptions } from '../server/index.cjs';
|
|
2
|
+
import 'cookie-prefs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The stored theme for the current request: what `useTheme().theme` holds after
|
|
6
|
+
* hydration for the same cookie and the same `ThemeProvider` options (an unlisted or
|
|
7
|
+
* malformed value reads as `defaultTheme`; `'system'` is returned as is, since only the
|
|
8
|
+
* browser can resolve it; `forcedTheme` is not applied). Reads the request like
|
|
9
|
+
* `getPrefs` from `cookie-prefs/next`, so a theme cookie set earlier in the same request
|
|
10
|
+
* (a Server Function, middleware) is seen. Server-only (Server Components, Server
|
|
11
|
+
* Functions, Route Handlers). Reading request cookies makes the route dynamic, so this
|
|
12
|
+
* does not fit static rendering or Cache Components; the provider's inline script still
|
|
13
|
+
* applies the theme before paint, and `<html>` keeps `suppressHydrationWarning`.
|
|
14
|
+
* @example
|
|
15
|
+
* const theme = await getTheme({ themes: ['light', 'dark'] })
|
|
16
|
+
* <html lang="en" className={theme == 'system' ? undefined : theme} suppressHydrationWarning>
|
|
17
|
+
*/
|
|
18
|
+
declare function getTheme<const T extends string = string>(options?: ThemeOptions<T>): Promise<T | 'system'>;
|
|
19
|
+
|
|
20
|
+
export { ThemeOptions, getTheme };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ThemeOptions } from '../server/index.js';
|
|
2
|
+
import 'cookie-prefs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The stored theme for the current request: what `useTheme().theme` holds after
|
|
6
|
+
* hydration for the same cookie and the same `ThemeProvider` options (an unlisted or
|
|
7
|
+
* malformed value reads as `defaultTheme`; `'system'` is returned as is, since only the
|
|
8
|
+
* browser can resolve it; `forcedTheme` is not applied). Reads the request like
|
|
9
|
+
* `getPrefs` from `cookie-prefs/next`, so a theme cookie set earlier in the same request
|
|
10
|
+
* (a Server Function, middleware) is seen. Server-only (Server Components, Server
|
|
11
|
+
* Functions, Route Handlers). Reading request cookies makes the route dynamic, so this
|
|
12
|
+
* does not fit static rendering or Cache Components; the provider's inline script still
|
|
13
|
+
* applies the theme before paint, and `<html>` keeps `suppressHydrationWarning`.
|
|
14
|
+
* @example
|
|
15
|
+
* const theme = await getTheme({ themes: ['light', 'dark'] })
|
|
16
|
+
* <html lang="en" className={theme == 'system' ? undefined : theme} suppressHydrationWarning>
|
|
17
|
+
*/
|
|
18
|
+
declare function getTheme<const T extends string = string>(options?: ThemeOptions<T>): Promise<T | 'system'>;
|
|
19
|
+
|
|
20
|
+
export { ThemeOptions, getTheme };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { definePrefs, pref } from 'cookie-prefs';
|
|
2
|
+
import { getPrefs } from 'cookie-prefs/next';
|
|
3
|
+
import { readTheme } from 'cookie-prefs/theme/server';
|
|
4
|
+
|
|
5
|
+
// src/theme/next/index.ts
|
|
6
|
+
async function getTheme(options = {}) {
|
|
7
|
+
const raw = definePrefs({ theme: pref.string().options({ name: options.storageKey ?? "theme" }) });
|
|
8
|
+
return readTheme({ [raw.theme.name]: (await getPrefs(raw)).theme }, options);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { getTheme };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var cookiePrefs = require('cookie-prefs');
|
|
4
|
+
|
|
5
|
+
// src/theme/server/index.ts
|
|
6
|
+
function readTheme(source, {
|
|
7
|
+
themes = ["light", "dark"],
|
|
8
|
+
enableSystem = true,
|
|
9
|
+
storageKey = "theme",
|
|
10
|
+
defaultTheme = enableSystem ? "system" : "light",
|
|
11
|
+
value
|
|
12
|
+
} = {}) {
|
|
13
|
+
return cookiePrefs.definePrefs({
|
|
14
|
+
theme: cookiePrefs.pref.enum(
|
|
15
|
+
Object.keys(
|
|
16
|
+
Object.fromEntries(
|
|
17
|
+
(enableSystem ? [...themes, "system"] : themes).concat(Object.keys(value || {})).map((t) => [t, t])
|
|
18
|
+
)
|
|
19
|
+
)
|
|
20
|
+
).default(defaultTheme).options({ name: storageKey })
|
|
21
|
+
}).theme.read(source);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
exports.readTheme = readTheme;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { CookieSource } from 'cookie-prefs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The `ThemeProvider` props that decide which stored theme is valid. Pass the values
|
|
5
|
+
* the provider gets, with the same defaults; any other provider prop may be passed and
|
|
6
|
+
* is ignored, so one config object can feed both. When `themes` is a literal tuple,
|
|
7
|
+
* `defaultTheme` and the keys of `value` must be among them (or `'system'`), so the
|
|
8
|
+
* returned type is exactly the names that can come back.
|
|
9
|
+
*/
|
|
10
|
+
interface ThemeOptions<T extends string = string> {
|
|
11
|
+
/** All theme names. `'system'` is added when `enableSystem` is on. @default ['light', 'dark'] */
|
|
12
|
+
themes?: readonly T[] | undefined;
|
|
13
|
+
/** Whether `'system'` is a valid stored theme. @default true */
|
|
14
|
+
enableSystem?: boolean | undefined;
|
|
15
|
+
/** Cookie name that stores the choice. @default 'theme' */
|
|
16
|
+
storageKey?: string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Theme when nothing valid is stored. With `enableSystem` off and no `'light'` in
|
|
19
|
+
* `themes`, pass it: the next-themes default `'light'` is returned as is.
|
|
20
|
+
* @default enableSystem ? 'system' : 'light'
|
|
21
|
+
*/
|
|
22
|
+
defaultTheme?: NoInfer<T> | 'system' | undefined;
|
|
23
|
+
/** Theme name to DOM value; its keys are valid stored themes too, as in the provider. */
|
|
24
|
+
value?: Readonly<Partial<Record<NoInfer<T> | 'system', string>>> | undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The stored theme for a request, read from its cookies: what `useTheme().theme` holds
|
|
28
|
+
* after hydration for the same cookie and the same `ThemeProvider` options. A name the
|
|
29
|
+
* provider does not list, or a malformed cookie, reads as `defaultTheme`. `'system'` is
|
|
30
|
+
* returned as is: only the browser knows the OS scheme. `forcedTheme` is not applied.
|
|
31
|
+
* Works on any server: a `Cookie` header string, a cookie store with `get(name)` or a
|
|
32
|
+
* plain map, like the core's `readPrefs`. In the Next.js App Router use `getTheme()`
|
|
33
|
+
* from `cookie-prefs/theme/next`.
|
|
34
|
+
* @example
|
|
35
|
+
* // React Router loader
|
|
36
|
+
* const theme = readTheme(request.headers.get('cookie'), { themes: ['light', 'dark'] })
|
|
37
|
+
*/
|
|
38
|
+
declare function readTheme<const T extends string = string>(source: CookieSource, options?: ThemeOptions<T>): T | 'system';
|
|
39
|
+
|
|
40
|
+
export { readTheme };
|
|
41
|
+
export type { ThemeOptions };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { CookieSource } from 'cookie-prefs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The `ThemeProvider` props that decide which stored theme is valid. Pass the values
|
|
5
|
+
* the provider gets, with the same defaults; any other provider prop may be passed and
|
|
6
|
+
* is ignored, so one config object can feed both. When `themes` is a literal tuple,
|
|
7
|
+
* `defaultTheme` and the keys of `value` must be among them (or `'system'`), so the
|
|
8
|
+
* returned type is exactly the names that can come back.
|
|
9
|
+
*/
|
|
10
|
+
interface ThemeOptions<T extends string = string> {
|
|
11
|
+
/** All theme names. `'system'` is added when `enableSystem` is on. @default ['light', 'dark'] */
|
|
12
|
+
themes?: readonly T[] | undefined;
|
|
13
|
+
/** Whether `'system'` is a valid stored theme. @default true */
|
|
14
|
+
enableSystem?: boolean | undefined;
|
|
15
|
+
/** Cookie name that stores the choice. @default 'theme' */
|
|
16
|
+
storageKey?: string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Theme when nothing valid is stored. With `enableSystem` off and no `'light'` in
|
|
19
|
+
* `themes`, pass it: the next-themes default `'light'` is returned as is.
|
|
20
|
+
* @default enableSystem ? 'system' : 'light'
|
|
21
|
+
*/
|
|
22
|
+
defaultTheme?: NoInfer<T> | 'system' | undefined;
|
|
23
|
+
/** Theme name to DOM value; its keys are valid stored themes too, as in the provider. */
|
|
24
|
+
value?: Readonly<Partial<Record<NoInfer<T> | 'system', string>>> | undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The stored theme for a request, read from its cookies: what `useTheme().theme` holds
|
|
28
|
+
* after hydration for the same cookie and the same `ThemeProvider` options. A name the
|
|
29
|
+
* provider does not list, or a malformed cookie, reads as `defaultTheme`. `'system'` is
|
|
30
|
+
* returned as is: only the browser knows the OS scheme. `forcedTheme` is not applied.
|
|
31
|
+
* Works on any server: a `Cookie` header string, a cookie store with `get(name)` or a
|
|
32
|
+
* plain map, like the core's `readPrefs`. In the Next.js App Router use `getTheme()`
|
|
33
|
+
* from `cookie-prefs/theme/next`.
|
|
34
|
+
* @example
|
|
35
|
+
* // React Router loader
|
|
36
|
+
* const theme = readTheme(request.headers.get('cookie'), { themes: ['light', 'dark'] })
|
|
37
|
+
*/
|
|
38
|
+
declare function readTheme<const T extends string = string>(source: CookieSource, options?: ThemeOptions<T>): T | 'system';
|
|
39
|
+
|
|
40
|
+
export { readTheme };
|
|
41
|
+
export type { ThemeOptions };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { definePrefs, pref } from 'cookie-prefs';
|
|
2
|
+
|
|
3
|
+
// src/theme/server/index.ts
|
|
4
|
+
function readTheme(source, {
|
|
5
|
+
themes = ["light", "dark"],
|
|
6
|
+
enableSystem = true,
|
|
7
|
+
storageKey = "theme",
|
|
8
|
+
defaultTheme = enableSystem ? "system" : "light",
|
|
9
|
+
value
|
|
10
|
+
} = {}) {
|
|
11
|
+
return definePrefs({
|
|
12
|
+
theme: pref.enum(
|
|
13
|
+
Object.keys(
|
|
14
|
+
Object.fromEntries(
|
|
15
|
+
(enableSystem ? [...themes, "system"] : themes).concat(Object.keys(value || {})).map((t) => [t, t])
|
|
16
|
+
)
|
|
17
|
+
)
|
|
18
|
+
).default(defaultTheme).options({ name: storageKey })
|
|
19
|
+
}).theme.read(source);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { readTheme };
|
|
@@ -141,6 +141,18 @@ interface EnumPrefBuilder<T extends string, V = T | undefined> extends PrefBuild
|
|
|
141
141
|
* @example pref.enum(['compact', 'cozy']).default('cozy').reflect('data-density')
|
|
142
142
|
*/
|
|
143
143
|
reflect(target: EnumReflectTarget | readonly EnumReflectTarget[], value?: Partial<Record<T, string>>): EnumPrefBuilder<T, V>;
|
|
144
|
+
/**
|
|
145
|
+
* Read old (renamed or removed) values as current ones: a cookie holding a key of
|
|
146
|
+
* `map` reads as its value everywhere (`get()`, `read()`, `readPrefs`, hydration from
|
|
147
|
+
* a snapshot, reflection and the inline script). Reads never rewrite the cookie; the
|
|
148
|
+
* next `set()` writes the current name. One level only: a value must be a current
|
|
149
|
+
* value, and a key must not be one (a current value always reads as itself).
|
|
150
|
+
* Repeated calls add to the map.
|
|
151
|
+
* @example pref.enum(['compact', 'comfortable']).default('comfortable').migrate({ cozy: 'comfortable' })
|
|
152
|
+
*/
|
|
153
|
+
migrate<const M extends Readonly<Record<string, T>>>(map: M & {
|
|
154
|
+
readonly [K in keyof M & T]?: never;
|
|
155
|
+
}): EnumPrefBuilder<T, V>;
|
|
144
156
|
}
|
|
145
157
|
/** A custom codec for `pref.custom()`. */
|
|
146
158
|
interface PrefCodec<T> {
|
|
@@ -141,6 +141,18 @@ interface EnumPrefBuilder<T extends string, V = T | undefined> extends PrefBuild
|
|
|
141
141
|
* @example pref.enum(['compact', 'cozy']).default('cozy').reflect('data-density')
|
|
142
142
|
*/
|
|
143
143
|
reflect(target: EnumReflectTarget | readonly EnumReflectTarget[], value?: Partial<Record<T, string>>): EnumPrefBuilder<T, V>;
|
|
144
|
+
/**
|
|
145
|
+
* Read old (renamed or removed) values as current ones: a cookie holding a key of
|
|
146
|
+
* `map` reads as its value everywhere (`get()`, `read()`, `readPrefs`, hydration from
|
|
147
|
+
* a snapshot, reflection and the inline script). Reads never rewrite the cookie; the
|
|
148
|
+
* next `set()` writes the current name. One level only: a value must be a current
|
|
149
|
+
* value, and a key must not be one (a current value always reads as itself).
|
|
150
|
+
* Repeated calls add to the map.
|
|
151
|
+
* @example pref.enum(['compact', 'comfortable']).default('comfortable').migrate({ cozy: 'comfortable' })
|
|
152
|
+
*/
|
|
153
|
+
migrate<const M extends Readonly<Record<string, T>>>(map: M & {
|
|
154
|
+
readonly [K in keyof M & T]?: never;
|
|
155
|
+
}): EnumPrefBuilder<T, V>;
|
|
144
156
|
}
|
|
145
157
|
/** A custom codec for `pref.custom()`. */
|
|
146
158
|
interface PrefCodec<T> {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cookie-prefs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Typed cookie-backed user preference state for React and Next.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -58,6 +58,16 @@
|
|
|
58
58
|
"default": "./dist/next/index.cjs"
|
|
59
59
|
}
|
|
60
60
|
},
|
|
61
|
+
"./script": {
|
|
62
|
+
"import": {
|
|
63
|
+
"types": "./dist/script/index.d.ts",
|
|
64
|
+
"default": "./dist/script/index.js"
|
|
65
|
+
},
|
|
66
|
+
"require": {
|
|
67
|
+
"types": "./dist/script/index.d.cts",
|
|
68
|
+
"default": "./dist/script/index.cjs"
|
|
69
|
+
}
|
|
70
|
+
},
|
|
61
71
|
"./theme": {
|
|
62
72
|
"import": {
|
|
63
73
|
"types": "./dist/theme/index.d.ts",
|
|
@@ -68,6 +78,36 @@
|
|
|
68
78
|
"default": "./dist/theme/index.cjs"
|
|
69
79
|
}
|
|
70
80
|
},
|
|
81
|
+
"./theme/server": {
|
|
82
|
+
"import": {
|
|
83
|
+
"types": "./dist/theme/server/index.d.ts",
|
|
84
|
+
"default": "./dist/theme/server/index.js"
|
|
85
|
+
},
|
|
86
|
+
"require": {
|
|
87
|
+
"types": "./dist/theme/server/index.d.cts",
|
|
88
|
+
"default": "./dist/theme/server/index.cjs"
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
"./theme/next": {
|
|
92
|
+
"import": {
|
|
93
|
+
"types": "./dist/theme/next/index.d.ts",
|
|
94
|
+
"default": "./dist/theme/next/index.js"
|
|
95
|
+
},
|
|
96
|
+
"require": {
|
|
97
|
+
"types": "./dist/theme/next/index.d.cts",
|
|
98
|
+
"default": "./dist/theme/next/index.cjs"
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
"./theme/color": {
|
|
102
|
+
"import": {
|
|
103
|
+
"types": "./dist/theme/color/index.d.ts",
|
|
104
|
+
"default": "./dist/theme/color/index.js"
|
|
105
|
+
},
|
|
106
|
+
"require": {
|
|
107
|
+
"types": "./dist/theme/color/index.d.cts",
|
|
108
|
+
"default": "./dist/theme/color/index.cjs"
|
|
109
|
+
}
|
|
110
|
+
},
|
|
71
111
|
"./package.json": "./package.json"
|
|
72
112
|
},
|
|
73
113
|
"typesVersions": {
|
|
@@ -78,8 +118,20 @@
|
|
|
78
118
|
"next": [
|
|
79
119
|
"./dist/next/index.d.ts"
|
|
80
120
|
],
|
|
121
|
+
"script": [
|
|
122
|
+
"./dist/script/index.d.ts"
|
|
123
|
+
],
|
|
81
124
|
"theme": [
|
|
82
125
|
"./dist/theme/index.d.ts"
|
|
126
|
+
],
|
|
127
|
+
"theme/server": [
|
|
128
|
+
"./dist/theme/server/index.d.ts"
|
|
129
|
+
],
|
|
130
|
+
"theme/next": [
|
|
131
|
+
"./dist/theme/next/index.d.ts"
|
|
132
|
+
],
|
|
133
|
+
"theme/color": [
|
|
134
|
+
"./dist/theme/color/index.d.ts"
|
|
83
135
|
]
|
|
84
136
|
}
|
|
85
137
|
},
|
|
@@ -106,7 +158,17 @@
|
|
|
106
158
|
{
|
|
107
159
|
"name": "cookie-prefs/react",
|
|
108
160
|
"path": "dist/react/index.js",
|
|
109
|
-
"limit": "
|
|
161
|
+
"limit": "1040 B",
|
|
162
|
+
"gzip": true,
|
|
163
|
+
"ignore": [
|
|
164
|
+
"cookie-prefs"
|
|
165
|
+
]
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
"name": "cookie-prefs/react (usePref and PrefsProvider only)",
|
|
169
|
+
"path": "dist/react/index.js",
|
|
170
|
+
"import": "{ usePref, PrefsProvider }",
|
|
171
|
+
"limit": "930 B",
|
|
110
172
|
"gzip": true,
|
|
111
173
|
"ignore": [
|
|
112
174
|
"cookie-prefs"
|
|
@@ -121,6 +183,12 @@
|
|
|
121
183
|
"cookie-prefs"
|
|
122
184
|
]
|
|
123
185
|
},
|
|
186
|
+
{
|
|
187
|
+
"name": "cookie-prefs/script",
|
|
188
|
+
"path": "dist/script/index.js",
|
|
189
|
+
"limit": "570 B",
|
|
190
|
+
"gzip": true
|
|
191
|
+
},
|
|
124
192
|
{
|
|
125
193
|
"name": "cookie-prefs/theme",
|
|
126
194
|
"path": "dist/theme/index.js",
|
|
@@ -129,6 +197,33 @@
|
|
|
129
197
|
"ignore": [
|
|
130
198
|
"cookie-prefs"
|
|
131
199
|
]
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
"name": "cookie-prefs/theme/server",
|
|
203
|
+
"path": "dist/theme/server/index.js",
|
|
204
|
+
"limit": "255 B",
|
|
205
|
+
"gzip": true,
|
|
206
|
+
"ignore": [
|
|
207
|
+
"cookie-prefs"
|
|
208
|
+
]
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
"name": "cookie-prefs/theme/next",
|
|
212
|
+
"path": "dist/theme/next/index.js",
|
|
213
|
+
"limit": "200 B",
|
|
214
|
+
"gzip": true,
|
|
215
|
+
"ignore": [
|
|
216
|
+
"cookie-prefs"
|
|
217
|
+
]
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
"name": "cookie-prefs/theme/color",
|
|
221
|
+
"path": "dist/theme/color/index.js",
|
|
222
|
+
"limit": "1030 B",
|
|
223
|
+
"gzip": true,
|
|
224
|
+
"ignore": [
|
|
225
|
+
"cookie-prefs"
|
|
226
|
+
]
|
|
132
227
|
}
|
|
133
228
|
],
|
|
134
229
|
"peerDependencies": {
|