menhir 0.0.0 → 0.1.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.
Files changed (242) hide show
  1. package/LICENSE +0 -28
  2. package/README.md +44 -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,57 @@
1
+ import { getRequestEvent, isServer, parseCookieHeader, serializeCookie, } from "@solidjs/web";
2
+ import { scriptLiteral } from "./preference.js";
3
+ /** A year, in seconds: how long a saved choice lasts without a visit. */
4
+ const maxAge = 60 * 60 * 24 * 365;
5
+ /** The value of the cookie `key` in a `Cookie` header. */
6
+ export function cookieValue(cookieHeader, key) {
7
+ return parseCookieHeader(cookieHeader)[key];
8
+ }
9
+ /**
10
+ * A cookie named by the key, which the browser sends with every request:
11
+ * read on the server through `getRequestEvent()`, and in the browser from
12
+ * `document.cookie`. Cookies have no `storage` event, so another tab's
13
+ * choice is read when this tab is shown again.
14
+ */
15
+ export const cookiePreference = {
16
+ read: (key) => {
17
+ try {
18
+ return cookieValue(isServer
19
+ ? getRequestEvent()?.request.headers.get("cookie")
20
+ : document.cookie, key);
21
+ }
22
+ catch {
23
+ // Cookies can be unavailable, as in a sandboxed frame; the choice
24
+ // then lasts until the page is left.
25
+ return undefined;
26
+ }
27
+ },
28
+ write: (key, value) => {
29
+ try {
30
+ document.cookie = serializeCookie(key, value ?? "", {
31
+ path: "/",
32
+ maxAge: value === null ? 0 : maxAge,
33
+ sameSite: "lax",
34
+ // A browser refuses a cookie with either prefix unless it is Secure.
35
+ secure: /^__(Host|Secure)-/.test(key),
36
+ });
37
+ }
38
+ catch {
39
+ // See read.
40
+ }
41
+ },
42
+ watch: (key, onChange) => {
43
+ const onVisibilityChange = () => {
44
+ if (document.visibilityState !== "hidden") {
45
+ onChange(cookiePreference.read(key));
46
+ }
47
+ };
48
+ document.addEventListener("visibilitychange", onVisibilityChange);
49
+ return () => document.removeEventListener("visibilitychange", onVisibilityChange);
50
+ },
51
+ readsOnServer: true,
52
+ // The name is saved encoded (`serializeCookie`), so the script looks for
53
+ // the encoded form. The last cookie of the name wins, as in
54
+ // `parseCookieHeader`.
55
+ script: (key) => `var c=document.cookie.split(";"),n=${scriptLiteral(`${encodeURIComponent(key)}=`)};` +
56
+ "for(var i=0;i<c.length;i++){var p=c[i].trim();if(p.indexOf(n)===0)m=p.slice(n.length)}",
57
+ };
@@ -0,0 +1,44 @@
1
+ import type { Accessor } from "solid-js";
2
+ import type { PreferenceAttribute, PreferenceStorage } from "./preference.ts";
3
+ export interface PreferenceOptions<T extends string> {
4
+ storageKey: string;
5
+ attribute: PreferenceAttribute;
6
+ /**
7
+ * Every value that can be saved, and that the attribute can hold. A saved
8
+ * value outside it reads as `unset`.
9
+ */
10
+ values: readonly string[];
11
+ /**
12
+ * The value while nothing is saved, such as color scheme's `system` or a
13
+ * default theme. Choosing it removes the saved value.
14
+ */
15
+ unset: T;
16
+ /**
17
+ * The value that goes on `<html>` for a value, such as `system`'s look.
18
+ * Default: the value itself.
19
+ */
20
+ resolve?: (value: T) => string;
21
+ }
22
+ export interface Preference<T extends string> {
23
+ value: Accessor<T>;
24
+ /** Saves a value and applies it, here and in the other instances. */
25
+ set: (value: T) => void;
26
+ /** Applies the value again, for a `resolve` whose result has changed. */
27
+ apply: () => void;
28
+ }
29
+ /**
30
+ * A preference of the reader's, saved in `storage` and applied to `<html>`,
31
+ * that stays in step with the other instances in this tab and with other
32
+ * tabs: what color scheme and theme share.
33
+ *
34
+ * A storage only the browser can read, such as `localStorage`, is read once
35
+ * the page has settled, so the server's HTML and the first client render
36
+ * agree; until then the value is `unset`. One the server can read too, such
37
+ * as a cookie, is read during the first render. The head script applies the
38
+ * value before the first paint, so the page does not flash in between.
39
+ *
40
+ * The DOM is written imperatively, from `set` and the listeners, not from
41
+ * an effect: an effect's first run would apply `unset` before the saved
42
+ * value is read, undoing the head script.
43
+ */
44
+ export declare function createPreference<T extends string>(storage: PreferenceStorage, options: PreferenceOptions<T>): Preference<T>;
@@ -0,0 +1,60 @@
1
+ import { createSignal, onSettled } from "solid-js";
2
+ import { applyPreference, parsePreference } from "./preference.js";
3
+ /** Tells the other instances in the same tab about a choice. */
4
+ const changeEvent = "menhir:preference";
5
+ /**
6
+ * A preference of the reader's, saved in `storage` and applied to `<html>`,
7
+ * that stays in step with the other instances in this tab and with other
8
+ * tabs: what color scheme and theme share.
9
+ *
10
+ * A storage only the browser can read, such as `localStorage`, is read once
11
+ * the page has settled, so the server's HTML and the first client render
12
+ * agree; until then the value is `unset`. One the server can read too, such
13
+ * as a cookie, is read during the first render. The head script applies the
14
+ * value before the first paint, so the page does not flash in between.
15
+ *
16
+ * The DOM is written imperatively, from `set` and the listeners, not from
17
+ * an effect: an effect's first run would apply `unset` before the saved
18
+ * value is read, undoing the head script.
19
+ */
20
+ export function createPreference(storage, options) {
21
+ const { storageKey, attribute, values, unset } = options;
22
+ const resolve = options.resolve ?? ((value) => value);
23
+ const parse = (raw) => parsePreference(raw, values, unset);
24
+ const initial = storage.readsOnServer
25
+ ? parse(storage.read(storageKey))
26
+ : unset;
27
+ // TypeScript cannot rule out that a `T` is a function, which
28
+ // `createSignal` would take as a computation.
29
+ const [value, setValue] = createSignal(initial);
30
+ // Reads lag writes until the next flush, so the DOM is updated from this
31
+ // copy rather than from the signal.
32
+ let current = initial;
33
+ const apply = () => applyPreference(attribute, resolve(current), values);
34
+ const update = (next) => {
35
+ setValue(() => (current = next));
36
+ apply();
37
+ };
38
+ onSettled(() => {
39
+ update(parse(storage.read(storageKey)));
40
+ const onChange = (event) => {
41
+ const detail = event.detail;
42
+ if (detail.storageKey === storageKey)
43
+ update(parse(detail.value));
44
+ };
45
+ const unwatch = storage.watch(storageKey, (raw) => update(parse(raw)));
46
+ window.addEventListener(changeEvent, onChange);
47
+ return () => {
48
+ unwatch();
49
+ window.removeEventListener(changeEvent, onChange);
50
+ };
51
+ });
52
+ const set = (next) => {
53
+ storage.write(storageKey, next === unset ? null : next);
54
+ update(next);
55
+ window.dispatchEvent(new CustomEvent(changeEvent, {
56
+ detail: { storageKey, value: next },
57
+ }));
58
+ };
59
+ return { value, set, apply };
60
+ }
@@ -0,0 +1,3 @@
1
+ import type { PreferenceStorage } from "./preference.ts";
2
+ /** `localStorage`, which only the browser can read. */
3
+ export declare const localStoragePreference: PreferenceStorage;
@@ -0,0 +1,36 @@
1
+ import { scriptLiteral } from "./preference.js";
2
+ /** `localStorage`, which only the browser can read. */
3
+ export const localStoragePreference = {
4
+ read: (key) => {
5
+ try {
6
+ return localStorage.getItem(key);
7
+ }
8
+ catch {
9
+ // Storage can be unavailable, such as with cookies blocked; the
10
+ // choice then lasts until the page is left.
11
+ return null;
12
+ }
13
+ },
14
+ write: (key, value) => {
15
+ try {
16
+ if (value === null)
17
+ localStorage.removeItem(key);
18
+ else
19
+ localStorage.setItem(key, value);
20
+ }
21
+ catch {
22
+ // See read.
23
+ }
24
+ },
25
+ watch: (key, onChange) => {
26
+ const onStorage = (event) => {
27
+ // A `null` key means the whole storage was cleared.
28
+ if (event.key === key || event.key === null)
29
+ onChange(event.newValue);
30
+ };
31
+ window.addEventListener("storage", onStorage);
32
+ return () => window.removeEventListener("storage", onStorage);
33
+ },
34
+ readsOnServer: false,
35
+ script: (key) => `m=localStorage.getItem(${scriptLiteral(key)})`,
36
+ };
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Where a preference goes on `<html>`: a `data-*` attribute set to its
3
+ * value, or `"class"` for a class named after it.
4
+ */
5
+ export type PreferenceAttribute = "class" | `data-${string}`;
6
+ /**
7
+ * Where a preference is saved, such as `localStorage` or a cookie. The
8
+ * values are raw: the utility on top parses them.
9
+ */
10
+ export interface PreferenceStorage {
11
+ /** The saved value; none if nothing is saved or it cannot be read. */
12
+ read: (key: string) => string | null | undefined;
13
+ /** Saves a value; `null` removes the saved one. */
14
+ write: (key: string, value: string | null) => void;
15
+ /**
16
+ * Calls `onChange` with the saved value when another tab may have changed
17
+ * it. Returns the function that stops listening.
18
+ */
19
+ watch: (key: string, onChange: (value: string | null | undefined) => void) => () => void;
20
+ /**
21
+ * Whether `read` works on the server too, so that `createPreference` can
22
+ * read the value during the first render without hydration disagreeing.
23
+ */
24
+ readsOnServer: boolean;
25
+ /**
26
+ * The head script's source for reading the value: statements that set
27
+ * the variable `m` to the saved value. They run inside a `try`, and must
28
+ * not be able to close the script element whatever `key` holds.
29
+ */
30
+ script: (key: string) => string;
31
+ }
32
+ /** A saved value, if it is one of `values`; otherwise `unset`. */
33
+ export declare function parsePreference<T extends string>(raw: string | null | undefined, values: readonly string[], unset: T): T;
34
+ /**
35
+ * Puts a preference's value on `<html>`. With `"class"`, first removes the
36
+ * classes of every value in `values`, so only one is on at a time.
37
+ */
38
+ export declare function applyPreference(attribute: PreferenceAttribute, value: string, values: readonly string[]): void;
39
+ /** A string as a JavaScript literal that cannot close the script element. */
40
+ export declare function scriptLiteral(value: string): string;
@@ -0,0 +1,22 @@
1
+ /** A saved value, if it is one of `values`; otherwise `unset`. */
2
+ export function parsePreference(raw, values, unset) {
3
+ return raw && values.includes(raw) ? raw : unset;
4
+ }
5
+ /**
6
+ * Puts a preference's value on `<html>`. With `"class"`, first removes the
7
+ * classes of every value in `values`, so only one is on at a time.
8
+ */
9
+ export function applyPreference(attribute, value, values) {
10
+ const root = document.documentElement;
11
+ if (attribute === "class") {
12
+ root.classList.remove(...values);
13
+ root.classList.add(value);
14
+ }
15
+ else {
16
+ root.setAttribute(attribute, value);
17
+ }
18
+ }
19
+ /** A string as a JavaScript literal that cannot close the script element. */
20
+ export function scriptLiteral(value) {
21
+ return JSON.stringify(value).replaceAll("<", "\\u003c");
22
+ }
@@ -0,0 +1,17 @@
1
+ import type { PreferenceAttribute, PreferenceStorage } from "./preference.ts";
2
+ export interface PreferenceScriptOptions {
3
+ storageKey: string;
4
+ attribute: PreferenceAttribute;
5
+ /** Every value the attribute can hold. A saved value outside it is unset. */
6
+ values: readonly string[];
7
+ /**
8
+ * A JavaScript expression for the value to apply when nothing valid is
9
+ * saved, such as a literal from `scriptLiteral`.
10
+ */
11
+ fallback: string;
12
+ }
13
+ /**
14
+ * The source of a script for the document head that applies a saved
15
+ * preference, or `fallback`, to `<html>` before the page first paints.
16
+ */
17
+ export declare function preferenceScript(storage: PreferenceStorage, options: PreferenceScriptOptions): string;
@@ -0,0 +1,13 @@
1
+ import { scriptLiteral } from "./preference.js";
2
+ /**
3
+ * The source of a script for the document head that applies a saved
4
+ * preference, or `fallback`, to `<html>` before the page first paints.
5
+ */
6
+ export function preferenceScript(storage, options) {
7
+ const values = options.values.map(scriptLiteral).join(",");
8
+ const apply = options.attribute === "class"
9
+ ? "d.classList.remove.apply(d.classList,l);d.classList.add(r)"
10
+ : `d.setAttribute(${scriptLiteral(options.attribute)},r)`;
11
+ return (`(function(){var m,l=[${values}];try{${storage.script(options.storageKey)}}catch(e){}` +
12
+ `var r=l.indexOf(m)>=0?m:${options.fallback},d=document.documentElement;${apply}})()`);
13
+ }
@@ -0,0 +1,52 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { ElementProps } from "./renderElement.tsx";
3
+ /** The props of a part whose value lies along a range. */
4
+ export interface RangeProps {
5
+ /** The value, clamped to `min` and `max`; `null` while unknown. */
6
+ value: number | null;
7
+ /** Default 0, the implicit `aria-valuemin` of WAI-ARIA's range roles. */
8
+ min?: number;
9
+ /** Default 100, the implicit `aria-valuemax`. */
10
+ max?: number;
11
+ /**
12
+ * `Intl.NumberFormat` options for the value's text. The `percent` style,
13
+ * the default, formats the percentage of the range; any other style
14
+ * formats the value itself.
15
+ */
16
+ format?: Intl.NumberFormatOptions;
17
+ /** The language to format in, else the runtime's default. */
18
+ locale?: Intl.LocalesArgument;
19
+ /**
20
+ * The consumer's text for the value, which replaces the formatted one when
21
+ * it is a string.
22
+ */
23
+ "aria-valuetext"?: JSX.HTMLAttributes<HTMLElement>["aria-valuetext"];
24
+ }
25
+ export interface Range {
26
+ /**
27
+ * How far along the range the value is, from 0 at `min` to 1 at `max`, or
28
+ * `undefined` while it is `null`.
29
+ */
30
+ fraction: () => number | undefined;
31
+ /**
32
+ * The element's own props: `role`, `aria-valuemin`, `aria-valuemax`,
33
+ * `aria-valuenow` and `aria-valuetext`, and a `style` that sets the CSS
34
+ * custom property to the percentage of the range, such as `25%`. The
35
+ * value's attributes and the property are absent while it is `null`.
36
+ */
37
+ props: ElementProps;
38
+ /**
39
+ * What the element announces, for a part that shows it as text: the
40
+ * consumer's `aria-valuetext`, else the formatted value.
41
+ */
42
+ valueText: () => string | undefined;
43
+ }
44
+ /**
45
+ * The value, attributes and text of a part along a range, shared by
46
+ * Progress and Meter (PLAN.md section 8, "API conventions"). Call it in the
47
+ * part's body with the element's role; `property` names the custom
48
+ * property, such as `--progress`.
49
+ * Omit `value`, `min`, `max`, `format` and `locale` from the props the part
50
+ * merges onto its element.
51
+ */
52
+ export declare function createRange(props: RangeProps, role: "progressbar" | "meter", property: `--${string}`): Range;
@@ -0,0 +1,60 @@
1
+ import { createMemo } from "solid-js";
2
+ /**
3
+ * The value, attributes and text of a part along a range, shared by
4
+ * Progress and Meter (PLAN.md section 8, "API conventions"). Call it in the
5
+ * part's body with the element's role; `property` names the custom
6
+ * property, such as `--progress`.
7
+ * Omit `value`, `min`, `max`, `format` and `locale` from the props the part
8
+ * merges onto its element.
9
+ */
10
+ export function createRange(props, role, property) {
11
+ const min = () => props.min ?? 0;
12
+ const max = () => props.max ?? 100;
13
+ const value = () => props.value == null
14
+ ? undefined
15
+ : Math.min(Math.max(props.value, min()), max());
16
+ // How far along the range the value is, from 0 to 1.
17
+ const fraction = () => {
18
+ const current = value();
19
+ return current === undefined
20
+ ? undefined
21
+ : (current - min()) / (max() - min());
22
+ };
23
+ const text = createMemo(() => {
24
+ const current = value();
25
+ if (current === undefined)
26
+ return undefined;
27
+ const format = props.format;
28
+ // The percent style formats a fraction: of the range, not the value.
29
+ const percent = !format || format.style === "percent";
30
+ return new Intl.NumberFormat(props.locale, format ?? { style: "percent" }).format(percent ? fraction() : current);
31
+ });
32
+ return {
33
+ fraction,
34
+ props: {
35
+ role,
36
+ get "aria-valuemin"() {
37
+ return min();
38
+ },
39
+ get "aria-valuemax"() {
40
+ return max();
41
+ },
42
+ get "aria-valuenow"() {
43
+ return value();
44
+ },
45
+ get "aria-valuetext"() {
46
+ return text();
47
+ },
48
+ get style() {
49
+ const current = fraction();
50
+ return {
51
+ [property]: current === undefined ? undefined : `${current * 100}%`,
52
+ };
53
+ },
54
+ },
55
+ valueText: () => {
56
+ const own = props["aria-valuetext"];
57
+ return typeof own === "string" ? own : text();
58
+ },
59
+ };
60
+ }
@@ -0,0 +1,25 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ /**
3
+ * The props a part hands to its element, or to the consumer's `render`
4
+ * function. Typed for `HTMLElement` so that they spread onto any tag. `ref`
5
+ * is narrowed to a callback: `JSX.Ref<HTMLElement>` also allows an element
6
+ * and an array, which are not assignable to the ref of a specific element.
7
+ */
8
+ export type ElementProps = Omit<JSX.HTMLAttributes<HTMLElement>, "ref"> & {
9
+ ref?: (element: HTMLElement) => void;
10
+ };
11
+ /**
12
+ * A part's `render` prop: the component to render the part with. It
13
+ * receives the element's props, spreads them and adds only what the part
14
+ * cannot know, such as `href`.
15
+ */
16
+ export type RenderProp = (props: ElementProps) => JSX.Element;
17
+ /**
18
+ * Renders a part's element: the consumer's `render` if there is one, else
19
+ * the part's `fallback`. `render` is read once, untracked, when the part is
20
+ * created, as an event handler is. Call it in the part's body, with
21
+ * `elementProps` already merged (`mergeElementProps`).
22
+ */
23
+ export declare function renderElement(props: {
24
+ render?: RenderProp;
25
+ }, elementProps: ElementProps, fallback: RenderProp): JSX.Element;
@@ -0,0 +1,11 @@
1
+ import { untrack } from "solid-js";
2
+ /**
3
+ * Renders a part's element: the consumer's `render` if there is one, else
4
+ * the part's `fallback`. `render` is read once, untracked, when the part is
5
+ * created, as an event handler is. Call it in the part's body, with
6
+ * `elementProps` already merged (`mergeElementProps`).
7
+ */
8
+ export function renderElement(props, elementProps, fallback) {
9
+ const Render = untrack(() => props.render) ?? fallback;
10
+ return <Render {...elementProps}/>;
11
+ }
@@ -0,0 +1,42 @@
1
+ /** An item of a composite widget, as its roving focus knows it. */
2
+ export interface RovingItem {
3
+ readonly element: HTMLElement;
4
+ }
5
+ /** What `createRovingFocus` returns. */
6
+ export interface RovingFocus<T extends RovingItem> {
7
+ /** Adds a mounted item to the focus order; returns what removes it. */
8
+ register(item: T): () => void;
9
+ /**
10
+ * Whether this item has the tab stop. Before any item has registered, as
11
+ * on the server and in the first render, it is `fallback`, which the
12
+ * component decides.
13
+ */
14
+ isTabStop(element: HTMLElement | undefined, fallback: boolean): boolean;
15
+ /** Moves focus for an arrow, Home or End key pressed on an item. */
16
+ navigate(event: KeyboardEvent, from: HTMLElement): void;
17
+ }
18
+ /**
19
+ * One tab stop among a group's items, with arrow keys between them (roving
20
+ * tabindex, PLAN.md section 8, "A group of items is one tab stop"): Toggle
21
+ * Group's items, Tabs' tabs. Pass the options as getters and functions, so
22
+ * they are read where they are used:
23
+ *
24
+ * ```ts
25
+ * const roving = createRovingFocus({
26
+ * get vertical() { return props.orientation === "vertical"; },
27
+ * focusable: (item) => !item.disabled(),
28
+ * preferred: (item) => item.element === active(),
29
+ * });
30
+ * ```
31
+ *
32
+ * Items register once mounted, so the order follows the document. The tab
33
+ * stop is the first `preferred` focusable item, else the first focusable
34
+ * one. Arrow keys move focus among the focusable items and wrap: Left and
35
+ * Right, mirrored right to left, or Up and Down when `vertical`, and Home
36
+ * and End. With a modifier held, the keys are left to the browser.
37
+ */
38
+ export declare function createRovingFocus<T extends RovingItem>(options: {
39
+ readonly vertical: boolean;
40
+ focusable(item: T): boolean;
41
+ preferred(item: T): boolean;
42
+ }): RovingFocus<T>;
@@ -0,0 +1,71 @@
1
+ import { createMemo, createSignal } from "solid-js";
2
+ // Document order, which can differ from the order items registered in.
3
+ const byPosition = (a, b) => a.element.compareDocumentPosition(b.element) &
4
+ Node.DOCUMENT_POSITION_FOLLOWING
5
+ ? -1
6
+ : 1;
7
+ /**
8
+ * One tab stop among a group's items, with arrow keys between them (roving
9
+ * tabindex, PLAN.md section 8, "A group of items is one tab stop"): Toggle
10
+ * Group's items, Tabs' tabs. Pass the options as getters and functions, so
11
+ * they are read where they are used:
12
+ *
13
+ * ```ts
14
+ * const roving = createRovingFocus({
15
+ * get vertical() { return props.orientation === "vertical"; },
16
+ * focusable: (item) => !item.disabled(),
17
+ * preferred: (item) => item.element === active(),
18
+ * });
19
+ * ```
20
+ *
21
+ * Items register once mounted, so the order follows the document. The tab
22
+ * stop is the first `preferred` focusable item, else the first focusable
23
+ * one. Arrow keys move focus among the focusable items and wrap: Left and
24
+ * Right, mirrored right to left, or Up and Down when `vertical`, and Home
25
+ * and End. With a modifier held, the keys are left to the browser.
26
+ */
27
+ export function createRovingFocus(options) {
28
+ const [items, setItems] = createSignal([]);
29
+ const focusable = () => items().filter(options.focusable).sort(byPosition);
30
+ const tabStop = createMemo(() => {
31
+ const list = focusable();
32
+ return (list.find(options.preferred) ?? list[0])?.element;
33
+ });
34
+ return {
35
+ register: (item) => {
36
+ setItems((list) => [...list, item]);
37
+ return () => setItems((list) => list.filter((other) => other !== item));
38
+ },
39
+ isTabStop: (element, fallback) => items().length ? !!element && tabStop() === element : fallback,
40
+ navigate: (event, from) => {
41
+ // With a modifier, the keys are the browser's or the app's, such as
42
+ // Alt+Left for back.
43
+ if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey)
44
+ return;
45
+ const list = focusable();
46
+ const count = list.length;
47
+ const index = list.findIndex((item) => item.element === from);
48
+ // Right to left, the next item is on the left. `direction` inherits,
49
+ // so the item's own is the group's.
50
+ const [previous, next] = options.vertical
51
+ ? ["ArrowUp", "ArrowDown"]
52
+ : getComputedStyle(from).direction === "rtl"
53
+ ? ["ArrowRight", "ArrowLeft"]
54
+ : ["ArrowLeft", "ArrowRight"];
55
+ const target = event.key === previous
56
+ ? index - 1
57
+ : event.key === next
58
+ ? index + 1
59
+ : event.key === "Home"
60
+ ? 0
61
+ : event.key === "End"
62
+ ? count - 1
63
+ : undefined;
64
+ if (target === undefined || !count)
65
+ return;
66
+ // Arrow keys and Home and End would scroll the page.
67
+ event.preventDefault();
68
+ list[(target + count) % count].element.focus();
69
+ },
70
+ };
71
+ }
@@ -0,0 +1,22 @@
1
+ import type { ButtonElementProps, TwoStateButtonState } from "./button.ts";
2
+ /**
3
+ * The own props of a part that is a toggle button, a native
4
+ * `<button type="button" aria-pressed>` (WAI-ARIA button pattern): Toggle's
5
+ * Root and Toggle Group's Item. `value` is whether it is pressed. Pass the
6
+ * state as getters, so it is read where the props are used:
7
+ *
8
+ * ```ts
9
+ * const own = toggleButtonProps(
10
+ * {
11
+ * get value() { return pressed(); },
12
+ * get disabled() { return props.disabled; },
13
+ * setValue: setPressed,
14
+ * },
15
+ * "Toggle.Root",
16
+ * );
17
+ * ```
18
+ *
19
+ * `part` names the part in the development warning of a `render` that is
20
+ * not a button.
21
+ */
22
+ export declare function toggleButtonProps(state: TwoStateButtonState, part: string): ButtonElementProps;
@@ -0,0 +1,35 @@
1
+ import { merge } from "solid-js";
2
+ import { twoStateButtonProps } from "./button.js";
3
+ /**
4
+ * The own props of a part that is a toggle button, a native
5
+ * `<button type="button" aria-pressed>` (WAI-ARIA button pattern): Toggle's
6
+ * Root and Toggle Group's Item. `value` is whether it is pressed. Pass the
7
+ * state as getters, so it is read where the props are used:
8
+ *
9
+ * ```ts
10
+ * const own = toggleButtonProps(
11
+ * {
12
+ * get value() { return pressed(); },
13
+ * get disabled() { return props.disabled; },
14
+ * setValue: setPressed,
15
+ * },
16
+ * "Toggle.Root",
17
+ * );
18
+ * ```
19
+ *
20
+ * `part` names the part in the development warning of a `render` that is
21
+ * not a button.
22
+ */
23
+ export function toggleButtonProps(state, part) {
24
+ return merge(twoStateButtonProps(state, "aria-pressed", part), {
25
+ get "data-pressed"() {
26
+ return state.value ? "" : undefined;
27
+ },
28
+ get "data-unpressed"() {
29
+ return state.value ? undefined : "";
30
+ },
31
+ get "data-disabled"() {
32
+ return state.disabled ? "" : undefined;
33
+ },
34
+ });
35
+ }
@@ -0,0 +1 @@
1
+ export * as Meter from "./index.parts.ts";
@@ -0,0 +1 @@
1
+ export * as Meter from "./index.parts.js";
@@ -0,0 +1,2 @@
1
+ export { MeterRoot as Root } from "./root/MeterRoot.tsx";
2
+ export { MeterValue as Value } from "./value/MeterValue.tsx";
@@ -0,0 +1,2 @@
1
+ export { MeterRoot as Root } from "./root/MeterRoot.jsx";
2
+ export { MeterValue as Value } from "./value/MeterValue.jsx";