@astryxdesign/core 0.7.17 → 0.7.18-canary.g32556bb

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 (473) hide show
  1. package/dist/AlertDialog/AlertDialog.d.ts.map +1 -1
  2. package/dist/AlertDialog/AlertDialog.js +7 -0
  3. package/dist/AppShell/AppShell.d.ts.map +1 -1
  4. package/dist/AppShell/AppShell.js +5 -3
  5. package/dist/AppShell/observeMainOverflow.d.ts +5 -0
  6. package/dist/AppShell/observeMainOverflow.d.ts.map +1 -1
  7. package/dist/AppShell/observeMainOverflow.js +39 -9
  8. package/dist/BottomSheet/BottomSheet.d.ts.map +1 -1
  9. package/dist/BottomSheet/BottomSheet.js +8 -0
  10. package/dist/BottomSheet/useSheetGestures.d.ts +8 -0
  11. package/dist/BottomSheet/useSheetGestures.d.ts.map +1 -1
  12. package/dist/BottomSheet/useSheetGestures.js +16 -1
  13. package/dist/Button/Button.d.ts.map +1 -1
  14. package/dist/Button/Button.js +3 -16
  15. package/dist/Card/Card.d.ts.map +1 -1
  16. package/dist/Card/Card.js +5 -1
  17. package/dist/Citation/Citation.d.ts.map +1 -1
  18. package/dist/Citation/Citation.js +2 -2
  19. package/dist/ClickableCard/ClickableCard.d.ts.map +1 -1
  20. package/dist/ClickableCard/ClickableCard.js +7 -2
  21. package/dist/DateInput/DateInput.d.ts +0 -3
  22. package/dist/DateInput/DateInput.d.ts.map +1 -1
  23. package/dist/DateInput/DateInput.js +3 -5
  24. package/dist/DateInput/NativeDateField.d.ts.map +1 -1
  25. package/dist/DateInput/NativeDateField.js +3 -5
  26. package/dist/DateInput/TouchDateField.d.ts.map +1 -1
  27. package/dist/DateInput/TouchDateField.js +2 -4
  28. package/dist/DateInput/useTypedDate.d.ts.map +1 -1
  29. package/dist/DateInput/useTypedDate.js +9 -6
  30. package/dist/DateRangeInput/DateRangeInput.d.ts.map +1 -1
  31. package/dist/DateRangeInput/DateRangeInput.js +13 -9
  32. package/dist/DateTimeInput/DateTimeInput.d.ts.map +1 -1
  33. package/dist/DateTimeInput/DateTimeInput.js +3 -15
  34. package/dist/DateTimeInput/TouchDateTimeField.d.ts.map +1 -1
  35. package/dist/DateTimeInput/TouchDateTimeField.js +2 -14
  36. package/dist/Dialog/Dialog.d.ts.map +1 -1
  37. package/dist/Dialog/Dialog.js +7 -1
  38. package/dist/Drawer/Drawer.d.ts.map +1 -1
  39. package/dist/Drawer/Drawer.js +7 -0
  40. package/dist/Field/Field.d.ts.map +1 -1
  41. package/dist/Field/Field.js +2 -1
  42. package/dist/Field/FieldLabel.d.ts.map +1 -1
  43. package/dist/Field/FieldLabel.js +9 -2
  44. package/dist/Field/InputClearButton.d.ts.map +1 -1
  45. package/dist/Field/InputClearButton.js +23 -0
  46. package/dist/Field/inputStyles.stylex.d.ts +22 -6
  47. package/dist/Field/inputStyles.stylex.d.ts.map +1 -1
  48. package/dist/Field/inputStyles.stylex.js +32 -0
  49. package/dist/FieldStatus/FieldStatus.d.ts.map +1 -1
  50. package/dist/FieldStatus/FieldStatus.js +7 -2
  51. package/dist/FileInput/FileInput.d.ts.map +1 -1
  52. package/dist/FileInput/FileInput.js +4 -4
  53. package/dist/InputGroup/InputGroup.d.ts.map +1 -1
  54. package/dist/InputGroup/InputGroup.js +6 -16
  55. package/dist/InputGroup/InputGroupText.d.ts.map +1 -1
  56. package/dist/InputGroup/InputGroupText.js +2 -8
  57. package/dist/Item/Item.d.ts.map +1 -1
  58. package/dist/Item/Item.js +8 -2
  59. package/dist/Kbd/Kbd.d.ts +3 -2
  60. package/dist/Kbd/Kbd.d.ts.map +1 -1
  61. package/dist/Kbd/Kbd.js +3 -2
  62. package/dist/Layer/gestureCounter.d.ts +5 -0
  63. package/dist/Layer/gestureCounter.d.ts.map +1 -1
  64. package/dist/Layer/gestureCounter.js +40 -4
  65. package/dist/Layer/inputHold.d.ts +13 -0
  66. package/dist/Layer/inputHold.d.ts.map +1 -1
  67. package/dist/Layer/inputHold.js +203 -14
  68. package/dist/Layer/layerHost.d.ts +3 -2
  69. package/dist/Layer/layerHost.d.ts.map +1 -1
  70. package/dist/Layer/layerHost.js +3 -2
  71. package/dist/Layer/outsidePanClaim.d.ts +17 -0
  72. package/dist/Layer/outsidePanClaim.d.ts.map +1 -1
  73. package/dist/Layer/outsidePanClaim.js +36 -8
  74. package/dist/Layer/outsidePress.d.ts +7 -1
  75. package/dist/Layer/outsidePress.d.ts.map +1 -1
  76. package/dist/Layer/outsidePress.js +9 -2
  77. package/dist/Layer/useLayer.d.ts +21 -5
  78. package/dist/Layer/useLayer.d.ts.map +1 -1
  79. package/dist/Layer/useLayer.js +63 -40
  80. package/dist/Lightbox/Lightbox.d.ts.map +1 -1
  81. package/dist/Lightbox/Lightbox.js +7 -0
  82. package/dist/MetadataList/MetadataList.d.ts +1 -1
  83. package/dist/MetadataList/MetadataList.d.ts.map +1 -1
  84. package/dist/MetadataList/MetadataList.js +8 -0
  85. package/dist/MultiSelector/MultiSelector.d.ts.map +1 -1
  86. package/dist/MultiSelector/MultiSelector.js +37 -17
  87. package/dist/NavIcon/NavIcon.d.ts +2 -2
  88. package/dist/NavIcon/NavIcon.d.ts.map +1 -1
  89. package/dist/NavIcon/NavIcon.js +2 -2
  90. package/dist/NavMenu/NavHeadingMenuItem.d.ts.map +1 -1
  91. package/dist/NavMenu/NavHeadingMenuItem.js +4 -3
  92. package/dist/NumberInput/NumberInput.d.ts +13 -5
  93. package/dist/NumberInput/NumberInput.d.ts.map +1 -1
  94. package/dist/NumberInput/NumberInput.js +6 -17
  95. package/dist/NumberInput/numberParser.d.ts +9 -2
  96. package/dist/NumberInput/numberParser.d.ts.map +1 -1
  97. package/dist/NumberInput/numberParser.js +15 -2
  98. package/dist/Overlay/OverlayScrim.d.ts.map +1 -1
  99. package/dist/Overlay/OverlayScrim.js +13 -4
  100. package/dist/Overlay/useOverlay.d.ts +1 -1
  101. package/dist/Pagination/Pagination.d.ts +3 -2
  102. package/dist/Pagination/Pagination.d.ts.map +1 -1
  103. package/dist/Pagination/Pagination.js +9 -2
  104. package/dist/Popover/usePopover.d.ts +37 -6
  105. package/dist/Popover/usePopover.d.ts.map +1 -1
  106. package/dist/Popover/usePopover.js +103 -20
  107. package/dist/PowerSearch/PowerSearch.d.ts +4 -1
  108. package/dist/PowerSearch/PowerSearch.d.ts.map +1 -1
  109. package/dist/PowerSearch/PowerSearch.js +3 -4
  110. package/dist/PowerSearch/PowerSearchBottomSheet.d.ts +1 -2
  111. package/dist/PowerSearch/PowerSearchBottomSheet.d.ts.map +1 -1
  112. package/dist/PowerSearch/PowerSearchBottomSheet.js +173 -185
  113. package/dist/PowerSearch/PowerSearchValueEditor.d.ts +0 -12
  114. package/dist/PowerSearch/PowerSearchValueEditor.d.ts.map +1 -1
  115. package/dist/PowerSearch/PowerSearchValueEditor.js +1 -51
  116. package/dist/PowerSearch/formatFilterValue.d.ts.map +1 -1
  117. package/dist/PowerSearch/formatFilterValue.js +6 -8
  118. package/dist/PowerSearch/formatTokenValue.d.ts +5 -10
  119. package/dist/PowerSearch/formatTokenValue.d.ts.map +1 -1
  120. package/dist/PowerSearch/formatTokenValue.js +21 -38
  121. package/dist/PowerSearch/relativeDateOptions.d.ts +30 -0
  122. package/dist/PowerSearch/relativeDateOptions.d.ts.map +1 -0
  123. package/dist/PowerSearch/relativeDateOptions.js +74 -0
  124. package/dist/ProgressBar/ProgressBar.d.ts +1 -1
  125. package/dist/ProgressBar/ProgressBar.d.ts.map +1 -1
  126. package/dist/ProgressBar/ProgressBar.js +20 -14
  127. package/dist/Resizable/createResizable.d.ts.map +1 -1
  128. package/dist/Resizable/createResizable.js +38 -4
  129. package/dist/SegmentedControl/SegmentedControlItem.d.ts.map +1 -1
  130. package/dist/SegmentedControl/SegmentedControlItem.js +5 -3
  131. package/dist/SelectableCard/SelectableCard.d.ts.map +1 -1
  132. package/dist/SelectableCard/SelectableCard.js +6 -2
  133. package/dist/Selector/Selector.d.ts.map +1 -1
  134. package/dist/Selector/Selector.js +2 -1
  135. package/dist/SideNav/SideNavItem.d.ts.map +1 -1
  136. package/dist/SideNav/SideNavItem.js +1 -1
  137. package/dist/Slider/Slider.d.ts.map +1 -1
  138. package/dist/Slider/Slider.js +53 -8
  139. package/dist/Stepper/Step.d.ts.map +1 -1
  140. package/dist/Stepper/Step.js +2 -2
  141. package/dist/TabList/Tab.d.ts.map +1 -1
  142. package/dist/TabList/Tab.js +2 -0
  143. package/dist/TabList/TabMenu.d.ts.map +1 -1
  144. package/dist/TabList/TabMenu.js +2 -0
  145. package/dist/Table/BaseTable.d.ts.map +1 -1
  146. package/dist/Table/BaseTable.js +30 -8
  147. package/dist/Table/Table.d.ts +9 -4
  148. package/dist/Table/Table.d.ts.map +1 -1
  149. package/dist/Table/Table.js +9 -4
  150. package/dist/Table/TableCell.d.ts.map +1 -1
  151. package/dist/Table/TableCell.js +2 -2
  152. package/dist/Table/TableHeaderCell.d.ts.map +1 -1
  153. package/dist/Table/TableHeaderCell.js +3 -7
  154. package/dist/Table/TableRow.d.ts.map +1 -1
  155. package/dist/Table/TableRow.js +10 -1
  156. package/dist/Table/plugins/rowExpansion/useTableRowExpansion.d.ts +11 -3
  157. package/dist/Table/plugins/rowExpansion/useTableRowExpansion.d.ts.map +1 -1
  158. package/dist/Table/plugins/rowExpansion/useTableRowExpansion.js +203 -51
  159. package/dist/Table/plugins/rowIndex/useTableRowIndex.d.ts.map +1 -1
  160. package/dist/Table/plugins/rowIndex/useTableRowIndex.js +4 -1
  161. package/dist/Table/plugins/rowStatus/useTableRowStatus.d.ts.map +1 -1
  162. package/dist/Table/plugins/rowStatus/useTableRowStatus.js +2 -0
  163. package/dist/Table/plugins/sortable/useTableSortable.d.ts.map +1 -1
  164. package/dist/Table/plugins/sortable/useTableSortable.js +18 -1
  165. package/dist/Table/plugins/stickyColumns/useTableStickyColumns.d.ts.map +1 -1
  166. package/dist/Table/plugins/stickyColumns/useTableStickyColumns.js +34 -24
  167. package/dist/Table/tableColumnAdmission.d.ts +7 -0
  168. package/dist/Table/tableColumnAdmission.d.ts.map +1 -1
  169. package/dist/Table/tableColumnAdmission.js +15 -0
  170. package/dist/Table/types.d.ts +5 -5
  171. package/dist/Text/Text.d.ts.map +1 -1
  172. package/dist/Text/Text.js +13 -8
  173. package/dist/Text/TruncationTooltip.d.ts +7 -39
  174. package/dist/Text/TruncationTooltip.d.ts.map +1 -1
  175. package/dist/Text/TruncationTooltip.js +47 -128
  176. package/dist/Text/cutText.d.ts +22 -0
  177. package/dist/Text/cutText.d.ts.map +1 -0
  178. package/dist/Text/cutText.js +92 -0
  179. package/dist/Text/cutTextOnKeyboardFocus.d.ts +41 -0
  180. package/dist/Text/cutTextOnKeyboardFocus.d.ts.map +1 -0
  181. package/dist/Text/cutTextOnKeyboardFocus.js +112 -0
  182. package/dist/Text/textClampContext.d.ts +1 -0
  183. package/dist/Text/textClampContext.d.ts.map +1 -1
  184. package/dist/Text/textClampContext.js +10 -5
  185. package/dist/Text/useTruncation.d.ts +23 -1
  186. package/dist/Text/useTruncation.d.ts.map +1 -1
  187. package/dist/Text/useTruncation.js +74 -28
  188. package/dist/TextInput/TextInput.d.ts +6 -6
  189. package/dist/Thumbnail/Thumbnail.js +4 -1
  190. package/dist/TimeInput/TimeInput.d.ts +0 -3
  191. package/dist/TimeInput/TimeInput.d.ts.map +1 -1
  192. package/dist/TimeInput/TimeInput.js +3 -5
  193. package/dist/TimeInput/TouchTimeField.d.ts.map +1 -1
  194. package/dist/TimeInput/TouchTimeField.js +3 -5
  195. package/dist/Timestamp/Timestamp.d.ts.map +1 -1
  196. package/dist/Timestamp/Timestamp.js +22 -16
  197. package/dist/Timestamp/TimestampHoverCard.d.ts +6 -3
  198. package/dist/Timestamp/TimestampHoverCard.d.ts.map +1 -1
  199. package/dist/Timestamp/TimestampHoverCard.js +30 -19
  200. package/dist/Timestamp/formatInstant.d.ts.map +1 -1
  201. package/dist/Timestamp/formatInstant.js +3 -3
  202. package/dist/Timestamp/tooltipEntries.d.ts.map +1 -1
  203. package/dist/Timestamp/tooltipEntries.js +4 -2
  204. package/dist/Toast/Toast.d.ts.map +1 -1
  205. package/dist/Toast/Toast.js +62 -31
  206. package/dist/Toast/ToastViewport.d.ts.map +1 -1
  207. package/dist/Toast/ToastViewport.js +35 -13
  208. package/dist/Toast/useToast.d.ts.map +1 -1
  209. package/dist/Toast/useToast.js +35 -14
  210. package/dist/Toolbar/Toolbar.d.ts.map +1 -1
  211. package/dist/Toolbar/Toolbar.js +20 -3
  212. package/dist/Tooltip/useTooltip.d.ts.map +1 -1
  213. package/dist/Tooltip/useTooltip.js +3 -3
  214. package/dist/astryx.css +41 -10
  215. package/dist/hooks/useAnnounceRenderedText.d.ts +10 -3
  216. package/dist/hooks/useAnnounceRenderedText.d.ts.map +1 -1
  217. package/dist/hooks/useAnnounceRenderedText.js +11 -3
  218. package/dist/hooks/useFocusTrap.d.ts +12 -0
  219. package/dist/hooks/useFocusTrap.d.ts.map +1 -1
  220. package/dist/hooks/useFocusTrap.js +13 -6
  221. package/dist/hooks/useHotkeys.d.ts +9 -1
  222. package/dist/hooks/useHotkeys.d.ts.map +1 -1
  223. package/dist/hooks/useHotkeys.js +52 -2
  224. package/dist/hooks/useInputStatusIcon.d.ts.map +1 -1
  225. package/dist/hooks/useInputStatusIcon.js +2 -1
  226. package/dist/hooks/useKeyboardHint.d.ts.map +1 -1
  227. package/dist/hooks/useKeyboardHint.js +4 -3
  228. package/dist/i18n/generated-english/DateRangeInput/DateRangeInput.generated.d.ts.map +1 -1
  229. package/dist/i18n/generated-english/DateRangeInput/DateRangeInput.generated.js +2 -0
  230. package/dist/i18n/generated-english/MultiSelector/MultiSelector.generated.d.ts.map +1 -1
  231. package/dist/i18n/generated-english/MultiSelector/MultiSelector.generated.js +1 -0
  232. package/dist/i18n/generated-english/PowerSearch/PowerSearchValueEditor.generated.d.ts.map +1 -1
  233. package/dist/i18n/generated-english/PowerSearch/PowerSearchValueEditor.generated.js +0 -6
  234. package/dist/i18n/generated-english/PowerSearch/relativeDateOptions.generated.d.ts +8 -0
  235. package/dist/i18n/generated-english/PowerSearch/relativeDateOptions.generated.d.ts.map +1 -0
  236. package/dist/i18n/generated-english/PowerSearch/relativeDateOptions.generated.js +17 -0
  237. package/dist/i18n/generated-english/Slider/Slider.generated.d.ts.map +1 -1
  238. package/dist/i18n/generated-english/Slider/Slider.generated.js +1 -0
  239. package/dist/i18n/generated-english/Thumbnail/Thumbnail.generated.d.ts.map +1 -1
  240. package/dist/i18n/generated-english/Thumbnail/Thumbnail.generated.js +1 -0
  241. package/dist/i18n/generated-locales/en.generated.d.ts.map +1 -1
  242. package/dist/i18n/generated-locales/en.generated.js +5 -0
  243. package/dist/i18n/generated-locales/pseudo.generated.d.ts.map +1 -1
  244. package/dist/i18n/generated-locales/pseudo.generated.js +5 -0
  245. package/dist/i18n/getLocaleDirection.d.ts +0 -16
  246. package/dist/i18n/getLocaleDirection.d.ts.map +1 -1
  247. package/dist/i18n/getLocaleDirection.js +16 -6
  248. package/dist/i18n/listFormat.d.ts +34 -0
  249. package/dist/i18n/listFormat.d.ts.map +1 -0
  250. package/dist/i18n/listFormat.js +45 -0
  251. package/dist/i18n/useListFormatter.d.ts +7 -3
  252. package/dist/i18n/useListFormatter.d.ts.map +1 -1
  253. package/dist/i18n/useListFormatter.js +16 -13
  254. package/dist/internal/module-loader.d.ts +15 -0
  255. package/dist/internal/module-loader.d.ts.map +1 -0
  256. package/dist/internal/module-loader.js +15 -0
  257. package/dist/motion/animatedLayout.d.ts +6 -0
  258. package/dist/motion/animatedLayout.d.ts.map +1 -1
  259. package/dist/motion/animatedLayout.js +201 -27
  260. package/dist/palette/color.d.ts +16 -0
  261. package/dist/palette/color.d.ts.map +1 -1
  262. package/dist/palette/color.js +37 -1
  263. package/dist/palette/derive-look.d.ts +235 -0
  264. package/dist/palette/derive-look.d.ts.map +1 -0
  265. package/dist/palette/derive-look.js +879 -0
  266. package/dist/palette/editor-look.d.ts +41 -0
  267. package/dist/palette/editor-look.d.ts.map +1 -0
  268. package/dist/palette/editor-look.js +401 -0
  269. package/dist/palette/mapping.d.ts +99 -6
  270. package/dist/palette/mapping.d.ts.map +1 -1
  271. package/dist/palette/mapping.js +68 -11
  272. package/dist/theme/tokens.d.ts +1 -1
  273. package/dist/theme/tokens.d.ts.map +1 -1
  274. package/dist/theme/tokens.js +1 -1
  275. package/dist/utils/characters.d.ts.map +1 -1
  276. package/dist/utils/characters.js +7 -0
  277. package/dist/utils/devWarning.d.ts +18 -0
  278. package/dist/utils/devWarning.d.ts.map +1 -1
  279. package/dist/utils/devWarning.js +30 -0
  280. package/dist/utils/focusReturn.d.ts +2 -0
  281. package/dist/utils/focusReturn.d.ts.map +1 -1
  282. package/dist/utils/focusReturn.js +3 -8
  283. package/dist/utils/hitArea.d.ts +4 -1
  284. package/dist/utils/hitArea.d.ts.map +1 -1
  285. package/dist/utils/hitArea.js +72 -9
  286. package/dist/utils/mergeProps.d.ts.map +1 -1
  287. package/dist/utils/mergeProps.js +53 -38
  288. package/dist/utils/scrollView.d.ts +2 -2
  289. package/dist/utils/scrollView.d.ts.map +1 -1
  290. package/dist/utils/scrollView.js +3 -3
  291. package/dist/utils/textDirection.d.ts +29 -5
  292. package/dist/utils/textDirection.d.ts.map +1 -1
  293. package/dist/utils/textDirection.js +56 -4
  294. package/dist/utils/themeProps.d.ts.map +1 -1
  295. package/dist/utils/themeProps.js +22 -3
  296. package/dist/utils/touchHitArea.d.ts.map +1 -1
  297. package/dist/utils/touchHitArea.js +4 -2
  298. package/dist/virtualization/useInlineListWindow.d.ts.map +1 -1
  299. package/dist/virtualization/useInlineListWindow.js +63 -4
  300. package/locales/en.json +26 -6
  301. package/locales/pseudo.json +15 -0
  302. package/package.json +10 -5
  303. package/src/AlertDialog/AlertDialog.tsx +15 -0
  304. package/src/AlertDialog/useImperativeAlertDialog.doc.mjs +7 -4
  305. package/src/AppShell/AppShell.tsx +6 -3
  306. package/src/AppShell/observeMainOverflow.ts +39 -9
  307. package/src/Badge/Badge.doc.mjs +3 -3
  308. package/src/BottomSheet/BottomSheet.tsx +15 -0
  309. package/src/BottomSheet/useSheetGestures.ts +26 -1
  310. package/src/Button/Button.tsx +3 -23
  311. package/src/Card/Card.doc.mjs +3 -3
  312. package/src/Card/Card.tsx +11 -6
  313. package/src/Chat/ChatSendButton.doc.mjs +6 -6
  314. package/src/Chat/ChatToolCalls.doc.mjs +1 -1
  315. package/src/CheckboxInput/CheckboxInput.doc.mjs +3 -3
  316. package/src/Citation/Citation.tsx +8 -2
  317. package/src/ClickableCard/ClickableCard.tsx +9 -2
  318. package/src/ComplexSelector/ComplexSelector.doc.mjs +3 -3
  319. package/src/ContextMenu/ContextMenuItem.doc.mjs +3 -3
  320. package/src/DateInput/DateInput.tsx +2 -4
  321. package/src/DateInput/NativeDateField.tsx +5 -4
  322. package/src/DateInput/TouchDateField.tsx +2 -3
  323. package/src/DateInput/useTypedDate.ts +13 -6
  324. package/src/DateRangeInput/DateRangeInput.tsx +14 -10
  325. package/src/DateTimeInput/DateTimeInput.tsx +2 -12
  326. package/src/DateTimeInput/TouchDateTimeField.tsx +2 -11
  327. package/src/Dialog/Dialog.tsx +15 -1
  328. package/src/Dialog/useImperativeDialog.doc.mjs +7 -4
  329. package/src/Drawer/Drawer.tsx +15 -0
  330. package/src/DropdownMenu/DropdownMenu.doc.mjs +1 -1
  331. package/src/DropdownMenu/DropdownMenuCheckboxItem.doc.mjs +3 -3
  332. package/src/DropdownMenu/DropdownMenuItem.doc.mjs +3 -3
  333. package/src/DropdownMenu/DropdownMenuRadioItem.doc.mjs +3 -3
  334. package/src/DropdownMenu/DropdownMenuSubMenu.doc.mjs +2 -2
  335. package/src/Field/Field.doc.mjs +1 -1
  336. package/src/Field/Field.tsx +2 -1
  337. package/src/Field/FieldLabel.doc.mjs +3 -3
  338. package/src/Field/FieldLabel.tsx +11 -3
  339. package/src/Field/InputClearButton.tsx +27 -0
  340. package/src/Field/inputStyles.stylex.ts +53 -6
  341. package/src/FieldStatus/FieldStatus.tsx +7 -2
  342. package/src/FileInput/FileInput.tsx +12 -2
  343. package/src/Icon/Icon.doc.mjs +17 -14
  344. package/src/Icon/useIcon.doc.mjs +6 -6
  345. package/src/IconButton/IconButton.doc.mjs +2 -2
  346. package/src/InfoTip/InfoTip.doc.mjs +1 -1
  347. package/src/InputGroup/InputGroup.tsx +5 -12
  348. package/src/InputGroup/InputGroupText.tsx +2 -3
  349. package/src/Item/Item.tsx +10 -1
  350. package/src/Kbd/Kbd.doc.mjs +5 -5
  351. package/src/Kbd/Kbd.tsx +3 -2
  352. package/src/Layer/gestureCounter.ts +39 -3
  353. package/src/Layer/inputHold.ts +241 -18
  354. package/src/Layer/layerHost.ts +3 -2
  355. package/src/Layer/outsidePanClaim.ts +50 -8
  356. package/src/Layer/outsidePress.ts +18 -4
  357. package/src/Layer/useLayer.tsx +128 -76
  358. package/src/Lightbox/Lightbox.tsx +15 -0
  359. package/src/MetadataList/MetadataList.tsx +8 -0
  360. package/src/MoreMenu/MoreMenu.doc.mjs +1 -1
  361. package/src/MultiSelector/MultiSelector.doc.mjs +2 -2
  362. package/src/MultiSelector/MultiSelector.tsx +45 -15
  363. package/src/NavIcon/NavIcon.doc.mjs +19 -10
  364. package/src/NavIcon/NavIcon.tsx +3 -2
  365. package/src/NavMenu/NavHeadingMenuItem.tsx +9 -3
  366. package/src/NumberInput/NumberInput.doc.mjs +12 -12
  367. package/src/NumberInput/NumberInput.tsx +12 -15
  368. package/src/NumberInput/numberParser.ts +15 -2
  369. package/src/Overlay/Overlay.doc.mjs +13 -1
  370. package/src/Overlay/OverlayScrim.tsx +45 -15
  371. package/src/Overlay/useOverlay.doc.mjs +7 -7
  372. package/src/Overlay/useOverlay.tsx +1 -1
  373. package/src/Pagination/Pagination.doc.mjs +9 -6
  374. package/src/Pagination/Pagination.tsx +15 -3
  375. package/src/Popover/usePopover.doc.mjs +14 -4
  376. package/src/Popover/usePopover.tsx +180 -23
  377. package/src/PowerSearch/PowerSearch.doc.mjs +165 -15
  378. package/src/PowerSearch/PowerSearch.tsx +6 -8
  379. package/src/PowerSearch/PowerSearchBottomSheet.tsx +182 -223
  380. package/src/PowerSearch/PowerSearchValueEditor.tsx +1 -51
  381. package/src/PowerSearch/formatFilterValue.ts +8 -9
  382. package/src/PowerSearch/formatTokenValue.ts +24 -42
  383. package/src/PowerSearch/relativeDateOptions.ts +84 -0
  384. package/src/ProgressBar/ProgressBar.doc.mjs +24 -5
  385. package/src/ProgressBar/ProgressBar.tsx +28 -18
  386. package/src/Resizable/createResizable.ts +46 -5
  387. package/src/SegmentedControl/SegmentedControlItem.tsx +4 -3
  388. package/src/SelectableCard/SelectableCard.tsx +8 -2
  389. package/src/Selector/Selector.doc.mjs +3 -3
  390. package/src/Selector/Selector.tsx +3 -1
  391. package/src/Selector/SelectorOption.doc.mjs +3 -3
  392. package/src/SideNav/SideNavItem.doc.mjs +6 -6
  393. package/src/SideNav/SideNavItem.tsx +3 -0
  394. package/src/Slider/Slider.tsx +48 -6
  395. package/src/Stepper/Step.tsx +7 -1
  396. package/src/Switch/Switch.doc.mjs +3 -3
  397. package/src/TabList/Tab.tsx +4 -0
  398. package/src/TabList/TabMenu.tsx +4 -0
  399. package/src/Table/BaseTable.tsx +35 -7
  400. package/src/Table/Table.doc.mjs +63 -6
  401. package/src/Table/Table.tsx +9 -4
  402. package/src/Table/TableCell.tsx +5 -2
  403. package/src/Table/TableHeaderCell.tsx +2 -8
  404. package/src/Table/TableRow.tsx +10 -1
  405. package/src/Table/plugins/rowExpansion/useTableRowExpansion.tsx +250 -53
  406. package/src/Table/plugins/rowIndex/useTableRowIndex.tsx +10 -1
  407. package/src/Table/plugins/rowStatus/useTableRowStatus.tsx +5 -0
  408. package/src/Table/plugins/sortable/useTableSortable.tsx +41 -2
  409. package/src/Table/plugins/stickyColumns/useTableStickyColumns.tsx +49 -20
  410. package/src/Table/tableColumnAdmission.ts +23 -0
  411. package/src/Table/types.ts +5 -5
  412. package/src/Table/useTableRowsVirtualization.doc.mjs +2 -2
  413. package/src/Text/Text.tsx +21 -7
  414. package/src/Text/TruncationTooltip.tsx +51 -184
  415. package/src/Text/cutText.tsx +126 -0
  416. package/src/Text/cutTextOnKeyboardFocus.tsx +158 -0
  417. package/src/Text/textClampContext.ts +14 -4
  418. package/src/Text/useTruncation.ts +85 -27
  419. package/src/TextArea/TextArea.doc.mjs +4 -4
  420. package/src/TextInput/TextInput.doc.mjs +3 -3
  421. package/src/Thumbnail/Thumbnail.tsx +1 -1
  422. package/src/TimeInput/TimeInput.tsx +3 -9
  423. package/src/TimeInput/TouchTimeField.tsx +6 -4
  424. package/src/Timestamp/Timestamp.tsx +29 -21
  425. package/src/Timestamp/TimestampHoverCard.tsx +44 -31
  426. package/src/Timestamp/formatInstant.ts +7 -3
  427. package/src/Timestamp/tooltipEntries.ts +4 -2
  428. package/src/Toast/Toast.doc.mjs +24 -37
  429. package/src/Toast/Toast.tsx +77 -29
  430. package/src/Toast/ToastViewport.tsx +33 -13
  431. package/src/Toast/useToast.tsx +42 -15
  432. package/src/Tokenizer/Tokenizer.doc.mjs +2 -2
  433. package/src/Toolbar/Toolbar.tsx +20 -6
  434. package/src/Tooltip/useTooltip.tsx +2 -1
  435. package/src/TopNav/TopNavMegaMenu.doc.mjs +13 -0
  436. package/src/TopNav/TopNavMenu.doc.mjs +2 -0
  437. package/src/Typeahead/Typeahead.doc.mjs +2 -2
  438. package/src/hooks/useAnnounceRenderedText.ts +15 -2
  439. package/src/hooks/useFocusTrap.ts +27 -4
  440. package/src/hooks/useHotkeys.doc.mjs +54 -11
  441. package/src/hooks/useHotkeys.ts +72 -3
  442. package/src/hooks/useInputStatusIcon.tsx +2 -1
  443. package/src/hooks/useKeyboardHint.tsx +3 -2
  444. package/src/i18n/generated-english/DateRangeInput/DateRangeInput.generated.ts +2 -0
  445. package/src/i18n/generated-english/MultiSelector/MultiSelector.generated.ts +1 -0
  446. package/src/i18n/generated-english/PowerSearch/PowerSearchValueEditor.generated.ts +0 -6
  447. package/src/i18n/generated-english/PowerSearch/relativeDateOptions.generated.ts +18 -0
  448. package/src/i18n/generated-english/Slider/Slider.generated.ts +1 -0
  449. package/src/i18n/generated-english/Thumbnail/Thumbnail.generated.ts +1 -0
  450. package/src/i18n/generated-locales/en.generated.ts +5 -0
  451. package/src/i18n/generated-locales/pseudo.generated.ts +5 -0
  452. package/src/i18n/getLocaleDirection.ts +57 -7
  453. package/src/i18n/listFormat.ts +55 -0
  454. package/src/i18n/useListFormatter.ts +22 -13
  455. package/src/internal/module-loader.ts +15 -0
  456. package/src/motion/animatedLayout.ts +215 -28
  457. package/src/palette/color.ts +41 -1
  458. package/src/palette/derive-look.ts +1186 -0
  459. package/src/palette/editor-look.ts +480 -0
  460. package/src/palette/mapping.ts +129 -15
  461. package/src/theme/SyntaxTheme.doc.mjs +1 -1
  462. package/src/theme/tokens.ts +2 -1
  463. package/src/theme/useThemeName.doc.mjs +1 -1
  464. package/src/utils/characters.ts +11 -0
  465. package/src/utils/devWarning.ts +35 -0
  466. package/src/utils/focusReturn.ts +3 -11
  467. package/src/utils/hitArea.ts +87 -18
  468. package/src/utils/mergeProps.ts +57 -34
  469. package/src/utils/scrollView.ts +3 -2
  470. package/src/utils/textDirection.ts +65 -5
  471. package/src/utils/themeProps.ts +22 -3
  472. package/src/utils/touchHitArea.ts +3 -2
  473. package/src/virtualization/useInlineListWindow.ts +102 -11
