menhir 0.0.0 → 0.1.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 (242) hide show
  1. package/LICENSE +0 -28
  2. package/README.md +53 -1
  3. package/dist/accordion/header/AccordionHeader.js +22 -0
  4. package/dist/accordion/index.js +2 -0
  5. package/dist/accordion/index.parts.js +6 -0
  6. package/dist/accordion/item/AccordionItem.js +51 -0
  7. package/dist/accordion/item/AccordionItemContext.js +10 -0
  8. package/dist/accordion/panel/AccordionPanel.js +32 -0
  9. package/dist/accordion/root/AccordionRoot.js +45 -0
  10. package/dist/accordion/root/AccordionRootContext.js +10 -0
  11. package/dist/accordion/trigger/AccordionTrigger.js +39 -0
  12. package/dist/checkbox/index.js +2 -0
  13. package/dist/checkbox/index.parts.js +3 -0
  14. package/dist/checkbox/indicator/CheckboxIndicator.js +20 -0
  15. package/dist/checkbox/root/CheckboxRoot.js +136 -0
  16. package/dist/checkbox/root/CheckboxRootContext.js +10 -0
  17. package/dist/collapsible/index.js +2 -0
  18. package/dist/collapsible/index.parts.js +4 -0
  19. package/dist/collapsible/panel/CollapsiblePanel.js +27 -0
  20. package/dist/collapsible/root/CollapsibleRoot.js +53 -0
  21. package/dist/collapsible/root/CollapsibleRootContext.js +10 -0
  22. package/dist/collapsible/trigger/CollapsibleTrigger.js +31 -0
  23. package/dist/color-scheme/colorScheme.js +28 -0
  24. package/dist/color-scheme/colorSchemeScript.js +18 -0
  25. package/dist/color-scheme/cookie/cookie.js +50 -0
  26. package/dist/color-scheme/cookie/index.js +3 -0
  27. package/dist/color-scheme/createColorScheme.js +41 -0
  28. package/dist/color-scheme/local-storage/index.js +3 -0
  29. package/dist/color-scheme/local-storage/localStorage.js +38 -0
  30. package/dist/core/button.js +56 -0
  31. package/dist/core/createControllableSignal.js +50 -0
  32. package/dist/core/listValue.js +49 -0
  33. package/dist/core/mergeElementProps.js +67 -0
  34. package/dist/core/preference/cookiePreference.js +44 -0
  35. package/dist/core/preference/createPreference.js +60 -0
  36. package/dist/core/preference/localStoragePreference.js +28 -0
  37. package/dist/core/preference/preference.js +21 -0
  38. package/dist/core/preference/preferenceScript.js +12 -0
  39. package/dist/core/range.js +53 -0
  40. package/dist/core/renderElement.js +14 -0
  41. package/dist/core/rovingFocus.js +50 -0
  42. package/dist/core/toggleButton.js +37 -0
  43. package/dist/meter/index.js +2 -0
  44. package/dist/meter/index.parts.js +3 -0
  45. package/dist/meter/root/MeterRoot.js +28 -0
  46. package/dist/meter/root/MeterRootContext.js +10 -0
  47. package/dist/meter/value/MeterValue.js +24 -0
  48. package/dist/progress/index.js +2 -0
  49. package/dist/progress/index.parts.js +4 -0
  50. package/dist/progress/indicator/ProgressIndicator.js +21 -0
  51. package/dist/progress/root/ProgressRoot.js +47 -0
  52. package/dist/progress/root/ProgressRootContext.js +10 -0
  53. package/dist/progress/value/ProgressValue.js +26 -0
  54. package/dist/separator/index.js +2 -0
  55. package/dist/separator/index.parts.js +1 -0
  56. package/dist/separator/root/SeparatorRoot.js +31 -0
  57. package/dist/source/accordion/header/AccordionHeader.d.ts +11 -0
  58. package/dist/source/accordion/header/AccordionHeader.jsx +14 -0
  59. package/dist/source/accordion/index.d.ts +1 -0
  60. package/dist/source/accordion/index.js +1 -0
  61. package/dist/source/accordion/index.parts.d.ts +5 -0
  62. package/dist/source/accordion/index.parts.js +5 -0
  63. package/dist/source/accordion/item/AccordionItem.d.ts +17 -0
  64. package/dist/source/accordion/item/AccordionItem.jsx +42 -0
  65. package/dist/source/accordion/item/AccordionItemContext.d.ts +33 -0
  66. package/dist/source/accordion/item/AccordionItemContext.js +12 -0
  67. package/dist/source/accordion/panel/AccordionPanel.d.ts +11 -0
  68. package/dist/source/accordion/panel/AccordionPanel.jsx +27 -0
  69. package/dist/source/accordion/root/AccordionRoot.d.ts +26 -0
  70. package/dist/source/accordion/root/AccordionRoot.jsx +37 -0
  71. package/dist/source/accordion/root/AccordionRootContext.d.ts +11 -0
  72. package/dist/source/accordion/root/AccordionRootContext.js +13 -0
  73. package/dist/source/accordion/trigger/AccordionTrigger.d.ts +10 -0
  74. package/dist/source/accordion/trigger/AccordionTrigger.jsx +35 -0
  75. package/dist/source/checkbox/index.d.ts +1 -0
  76. package/dist/source/checkbox/index.js +1 -0
  77. package/dist/source/checkbox/index.parts.d.ts +2 -0
  78. package/dist/source/checkbox/index.parts.js +2 -0
  79. package/dist/source/checkbox/indicator/CheckboxIndicator.d.ts +10 -0
  80. package/dist/source/checkbox/indicator/CheckboxIndicator.jsx +12 -0
  81. package/dist/source/checkbox/root/CheckboxRoot.d.ts +37 -0
  82. package/dist/source/checkbox/root/CheckboxRoot.jsx +135 -0
  83. package/dist/source/checkbox/root/CheckboxRootContext.d.ts +16 -0
  84. package/dist/source/checkbox/root/CheckboxRootContext.js +13 -0
  85. package/dist/source/collapsible/index.d.ts +1 -0
  86. package/dist/source/collapsible/index.js +1 -0
  87. package/dist/source/collapsible/index.parts.d.ts +3 -0
  88. package/dist/source/collapsible/index.parts.js +3 -0
  89. package/dist/source/collapsible/panel/CollapsiblePanel.d.ts +10 -0
  90. package/dist/source/collapsible/panel/CollapsiblePanel.jsx +22 -0
  91. package/dist/source/collapsible/root/CollapsibleRoot.d.ts +21 -0
  92. package/dist/source/collapsible/root/CollapsibleRoot.jsx +42 -0
  93. package/dist/source/collapsible/root/CollapsibleRootContext.d.ts +26 -0
  94. package/dist/source/collapsible/root/CollapsibleRootContext.js +13 -0
  95. package/dist/source/collapsible/trigger/CollapsibleTrigger.d.ts +10 -0
  96. package/dist/source/collapsible/trigger/CollapsibleTrigger.jsx +23 -0
  97. package/dist/source/color-scheme/colorScheme.d.ts +35 -0
  98. package/dist/source/color-scheme/colorScheme.js +30 -0
  99. package/dist/source/color-scheme/colorSchemeScript.d.ts +8 -0
  100. package/dist/source/color-scheme/colorSchemeScript.js +16 -0
  101. package/dist/source/color-scheme/cookie/cookie.d.ts +39 -0
  102. package/dist/source/color-scheme/cookie/cookie.js +48 -0
  103. package/dist/source/color-scheme/cookie/index.d.ts +4 -0
  104. package/dist/source/color-scheme/cookie/index.js +2 -0
  105. package/dist/source/color-scheme/createColorScheme.d.ts +26 -0
  106. package/dist/source/color-scheme/createColorScheme.js +44 -0
  107. package/dist/source/color-scheme/local-storage/index.d.ts +4 -0
  108. package/dist/source/color-scheme/local-storage/index.js +2 -0
  109. package/dist/source/color-scheme/local-storage/localStorage.d.ts +30 -0
  110. package/dist/source/color-scheme/local-storage/localStorage.js +36 -0
  111. package/dist/source/core/button.d.ts +53 -0
  112. package/dist/source/core/button.js +61 -0
  113. package/dist/source/core/createControllableSignal.d.ts +27 -0
  114. package/dist/source/core/createControllableSignal.js +74 -0
  115. package/dist/source/core/index.d.ts +1 -0
  116. package/dist/source/core/index.js +1 -0
  117. package/dist/source/core/listValue.d.ts +32 -0
  118. package/dist/source/core/listValue.js +51 -0
  119. package/dist/source/core/mergeElementProps.d.ts +19 -0
  120. package/dist/source/core/mergeElementProps.js +91 -0
  121. package/dist/source/core/preference/cookiePreference.d.ts +10 -0
  122. package/dist/source/core/preference/cookiePreference.js +57 -0
  123. package/dist/source/core/preference/createPreference.d.ts +44 -0
  124. package/dist/source/core/preference/createPreference.js +60 -0
  125. package/dist/source/core/preference/localStoragePreference.d.ts +3 -0
  126. package/dist/source/core/preference/localStoragePreference.js +36 -0
  127. package/dist/source/core/preference/preference.d.ts +40 -0
  128. package/dist/source/core/preference/preference.js +22 -0
  129. package/dist/source/core/preference/preferenceScript.d.ts +17 -0
  130. package/dist/source/core/preference/preferenceScript.js +13 -0
  131. package/dist/source/core/range.d.ts +52 -0
  132. package/dist/source/core/range.js +60 -0
  133. package/dist/source/core/renderElement.d.ts +25 -0
  134. package/dist/source/core/renderElement.jsx +11 -0
  135. package/dist/source/core/rovingFocus.d.ts +42 -0
  136. package/dist/source/core/rovingFocus.js +71 -0
  137. package/dist/source/core/toggleButton.d.ts +22 -0
  138. package/dist/source/core/toggleButton.js +35 -0
  139. package/dist/source/meter/index.d.ts +1 -0
  140. package/dist/source/meter/index.js +1 -0
  141. package/dist/source/meter/index.parts.d.ts +2 -0
  142. package/dist/source/meter/index.parts.js +2 -0
  143. package/dist/source/meter/root/MeterRoot.d.ts +27 -0
  144. package/dist/source/meter/root/MeterRoot.jsx +15 -0
  145. package/dist/source/meter/root/MeterRootContext.d.ts +9 -0
  146. package/dist/source/meter/root/MeterRootContext.js +13 -0
  147. package/dist/source/meter/value/MeterValue.d.ts +11 -0
  148. package/dist/source/meter/value/MeterValue.jsx +19 -0
  149. package/dist/source/progress/index.d.ts +1 -0
  150. package/dist/source/progress/index.js +1 -0
  151. package/dist/source/progress/index.parts.d.ts +3 -0
  152. package/dist/source/progress/index.parts.js +3 -0
  153. package/dist/source/progress/indicator/ProgressIndicator.d.ts +11 -0
  154. package/dist/source/progress/indicator/ProgressIndicator.jsx +13 -0
  155. package/dist/source/progress/root/ProgressRoot.d.ts +30 -0
  156. package/dist/source/progress/root/ProgressRoot.jsx +32 -0
  157. package/dist/source/progress/root/ProgressRootContext.d.ts +20 -0
  158. package/dist/source/progress/root/ProgressRootContext.js +13 -0
  159. package/dist/source/progress/value/ProgressValue.d.ts +12 -0
  160. package/dist/source/progress/value/ProgressValue.jsx +20 -0
  161. package/dist/source/separator/index.d.ts +1 -0
  162. package/dist/source/separator/index.js +1 -0
  163. package/dist/source/separator/index.parts.d.ts +1 -0
  164. package/dist/source/separator/index.parts.js +1 -0
  165. package/dist/source/separator/root/SeparatorRoot.d.ts +12 -0
  166. package/dist/source/separator/root/SeparatorRoot.jsx +26 -0
  167. package/dist/source/switch/index.d.ts +1 -0
  168. package/dist/source/switch/index.js +1 -0
  169. package/dist/source/switch/index.parts.d.ts +2 -0
  170. package/dist/source/switch/index.parts.js +2 -0
  171. package/dist/source/switch/root/SwitchRoot.d.ts +31 -0
  172. package/dist/source/switch/root/SwitchRoot.jsx +124 -0
  173. package/dist/source/switch/root/SwitchRootContext.d.ts +14 -0
  174. package/dist/source/switch/root/SwitchRootContext.js +13 -0
  175. package/dist/source/switch/thumb/SwitchThumb.d.ts +10 -0
  176. package/dist/source/switch/thumb/SwitchThumb.jsx +12 -0
  177. package/dist/source/tabs/index.d.ts +1 -0
  178. package/dist/source/tabs/index.js +1 -0
  179. package/dist/source/tabs/index.parts.d.ts +4 -0
  180. package/dist/source/tabs/index.parts.js +4 -0
  181. package/dist/source/tabs/list/TabsList.d.ts +11 -0
  182. package/dist/source/tabs/list/TabsList.jsx +27 -0
  183. package/dist/source/tabs/panel/TabsPanel.d.ts +12 -0
  184. package/dist/source/tabs/panel/TabsPanel.jsx +38 -0
  185. package/dist/source/tabs/root/TabsRoot.d.ts +30 -0
  186. package/dist/source/tabs/root/TabsRoot.jsx +74 -0
  187. package/dist/source/tabs/root/TabsRootContext.d.ts +47 -0
  188. package/dist/source/tabs/root/TabsRootContext.js +13 -0
  189. package/dist/source/tabs/tab/TabsTab.d.ts +20 -0
  190. package/dist/source/tabs/tab/TabsTab.jsx +77 -0
  191. package/dist/source/theme/cookie/cookie.d.ts +34 -0
  192. package/dist/source/theme/cookie/cookie.js +42 -0
  193. package/dist/source/theme/cookie/index.d.ts +2 -0
  194. package/dist/source/theme/cookie/index.js +1 -0
  195. package/dist/source/theme/local-storage/index.d.ts +2 -0
  196. package/dist/source/theme/local-storage/index.js +1 -0
  197. package/dist/source/theme/local-storage/localStorage.d.ts +28 -0
  198. package/dist/source/theme/local-storage/localStorage.js +34 -0
  199. package/dist/source/theme/theme.d.ts +46 -0
  200. package/dist/source/theme/theme.js +57 -0
  201. package/dist/source/toggle/index.d.ts +1 -0
  202. package/dist/source/toggle/index.js +1 -0
  203. package/dist/source/toggle/index.parts.d.ts +1 -0
  204. package/dist/source/toggle/index.parts.js +1 -0
  205. package/dist/source/toggle/root/ToggleRoot.d.ts +20 -0
  206. package/dist/source/toggle/root/ToggleRoot.jsx +30 -0
  207. package/dist/source/toggle-group/index.d.ts +1 -0
  208. package/dist/source/toggle-group/index.js +1 -0
  209. package/dist/source/toggle-group/index.parts.d.ts +2 -0
  210. package/dist/source/toggle-group/index.parts.js +2 -0
  211. package/dist/source/toggle-group/item/ToggleGroupItem.d.ts +14 -0
  212. package/dist/source/toggle-group/item/ToggleGroupItem.jsx +39 -0
  213. package/dist/source/toggle-group/root/ToggleGroupRoot.d.ts +28 -0
  214. package/dist/source/toggle-group/root/ToggleGroupRoot.jsx +68 -0
  215. package/dist/source/toggle-group/root/ToggleGroupRootContext.d.ts +25 -0
  216. package/dist/source/toggle-group/root/ToggleGroupRootContext.js +13 -0
  217. package/dist/switch/index.js +2 -0
  218. package/dist/switch/index.parts.js +3 -0
  219. package/dist/switch/root/SwitchRoot.js +129 -0
  220. package/dist/switch/root/SwitchRootContext.js +10 -0
  221. package/dist/switch/thumb/SwitchThumb.js +20 -0
  222. package/dist/tabs/index.js +2 -0
  223. package/dist/tabs/index.parts.js +5 -0
  224. package/dist/tabs/list/TabsList.js +33 -0
  225. package/dist/tabs/panel/TabsPanel.js +40 -0
  226. package/dist/tabs/root/TabsRoot.js +74 -0
  227. package/dist/tabs/root/TabsRootContext.js +10 -0
  228. package/dist/tabs/tab/TabsTab.js +74 -0
  229. package/dist/theme/cookie/cookie.js +44 -0
  230. package/dist/theme/cookie/index.js +1 -0
  231. package/dist/theme/local-storage/index.js +1 -0
  232. package/dist/theme/local-storage/localStorage.js +36 -0
  233. package/dist/theme/theme.js +52 -0
  234. package/dist/toggle/index.js +2 -0
  235. package/dist/toggle/index.parts.js +1 -0
  236. package/dist/toggle/root/ToggleRoot.js +38 -0
  237. package/dist/toggle-group/index.js +2 -0
  238. package/dist/toggle-group/index.parts.js +3 -0
  239. package/dist/toggle-group/item/ToggleGroupItem.js +48 -0
  240. package/dist/toggle-group/root/ToggleGroupRoot.js +71 -0
  241. package/dist/toggle-group/root/ToggleGroupRootContext.js +10 -0
  242. package/package.json +99 -6
