@codefast/theme 0.8.0 → 0.8.2
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/CHANGELOG.md +23 -0
- package/README.md +110 -34
- package/dist/appearance-context.d.ts +1 -2
- package/dist/appearance-context.js +1 -2
- package/dist/appearance-provider.d.ts +3 -2
- package/dist/appearance-provider.js +7 -4
- package/dist/appearance-script.d.ts +3 -2
- package/dist/appearance-script.js +1 -2
- package/dist/appearance.d.ts +1 -2
- package/dist/appearance.js +1 -2
- package/dist/color-scheme.d.ts +1 -2
- package/dist/color-scheme.js +1 -2
- package/dist/constants.d.ts +1 -2
- package/dist/constants.js +1 -2
- package/dist/dom.d.ts +1 -2
- package/dist/dom.js +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/dist/use-appearance.d.ts +1 -2
- package/dist/use-appearance.js +1 -2
- package/package.json +10 -27
- package/dist/appearance-context.d.ts.map +0 -1
- package/dist/appearance-context.js.map +0 -1
- package/dist/appearance-provider.d.ts.map +0 -1
- package/dist/appearance-provider.js.map +0 -1
- package/dist/appearance-script.d.ts.map +0 -1
- package/dist/appearance-script.js.map +0 -1
- package/dist/appearance.d.ts.map +0 -1
- package/dist/appearance.js.map +0 -1
- package/dist/color-scheme.d.ts.map +0 -1
- package/dist/color-scheme.js.map +0 -1
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/dom.d.ts.map +0 -1
- package/dist/dom.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/use-appearance.d.ts.map +0 -1
- package/dist/use-appearance.js.map +0 -1
- package/src/appearance-context.ts +0 -21
- package/src/appearance-provider.tsx +0 -361
- package/src/appearance-script.tsx +0 -85
- package/src/appearance.ts +0 -70
- package/src/color-scheme.ts +0 -40
- package/src/constants.ts +0 -49
- package/src/dom.ts +0 -85
- package/src/index.ts +0 -32
- package/src/use-appearance.ts +0 -44
package/src/dom.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import type { ColorScheme } from "#/appearance";
|
|
2
|
-
|
|
3
|
-
// ── DOM Utilities ────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Applies a color scheme to the DOM by updating the `<html>` element.
|
|
7
|
-
*
|
|
8
|
-
* Updates both:
|
|
9
|
-
* - CSS class (for Tailwind's `dark:` variants)
|
|
10
|
-
* - `color-scheme` style (for native form controls and scrollbars)
|
|
11
|
-
*
|
|
12
|
-
* @since 0.3.16-canary.0
|
|
13
|
-
*/
|
|
14
|
-
export function applyColorScheme(colorScheme: ColorScheme): void {
|
|
15
|
-
if (typeof globalThis.window === "undefined") {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const root = window.document.documentElement;
|
|
20
|
-
|
|
21
|
-
root.classList.remove("light", "dark", "automatic");
|
|
22
|
-
root.classList.add(colorScheme);
|
|
23
|
-
root.style.colorScheme = colorScheme;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Temporarily suppress all CSS transitions during appearance changes.
|
|
28
|
-
*
|
|
29
|
-
* Prevents jarring color animations when switching between light/dark appearances.
|
|
30
|
-
* Respects user's `prefers-reduced-motion` preference (does nothing if enabled).
|
|
31
|
-
*
|
|
32
|
-
* @param nonce - Optional CSP nonce for the injected style element
|
|
33
|
-
* @returns Cleanup function to re-enable transitions. Call after the color scheme is applied.
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```tsx
|
|
37
|
-
* const enableTransitions = suppressTransitions();
|
|
38
|
-
* applyColorScheme('dark');
|
|
39
|
-
* enableTransitions(); // Re-enables CSS transitions
|
|
40
|
-
* ```
|
|
41
|
-
*
|
|
42
|
-
* @since 0.3.16-canary.0
|
|
43
|
-
*/
|
|
44
|
-
export function suppressTransitions(nonce?: string): () => void {
|
|
45
|
-
if (typeof globalThis.window === "undefined") {
|
|
46
|
-
return () => {
|
|
47
|
-
/* noop */
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Respect user's motion preferences
|
|
52
|
-
const prefersReducedMotion = globalThis.window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
53
|
-
|
|
54
|
-
if (prefersReducedMotion) {
|
|
55
|
-
return () => {
|
|
56
|
-
/* noop */
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Inject style to suppress all transitions
|
|
61
|
-
const css = document.createElement("style");
|
|
62
|
-
|
|
63
|
-
if (nonce) {
|
|
64
|
-
css.setAttribute("nonce", nonce);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Only the unprefixed property is needed — all modern browsers support it
|
|
68
|
-
css.append(document.createTextNode(`*,*::before,*::after{transition:none!important}`));
|
|
69
|
-
|
|
70
|
-
document.head.append(css);
|
|
71
|
-
|
|
72
|
-
return () => {
|
|
73
|
-
// Force reflow to ensure styles are applied before removing
|
|
74
|
-
void window.getComputedStyle(document.body);
|
|
75
|
-
|
|
76
|
-
// Use double RAF to ensure paint happens before removing style
|
|
77
|
-
requestAnimationFrame(() => {
|
|
78
|
-
requestAnimationFrame(() => {
|
|
79
|
-
if (css.parentNode) {
|
|
80
|
-
css.remove();
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
};
|
|
85
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* \@codefast/theme
|
|
3
|
-
*
|
|
4
|
-
* React 19 appearance management, with vocabulary following Apple's Human Interface
|
|
5
|
-
* Guidelines: **appearance** is the user's preference (light / dark / automatic),
|
|
6
|
-
* **color scheme** is the resolved light-or-dark value actually applied. Client-only
|
|
7
|
-
* `localStorage` persistence, SSR-safe `automatic` resolution, cross-tab sync,
|
|
8
|
-
* optimistic updates.
|
|
9
|
-
*
|
|
10
|
-
* - **Root** (this module): `AppearanceProvider`, `useAppearance`, `AppearanceScript`, `resolveColorScheme`, types, defaults.
|
|
11
|
-
* - **`@codefast/theme/color-scheme` / `@codefast/theme/dom`**: `getSystemColorScheme`, `applyColorScheme`, `suppressTransitions`.
|
|
12
|
-
* - **`@codefast/theme/appearance-context`**: `AppearanceContext` for rare custom wiring.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
// Types & Schema
|
|
16
|
-
export type { Appearance, AppearanceContextValue, ColorScheme } from "#/appearance";
|
|
17
|
-
export { appearances, appearanceSchema } from "#/appearance";
|
|
18
|
-
|
|
19
|
-
// Constants
|
|
20
|
-
export { DEFAULT_APPEARANCE, DEFAULT_COLOR_SCHEME } from "#/constants";
|
|
21
|
-
|
|
22
|
-
// Provider & Hook (use `@codefast/theme/appearance-context` for `AppearanceContext`)
|
|
23
|
-
export { AppearanceProvider } from "#/appearance-provider";
|
|
24
|
-
export type { AppearanceProviderProps } from "#/appearance-provider";
|
|
25
|
-
export { useAppearance } from "#/use-appearance";
|
|
26
|
-
|
|
27
|
-
// Script (FOUC prevention)
|
|
28
|
-
export { AppearanceScript } from "#/appearance-script";
|
|
29
|
-
export type { AppearanceScriptProps } from "#/appearance-script";
|
|
30
|
-
|
|
31
|
-
// Resolution: `getSystemColorScheme` lives under `@codefast/theme/color-scheme`
|
|
32
|
-
export { resolveColorScheme } from "#/color-scheme";
|
package/src/use-appearance.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { use } from "react";
|
|
2
|
-
|
|
3
|
-
import type { AppearanceContextValue } from "#/appearance";
|
|
4
|
-
import { AppearanceContext } from "#/appearance-context";
|
|
5
|
-
|
|
6
|
-
// ── Hook ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Hook to access appearance context.
|
|
10
|
-
*
|
|
11
|
-
* Uses React 19's `use()` API for context consumption, enabling:
|
|
12
|
-
* - Conditional context reading
|
|
13
|
-
* - Better Suspense boundary integration
|
|
14
|
-
*
|
|
15
|
-
* @returns Appearance context with `appearance`, `colorScheme`, `setAppearance`, and `isPending`
|
|
16
|
-
* @throws Error if called outside of AppearanceProvider
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```tsx
|
|
20
|
-
* function AppearanceToggle() {
|
|
21
|
-
* const { appearance, setAppearance, isPending } = useAppearance();
|
|
22
|
-
*
|
|
23
|
-
* return (
|
|
24
|
-
* <button
|
|
25
|
-
* onClick={() => setAppearance(appearance === 'dark' ? 'light' : 'dark')}
|
|
26
|
-
* disabled={isPending}
|
|
27
|
-
* >
|
|
28
|
-
* {appearance}
|
|
29
|
-
* </button>
|
|
30
|
-
* );
|
|
31
|
-
* }
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* @since 0.5.0-canary.2
|
|
35
|
-
*/
|
|
36
|
-
export function useAppearance(): AppearanceContextValue {
|
|
37
|
-
const value = use(AppearanceContext);
|
|
38
|
-
|
|
39
|
-
if (!value) {
|
|
40
|
-
throw new Error("useAppearance must be used within an AppearanceProvider");
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return value;
|
|
44
|
-
}
|