@@ -0,0 +1,1186 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file The colors a palette derives beyond its own families: Core's
5
+ * category colors (Badge, Token and Card tints) and the chart colors.
6
+ * @input A generated palette, the theme's text and surface colors per mode.
7
+ * @output The seeds of the derived hues, and each derived token's stops.
8
+ * @position Beside the generator, so the theme editor and a later
9
+ * `defineTheme({palette})` derive the same colors from one function.
10
+ *
11
+ * Each hue grows from Core's own hue at the strength of the theme's accent
12
+ * (the palette's vibrancy then shapes it as it shapes every color). Each
13
+ * token starts at the stop the official neutral theme maps it to: a card
14
+ * tint pale enough that the theme's text and secondary text read on it,
15
+ * text on it and on the page at 4.5:1, icons at 3:1 (WCAG 2.2 SC 1.4.3,
16
+ * 1.4.11), and chart series at 3:1 on the page. Every set is written. A
17
+ * chart series drawn like an earlier one takes the nearest shade apart
18
+ * that still reads. A card tint drawn like an earlier one, or like the
19
+ * page, takes the nearest quieter tint apart, else the nearest stronger
20
+ * one. A status fill stays apart from the accent where a shade does. A
21
+ * status role whose text reads at no shade keeps the base theme's colors.
22
+ * A limit no shade meets is a note.
23
+ */
24
+
25
+ import {
26
+ compositeOver,
27
+ contrastRatio,
28
+ hexToRgbTriple as rgbOf,
29
+ rgbTripleToHex as hexOf,
30
+ hexToLinearRgb,
31
+ hexToOklch,
32
+ linearRgbToOklab,
33
+ maxOklchChroma,
34
+ oklchClampedHex,
35
+ ratioDown,
36
+ } from './color';
37
+ import {perceptualDelta} from './generator';
38
+
39
+ export {ratioDown};
40
+
41
+ type Mode = 'light' | 'dark';
42
+ /** A shade of a family: its stop and color. */
43
+
44
+ /**
45
+ * The stops a category fill may take, by mode: it is also a card's tint, so
46
+ * it stays pale in light and deep in dark.
47
+ */
48
+ const TINT_STOPS = {light: [70, 95], dark: [10, 35]} as const;
49
+ export interface Stop {
50
+ stop: number;
51
+ color: string;
52
+ family?: string;
53
+ alpha?: string;
54
+ }
55
+ /** A derived token: its family and its shade in each mode. */
56
+ export interface DerivedToken {
57
+ family: string;
58
+ light: Stop;
59
+ dark: Stop;
60
+ }
61
+ /**
62
+ * The category and chart colors a palette derives. Each set is always
63
+ * written when the palette has its hues.
64
+ */
65
+ export interface DerivedLook {
66
+ tokens: Record<string, DerivedToken>;
67
+ badges: 'derived' | 'none';
68
+ chart: 'derived' | 'none';
69
+ /** Each limit no shade meets: a color still drawn alike, or one under
70
+ * its contrast at its most readable shade. */
71
+ limits: string[];
72
+ /** Each color moved apart from an earlier one (a tint also from the page). */
73
+ moved: string[];
74
+ /** Whether a color is still drawn like an earlier one. */
75
+ alike: boolean;
76
+ /** Whether a color is under its contrast at its most readable shade. */
77
+ unread: boolean;
78
+ }
79
+
80
+ /** Core's category hues; gray is the neutral family's. */
81
+ export const CATEGORY_HUES = [
82
+ 'red',
83
+ 'orange',
84
+ 'yellow',
85
+ 'green',
86
+ 'teal',
87
+ 'cyan',
88
+ 'blue',
89
+ 'purple',
90
+ 'pink',
91
+ 'gray',
92
+ ] as const;
93
+
94
+ /** The chart's series hues, in Core's series order. */
95
+ export const CHART_SERIES = [
96
+ 'blue',
97
+ 'orange',
98
+ 'purple',
99
+ 'green',
100
+ 'pink',
101
+ 'cyan',
102
+ 'red',
103
+ 'teal',
104
+ 'brown',
105
+ 'indigo',
106
+ ] as const;
107
+
108
+ /** Each chart scale and the family it is drawn from. */
109
+ export const CHART_SCALES: Readonly<Record<string, string>> = {
110
+ blue: 'blue',
111
+ shamrock: 'green',
112
+ orange: 'orange',
113
+ pink: 'pink',
114
+ purple: 'purple',
115
+ red: 'red',
116
+ teal: 'teal',
117
+ yellow: 'yellow',
118
+ gray: 'neutral',
119
+ };
120
+
121
+ /** The stops of a scale's five steps, pale to deep. */
122
+ const SCALE_STOPS = [92, 75, 55, 35, 15];
123
+
124
+ /**
125
+ * The seed each derived hue grows from: the official neutral palette's
126
+ * category seeds (packages/themes/neutral/palette.config.json; a test keeps
127
+ * them equal), and Core's own chart colors for the two series no category
128
+ * has.
129
+ */
130
+ export const HUE_SEEDS: Readonly<Record<string, string>> = {
131
+ red: '#d62830',
132
+ orange: '#d57113',
133
+ yellow: '#f8c723',
134
+ green: '#358a3a',
135
+ teal: '#0c7365',
136
+ cyan: '#0c6f82',
137
+ blue: '#0074e2',
138
+ purple: '#980fb2',
139
+ pink: '#b10e69',
140
+ brown: '#965E03',
141
+ indigo: '#6F8AFF',
142
+ };
143
+
144
+ /**
145
+ * Where each category token starts, `[light, dark]`, as the official
146
+ * neutral theme maps it (a test keeps them equal).
147
+ */
148
+ export const CATEGORY_STOPS = {
149
+ background: [85, 25],
150
+ border: [80, 65],
151
+ icon: [25, 75],
152
+ text: [25, 80],
153
+ grayBackground: [90, 20],
154
+ grayBorder: [85, 15],
155
+ grayIcon: [30, 65],
156
+ grayText: [10, 85],
157
+ } as const;
158
+
159
+ /** WCAG 2.2 AA by role. */
160
+ const TEXT = 4.5;
161
+ const NON_TEXT = 3;
162
+ /** Two colors under this ΔE (×100) read as one. */
163
+ const ALIKE = 2;
164
+ /**
165
+ * How close a card tint can be to another tint, or to a page, before they
166
+ * read as one. Core's own red and orange tints sit about ΔE 2 apart.
167
+ */
168
+ const TINT_ALIKE = 1.5;
169
+ /** Below this chroma a seed is a gray. */
170
+ const GRAY_CHROMA = 0.02;
171
+
172
+ /** How strong a color is for its lightness and hue, 0 to 1. */
173
+ function strength(color: string): number {
174
+ const {L, C, H} = hexToOklch(color);
175
+ const most = maxOklchChroma(H, L);
176
+ return most > 0 ? Math.min(1, C / most) : 0;
177
+ }
178
+
179
+ /**
180
+ * How strong the look's colors are against Core's: the accent's strength
181
+ * over the median of Core's hues, in steps of 0.05 (so an accent of about
182
+ * the same strength leaves them as they are); 1 without a colored accent.
183
+ */
184
+ function strengthScale(accent: string | null | undefined): number {
185
+ const core = Object.values(HUE_SEEDS)
186
+ .map(strength)
187
+ .sort((a, b) => a - b);
188
+ const median = core[Math.floor(core.length / 2)];
189
+ return accent && hexToOklch(accent).C >= GRAY_CHROMA && median > 0
190
+ ? Math.round(
191
+ Math.min(1.2, Math.max(0.25, strength(accent) / median)) * 20,
192
+ ) / 20
193
+ : 1;
194
+ }
195
+
196
+ /**
197
+ * The seeds of the derived hues: Core's hues at the strength of the accent,
198
+ * in steps of 0.05 so an accent of about the same strength leaves them as
199
+ * they are, turned a little toward the accent's and a tinted neutral's hue
200
+ * (hueLean); Core's strength without a colored accent.
201
+ */
202
+ export function lookSeeds(
203
+ accent: string | null | undefined,
204
+ neutral?: string | null,
205
+ ): Record<string, string> {
206
+ const hues = Object.keys(HUE_SEEDS);
207
+ const scale = strengthScale(accent);
208
+ const lean = hueLean(accent, neutral);
209
+ return Object.fromEntries(
210
+ hues.map(h => {
211
+ const {L, C, H} = hexToOklch(HUE_SEEDS[h]);
212
+ return [h, oklchClampedHex(L, C * scale, lean(H))];
213
+ }),
214
+ );
215
+ }
216
+
217
+ /** The most a look turns a hue toward its accent, and toward its neutral. */
218
+ const LEAN_TO_ACCENT = 10;
219
+ const LEAN_TO_NEUTRAL = 6;
220
+
221
+ /**
222
+ * How the look turns each hue: a share of the way toward the accent's hue
223
+ * (at most 10°), and toward a tinted neutral's (at most 6°), so a slate
224
+ * theme's colors run cool and a warm gray's warm, and two looks with the
225
+ * same strength still differ. Rounded to whole degrees so a nearby color
226
+ * leaves the hues as they are.
227
+ */
228
+ function hueLean(
229
+ accent: string | null | undefined,
230
+ neutral: string | null | undefined,
231
+ ): (hue: number) => number {
232
+ const toward = (
233
+ c: string | null | undefined,
234
+ share: number,
235
+ most: number,
236
+ ): ((hue: number) => number) => {
237
+ if (!c) {
238
+ return () => 0;
239
+ }
240
+ const {C, H} = hexToOklch(c);
241
+ if (C < GRAY_CHROMA) {
242
+ return () => 0;
243
+ }
244
+ return hue => {
245
+ const gap = ((H - hue + 540) % 360) - 180;
246
+ return Math.max(-most, Math.min(most, gap * share));
247
+ };
248
+ };
249
+ const a = toward(accent, 0.15, LEAN_TO_ACCENT);
250
+ const n = toward(neutral, 0.1, LEAN_TO_NEUTRAL);
251
+ return hue => (Math.round(hue + a(hue) + n(hue)) + 360) % 360;
252
+ }
253
+
254
+ /**
255
+ * Where a look's card tints start, `[light, dark]`: a strong or dark
256
+ * accent draws them a step deeper, a pale accent a step paler; the
257
+ * official neutral theme's stops otherwise.
258
+ */
259
+ export function tintStops(
260
+ accent: string | null | undefined,
261
+ ): readonly [number, number] {
262
+ if (!accent || hexToOklch(accent).C < GRAY_CHROMA) {
263
+ return CATEGORY_STOPS.background;
264
+ }
265
+ const {L} = hexToOklch(accent);
266
+ if (strengthScale(accent) >= 1.1 || L < 0.4) {
267
+ return [80, 30];
268
+ }
269
+ if (L > 0.8) {
270
+ return [90, 20];
271
+ }
272
+ return CATEGORY_STOPS.background;
273
+ }
274
+
275
+ /**
276
+ * Whether a family is one lookSeeds grows: an id, a name and a seed at
277
+ * Core's own lightness and hue for that id (only its strength follows the
278
+ * look).
279
+ */
280
+ export function isLookFamily(family: unknown): boolean {
281
+ const f = family as {id?: unknown; seed?: unknown} | null | undefined;
282
+ const core = typeof f?.id === 'string' ? HUE_SEEDS[f.id] : undefined;
283
+ if (!core || typeof f?.seed !== 'string') {
284
+ return false;
285
+ }
286
+ if (!Object.keys(f).every(k => ['id', 'name', 'seed'].includes(k))) {
287
+ return false;
288
+ }
289
+ let a: {L: number; C: number; H: number};
290
+ try {
291
+ a = hexToOklch(f.seed);
292
+ } catch {
293
+ return false;
294
+ }
295
+ const b = hexToOklch(core);
296
+ const turn = Math.abs(((a.H - b.H + 540) % 360) - 180);
297
+ // A weak seed's hue moves a little when written as hex.
298
+ const slack =
299
+ 2 +
300
+ LEAN_TO_ACCENT +
301
+ LEAN_TO_NEUTRAL +
302
+ (Math.atan2(0.004, Math.max(a.C, 1e-6)) * 180) / Math.PI;
303
+ return Math.abs(a.L - b.L) < 0.02 && (turn < slack || a.C < GRAY_CHROMA);
304
+ }
305
+
306
+ /** What deriveLookColors reads. */
307
+ export interface DeriveLookInput {
308
+ /** Generated families by id. */
309
+ palette: Record<string, Record<Mode, Record<string | number, string>>>;
310
+ /** The palette's stops. */
311
+ stops: ReadonlyArray<number>;
312
+ modes: ReadonlyArray<Mode>;
313
+ /** The theme's text and secondary text colors (opaque hex) in a mode. */
314
+ text: (m: Mode) => string[];
315
+ /** The look's accent seed: how deep its card tints sit (tintStops). */
316
+ accent?: string | null;
317
+ /** The surfaces text sits on in a mode: page, surface, card (opaque hex). */
318
+ grounds: (m: Mode) => string[];
319
+ /** Whether a token is written (Core has it and the theme does not set it by hand). */
320
+ wanted?: (token: string) => boolean;
321
+ /** The modes the theme shows: notes name only these. Every mode by default. */
322
+ shown?: ReadonlyArray<Mode>;
323
+ }
324
+
325
+ /** The category and chart colors a palette derives. */
326
+ export function deriveLookColors({
327
+ palette,
328
+ accent = null,
329
+ stops,
330
+ modes,
331
+ text,
332
+ grounds,
333
+ wanted = () => true,
334
+ shown = modes,
335
+ }: DeriveLookInput): DerivedLook {
336
+ const tokens: Record<string, DerivedToken> = {};
337
+ const tint = tintStops(accent);
338
+ const limits: string[] = [];
339
+ const moved: string[] = [];
340
+ const out: DerivedLook = {
341
+ tokens,
342
+ badges: 'none',
343
+ chart: 'none',
344
+ limits,
345
+ moved,
346
+ alike: false,
347
+ unread: false,
348
+ };
349
+ /** A limit no shade meets in a mode the theme shows: one note, however many times it is met. */
350
+ const unmet = (line: string, kind: 'alike' | 'unread', m: Mode): void => {
351
+ if (!shown.includes(m)) {
352
+ return;
353
+ }
354
+ out[kind] = true;
355
+ if (!limits.includes(line)) {
356
+ limits.push(line);
357
+ }
358
+ };
359
+ const sorted = [...stops].sort((a, b) => a - b);
360
+ const near = (n: number): number =>
361
+ sorted.reduce(
362
+ (best, s) => (Math.abs(s - n) < Math.abs(best - n) ? s : best),
363
+ sorted[0],
364
+ );
365
+ const at = (id: string, m: Mode, stop: number): Stop => ({
366
+ stop,
367
+ color: palette[id][m][stop],
368
+ });
369
+ /** Stops by distance from `s0`. Ties go darker first in light and
370
+ * lighter first in dark. */
371
+ const outward = (m: Mode, s0: number): number[] =>
372
+ [...sorted].sort(
373
+ (a, b) =>
374
+ Math.abs(a - s0) - Math.abs(b - s0) || (m === 'light' ? a - b : b - a),
375
+ );
376
+ const lowest = (color: string, on: string[]): number =>
377
+ Math.min(...on.map(bg => contrastRatio(color, bg)));
378
+ /**
379
+ * The stop nearest `start` that reads at `needs` on every ground,
380
+ * searched both ways, or the most contrasting one when none does; and
381
+ * the ratio it reads at.
382
+ */
383
+ const nearest = (
384
+ id: string,
385
+ m: Mode,
386
+ start: number,
387
+ on: string[],
388
+ needs: number,
389
+ ): {stop: Stop; r: number} => {
390
+ const scored = outward(m, near(start)).map(stop => ({
391
+ stop,
392
+ r: lowest(at(id, m, stop).color, on),
393
+ }));
394
+ const best =
395
+ scored.find(x => x.r >= needs) ??
396
+ scored.reduce((x, y) => (y.r > x.r ? y : x));
397
+ return {stop: at(id, m, best.stop), r: best.r};
398
+ };
399
+ /** A color under its contrast at its most readable shade is a note. */
400
+ const noteShort = (
401
+ token: string,
402
+ m: Mode,
403
+ r: number,
404
+ needs: number,
405
+ ): void => {
406
+ if (r < needs) {
407
+ unmet(
408
+ `${token} reads at only ${ratioDown(r)}:1 in ${m} at its most readable shade (needs ${needs}:1)`,
409
+ 'unread',
410
+ m,
411
+ );
412
+ }
413
+ };
414
+ /** The nearest stop that reads, noted when none does. */
415
+ const pick = (
416
+ token: string,
417
+ id: string,
418
+ m: Mode,
419
+ start: number,
420
+ on: string[],
421
+ needs: number,
422
+ ): Stop => {
423
+ const {stop, r} = nearest(id, m, start, on, needs);
424
+ noteShort(token, m, r, needs);
425
+ return stop;
426
+ };
427
+ const each = (fn: (m: Mode) => Stop): Record<Mode, Stop> => {
428
+ const v = {} as Record<Mode, Stop>;
429
+ for (const m of ['light', 'dark'] as const) {
430
+ v[m] = fn(modes.includes(m) ? m : modes[0]);
431
+ }
432
+ return v;
433
+ };
434
+ const shift = (from: number, to: number): string =>
435
+ to < from ? 'deeper' : 'paler';
436
+
437
+ // Category colors: Badge and Token fill and text, Card tints. A hue is
438
+ // drawn alike when its fill and text are both under ΔE 2 from an earlier
439
+ // hue's, or its tint is under TINT_ALIKE from an earlier tint or a
440
+ // page. A Card shows only its tint behind the theme's text. Such a hue
441
+ // takes another tint that carries the text. A limit no tint meets
442
+ // becomes a note.
443
+ const hues = CATEGORY_HUES.filter(h => h !== 'gray' || palette.neutral);
444
+ const famOf = (h: string): string => (h === 'gray' ? 'neutral' : h);
445
+ if (hues.every(h => palette[famOf(h)])) {
446
+ const start = (
447
+ h: string,
448
+ role: 'background' | 'border' | 'icon' | 'text',
449
+ m: Mode,
450
+ ): number => {
451
+ const pair =
452
+ h === 'gray'
453
+ ? {
454
+ background: CATEGORY_STOPS.grayBackground,
455
+ border: CATEGORY_STOPS.grayBorder,
456
+ icon: CATEGORY_STOPS.grayIcon,
457
+ text: CATEGORY_STOPS.grayText,
458
+ }[role]
459
+ : CATEGORY_STOPS[role];
460
+ return pair[m === 'light' ? 0 : 1];
461
+ };
462
+ interface Badge {
463
+ fill: Stop;
464
+ ink: {stop: Stop; r: number};
465
+ icon: {stop: Stop; r: number};
466
+ }
467
+ const badgeOn = (h: string, m: Mode, fillStop: number): Badge => {
468
+ const id = famOf(h);
469
+ const fill = at(id, m, fillStop);
470
+ const on = [fill.color, ...grounds(m)];
471
+ return {
472
+ fill,
473
+ ink: nearest(id, m, start(h, 'text', m), on, TEXT),
474
+ icon: nearest(id, m, start(h, 'icon', m), on, NON_TEXT),
475
+ };
476
+ };
477
+ const drawn: Record<
478
+ string,
479
+ Partial<Record<Mode, Badge>>
480
+ > = Object.fromEntries(hues.map(h => [h, {}]));
481
+ for (const m of modes) {
482
+ const placed: {hue: string; fill: string; ink: string}[] = [];
483
+ for (const h of hues) {
484
+ const id = famOf(h);
485
+ const s0 = near(
486
+ h === 'gray'
487
+ ? start(h, 'background', m)
488
+ : tint[m === 'light' ? 0 : 1],
489
+ );
490
+ // Paler first in light, darker first in dark, inside the tint's
491
+ // stops: a tint, never a solid fill, since it is also the card's.
492
+ const [lo, hi] = TINT_STOPS[m];
493
+ const carrying = [...sorted]
494
+ .filter(x => x >= lo && x <= hi)
495
+ .sort(
496
+ (a, b) =>
497
+ Math.abs(a - s0) - Math.abs(b - s0) ||
498
+ (m === 'light' ? b - a : a - b),
499
+ )
500
+ .filter(x =>
501
+ text(m).every(c => contrastRatio(c, at(id, m, x).color) >= TEXT),
502
+ );
503
+ if (!carrying.length) {
504
+ const worst = Math.min(
505
+ ...text(m).map(c => contrastRatio(c, at(id, m, s0).color)),
506
+ );
507
+ unmet(
508
+ `--color-background-${h} carries the theme's text at only ${ratioDown(worst)}:1 in ${m} (needs ${TEXT}:1)`,
509
+ 'unread',
510
+ m,
511
+ );
512
+ }
513
+ const like = (b: Badge): {hue: string} | undefined =>
514
+ placed.find(p => {
515
+ const fill = perceptualDelta(p.fill, b.fill.color);
516
+ return (
517
+ fill < TINT_ALIKE ||
518
+ (fill < ALIKE && perceptualDelta(p.ink, b.ink.stop.color) < ALIKE)
519
+ );
520
+ }) ??
521
+ (grounds(m).some(g => perceptualDelta(g, b.fill.color) < TINT_ALIKE)
522
+ ? {hue: 'the page'}
523
+ : undefined);
524
+ let badge = badgeOn(h, m, carrying[0] ?? s0);
525
+ const twin = like(badge);
526
+ if (twin !== undefined) {
527
+ let apart: Badge | undefined;
528
+ // Paler first in light, darker first in dark: a moved tint stays
529
+ // as quiet as the look; a stronger one only when no quieter one
530
+ // stands apart.
531
+ const first = carrying[0] ?? s0;
532
+ const quieter = (x: number) =>
533
+ m === 'light' ? x > first : x < first;
534
+ const tries = [
535
+ ...carrying.filter(quieter),
536
+ ...carrying.slice(1).filter(x => !quieter(x)),
537
+ ];
538
+ for (const s of tries) {
539
+ const b = badgeOn(h, m, s);
540
+ if (like(b) === undefined) {
541
+ apart = b;
542
+ break;
543
+ }
544
+ }
545
+ if (apart === undefined) {
546
+ unmet(
547
+ `--color-background-${h} is drawn like ${twin.hue} in ${m}: no tint of it that carries the text stands apart`,
548
+ 'alike',
549
+ m,
550
+ );
551
+ } else {
552
+ if (shown.includes(m))
553
+ moved.push(
554
+ `--color-background-${h} takes a ${shift(badge.fill.stop, apart.fill.stop)} tint in ${m}, apart from ${twin.hue}`,
555
+ );
556
+ badge = apart;
557
+ }
558
+ }
559
+ noteShort(`--color-text-${h}`, m, badge.ink.r, TEXT);
560
+ noteShort(`--color-icon-${h}`, m, badge.icon.r, NON_TEXT);
561
+ placed.push({
562
+ hue: h,
563
+ fill: badge.fill.color,
564
+ ink: badge.ink.stop.color,
565
+ });
566
+ drawn[h][m] = badge;
567
+ }
568
+ }
569
+ for (const h of hues) {
570
+ const id = famOf(h);
571
+ const of = (m: Mode): Badge => drawn[h][m] as Badge;
572
+ const set: Record<string, Record<Mode, Stop>> = {
573
+ [`--color-background-${h}`]: each(m => of(m).fill),
574
+ [`--color-text-${h}`]: each(m => of(m).ink.stop),
575
+ [`--color-icon-${h}`]: each(m => of(m).icon.stop),
576
+ [`--color-border-${h}`]: each(m =>
577
+ at(id, m, near(start(h, 'border', m))),
578
+ ),
579
+ };
580
+ for (const [k, v] of Object.entries(set)) {
581
+ if (wanted(k)) {
582
+ tokens[k] = {family: id, ...v};
583
+ }
584
+ }
585
+ }
586
+ out.badges = 'derived';
587
+ }
588
+
589
+ // Chart colors: series at 3:1 on the page (graphics), a gray, and the
590
+ // five-step scales. A series drawn like an earlier one takes the nearest
591
+ // shade that reads at 3:1 and stands apart from every earlier series. A
592
+ // limit the palette cannot meet becomes a note.
593
+ if (CHART_SERIES.every(h => palette[h])) {
594
+ const chart: Record<string, DerivedToken> = {};
595
+ const picked: Record<string, Record<Mode, Stop>> = {};
596
+ for (const h of CHART_SERIES) {
597
+ picked[h] = each(m =>
598
+ pick(
599
+ `--color-data-categorical-${h}`,
600
+ h,
601
+ m,
602
+ m === 'light' ? 50 : 60,
603
+ grounds(m),
604
+ NON_TEXT,
605
+ ),
606
+ );
607
+ }
608
+ for (const m of modes) {
609
+ const placed: {hue: string; color: string}[] = [];
610
+ for (const h of CHART_SERIES) {
611
+ const own = picked[h][m];
612
+ const like = placed.find(
613
+ p => perceptualDelta(p.color, own.color) < ALIKE,
614
+ );
615
+ if (like !== undefined) {
616
+ const apart = outward(m, own.stop).find(stop => {
617
+ const c = at(h, m, stop).color;
618
+ return (
619
+ lowest(c, grounds(m)) >= NON_TEXT &&
620
+ placed.every(p => perceptualDelta(p.color, c) >= ALIKE)
621
+ );
622
+ });
623
+ if (apart === undefined) {
624
+ unmet(
625
+ `--color-data-categorical-${h} is drawn like ${like.hue} in ${m}: no shade of it that reads at ${NON_TEXT}:1 stands apart`,
626
+ 'alike',
627
+ m,
628
+ );
629
+ } else {
630
+ picked[h][m] = at(h, m, apart);
631
+ if (shown.includes(m))
632
+ moved.push(
633
+ `--color-data-categorical-${h} takes a ${shift(own.stop, apart)} shade in ${m}, apart from ${like.hue}`,
634
+ );
635
+ }
636
+ }
637
+ placed.push({hue: h, color: picked[h][m].color});
638
+ }
639
+ }
640
+ for (const h of CHART_SERIES) {
641
+ // A one-mode theme writes its mode on both sides.
642
+ const refs = each(m => picked[h][m]);
643
+ chart[`--color-data-categorical-${h}`] = {family: h, ...refs};
644
+ }
645
+ if (palette.neutral) {
646
+ chart['--color-data-neutral'] = {
647
+ family: 'neutral',
648
+ ...each(m =>
649
+ pick('--color-data-neutral', 'neutral', m, 55, grounds(m), NON_TEXT),
650
+ ),
651
+ };
652
+ }
653
+ for (const [scale, id] of Object.entries(CHART_SCALES)) {
654
+ if (palette[id]) {
655
+ SCALE_STOPS.forEach((stop, i) => {
656
+ chart[`--color-data-${scale}-${i + 1}`] = {
657
+ family: id,
658
+ ...each(m => at(id, m, near(stop))),
659
+ };
660
+ });
661
+ }
662
+ }
663
+ for (const [k, v] of Object.entries(chart)) {
664
+ if (wanted(k)) {
665
+ tokens[k] = v;
666
+ }
667
+ }
668
+ out.chart = 'derived';
669
+ }
670
+ return out;
671
+ }
672
+
673
+ /**
674
+ * The status roles a palette derives for a look that leaves them unset: each
675
+ * role's dial id and the word its tokens carry.
676
+ */
677
+ export const STATUS_ROLES: Readonly<Record<string, string>> = {
678
+ info: 'info',
679
+ success: 'success',
680
+ warning: 'warning',
681
+ danger: 'error',
682
+ };
683
+
684
+ /**
685
+ * A status fill under this ΔE (×100, an OKLab distance of 0.1) from the
686
+ * accent's reads as the accent at a glance, as two button fills do.
687
+ */
688
+ const APART_FROM_ACCENT = 10;
689
+
690
+ /**
691
+ * The seeds of the status roles a look derives: each role's own hue and
692
+ * lightness (the base theme's color for it) at the accent's strength. A
693
+ * role keeps its hue whatever the accent: an error stays the base's red,
694
+ * info its blue. Only its strength follows the look; a role that would
695
+ * then read as the accent takes the nearest shade apart from it
696
+ * (deriveStatusColors).
697
+ * `base` holds each role's base color by dial id (info, success, warning,
698
+ * danger).
699
+ */
700
+ export function statusSeeds(
701
+ base: Record<string, string>,
702
+ accent: string | null | undefined,
703
+ ): Record<string, string> {
704
+ const scale = strengthScale(accent);
705
+ const out: Record<string, string> = {};
706
+ for (const [id, color] of Object.entries(base)) {
707
+ const {L, C, H} = hexToOklch(color);
708
+ out[id] = oklchClampedHex(L, C * scale, H);
709
+ }
710
+ return out;
711
+ }
712
+
713
+ /**
714
+ * Where each status token starts, `[light, dark]`: the fill at its middle,
715
+ * the text, tint and text on the tint where the official neutral theme maps
716
+ * them, icons and borders at the fill.
717
+ */
718
+ export const STATUS_STOPS = {
719
+ fill: [50, 70],
720
+ warningFill: [85, 85],
721
+ text: [45, 80],
722
+ muted: [85, 75],
723
+ /** The dark tint is see-through, as the official neutral theme's is. */
724
+ mutedDarkAlpha: '3D',
725
+ onMuted: [25, 80],
726
+ } as const;
727
+
728
+ /** What deriveStatusColors reads. */
729
+ export interface DeriveStatusInput {
730
+ /** Generated families by id; a role's family has the role's dial id. */
731
+ palette: Record<string, Record<Mode, Record<string | number, string>>>;
732
+ stops: ReadonlyArray<number>;
733
+ modes: ReadonlyArray<Mode>;
734
+ /** The dial ids to derive (info, success, warning, danger). */
735
+ roles: ReadonlyArray<string>;
736
+ /** Page, surface, card (opaque hex). */
737
+ grounds: (m: Mode) => string[];
738
+ /** The text the theme draws on a role's fill in a mode, preferred first (white or near-black). */
739
+ inks: (m: Mode, role: string) => (Stop & {family: string})[];
740
+ /** The accent fill in a mode, kept apart from. */
741
+ accent: (m: Mode) => string | null;
742
+ wanted?: (token: string) => boolean;
743
+ /** The modes the theme shows: notes name only these. Every mode by default. */
744
+ shown?: ReadonlyArray<Mode>;
745
+ }
746
+
747
+ /** The status colors a palette derives, and each role's state. A token's stop may name its own family (the text on a fill is the neutral color's, or white or black). */
748
+ export interface DerivedStatus {
749
+ tokens: Record<string, DerivedToken>;
750
+ /** The roles derived and written. */
751
+ roles: Record<string, 'derived'>;
752
+ /** What the roles gave up to stay written (a fill that cannot meet every
753
+ * limit), and each role that keeps the base theme's colors because its
754
+ * text, icon or border reads at no shade. */
755
+ why: string[];
756
+ /** The roles `why` names, by dial id. */
757
+ short: string[];
758
+ }
759
+
760
+ /** The status colors a palette derives for the roles a theme leaves unset. */
761
+ export function deriveStatusColors({
762
+ palette,
763
+ stops,
764
+ modes,
765
+ roles,
766
+ grounds,
767
+ inks,
768
+ accent,
769
+ wanted = () => true,
770
+ shown = modes,
771
+ }: DeriveStatusInput): DerivedStatus {
772
+ const tokens: Record<string, DerivedToken> = {};
773
+ const state: Record<string, 'derived'> = {};
774
+ const why: string[] = [];
775
+ const short: string[] = [];
776
+ const sorted = [...stops].filter(s => s > 0 && s < 100).sort((a, b) => a - b);
777
+ const near = (n: number) =>
778
+ sorted.reduce(
779
+ (best, s) => (Math.abs(s - n) < Math.abs(best - n) ? s : best),
780
+ sorted[0],
781
+ );
782
+ const each = (fn: (m: Mode) => Stop) => {
783
+ const v = {} as Record<Mode, Stop>;
784
+ for (const m of ['light', 'dark'] as Mode[])
785
+ v[m] = fn(modes.includes(m) ? m : modes[0]);
786
+ return v;
787
+ };
788
+ for (const id of roles) {
789
+ const w = STATUS_ROLES[id];
790
+ const fam = palette[id];
791
+ if (!w || !fam) continue;
792
+ const at = (m: Mode, stop: number) => ({stop, color: fam[m][stop]});
793
+ const fails: string[] = [];
794
+ // Text or an icon that reads at no shade: the role keeps the base
795
+ // theme's colors, which read.
796
+ let unread = false;
797
+ /** A note for a mode the theme shows. */
798
+ const fail = (m: Mode, line: string, text = false): void => {
799
+ if (!shown.includes(m)) return;
800
+ fails.push(line);
801
+ if (text) unread = true;
802
+ };
803
+ /**
804
+ * The stop nearest `start` that reads at `needs` on every color in `bg`.
805
+ */
806
+ const pick = (
807
+ token: string,
808
+ m: Mode,
809
+ start: number,
810
+ bg: string[],
811
+ needs: number,
812
+ ): Stop => {
813
+ const s0 = near(start);
814
+ const order = [...sorted].sort(
815
+ (a, b) =>
816
+ Math.abs(a - s0) - Math.abs(b - s0) ||
817
+ (m === 'light' ? a - b : b - a),
818
+ );
819
+ const scored = order.map(stop => ({
820
+ stop,
821
+ r: Math.min(...bg.map(c => contrastRatio(at(m, stop).color, c))),
822
+ }));
823
+ const best =
824
+ scored.find(x => x.r >= needs) ??
825
+ scored.reduce((x, y) => (y.r > x.r ? y : x));
826
+ if (best.r < needs)
827
+ fail(
828
+ m,
829
+ `${token} reads at only ${ratioDown(best.r)}:1 in ${m} at its most readable shade (needs ${needs}:1)`,
830
+ true,
831
+ );
832
+ return at(m, best.stop);
833
+ };
834
+ // The fill: nearest its middle that stands out from the page at 3:1 and
835
+ // carries white or near-black text at 4.5:1 (near-black first on
836
+ // warning, white first otherwise).
837
+ const onFill = {} as Record<Mode, Stop & {family: string}>;
838
+ // Warning is a bright fill that carries near-black text, as Core's is:
839
+ // its text, not its edge, makes it read, so it is not held to 3:1 on
840
+ // the page.
841
+ const bright = id === 'warning';
842
+ const fill = each(m => {
843
+ const s0 = near(
844
+ (bright ? STATUS_STOPS.warningFill : STATUS_STOPS.fill)[
845
+ m === 'light' ? 0 : 1
846
+ ],
847
+ );
848
+ const order = [...sorted].sort(
849
+ (a, b) => Math.abs(a - s0) - Math.abs(b - s0) || b - a,
850
+ );
851
+ const choices = inks(m, id);
852
+ const a = accent(m);
853
+ const standsOut = (c: string) =>
854
+ bright ||
855
+ Math.min(...grounds(m).map(g => contrastRatio(c, g))) >= NON_TEXT;
856
+ const apart = (c: string) =>
857
+ !a || perceptualDelta(c, a) >= APART_FROM_ACCENT;
858
+ // (A bright fill already starts at the palest stop that reads as a
859
+ // fill on a light page: one kept apart from the accent moves deeper,
860
+ // never paler toward the page.)
861
+ const allowed = order.filter(s => !(bright && m === 'light' && s > s0));
862
+ /** The fill at a stop, with a note for each limit it gives up. */
863
+ const take = (s: number, ink: Stop & {family: string}): Stop => {
864
+ const c = at(m, s).color;
865
+ if (!standsOut(c))
866
+ fail(
867
+ m,
868
+ `--color-${w} stands out from the page at under ${NON_TEXT}:1 in ${m}`,
869
+ );
870
+ if (!apart(c))
871
+ fail(m, `--color-${w} is drawn like your accent in ${m}`);
872
+ const r = contrastRatio(ink.color, c);
873
+ if (r < TEXT)
874
+ fail(
875
+ m,
876
+ `--color-on-${w} reads at only ${ratioDown(r)}:1 on --color-${w} in ${m} at its most readable shade (needs ${TEXT}:1)`,
877
+ true,
878
+ );
879
+ onFill[m] = ink;
880
+ return at(m, s);
881
+ };
882
+ // The limits, dropped one at a time when no stop meets them all:
883
+ // standing out from the page first, then standing apart from the
884
+ // accent. Text that reads on no stop keeps the role at the base
885
+ // theme's colors.
886
+ for (const keep of [[standsOut, apart], [apart], []])
887
+ // The role's usual text first: the nearest stop it reads on, before
888
+ // the other text on a nearer stop.
889
+ for (const ink of choices)
890
+ for (const s of allowed) {
891
+ const c = at(m, s).color;
892
+ if (keep.every(ok => ok(c)) && contrastRatio(ink.color, c) >= TEXT)
893
+ return take(s, ink);
894
+ }
895
+ // No stop carries readable text: the one that carries it best.
896
+ const best = allowed.reduce((x, y) =>
897
+ contrastRatio(choices[0].color, at(m, y).color) >
898
+ contrastRatio(choices[0].color, at(m, x).color)
899
+ ? y
900
+ : x,
901
+ );
902
+ return take(best, choices[0]);
903
+ });
904
+ const muted = each(m => {
905
+ let stop = near(STATUS_STOPS.muted[m === 'light' ? 0 : 1]);
906
+ // (A light tint is paler than its fill: where the fill already sits
907
+ // at the tint's stop or paler, a bright warning or one an accent made
908
+ // pale, the tint takes the next paler stop that is another color.)
909
+ if (m === 'light') {
910
+ const paler = sorted.filter(
911
+ s => s > fill[m].stop && at(m, s).color !== fill[m].color,
912
+ );
913
+ if (stop <= fill[m].stop || at(m, stop).color === fill[m].color)
914
+ stop = paler[0] ?? stop;
915
+ }
916
+ const solid = at(m, stop);
917
+ return m === 'dark'
918
+ ? {
919
+ stop,
920
+ color: `${solid.color}${STATUS_STOPS.mutedDarkAlpha.toLowerCase()}`,
921
+ alpha: STATUS_STOPS.mutedDarkAlpha.toLowerCase(),
922
+ }
923
+ : solid;
924
+ });
925
+ /** The tint as it shows over each surface. */
926
+ const tintSeen = (m: Mode) =>
927
+ muted[m].alpha
928
+ ? grounds(m).map(g =>
929
+ hexOf(
930
+ compositeOver(
931
+ rgbOf(muted[m].color.slice(0, 7)),
932
+ parseInt(muted[m].alpha ?? 'ff', 16) / 255,
933
+ rgbOf(g),
934
+ ),
935
+ ),
936
+ )
937
+ : [muted[m].color];
938
+ const set = {
939
+ [`--color-${w}`]: fill,
940
+ [`--color-${w}-muted`]: muted,
941
+ [`--color-text-${w}`]: each(m =>
942
+ pick(
943
+ `--color-text-${w}`,
944
+ m,
945
+ STATUS_STOPS.text[m === 'light' ? 0 : 1],
946
+ [...grounds(m), ...tintSeen(m)],
947
+ TEXT,
948
+ ),
949
+ ),
950
+ [`--color-on-${w}-muted`]: each(m =>
951
+ pick(
952
+ `--color-on-${w}-muted`,
953
+ m,
954
+ STATUS_STOPS.onMuted[m === 'light' ? 0 : 1],
955
+ tintSeen(m),
956
+ TEXT,
957
+ ),
958
+ ),
959
+ [`--color-icon-${w}`]: each(m =>
960
+ pick(`--color-icon-${w}`, m, fill[m].stop, grounds(m), NON_TEXT),
961
+ ),
962
+ [`--color-border-${w}`]: each(m =>
963
+ pick(`--color-border-${w}`, m, fill[m].stop, grounds(m), NON_TEXT),
964
+ ),
965
+ };
966
+ if (unread) {
967
+ why.push(
968
+ `${id} keeps the base theme's colors: no shade of its family reads`,
969
+ );
970
+ short.push(id);
971
+ continue;
972
+ }
973
+ // A one-mode theme measures its mode for both sides: one note each.
974
+ for (const line of fails) if (!why.includes(line)) why.push(line);
975
+ if (fails.length) short.push(id);
976
+ state[id] = 'derived';
977
+ for (const [k, v] of Object.entries(set))
978
+ if (wanted(k)) tokens[k] = {family: id, ...v};
979
+ const inkToken = `--color-on-${w}`;
980
+ if (wanted(inkToken))
981
+ tokens[inkToken] = {
982
+ family: onFill.light.family,
983
+ light: onFill.light,
984
+ dark: onFill.dark,
985
+ };
986
+ }
987
+ return {tokens, roles: state, why, short};
988
+ }
989
+
990
+ /**
991
+ * Code colors, `[family, light stop, dark stop]` by syntax role, as the
992
+ * official neutral theme maps them. The gray roles read the neutral.
993
+ */
994
+ export const SYNTAX_STOPS = {
995
+ keyword: ['purple', 30, 80],
996
+ string: ['green', 30, 80],
997
+ comment: ['neutral', 35, 75],
998
+ number: ['orange', 30, 80],
999
+ function: ['blue', 30, 80],
1000
+ type: ['purple', 30, 80],
1001
+ variable: ['neutral', 5, 90],
1002
+ operator: ['neutral', 35, 75],
1003
+ constant: ['orange', 30, 80],
1004
+ tag: ['red', 30, 80],
1005
+ attribute: ['yellow', 30, 80],
1006
+ property: ['teal', 30, 80],
1007
+ punctuation: ['neutral', 35, 75],
1008
+ } as const;
1009
+
1010
+ /** The code background and the overlay scrim, from the neutral. */
1011
+ const CODE_GROUND = {light: 100, dark: 5} as const;
1012
+ const SCRIM = {
1013
+ light: {stop: 10, alpha: '80'},
1014
+ dark: {stop: 5, alpha: 'cc'},
1015
+ } as const;
1016
+
1017
+ /** What deriveCodeColors reads. */
1018
+ export interface DeriveCodeInput {
1019
+ palette: Record<string, Record<Mode, Record<string | number, string>>>;
1020
+ stops: ReadonlyArray<number>;
1021
+ modes: ReadonlyArray<Mode>;
1022
+ /** The page colors code sits on, opaque hex. */
1023
+ grounds: (m: Mode) => string[];
1024
+ /** The code background the theme sets itself in a mode (opaque hex), which code colors read on instead of the neutral's. */
1025
+ codeGround?: (m: Mode) => string | null;
1026
+ wanted?: (token: string) => boolean;
1027
+ }
1028
+
1029
+ /**
1030
+ * The code colors and the overlay scrim a palette derives: each syntax role
1031
+ * from its hue's family, the nearest stop that reads at 4.5:1 on the code
1032
+ * background and on the page; the code background and the scrim from the
1033
+ * neutral. A role that cannot read keeps the base theme's.
1034
+ */
1035
+ export function deriveCodeColors({
1036
+ palette,
1037
+ stops,
1038
+ modes,
1039
+ grounds,
1040
+ codeGround = () => null,
1041
+ wanted = () => true,
1042
+ }: DeriveCodeInput): {
1043
+ tokens: Record<string, DerivedToken>;
1044
+ unreadable: string[];
1045
+ } {
1046
+ const tokens: Record<string, DerivedToken> = {};
1047
+ const unreadable: string[] = [];
1048
+ const sorted = [...stops].sort((a, b) => a - b);
1049
+ const near = (n: number) =>
1050
+ sorted.reduce((x, y) => (Math.abs(y - n) < Math.abs(x - n) ? y : x));
1051
+ const at = (id: string, m: Mode, stop: number): Stop => ({
1052
+ stop,
1053
+ color: palette[id][m][stop],
1054
+ });
1055
+ const ground: Partial<Record<Mode, Stop>> = {};
1056
+ if (palette.neutral) {
1057
+ const bg = {} as Record<Mode, Stop>;
1058
+ for (const m of modes) {
1059
+ bg[m] = at('neutral', m, near(CODE_GROUND[m]));
1060
+ ground[m] = bg[m];
1061
+ }
1062
+ if (wanted('--color-syntax-background')) {
1063
+ tokens['--color-syntax-background'] = {family: 'neutral', ...bg};
1064
+ }
1065
+ const scrim = {} as Record<Mode, Stop>;
1066
+ for (const m of modes) {
1067
+ const s = near(SCRIM[m].stop);
1068
+ const c = palette.neutral[m][s];
1069
+ scrim[m] = {
1070
+ stop: s,
1071
+ color: `${c}${SCRIM[m].alpha}`,
1072
+ alpha: SCRIM[m].alpha,
1073
+ };
1074
+ }
1075
+ if (wanted('--color-overlay')) {
1076
+ tokens['--color-overlay'] = {family: 'neutral', ...scrim};
1077
+ }
1078
+ }
1079
+ for (const [role, [id, light, dark]] of Object.entries(SYNTAX_STOPS)) {
1080
+ const k = `--color-syntax-${role}`;
1081
+ if (!palette[id] || !wanted(k)) {
1082
+ continue;
1083
+ }
1084
+ const refs = {} as Record<Mode, Stop>;
1085
+ let ok = true;
1086
+ for (const m of modes) {
1087
+ // A code background the theme sets itself is the one code shows on.
1088
+ const own = codeGround(m);
1089
+ const on = [
1090
+ ...(own ? [own] : ground[m] ? [ground[m].color] : []),
1091
+ ...grounds(m),
1092
+ ];
1093
+ const s0 = near(m === 'light' ? light : dark);
1094
+ const order = [...sorted].sort(
1095
+ (a, b) =>
1096
+ Math.abs(a - s0) - Math.abs(b - s0) ||
1097
+ (m === 'light' ? a - b : b - a),
1098
+ );
1099
+ const s = order.find(x =>
1100
+ on.every(bg => contrastRatio(palette[id][m][x], bg) >= TEXT),
1101
+ );
1102
+ if (s == null) {
1103
+ ok = false;
1104
+ unreadable.push(`${k} (${m})`);
1105
+ break;
1106
+ }
1107
+ refs[m] = at(id, m, s);
1108
+ }
1109
+ if (ok) {
1110
+ tokens[k] = {family: id, ...refs};
1111
+ }
1112
+ }
1113
+ return {tokens, unreadable};
1114
+ }
1115
+
1116
+ /**
1117
+ * How full red-blind (protanopia) and green-blind (deuteranopia) vision maps
1118
+ * linear sRGB (Machado, Oliveira and Fernandes, 2009, severity 1). Anyone
1119
+ * red-weak or green-weak sees between these and normal vision.
1120
+ */
1121
+ const CVD: Record<
1122
+ 'red-weak' | 'green-weak',
1123
+ ReadonlyArray<ReadonlyArray<number>>
1124
+ > = {
1125
+ 'red-weak': [
1126
+ [0.152286, 1.052583, -0.204868],
1127
+ [0.114503, 0.786281, 0.099216],
1128
+ [-0.003882, -0.048116, 1.051998],
1129
+ ],
1130
+ 'green-weak': [
1131
+ [0.367322, 0.860646, -0.227968],
1132
+ [0.280085, 0.672501, 0.047413],
1133
+ [-0.01182, 0.04294, 0.968881],
1134
+ ],
1135
+ };
1136
+
1137
+ /** How far toward full red- or green-blindness the check looks. */
1138
+ const CVD_SEVERITY = 0.6;
1139
+
1140
+ /** Two fills closer than this (OKLab distance) read as one color. */
1141
+ const CVD_ALIKE = 0.06;
1142
+
1143
+ /**
1144
+ * The pairs of status fills that someone red-weak or green-weak may not
1145
+ * tell apart: their colors as that vision sees them sit closer than
1146
+ * CVD_ALIKE. `fills` holds opaque hex by role.
1147
+ */
1148
+ export function cvdClashes(
1149
+ fills: Record<string, string>,
1150
+ ): {a: string; b: string; vision: 'red-weak' | 'green-weak'}[] {
1151
+ const out: {a: string; b: string; vision: 'red-weak' | 'green-weak'}[] = [];
1152
+ const roles = Object.keys(fills).filter(r =>
1153
+ /^#[0-9a-f]{6}$/i.test(fills[r]),
1154
+ );
1155
+ for (const [vision, M] of Object.entries(CVD) as [
1156
+ 'red-weak' | 'green-weak',
1157
+ ReadonlyArray<ReadonlyArray<number>>,
1158
+ ][]) {
1159
+ // Weak, not absent: partway from normal vision to the full matrix.
1160
+ const W = M.map((row, r) =>
1161
+ row.map(
1162
+ (v, c) => (r === c ? 1 : 0) + CVD_SEVERITY * (v - (r === c ? 1 : 0)),
1163
+ ),
1164
+ );
1165
+ const seen = (hex: string) => {
1166
+ const lin = hexToLinearRgb(hex);
1167
+ const [r, g, b] = W.map(row =>
1168
+ Math.min(
1169
+ 1,
1170
+ Math.max(0, row[0] * lin[0] + row[1] * lin[1] + row[2] * lin[2]),
1171
+ ),
1172
+ );
1173
+ return linearRgbToOklab(r, g, b);
1174
+ };
1175
+ for (let i = 0; i < roles.length; i++) {
1176
+ for (let j = i + 1; j < roles.length; j++) {
1177
+ const x = seen(fills[roles[i]]);
1178
+ const y = seen(fills[roles[j]]);
1179
+ if (Math.hypot(x[0] - y[0], x[1] - y[1], x[2] - y[2]) < CVD_ALIKE) {
1180
+ out.push({a: roles[i], b: roles[j], vision});
1181
+ }
1182
+ }
1183
+ }
1184
+ }
1185
+ return out;
1186
+ }