zabi-components 8.0.0 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (364) hide show
  1. package/CHANGELOG.md +1924 -0
  2. package/README.md +243 -10
  3. package/THEME.md +149 -332
  4. package/THEMING.md +1192 -266
  5. package/dist/atoms/ActionPanel.svelte +15 -5
  6. package/dist/atoms/Avatar.svelte +104 -0
  7. package/dist/atoms/Avatar.svelte.d.ts +34 -0
  8. package/dist/atoms/Badge.svelte +36 -7
  9. package/dist/atoms/Badge.svelte.d.ts +2 -2
  10. package/dist/atoms/Button.svelte +177 -27
  11. package/dist/atoms/Button.svelte.d.ts +30 -2
  12. package/dist/atoms/Card.svelte +4 -4
  13. package/dist/atoms/CardHeader.svelte +16 -4
  14. package/dist/atoms/Checkbox.svelte +48 -21
  15. package/dist/atoms/Checkbox.svelte.d.ts +4 -2
  16. package/dist/atoms/CodeBlock.svelte +20 -2
  17. package/dist/atoms/CodeBlock.svelte.d.ts +4 -0
  18. package/dist/atoms/ColorPicker.svelte +259 -49
  19. package/dist/atoms/ColorPicker.svelte.d.ts +3 -0
  20. package/dist/atoms/Container.svelte +16 -8
  21. package/dist/atoms/DateField.svelte +107 -0
  22. package/dist/atoms/DateField.svelte.d.ts +53 -0
  23. package/dist/atoms/FieldMessages.svelte +69 -0
  24. package/dist/atoms/FieldMessages.svelte.d.ts +23 -0
  25. package/dist/atoms/FloatingActionButton.svelte +174 -0
  26. package/dist/atoms/FloatingActionButton.svelte.d.ts +51 -0
  27. package/dist/atoms/Heading.svelte +37 -5
  28. package/dist/atoms/Heading.svelte.d.ts +7 -0
  29. package/dist/atoms/IconButton.svelte +220 -24
  30. package/dist/atoms/IconButton.svelte.d.ts +49 -4
  31. package/dist/atoms/Input.svelte +253 -52
  32. package/dist/atoms/Input.svelte.d.ts +32 -2
  33. package/dist/atoms/List.svelte +4 -1
  34. package/dist/atoms/ListItem.svelte +44 -11
  35. package/dist/atoms/ListItem.svelte.d.ts +5 -0
  36. package/dist/atoms/Progress.svelte +27 -8
  37. package/dist/atoms/Progress.svelte.d.ts +4 -0
  38. package/dist/atoms/Radio.svelte +19 -3
  39. package/dist/atoms/Radio.svelte.d.ts +4 -2
  40. package/dist/atoms/Rating.svelte +494 -0
  41. package/dist/atoms/Rating.svelte.d.ts +56 -0
  42. package/dist/atoms/Select.svelte +433 -91
  43. package/dist/atoms/Select.svelte.d.ts +49 -3
  44. package/dist/atoms/SelectionControl.svelte +94 -17
  45. package/dist/atoms/SelectionControl.svelte.d.ts +4 -3
  46. package/dist/atoms/Skeleton.svelte +1 -1
  47. package/dist/atoms/Slider.svelte +309 -0
  48. package/dist/atoms/Slider.svelte.d.ts +40 -0
  49. package/dist/atoms/Spinner.svelte +57 -0
  50. package/dist/atoms/Spinner.svelte.d.ts +19 -0
  51. package/dist/atoms/Table.svelte +151 -6
  52. package/dist/atoms/Table.svelte.d.ts +15 -3
  53. package/dist/atoms/TemporalField.svelte +140 -0
  54. package/dist/atoms/TemporalField.svelte.d.ts +32 -0
  55. package/dist/atoms/Text.svelte +23 -11
  56. package/dist/atoms/Text.svelte.d.ts +6 -3
  57. package/dist/atoms/Textarea.svelte +55 -44
  58. package/dist/atoms/Textarea.svelte.d.ts +14 -2
  59. package/dist/atoms/ThemeToggle.svelte +223 -44
  60. package/dist/atoms/ThemeToggle.svelte.d.ts +26 -3
  61. package/dist/atoms/TimeField.svelte +108 -0
  62. package/dist/atoms/TimeField.svelte.d.ts +54 -0
  63. package/dist/atoms/Toast.svelte +17 -4
  64. package/dist/atoms/Toast.svelte.d.ts +2 -0
  65. package/dist/atoms/Toggle.svelte +41 -8
  66. package/dist/atoms/Toggle.svelte.d.ts +11 -2
  67. package/dist/atoms/Tooltip.svelte +508 -23
  68. package/dist/atoms/Tooltip.svelte.d.ts +11 -0
  69. package/dist/atoms/ZabiStringsProvider.svelte +45 -0
  70. package/dist/atoms/ZabiStringsProvider.svelte.d.ts +27 -0
  71. package/dist/atoms/index.d.ts +17 -0
  72. package/dist/atoms/index.js +12 -0
  73. package/dist/atoms/selection-control.styles.d.ts +36 -5
  74. package/dist/atoms/selection-control.styles.js +40 -9
  75. package/dist/components/atoms/index.d.ts +17 -0
  76. package/dist/components/atoms/index.d.ts.map +1 -1
  77. package/dist/components/atoms/selection-control.styles.d.ts +36 -5
  78. package/dist/components/atoms/selection-control.styles.d.ts.map +1 -1
  79. package/dist/components/index.d.ts +80 -2
  80. package/dist/components/index.d.ts.map +1 -1
  81. package/dist/components/molecules/index.d.ts +46 -0
  82. package/dist/components/molecules/index.d.ts.map +1 -1
  83. package/dist/components/molecules/toast-store.d.ts +53 -7
  84. package/dist/components/molecules/toast-store.d.ts.map +1 -1
  85. package/dist/components/organisms/index.d.ts +5 -0
  86. package/dist/components/organisms/index.d.ts.map +1 -1
  87. package/dist/components/types/variants.d.ts +15 -1
  88. package/dist/components/types/variants.d.ts.map +1 -1
  89. package/dist/components/util/app-shell.d.ts +28 -0
  90. package/dist/components/util/app-shell.d.ts.map +1 -0
  91. package/dist/components/util/avatar.d.ts +36 -0
  92. package/dist/components/util/avatar.d.ts.map +1 -0
  93. package/dist/components/util/bottom-sheet.d.ts +30 -0
  94. package/dist/components/util/bottom-sheet.d.ts.map +1 -0
  95. package/dist/components/util/bottom-tab-bar.d.ts +35 -0
  96. package/dist/components/util/bottom-tab-bar.d.ts.map +1 -0
  97. package/dist/components/util/calendar.d.ts +56 -0
  98. package/dist/components/util/calendar.d.ts.map +1 -0
  99. package/dist/components/util/collapsible.d.ts +58 -0
  100. package/dist/components/util/collapsible.d.ts.map +1 -0
  101. package/dist/components/util/confirm-dialog.d.ts +12 -0
  102. package/dist/components/util/confirm-dialog.d.ts.map +1 -0
  103. package/dist/components/util/date.d.ts +96 -0
  104. package/dist/components/util/date.d.ts.map +1 -0
  105. package/dist/components/util/drawer.d.ts +13 -0
  106. package/dist/components/util/drawer.d.ts.map +1 -0
  107. package/dist/components/util/dropdown.d.ts +47 -0
  108. package/dist/components/util/dropdown.d.ts.map +1 -0
  109. package/dist/components/util/field.d.ts +39 -0
  110. package/dist/components/util/field.d.ts.map +1 -0
  111. package/dist/components/util/fit-in-viewport.d.ts +201 -0
  112. package/dist/components/util/fit-in-viewport.d.ts.map +1 -0
  113. package/dist/components/util/focus-utils.d.ts +75 -2
  114. package/dist/components/util/focus-utils.d.ts.map +1 -1
  115. package/dist/components/util/hydration.d.ts +45 -0
  116. package/dist/components/util/hydration.d.ts.map +1 -0
  117. package/dist/components/util/keyboard-inset.d.ts +18 -0
  118. package/dist/components/util/keyboard-inset.d.ts.map +1 -0
  119. package/dist/components/util/media-grid.d.ts +51 -0
  120. package/dist/components/util/media-grid.d.ts.map +1 -0
  121. package/dist/components/util/media.d.ts +6 -0
  122. package/dist/components/util/media.d.ts.map +1 -0
  123. package/dist/components/util/overlay-chrome.d.ts +53 -0
  124. package/dist/components/util/overlay-chrome.d.ts.map +1 -0
  125. package/dist/components/util/overlay.d.ts +72 -0
  126. package/dist/components/util/overlay.d.ts.map +1 -0
  127. package/dist/components/util/photo.d.ts +215 -0
  128. package/dist/components/util/photo.d.ts.map +1 -0
  129. package/dist/components/util/portal.d.ts +23 -0
  130. package/dist/components/util/portal.d.ts.map +1 -0
  131. package/dist/components/util/pull-to-refresh.d.ts +27 -0
  132. package/dist/components/util/pull-to-refresh.d.ts.map +1 -0
  133. package/dist/components/util/radio-keys.d.ts +25 -0
  134. package/dist/components/util/radio-keys.d.ts.map +1 -0
  135. package/dist/components/util/rating.d.ts +23 -0
  136. package/dist/components/util/rating.d.ts.map +1 -0
  137. package/dist/components/util/ready-made-strings.d.ts +77 -0
  138. package/dist/components/util/ready-made-strings.d.ts.map +1 -0
  139. package/dist/components/util/scroll-padding.d.ts +20 -0
  140. package/dist/components/util/scroll-padding.d.ts.map +1 -0
  141. package/dist/components/util/segmented-control.d.ts +16 -0
  142. package/dist/components/util/segmented-control.d.ts.map +1 -0
  143. package/dist/components/util/select.d.ts +41 -0
  144. package/dist/components/util/select.d.ts.map +1 -0
  145. package/dist/components/util/sheet-drag.d.ts +60 -0
  146. package/dist/components/util/sheet-drag.d.ts.map +1 -0
  147. package/dist/components/util/sidebar.d.ts +63 -0
  148. package/dist/components/util/sidebar.d.ts.map +1 -0
  149. package/dist/components/util/sortable-list.d.ts +65 -0
  150. package/dist/components/util/sortable-list.d.ts.map +1 -0
  151. package/dist/components/util/stepper.d.ts +40 -0
  152. package/dist/components/util/stepper.d.ts.map +1 -0
  153. package/dist/components/util/swipeable-list-item.d.ts +36 -0
  154. package/dist/components/util/swipeable-list-item.d.ts.map +1 -0
  155. package/dist/components/util/theme-mode.d.ts +80 -0
  156. package/dist/components/util/theme-mode.d.ts.map +1 -0
  157. package/dist/components/util/toaster.d.ts +93 -0
  158. package/dist/components/util/toaster.d.ts.map +1 -0
  159. package/dist/components/util/tooltip.d.ts +10 -0
  160. package/dist/components/util/tooltip.d.ts.map +1 -0
  161. package/dist/components/util/top-navbar.d.ts +11 -0
  162. package/dist/components/util/top-navbar.d.ts.map +1 -0
  163. package/dist/components/util/touch-target.d.ts +31 -0
  164. package/dist/components/util/touch-target.d.ts.map +1 -0
  165. package/dist/components/util/zabi-strings.d.ts +133 -0
  166. package/dist/components/util/zabi-strings.d.ts.map +1 -0
  167. package/dist/create-theme/cli.js +169 -0
  168. package/dist/create-theme/index.d.ts +176 -0
  169. package/dist/create-theme/index.js +817 -0
  170. package/dist/create-theme/lib/ramp-math.js +128 -0
  171. package/dist/create-theme/lib/resolve.js +141 -0
  172. package/dist/create-theme/theme-data.js +1116 -0
  173. package/dist/index.d.ts +80 -2
  174. package/dist/index.js +57 -2
  175. package/dist/molecules/Alert.svelte +33 -5
  176. package/dist/molecules/Alert.svelte.d.ts +2 -0
  177. package/dist/molecules/AppBar.svelte +415 -0
  178. package/dist/molecules/AppBar.svelte.d.ts +69 -0
  179. package/dist/molecules/AvatarGroup.svelte +116 -0
  180. package/dist/molecules/AvatarGroup.svelte.d.ts +40 -0
  181. package/dist/molecules/BottomSheet.svelte +540 -0
  182. package/dist/molecules/BottomSheet.svelte.d.ts +93 -0
  183. package/dist/molecules/BottomTabBar.svelte +297 -0
  184. package/dist/molecules/BottomTabBar.svelte.d.ts +64 -0
  185. package/dist/molecules/Calendar.svelte +597 -0
  186. package/dist/molecules/Calendar.svelte.d.ts +72 -0
  187. package/dist/molecules/Collapsible.svelte +243 -0
  188. package/dist/molecules/Collapsible.svelte.d.ts +44 -0
  189. package/dist/molecules/CollapsibleGroup.svelte +126 -0
  190. package/dist/molecules/CollapsibleGroup.svelte.d.ts +14 -0
  191. package/dist/molecules/ComponentDemo.svelte +16 -4
  192. package/dist/molecules/ComponentDemo.svelte.d.ts +3 -0
  193. package/dist/molecules/ConfirmDialog.svelte +253 -0
  194. package/dist/molecules/ConfirmDialog.svelte.d.ts +55 -0
  195. package/dist/molecules/ContactForm.svelte +34 -17
  196. package/dist/molecules/ContactForm.svelte.d.ts +8 -0
  197. package/dist/molecules/Drawer.svelte +394 -0
  198. package/dist/molecules/Drawer.svelte.d.ts +55 -0
  199. package/dist/molecules/Dropdown.svelte +509 -48
  200. package/dist/molecules/Dropdown.svelte.d.ts +45 -6
  201. package/dist/molecules/DropdownItem.svelte +122 -0
  202. package/dist/molecules/DropdownItem.svelte.d.ts +28 -0
  203. package/dist/molecules/EmptyState.svelte +55 -10
  204. package/dist/molecules/EmptyState.svelte.d.ts +8 -0
  205. package/dist/molecules/FormField.svelte +9 -1
  206. package/dist/molecules/FormField.svelte.d.ts +2 -0
  207. package/dist/molecules/Header.svelte +1 -1
  208. package/dist/molecules/ImageUpload.svelte +383 -70
  209. package/dist/molecules/ImageUpload.svelte.d.ts +47 -2
  210. package/dist/molecules/MediaGrid.svelte +527 -0
  211. package/dist/molecules/MediaGrid.svelte.d.ts +77 -0
  212. package/dist/molecules/Modal.svelte +288 -72
  213. package/dist/molecules/Modal.svelte.d.ts +51 -2
  214. package/dist/molecules/NavigationMenuContent.svelte +58 -1
  215. package/dist/molecules/NavigationMenuItem.svelte +0 -1
  216. package/dist/molecules/NavigationMenuLink.svelte +1 -1
  217. package/dist/molecules/NavigationMenuList.svelte +10 -3
  218. package/dist/molecules/NavigationMenuTrigger.svelte +2 -2
  219. package/dist/molecules/Page.svelte +32 -2
  220. package/dist/molecules/Page.svelte.d.ts +11 -0
  221. package/dist/molecules/PhotoGrid.svelte +433 -0
  222. package/dist/molecules/PhotoGrid.svelte.d.ts +62 -0
  223. package/dist/molecules/PhotoViewer.svelte +1116 -0
  224. package/dist/molecules/PhotoViewer.svelte.d.ts +56 -0
  225. package/dist/molecules/PropsTable.svelte +22 -10
  226. package/dist/molecules/PropsTable.svelte.d.ts +3 -0
  227. package/dist/molecules/PullToRefresh.svelte +263 -0
  228. package/dist/molecules/PullToRefresh.svelte.d.ts +52 -0
  229. package/dist/molecules/RadioGroup.svelte +52 -14
  230. package/dist/molecules/RadioGroup.svelte.d.ts +14 -3
  231. package/dist/molecules/Section.svelte +1 -1
  232. package/dist/molecules/SegmentedControl.svelte +323 -0
  233. package/dist/molecules/SegmentedControl.svelte.d.ts +34 -0
  234. package/dist/molecules/SidebarBrandHeader.svelte +11 -1
  235. package/dist/molecules/SidebarBrandHeader.svelte.d.ts +3 -0
  236. package/dist/molecules/SidebarFooter.svelte +29 -6
  237. package/dist/molecules/SidebarFooter.svelte.d.ts +3 -0
  238. package/dist/molecules/SlideUp.svelte +220 -61
  239. package/dist/molecules/SlideUp.svelte.d.ts +26 -0
  240. package/dist/molecules/SortableList.svelte +632 -0
  241. package/dist/molecules/SortableList.svelte.d.ts +69 -0
  242. package/dist/molecules/Stepper.svelte +548 -0
  243. package/dist/molecules/Stepper.svelte.d.ts +39 -0
  244. package/dist/molecules/StickyActionBar.svelte +191 -0
  245. package/dist/molecules/StickyActionBar.svelte.d.ts +65 -0
  246. package/dist/molecules/SwipeableListItem.svelte +334 -0
  247. package/dist/molecules/SwipeableListItem.svelte.d.ts +54 -0
  248. package/dist/molecules/Tabs.svelte +233 -9
  249. package/dist/molecules/Tabs.svelte.d.ts +7 -0
  250. package/dist/molecules/Toaster.svelte +350 -5
  251. package/dist/molecules/Toaster.svelte.d.ts +95 -2
  252. package/dist/molecules/ToasterToast.svelte +253 -67
  253. package/dist/molecules/ToasterToast.svelte.d.ts +9 -0
  254. package/dist/molecules/UnsavedChangesBar.svelte +212 -0
  255. package/dist/molecules/UnsavedChangesBar.svelte.d.ts +43 -0
  256. package/dist/molecules/index.d.ts +46 -0
  257. package/dist/molecules/index.js +27 -0
  258. package/dist/molecules/toast-store.d.ts +53 -7
  259. package/dist/molecules/toast-store.js +21 -0
  260. package/dist/organisms/AppShell.svelte +196 -0
  261. package/dist/organisms/AppShell.svelte.d.ts +61 -0
  262. package/dist/organisms/SidebarAccountPanel.svelte +107 -12
  263. package/dist/organisms/SidebarAccountPanel.svelte.d.ts +19 -0
  264. package/dist/organisms/SidebarNavigation.svelte +96 -15
  265. package/dist/organisms/SidebarNavigation.svelte.d.ts +29 -1
  266. package/dist/organisms/SidebarPanel.svelte +31 -10
  267. package/dist/organisms/SidebarShell.svelte +162 -11
  268. package/dist/organisms/SidebarShell.svelte.d.ts +56 -2
  269. package/dist/organisms/TopNavbar.svelte +256 -31
  270. package/dist/organisms/TopNavbar.svelte.d.ts +26 -0
  271. package/dist/organisms/index.d.ts +5 -0
  272. package/dist/organisms/index.js +3 -0
  273. package/dist/types/components.d.ts +4 -0
  274. package/dist/types/components.d.ts.map +1 -1
  275. package/dist/types/index.d.ts +605 -25
  276. package/dist/types/index.d.ts.map +1 -1
  277. package/dist/types/variants.ts +17 -1
  278. package/dist/util/app-shell.d.ts +27 -0
  279. package/dist/util/app-shell.js +72 -0
  280. package/dist/util/avatar.d.ts +35 -0
  281. package/dist/util/avatar.js +52 -0
  282. package/dist/util/bottom-sheet.d.ts +29 -0
  283. package/dist/util/bottom-sheet.js +43 -0
  284. package/dist/util/bottom-tab-bar.d.ts +34 -0
  285. package/dist/util/bottom-tab-bar.js +54 -0
  286. package/dist/util/calendar.d.ts +55 -0
  287. package/dist/util/calendar.js +74 -0
  288. package/dist/util/collapsible.d.ts +57 -0
  289. package/dist/util/collapsible.js +23 -0
  290. package/dist/util/confirm-dialog.d.ts +11 -0
  291. package/dist/util/confirm-dialog.js +2 -0
  292. package/dist/util/date.d.ts +95 -0
  293. package/dist/util/date.js +237 -0
  294. package/dist/util/drawer.d.ts +12 -0
  295. package/dist/util/drawer.js +7 -0
  296. package/dist/util/dropdown.d.ts +46 -0
  297. package/dist/util/dropdown.js +21 -0
  298. package/dist/util/field.d.ts +38 -0
  299. package/dist/util/field.js +38 -0
  300. package/dist/util/fit-in-viewport.d.ts +200 -0
  301. package/dist/util/fit-in-viewport.js +399 -0
  302. package/dist/util/focus-utils.d.ts +75 -2
  303. package/dist/util/focus-utils.js +182 -9
  304. package/dist/util/hydration.d.ts +44 -0
  305. package/dist/util/hydration.js +58 -0
  306. package/dist/util/keyboard-inset.d.ts +17 -0
  307. package/dist/util/keyboard-inset.js +53 -0
  308. package/dist/util/media-grid.d.ts +50 -0
  309. package/dist/util/media-grid.js +81 -0
  310. package/dist/util/media.d.ts +5 -0
  311. package/dist/util/media.js +10 -0
  312. package/dist/util/overlay-chrome.d.ts +52 -0
  313. package/dist/util/overlay-chrome.js +56 -0
  314. package/dist/util/overlay.d.ts +71 -0
  315. package/dist/util/overlay.js +240 -0
  316. package/dist/util/photo.d.ts +214 -0
  317. package/dist/util/photo.js +206 -0
  318. package/dist/util/portal.d.ts +22 -0
  319. package/dist/util/portal.js +50 -0
  320. package/dist/util/pull-to-refresh.d.ts +26 -0
  321. package/dist/util/pull-to-refresh.js +30 -0
  322. package/dist/util/radio-keys.d.ts +24 -0
  323. package/dist/util/radio-keys.js +49 -0
  324. package/dist/util/rating.d.ts +22 -0
  325. package/dist/util/rating.js +26 -0
  326. package/dist/util/ready-made-strings.d.ts +76 -0
  327. package/dist/util/ready-made-strings.js +67 -0
  328. package/dist/util/scroll-padding.d.ts +19 -0
  329. package/dist/util/scroll-padding.js +44 -0
  330. package/dist/util/segmented-control.d.ts +15 -0
  331. package/dist/util/segmented-control.js +1 -0
  332. package/dist/util/select.d.ts +40 -0
  333. package/dist/util/select.js +63 -0
  334. package/dist/util/sheet-drag.d.ts +59 -0
  335. package/dist/util/sheet-drag.js +241 -0
  336. package/dist/util/sidebar.d.ts +62 -0
  337. package/dist/util/sidebar.js +38 -0
  338. package/dist/util/sortable-list.d.ts +64 -0
  339. package/dist/util/sortable-list.js +53 -0
  340. package/dist/util/stepper.d.ts +39 -0
  341. package/dist/util/stepper.js +38 -0
  342. package/dist/util/swipeable-list-item.d.ts +35 -0
  343. package/dist/util/swipeable-list-item.js +32 -0
  344. package/dist/util/theme-mode.d.ts +79 -0
  345. package/dist/util/theme-mode.js +121 -0
  346. package/dist/util/toaster.d.ts +92 -0
  347. package/dist/util/toaster.js +75 -0
  348. package/dist/util/tooltip.d.ts +9 -0
  349. package/dist/util/tooltip.js +18 -0
  350. package/dist/util/top-navbar.d.ts +10 -0
  351. package/dist/util/top-navbar.js +5 -0
  352. package/dist/util/touch-target.d.ts +30 -0
  353. package/dist/util/touch-target.js +30 -0
  354. package/dist/util/zabi-strings.d.ts +132 -0
  355. package/dist/util/zabi-strings.js +105 -0
  356. package/dist/zabi-components-colors.css +5 -2
  357. package/dist/zabi-components-theme-dark-only.css +543 -241
  358. package/dist/zabi-components-theme-dark.css +543 -241
  359. package/dist/zabi-components-theme-only.css +1584 -367
  360. package/dist/zabi-components-theme.css +1584 -367
  361. package/dist/zabi-components.css +3885 -518
  362. package/docs/lucide-icons.md +40 -17
  363. package/docs/theme-imports.md +81 -11
  364. package/package.json +35 -8
