@torpor/ui 0.1.6 → 0.2.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 (278) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/dist/Accordion/Accordion.torp +2 -2
  3. package/dist/Accordion/AccordionContent.torp +2 -2
  4. package/dist/Accordion/AccordionHeader.torp +2 -2
  5. package/dist/Accordion/AccordionItem.torp +2 -2
  6. package/dist/Accordion/AccordionTrigger.torp +2 -2
  7. package/dist/Accordion/index.d.ts +92 -0
  8. package/dist/Breadcrumb/Breadcrumb.torp +33 -0
  9. package/dist/Breadcrumb/BreadcrumbItem.torp +19 -0
  10. package/dist/Breadcrumb/BreadcrumbSeparator.torp +16 -0
  11. package/dist/Breadcrumb/index.d.ts +56 -0
  12. package/dist/Breadcrumb/index.js +14 -0
  13. package/dist/Calendar/Calendar.torp +265 -0
  14. package/dist/Calendar/CalendarDay.torp +67 -0
  15. package/dist/Calendar/CalendarTypes.ts +22 -0
  16. package/dist/Calendar/index.d.ts +38 -0
  17. package/dist/Calendar/index.js +10 -0
  18. package/dist/Calendar/index.js.map +1 -0
  19. package/dist/Dialog/Dialog.torp +3 -3
  20. package/dist/Dialog/DialogBody.torp +2 -2
  21. package/dist/Dialog/DialogButton.torp +2 -2
  22. package/dist/Dialog/DialogFooter.torp +2 -2
  23. package/dist/Dialog/DialogHeader.torp +2 -2
  24. package/dist/Dialog/index.d.ts +92 -0
  25. package/dist/Disclosure/Disclosure.torp +69 -0
  26. package/dist/Disclosure/DisclosureContent.torp +36 -0
  27. package/dist/Disclosure/DisclosureHeader.torp +34 -0
  28. package/dist/Disclosure/DisclosureTrigger.torp +36 -0
  29. package/dist/Disclosure/DisclosureTypes.ts +14 -0
  30. package/dist/Disclosure/index.d.ts +74 -0
  31. package/dist/Disclosure/index.js +18 -0
  32. package/dist/Disclosure/index.js.map +1 -0
  33. package/{src/Offscreen/Offscreen.torp → dist/Drawer/Drawer.torp} +10 -19
  34. package/dist/Drawer/index.d.ts +20 -0
  35. package/dist/Drawer/index.js +6 -0
  36. package/dist/Drawer/index.js.map +1 -0
  37. package/dist/Menu/Menu.torp +160 -0
  38. package/dist/Menu/MenuButton.torp +53 -0
  39. package/dist/Menu/MenuCheck.torp +70 -0
  40. package/dist/Menu/MenuGroup.torp +19 -0
  41. package/dist/Menu/MenuIndicator.torp +29 -0
  42. package/dist/Menu/MenuPopout.torp +71 -0
  43. package/dist/Menu/MenuPopoutContent.torp +74 -0
  44. package/dist/Menu/MenuPopoutTrigger.torp +117 -0
  45. package/dist/Menu/MenuRadio.torp +88 -0
  46. package/dist/Menu/MenuRadioGroup.torp +86 -0
  47. package/dist/Menu/MenuSeparator.torp +14 -0
  48. package/dist/Menu/MenuTypes.ts +58 -0
  49. package/dist/Menu/index.d.ts +200 -0
  50. package/dist/Menu/index.js +46 -0
  51. package/dist/Menu/index.js.map +1 -0
  52. package/dist/MenuBar/MenuBar.torp +150 -0
  53. package/dist/MenuBar/MenuBarItem.torp +104 -0
  54. package/dist/MenuBar/MenuBarItemContent.torp +108 -0
  55. package/dist/MenuBar/MenuBarItemTrigger.torp +89 -0
  56. package/dist/MenuBar/MenuBarTypes.ts +36 -0
  57. package/dist/MenuBar/index.d.ts +74 -0
  58. package/dist/MenuBar/index.js +18 -0
  59. package/dist/MenuBar/index.js.map +1 -0
  60. package/dist/NavMenu/NavMenu.torp +2 -2
  61. package/dist/NavMenu/NavMenuButton.torp +2 -2
  62. package/dist/NavMenu/NavMenuElement.torp +2 -2
  63. package/dist/NavMenu/NavMenuGroup.torp +2 -2
  64. package/dist/NavMenu/NavMenuGroupHeader.torp +2 -2
  65. package/dist/NavMenu/NavMenuLink.torp +2 -2
  66. package/dist/NavMenu/NavMenuPopout.torp +2 -2
  67. package/dist/NavMenu/NavMenuPopoutButton.torp +3 -3
  68. package/dist/NavMenu/NavMenuPopoutContent.torp +29 -27
  69. package/dist/NavMenu/NavMenuSeparator.torp +2 -2
  70. package/dist/NavMenu/index.d.ts +182 -0
  71. package/dist/Notification/Notification.torp +48 -0
  72. package/dist/Notification/NotificationClose.torp +28 -0
  73. package/dist/Notification/NotificationContainer.torp +14 -0
  74. package/dist/Notification/NotificationContent.torp +13 -0
  75. package/dist/Notification/NotificationTypes.ts +5 -0
  76. package/dist/Notification/index.d.ts +74 -0
  77. package/dist/Notification/index.js +36 -0
  78. package/dist/Notification/index.js.map +1 -0
  79. package/dist/Notification/showNotification.ts +30 -0
  80. package/dist/Popover/Popover.torp +2 -2
  81. package/dist/Popover/PopoverContent.torp +31 -28
  82. package/dist/Popover/PopoverContentAnchored.torp +39 -38
  83. package/dist/Popover/PopoverContentContextual.torp +39 -36
  84. package/dist/Popover/PopoverOverlay.torp +3 -3
  85. package/dist/Popover/PopoverTrigger.torp +2 -2
  86. package/dist/Popover/PopoverTriggerContextual.torp +2 -2
  87. package/dist/Popover/PopoverTriggerHover.torp +3 -3
  88. package/dist/Popover/index.d.ts +146 -0
  89. package/dist/TabGroup/TabContent.torp +2 -2
  90. package/dist/TabGroup/TabGroup.torp +2 -2
  91. package/dist/TabGroup/TabHeader.torp +3 -3
  92. package/dist/TabGroup/TabList.torp +2 -2
  93. package/dist/TabGroup/TabTrigger.torp +2 -2
  94. package/dist/TabGroup/index.d.ts +92 -0
  95. package/dist/icons/ArrowDown.torp +15 -0
  96. package/dist/icons/ArrowLeft.torp +15 -0
  97. package/dist/icons/ArrowRight.torp +15 -0
  98. package/dist/icons/ArrowUp.torp +15 -0
  99. package/dist/icons/Bars.torp +17 -0
  100. package/dist/icons/Check.torp +15 -0
  101. package/dist/icons/ChevronDown.torp +15 -0
  102. package/dist/icons/ChevronLeft.torp +15 -0
  103. package/dist/icons/ChevronRight.torp +15 -0
  104. package/dist/icons/ChevronUp.torp +15 -0
  105. package/dist/icons/Cross.torp +16 -0
  106. package/dist/icons/Loading.torp +16 -0
  107. package/dist/icons/Minus.torp +15 -0
  108. package/dist/icons/Plus.torp +16 -0
  109. package/dist/icons/index.d.ts +254 -0
  110. package/dist/icons/index.js +58 -0
  111. package/dist/icons/index.js.map +1 -0
  112. package/dist/{motion.d.cts → index.d.ts} +2 -2
  113. package/dist/metafile-esm.json +1 -1
  114. package/dist/motion/fade.ts +7 -0
  115. package/dist/motion/index.d.ts +2 -0
  116. package/dist/{motion.js → motion/index.js} +1 -1
  117. package/dist/motion/index.js.map +1 -0
  118. package/dist/mount/index.d.ts +2 -0
  119. package/dist/utils/dateUtils.ts +244 -0
  120. package/dist/utils/index.d.ts +2 -0
  121. package/dist/utils/setPopoverPosition.ts +4 -0
  122. package/package.json +51 -35
  123. package/scripts/postbuild.ts +42 -5
  124. package/src/Accordion/Accordion.torp +2 -2
  125. package/src/Accordion/AccordionContent.torp +2 -2
  126. package/src/Accordion/AccordionHeader.torp +2 -2
  127. package/src/Accordion/AccordionItem.torp +2 -2
  128. package/src/Accordion/AccordionTrigger.torp +2 -2
  129. package/src/Breadcrumb/Breadcrumb.torp +33 -0
  130. package/src/Breadcrumb/BreadcrumbItem.torp +19 -0
  131. package/src/Breadcrumb/BreadcrumbSeparator.torp +16 -0
  132. package/src/Breadcrumb/index.ts +5 -0
  133. package/src/Calendar/Calendar.torp +265 -0
  134. package/src/Calendar/CalendarDay.torp +67 -0
  135. package/src/Calendar/CalendarTypes.ts +22 -0
  136. package/src/Calendar/index.ts +4 -0
  137. package/src/Dialog/Dialog.torp +3 -3
  138. package/src/Dialog/DialogBody.torp +2 -2
  139. package/src/Dialog/DialogButton.torp +2 -2
  140. package/src/Dialog/DialogFooter.torp +2 -2
  141. package/src/Dialog/DialogHeader.torp +2 -2
  142. package/src/Disclosure/Disclosure.torp +69 -0
  143. package/src/Disclosure/DisclosureContent.torp +36 -0
  144. package/src/Disclosure/DisclosureHeader.torp +34 -0
  145. package/src/Disclosure/DisclosureTrigger.torp +36 -0
  146. package/src/Disclosure/DisclosureTypes.ts +14 -0
  147. package/src/Disclosure/index.ts +6 -0
  148. package/{dist/Offscreen/Offscreen.torp → src/Drawer/Drawer.torp} +10 -19
  149. package/src/Drawer/index.ts +3 -0
  150. package/src/Menu/Menu.torp +160 -0
  151. package/src/Menu/MenuButton.torp +53 -0
  152. package/src/Menu/MenuCheck.torp +70 -0
  153. package/src/Menu/MenuGroup.torp +19 -0
  154. package/src/Menu/MenuIndicator.torp +29 -0
  155. package/src/Menu/MenuPopout.torp +71 -0
  156. package/src/Menu/MenuPopoutContent.torp +74 -0
  157. package/src/Menu/MenuPopoutTrigger.torp +117 -0
  158. package/src/Menu/MenuRadio.torp +88 -0
  159. package/src/Menu/MenuRadioGroup.torp +86 -0
  160. package/src/Menu/MenuSeparator.torp +14 -0
  161. package/src/Menu/MenuTypes.ts +58 -0
  162. package/src/Menu/index.ts +25 -0
  163. package/src/MenuBar/MenuBar.torp +150 -0
  164. package/src/MenuBar/MenuBarItem.torp +104 -0
  165. package/src/MenuBar/MenuBarItemContent.torp +108 -0
  166. package/src/MenuBar/MenuBarItemTrigger.torp +89 -0
  167. package/src/MenuBar/MenuBarTypes.ts +36 -0
  168. package/src/MenuBar/index.ts +6 -0
  169. package/src/NavMenu/NavMenu.torp +2 -2
  170. package/src/NavMenu/NavMenuButton.torp +2 -2
  171. package/src/NavMenu/NavMenuElement.torp +2 -2
  172. package/src/NavMenu/NavMenuGroup.torp +2 -2
  173. package/src/NavMenu/NavMenuGroupHeader.torp +2 -2
  174. package/src/NavMenu/NavMenuLink.torp +2 -2
  175. package/src/NavMenu/NavMenuPopout.torp +2 -2
  176. package/src/NavMenu/NavMenuPopoutButton.torp +3 -3
  177. package/src/NavMenu/NavMenuPopoutContent.torp +29 -27
  178. package/src/NavMenu/NavMenuSeparator.torp +2 -2
  179. package/src/Notification/Notification.torp +48 -0
  180. package/src/Notification/NotificationClose.torp +28 -0
  181. package/src/Notification/NotificationContainer.torp +14 -0
  182. package/src/Notification/NotificationContent.torp +13 -0
  183. package/src/Notification/NotificationTypes.ts +5 -0
  184. package/src/Notification/index.ts +13 -0
  185. package/src/Notification/showNotification.ts +30 -0
  186. package/src/Popover/Popover.torp +2 -2
  187. package/src/Popover/PopoverContent.torp +31 -28
  188. package/src/Popover/PopoverContentAnchored.torp +39 -38
  189. package/src/Popover/PopoverContentContextual.torp +39 -36
  190. package/src/Popover/PopoverOverlay.torp +3 -3
  191. package/src/Popover/PopoverTrigger.torp +2 -2
  192. package/src/Popover/PopoverTriggerContextual.torp +2 -2
  193. package/src/Popover/PopoverTriggerHover.torp +3 -3
  194. package/src/Slider/Slider.torp +83 -0
  195. package/src/Slider/index.ts +3 -0
  196. package/src/TabGroup/TabContent.torp +2 -2
  197. package/src/TabGroup/TabGroup.torp +2 -2
  198. package/src/TabGroup/TabHeader.torp +3 -3
  199. package/src/TabGroup/TabList.torp +2 -2
  200. package/src/TabGroup/TabTrigger.torp +2 -2
  201. package/src/icons/ArrowDown.torp +15 -0
  202. package/src/icons/ArrowLeft.torp +15 -0
  203. package/src/icons/ArrowRight.torp +15 -0
  204. package/src/icons/ArrowUp.torp +15 -0
  205. package/src/icons/Bars.torp +17 -0
  206. package/src/icons/Check.torp +15 -0
  207. package/src/icons/ChevronDown.torp +15 -0
  208. package/src/icons/ChevronLeft.torp +15 -0
  209. package/src/icons/ChevronRight.torp +15 -0
  210. package/src/icons/ChevronUp.torp +15 -0
  211. package/src/icons/Cross.torp +16 -0
  212. package/src/icons/Loading.torp +16 -0
  213. package/src/icons/Minus.torp +15 -0
  214. package/src/icons/Plus.torp +16 -0
  215. package/src/icons/index.ts +31 -0
  216. package/src/motion/fade.ts +2 -2
  217. package/src/motion/index.ts +3 -0
  218. package/src/utils/dateUtils.ts +244 -0
  219. package/src/utils/setPopoverPosition.ts +4 -0
  220. package/test/Breadcrumb/accessibility.test.ts +29 -0
  221. package/test/Breadcrumb/basic.test.ts +17 -0
  222. package/test/Breadcrumb/components/BreadcrumbTest.torp +15 -0
  223. package/test/Calendar/components/CalendarTest.torp +9 -0
  224. package/test/Calendar/default.test.ts +18 -0
  225. package/test/Dialog/components/DialogAccessibility.torp +5 -5
  226. package/test/Dialog/components/DialogFunction.torp +4 -6
  227. package/test/Dialog/components/DialogVariable.torp +19 -21
  228. package/test/Disclosure/accessibility.test.ts +30 -0
  229. package/test/Disclosure/components/DisclosureTest.torp +17 -0
  230. package/test/Disclosure/default.test.ts +15 -0
  231. package/test/Disclosure/toggle.test.ts +19 -0
  232. package/test/Menu/accessibility.test.ts +82 -0
  233. package/test/Menu/basic.test.ts +57 -0
  234. package/test/Menu/components/MenuAccessibility.torp +59 -0
  235. package/test/Menu/components/MenuBasic.torp +58 -0
  236. package/test/MenuBar/accessibility.test.ts +86 -0
  237. package/test/MenuBar/basic.test.ts +21 -0
  238. package/test/MenuBar/components/MenuBarAccessibility.torp +34 -0
  239. package/test/MenuBar/components/MenuBarBasic.torp +25 -0
  240. package/test/Notification/accessibility.test.ts +21 -0
  241. package/test/Notification/components/CustomNotification.torp +14 -0
  242. package/test/Notification/components/NotificationAccessibility.torp +14 -0
  243. package/test/Notification/components/NotificationFunction.torp +20 -0
  244. package/test/Notification/components/NotificationVariable.torp +22 -0
  245. package/test/Notification/function.test.ts +25 -0
  246. package/test/Notification/variable.test.ts +25 -0
  247. package/test/Popover/components/CustomDialog.torp +25 -0
  248. package/test/Popover/components/PromptFunction.torp +19 -0
  249. package/test/Popover/components/PromptVariable.torp +38 -0
  250. package/test/Popover/function.test.ts +26 -0
  251. package/test/Popover/variable.test.ts +27 -0
  252. package/tsup.config.ts +12 -4
  253. package/dist/Accordion/index.cjs +0 -53
  254. package/dist/Accordion/index.cjs.map +0 -1
  255. package/dist/Dialog/index.cjs +0 -53
  256. package/dist/Dialog/index.cjs.map +0 -1
  257. package/dist/NavMenu/index.cjs +0 -78
  258. package/dist/NavMenu/index.cjs.map +0 -1
  259. package/dist/Offscreen/index.cjs +0 -33
  260. package/dist/Offscreen/index.cjs.map +0 -1
  261. package/dist/Offscreen/index.js +0 -6
  262. package/dist/Popover/index.cjs +0 -222
  263. package/dist/Popover/index.cjs.map +0 -1
  264. package/dist/TabGroup/index.cjs +0 -53
  265. package/dist/TabGroup/index.cjs.map +0 -1
  266. package/dist/metafile-cjs.json +0 -1
  267. package/dist/motion.cjs +0 -34
  268. package/dist/motion.cjs.map +0 -1
  269. package/dist/motion.d.ts +0 -6
  270. package/dist/motion.js.map +0 -1
  271. package/src/Accordion.ts +0 -7
  272. package/src/Dialog.ts +0 -7
  273. package/src/NavMenu.ts +0 -23
  274. package/src/Offscreen/index.ts +0 -3
  275. package/src/Popover.ts +0 -29
  276. package/src/TabGroup.ts +0 -7
  277. package/src/motion.ts +0 -3
  278. /package/dist/{Offscreen → Breadcrumb}/index.js.map +0 -0
