@blankways/ui 0.0.0-stage → 0.1.0-alpha.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.
Files changed (216) hide show
  1. package/README.md +228 -2
  2. package/dist/cli/cli.options.d.ts +5 -0
  3. package/dist/cli/cli.options.js +53 -0
  4. package/dist/cli/cli.registry.d.ts +6 -0
  5. package/dist/cli/cli.registry.js +37 -0
  6. package/dist/cli/cli.types.d.ts +32 -0
  7. package/dist/cli/cli.types.js +1 -0
  8. package/dist/cli/index.d.ts +3 -0
  9. package/dist/cli/index.js +3 -0
  10. package/dist/cli.d.ts +2 -0
  11. package/dist/cli.js +47 -0
  12. package/dist/commands/add.command.d.ts +2 -0
  13. package/dist/commands/add.command.js +101 -0
  14. package/dist/commands/doctor.command.d.ts +3 -0
  15. package/dist/commands/doctor.command.js +92 -0
  16. package/dist/commands/index.d.ts +6 -0
  17. package/dist/commands/index.js +6 -0
  18. package/dist/commands/init.command.d.ts +2 -0
  19. package/dist/commands/init.command.js +38 -0
  20. package/dist/commands/list.command.d.ts +2 -0
  21. package/dist/commands/list.command.js +54 -0
  22. package/dist/commands/meta.command.d.ts +1 -0
  23. package/dist/commands/meta.command.js +19 -0
  24. package/dist/commands/styles.command.d.ts +6 -0
  25. package/dist/commands/styles.command.js +68 -0
  26. package/dist/config/config.helpers.d.ts +7 -0
  27. package/dist/config/config.helpers.js +63 -0
  28. package/dist/config/framework.helpers.d.ts +3 -0
  29. package/dist/config/framework.helpers.js +14 -0
  30. package/dist/config/index.d.ts +2 -0
  31. package/dist/config/index.js +2 -0
  32. package/dist/constants/cli.constants.d.ts +9 -0
  33. package/dist/constants/cli.constants.js +15 -0
  34. package/dist/constants/framework.constants.d.ts +3 -0
  35. package/dist/constants/framework.constants.js +2 -0
  36. package/dist/constants/index.d.ts +2 -0
  37. package/dist/constants/index.js +2 -0
  38. package/dist/doctor/checks/entries.check.d.ts +1 -0
  39. package/dist/doctor/checks/entries.check.js +21 -0
  40. package/dist/doctor/checks/icons.check.d.ts +1 -0
  41. package/dist/doctor/checks/icons.check.js +55 -0
  42. package/dist/doctor/checks/imports.check.d.ts +1 -0
  43. package/dist/doctor/checks/imports.check.js +45 -0
  44. package/dist/doctor/checks/index.d.ts +7 -0
  45. package/dist/doctor/checks/index.js +7 -0
  46. package/dist/doctor/checks/motion.check.d.ts +1 -0
  47. package/dist/doctor/checks/motion.check.js +66 -0
  48. package/dist/doctor/checks/peers.check.d.ts +1 -0
  49. package/dist/doctor/checks/peers.check.js +55 -0
  50. package/dist/doctor/checks/styles.check.d.ts +1 -0
  51. package/dist/doctor/checks/styles.check.js +45 -0
  52. package/dist/doctor/checks/themes.check.d.ts +1 -0
  53. package/dist/doctor/checks/themes.check.js +64 -0
  54. package/dist/doctor/doctor.helpers.d.ts +6 -0
  55. package/dist/doctor/doctor.helpers.js +39 -0
  56. package/dist/doctor/doctor.registry.d.ts +10 -0
  57. package/dist/doctor/doctor.registry.js +24 -0
  58. package/dist/doctor/doctor.types.d.ts +27 -0
  59. package/dist/doctor/doctor.types.js +1 -0
  60. package/dist/doctor/index.d.ts +3 -0
  61. package/dist/doctor/index.js +3 -0
  62. package/dist/errors/cli-error.d.ts +3 -0
  63. package/dist/errors/cli-error.js +6 -0
  64. package/dist/errors/index.d.ts +1 -0
  65. package/dist/errors/index.js +1 -0
  66. package/dist/helpers/args.helpers.d.ts +4 -0
  67. package/dist/helpers/args.helpers.js +75 -0
  68. package/dist/helpers/help.helpers.d.ts +1 -0
  69. package/dist/helpers/help.helpers.js +30 -0
  70. package/dist/helpers/index.d.ts +5 -0
  71. package/dist/helpers/index.js +5 -0
  72. package/dist/helpers/package.helpers.d.ts +2 -0
  73. package/dist/helpers/package.helpers.js +12 -0
  74. package/dist/helpers/path.helpers.d.ts +3 -0
  75. package/dist/helpers/path.helpers.js +15 -0
  76. package/dist/helpers/styles.helpers.d.ts +21 -0
  77. package/dist/helpers/styles.helpers.js +164 -0
  78. package/dist/index.d.ts +9 -0
  79. package/dist/index.js +9 -0
  80. package/dist/registry/index.d.ts +1 -0
  81. package/dist/registry/index.js +1 -0
  82. package/dist/registry/registry.helpers.d.ts +22 -0
  83. package/dist/registry/registry.helpers.js +328 -0
  84. package/dist/registry-data/README.md +71 -0
  85. package/dist/registry-data/avatar/Avatar.tsx +64 -0
  86. package/dist/registry-data/avatar/component.json +30 -0
  87. package/dist/registry-data/avatar/index.ts +1 -0
  88. package/dist/registry-data/badge/Badge.tsx +76 -0
  89. package/dist/registry-data/badge/component.json +30 -0
  90. package/dist/registry-data/badge/index.ts +1 -0
  91. package/dist/registry-data/brand-logo/BrandLogo.tsx +24 -0
  92. package/dist/registry-data/brand-logo/component.json +26 -0
  93. package/dist/registry-data/brand-logo/index.ts +1 -0
  94. package/dist/registry-data/button/Button.tsx +76 -0
  95. package/dist/registry-data/button/component.json +33 -0
  96. package/dist/registry-data/button/index.ts +1 -0
  97. package/dist/registry-data/button-link/ButtonLink.tsx +83 -0
  98. package/dist/registry-data/button-link/component.json +33 -0
  99. package/dist/registry-data/button-link/index.ts +5 -0
  100. package/dist/registry-data/button-styles/component.json +19 -0
  101. package/dist/registry-data/button-styles/index.ts +45 -0
  102. package/dist/registry-data/cn/cn.ts +6 -0
  103. package/dist/registry-data/cn/component.json +16 -0
  104. package/dist/registry-data/confirm-dialog/ConfirmDialog.tsx +134 -0
  105. package/dist/registry-data/confirm-dialog/component.json +34 -0
  106. package/dist/registry-data/confirm-dialog/index.ts +1 -0
  107. package/dist/registry-data/container/Container.tsx +22 -0
  108. package/dist/registry-data/container/component.json +28 -0
  109. package/dist/registry-data/container/index.ts +1 -0
  110. package/dist/registry-data/cursor-glow/CursorGlow.tsx +29 -0
  111. package/dist/registry-data/cursor-glow/component.json +28 -0
  112. package/dist/registry-data/cursor-glow/index.ts +1 -0
  113. package/dist/registry-data/dropdown-menu/DropdownMenu.tsx +157 -0
  114. package/dist/registry-data/dropdown-menu/component.json +34 -0
  115. package/dist/registry-data/dropdown-menu/index.ts +5 -0
  116. package/dist/registry-data/empty-state/EmptyState.tsx +92 -0
  117. package/dist/registry-data/empty-state/component.json +30 -0
  118. package/dist/registry-data/empty-state/index.ts +5 -0
  119. package/dist/registry-data/eyebrow/Eyebrow.tsx +68 -0
  120. package/dist/registry-data/eyebrow/component.json +30 -0
  121. package/dist/registry-data/eyebrow/index.ts +1 -0
  122. package/dist/registry-data/field-styles/component.json +19 -0
  123. package/dist/registry-data/field-styles/index.ts +42 -0
  124. package/dist/registry-data/form-dialog/FormDialog.tsx +128 -0
  125. package/dist/registry-data/form-dialog/component.json +34 -0
  126. package/dist/registry-data/form-dialog/index.ts +1 -0
  127. package/dist/registry-data/icons/component.json +22 -0
  128. package/dist/registry-data/icons/index.ts +21 -0
  129. package/dist/registry-data/icons-form/component.json +17 -0
  130. package/dist/registry-data/icons-form/index.ts +1 -0
  131. package/dist/registry-data/icons-sidebar/component.json +17 -0
  132. package/dist/registry-data/icons-sidebar/index.ts +1 -0
  133. package/dist/registry-data/icons-theme/component.json +17 -0
  134. package/dist/registry-data/icons-theme/index.ts +1 -0
  135. package/dist/registry-data/icons-toast/component.json +17 -0
  136. package/dist/registry-data/icons-toast/index.ts +1 -0
  137. package/dist/registry-data/info-dialog/InfoDialog.tsx +119 -0
  138. package/dist/registry-data/info-dialog/component.json +36 -0
  139. package/dist/registry-data/info-dialog/index.ts +1 -0
  140. package/dist/registry-data/input/Input.tsx +96 -0
  141. package/dist/registry-data/input/component.json +33 -0
  142. package/dist/registry-data/input/index.ts +1 -0
  143. package/dist/registry-data/multi-combobox/MultiCombobox.tsx +205 -0
  144. package/dist/registry-data/multi-combobox/component.json +35 -0
  145. package/dist/registry-data/multi-combobox/index.ts +5 -0
  146. package/dist/registry-data/remote-provider/RemoteProvider.tsx +8 -0
  147. package/dist/registry-data/remote-provider/component.json +25 -0
  148. package/dist/registry-data/remote-provider/index.ts +3 -0
  149. package/dist/registry-data/reveal/Reveal.tsx +22 -0
  150. package/dist/registry-data/reveal/component.json +30 -0
  151. package/dist/registry-data/reveal/index.ts +1 -0
  152. package/dist/registry-data/section/Section.tsx +80 -0
  153. package/dist/registry-data/section/component.json +30 -0
  154. package/dist/registry-data/section/index.ts +1 -0
  155. package/dist/registry-data/section-heading/SectionHeading.tsx +105 -0
  156. package/dist/registry-data/section-heading/component.json +32 -0
  157. package/dist/registry-data/section-heading/index.ts +1 -0
  158. package/dist/registry-data/select/Select.tsx +197 -0
  159. package/dist/registry-data/select/component.json +34 -0
  160. package/dist/registry-data/select/index.ts +1 -0
  161. package/dist/registry-data/sidebar/Sidebar.tsx +432 -0
  162. package/dist/registry-data/sidebar/SidebarCollapsible.tsx +77 -0
  163. package/dist/registry-data/sidebar/SidebarParts.tsx +373 -0
  164. package/dist/registry-data/sidebar/component.json +47 -0
  165. package/dist/registry-data/sidebar/index.ts +4 -0
  166. package/dist/registry-data/sidebar/useSidebar.ts +29 -0
  167. package/dist/registry-data/styles-base/component.json +19 -0
  168. package/dist/registry-data/styles-base/index.css +67 -0
  169. package/dist/registry-data/styles-components/component.json +19 -0
  170. package/dist/registry-data/styles-components/index.css +30 -0
  171. package/dist/registry-data/styles-motion/component.json +20 -0
  172. package/dist/registry-data/styles-motion/index.css +381 -0
  173. package/dist/registry-data/styles-tokens/component.json +17 -0
  174. package/dist/registry-data/styles-tokens/index.css +77 -0
  175. package/dist/registry-data/styles-utilities/component.json +19 -0
  176. package/dist/registry-data/styles-utilities/index.css +29 -0
  177. package/dist/registry-data/switch/Switch.tsx +91 -0
  178. package/dist/registry-data/switch/component.json +34 -0
  179. package/dist/registry-data/switch/index.ts +1 -0
  180. package/dist/registry-data/tabs/Tabs.tsx +226 -0
  181. package/dist/registry-data/tabs/component.json +32 -0
  182. package/dist/registry-data/tabs/index.ts +1 -0
  183. package/dist/registry-data/textarea/Textarea.tsx +94 -0
  184. package/dist/registry-data/textarea/component.json +33 -0
  185. package/dist/registry-data/textarea/index.ts +1 -0
  186. package/dist/registry-data/theme-core/component.json +17 -0
  187. package/dist/registry-data/theme-core/theme.ts +84 -0
  188. package/dist/registry-data/theme-dark/component.json +19 -0
  189. package/dist/registry-data/theme-dark/index.ts +7 -0
  190. package/dist/registry-data/theme-light/component.json +19 -0
  191. package/dist/registry-data/theme-light/index.ts +7 -0
  192. package/dist/registry-data/theme-provider/ThemeContext.tsx +177 -0
  193. package/dist/registry-data/theme-provider/component.json +30 -0
  194. package/dist/registry-data/theme-provider/index.ts +2 -0
  195. package/dist/registry-data/theme-selector/ThemeSelector.tsx +269 -0
  196. package/dist/registry-data/theme-selector/component.json +35 -0
  197. package/dist/registry-data/theme-selector/selector.ts +1 -0
  198. package/dist/registry-data/theme-system/component.json +19 -0
  199. package/dist/registry-data/theme-system/index.ts +7 -0
  200. package/dist/registry-data/theme-toggle/ThemeToggle.tsx +71 -0
  201. package/dist/registry-data/theme-toggle/component.json +31 -0
  202. package/dist/registry-data/theme-toggle/toggle.ts +1 -0
  203. package/dist/registry-data/toast/Toast.tsx +260 -0
  204. package/dist/registry-data/toast/ToastTypeIcon.tsx +83 -0
  205. package/dist/registry-data/toast/component.json +43 -0
  206. package/dist/registry-data/toast/index.ts +3 -0
  207. package/dist/registry-data/toast/useToast.ts +47 -0
  208. package/dist/types/cli.types.d.ts +6 -0
  209. package/dist/types/cli.types.js +1 -0
  210. package/dist/types/config.types.d.ts +41 -0
  211. package/dist/types/config.types.js +1 -0
  212. package/dist/types/index.d.ts +3 -0
  213. package/dist/types/index.js +3 -0
  214. package/dist/types/registry.types.d.ts +37 -0
  215. package/dist/types/registry.types.js +1 -0
  216. package/package.json +35 -3