@@ -0,0 +1,200 @@
1
+ /**
2
+ * Keeps a floating box (a menu, a panel, a tooltip) where it can be seen.
3
+ *
4
+ * One module for everything that opens next to a trigger. Two layers:
5
+ *
6
+ * - `pickSide` and `shiftIntoViewport` are the two decisions every such box
7
+ * needs: which side of its trigger it opens on, and how far it has to move
8
+ * along that side to stay on screen. Tooltip uses them as they are.
9
+ * - `fitPanel` builds on them for a panel that hangs from one edge of its
10
+ * anchor (Dropdown, Select's list, NavigationMenuContent): it also narrows
11
+ * and shortens a panel too large for the screen. `measurePlacement` reads
12
+ * the layout for it, and `watchViewport` re-runs it while the panel is open.
13
+ *
14
+ * The arithmetic is pure, so it can be tested without a layout.
15
+ *
16
+ * The screen is not the only thing that cuts a panel off. Inside a box that
17
+ * scrolls (a Modal's body, a list with its own scrollbar) the box clips it
18
+ * first. `measurePlacement` takes the nearest such box into account: it
19
+ * decides which side the panel opens on, and when the panel fits on neither
20
+ * side it says where to put the panel with `position: fixed`, which a
21
+ * scrolling ancestor does not clip.
22
+ */
23
+ export type PanelBlock = "bottom" | "top";
24
+ export type PanelInline = "start" | "end";
25
+ export interface PanelBox {
26
+ left: number;
27
+ right: number;
28
+ top: number;
29
+ bottom: number;
30
+ }
31
+ export interface FitPanelInput {
32
+ /** The box the panel is positioned against, in viewport coordinates. */
33
+ anchor: PanelBox;
34
+ /** The panel's natural size, measured with no limit on it. */
35
+ panel: {
36
+ width: number;
37
+ height: number;
38
+ };
39
+ viewport: {
40
+ width: number;
41
+ height: number;
42
+ };
43
+ /** The side asked for. It is kept whenever the panel fits there. */
44
+ block: PanelBlock;
45
+ inline: PanelInline;
46
+ /** Right-to-left layout: `start` is then the right edge. */
47
+ rtl?: boolean;
48
+ /** Room kept between the panel and every edge of the viewport. */
49
+ margin?: number;
50
+ /** Distance between the anchor and the panel. */
51
+ gap?: number;
52
+ /** False keeps the panel on the `block` side asked for, whatever fits. */
53
+ flipBlock?: boolean;
54
+ /** False keeps it on the `inline` edge asked for and slides it back inside instead. */
55
+ flipInline?: boolean;
56
+ /**
57
+ * The inside of the nearest ancestor that clips, in viewport coordinates.
58
+ * It decides which side the panel opens on and how far it slides; it
59
+ * never limits the panel's size, which only the viewport does.
60
+ */
61
+ clip?: PanelBox | null;
62
+ }
63
+ export interface PanelFit {
64
+ block: PanelBlock;
65
+ inline: PanelInline;
66
+ /**
67
+ * Distance to put between the anchor's `inline` edge and the panel's, for
68
+ * `inset-inline-start` or `inset-inline-end`. 0 when the panel fits as it
69
+ * is; otherwise what slides it back inside.
70
+ */
71
+ inlineOffset: number;
72
+ /** A limit to set, or null when the panel fits without one. */
73
+ maxWidth: number | null;
74
+ maxHeight: number | null;
75
+ /** Where the panel's box ends up, in viewport coordinates, and how wide it is there. */
76
+ left: number;
77
+ top: number;
78
+ width: number;
79
+ /** False when part of the panel is outside `clip` (or the viewport) even so. */
80
+ visible: boolean;
81
+ }
82
+ export type FloatingSide = "top" | "bottom" | "left" | "right";
83
+ /**
84
+ * The side a floating box of `size` opens on: the preferred one when it fits
85
+ * there, the opposite one when only that fits, and otherwise whichever of the
86
+ * two has more room.
87
+ *
88
+ * `gap` is the distance between trigger and box; `margin` is how close to the
89
+ * viewport edge the box may come. `clip` is the inside of an ancestor that
90
+ * cuts the box off, when there is one: the box then has to fit in what both
91
+ * leave.
92
+ */
93
+ export declare function pickSide(preferred: FloatingSide, trigger: PanelBox, size: {
94
+ width: number;
95
+ height: number;
96
+ }, viewport: {
97
+ width: number;
98
+ height: number;
99
+ }, { gap, margin, clip, }?: {
100
+ gap?: number;
101
+ margin?: number;
102
+ clip?: PanelBox | null;
103
+ }): FloatingSide;
104
+ /**
105
+ * How far `box` has to move, in px, to lie inside the viewport with `margin`
106
+ * to spare. Zero on an axis where it already does. A box larger than the
107
+ * viewport is aligned to the start edge (top, left), where its text begins.
108
+ */
109
+ export declare function shiftIntoViewport(box: PanelBox, viewport: {
110
+ width: number;
111
+ height: number;
112
+ }, margin?: number): {
113
+ x: number;
114
+ y: number;
115
+ };
116
+ export interface ViewportPoint {
117
+ x: number;
118
+ y: number;
119
+ }
120
+ /**
121
+ * True when `point` lies in the triangle `a`, `b`, `c`, edges included. For
122
+ * the path a pointer takes from a trigger to its floating box: as long as it
123
+ * stays in the triangle between where it left and the near edge of the box,
124
+ * it is on its way there.
125
+ */
126
+ export declare function insideTriangle(point: ViewportPoint, a: ViewportPoint, b: ViewportPoint, c: ViewportPoint): boolean;
127
+ /** Where the panel fits, starting from the side asked for. */
128
+ export declare function fitPanel(input: FitPanelInput): PanelFit;
129
+ /** A fit that changes nothing: the panel where the CSS puts it, with no limits. */
130
+ export declare function unfitted(block: PanelBlock, inline: PanelInline): PanelFit;
131
+ export interface MeasureOptions {
132
+ margin?: number;
133
+ gap?: number;
134
+ flipBlock?: boolean;
135
+ flipInline?: boolean;
136
+ /** Overrides the anchor's own direction; `false` for a panel placed with `left`, not `inset-inline-start`. */
137
+ rtl?: boolean;
138
+ clip?: PanelBox | null;
139
+ }
140
+ /**
141
+ * Measures `panel` against `anchor` and returns its fit. Any limit already on
142
+ * the panel is lifted for the measurement, so the answer does not depend on
143
+ * the previous one. Call it before paint (from an effect) and apply the result.
144
+ */
145
+ export declare function measureFit(anchor: Element, panel: HTMLElement, preferred: {
146
+ block: PanelBlock;
147
+ inline: PanelInline;
148
+ }, options?: MeasureOptions): PanelFit;
149
+ /**
150
+ * The nearest ancestor of `element` that clips what reaches outside it (any
151
+ * `overflow` but `visible`), with the box it clips to: its inside, without
152
+ * borders or scrollbars, in viewport coordinates. Null when only the viewport
153
+ * clips.
154
+ */
155
+ export declare function clippingAncestor(element: Element): {
156
+ element: HTMLElement;
157
+ box: PanelBox;
158
+ } | null;
159
+ export interface PanelPlacement extends PanelFit {
160
+ /**
161
+ * Set when the panel has to leave a clipping ancestor to be seen: the
162
+ * `top`, `left` and `width` to give it with `position: fixed`. Null when
163
+ * it stays where the CSS puts it.
164
+ *
165
+ * The width is the one the panel has where the CSS puts it. A fixed box
166
+ * is sized against the viewport instead of the anchor, so without it the
167
+ * panel would change width as it leaves the box.
168
+ */
169
+ fixed: {
170
+ left: number;
171
+ top: number;
172
+ width: number;
173
+ } | null;
174
+ }
175
+ /**
176
+ * The fit of `panel`, with the nearest clipping ancestor of `anchor` taken
177
+ * into account.
178
+ *
179
+ * - The panel fits inside that ancestor on the side asked for: nothing changes.
180
+ * - It fits on the other side: it opens there.
181
+ * - It fits on neither: it is placed against the viewport with `fixed`, on the
182
+ * side the viewport has room for, and reaches out of the ancestor. Only
183
+ * while some of the anchor can be seen in the ancestor: scrolled out of it,
184
+ * the anchor takes the panel with it.
185
+ * - It cannot be: something between the panel and that ancestor is the
186
+ * containing block of fixed descendants (a `transform`, a `filter`). It then
187
+ * opens on the side of the ancestor with more room, and is cut where that
188
+ * ends. That is the limit of what a panel can do without leaving its place
189
+ * in the DOM, and it stays there so that the keyboard, focus and a dialog's
190
+ * focus trap treat it as part of what opened it.
191
+ */
192
+ export declare function measurePlacement(anchor: Element, panel: HTMLElement, preferred: {
193
+ block: PanelBlock;
194
+ inline: PanelInline;
195
+ }, options?: MeasureOptions): PanelPlacement;
196
+ /**
197
+ * Calls `update` when the viewport changes size or anything outside `panel`
198
+ * scrolls, at most once a frame. Returns the function that stops it.
199
+ */
200
+ export declare function watchViewport(update: () => void, panel?: Element | null): () => void;
@@ -0,0 +1,399 @@
1
+ /**
2
+ * Keeps a floating box (a menu, a panel, a tooltip) where it can be seen.
3
+ *
4
+ * One module for everything that opens next to a trigger. Two layers:
5
+ *
6
+ * - `pickSide` and `shiftIntoViewport` are the two decisions every such box
7
+ * needs: which side of its trigger it opens on, and how far it has to move
8
+ * along that side to stay on screen. Tooltip uses them as they are.
9
+ * - `fitPanel` builds on them for a panel that hangs from one edge of its
10
+ * anchor (Dropdown, Select's list, NavigationMenuContent): it also narrows
11
+ * and shortens a panel too large for the screen. `measurePlacement` reads
12
+ * the layout for it, and `watchViewport` re-runs it while the panel is open.
13
+ *
14
+ * The arithmetic is pure, so it can be tested without a layout.
15
+ *
16
+ * The screen is not the only thing that cuts a panel off. Inside a box that
17
+ * scrolls (a Modal's body, a list with its own scrollbar) the box clips it
18
+ * first. `measurePlacement` takes the nearest such box into account: it
19
+ * decides which side the panel opens on, and when the panel fits on neither
20
+ * side it says where to put the panel with `position: fixed`, which a
21
+ * scrolling ancestor does not clip.
22
+ */
23
+ /** The viewport less its margin, and less whatever `clip` cuts off. */
24
+ function boundsOf(viewport, margin, clip) {
25
+ const view = {
26
+ left: margin,
27
+ top: margin,
28
+ right: viewport.width - margin,
29
+ bottom: viewport.height - margin,
30
+ };
31
+ if (!clip)
32
+ return view;
33
+ return {
34
+ left: Math.max(view.left, clip.left),
35
+ top: Math.max(view.top, clip.top),
36
+ right: Math.min(view.right, clip.right),
37
+ bottom: Math.min(view.bottom, clip.bottom),
38
+ };
39
+ }
40
+ const OPPOSITE = {
41
+ top: "bottom",
42
+ bottom: "top",
43
+ left: "right",
44
+ right: "left",
45
+ };
46
+ /** The room, in px, between each side of `trigger` and the matching edge of `bounds`. */
47
+ function roomAround(trigger, bounds) {
48
+ return {
49
+ top: trigger.top - bounds.top,
50
+ bottom: bounds.bottom - trigger.bottom,
51
+ left: trigger.left - bounds.left,
52
+ right: bounds.right - trigger.right,
53
+ };
54
+ }
55
+ /**
56
+ * The side a floating box of `size` opens on: the preferred one when it fits
57
+ * there, the opposite one when only that fits, and otherwise whichever of the
58
+ * two has more room.
59
+ *
60
+ * `gap` is the distance between trigger and box; `margin` is how close to the
61
+ * viewport edge the box may come. `clip` is the inside of an ancestor that
62
+ * cuts the box off, when there is one: the box then has to fit in what both
63
+ * leave.
64
+ */
65
+ export function pickSide(preferred, trigger, size, viewport, { gap = 8, margin = 8, clip = null, } = {}) {
66
+ const room = roomAround(trigger, boundsOf(viewport, margin, clip));
67
+ const needed = (preferred === "top" || preferred === "bottom" ? size.height : size.width) + gap;
68
+ const opposite = OPPOSITE[preferred];
69
+ if (room[preferred] >= needed)
70
+ return preferred;
71
+ if (room[opposite] >= needed)
72
+ return opposite;
73
+ return room[opposite] > room[preferred] ? opposite : preferred;
74
+ }
75
+ /**
76
+ * Where a box `extent` long that starts at `start` ends up when it has to lie
77
+ * between `from` and `to`. A box longer than that is put at `from`, where its
78
+ * text begins.
79
+ */
80
+ function slideInto(start, extent, from, to) {
81
+ if (extent > to - from)
82
+ return from;
83
+ return Math.min(Math.max(start, from), to - extent);
84
+ }
85
+ /**
86
+ * How far `box` has to move, in px, to lie inside the viewport with `margin`
87
+ * to spare. Zero on an axis where it already does. A box larger than the
88
+ * viewport is aligned to the start edge (top, left), where its text begins.
89
+ */
90
+ export function shiftIntoViewport(box, viewport, margin = 8) {
91
+ const view = boundsOf(viewport, margin);
92
+ return {
93
+ x: slideInto(box.left, box.right - box.left, view.left, view.right) - box.left,
94
+ y: slideInto(box.top, box.bottom - box.top, view.top, view.bottom) - box.top,
95
+ };
96
+ }
97
+ /**
98
+ * True when `point` lies in the triangle `a`, `b`, `c`, edges included. For
99
+ * the path a pointer takes from a trigger to its floating box: as long as it
100
+ * stays in the triangle between where it left and the near edge of the box,
101
+ * it is on its way there.
102
+ */
103
+ export function insideTriangle(point, a, b, c) {
104
+ const side = (p, q, r) => (p.x - r.x) * (q.y - r.y) - (q.x - r.x) * (p.y - r.y);
105
+ const first = side(point, a, b);
106
+ const second = side(point, b, c);
107
+ const third = side(point, c, a);
108
+ const negative = first < 0 || second < 0 || third < 0;
109
+ const positive = first > 0 || second > 0 || third > 0;
110
+ return !(negative && positive);
111
+ }
112
+ /** Where the panel fits, starting from the side asked for. */
113
+ export function fitPanel(input) {
114
+ const { anchor, panel, viewport, rtl = false, margin = 8, gap = 8, flipBlock = true, flipInline = true, clip = null, } = input;
115
+ const view = boundsOf(viewport, margin);
116
+ // What the panel has to be inside to be seen: the same as `view` unless something clips.
117
+ const within = boundsOf(viewport, margin, clip);
118
+ // Width first: a panel wider than the screen is narrowed, whichever side it is on.
119
+ const widest = Math.max(0, view.right - view.left);
120
+ const width = Math.min(panel.width, widest);
121
+ const maxWidth = panel.width > widest ? widest : null;
122
+ // `start` grows from the anchor's left edge (its right one when right-to-left).
123
+ const growsRight = (side) => (side === "start") !== rtl;
124
+ const leftFor = (side) => growsRight(side) ? anchor.left : anchor.right - width;
125
+ const fitsInline = (side) => {
126
+ const left = leftFor(side);
127
+ return left >= within.left && left + width <= within.right;
128
+ };
129
+ const other = input.inline === "start" ? "end" : "start";
130
+ const inline = !flipInline || fitsInline(input.inline) || !fitsInline(other) ? input.inline : other;
131
+ // Neither side fits: stay on the side asked for and slide back inside. A
132
+ // panel too wide for a clipping box cannot be slid into it; the screen is
133
+ // then all there is to stay inside.
134
+ const start = leftFor(inline);
135
+ const lane = width <= within.right - within.left ? within : view;
136
+ const left = slideInto(start, width, lane.left, lane.right);
137
+ const inlineOffset = growsRight(inline) ? left - start : start - left;
138
+ const roomIn = (bounds, side) => side === "bottom" ? bounds.bottom - anchor.bottom - gap : anchor.top - gap - bounds.top;
139
+ // The other side if the panel fits only there, else whichever has more room.
140
+ const block = flipBlock
141
+ ? pickSide(input.block, anchor, panel, viewport, { gap, margin, clip })
142
+ : input.block;
143
+ // The size limit is the screen's, whatever clips: a menu squeezed into a
144
+ // 160px box would be worse than one that reaches out of it.
145
+ const maxHeight = panel.height > roomIn(view, block) ? Math.max(0, roomIn(view, block)) : null;
146
+ const height = maxHeight ?? panel.height;
147
+ const top = block === "bottom" ? anchor.bottom + gap : anchor.top - gap - height;
148
+ const slack = 0.5;
149
+ const visible = left >= within.left - slack &&
150
+ left + width <= within.right + slack &&
151
+ top >= within.top - slack &&
152
+ top + height <= within.bottom + slack;
153
+ return {
154
+ block,
155
+ inline,
156
+ // -0 would print as "-0px".
157
+ inlineOffset: inlineOffset === 0 ? 0 : inlineOffset,
158
+ maxWidth,
159
+ maxHeight,
160
+ left,
161
+ top,
162
+ width,
163
+ visible,
164
+ };
165
+ }
166
+ /** A fit that changes nothing: the panel where the CSS puts it, with no limits. */
167
+ export function unfitted(block, inline) {
168
+ return {
169
+ block,
170
+ inline,
171
+ inlineOffset: 0,
172
+ maxWidth: null,
173
+ maxHeight: null,
174
+ left: 0,
175
+ top: 0,
176
+ width: 0,
177
+ visible: true,
178
+ };
179
+ }
180
+ /**
181
+ * Notes how far `panel` and whatever scrolls inside it are scrolled, and
182
+ * returns the function that puts it back. The part of a panel that scrolls is
183
+ * often a list inside it (Dropdown's, Select's), not the panel.
184
+ */
185
+ function rememberScroll(panel) {
186
+ const scrolled = [panel, ...panel.querySelectorAll("*")]
187
+ .filter((element) => element.scrollTop !== 0 || element.scrollLeft !== 0)
188
+ .map((element) => ({ element, top: element.scrollTop, left: element.scrollLeft }));
189
+ return () => {
190
+ for (const { element, top, left } of scrolled) {
191
+ element.scrollTop = top;
192
+ element.scrollLeft = left;
193
+ }
194
+ };
195
+ }
196
+ /**
197
+ * Measures `panel` against `anchor` and returns its fit. Any limit already on
198
+ * the panel is lifted for the measurement, so the answer does not depend on
199
+ * the previous one. Call it before paint (from an effect) and apply the result.
200
+ */
201
+ export function measureFit(anchor, panel, preferred, options = {}) {
202
+ const { maxWidth, maxHeight } = panel.style;
203
+ // Without its limit the panel has nothing to scroll, and the browser
204
+ // forgets how far it was scrolled: put that back as well.
205
+ const restoreScroll = rememberScroll(panel);
206
+ panel.style.maxWidth = "none";
207
+ panel.style.maxHeight = "none";
208
+ const natural = panel.getBoundingClientRect();
209
+ let height = natural.height;
210
+ // A panel that will be narrowed wraps its text and grows taller: the
211
+ // height that has to fit is the one it has at that width.
212
+ const viewportWidth = document.documentElement.clientWidth || window.innerWidth;
213
+ const widest = Math.max(0, viewportWidth - (options.margin ?? 8) * 2);
214
+ if (natural.width > widest) {
215
+ const { minWidth } = panel.style;
216
+ panel.style.maxWidth = `${widest}px`;
217
+ panel.style.minWidth = `${widest}px`;
218
+ height = panel.getBoundingClientRect().height;
219
+ panel.style.minWidth = minWidth;
220
+ }
221
+ const size = { width: natural.width, height };
222
+ panel.style.maxWidth = maxWidth;
223
+ panel.style.maxHeight = maxHeight;
224
+ restoreScroll();
225
+ // Nothing is laid out (a hidden ancestor, or a test without a layout engine).
226
+ if (size.width === 0 && size.height === 0)
227
+ return unfitted(preferred.block, preferred.inline);
228
+ const box = anchor.getBoundingClientRect();
229
+ return fitPanel({
230
+ anchor: { left: box.left, right: box.right, top: box.top, bottom: box.bottom },
231
+ panel: { width: size.width, height: size.height },
232
+ // The layout viewport, without a scrollbar's width.
233
+ viewport: {
234
+ width: viewportWidth,
235
+ height: document.documentElement.clientHeight || window.innerHeight,
236
+ },
237
+ ...preferred,
238
+ rtl: getComputedStyle(anchor).direction === "rtl",
239
+ ...options,
240
+ });
241
+ }
242
+ /**
243
+ * The nearest ancestor of `element` that clips what reaches outside it (any
244
+ * `overflow` but `visible`), with the box it clips to: its inside, without
245
+ * borders or scrollbars, in viewport coordinates. Null when only the viewport
246
+ * clips.
247
+ */
248
+ export function clippingAncestor(element) {
249
+ for (let current = element.parentElement; current && current !== document.body && current !== document.documentElement; current = current.parentElement) {
250
+ const style = getComputedStyle(current);
251
+ if (style.overflowX === "visible" && style.overflowY === "visible")
252
+ continue;
253
+ const outer = current.getBoundingClientRect();
254
+ const left = outer.left + current.clientLeft;
255
+ const top = outer.top + current.clientTop;
256
+ return {
257
+ element: current,
258
+ box: {
259
+ left,
260
+ top,
261
+ right: left + current.clientWidth,
262
+ bottom: top + current.clientHeight,
263
+ },
264
+ };
265
+ }
266
+ return null;
267
+ }
268
+ /**
269
+ * Whether `element` is the containing block of `position: fixed` descendants:
270
+ * such a descendant is placed against it, not the viewport, and is clipped by
271
+ * it like any other child.
272
+ */
273
+ function containsFixed(element) {
274
+ const style = getComputedStyle(element);
275
+ const set = (value) => !!value && value !== "none";
276
+ return (set(style.transform) ||
277
+ set(style.filter) ||
278
+ set(style.perspective) ||
279
+ set(style.backdropFilter) ||
280
+ /paint|layout|strict|content/.test(style.contain ?? "") ||
281
+ /transform|filter|perspective/.test(style.willChange ?? "") ||
282
+ (!!style.containerType && style.containerType !== "normal"));
283
+ }
284
+ /** Whether a fixed `panel` would get out of `clipper`: nothing from the panel up to it holds fixed descendants. */
285
+ function escapes(panel, clipper) {
286
+ for (let current = panel.parentElement; current; current = current.parentElement) {
287
+ if (containsFixed(current))
288
+ return false;
289
+ if (current === clipper)
290
+ return true;
291
+ }
292
+ return true;
293
+ }
294
+ /**
295
+ * Where `position: fixed; top: 0; left: 0` puts `panel`. That is the viewport's
296
+ * corner unless an ancestor further up holds fixed descendants, in which case
297
+ * coordinates have to be given from its corner instead.
298
+ */
299
+ function fixedOrigin(panel) {
300
+ const saved = panel.style.cssText;
301
+ panel.style.position = "fixed";
302
+ panel.style.inset = "0 auto auto 0";
303
+ panel.style.margin = "0";
304
+ panel.style.translate = "none";
305
+ const box = panel.getBoundingClientRect();
306
+ panel.style.cssText = saved;
307
+ return { left: box.left, top: box.top };
308
+ }
309
+ /** The inline styles `fixed` is applied with. Lifted while the panel is measured. */
310
+ const FIXED_STYLES = ["position", "top", "left", "right", "bottom", "margin", "width"];
311
+ /** Runs `measure` with the panel where the CSS puts it, whatever an earlier placement set on it. */
312
+ function asPlaced(panel, measure) {
313
+ const saved = FIXED_STYLES.map((name) => panel.style[name]);
314
+ const restoreScroll = rememberScroll(panel);
315
+ for (const name of FIXED_STYLES)
316
+ panel.style[name] = "";
317
+ try {
318
+ return measure();
319
+ }
320
+ finally {
321
+ FIXED_STYLES.forEach((name, index) => (panel.style[name] = saved[index]));
322
+ restoreScroll();
323
+ }
324
+ }
325
+ /**
326
+ * The fit of `panel`, with the nearest clipping ancestor of `anchor` taken
327
+ * into account.
328
+ *
329
+ * - The panel fits inside that ancestor on the side asked for: nothing changes.
330
+ * - It fits on the other side: it opens there.
331
+ * - It fits on neither: it is placed against the viewport with `fixed`, on the
332
+ * side the viewport has room for, and reaches out of the ancestor. Only
333
+ * while some of the anchor can be seen in the ancestor: scrolled out of it,
334
+ * the anchor takes the panel with it.
335
+ * - It cannot be: something between the panel and that ancestor is the
336
+ * containing block of fixed descendants (a `transform`, a `filter`). It then
337
+ * opens on the side of the ancestor with more room, and is cut where that
338
+ * ends. That is the limit of what a panel can do without leaving its place
339
+ * in the DOM, and it stays there so that the keyboard, focus and a dialog's
340
+ * focus trap treat it as part of what opened it.
341
+ */
342
+ export function measurePlacement(anchor, panel, preferred, options = {}) {
343
+ const clipper = clippingAncestor(anchor);
344
+ if (!clipper)
345
+ return { ...measureFit(anchor, panel, preferred, options), fixed: null };
346
+ const inside = asPlaced(panel, () => measureFit(anchor, panel, preferred, { ...options, clip: clipper.box }));
347
+ if (inside.visible || !escapes(panel, clipper.element))
348
+ return { ...inside, fixed: null };
349
+ // The anchor itself is scrolled out of the box: the panel goes with it. Left
350
+ // fixed, it would float over the page with nothing to show what it belongs to.
351
+ const box = anchor.getBoundingClientRect();
352
+ const { box: clip } = clipper;
353
+ if (box.bottom <= clip.top || box.top >= clip.bottom || box.right <= clip.left || box.left >= clip.right) {
354
+ return { ...inside, fixed: null };
355
+ }
356
+ // Out of the box: only the viewport decides now.
357
+ const free = asPlaced(panel, () => measureFit(anchor, panel, preferred, options));
358
+ // Out of the box it would still not be on screen (a panel that may not
359
+ // flip, under an anchor near the bottom of the page's own scroller): it
360
+ // stays where it is, and scrolling brings it into view as it always did.
361
+ // Fixed, it would hang below the screen until the anchor was scrolled up.
362
+ if (!free.visible)
363
+ return { ...inside, fixed: null };
364
+ const origin = fixedOrigin(panel);
365
+ return {
366
+ ...free,
367
+ fixed: { left: free.left - origin.left, top: free.top - origin.top, width: free.width },
368
+ };
369
+ }
370
+ /**
371
+ * Calls `update` when the viewport changes size or anything outside `panel`
372
+ * scrolls, at most once a frame. Returns the function that stops it.
373
+ */
374
+ export function watchViewport(update, panel) {
375
+ if (typeof window === "undefined")
376
+ return () => { };
377
+ let frame = 0;
378
+ const schedule = (event) => {
379
+ // The panel scrolling its own content moves nothing it is placed against.
380
+ if (panel && event?.type === "scroll" && event.target instanceof Node && panel.contains(event.target)) {
381
+ return;
382
+ }
383
+ if (frame)
384
+ return;
385
+ frame = requestAnimationFrame(() => {
386
+ frame = 0;
387
+ update();
388
+ });
389
+ };
390
+ window.addEventListener("resize", schedule);
391
+ // Capture: a scroll inside any ancestor moves the anchor too, and scroll does not bubble.
392
+ window.addEventListener("scroll", schedule, true);
393
+ return () => {
394
+ if (frame)
395
+ cancelAnimationFrame(frame);
396
+ window.removeEventListener("resize", schedule);
397
+ window.removeEventListener("scroll", schedule, true);
398
+ };
399
+ }
@@ -2,10 +2,83 @@
2
2
  /** From `app.css` `@layer components` — import the library/app stylesheet. */