@@ -0,0 +1,48 @@
1
+ import { cookiePreference, cookieValue, } from "../../core/preference/cookiePreference.js";
2
+ import { defaultStorageKey, parseColorScheme } from "../colorScheme.js";
3
+ import { colorSchemeScriptWithStorage } from "../colorSchemeScript.js";
4
+ import { createColorSchemeWithStorage } from "../createColorScheme.js";
5
+ const storage = cookiePreference;
6
+ /**
7
+ * The reader's light, dark or system choice, saved in a cookie and applied
8
+ * to `<html>` (`ColorSchemeOptions.attribute`). Follows the system preference
9
+ * while the choice is `system`, and stays in step with the other instances
10
+ * in this tab, and with other tabs once they are shown again.
11
+ *
12
+ * Call it in a component. The cookie is read during the first render, on
13
+ * the server from the request (`getRequestEvent()`), so the server's HTML
14
+ * shows the choice and hydration agrees. Only `system` waits until the page
15
+ * has settled, since the server cannot see the system's preference. The
16
+ * page has to be rendered for each request: a prerendered page has no
17
+ * cookie to read. The head script from `colorSchemeScript`, with the same
18
+ * options, applies a `system` choice before the first paint.
19
+ */
20
+ export function createColorScheme(options) {
21
+ return createColorSchemeWithStorage(storage, options);
22
+ }
23
+ /**
24
+ * The source of a script for the document head that applies the choice
25
+ * saved in the cookie, or the system preference, before the page first
26
+ * paints. Pass the options given to `createColorScheme`. Render it inline,
27
+ * before any styles that depend on the scheme, even when the server renders
28
+ * the choice: it is what applies `system`.
29
+ *
30
+ * ```tsx
31
+ * <script innerHTML={colorSchemeScript()} />
32
+ * ```
33
+ *
34
+ * Under a Content Security Policy without `'unsafe-inline'`, put the
35
+ * policy's nonce on the element (`<script nonce={nonce} …>`), or allow the
36
+ * SHA-256 hash of the returned source.
37
+ */
38
+ export function colorSchemeScript(options) {
39
+ return colorSchemeScriptWithStorage(storage, options);
40
+ }
41
+ /**
42
+ * The scheme saved in a `Cookie` request header, for a server that puts the
43
+ * attribute on its own `<html>`. Pass the options given to
44
+ * `createColorScheme`.
45
+ */
46
+ export function colorSchemeFromCookie(cookieHeader, options = {}) {
47
+ return parseColorScheme(cookieValue(cookieHeader, options.storageKey ?? defaultStorageKey));
48
+ }
@@ -0,0 +1,4 @@
1
+ export { nextColorScheme, resolveColorScheme } from "../colorScheme.ts";
2
+ export type { ColorScheme, ColorSchemeOptions, ResolvedColorScheme, } from "../colorScheme.ts";
3
+ export type { ColorSchemeState } from "../createColorScheme.ts";
4
+ export { colorSchemeFromCookie, colorSchemeScript, createColorScheme, } from "./cookie.ts";
@@ -0,0 +1,2 @@
1
+ export { nextColorScheme, resolveColorScheme } from "../colorScheme.js";
2
+ export { colorSchemeFromCookie, colorSchemeScript, createColorScheme, } from "./cookie.js";
@@ -0,0 +1,26 @@
1
+ import type { Accessor } from "solid-js";
2
+ import type { PreferenceStorage } from "../core/preference/preference.ts";
3
+ import type { ColorScheme, ColorSchemeOptions, ResolvedColorScheme } from "./colorScheme.ts";
4
+ export interface ColorSchemeState {
5
+ /**
6
+ * The reader's choice. `system` until the page has settled, unless it is
7
+ * saved in a cookie (`menhir/color-scheme/cookie`).
8
+ */
9
+ scheme: Accessor<ColorScheme>;
10
+ /** The look the system prefers. `light` until the page has settled. */
11
+ system: Accessor<ResolvedColorScheme>;
12
+ /** The look on screen: `scheme`, with `system` replaced by the preference. */
13
+ resolved: Accessor<ResolvedColorScheme>;
14
+ /** Saves a new choice and applies it. Call from an event handler. */
15
+ setScheme: (scheme: ColorScheme) => void;
16
+ }
17
+ /**
18
+ * `createColorScheme` of each entry point, with the entry point's storage.
19
+ *
20
+ * The reader's light, dark or system choice, saved in `storage` and applied
21
+ * to `<html>` (`ColorSchemeOptions.attribute`), through `createPreference`.
22
+ * Follows the system preference while the choice is `system`. `system`
23
+ * waits until the page has settled, since the server cannot see it; until
24
+ * then it is `light`.
25
+ */
26
+ export declare function createColorSchemeWithStorage(storage: PreferenceStorage, options?: ColorSchemeOptions): ColorSchemeState;
@@ -0,0 +1,44 @@
1
+ import { createSignal, onSettled } from "solid-js";
2
+ import { createPreference } from "../core/preference/createPreference.js";
3
+ import { resolvedSchemes, darkQuery, defaultAttribute, defaultStorageKey, resolveColorScheme, } from "./colorScheme.js";
4
+ /**
5
+ * `createColorScheme` of each entry point, with the entry point's storage.
6
+ *
7
+ * The reader's light, dark or system choice, saved in `storage` and applied
8
+ * to `<html>` (`ColorSchemeOptions.attribute`), through `createPreference`.
9
+ * Follows the system preference while the choice is `system`. `system`
10
+ * waits until the page has settled, since the server cannot see it; until
11
+ * then it is `light`.
12
+ */
13
+ export function createColorSchemeWithStorage(storage, options = {}) {
14
+ const [system, setSystem] = createSignal("light");
15
+ // Reads lag writes until the next flush, so the look is resolved from this
16
+ // copy rather than from the signal.
17
+ let currentSystem = "light";
18
+ const preference = createPreference(storage, {
19
+ storageKey: options.storageKey ?? defaultStorageKey,
20
+ attribute: options.attribute ?? defaultAttribute,
21
+ values: resolvedSchemes,
22
+ unset: "system",
23
+ resolve: (scheme) => resolveColorScheme(scheme, currentSystem),
24
+ });
25
+ // Applies the look again whenever the system is read, so it is right
26
+ // whether this runs before or after the preference reads the saved
27
+ // choice; both happen as the page settles, before it paints.
28
+ onSettled(() => {
29
+ const media = window.matchMedia(darkQuery);
30
+ const onMediaChange = () => {
31
+ setSystem((currentSystem = media.matches ? "dark" : "light"));
32
+ preference.apply();
33
+ };
34
+ onMediaChange();
35
+ media.addEventListener("change", onMediaChange);
36
+ return () => media.removeEventListener("change", onMediaChange);
37
+ });
38
+ return {
39
+ scheme: preference.value,
40
+ system,
41
+ resolved: () => resolveColorScheme(preference.value(), system()),
42
+ setScheme: preference.set,
43
+ };
44
+ }
@@ -0,0 +1,4 @@
1
+ export { nextColorScheme, resolveColorScheme } from "../colorScheme.ts";
2
+ export type { ColorScheme, ColorSchemeOptions, ResolvedColorScheme, } from "../colorScheme.ts";
3
+ export type { ColorSchemeState } from "../createColorScheme.ts";
4
+ export { colorSchemeScript, createColorScheme } from "./localStorage.ts";
@@ -0,0 +1,2 @@
1
+ export { nextColorScheme, resolveColorScheme } from "../colorScheme.js";
2
+ export { colorSchemeScript, createColorScheme } from "./localStorage.js";
@@ -0,0 +1,30 @@
1
+ import type { ColorSchemeOptions } from "../colorScheme.ts";
2
+ import type { ColorSchemeState } from "../createColorScheme.ts";
3
+ /**
4
+ * The reader's light, dark or system choice, saved in `localStorage` and
5
+ * applied to `<html>` (`ColorSchemeOptions.attribute`). Follows the system
6
+ * preference while the choice is `system`, and stays in step with the other
7
+ * instances in this tab and with other tabs.
8
+ *
9
+ * Call it in a component. The saved choice is read once the page has
10
+ * settled, so the server's HTML and the first client render agree; until
11
+ * then the state is `system` and `light`. The head script from
12
+ * `colorSchemeScript`, with the same options, applies the choice before the
13
+ * first paint, so the page does not flash in between.
14
+ */
15
+ export declare function createColorScheme(options?: ColorSchemeOptions): ColorSchemeState;
16
+ /**
17
+ * The source of a script for the document head that applies the choice
18
+ * saved in `localStorage`, or the system preference, before the page first
19
+ * paints. Pass the options given to `createColorScheme`. Render it inline,
20
+ * before any styles that depend on the scheme:
21
+ *
22
+ * ```tsx
23
+ * <script innerHTML={colorSchemeScript()} />
24
+ * ```
25
+ *
26
+ * Under a Content Security Policy without `'unsafe-inline'`, put the
27
+ * policy's nonce on the element (`<script nonce={nonce} …>`), or allow the
28
+ * SHA-256 hash of the returned source.
29
+ */
30
+ export declare function colorSchemeScript(options?: ColorSchemeOptions): string;
@@ -0,0 +1,36 @@
1
+ import { localStoragePreference } from "../../core/preference/localStoragePreference.js";
2
+ import { colorSchemeScriptWithStorage } from "../colorSchemeScript.js";
3
+ import { createColorSchemeWithStorage } from "../createColorScheme.js";
4
+ const storage = localStoragePreference;
5
+ /**
6
+ * The reader's light, dark or system choice, saved in `localStorage` and
7
+ * applied to `<html>` (`ColorSchemeOptions.attribute`). Follows the system
8
+ * preference while the choice is `system`, and stays in step with the other
9
+ * instances in this tab and with other tabs.
10
+ *
11
+ * Call it in a component. The saved choice is read once the page has
12
+ * settled, so the server's HTML and the first client render agree; until
13
+ * then the state is `system` and `light`. The head script from
14
+ * `colorSchemeScript`, with the same options, applies the choice before the
15
+ * first paint, so the page does not flash in between.
16
+ */
17
+ export function createColorScheme(options) {
18
+ return createColorSchemeWithStorage(storage, options);
19
+ }
20
+ /**
21
+ * The source of a script for the document head that applies the choice
22
+ * saved in `localStorage`, or the system preference, before the page first
23
+ * paints. Pass the options given to `createColorScheme`. Render it inline,
24
+ * before any styles that depend on the scheme:
25
+ *
26
+ * ```tsx
27
+ * <script innerHTML={colorSchemeScript()} />
28
+ * ```
29
+ *
30
+ * Under a Content Security Policy without `'unsafe-inline'`, put the
31
+ * policy's nonce on the element (`<script nonce={nonce} …>`), or allow the
32
+ * SHA-256 hash of the returned source.
33
+ */
34
+ export function colorSchemeScript(options) {
35
+ return colorSchemeScriptWithStorage(storage, options);
36
+ }
@@ -0,0 +1,53 @@
1
+ import type { ElementProps } from "./renderElement.tsx";
2
+ /**
3
+ * The own props of a part that is a native button (PLAN.md section 8, "Form
4
+ * controls are native buttons"). `type` and `disabled` are button
5
+ * attributes, which `ElementProps`, typed for any element, does not list.
6
+ */
7
+ export type ButtonElementProps = ElementProps & {
8
+ type: "button";
9
+ disabled?: boolean;
10
+ };
11
+ /**
12
+ * Warns when the consumer's `render` function rendered a part that must be
13
+ * a native button as another element: its keyboard support, focus and name
14
+ * come from the element being a button. Call it from the part's ref behind
15
+ * `isDev`, so that production builds drop the call and this module:
16
+ *
17
+ * ```ts
18
+ * ref: (element) => {
19
+ * if (isDev) warnUnlessButton(element, "Switch.Root");
20
+ * },
21
+ * ```
22
+ */
23
+ export declare function warnUnlessButton(element: HTMLElement, part: string): void;
24
+ /** The state of a two-state button, as getters read where they are used. */
25
+ export interface TwoStateButtonState {
26
+ readonly value: boolean;
27
+ readonly disabled: boolean | undefined;
28
+ /** Called by a click, with the opposite of `value`. */
29
+ setValue(value: boolean): void;
30
+ }
31
+ /**
32
+ * The own props of a part that is a native `<button type="button">` whose
33
+ * click flips a state it announces in `attribute`: `aria-pressed` on a
34
+ * toggle button (`toggleButton.ts`), `aria-expanded` on a disclosure's
35
+ * trigger. Pass the state as getters, so it is read where the props are
36
+ * used:
37
+ *
38
+ * ```ts
39
+ * const own = twoStateButtonProps(
40
+ * {
41
+ * get value() { return open(); },
42
+ * get disabled() { return props.disabled; },
43
+ * setValue: setOpen,
44
+ * },
45
+ * "aria-expanded",
46
+ * "Collapsible.Trigger",
47
+ * );
48
+ * ```
49
+ *
50
+ * `part` names the part in the development warning of a `render` that is
51
+ * not a button. The part adds its own `data-*` attributes.
52
+ */
53
+ export declare function twoStateButtonProps(state: TwoStateButtonState, attribute: "aria-pressed" | "aria-expanded", part: string): ButtonElementProps;
@@ -0,0 +1,61 @@
1
+ import { isDev } from "@solidjs/web";
2
+ /**
3
+ * Warns when the consumer's `render` function rendered a part that must be
4
+ * a native button as another element: its keyboard support, focus and name
5
+ * come from the element being a button. Call it from the part's ref behind
6
+ * `isDev`, so that production builds drop the call and this module:
7
+ *
8
+ * ```ts
9
+ * ref: (element) => {
10
+ * if (isDev) warnUnlessButton(element, "Switch.Root");
11
+ * },
12
+ * ```
13
+ */
14
+ export function warnUnlessButton(element, part) {
15
+ if (element.tagName === "BUTTON")
16
+ return;
17
+ console.warn(`[menhir] ${part}'s render function rendered a <${element.tagName.toLowerCase()}>. ` +
18
+ "Render a <button>, which gives the part its keyboard support, focus and name.");
19
+ }
20
+ /**
21
+ * The own props of a part that is a native `<button type="button">` whose
22
+ * click flips a state it announces in `attribute`: `aria-pressed` on a
23
+ * toggle button (`toggleButton.ts`), `aria-expanded` on a disclosure's
24
+ * trigger. Pass the state as getters, so it is read where the props are
25
+ * used:
26
+ *
27
+ * ```ts
28
+ * const own = twoStateButtonProps(
29
+ * {
30
+ * get value() { return open(); },
31
+ * get disabled() { return props.disabled; },
32
+ * setValue: setOpen,
33
+ * },
34
+ * "aria-expanded",
35
+ * "Collapsible.Trigger",
36
+ * );
37
+ * ```
38
+ *
39
+ * `part` names the part in the development warning of a `render` that is
40
+ * not a button. The part adds its own `data-*` attributes.
41
+ */
42
+ export function twoStateButtonProps(state, attribute, part) {
43
+ // A computed key widens the literal's type to an index signature, which
44
+ // TypeScript then checks against every attribute.
45
+ return {
46
+ type: "button",
47
+ get disabled() {
48
+ return state.disabled;
49
+ },
50
+ // Solid 2.0 removes an attribute set to false, so spell the value out.
51
+ get [attribute]() {
52
+ return state.value ? "true" : "false";
53
+ },
54
+ onClick: () => state.setValue(!state.value),
55
+ ref: (element) => {
56
+ // Production builds drop the check with `isDev`.
57
+ if (isDev)
58
+ warnUnlessButton(element, part);
59
+ },
60
+ };
61
+ }
@@ -0,0 +1,27 @@
1
+ import type { Accessor } from "solid-js";
2
+ /**
3
+ * A part's value that the consumer may control. Pass the props as getters,
4
+ * so they are read where they are used:
5
+ *
6
+ * ```ts
7
+ * const [checked, setChecked] = createControllableSignal({
8
+ * get value() { return props.checked; },
9
+ * get defaultValue() { return props.defaultChecked ?? false; },
10
+ * onChange: (value) => props.onCheckedChange?.(value),
11
+ * });
12
+ * ```
13
+ *
14
+ * Controlled while `value` is not `undefined`: the value follows it, and a
15
+ * set only calls `onChange`, for the consumer to decide. Otherwise the value
16
+ * starts at `defaultValue`, read once, and a set calls `onChange` and
17
+ * changes the value right after the pending flush, in an update of its own
18
+ * (`onFlush`), so async work that `onChange` starts does not hold it back.
19
+ * Setting the value it already has, counting earlier sets in the same turn,
20
+ * does nothing. Set from event handlers, not from a component body or a
21
+ * memo.
22
+ */
23
+ export declare function createControllableSignal<T>(options: {
24
+ readonly value?: T;
25
+ readonly defaultValue: T;
26
+ onChange?: (value: T) => void;
27
+ }): [value: Accessor<T>, setValue: (next: T) => void];
@@ -0,0 +1,74 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { createSignal, onFlush, untrack } from "solid-js";
3
+ /**
4
+ * A part's value that the consumer may control. Pass the props as getters,
5
+ * so they are read where they are used:
6
+ *
7
+ * ```ts
8
+ * const [checked, setChecked] = createControllableSignal({
9
+ * get value() { return props.checked; },
10
+ * get defaultValue() { return props.defaultChecked ?? false; },
11
+ * onChange: (value) => props.onCheckedChange?.(value),
12
+ * });
13
+ * ```
14
+ *
15
+ * Controlled while `value` is not `undefined`: the value follows it, and a
16
+ * set only calls `onChange`, for the consumer to decide. Otherwise the value
17
+ * starts at `defaultValue`, read once, and a set calls `onChange` and
18
+ * changes the value right after the pending flush, in an update of its own
19
+ * (`onFlush`), so async work that `onChange` starts does not hold it back.
20
+ * Setting the value it already has, counting earlier sets in the same turn,
21
+ * does nothing. Set from event handlers, not from a component body or a
22
+ * memo.
23
+ */
24
+ export function createControllableSignal(options) {
25
+ // A writable derived signal that reads the default once, untracked, so a
26
+ // later change to it does not reset the value. Seeding through a function
27
+ // also keeps a function-valued default from being taken as a derivation.
28
+ const [internal, setInternal] = createSignal(() => untrack(() => options.defaultValue));
29
+ // Controlled while the consumer passes a value. `undefined` means
30
+ // uncontrolled, as with an absent prop.
31
+ //
32
+ // A switch is usually a bug, such as state that starts `undefined`, so
33
+ // development builds warn, on the first read in the new mode. The check
34
+ // is not an effect: an effect takes a place in the owner tree, which
35
+ // hydration keys come from, so a server and a client that disagree on
36
+ // `isDev` would no longer match. Production builds drop this branch:
37
+ // Solid's production `isDev` is a constant `false`.
38
+ let wasControlled;
39
+ const isControlled = () => {
40
+ const controlled = options.value !== undefined;
41
+ if (isDev) {
42
+ if (wasControlled !== undefined && controlled !== wasControlled) {
43
+ console.warn(`[menhir] A part switched from ${wasControlled ? "controlled" : "uncontrolled"} to ${controlled ? "controlled" : "uncontrolled"}. ` +
44
+ "Keep it one or the other for its lifetime; for an empty controlled value, pass null, not undefined.");
45
+ }
46
+ wasControlled = controlled;
47
+ }
48
+ return controlled;
49
+ };
50
+ const value = () => (isControlled() ? options.value : internal());
51
+ // Reads lag writes, so a set is compared with the last value set in this
52
+ // turn, if any, not with what `value` still reads.
53
+ let pending;
54
+ const setValue = (next) => {
55
+ if (Object.is(next, pending ? pending[0] : untrack(value)))
56
+ return;
57
+ // The part's own write is an update of its own, after the pending
58
+ // flush: written inline, it would be held with any async work that
59
+ // `onChange` starts, such as a fetch, and the part would not change
60
+ // until that work settled.
61
+ if (!untrack(isControlled))
62
+ onFlush(() => setInternal(() => next));
63
+ options.onChange?.(next);
64
+ const set = [next];
65
+ pending = set;
66
+ // `onFlush` callbacks run in call order, each flushed before the next,
67
+ // so `value` is current by the time this one runs.
68
+ onFlush(() => {
69
+ if (pending === set)
70
+ pending = undefined;
71
+ });
72
+ };
73
+ return [value, setValue];
74
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ /** A value that lists items, in one of its two modes. */
2
+ export interface ListValue {
3
+ /** Whether the item with this value is in the list. */
4
+ has(item: string): boolean;
5
+ /** Asks to add the item to the list, or to take it out. */
6
+ set(item: string, included: boolean): void;
7
+ }
8
+ /**
9
+ * A part's value that lists items and that the consumer may control, as an
10
+ * array in every mode, `[]` when empty (PLAN.md section 8, "Controlled
11
+ * state"): Toggle Group's pressed items, Accordion's open ones. Pass the
12
+ * props as getters, so they are read where they are used:
13
+ *
14
+ * ```ts
15
+ * const list = createListValue({
16
+ * get value() { return props.value; },
17
+ * get defaultValue() { return props.defaultValue; },
18
+ * get multiple() { return props.multiple; },
19
+ * onChange: (value) => props.onValueChange?.(value),
20
+ * });
21
+ * ```
22
+ *
23
+ * By default the list holds one item at most: adding an item replaces the
24
+ * one before. With `multiple`, items are added and taken out on their own.
25
+ * Sets in one turn build on each other, before the flush.
26
+ */
27
+ export declare function createListValue(options: {
28
+ readonly value?: string[];
29
+ readonly defaultValue?: string[];
30
+ readonly multiple?: boolean;
31
+ onChange?: (value: string[]) => void;
32
+ }): ListValue;
@@ -0,0 +1,51 @@
1
+ import { onFlush } from "solid-js";
2
+ import { createControllableSignal } from "./createControllableSignal.js";
3
+ /**
4
+ * A part's value that lists items and that the consumer may control, as an
5
+ * array in every mode, `[]` when empty (PLAN.md section 8, "Controlled
6
+ * state"): Toggle Group's pressed items, Accordion's open ones. Pass the
7
+ * props as getters, so they are read where they are used:
8
+ *
9
+ * ```ts
10
+ * const list = createListValue({
11
+ * get value() { return props.value; },
12
+ * get defaultValue() { return props.defaultValue; },
13
+ * get multiple() { return props.multiple; },
14
+ * onChange: (value) => props.onValueChange?.(value),
15
+ * });
16
+ * ```
17
+ *
18
+ * By default the list holds one item at most: adding an item replaces the
19
+ * one before. With `multiple`, items are added and taken out on their own.
20
+ * Sets in one turn build on each other, before the flush.
21
+ */
22
+ export function createListValue(options) {
23
+ const [value, setValue] = createControllableSignal({
24
+ get value() {
25
+ return options.value;
26
+ },
27
+ get defaultValue() {
28
+ return options.defaultValue ?? [];
29
+ },
30
+ onChange: (value) => options.onChange?.(value),
31
+ });
32
+ // The value asked for in this turn, if any: reads lag writes, so a second
33
+ // set before the flush builds on this, not on what `value` still reads.
34
+ let pending;
35
+ return {
36
+ has: (item) => value().includes(item),
37
+ set: (item, included) => {
38
+ const current = pending ?? value();
39
+ let next = included ? [item] : [];
40
+ if (options.multiple) {
41
+ next = current.filter((other) => other !== item);
42
+ if (included)
43
+ next.push(item);
44
+ }
45
+ if (!pending)
46
+ onFlush(() => (pending = undefined));
47
+ pending = next;
48
+ setValue(next);
49
+ },
50
+ };
51
+ }
@@ -0,0 +1,19 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { ElementProps } from "./renderElement.tsx";
3
+ /**
4
+ * Merges a part's own props with the consumer's, for one element. The
5
+ * consumer's props win, except for these, where both apply:
6
+ *
7
+ * - Event handlers both run, the consumer's first.
8
+ * - `class` keeps both, in any of Solid's class forms.
9
+ * - `style` keeps both; the consumer's wins a conflict.
10
+ * - `ref` runs both, the consumer's first. The result's `ref` is one
11
+ * callback, so it spreads onto any element (see `ElementProps`).
12
+ *
13
+ * Only keys the part sets are merged this way; any other prop of the
14
+ * consumer passes through unchanged. The part's handlers and ref are read
15
+ * once, here; its other props, and all of the consumer's, are read where
16
+ * they are used, so they stay reactive. Call it in the part's body, not
17
+ * inline in a spread, which would run it again on every change.
18
+ */
19
+ export declare function mergeElementProps(own: ElementProps, props: JSX.HTMLAttributes<HTMLElement>): ElementProps;
@@ -0,0 +1,91 @@
1
+ import { merge } from "solid-js";
2
+ function callHandler(handler, event) {
3
+ if (typeof handler === "function")
4
+ handler(event);
5
+ else if (handler)
6
+ handler[0](handler[1], event);
7
+ }
8
+ function toCss(style) {
9
+ if (typeof style === "string")
10
+ return style;
11
+ if (!style)
12
+ return "";
13
+ return Object.entries(style)
14
+ .filter(([, value]) => value != null)
15
+ .map(([name, value]) => `${name}:${value}`)
16
+ .join(";");
17
+ }
18
+ // Two objects merge by property. If either is a string, both become
19
+ // declarations in one string, the consumer's last, so they win a conflict.
20
+ function mergeStyles(own, consumer) {
21
+ if (consumer == null)
22
+ return own;
23
+ if (typeof own === "object" && typeof consumer === "object") {
24
+ return { ...own, ...consumer };
25
+ }
26
+ const ownCss = toCss(own);
27
+ const consumerCss = toCss(consumer);
28
+ return ownCss && consumerCss
29
+ ? `${ownCss};${consumerCss}`
30
+ : ownCss || consumerCss;
31
+ }
32
+ // A consumer's ref is a callback, or an array of refs. A `let` variable
33
+ // reaches the part as a callback, written by the compiler.
34
+ function applyRef(ref, element) {
35
+ if (typeof ref === "function")
36
+ ref(element);
37
+ else if (Array.isArray(ref))
38
+ for (const entry of ref)
39
+ applyRef(entry, element);
40
+ }
41
+ /**
42
+ * Merges a part's own props with the consumer's, for one element. The
43
+ * consumer's props win, except for these, where both apply:
44
+ *
45
+ * - Event handlers both run, the consumer's first.
46
+ * - `class` keeps both, in any of Solid's class forms.
47
+ * - `style` keeps both; the consumer's wins a conflict.
48
+ * - `ref` runs both, the consumer's first. The result's `ref` is one
49
+ * callback, so it spreads onto any element (see `ElementProps`).
50
+ *
51
+ * Only keys the part sets are merged this way; any other prop of the
52
+ * consumer passes through unchanged. The part's handlers and ref are read
53
+ * once, here; its other props, and all of the consumer's, are read where
54
+ * they are used, so they stay reactive. Call it in the part's body, not
55
+ * inline in a spread, which would run it again on every change.
56
+ */
57
+ export function mergeElementProps(own, props) {
58
+ const overrides = {};
59
+ if ("class" in own) {
60
+ // Solid's class array form merges its entries, whatever form each is.
61
+ Object.defineProperty(overrides, "class", {
62
+ enumerable: true,
63
+ get: () => [own.class, props.class],
64
+ });
65
+ }
66
+ if ("style" in own) {
67
+ Object.defineProperty(overrides, "style", {
68
+ enumerable: true,
69
+ get: () => mergeStyles(own.style, props.style),
70
+ });
71
+ }
72
+ // Always one callback, even when only the consumer has a ref, which may
73
+ // be an array: `ElementProps` promises a callback.
74
+ const ownRef = own.ref;
75
+ overrides.ref = (element) => {
76
+ applyRef(props.ref, element);
77
+ ownRef?.(element);
78
+ };
79
+ for (const key of Object.keys(own)) {
80
+ if (!key.startsWith("on"))
81
+ continue;
82
+ const ownHandler = own[key];
83
+ // The consumer's handler is read when the event fires, so a handler
84
+ // passed as an expression is current without tracking anything.
85
+ overrides[key] = (event) => {
86
+ callHandler(props[key], event);
87
+ callHandler(ownHandler, event);
88
+ };
89
+ }
90
+ return merge(own, props, overrides);
91
+ }
@@ -0,0 +1,10 @@
1
+ import type { PreferenceStorage } from "./preference.ts";
2
+ /** The value of the cookie `key` in a `Cookie` header. */
3
+ export declare function cookieValue(cookieHeader: string | null | undefined, key: string): string | undefined;
4
+ /**
5
+ * A cookie named by the key, which the browser sends with every request:
6
+ * read on the server through `getRequestEvent()`, and in the browser from
7
+ * `document.cookie`. Cookies have no `storage` event, so another tab's
8
+ * choice is read when this tab is shown again.
9
+ */
10
+ export declare const cookiePreference: PreferenceStorage;