@codefast/theme 0.8.1 → 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.
Files changed (48) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +110 -34
  3. package/dist/appearance-context.d.ts +1 -2
  4. package/dist/appearance-context.js +1 -2
  5. package/dist/appearance-provider.d.ts +1 -2
  6. package/dist/appearance-provider.js +1 -2
  7. package/dist/appearance-script.d.ts +1 -2
  8. package/dist/appearance-script.js +1 -2
  9. package/dist/appearance.d.ts +1 -2
  10. package/dist/appearance.js +1 -2
  11. package/dist/color-scheme.d.ts +1 -2
  12. package/dist/color-scheme.js +1 -2
  13. package/dist/constants.d.ts +1 -2
  14. package/dist/constants.js +1 -2
  15. package/dist/dom.d.ts +1 -2
  16. package/dist/dom.js +1 -2
  17. package/dist/index.d.ts +1 -2
  18. package/dist/index.js +1 -2
  19. package/dist/use-appearance.d.ts +1 -2
  20. package/dist/use-appearance.js +1 -2
  21. package/package.json +9 -26
  22. package/dist/appearance-context.d.ts.map +0 -1
  23. package/dist/appearance-context.js.map +0 -1
  24. package/dist/appearance-provider.d.ts.map +0 -1
  25. package/dist/appearance-provider.js.map +0 -1
  26. package/dist/appearance-script.d.ts.map +0 -1
  27. package/dist/appearance-script.js.map +0 -1
  28. package/dist/appearance.d.ts.map +0 -1
  29. package/dist/appearance.js.map +0 -1
  30. package/dist/color-scheme.d.ts.map +0 -1
  31. package/dist/color-scheme.js.map +0 -1
  32. package/dist/constants.d.ts.map +0 -1
  33. package/dist/constants.js.map +0 -1
  34. package/dist/dom.d.ts.map +0 -1
  35. package/dist/dom.js.map +0 -1
  36. package/dist/index.d.ts.map +0 -1
  37. package/dist/index.js.map +0 -1
  38. package/dist/use-appearance.d.ts.map +0 -1
  39. package/dist/use-appearance.js.map +0 -1
  40. package/src/appearance-context.ts +0 -21
  41. package/src/appearance-provider.tsx +0 -368
  42. package/src/appearance-script.tsx +0 -87
  43. package/src/appearance.ts +0 -70
  44. package/src/color-scheme.ts +0 -40
  45. package/src/constants.ts +0 -49
  46. package/src/dom.ts +0 -85
  47. package/src/index.ts +0 -32
  48. 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";
@@ -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
- }