3
3
  export declare const FOCUS_BRAND_CLASS = "focus-brand";
4
4
  export declare const FOCUS_NAV_CLASS = "focus-nav";
5
+ /** Every dialog panel: `dialog` and `alertdialog` are both dialogs to a focus trap. */
6
+ export declare const DIALOG_SELECTOR = "[role=\"dialog\"], [role=\"alertdialog\"]";
5
7
  export declare function getFocusableElements(container: HTMLElement): HTMLElement[];
8
+ /** The region a `Toaster` renders its toasts in. */
9
+ export declare const TOAST_REGION_SELECTOR = "[data-zabi-toaster]";
10
+ /**
11
+ * Whether an event's target is in a toast. A toast is drawn over whatever is
12
+ * open, and a press on it is for the toast (to hold it, read it, dismiss it):
13
+ * a menu or a panel that closes on a press outside itself asks this first,
14
+ * and leaves a press on a toast alone.
15
+ */
16
+ export declare function isInsideToastRegion(target: unknown): boolean;
17
+ /**
18
+ * The controls of the toasts on screen. A toast is drawn over a modal
19
+ * overlay and can cover its buttons, so a focus trap that kept the keyboard
20
+ * out of it would leave no way to dismiss it: the trap takes these in, after
21
+ * the overlay's own controls.
22
+ */
23
+ export declare function getToastFocusables(): HTMLElement[];
24
+ /**
25
+ * One step of the Tab cycle of a modal overlay: through the controls of
26
+ * `container`, then through the toasts on screen, and round again. Focusables
27
+ * are looked up on every Tab, so what is added while the overlay is open is
28
+ * in the cycle. Focus that is nowhere in it goes to the first control.
29
+ */
30
+ export declare function cycleTrappedFocus(container: HTMLElement, event: KeyboardEvent): void;
6
31
  /** Tab wraps first↔last inside `container`. Caller should pair with `saveFocus` / `returnFocus`. */