@@ -0,0 +1,86 @@
1
+ import {
2
+ type MenuRadioGroupContext,
3
+ MenuRadioGroupContextName,
4
+ type RadioGroupItemState
5
+ } from "./MenuTypes";
6
+
7
+ /**
8
+ * A container for a logical group of menu RadioGroup items.
9
+ *
10
+ * @prop {string} ariaLabel -- An ARIA label for describing the menu to screen readers
11
+ * @prop {string | undefined} value -- The value of the checked MenuRadio
12
+ * @prop {(any) => void} onchange - Called when the checked item has changed
13
+ */
14
+ export default function MenuRadioGroup() {
15
+ // A collection containing the state and some functions for each item
16
+ let itemStates: RadioGroupItemState[] = [];
17
+
18
+ // Set the context to pass down to items
19
+ let context: MenuRadioGroupContext = {
20
+ registerItemInGroup,
21
+ toggleItem
22
+ };
23
+ $context[MenuRadioGroupContextName] = context;
24
+
25
+ $mount(() => {
26
+ // Set the initial checked item
27
+ setValue($props.value);
28
+ });
29
+
30
+ function registerItemInGroup(itemValue: string, setChecked: (value: boolean) => void) {
31
+ // Add the item"s state to the array
32
+ const newItemState: RadioGroupItemState = {
33
+ value: itemValue,
34
+ checked: false,
35
+ setChecked
36
+ };
37
+ itemStates.push(newItemState);
38
+ itemStates = itemStates;
39
+ }
40
+
41
+ function setValue(newValue?: string) {
42
+ // No point doing this if there aren't any items yet (e.g. from the initial setValue() call)
43
+ if (!itemStates.length) {
44
+ return;
45
+ }
46
+
47
+ // Ensure the value passed in was a string (in case e.g. the user has set values to ints)
48
+ newValue = newValue?.toString();
49
+ for (let item of itemStates) {
50
+ item.checked = item.value === newValue;
51
+ item.setChecked(item.checked);
52
+ }
53
+
54
+ // Set the value
55
+ if ($props.value !== newValue) {
56
+ $props.value = newValue;
57
+
58
+ if ($props.onchange) {
59
+ $props.onchange($props.value);
60
+ }
61
+ }
62
+ }
63
+
64
+ function toggleItem(toggleValue: string) {
65
+ let newValue = "";
66
+ for (let item of itemStates) {
67
+ const checked = item.value === toggleValue;
68
+ item.setChecked(checked);
69
+ if (checked) {
70
+ newValue = item.value;
71
+ }
72
+ }
73
+ $props.value = newValue;
74
+ }
75
+
76
+ @render {
77
+ <div
78
+ {$props.id}
79
+ class={`torp-menu-radio-group ${$props.class ?? ""}`.trim()}
80
+ role="group"
81
+ aria-label={$props.ariaLabel}
82
+ >
83
+ <slot />
84
+ </div>
85
+ }
86
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * An element in a menu that separates logical sets of menu items.
3
+ */
4
+ export default function MenuSeparator() {
5
+ @render {
6
+ <div
7
+ {$props.id}
8
+ class={`torp-menu-separator ${$props.class ?? ""}`.trim()}
9
+ role="separator"
10
+ >
11
+ <slot />
12
+ </div>
13
+ }
14
+ }
@@ -0,0 +1,58 @@
1
+ export const MenuContextName = "Menu";
2
+ export const MenuButtonContextName = "MenuButton";
3
+ export const MenuRadioGroupContextName = "MenuRadioGroup";
4
+ export const MenuPopoutContextName = "MenuPopout";
5
+
6
+ export interface MenuContext {
7
+ setVisible: (value: boolean, result?: any) => void;
8
+
9
+ /** Called from a MenuButton to close the Menu and return a result */
10
+ handleTrigger: (type: "confirm" | "cancel" | undefined, value?: any) => void;
11
+
12
+ /** Called from a MenuButton when its button is focused */
13
+ handleItemFocus: (index: number) => void;
14
+
15
+ /** Called from a MenuButton when a key is pressed */
16
+ handleItemKey: (e: KeyboardEvent) => void;
17
+
18
+ state: {
19
+ /** Whether the Menu is visible. Writable so that it can be subscribed to by children */
20
+ visible: boolean;
21
+ };
22
+
23
+ /** The element that caused this Menu to be shown */
24
+ anchorElement?: HTMLElement;
25
+
26
+ /** Called from each MenuButton/Check/Radio to register itself with this Menu */
27
+ registerItem: (setFocused: () => void) => { index: number };
28
+ }
29
+
30
+ export interface MenuButtonContext {
31
+ state: {
32
+ checked: boolean;
33
+ };
34
+ }
35
+
36
+ export interface MenuRadioGroupContext {
37
+ registerItemInGroup: (value: string, setChecked: (value: boolean) => void) => void;
38
+ toggleItem: (value: string) => void;
39
+ }
40
+
41
+ export interface MenuPopoutContext {
42
+ setVisible: (value: boolean) => void;
43
+ state: {
44
+ visible: boolean;
45
+ };
46
+ anchorElement?: HTMLElement;
47
+ focusFirstElement?: () => void;
48
+ }
49
+
50
+ export interface ItemState {
51
+ setFocused: () => void;
52
+ }
53
+
54
+ export interface RadioGroupItemState {
55
+ value: string;
56
+ checked: boolean;
57
+ setChecked: (value: boolean) => void;
58
+ }
@@ -0,0 +1,25 @@
1
+ import Menu from "./Menu.torp";
2
+ import MenuButton from "./MenuButton.torp";
3
+ import MenuCheck from "./MenuCheck.torp";
4
+ import MenuGroup from "./MenuGroup.torp";
5
+ import MenuIndicator from "./MenuIndicator.torp";
6
+ import MenuPopout from "./MenuPopout.torp";
7
+ import MenuPopoutContent from "./MenuPopoutContent.torp";
8
+ import MenuPopoutTrigger from "./MenuPopoutTrigger.torp";
9
+ import MenuRadio from "./MenuRadio.torp";
10
+ import MenuRadioGroup from "./MenuRadioGroup.torp";
11
+ import MenuSeparator from "./MenuSeparator.torp";
12
+
13
+ export {
14
+ Menu,
15
+ MenuGroup,
16
+ MenuButton,
17
+ MenuCheck,
18
+ MenuRadio,
19
+ MenuRadioGroup,
20
+ MenuIndicator,
21
+ MenuPopout,
22
+ MenuPopoutTrigger,
23
+ MenuPopoutContent,
24
+ MenuSeparator,
25
+ };
@@ -0,0 +1,150 @@
1
+ import { MenuBarContextName, type MenuBarContext, type ItemState } from "./MenuBarTypes";
2
+
3
+ /**
4
+ * A menu bar, similar to that shown at the top of Windows applications or at the top of the screen on Macs.
5
+ *
6
+ * See [the WAI ARIA guidelines for Menus](https://www.w3.org/WAI/ARIA/apg/patterns/menu/).
7
+ *
8
+ * The structure of a MenuBar is:
9
+ *
10
+ * ```
11
+ * <MenuBar>
12
+ * <MenuBarItem>
13
+ * <MenuBarItemTrigger />
14
+ * <MenuBarItemContent>
15
+ * <Menu />
16
+ * </MenuBarItemContent>
17
+ * </MenuBarItem>
18
+ * </MenuBar>
19
+ * ```
20
+ *
21
+ * @prop {string} ariaLabel -- An ARIA label for describing the menu to screen readers
22
+ * @prop {"horizontal" | "vertical"} orientation -- The orientation of the menu, which affects keyboard navigation
23
+ */
24
+ export default function MenuBar() {
25
+ $props.orientation ??= "horizontal";
26
+
27
+ let $state = $watch({
28
+ active: false,
29
+ });
30
+
31
+ // A collection containing the state and some functions for each item
32
+ let itemStates: ItemState[] = [];
33
+ let focusIndex = -1;
34
+
35
+ // Set the context to pass down to items
36
+ let context: MenuBarContext = {
37
+ handleItemShow,
38
+ handleButtonFocus,
39
+ handleButtonKey,
40
+ registerItem,
41
+ state: $state
42
+ };
43
+ $context[MenuBarContextName] = context;
44
+
45
+ function registerItem(setVisible: (value: boolean) => void, setFocused: () => void) {
46
+ // Add the item"s state to the array and return its index
47
+ const newItemState: ItemState = {
48
+ setVisible,
49
+ setFocused
50
+ };
51
+ itemStates.push(newItemState);
52
+ itemStates = itemStates;
53
+ return { index: itemStates.length - 1 };
54
+ }
55
+
56
+ function handleItemShow(index: number) {
57
+ // Make sure other items are hidden
58
+ itemStates.forEach((item, i) => {
59
+ if (i !== index) {
60
+ item.setVisible(false);
61
+ }
62
+ });
63
+ }
64
+
65
+ function handleButtonFocus(index: number) {
66
+ focusIndex = index;
67
+ }
68
+
69
+ function handleButtonKey(e: KeyboardEvent) {
70
+ switch (e.key) {
71
+ case "Right":
72
+ case "ArrowRight": {
73
+ if (orientation === "horizontal") {
74
+ e.preventDefault();
75
+ focusItem("next");
76
+ }
77
+ break;
78
+ }
79
+ case "Left":
80
+ case "ArrowLeft": {
81
+ if (orientation === "horizontal") {
82
+ e.preventDefault();
83
+ focusItem("previous");
84
+ }
85
+ break;
86
+ }
87
+ case "Down":
88
+ case "ArrowDown": {
89
+ if (orientation === "vertical") {
90
+ e.preventDefault();
91
+ focusItem("next");
92
+ }
93
+ break;
94
+ }
95
+ case "Up":
96
+ case "ArrowUp": {
97
+ if (orientation === "vertical") {
98
+ e.preventDefault();
99
+ focusItem("previous");
100
+ }
101
+ break;
102
+ }
103
+ case "Home": {
104
+ e.preventDefault();
105
+ focusItem("start");
106
+ break;
107
+ }
108
+ case "End": {
109
+ e.preventDefault();
110
+ focusItem("end");
111
+ break;
112
+ }
113
+ }
114
+ }
115
+
116
+ function focusItem(target: "start" | "previous" | "next" | "end") {
117
+ switch (target) {
118
+ case "start": {
119
+ focusIndex = 0;
120
+ break;
121
+ }
122
+ case "previous": {
123
+ focusIndex = Math.max(0, focusIndex - 1);
124
+ break;
125
+ }
126
+ case "next": {
127
+ focusIndex = Math.min(itemStates.length - 1, focusIndex + 1);
128
+ break;
129
+ }
130
+ case "end": {
131
+ focusIndex = itemStates.length - 1;
132
+ break;
133
+ }
134
+ }
135
+ itemStates[focusIndex]?.setFocused();
136
+ }
137
+
138
+ @render {
139
+ <div
140
+ {$props.id}
141
+ class={`torp-menu-bar ${$props.class ?? ""}`.trim()}
142
+ tabindex="-1"
143
+ role="menubar"
144
+ aria-label={$props.ariaLabel}
145
+ aria-orientation={$props.orientation}
146
+ >
147
+ <slot />
148
+ </div>
149
+ }
150
+ }
@@ -0,0 +1,104 @@
1
+ import {
2
+ MenuBarContextName,
3
+ MenuBarItemContextName,
4
+ type MenuBarContext,
5
+ type MenuBarItemContext
6
+ } from "./MenuBarTypes";
7
+ import { PopoverContextName, type PopoverContext } from "../Popover/PopoverTypes";
8
+
9
+ /**
10
+ * A popout that is attached to a menubar button and can be used to show a submenu.
11
+ *
12
+ * The structure of a MenuBarItem is:
13
+ *
14
+ * ```
15
+ * <MenuBarItem>
16
+ * <MenuBarItemTrigger /> (optional)
17
+ * <MenuBarItemContent />
18
+ * </MenuBarItem>
19
+ * ```
20
+ *
21
+ * @prop {boolean} visible -- Whether the item is visible
22
+ * @prop {(boolean) => void} ontoggle -- Called when the item is opened or closed
23
+ * @prop {() => void} onopen -- Called when the item has opened
24
+ * @prop {() => void} onclose -- Called when the item has closed
25
+ */
26
+ export default function MenuBarItem() {
27
+ $props.visible ??= false;
28
+
29
+ let $state = $watch({
30
+ visible: $props.visible
31
+ });
32
+
33
+ let result: any;
34
+
35
+ // Get stuff out of the MenuBarContext
36
+ const menubarContext = $context[MenuBarContextName] as MenuBarContext;
37
+ if (!menubarContext) {
38
+ throw new Error("MenuBarItem must be contained within a MenuBar");
39
+ }
40
+ const { handleItemShow } = menubarContext;
41
+
42
+ // Set the context to pass down to items
43
+ let context: MenuBarItemContext = {
44
+ setVisible,
45
+ state: $state,
46
+ index: -1
47
+ };
48
+ $context[MenuBarItemContextName] = context;
49
+
50
+ // Set the context to pass down to items
51
+ let popoverContext: PopoverContext = {
52
+ setVisible,
53
+ handleButton,
54
+ modal: false,
55
+ state: $state,
56
+ //anchorElement: anchor,
57
+ };
58
+ $context[PopoverContextName] = popoverContext;
59
+
60
+ $mount(() => {
61
+ setVisible();
62
+ });
63
+
64
+ function setVisible(value: boolean) {
65
+ if (!value || menubarContext.state.active) {
66
+ $state.visible = value;
67
+ if (value) {
68
+ handleItemShow(context.index);
69
+ }
70
+ }
71
+ }
72
+
73
+ let firstToggle = true;
74
+ $run(() => {
75
+ const ontoggle = $props.ontoggle;
76
+ const onopen = $props.onopen;
77
+ const onclose = $props.onclose;
78
+ const visible = $state.visible;
79
+ if (firstToggle) {
80
+ firstToggle = false;
81
+ } else {
82
+ if (ontoggle) ontoggle(visible);
83
+ if (visible) {
84
+ if (onopen) onopen()
85
+ } else {
86
+ if (onclose) onclose(result);
87
+ }
88
+ }
89
+ });
90
+
91
+ function handleButton(type: "confirm" | "cancel" | undefined, value?: any) {
92
+ menubarContext.state.active = false;
93
+ setVisible(false);
94
+ }
95
+
96
+ @render {
97
+ <div
98
+ {$props.id}
99
+ class={`torp-menu-bar-item ${$props.class ?? ""}`.trim()}
100
+ >
101
+ <slot />
102
+ </div>
103
+ }
104
+ }
@@ -0,0 +1,108 @@
1
+ import { addDocumentEvent, removeDocumentEvent } from "../utils/documentEvents";
2
+ import setPopoverPosition from "../utils/setPopoverPosition";
3
+ import {
4
+ MenuBarContextName,
5
+ MenuBarItemContextName,
6
+ type MenuBarContext,
7
+ type MenuBarItemContext
8
+ } from "./MenuBarTypes";
9
+
10
+ /**
11
+ * The content that is displayed for a MenuBarItem when it is open.
12
+ *
13
+ * @prop {"top" | "right" | "bottom" | "left"} side -- Where the popout will be shown, relative to the popout button
14
+ * @prop {"start" | "center" | "end"} alignment -- How the popout will be aligned, relative to the popout button
15
+ */
16
+ export default function MenuBarItemContent() {
17
+ $props.side ??= "bottom";
18
+ $props.alignment ??= "start";
19
+
20
+ let div: HTMLDivElement;
21
+ let closeOnClick = false;
22
+ let shown = false;
23
+
24
+ // Get stuff out of the MenuBarContext
25
+ const context = $context[MenuBarContextName] as MenuBarContext;
26
+ if (!context) {
27
+ throw new Error("MenuBarItemContent must be contained within a MenuBar");
28
+ }
29
+
30
+ // Get stuff out of the MenuBarItemContext
31
+ const itemContext = $context[MenuBarItemContextName] as MenuBarItemContext;
32
+ if (!itemContext) {
33
+ throw new Error("MenuBarItemContent must be contained within a MenuBarItem");
34
+ }
35
+ const { state: $state, setVisible } = itemContext;
36
+
37
+ $run(() => {
38
+ if (!$state.visible) {
39
+ hide();
40
+ }
41
+ });
42
+
43
+ function show() {
44
+ shown = true;
45
+
46
+ // HACK: Because this Contextual could be shown with a click, we need to wait for another mousedown
47
+ // before we close on click. Otherwise the click immediately bubbles to the document. I guess
48
+ // we could require the user to preventDefault after calling showContextual, but that seems very
49
+ // error-prone?
50
+ closeOnClick = false;
51
+ addDocumentEvent("mousedown", handleDocumentMouseDown);
52
+ addDocumentEvent("click", handleDocumentClick);
53
+
54
+ // Set the position and listen for window resize and scroll to reset the position
55
+ setPosition();
56
+ addEventListener("resize", setPosition);
57
+ addEventListener("scroll", setPosition);
58
+ if (itemContext.focusFirstElement) {
59
+ itemContext.focusFirstElement();
60
+ }
61
+ }
62
+
63
+ function hide() {
64
+ if (!shown) return;
65
+
66
+ removeDocumentEvent("mousedown", handleDocumentMouseDown);
67
+ removeDocumentEvent("click", handleDocumentClick);
68
+
69
+ // Stop listening for window resize and focus the anchor element per WAI guidelines
70
+ removeEventListener("resize", setPosition);
71
+ removeEventListener("scroll", setPosition);
72
+ if (itemContext.anchorElement?.focus) {
73
+ itemContext.anchorElement.focus();
74
+ }
75
+ }
76
+
77
+ function handleDocumentMouseDown() {
78
+ closeOnClick = true;
79
+ }
80
+
81
+ function handleDocumentClick(e: MouseEvent) {
82
+ if (closeOnClick && !div.contains(e.target as HTMLElement)) {
83
+ context.active = false;
84
+ setVisible(false);
85
+ }
86
+ }
87
+
88
+ function setPosition() {
89
+ if (!itemContext.anchorElement) {
90
+ throw new Error("MenuBarItemContent must have an anchor element");
91
+ }
92
+
93
+ setPopoverPosition(itemContext.anchorElement, div, $props.side, $props.alignment);
94
+ }
95
+
96
+ @render {
97
+ @if ($state.visible) {
98
+ <div
99
+ {$props.id}
100
+ class={`torp-menu-bar-item-content ${$props.class ?? ""}`.trim()}
101
+ &ref={div}
102
+ onmount={show}
103
+ >
104
+ <slot />
105
+ </div>
106
+ }
107
+ }
108
+ }
@@ -0,0 +1,89 @@
1
+ import {
2
+ MenuBarContextName,
3
+ MenuBarItemContextName,
4
+ type MenuBarContext,
5
+ type MenuBarItemContext
6
+ } from "./MenuBarTypes";
7
+
8
+ /**
9
+ * A button that is placed in a MenuBar and opens a submenu.
10
+ *
11
+ * @prop {boolean} disabled -- Whether this button is disabled
12
+ */
13
+ export default function MenuBarItemTrigger() {
14
+ $props.disabled ??= false;
15
+
16
+ let button: HTMLButtonElement;
17
+
18
+ // Get stuff out of the MenuBarContext
19
+ const menubarContext = $context[MenuBarContextName] as MenuBarContext;
20
+ if (!menubarContext) {
21
+ throw new Error("MenuBarItemTrigger must be contained within a MenuBar");
22
+ }
23
+ const { registerItem, handleButtonKey, handleButtonFocus } = menubarContext;
24
+
25
+ // Get stuff out of the MenuBarItemContext
26
+ const itemContext = $context[MenuBarItemContextName] as MenuBarItemContext;
27
+ if (!itemContext) {
28
+ throw new Error("MenuBarItemTrigger must be contained within a MenuBarItem");
29
+ }
30
+ const { state: $state, setVisible } = itemContext;
31
+
32
+ // Register this item with the parent MenuBar and store the index
33
+ const { index } = registerItem(setVisible, setFocused);
34
+
35
+ $mount(() => {
36
+ // Set the anchor element to the button when it exists
37
+ //itemContext.anchorElement = button;
38
+ itemContext.index = index;
39
+ });
40
+
41
+ function setFocused() {
42
+ button.focus();
43
+ }
44
+
45
+ function handleClick() {
46
+ menubarContext.state.active = !menubarContext.state.active;
47
+ setVisible(menubarContext.state.active);
48
+ }
49
+
50
+ function handleKey(e: KeyboardEvent) {
51
+ switch (e.key) {
52
+ case "Down":
53
+ case "ArrowDown":
54
+ // TODO: This should be based on what side the menu shows on
55
+ e.preventDefault();
56
+ menubarContext.state.active = true;
57
+ setVisible(true);
58
+ break;
59
+ default:
60
+ handleButtonKey(e);
61
+ }
62
+ }
63
+
64
+ function handleMouseEnter() {
65
+ setVisible(true);
66
+ }
67
+
68
+ @render {
69
+ <button
70
+ {$props.id}
71
+ class={`torp-menu-bar-item-button ${$props.class ?? ""}`.trim()}
72
+ type="button"
73
+ {$props.disabled}
74
+ role="menuitem"
75
+ aria-haspopup="true"
76
+ aria-expanded={($state.visible ?? false).toString()}
77
+ aria-disabled={$props.disabled ? "true" : undefined}
78
+ data-disabled={$props.disabled ? "true" : undefined}
79
+ onkeydown={handleKey}
80
+ onclick={handleClick}
81
+ onfocus={() => handleButtonFocus(index)}
82
+ onmouseenter={handleMouseEnter}
83
+ &ref={button}
84
+ onmount={(el) => itemContext.anchorElement = el}
85
+ >
86
+ <slot />
87
+ </button>
88
+ }
89
+ }
@@ -0,0 +1,36 @@
1
+ export const MenuBarContextName = "MenuBar";
2
+ export const MenuBarItemContextName = "MenuBarItem";
3
+
4
+ export interface MenuBarContext {
5
+ /** Called from a MenuBarItem when it is shown, to hide others */
6
+ handleItemShow: (index: number) => void;
7
+
8
+ /** Called from a MenuBarButton when its button is focused */
9
+ handleButtonFocus: (index: number) => void;
10
+
11
+ /** Called from a MenuBarButton when a key is pressed */
12
+ handleButtonKey: (e: KeyboardEvent) => void;
13
+
14
+ /** Called from each MenuBarButton to register itself with this MenuBar */
15
+ registerItem: (setVisible: (value: boolean) => void, setFocused: () => void) => { index: number };
16
+
17
+ state: {
18
+ /** Whether a menubar item is showing its content. If so, changing to another item should show its content */
19
+ active: boolean;
20
+ };
21
+ }
22
+
23
+ export interface MenuBarItemContext {
24
+ setVisible: (value: boolean) => void;
25
+ state: {
26
+ visible: boolean;
27
+ };
28
+ index: number;
29
+ anchorElement?: HTMLElement;
30
+ focusFirstElement?: () => void;
31
+ }
32
+
33
+ export interface ItemState {
34
+ setVisible: (value: boolean) => void;
35
+ setFocused: () => void;
36
+ }
@@ -0,0 +1,6 @@
1
+ import MenuBar from "./MenuBar.torp";
2
+ import MenuBarItem from "./MenuBarItem.torp";
3
+ import MenuBarItemContent from "./MenuBarItemContent.torp";
4
+ import MenuBarItemTrigger from "./MenuBarItemTrigger.torp";
5
+
6
+ export { MenuBar, MenuBarItem, MenuBarItemTrigger, MenuBarItemContent };
@@ -164,7 +164,7 @@ export default function NavMenu($props: any) {
164
164
 
165
165
  @render {
166
166
  <nav
167
- id={$props.id}
167
+ {$props.id}
168
168
  class={`torp-nav-menu ${$props.class ?? ""}`.trim()}
169
169
  tabindex="0"
170
170
  role="menubar"
@@ -172,7 +172,7 @@ export default function NavMenu($props: any) {
172
172
  aria-orientation={$props.orientation}
173
173
  onfocus={handleFocus}
174
174
  >
175
- <:slot />
175
+ <slot />
176
176
  </nav>
177
177
  }
178
178
  }