@nmnmcc/standard 0.0.0-stage → 0.4.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 (260) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +516 -2
  3. package/dist/Extend.d.ts +15 -0
  4. package/dist/Extend.d.ts.map +1 -0
  5. package/dist/Extend.js +1 -0
  6. package/dist/Node.d.ts +71 -0
  7. package/dist/Node.d.ts.map +1 -0
  8. package/dist/Palette-BIJ96BEh.js +84 -0
  9. package/dist/Palette.d.ts +8 -0
  10. package/dist/Palette.d.ts.map +1 -0
  11. package/dist/Palette.js +1 -0
  12. package/dist/Standard-BPpzr5SM.js +488 -0
  13. package/dist/Standard.d.ts +249 -0
  14. package/dist/Standard.d.ts.map +1 -0
  15. package/dist/Standard.js +1 -0
  16. package/dist/Ui-B9xQqqNT.js +468 -0
  17. package/dist/Ui.d.ts +500 -0
  18. package/dist/Ui.d.ts.map +1 -0
  19. package/dist/Ui.js +1 -0
  20. package/dist/index.d.ts +25 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +4 -0
  23. package/dist/rolldown-runtime-BMBA7GOT.js +10 -0
  24. package/dist/standard/button/button.d.ts +21 -0
  25. package/dist/standard/button/button.d.ts.map +1 -0
  26. package/dist/standard/colour/colour.d.ts +93 -0
  27. package/dist/standard/colour/colour.d.ts.map +1 -0
  28. package/dist/standard/field/field.d.ts +91 -0
  29. package/dist/standard/field/field.d.ts.map +1 -0
  30. package/dist/standard/form/form.d.ts +16 -0
  31. package/dist/standard/form/form.d.ts.map +1 -0
  32. package/dist/standard/icon/icon.d.ts +31 -0
  33. package/dist/standard/icon/icon.d.ts.map +1 -0
  34. package/dist/standard/image/image.d.ts +21 -0
  35. package/dist/standard/image/image.d.ts.map +1 -0
  36. package/dist/standard/layout/layout.d.ts +73 -0
  37. package/dist/standard/layout/layout.d.ts.map +1 -0
  38. package/dist/standard/list/list.d.ts +26 -0
  39. package/dist/standard/list/list.d.ts.map +1 -0
  40. package/dist/standard/page/page.d.ts +20 -0
  41. package/dist/standard/page/page.d.ts.map +1 -0
  42. package/dist/standard/table/table.d.ts +18 -0
  43. package/dist/standard/table/table.d.ts.map +1 -0
  44. package/dist/standard/text/text.d.ts +39 -0
  45. package/dist/standard/text/text.d.ts.map +1 -0
  46. package/dist/standard/tokens/classes.d.ts +16 -0
  47. package/dist/standard/tokens/classes.d.ts.map +1 -0
  48. package/dist/standard/tokens/tokens.d.ts +162 -0
  49. package/dist/standard/tokens/tokens.d.ts.map +1 -0
  50. package/dist/standard.css +2 -0
  51. package/dist/ui/arrow/arrow.d.ts +18 -0
  52. package/dist/ui/arrow/arrow.d.ts.map +1 -0
  53. package/dist/ui/avatar/avatar.d.ts +15 -0
  54. package/dist/ui/avatar/avatar.d.ts.map +1 -0
  55. package/dist/ui/board/board.d.ts +34 -0
  56. package/dist/ui/board/board.d.ts.map +1 -0
  57. package/dist/ui/bullet/bullet.d.ts +15 -0
  58. package/dist/ui/bullet/bullet.d.ts.map +1 -0
  59. package/dist/ui/card/card.d.ts +24 -0
  60. package/dist/ui/card/card.d.ts.map +1 -0
  61. package/dist/ui/carousel/carousel.d.ts +17 -0
  62. package/dist/ui/carousel/carousel.d.ts.map +1 -0
  63. package/dist/ui/check/check.d.ts +20 -0
  64. package/dist/ui/check/check.d.ts.map +1 -0
  65. package/dist/ui/choice/choice.d.ts +19 -0
  66. package/dist/ui/choice/choice.d.ts.map +1 -0
  67. package/dist/ui/control/control.d.ts +57 -0
  68. package/dist/ui/control/control.d.ts.map +1 -0
  69. package/dist/ui/dialog/dialog.d.ts +30 -0
  70. package/dist/ui/dialog/dialog.d.ts.map +1 -0
  71. package/dist/ui/disclosure/disclosure.d.ts +20 -0
  72. package/dist/ui/disclosure/disclosure.d.ts.map +1 -0
  73. package/dist/ui/flap/flap.d.ts +18 -0
  74. package/dist/ui/flap/flap.d.ts.map +1 -0
  75. package/dist/ui/item/item.d.ts +21 -0
  76. package/dist/ui/item/item.d.ts.map +1 -0
  77. package/dist/ui/key/key.d.ts +12 -0
  78. package/dist/ui/key/key.d.ts.map +1 -0
  79. package/dist/ui/menu/menu.d.ts +26 -0
  80. package/dist/ui/menu/menu.d.ts.map +1 -0
  81. package/dist/ui/modal/modal.d.ts +22 -0
  82. package/dist/ui/modal/modal.d.ts.map +1 -0
  83. package/dist/ui/navigator/navigator.d.ts +137 -0
  84. package/dist/ui/navigator/navigator.d.ts.map +1 -0
  85. package/dist/ui/notice/notice.d.ts +34 -0
  86. package/dist/ui/notice/notice.d.ts.map +1 -0
  87. package/dist/ui/options/options.d.ts +19 -0
  88. package/dist/ui/options/options.d.ts.map +1 -0
  89. package/dist/ui/pager/pager.d.ts +29 -0
  90. package/dist/ui/pager/pager.d.ts.map +1 -0
  91. package/dist/ui/pagination/pagination.d.ts +14 -0
  92. package/dist/ui/pagination/pagination.d.ts.map +1 -0
  93. package/dist/ui/popover/popover.d.ts +20 -0
  94. package/dist/ui/popover/popover.d.ts.map +1 -0
  95. package/dist/ui/progress/progress.d.ts +17 -0
  96. package/dist/ui/progress/progress.d.ts.map +1 -0
  97. package/dist/ui/route/route.d.ts +27 -0
  98. package/dist/ui/route/route.d.ts.map +1 -0
  99. package/dist/ui/sheet/sheet.d.ts +35 -0
  100. package/dist/ui/sheet/sheet.d.ts.map +1 -0
  101. package/dist/ui/skeleton/skeleton.d.ts +15 -0
  102. package/dist/ui/skeleton/skeleton.d.ts.map +1 -0
  103. package/dist/ui/slider/slider.d.ts +23 -0
  104. package/dist/ui/slider/slider.d.ts.map +1 -0
  105. package/dist/ui/stepper/stepper.d.ts +32 -0
  106. package/dist/ui/stepper/stepper.d.ts.map +1 -0
  107. package/dist/ui/tabs/tabs.d.ts +22 -0
  108. package/dist/ui/tabs/tabs.d.ts.map +1 -0
  109. package/dist/ui/tag/tag.d.ts +17 -0
  110. package/dist/ui/tag/tag.d.ts.map +1 -0
  111. package/dist/ui/tile/tile.d.ts +38 -0
  112. package/dist/ui/tile/tile.d.ts.map +1 -0
  113. package/dist/ui/toasts/toasts.d.ts +17 -0
  114. package/dist/ui/toasts/toasts.d.ts.map +1 -0
  115. package/dist/ui/toggle/toggle.d.ts +17 -0
  116. package/dist/ui/toggle/toggle.d.ts.map +1 -0
  117. package/dist/ui/trail/trail.d.ts +22 -0
  118. package/dist/ui/trail/trail.d.ts.map +1 -0
  119. package/package.json +70 -3
  120. package/src/Extend.ts +14 -0
  121. package/src/Node.ts +44 -0
  122. package/src/Palette.ts +7 -0
  123. package/src/Standard.ts +109 -0
  124. package/src/Ui.ts +156 -0
  125. package/src/index.ts +27 -0
  126. package/src/standard/button/button.module.css +43 -0
  127. package/src/standard/button/button.module.d.css.ts +6 -0
  128. package/src/standard/button/button.ts +38 -0
  129. package/src/standard/colour/colour.ts +197 -0
  130. package/src/standard/field/field.module.css +222 -0
  131. package/src/standard/field/field.module.d.css.ts +13 -0
  132. package/src/standard/field/field.ts +266 -0
  133. package/src/standard/form/form.ts +25 -0
  134. package/src/standard/icon/icon.module.css +349 -0
  135. package/src/standard/icon/icon.module.d.css.ts +112 -0
  136. package/src/standard/icon/icon.ts +148 -0
  137. package/src/standard/image/image.module.css +42 -0
  138. package/src/standard/image/image.module.d.css.ts +12 -0
  139. package/src/standard/image/image.ts +39 -0
  140. package/src/standard/layout/layout.module.css +224 -0
  141. package/src/standard/layout/layout.module.d.css.ts +41 -0
  142. package/src/standard/layout/layout.ts +133 -0
  143. package/src/standard/list/list.module.css +71 -0
  144. package/src/standard/list/list.module.d.css.ts +10 -0
  145. package/src/standard/list/list.ts +53 -0
  146. package/src/standard/page/page.module.css +244 -0
  147. package/src/standard/page/page.module.d.css.ts +9 -0
  148. package/src/standard/page/page.ts +32 -0
  149. package/src/standard/table/table.module.css +40 -0
  150. package/src/standard/table/table.module.d.css.ts +6 -0
  151. package/src/standard/table/table.ts +36 -0
  152. package/src/standard/text/text.module.css +149 -0
  153. package/src/standard/text/text.module.d.css.ts +19 -0
  154. package/src/standard/text/text.ts +77 -0
  155. package/src/standard/tokens/classes.ts +31 -0
  156. package/src/standard/tokens/tokens.module.css +294 -0
  157. package/src/standard/tokens/tokens.module.d.css.ts +40 -0
  158. package/src/standard/tokens/tokens.ts +339 -0
  159. package/src/ui/arrow/arrow.module.css +38 -0
  160. package/src/ui/arrow/arrow.module.d.css.ts +13 -0
  161. package/src/ui/arrow/arrow.ts +35 -0
  162. package/src/ui/avatar/avatar.module.css +25 -0
  163. package/src/ui/avatar/avatar.module.d.css.ts +5 -0
  164. package/src/ui/avatar/avatar.ts +28 -0
  165. package/src/ui/board/board.module.css +94 -0
  166. package/src/ui/board/board.module.d.css.ts +10 -0
  167. package/src/ui/board/board.ts +84 -0
  168. package/src/ui/bullet/bullet.module.css +28 -0
  169. package/src/ui/bullet/bullet.module.d.css.ts +5 -0
  170. package/src/ui/bullet/bullet.ts +16 -0
  171. package/src/ui/card/card.module.css +80 -0
  172. package/src/ui/card/card.module.d.css.ts +6 -0
  173. package/src/ui/card/card.ts +42 -0
  174. package/src/ui/carousel/carousel.module.css +24 -0
  175. package/src/ui/carousel/carousel.module.d.css.ts +5 -0
  176. package/src/ui/carousel/carousel.ts +30 -0
  177. package/src/ui/check/check.module.css +53 -0
  178. package/src/ui/check/check.module.d.css.ts +8 -0
  179. package/src/ui/check/check.ts +46 -0
  180. package/src/ui/choice/choice.module.css +32 -0
  181. package/src/ui/choice/choice.module.d.css.ts +6 -0
  182. package/src/ui/choice/choice.ts +31 -0
  183. package/src/ui/control/control.module.css +127 -0
  184. package/src/ui/control/control.module.d.css.ts +12 -0
  185. package/src/ui/control/control.ts +97 -0
  186. package/src/ui/dialog/dialog.module.css +42 -0
  187. package/src/ui/dialog/dialog.module.d.css.ts +6 -0
  188. package/src/ui/dialog/dialog.ts +44 -0
  189. package/src/ui/disclosure/disclosure.module.css +57 -0
  190. package/src/ui/disclosure/disclosure.module.d.css.ts +7 -0
  191. package/src/ui/disclosure/disclosure.ts +32 -0
  192. package/src/ui/flap/flap.module.css +161 -0
  193. package/src/ui/flap/flap.module.d.css.ts +48 -0
  194. package/src/ui/flap/flap.ts +78 -0
  195. package/src/ui/item/item.module.css +39 -0
  196. package/src/ui/item/item.module.d.css.ts +7 -0
  197. package/src/ui/item/item.ts +33 -0
  198. package/src/ui/key/key.module.css +15 -0
  199. package/src/ui/key/key.module.d.css.ts +5 -0
  200. package/src/ui/key/key.ts +13 -0
  201. package/src/ui/menu/menu.module.css +11 -0
  202. package/src/ui/menu/menu.module.d.css.ts +5 -0
  203. package/src/ui/menu/menu.ts +48 -0
  204. package/src/ui/modal/modal.module.css +66 -0
  205. package/src/ui/modal/modal.module.d.css.ts +8 -0
  206. package/src/ui/modal/modal.ts +55 -0
  207. package/src/ui/navigator/navigator.module.css +658 -0
  208. package/src/ui/navigator/navigator.module.d.css.ts +39 -0
  209. package/src/ui/navigator/navigator.ts +337 -0
  210. package/src/ui/notice/notice.module.css +80 -0
  211. package/src/ui/notice/notice.module.d.css.ts +10 -0
  212. package/src/ui/notice/notice.ts +57 -0
  213. package/src/ui/options/options.module.css +30 -0
  214. package/src/ui/options/options.module.d.css.ts +6 -0
  215. package/src/ui/options/options.ts +31 -0
  216. package/src/ui/pager/pager.module.css +38 -0
  217. package/src/ui/pager/pager.module.d.css.ts +8 -0
  218. package/src/ui/pager/pager.ts +48 -0
  219. package/src/ui/pagination/pagination.module.css +38 -0
  220. package/src/ui/pagination/pagination.module.d.css.ts +7 -0
  221. package/src/ui/pagination/pagination.ts +52 -0
  222. package/src/ui/popover/popover.module.css +94 -0
  223. package/src/ui/popover/popover.module.d.css.ts +8 -0
  224. package/src/ui/popover/popover.ts +139 -0
  225. package/src/ui/progress/progress.module.css +69 -0
  226. package/src/ui/progress/progress.module.d.css.ts +6 -0
  227. package/src/ui/progress/progress.ts +26 -0
  228. package/src/ui/route/route.module.css +116 -0
  229. package/src/ui/route/route.module.d.css.ts +11 -0
  230. package/src/ui/route/route.ts +58 -0
  231. package/src/ui/sheet/sheet.module.css +194 -0
  232. package/src/ui/sheet/sheet.module.d.css.ts +7 -0
  233. package/src/ui/sheet/sheet.ts +47 -0
  234. package/src/ui/skeleton/skeleton.module.css +44 -0
  235. package/src/ui/skeleton/skeleton.module.d.css.ts +6 -0
  236. package/src/ui/skeleton/skeleton.ts +20 -0
  237. package/src/ui/slider/slider.module.css +56 -0
  238. package/src/ui/slider/slider.module.d.css.ts +6 -0
  239. package/src/ui/slider/slider.ts +48 -0
  240. package/src/ui/stepper/stepper.module.css +32 -0
  241. package/src/ui/stepper/stepper.module.d.css.ts +7 -0
  242. package/src/ui/stepper/stepper.ts +62 -0
  243. package/src/ui/tabs/tabs.module.css +38 -0
  244. package/src/ui/tabs/tabs.module.d.css.ts +6 -0
  245. package/src/ui/tabs/tabs.ts +24 -0
  246. package/src/ui/tag/tag.module.css +23 -0
  247. package/src/ui/tag/tag.module.d.css.ts +5 -0
  248. package/src/ui/tag/tag.ts +17 -0
  249. package/src/ui/tile/tile.module.css +137 -0
  250. package/src/ui/tile/tile.module.d.css.ts +13 -0
  251. package/src/ui/tile/tile.ts +69 -0
  252. package/src/ui/toasts/toasts.module.css +38 -0
  253. package/src/ui/toasts/toasts.module.d.css.ts +6 -0
  254. package/src/ui/toasts/toasts.ts +26 -0
  255. package/src/ui/toggle/toggle.module.css +46 -0
  256. package/src/ui/toggle/toggle.module.d.css.ts +6 -0
  257. package/src/ui/toggle/toggle.ts +29 -0
  258. package/src/ui/trail/trail.module.css +42 -0
  259. package/src/ui/trail/trail.module.d.css.ts +5 -0
  260. package/src/ui/trail/trail.ts +28 -0