@@ -0,0 +1,177 @@
1
+ import { State, type StateId } from '@blankways/state'
2
+ import { useCompute, useState } from '@blankways/state-react'
3
+ import {
4
+ createContext,
5
+ useCallback,
6
+ useContext,
7
+ useEffect,
8
+ useMemo,
9
+ type ReactNode,
10
+ } from 'react'
11
+ import {
12
+ DEFAULT_THEMES,
13
+ DARK_THEME,
14
+ getStoredPreference,
15
+ LIGHT_THEME,
16
+ normalizePreference,
17
+ resolveTheme,
18
+ systemTheme,
19
+ THEME_STORAGE_KEY,
20
+ type SystemThemes,
21
+ type Theme,
22
+ type ThemePreference,
23
+ } from './theme.js'
24
+
25
+ const DEFAULT_SYSTEM_THEMES: SystemThemes = {
26
+ light: LIGHT_THEME,
27
+ dark: DARK_THEME,
28
+ }
29
+
30
+ const DEFAULT_THEME_COLORS: Partial<Record<Theme, string>> = {
31
+ [LIGHT_THEME]: '#ffffff',
32
+ [DARK_THEME]: '#0a0a0a',
33
+ }
34
+
35
+ export type ThemeContextValue = {
36
+ /** Reactive concrete theme, never `system`. */
37
+ theme: StateId<Theme>
38
+ /** Reactive visitor choice, which may be `system` or any registered theme. */
39
+ preference: StateId<ThemePreference>
40
+ /** Themes this provider accepts. */
41
+ themes: readonly Theme[]
42
+ /** How the OS light/dark preference maps to registered themes. */
43
+ systemThemes: SystemThemes
44
+ /** Persists and applies a new preference. */
45
+ setPreference: (preference: ThemePreference) => void
46
+ }
47
+
48
+ export type ThemeProviderProps = {
49
+ children: ReactNode
50
+ themes?: readonly Theme[]
51
+ defaultTheme?: Theme
52
+ storageKey?: string
53
+ systemThemes?: SystemThemes
54
+ themeColors?: Partial<Record<Theme, string>>
55
+ }
56
+
57
+ export const ThemeContext = createContext<ThemeContextValue | null>(null)
58
+
59
+ /** Reads the active theme context. */
60
+ export function useTheme(): ThemeContextValue {
61
+ const context = useContext(ThemeContext)
62
+ if (!context) {
63
+ throw new Error('useTheme must be used within a ThemeProvider')
64
+ }
65
+ return context
66
+ }
67
+
68
+ /** Applies a string theme to `<html data-theme>` and persists the choice. */
69
+ export function ThemeProvider({
70
+ children,
71
+ themes = DEFAULT_THEMES,
72
+ defaultTheme = DEFAULT_THEMES[0],
73
+ storageKey = THEME_STORAGE_KEY,
74
+ systemThemes = DEFAULT_SYSTEM_THEMES,
75
+ themeColors,
76
+ }: ThemeProviderProps) {
77
+ const colors = useMemo(
78
+ () => ({ ...DEFAULT_THEME_COLORS, ...themeColors }),
79
+ [themeColors],
80
+ )
81
+ const preference = useState<ThemePreference>(
82
+ normalizePreference(getStoredPreference(storageKey), themes, defaultTheme),
83
+ )
84
+ const system = useState<Theme>(systemTheme(systemThemes))
85
+ const theme = useCompute(
86
+ [preference, system] as const,
87
+ ([currentPreference, currentSystem]): Theme =>
88
+ resolveTheme(currentPreference, currentSystem, defaultTheme),
89
+ )
90
+
91
+ const setPreference = useCallback(
92
+ (next: ThemePreference) => {
93
+ State.set(preference, normalizePreference(next, themes, defaultTheme))
94
+ },
95
+ [defaultTheme, preference, themes],
96
+ )
97
+
98
+ const value = useMemo<ThemeContextValue>(
99
+ () => ({ theme, preference, themes, systemThemes, setPreference }),
100
+ [preference, setPreference, systemThemes, theme, themes],
101
+ )
102
+
103
+ useEffect(() => {
104
+ if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') {
105
+ return
106
+ }
107
+
108
+ const query = window.matchMedia('(prefers-color-scheme: dark)')
109
+ const onChange = () =>
110
+ State.set(system, query.matches ? systemThemes.dark : systemThemes.light)
111
+ onChange()
112
+ query.addEventListener('change', onChange)
113
+ return () => query.removeEventListener('change', onChange)
114
+ }, [system, systemThemes])
115
+
116
+ useEffect(() => {
117
+ const applyTheme = (current: Theme, animate = false) => {
118
+ if (typeof document === 'undefined') return
119
+
120
+ const run = () => {
121
+ const root = document.documentElement
122
+ root.dataset.theme = current
123
+ root.style.colorScheme = current === systemThemes.dark ? 'dark' : 'light'
124
+ document
125
+ .querySelector('meta[name="theme-color"]')
126
+ ?.setAttribute('content', colors[current] ?? '#ffffff')
127
+ }
128
+
129
+ const reduceMotion =
130
+ typeof window !== 'undefined' &&
131
+ typeof window.matchMedia === 'function' &&
132
+ window.matchMedia('(prefers-reduced-motion: reduce)').matches
133
+ const withTransitions = document as Document & {
134
+ startViewTransition?: (callback: () => void) => void
135
+ }
136
+
137
+ if (
138
+ animate &&
139
+ !reduceMotion &&
140
+ typeof withTransitions.startViewTransition === 'function'
141
+ ) {
142
+ withTransitions.startViewTransition(run)
143
+ } else {
144
+ run()
145
+ }
146
+ }
147
+
148
+ const persistPreference = (current: ThemePreference) => {
149
+ if (typeof window === 'undefined') return
150
+ try {
151
+ if (current === 'system') {
152
+ window.localStorage.removeItem(storageKey)
153
+ } else {
154
+ window.localStorage.setItem(storageKey, current)
155
+ }
156
+ } catch {
157
+ /* storage unavailable: the choice lasts for this visit only */
158
+ }
159
+ }
160
+
161
+ applyTheme(State.get(theme))
162
+
163
+ const themeWatcher = State.watch(theme, () => {
164
+ applyTheme(State.get(theme), true)
165
+ })
166
+ const preferenceWatcher = State.watch(preference, () => {
167
+ persistPreference(State.get(preference))
168
+ })
169
+
170
+ return () => {
171
+ State.unwatch(themeWatcher)
172
+ State.unwatch(preferenceWatcher)
173
+ }
174
+ }, [colors, preference, storageKey, systemThemes, theme])
175
+
176
+ return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>
177
+ }
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "theme-provider",
3
+ "kind": "theme",
4
+ "framework": "react",
5
+ "description": "React context provider that applies and persists the active theme.",
6
+ "dependencies": [
7
+ "theme-core",
8
+ "styles-motion"
9
+ ],
10
+ "peers": [
11
+ "@blankways/state",
12
+ "@blankways/state-react@0.1.0-alpha.1",
13
+ "react",
14
+ "react-dom"
15
+ ],
16
+ "suggestions": [
17
+ "theme-toggle",
18
+ "styles-motion"
19
+ ],
20
+ "files": [
21
+ {
22
+ "source": "ThemeContext.tsx",
23
+ "target": "themes/ThemeContext.tsx"
24
+ },
25
+ {
26
+ "source": "index.ts",
27
+ "target": "themes/index.ts"
28
+ }
29
+ ]
30
+ }
@@ -0,0 +1,2 @@
1
+ export * from './theme.js'
2
+ export * from './ThemeContext.js'
@@ -0,0 +1,269 @@
1
+ import type { StateId } from '@blankways/state'
2
+ import { Show, useAttributes, useMatch } from '@blankways/state-react'
3
+ import { useId, useMemo, type CSSProperties } from 'react'
4
+ import { cn } from '../internals/cn.js'
5
+ import { Check } from '../icons/form.js'
6
+ import { useTheme } from './ThemeContext.js'
7
+ import {
8
+ DARK_THEME,
9
+ LIGHT_THEME,
10
+ SYSTEM_PREFERENCE,
11
+ type SystemThemes,
12
+ type Theme,
13
+ type ThemePreference,
14
+ } from './theme.js'
15
+
16
+ type Palette = {
17
+ paper: string
18
+ surface: string
19
+ line: string
20
+ ink: string
21
+ muted: string
22
+ }
23
+
24
+ export type ThemePaletteOverrides = Partial<Record<Theme, Partial<Palette>>>
25
+
26
+ /** Default palettes for the automatically registered themes. */
27
+ const DEFAULT_PALETTES: Record<string, Palette> = {
28
+ [LIGHT_THEME]: {
29
+ paper: '#ffffff',
30
+ surface: '#f7f7f7',
31
+ line: '#e4e4e4',
32
+ ink: '#111111',
33
+ muted: '#d4d4d4',
34
+ },
35
+ [DARK_THEME]: {
36
+ paper: '#0a0a0a',
37
+ surface: '#121212',
38
+ line: '#2a2a2a',
39
+ ink: '#f5f5f5',
40
+ muted: '#3f3f3f',
41
+ },
42
+ }
43
+
44
+ const CUSTOM_PALETTE: Palette = {
45
+ paper: '#1f1f1f',
46
+ surface: '#2e2e2e',
47
+ line: '#454545',
48
+ ink: '#f5f5f5',
49
+ muted: '#6e6e6e',
50
+ }
51
+
52
+ function paletteFor(theme: Theme, overrides: ThemePaletteOverrides): Palette {
53
+ return {
54
+ ...(DEFAULT_PALETTES[theme] ?? CUSTOM_PALETTE),
55
+ ...overrides[theme],
56
+ }
57
+ }
58
+
59
+ function MiniWindow({
60
+ theme,
61
+ palettes,
62
+ }: {
63
+ theme: Theme
64
+ palettes: ThemePaletteOverrides
65
+ }) {
66
+ const palette = paletteFor(theme, palettes)
67
+
68
+ return (
69
+ <div
70
+ aria-hidden="true"
71
+ className="absolute inset-0 flex"
72
+ style={{ background: palette.paper }}
73
+ >
74
+ <div
75
+ className="flex w-1/4 flex-col gap-1.5 p-1.5"
76
+ style={{
77
+ background: palette.surface,
78
+ borderRight: `1px solid ${palette.line}`,
79
+ }}
80
+ >
81
+ <span className="h-1.5 w-3/4 rounded-full bg-brand-500" />
82
+ <span className="h-1 rounded-full" style={{ background: palette.muted }} />
83
+ <span
84
+ className="h-1 w-2/3 rounded-full"
85
+ style={{ background: palette.muted }}
86
+ />
87
+ </div>
88
+ <div className="flex flex-1 flex-col gap-2 p-[0.5625rem]">
89
+ <span
90
+ className="h-2 w-1/3 rounded-full"
91
+ style={{ background: palette.ink }}
92
+ />
93
+ <div className="grid grid-cols-2 gap-1.5">
94
+ <span
95
+ className="h-7 rounded-2xl"
96
+ style={{
97
+ background: palette.surface,
98
+ border: `1px solid ${palette.line}`,
99
+ }}
100
+ />
101
+ <span
102
+ className="h-7 rounded-2xl"
103
+ style={{
104
+ background: palette.surface,
105
+ border: `1px solid ${palette.line}`,
106
+ }}
107
+ />
108
+ </div>
109
+ <span className="h-1.5 rounded-full" style={{ background: palette.muted }} />
110
+ <span
111
+ className="h-1.5 w-4/5 rounded-full"
112
+ style={{ background: palette.muted }}
113
+ />
114
+ </div>
115
+ </div>
116
+ )
117
+ }
118
+
119
+ function ThemePreview({
120
+ preference,
121
+ palettes,
122
+ systemThemes,
123
+ }: {
124
+ preference: ThemePreference
125
+ palettes: ThemePaletteOverrides
126
+ systemThemes: SystemThemes
127
+ }) {
128
+ return (
129
+ <div className="relative aspect-[16/10] overflow-hidden rounded-2xl border border-line">
130
+ {preference === SYSTEM_PREFERENCE ? (
131
+ <>
132
+ <MiniWindow theme={systemThemes.light} palettes={palettes} />
133
+ <div
134
+ className="absolute inset-0"
135
+ style={{ clipPath: 'polygon(100% 0, 100% 100%, 0 100%)' }}
136
+ >
137
+ <MiniWindow theme={systemThemes.dark} palettes={palettes} />
138
+ </div>
139
+ </>
140
+ ) : (
141
+ <MiniWindow theme={preference} palettes={palettes} />
142
+ )}
143
+ </div>
144
+ )
145
+ }
146
+
147
+ function labelFor(preference: ThemePreference) {
148
+ if (preference === LIGHT_THEME) return 'Light'
149
+ if (preference === DARK_THEME) return 'Dark'
150
+ if (preference === SYSTEM_PREFERENCE) return 'System'
151
+ return preference
152
+ .replace(/[-_]/g, ' ')
153
+ .replace(/\b\w/g, character => character.toUpperCase())
154
+ }
155
+
156
+ function OptionCard({
157
+ option,
158
+ name,
159
+ index,
160
+ checked,
161
+ label,
162
+ palettes,
163
+ systemThemes,
164
+ onSelect,
165
+ }: {
166
+ option: ThemePreference
167
+ name: string
168
+ index: number
169
+ checked: StateId<boolean>
170
+ label: string
171
+ palettes: ThemePaletteOverrides
172
+ systemThemes: SystemThemes
173
+ onSelect: (option: ThemePreference) => void
174
+ }) {
175
+ const bind = useAttributes<HTMLInputElement>({ checked })
176
+
177
+ return (
178
+ <label
179
+ className="motion-lift group flex h-full w-full cursor-pointer flex-col gap-3 rounded-2xl border border-line bg-surface p-[0.5625rem] text-left hover:border-line-strong focus-within:ring-2 focus-within:ring-brand-500 has-checked:border-brand-500"
180
+ style={{ '--index': index } as CSSProperties}
181
+ >
182
+ <ThemePreview
183
+ preference={option}
184
+ palettes={palettes}
185
+ systemThemes={systemThemes}
186
+ />
187
+ <span className="flex items-center justify-between px-[0.188rem] text-sm font-medium text-ink">
188
+ <span>{label}</span>
189
+ <span className="relative inline-flex size-5 items-center justify-center rounded-full border border-line-strong">
190
+ <input
191
+ ref={bind}
192
+ type="radio"
193
+ name={name}
194
+ value={option}
195
+ className="peer sr-only"
196
+ onChange={() => onSelect(option)}
197
+ />
198
+ <Show when={checked}>
199
+ <span className="motion-icon-swap check-draw absolute inset-[-1px] inline-flex items-center justify-center rounded-full bg-brand-500 text-[#111111]">
200
+ <Check className="size-3" strokeWidth={3} aria-hidden="true" />
201
+ </span>
202
+ </Show>
203
+ </span>
204
+ </span>
205
+ </label>
206
+ )
207
+ }
208
+
209
+ export type ThemeSelectorProps = {
210
+ /** Accessible name of the radio group. */
211
+ label: string
212
+ /** Themes to offer. Defaults to the provider's themes. */
213
+ themes?: readonly Theme[]
214
+ /** Adds the `system` preference. Enabled by default. */
215
+ includeSystem?: boolean
216
+ /** Overrides labels for any preference. */
217
+ labels?: Partial<Record<ThemePreference, string>>
218
+ /** Overrides preview colors for any registered theme. */
219
+ palettes?: ThemePaletteOverrides
220
+ className?: string
221
+ }
222
+
223
+ /** Cards with a mini preview for every registered theme plus `system`. */
224
+ export function ThemeSelector({
225
+ label,
226
+ themes,
227
+ includeSystem = true,
228
+ labels,
229
+ palettes = {},
230
+ className,
231
+ }: ThemeSelectorProps) {
232
+ const {
233
+ themes: contextThemes,
234
+ systemThemes,
235
+ preference,
236
+ setPreference,
237
+ } = useTheme()
238
+ const providerThemes = themes ?? contextThemes
239
+ const options = useMemo<readonly ThemePreference[]>(
240
+ () =>
241
+ includeSystem
242
+ ? [...providerThemes, SYSTEM_PREFERENCE]
243
+ : [...providerThemes],
244
+ [includeSystem, providerThemes],
245
+ )
246
+ const match = useMatch(preference, options)
247
+ const name = useId()
248
+
249
+ return (
250
+ <fieldset className={cn('min-w-0', className)}>
251
+ <legend className="sr-only">{label}</legend>
252
+ <div className="stagger-children grid gap-4 sm:grid-cols-3">
253
+ {options.map((option, index) => (
254
+ <OptionCard
255
+ key={option}
256
+ option={option}
257
+ name={name}
258
+ index={index}
259
+ checked={match[index]}
260
+ label={labels?.[option] ?? labelFor(option)}
261
+ palettes={palettes}
262
+ systemThemes={systemThemes}
263
+ onSelect={setPreference}
264
+ />
265
+ ))}
266
+ </div>
267
+ </fieldset>
268
+ )
269
+ }
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "theme-selector",
3
+ "kind": "theme",
4
+ "framework": "react",
5
+ "description": "Accessible cards for choosing any registered string theme.",
6
+ "dependencies": [
7
+ "theme-provider",
8
+ "icons-form",
9
+ "cn",
10
+ "styles-motion"
11
+ ],
12
+ "peers": [
13
+ "@blankways/state",
14
+ "@blankways/state-react@0.1.0-alpha.1",
15
+ "lucide-react",
16
+ "react"
17
+ ],
18
+ "suggestions": [
19
+ "styles-tokens",
20
+ "styles-base",
21
+ "styles-components",
22
+ "styles-utilities",
23
+ "styles-motion"
24
+ ],
25
+ "files": [
26
+ {
27
+ "source": "ThemeSelector.tsx",
28
+ "target": "themes/ThemeSelector.tsx"
29
+ },
30
+ {
31
+ "source": "selector.ts",
32
+ "target": "themes/selector.ts"
33
+ }
34
+ ]
35
+ }
@@ -0,0 +1 @@
1
+ export * from './ThemeSelector.js'
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "theme-system",
3
+ "kind": "theme",
4
+ "framework": "react",
5
+ "description": "System theme preference constant.",
6
+ "dependencies": [
7
+ "theme-core"
8
+ ],
9
+ "peers": [
10
+ "react"
11
+ ],
12
+ "suggestions": [],
13
+ "files": [
14
+ {
15
+ "source": "index.ts",
16
+ "target": "themes/system.ts"
17
+ }
18
+ ]
19
+ }
@@ -0,0 +1,7 @@
1
+ import type { ThemePreference } from './theme.js'
2
+
3
+ /** Preference that follows the operating system setting. */
4
+ export const SYSTEM_THEME_PREFERENCE: ThemePreference = 'system'
5
+
6
+ /** Alias used by theme controls that expose a system option. */
7
+ export const systemPreference: ThemePreference = 'system'
@@ -0,0 +1,71 @@
1
+ import { State } from '@blankways/state'
2
+ import { Show, useAttributes, useCompute, useMatch } from '@blankways/state-react'
3
+ import { useMemo, useRef, type ReactNode } from 'react'
4
+ import { cn } from '../internals/cn.js'
5
+ import { Moon, Sun } from '../icons/theme.js'
6
+ import { useTheme } from './ThemeContext.js'
7
+ import { DARK_THEME, LIGHT_THEME, type Theme } from './theme.js'
8
+
9
+ const DEFAULT_ICONS: Partial<Record<Theme, ReactNode>> = {
10
+ [LIGHT_THEME]: <Sun className="size-5" />,
11
+ [DARK_THEME]: <Moon className="size-5" />,
12
+ }
13
+
14
+ export type ThemeToggleProps = {
15
+ className?: string
16
+ /** Themes to cycle through. Defaults to the provider's themes. */
17
+ themes?: readonly Theme[]
18
+ /** Overrides the icon shown for a theme. */
19
+ icons?: Partial<Record<Theme, ReactNode>>
20
+ }
21
+
22
+ /** Accessible button that cycles through the registered themes. */
23
+ export function ThemeToggle({
24
+ className,
25
+ themes,
26
+ icons,
27
+ }: ThemeToggleProps) {
28
+ const { theme, themes: contextThemes, setPreference } = useTheme()
29
+ const options = useMemo(
30
+ () => (themes && themes.length > 0 ? themes : contextThemes),
31
+ [contextThemes, themes],
32
+ )
33
+ const matches = useMatch(theme, options)
34
+ const buttonRef = useRef<HTMLButtonElement>(null)
35
+ const label = useCompute(
36
+ [theme] as const,
37
+ ([current]): string => `Switch theme, current theme: ${current}`,
38
+ )
39
+ useAttributes(buttonRef, { 'aria-label': label, 'data-theme': theme })
40
+
41
+ const cycle = () => {
42
+ const current = State.get(theme)
43
+ const index = options.indexOf(current)
44
+ const next = options[(index + 1) % options.length] ?? options[0] ?? LIGHT_THEME
45
+ setPreference(next)
46
+ }
47
+
48
+ const mergedIcons = useMemo(() => ({ ...DEFAULT_ICONS, ...icons }), [icons])
49
+
50
+ return (
51
+ <button
52
+ ref={buttonRef}
53
+ type="button"
54
+ onClick={cycle}
55
+ className={cn(
56
+ 'motion-press inline-flex size-10 items-center justify-center rounded-2xl border border-line bg-paper text-ink hover:bg-surface',
57
+ className,
58
+ )}
59
+ >
60
+ <span aria-hidden="true">
61
+ {options.map((option, index) => (
62
+ <Show key={option} when={matches[index]}>
63
+ <span className="motion-icon-swap">
64
+ {mergedIcons[option] ?? <Sun className="size-5" />}
65
+ </span>
66
+ </Show>
67
+ ))}
68
+ </span>
69
+ </button>
70
+ )
71
+ }
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "theme-toggle",
3
+ "kind": "theme",
4
+ "framework": "react",
5
+ "description": "Button that cycles through any registered string themes.",
6
+ "dependencies": [
7
+ "theme-provider",
8
+ "icons-theme",
9
+ "cn",
10
+ "styles-motion"
11
+ ],
12
+ "peers": [
13
+ "@blankways/state",
14
+ "@blankways/state-react@0.1.0-alpha.1",
15
+ "lucide-react",
16
+ "react"
17
+ ],
18
+ "suggestions": [
19
+ "styles-motion"
20
+ ],
21
+ "files": [
22
+ {
23
+ "source": "ThemeToggle.tsx",
24
+ "target": "themes/ThemeToggle.tsx"
25
+ },
26
+ {
27
+ "source": "toggle.ts",
28
+ "target": "themes/toggle.ts"
29
+ }
30
+ ]
31
+ }
@@ -0,0 +1 @@
1
+ export * from './ThemeToggle.js'