7
32
  export declare function trapFocus(container: HTMLElement): () => void;
8
33
  export declare function saveFocus(): void;
9
- /** Restores focus from the last `saveFocus`. Skips disconnected or unfocusable nodes. */
34
+ /**
35
+ * Restores focus from the last `saveFocus`. If that element has left the
36
+ * document, the element that now has its id takes the focus instead (a
37
+ * dropzone swapped for its Change button, say); with neither, nothing happens.
38
+ */
10
39
  export declare function returnFocus(): void;
11
- export declare function focusFirstElement(container: HTMLElement): void;
40
+ /**
41
+ * Moves focus into an overlay: to its first focusable element, or to the
42
+ * container itself when it has none (give the container `tabindex="-1"`), so
43
+ * focus never stays on the opener behind the backdrop.
44
+ *
45
+ * `preferred` is a CSS selector, looked up inside the container: the matching
46
+ * control takes focus instead of the first one, if it can take focus.
47
+ */
48
+ export declare function focusFirstElement(container: HTMLElement, preferred?: string): void;
49
+ /**
50
+ * Joins the stack of open modal overlays (Modal, SlideUp, a drawer).
51
+ *
52
+ * Every overlay shares one z-index, so without this the paint order is the
53
+ * DOM order, and an overlay opened later but placed earlier in the document
54
+ * opens behind the first. `depth` is 0 for the first overlay and one more than
55
+ * the highest open one after that: put it on the overlay root as
56
+ * `z-index: calc(var(--z-modal) + depth)`.
57
+ *
58
+ * `panel` is the element with the dialog role. Call this when the overlay
59
+ * opens, in the browser, and `leave()` when it closes.
60
+ */
61
+ export declare function joinOverlayStack(panel: HTMLElement): {
62
+ depth: number;
63
+ leave: () => void;
64
+ };
65
+ /**
66
+ * Keeps a modal overlay in charge of the keyboard when focus has left it
67
+ * without a Tab: the focused control was disabled or removed and the browser
68
+ * dropped focus on `<body>`, where the overlay's own key handlers never hear
69
+ * the key, and Tab would walk the page behind it.
70
+ *
71
+ * While installed, a Tab pressed with focus outside every dialog moves focus
72
+ * to the first focusable element of `container` (or to `container` itself),
73
+ * and Escape calls `onEscape`. Only the overlay on top of the stack acts (the
74
+ * one opened last), so every open overlay can install its own. An overlay that
75
+ * has not called `joinOverlayStack` is added to the stack for as long as the
76
+ * recovery is installed.
77
+ *
78
+ * `container` is the panel with `role="dialog"` or `"alertdialog"` and
79
+ * `aria-modal="true"`. Call it when the overlay opens, in the browser, and call
80
+ * the returned function when it closes.
81
+ */
82
+ export declare function recoverStrayFocus(container: HTMLElement, options?: {
83
+ onEscape?: (event: KeyboardEvent) => void;
84
+ }): () => void;