@@ -0,0 +1,197 @@
1
+ /**
2
+ * How the colours are made.
3
+ *
4
+ * A colour of the palette is not picked by eye: it is worked out from a hue,
5
+ * so that whatever the hue, a block of it is vivid and whatever is written
6
+ * on it or in it can be read. The stylesheet holds what this module gives
7
+ * for the hues of the palette, and the tests check that it does, and check
8
+ * the guarantees below for every hue there is.
9
+ *
10
+ * It works in OKLCH, where lightness, chroma and hue can each be changed
11
+ * without changing the others, and it measures readability by the contrast
12
+ * ratio of WCAG 2: 4.5 to 1, the ratio required for body text.
13
+ *
14
+ * @since 0.4.0
15
+ */
16
+
17
+ /** A colour as lightness (0 to 1), chroma (0 to about 0.4) and hue (degrees). */
18
+ export interface Oklch {
19
+ readonly l: number
20
+ readonly c: number
21
+ readonly h: number
22
+ }
23
+
24
+ /** The contrast every piece of text is given against what it is on. */
25
+ export const readable = 4.5
26
+
27
+ /**
28
+ * The most chroma a colour is given. Past it the primaries of a screen
29
+ * glare: a flat colour should look printed, not lit.
30
+ */
31
+ export const strongest = 0.22
32
+
33
+ /** How much more chroma a block must gain by carrying ink before it gives up carrying paper. */
34
+ export const gain = 0.02
35
+
36
+ /** How far a surface on a ground may be tinted towards the text, as a card or a pressed control is. Text is readable on that too. */
37
+ export const tinted = 0.12
38
+
39
+ /**
40
+ * The two neutrals, black and white. In the light scheme they are what is
41
+ * printed and what it is printed on, ink and paper; in the dark scheme they
42
+ * swap places.
43
+ */
44
+ export const black: Oklch = { l: 0.16, c: 0, h: 0 }
45
+ export const white: Oklch = { l: 0.985, c: 0.003, h: 95 }
46
+
47
+ type Rgb = readonly [number, number, number]
48
+
49
+ /** Linear-light sRGB, which may be outside 0 to 1: then the screen can't show the colour. */
50
+ const linear = ({ c, h, l }: Oklch): Rgb => {
51
+ const a = c * Math.cos((h * Math.PI) / 180)
52
+ const b = c * Math.sin((h * Math.PI) / 180)
53
+ const long = (l + 0.3963377774 * a + 0.2158037573 * b) ** 3
54
+ const medium = (l - 0.1055613458 * a - 0.0638541728 * b) ** 3
55
+ const short = (l - 0.0894841775 * a - 1.291485548 * b) ** 3
56
+ return [
57
+ 4.0767416621 * long - 3.3077115913 * medium + 0.2309699292 * short,
58
+ -1.2684380046 * long + 2.6097574011 * medium - 0.3413193965 * short,
59
+ -0.0041960863 * long - 0.7034186147 * medium + 1.707614701 * short
60
+ ]
61
+ }
62
+
63
+ const shown = (colour: Oklch): boolean => linear(colour).every((channel) => channel >= -0.0001 && channel <= 1.0001)
64
+
65
+ const clamp = (value: number): number => Math.min(1, Math.max(0, value))
66
+
67
+ /** The colour as it is written for a screen: `#rrggbb`. */
68
+ export const hex = (colour: Oklch): string =>
69
+ `#${linear(colour)
70
+ .map((channel) => {
71
+ const value = clamp(channel)
72
+ const encoded = value <= 0.0031308 ? 12.92 * value : 1.055 * value ** (1 / 2.4) - 0.055
73
+ return Math.round(encoded * 255)
74
+ .toString(16)
75
+ .padStart(2, "0")
76
+ })
77
+ .join("")}`
78
+
79
+ /** How light a colour is to the eye, as WCAG measures it, from the colour a screen actually shows. */
80
+ const luminance = (colour: Oklch): number => {
81
+ const [red, green, blue] = linear(colour).map(clamp)
82
+ return 0.2126 * (red ?? 0) + 0.7152 * (green ?? 0) + 0.0722 * (blue ?? 0)
83
+ }
84
+
85
+ /** The contrast ratio of two colours, from 1 (the same) to 21 (black on white). */
86
+ export const contrast = (one: Oklch, other: Oklch): number => {
87
+ const [dark, light] = [luminance(one), luminance(other)].sort((a, b) => a - b)
88
+ return ((light ?? 0) + 0.05) / ((dark ?? 0) + 0.05)
89
+ }
90
+
91
+ /** The colour a part of the way from one to another, in a straight line through the colour space. */
92
+ export const between = (from: Oklch, to: Oklch, part: number): Oklch => {
93
+ const point = (colour: Oklch) =>
94
+ [
95
+ colour.l,
96
+ colour.c * Math.cos((colour.h * Math.PI) / 180),
97
+ colour.c * Math.sin((colour.h * Math.PI) / 180)
98
+ ] as const
99
+ const [l1, a1, b1] = point(from)
100
+ const [l2, a2, b2] = point(to)
101
+ const a = a1 + (a2 - a1) * part
102
+ const b = b1 + (b2 - b1) * part
103
+ return { l: l1 + (l2 - l1) * part, c: Math.hypot(a, b), h: ((Math.atan2(b, a) * 180) / Math.PI + 360) % 360 }
104
+ }
105
+
106
+ /** The most chroma a screen can show at a lightness and a hue, up to a limit. */
107
+ const fullest = (l: number, h: number, limit: number): Oklch => {
108
+ const search = (low: number, high: number, steps: number): number =>
109
+ steps === 0
110
+ ? low
111
+ : shown({ l, c: (low + high) / 2, h })
112
+ ? search((low + high) / 2, high, steps - 1)
113
+ : search(low, (low + high) / 2, steps - 1)
114
+ return { l, c: shown({ l, c: limit, h }) ? limit : search(0, limit, 20), h }
115
+ }
116
+
117
+ /** Every lightness tried, from dark to light. */
118
+ const steps: ReadonlyArray<number> = Array.from({ length: 321 }, (_, index) => 0.18 + index * 0.0025)
119
+
120
+ /** The better of two colours: the one with more chroma, and of two with the same, the one `prefer` says. */
121
+ const better =
122
+ (prefer: "lighter" | "darker") =>
123
+ (best: Oklch | undefined, next: Oklch): Oklch =>
124
+ best === undefined ||
125
+ next.c > best.c + 0.0005 ||
126
+ (Math.abs(next.c - best.c) <= 0.0005 && (prefer === "lighter") === next.l > best.l)
127
+ ? next
128
+ : best
129
+
130
+ /** The most vivid colour of a hue that `on` can be read on, or that can be read on `on`. */
131
+ const vivid = (h: number, limit: number, on: Oklch, prefer: "lighter" | "darker"): Oklch =>
132
+ steps
133
+ .map((l) => fullest(l, h, limit))
134
+ .filter((colour) => contrast(colour, on) >= readable)
135
+ .reduce<Oklch | undefined>(better(prefer), undefined) ?? on
136
+
137
+ /** What a ground looks like where it is tinted towards its text. */
138
+ const surface = (ground: Oklch, text: Oklch): Oklch => between(ground, text, tinted)
139
+
140
+ /** Text taken as far towards its ground as it can go and still be readable on it, tinted or not: for muted text. */
141
+ export const muted = (text: Oklch, ground: Oklch): Oklch =>
142
+ Array.from({ length: 101 }, (_, index) => between(text, ground, index / 100))
143
+ .filter((colour) => contrast(colour, ground) >= readable && contrast(colour, surface(ground, text)) >= readable)
144
+ .at(-1) ?? text
145
+
146
+ /** What the algorithm gives for a hue. */
147
+ export interface Swatch {
148
+ /** A block of the colour. */
149
+ readonly fill: Oklch
150
+ /** What is written on the block: the black or the white. */
151
+ readonly on: Oklch
152
+ /** Muted text, written on the block. */
153
+ readonly soft: Oklch
154
+ /** The colour as text or a thin mark, on the white. */
155
+ readonly onWhite: Oklch
156
+ /** The colour as text or a thin mark, on the black. */
157
+ readonly onBlack: Oklch
158
+ }
159
+
160
+ /**
161
+ * The colours of a hue. `chroma` is the most it may have: leave it out for
162
+ * a colour, pass 0 for a grey.
163
+ *
164
+ * 1. The block. Of every lightness, at the most chroma a screen can show
165
+ * there, take the most vivid colour that paper can be read on, and the
166
+ * most vivid that ink can be read on. The more vivid of the two is the
167
+ * block, and what can be read on it is what it carries. Where the two
168
+ * are equally vivid, it carries paper. So a hue that is only itself
169
+ * when light, as yellow is, is light and carries black, and one that is
170
+ * itself when deep, as blue is, is deep and carries paper.
171
+ * 2. The colour as text. On paper, the most vivid colour of the hue that
172
+ * can be read there; on black, the same. Neither has to be the
173
+ * block's colour.
174
+ * 3. Muted text is the text's colour taken towards the ground, as far as
175
+ * it can go and still be read.
176
+ *
177
+ * So for any hue: the block has all the chroma its lightness allows,
178
+ * text on it reaches 4.5 to 1, and so does the colour as text on the white
179
+ * ground and on the black one.
180
+ */
181
+ export const swatch = (h: number, chroma: number = strongest): Swatch => {
182
+ const light = vivid(h, chroma, black, "darker")
183
+ const dark = vivid(h, chroma, white, "lighter")
184
+ const carriesPaper = light.c <= dark.c + gain
185
+ const fill = carriesPaper ? dark : light
186
+ const on = carriesPaper ? white : black
187
+ return {
188
+ fill,
189
+ on,
190
+ soft: muted(on, fill),
191
+ onWhite: vivid(h, chroma, surface(white, black), "lighter"),
192
+ onBlack: vivid(h, chroma, surface(black, white), "darker")
193
+ }
194
+ }
195
+
196
+ /** The hues of the palette. Grey has none: it is given no chroma. */
197
+ export const hues = { red: 28, orange: 55, yellow: 98, green: 150, teal: 200, blue: 262, purple: 305 } as const
@@ -0,0 +1,222 @@
1
+ /* field, textarea and select: a labelled input. */
2
+
3
+ @layer standard.type {
4
+ .field > label {
5
+ font-size: 0.75rem;
6
+ line-height: 1rem;
7
+ font-weight: 700;
8
+ letter-spacing: 0.08em;
9
+ text-transform: uppercase;
10
+ }
11
+ }
12
+
13
+ @layer standard.controls {
14
+ .field {
15
+ display: grid;
16
+ grid-template-columns: minmax(0, 1fr) auto;
17
+ column-gap: calc(2 * var(--s-unit));
18
+ row-gap: var(--s-unit);
19
+
20
+ & > * {
21
+ grid-column: 1 / -1;
22
+ }
23
+ & > label {
24
+ grid-column: 1;
25
+ }
26
+
27
+ /* What has to be filled in says so beside its name, in the colour that is read. */
28
+ & > label > .must {
29
+ color: var(--s-read);
30
+ }
31
+
32
+ /* How much of the room is used, across from the label. */
33
+ & > .count {
34
+ grid-column: 2;
35
+ grid-row: 1;
36
+ color: var(--s-muted);
37
+ font-size: 0.75rem;
38
+ line-height: 1rem;
39
+ font-variant-numeric: tabular-nums;
40
+
41
+ &.full {
42
+ color: var(--s-wrong);
43
+ font-weight: 700;
44
+ }
45
+ }
46
+
47
+ & > p {
48
+ font-size: 0.875rem;
49
+ line-height: 1.5rem;
50
+ }
51
+
52
+ & > .hint {
53
+ color: var(--s-muted);
54
+ }
55
+
56
+ & > [role="alert"] {
57
+ font-weight: 700;
58
+ }
59
+
60
+ /* The error the Model holds colours the whole field: in red, whatever the accent is. */
61
+ &:has([aria-invalid="true"]) {
62
+ & > label,
63
+ & > [role="alert"] {
64
+ color: var(--s-wrong);
65
+ }
66
+
67
+ & > .box {
68
+ background: color-mix(in oklch, var(--s-wrong) 24%, transparent);
69
+ }
70
+ }
71
+ }
72
+
73
+ /*
74
+ * A place to write is a filled block, a step deeper than what is only
75
+ * shown, and it has no frame. The frame is kept for the one that is being
76
+ * written in: it appears with the caret, so on a page of blocks the only
77
+ * outlined thing is where the typing goes.
78
+ */
79
+ .box {
80
+ position: relative;
81
+ display: flex;
82
+ align-items: stretch;
83
+ min-block-size: var(--s-target);
84
+ /* Room for the frame is kept while there is none, so nothing moves when it appears. */
85
+ border-width: calc(var(--s-unit) / 4);
86
+ border-color: transparent;
87
+ background: color-mix(in oklch, var(--s-fg) 16%, transparent);
88
+ color: var(--s-fg);
89
+ cursor: text;
90
+
91
+ & > input,
92
+ & > textarea,
93
+ & > select {
94
+ flex: 1;
95
+ min-inline-size: 0;
96
+ inline-size: 100%;
97
+ padding: calc(0.75 * var(--s-unit)) calc(1.5 * var(--s-unit));
98
+ background: transparent;
99
+ line-height: 1.5rem;
100
+ appearance: none;
101
+ /* What is being typed can always be selected: that is writing, not reading. */
102
+ user-select: text;
103
+ -webkit-user-select: text;
104
+
105
+ &::placeholder {
106
+ color: var(--s-muted);
107
+ opacity: 1;
108
+ }
109
+ /* The box's frame shows the focus, so the input inside it draws none of its own. */
110
+ &:focus-visible {
111
+ outline: none;
112
+ }
113
+ }
114
+
115
+ & > textarea {
116
+ field-sizing: content;
117
+ min-block-size: calc(9 * var(--s-unit));
118
+ resize: none;
119
+ }
120
+
121
+ & > select {
122
+ cursor: pointer;
123
+ }
124
+
125
+ /* With the caret in it, the box is framed, twice over, in the colour that is read. */
126
+ &:has(> :is(input, textarea, select):focus) {
127
+ border-color: var(--s-read);
128
+ outline: calc(var(--s-unit) / 4) solid var(--s-read);
129
+ outline-offset: 0;
130
+ }
131
+
132
+ /* One that only shows a value is as light as everything else that is shown. */
133
+ &:has(> [readonly]) {
134
+ background: color-mix(in oklch, var(--s-fg) 6%, transparent);
135
+ cursor: default;
136
+ }
137
+
138
+ &:has(> :disabled) {
139
+ background: color-mix(in oklch, var(--s-fg) 8%, transparent);
140
+ color: var(--s-muted);
141
+ cursor: not-allowed;
142
+
143
+ & > :disabled {
144
+ cursor: not-allowed;
145
+ }
146
+ }
147
+ }
148
+
149
+ /* A pictogram or a unit before or after what is typed: part of the box, and a press on it puts the caret in. */
150
+ .affix {
151
+ flex: none;
152
+ padding-block: calc(0.75 * var(--s-unit));
153
+ color: var(--s-muted);
154
+ line-height: 1.5rem;
155
+ cursor: inherit;
156
+ user-select: none;
157
+
158
+ &:first-child {
159
+ padding-inline-start: calc(1.5 * var(--s-unit));
160
+ }
161
+ &:not(:first-child) {
162
+ padding-inline-end: calc(1.5 * var(--s-unit));
163
+ }
164
+ & + :is(input, textarea, select) {
165
+ padding-inline-start: var(--s-unit);
166
+ }
167
+ :is(input, textarea, select):has(+ &) {
168
+ padding-inline-end: var(--s-unit);
169
+ }
170
+ }
171
+
172
+ /* Something to do with what is typed, such as clearing it or showing it: a button at the end of the box. */
173
+ .act {
174
+ flex: none;
175
+ inline-size: calc(var(--s-target) - var(--s-unit) / 2);
176
+ background: var(--s-fg);
177
+ color: var(--s-bg);
178
+ font-size: 1.25rem;
179
+ line-height: 1.5rem;
180
+ cursor: pointer;
181
+ touch-action: manipulation;
182
+
183
+ &:active:not(:disabled) {
184
+ background: var(--s-mark);
185
+ color: var(--s-on-mark);
186
+ }
187
+
188
+ &:disabled {
189
+ background: transparent;
190
+ color: var(--s-muted);
191
+ cursor: not-allowed;
192
+ }
193
+
194
+ &:focus-visible {
195
+ outline-offset: calc(var(--s-unit) / -2);
196
+ outline-color: var(--s-bg);
197
+ }
198
+ }
199
+
200
+ /* The arrow that says a field opens a list. */
201
+ .select {
202
+ & > select {
203
+ padding-inline-end: calc(5 * var(--s-unit));
204
+ }
205
+ /* While nothing is chosen, what it shows is a prompt, in the colour of one. */
206
+ & > select:has(> option[hidden]:checked) {
207
+ color: var(--s-muted);
208
+ }
209
+
210
+ &::after {
211
+ content: "";
212
+ position: absolute;
213
+ inset-block-start: calc(50% - 0.4em);
214
+ inset-inline-end: calc(1.5 * var(--s-unit));
215
+ inline-size: 0.8em;
216
+ block-size: 0.8em;
217
+ background: currentColor;
218
+ clip-path: polygon(50% 100%, 95% 55%, 83.7% 43.7%, 50% 77.4%, 16.3% 43.7%, 5% 55%);
219
+ pointer-events: none;
220
+ }
221
+ }
222
+ }
@@ -0,0 +1,13 @@
1
+ declare const styles: {
2
+ readonly "act": string;
3
+ readonly "affix": string;
4
+ readonly "box": string;
5
+ readonly "count": string;
6
+ readonly "field": string;
7
+ readonly "full": string;
8
+ readonly "hint": string;
9
+ readonly "must": string;
10
+ readonly "select": string;
11
+ };
12
+ export = styles;
13
+
@@ -0,0 +1,266 @@
1
+ /**
2
+ * `field`, `textarea` and `select`: a labelled input.
3
+ *
4
+ * @since 0.4.0
5
+ */
6
+ import type { Html } from "effect/ui/html"
7
+ import { node, type Node } from "../../Node.ts"
8
+ import { type Icon, iconOf } from "../icon/icon.ts"
9
+ import styles from "./field.module.css"
10
+
11
+ /** What every input has around it: its name, its label, and what is said about it. */
12
+ export interface Frame {
13
+ readonly name: string
14
+ readonly label: string
15
+ /** What to enter, shown under the field. */
16
+ readonly hint?: string
17
+ /** What is wrong with the value as it stands. It colours the field red and is read out. */
18
+ readonly error?: string | null
19
+ /** It has to be filled in: the label says so, and so does a screen reader. The Model still checks it. */
20
+ readonly required?: boolean
21
+ readonly disabled?: boolean
22
+ /** Given when the caret leaves the field: the moment to check what was typed. */
23
+ readonly onBlur?: unknown
24
+ }
25
+
26
+ /** One thing to do with what is typed, as a button at the end of the box: clear it, show it, paste into it. */
27
+ export interface Action<Msg> {
28
+ readonly icon: Icon
29
+ /** What the button does, for a screen reader. */
30
+ readonly label: string
31
+ readonly onPress: Msg
32
+ }
33
+
34
+ const parts = <Msg>(h: Html.H<Msg>, options: Frame & { readonly value?: string; readonly maxLength?: number }) => {
35
+ const id = h.id(`s-field-${options.name}`)
36
+ const error =
37
+ options.error === undefined || options.error === null || options.error === "" ? undefined : options.error
38
+ const described = [options.hint === undefined ? [] : [`${id}-hint`], error === undefined ? [] : [`${id}-error`]]
39
+ .flat()
40
+ .join(" ")
41
+ const used = options.value?.length ?? 0
42
+ return {
43
+ id,
44
+ invalid: error === undefined ? undefined : ("true" as const),
45
+ described: described === "" ? undefined : described,
46
+ required: options.required === true ? ("true" as const) : undefined,
47
+ /** The label, and across from it how much of the room is used. */
48
+ head: [
49
+ h.label
50
+ .attr("for", id)
51
+ .children(
52
+ options.label,
53
+ options.required === true && h.span.class(styles.must).attr("aria-hidden", "true").children(" *")
54
+ ),
55
+ options.maxLength !== undefined &&
56
+ h.span
57
+ .class(styles.count, used >= options.maxLength && styles.full)
58
+ .attr("aria-hidden", "true")
59
+ .children(`${used} / ${options.maxLength}`)
60
+ ],
61
+ /** What is said under the box. */
62
+ foot: [
63
+ options.hint !== undefined && h.p.class(styles.hint).attr("id", `${id}-hint`).children(options.hint),
64
+ error !== undefined && h.p.attr("id", `${id}-error`).attr("role", "alert").children(error)
65
+ ]
66
+ }
67
+ }
68
+
69
+ export const field = <Msg>(h: Html.H<Msg>) => {
70
+ /** A pictogram or a unit in the box. It is a label, so a press on it puts the caret in the input. */
71
+ const affix = (id: string, content: Icon | string | undefined, icon: boolean) =>
72
+ content !== undefined &&
73
+ h.label
74
+ .class(styles.affix)
75
+ .attr("for", id)
76
+ .children(icon ? iconOf(h, content as Icon) : content)
77
+ const act = (action: Action<Msg> | undefined, disabled: boolean | undefined) =>
78
+ action !== undefined &&
79
+ h.button
80
+ .class(styles.act)
81
+ .attr("type", "button")
82
+ .attr("aria-label", action.label)
83
+ .disabled(disabled === true)
84
+ .on("click", action.onPress)
85
+ .children(iconOf(h, action.icon))
86
+
87
+ return {
88
+ /** A labelled text input, a filled block that is framed only while it is written in, with the error the Model holds for it. */
89
+ field: (
90
+ options: Omit<Frame, "onBlur"> & {
91
+ readonly value: string
92
+ readonly onInput: (value: string) => Msg
93
+ readonly onBlur?: Msg
94
+ readonly type?:
95
+ | "text"
96
+ | "email"
97
+ | "password"
98
+ | "search"
99
+ | "tel"
100
+ | "url"
101
+ | "number"
102
+ | "date"
103
+ | "time"
104
+ | "datetime-local"
105
+ | "month"
106
+ readonly placeholder?: string
107
+ /** A pictogram at the start of the box that says what goes in it: a magnifier for a search, a lock for a password. */
108
+ readonly icon?: Icon
109
+ /** Words before what is typed, such as a currency or a country code. They are read out with the label. */
110
+ readonly prefix?: string
111
+ /** Words after what is typed, such as a unit. They are read out with the label. */
112
+ readonly suffix?: string
113
+ /** A button at the end of the box. */
114
+ readonly action?: Action<Msg>
115
+ /** The keyboard a touch screen offers: the digits for a code, the digits and a point for an amount. */
116
+ readonly inputMode?: "text" | "numeric" | "decimal" | "tel" | "email" | "url" | "search"
117
+ /** What the browser may fill in, by the names of HTML's `autocomplete`: `"email"`, `"current-password"`, `"one-time-code"`, or `"off"`. */
118
+ readonly autoComplete?: string
119
+ /** The field the keyboard is in when the page, or the sheet or dialog it is in, opens. One at most. */
120
+ readonly autoFocus?: boolean
121
+ /** The most characters it takes. The count is shown across from the label. */
122
+ readonly maxLength?: number
123
+ /** The least, the most and the step of a number, a date or a time. */
124
+ readonly min?: number | string
125
+ readonly max?: number | string
126
+ readonly step?: number | string
127
+ /** It shows a value that can be copied but not changed, and is drawn as a block, not as a box. */
128
+ readonly readOnly?: boolean
129
+ /** Values the browser suggests as the person types. */
130
+ readonly suggestions?: ReadonlyArray<string>
131
+ }
132
+ ): Node<"block", Msg> => {
133
+ const { described, foot, head, id, invalid, required } = parts(h, options)
134
+ const input = h.input
135
+ .attr("id", id)
136
+ .attr("name", options.name)
137
+ .attr("type", options.type ?? "text")
138
+ .attr("placeholder", options.placeholder)
139
+ .attr("inputmode", options.inputMode)
140
+ .attr("autocomplete", options.autoComplete)
141
+ .attr("autofocus", options.autoFocus === true ? "" : undefined)
142
+ .attr("maxlength", options.maxLength === undefined ? undefined : String(options.maxLength))
143
+ .attr("min", options.min === undefined ? undefined : String(options.min))
144
+ .attr("max", options.max === undefined ? undefined : String(options.max))
145
+ .attr("step", options.step === undefined ? undefined : String(options.step))
146
+ .attr("readonly", options.readOnly === true ? "" : undefined)
147
+ .attr("aria-required", required)
148
+ .attr("aria-invalid", invalid)
149
+ .attr("aria-describedby", described)
150
+ .attr("list", options.suggestions === undefined ? undefined : `${id}-list`)
151
+ .disabled(options.disabled === true)
152
+ .value(options.value)
153
+ .on("input", ({ value }) => options.onInput(value))
154
+ return node(
155
+ h.div
156
+ .class(styles.field)
157
+ .children(
158
+ head,
159
+ h.div
160
+ .class(styles.box)
161
+ .children(
162
+ affix(id, options.icon, true),
163
+ affix(id, options.prefix, false),
164
+ options.onBlur === undefined ? input : input.on("blur", options.onBlur),
165
+ affix(id, options.suffix, false),
166
+ act(options.action, options.disabled)
167
+ ),
168
+ options.suggestions !== undefined &&
169
+ h.datalist
170
+ .attr("id", `${id}-list`)
171
+ .children(options.suggestions.map((value) => h.option.attr("value", value))),
172
+ foot
173
+ )
174
+ )
175
+ },
176
+
177
+ /** A labelled text input of several lines. It grows with what is typed. */
178
+ textarea: (
179
+ options: Omit<Frame, "onBlur"> & {
180
+ readonly value: string
181
+ readonly onInput: (value: string) => Msg
182
+ readonly onBlur?: Msg
183
+ readonly placeholder?: string
184
+ /** How many lines it is high while it is empty. Three, unless said. */
185
+ readonly rows?: number
186
+ /** The most characters it takes. The count is shown across from the label. */
187
+ readonly maxLength?: number
188
+ /** It shows a value that can be copied but not changed, and is drawn as a block, not as a box. */
189
+ readonly readOnly?: boolean
190
+ }
191
+ ): Node<"block", Msg> => {
192
+ const { described, foot, head, id, invalid, required } = parts(h, options)
193
+ const input = h.textarea
194
+ .attr("id", id)
195
+ .attr("name", options.name)
196
+ .attr("rows", String(options.rows ?? 3))
197
+ .attr("placeholder", options.placeholder)
198
+ .attr("maxlength", options.maxLength === undefined ? undefined : String(options.maxLength))
199
+ .attr("readonly", options.readOnly === true ? "" : undefined)
200
+ .attr("aria-required", required)
201
+ .attr("aria-invalid", invalid)
202
+ .attr("aria-describedby", described)
203
+ .disabled(options.disabled === true)
204
+ .value(options.value)
205
+ .on("input", ({ value }) => options.onInput(value))
206
+ return node(
207
+ h.div
208
+ .class(styles.field)
209
+ .children(
210
+ head,
211
+ h.div.class(styles.box).children(options.onBlur === undefined ? input : input.on("blur", options.onBlur)),
212
+ foot
213
+ )
214
+ )
215
+ },
216
+
217
+ /** A labelled choice from a list, using the browser's own picker: a wheel under a finger, a dropdown under a mouse. */
218
+ select: <A extends string>(
219
+ options: Omit<Frame, "onBlur"> & {
220
+ readonly value: A
221
+ readonly onChange: (value: string) => Msg
222
+ readonly options: ReadonlyArray<{ readonly value: A; readonly label: string; readonly disabled?: boolean }>
223
+ /** What is shown while nothing is chosen: while the value is none of the options. It cannot be chosen itself. */
224
+ readonly placeholder?: string
225
+ /** A pictogram at the start of the box that says what is chosen in it. */
226
+ readonly icon?: Icon
227
+ }
228
+ ): Node<"block", Msg> => {
229
+ const { described, foot, head, id, invalid, required } = parts(h, options)
230
+ const chosen = options.options.some((option) => option.value === options.value)
231
+ return node(
232
+ h.div.class(styles.field).children(
233
+ head,
234
+ h.div.class(styles.box, styles.select).children(
235
+ affix(id, options.icon, true),
236
+ h.select
237
+ .attr("id", id)
238
+ .attr("name", options.name)
239
+ .attr("aria-required", required)
240
+ .attr("aria-invalid", invalid)
241
+ .attr("aria-describedby", described)
242
+ .disabled(options.disabled === true)
243
+ .on("change", ({ value }) => options.onChange(value))
244
+ .children(
245
+ options.placeholder !== undefined &&
246
+ h.option
247
+ .attr("value", "")
248
+ .attr("hidden", "")
249
+ .selected(!chosen)
250
+ .disabled(true)
251
+ .children(options.placeholder),
252
+ options.options.map((option) =>
253
+ h.option
254
+ .attr("value", option.value)
255
+ .selected(option.value === options.value)
256
+ .disabled(option.disabled === true)
257
+ .children(option.label)
258
+ )
259
+ )
260
+ ),
261
+ foot
262
+ )
263
+ )
264
+ }
265
+ }
266
+ }