@geoffcox/sterling-svelte 2.0.2 → 2.0.4

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 (133) hide show
  1. package/README.md +18 -0
  2. package/dist/@types/clickOutside.d.ts +15 -0
  3. package/dist/Button.svelte +29 -0
  4. package/dist/Button.svelte.d.ts +8 -0
  5. package/dist/Callout.svelte +243 -0
  6. package/dist/Callout.svelte.d.ts +14 -0
  7. package/dist/Callout.types.d.ts +11 -0
  8. package/dist/Callout.types.js +1 -0
  9. package/dist/Checkbox.svelte +62 -0
  10. package/dist/Checkbox.svelte.d.ts +9 -0
  11. package/dist/Dialog.svelte +201 -0
  12. package/dist/Dialog.svelte.d.ts +14 -0
  13. package/dist/Dropdown.svelte +159 -0
  14. package/dist/Dropdown.svelte.d.ts +23 -0
  15. package/dist/Input.svelte +80 -0
  16. package/dist/Input.svelte.d.ts +11 -0
  17. package/dist/Label.constants.d.ts +2 -0
  18. package/dist/Label.constants.js +2 -0
  19. package/dist/Label.svelte +135 -0
  20. package/dist/Label.svelte.d.ts +17 -0
  21. package/dist/Link.svelte +31 -0
  22. package/dist/Link.svelte.d.ts +11 -0
  23. package/dist/List.constants.d.ts +1 -0
  24. package/dist/List.constants.js +1 -0
  25. package/dist/List.svelte +258 -0
  26. package/dist/List.svelte.d.ts +19 -0
  27. package/dist/List.types.d.ts +5 -0
  28. package/dist/List.types.js +1 -0
  29. package/dist/ListItem.svelte +64 -0
  30. package/dist/ListItem.svelte.d.ts +12 -0
  31. package/dist/Menu.svelte +105 -0
  32. package/dist/Menu.svelte.d.ts +12 -0
  33. package/dist/MenuBar.constants.d.ts +1 -0
  34. package/dist/MenuBar.constants.js +1 -0
  35. package/dist/MenuBar.svelte +144 -0
  36. package/dist/MenuBar.svelte.d.ts +12 -0
  37. package/dist/MenuBar.types.d.ts +4 -0
  38. package/dist/MenuBar.types.js +1 -0
  39. package/dist/MenuButton.svelte +156 -0
  40. package/dist/MenuButton.svelte.d.ts +20 -0
  41. package/dist/MenuItem.constants.d.ts +2 -0
  42. package/dist/MenuItem.constants.js +2 -0
  43. package/dist/MenuItem.svelte +431 -0
  44. package/dist/MenuItem.svelte.d.ts +22 -0
  45. package/dist/MenuItem.types.d.ts +20 -0
  46. package/dist/MenuItem.types.js +1 -0
  47. package/dist/MenuItem.utils.d.ts +7 -0
  48. package/dist/MenuItem.utils.js +36 -0
  49. package/dist/MenuSeparator.svelte +11 -0
  50. package/dist/MenuSeparator.svelte.d.ts +5 -0
  51. package/dist/Pagination.svelte +267 -0
  52. package/dist/Pagination.svelte.d.ts +4 -0
  53. package/dist/Pagination.types.d.ts +24 -0
  54. package/dist/Pagination.types.js +1 -0
  55. package/dist/Popover.constants.d.ts +1 -0
  56. package/dist/Popover.constants.js +14 -0
  57. package/dist/Popover.svelte +175 -0
  58. package/dist/Popover.svelte.d.ts +14 -0
  59. package/dist/Popover.types.d.ts +4 -0
  60. package/dist/Popover.types.js +1 -0
  61. package/dist/Portal.constants.d.ts +2 -0
  62. package/dist/Portal.constants.js +2 -0
  63. package/dist/Portal.types.d.ts +3 -0
  64. package/dist/Portal.types.js +1 -0
  65. package/dist/Progress.constants.d.ts +1 -0
  66. package/dist/Progress.constants.js +1 -0
  67. package/dist/Progress.svelte +61 -0
  68. package/dist/Progress.svelte.d.ts +11 -0
  69. package/dist/Progress.types.d.ts +4 -0
  70. package/dist/Progress.types.js +1 -0
  71. package/dist/Radio.svelte +65 -0
  72. package/dist/Radio.svelte.d.ts +12 -0
  73. package/dist/Select.svelte +262 -0
  74. package/dist/Select.svelte.d.ts +26 -0
  75. package/dist/Slider.svelte +182 -0
  76. package/dist/Slider.svelte.d.ts +18 -0
  77. package/dist/Switch.svelte +92 -0
  78. package/dist/Switch.svelte.d.ts +21 -0
  79. package/dist/Tab.svelte +66 -0
  80. package/dist/Tab.svelte.d.ts +11 -0
  81. package/dist/TabList.constants.d.ts +1 -0
  82. package/dist/TabList.constants.js +1 -0
  83. package/dist/TabList.svelte +253 -0
  84. package/dist/TabList.svelte.d.ts +17 -0
  85. package/dist/TabList.types.d.ts +5 -0
  86. package/dist/TabList.types.js +1 -0
  87. package/dist/TextArea.constants.d.ts +1 -0
  88. package/dist/TextArea.constants.js +1 -0
  89. package/dist/TextArea.svelte +116 -0
  90. package/dist/TextArea.svelte.d.ts +18 -0
  91. package/dist/TextArea.types.d.ts +4 -0
  92. package/dist/TextArea.types.js +1 -0
  93. package/dist/Tooltip.svelte +95 -0
  94. package/dist/Tooltip.svelte.d.ts +10 -0
  95. package/dist/Tree.constants.d.ts +1 -0
  96. package/dist/Tree.constants.js +1 -0
  97. package/dist/Tree.svelte +81 -0
  98. package/dist/Tree.svelte.d.ts +14 -0
  99. package/dist/Tree.types.d.ts +5 -0
  100. package/dist/Tree.types.js +1 -0
  101. package/dist/TreeChevron.svelte +39 -0
  102. package/dist/TreeChevron.svelte.d.ts +8 -0
  103. package/dist/TreeItem.constants.d.ts +1 -0
  104. package/dist/TreeItem.constants.js +1 -0
  105. package/dist/TreeItem.svelte +396 -0
  106. package/dist/TreeItem.svelte.d.ts +22 -0
  107. package/dist/TreeItem.types.d.ts +4 -0
  108. package/dist/TreeItem.types.js +1 -0
  109. package/dist/actions/applyLightDarkMode.d.ts +11 -0
  110. package/dist/actions/applyLightDarkMode.js +37 -0
  111. package/dist/actions/clickOutside.d.ts +15 -0
  112. package/dist/actions/clickOutside.js +28 -0
  113. package/dist/actions/colorScheme.d.ts +8 -0
  114. package/dist/actions/colorScheme.js +26 -0
  115. package/dist/actions/extraClass.d.ts +9 -0
  116. package/dist/actions/extraClass.js +15 -0
  117. package/dist/actions/forwardEvents.d.ts +12 -0
  118. package/dist/actions/forwardEvents.js +32 -0
  119. package/dist/actions/portal.d.ts +8 -0
  120. package/dist/actions/portal.js +19 -0
  121. package/dist/actions/trapKeyboardFocus.d.ts +3 -0
  122. package/dist/actions/trapKeyboardFocus.js +52 -0
  123. package/dist/idGenerator.d.ts +5 -0
  124. package/dist/idGenerator.js +11 -0
  125. package/dist/index.d.ts +61 -0
  126. package/dist/index.js +56 -0
  127. package/dist/mediaQueries/prefersColorSchemeDark.d.ts +1 -0
  128. package/dist/mediaQueries/prefersColorSchemeDark.js +14 -0
  129. package/dist/mediaQueries/prefersReducedMotion.d.ts +1 -0
  130. package/dist/mediaQueries/prefersReducedMotion.js +14 -0
  131. package/dist/mediaQueries/usingKeyboard.d.ts +1 -0
  132. package/dist/mediaQueries/usingKeyboard.js +17 -0
  133. package/package.json +17 -13
@@ -0,0 +1,156 @@
1
+ <svelte:options runes={true} />
2
+
3
+ <script lang="ts">
4
+ import { setContext, tick, type Snippet } from 'svelte';
5
+ import type { HTMLButtonAttributes } from 'svelte/elements';
6
+ import { clickOutside } from './actions/clickOutside';
7
+ import Button from './Button.svelte';
8
+ import Menu from './Menu.svelte';
9
+ import { MENU_ITEM_CONTEXT_KEY } from './MenuItem.constants';
10
+ import type { MenuItemContext } from './MenuItem.types';
11
+ import Popover from './Popover.svelte';
12
+ import type { PopoverPlacement } from './Popover.types';
13
+
14
+ const uuid = $props.id();
15
+
16
+ type Props = HTMLButtonAttributes & {
17
+ items: Snippet;
18
+ menuClass?: string;
19
+ onOpen?: (value: string) => void;
20
+ onClose?: (value: string) => void;
21
+ onSelect?: (value: string) => void;
22
+ open?: boolean | null | undefined;
23
+ popoverPlacement?: PopoverPlacement;
24
+ value?: string;
25
+ };
26
+
27
+ let {
28
+ children,
29
+ class: _class,
30
+ items,
31
+ menuClass,
32
+ open = $bindable(false),
33
+ onClose,
34
+ onOpen,
35
+ onSelect,
36
+ popoverPlacement = 'bottom-start',
37
+ value,
38
+ ...rest
39
+ }: Props = $props();
40
+
41
+ const instanceId = `MenuButton-${uuid}`;
42
+
43
+ let buttonRef: Button;
44
+ let openValues: string[] = $state([]);
45
+ let menuRef: Menu;
46
+ let menuId = $derived(`${value}-menu-${instanceId}`);
47
+ let prevOpen = $state(open);
48
+ let reference: HTMLDivElement | undefined = $state();
49
+
50
+ export const click = () => {
51
+ buttonRef?.click();
52
+ };
53
+
54
+ export const blur = () => {
55
+ buttonRef?.blur();
56
+ };
57
+
58
+ export const focus = (options?: FocusOptions) => {
59
+ buttonRef?.focus(options);
60
+ };
61
+
62
+ // update open based on openValues
63
+ $effect(() => {
64
+ open = openValues.length > 0;
65
+ });
66
+
67
+ // update openValues based on open
68
+ $effect(() => {
69
+ if (open) {
70
+ openValues = openValues.length > 0 ? openValues : ['menu-button'];
71
+ } else {
72
+ openValues = openValues.length === 0 ? openValues : [];
73
+ }
74
+ });
75
+
76
+ // focus when closing
77
+ $effect(() => {
78
+ if (!open && open !== prevOpen) {
79
+ focus();
80
+ }
81
+ prevOpen = open;
82
+ });
83
+
84
+ const onClick = async () => {
85
+ if (!open) {
86
+ openValues = ['menu-button'];
87
+ open = true;
88
+ await tick();
89
+ menuRef?.focusFirstMenuItem();
90
+ } else {
91
+ open = false;
92
+ openValues = [];
93
+ }
94
+ };
95
+
96
+ const closeAllMenus = () => {
97
+ openValues = [];
98
+ open = false;
99
+ };
100
+
101
+ const onClickOutside = (event: MouseEvent) => {
102
+ let element: HTMLElement | null = event.target as HTMLElement;
103
+ while (element) {
104
+ if (element.getAttribute('data-root-value') === value) {
105
+ return;
106
+ }
107
+ element = element.parentElement;
108
+ }
109
+ closeAllMenus?.();
110
+ };
111
+
112
+ // ----- Context ----- //
113
+
114
+ setContext<MenuItemContext>(MENU_ITEM_CONTEXT_KEY, {
115
+ depth: 1,
116
+ get openValues() {
117
+ return openValues;
118
+ },
119
+ set openValues(value: string[]) {
120
+ openValues = value;
121
+ },
122
+ rootValue: value,
123
+ closeContainingMenu: () => {
124
+ open = false;
125
+ },
126
+ onOpen,
127
+ onClose,
128
+ onSelect
129
+ });
130
+ </script>
131
+
132
+ <Button
133
+ bind:this={buttonRef}
134
+ aria-controls={menuId}
135
+ aria-expanded={!!open}
136
+ aria-haspopup={!!children}
137
+ aria-owns={menuId}
138
+ class={['sterling-menu-button', _class]}
139
+ data-value={value}
140
+ data-root-value={value}
141
+ {...rest}
142
+ onclick={onClick}
143
+ >
144
+ <div
145
+ class="reference"
146
+ bind:this={reference}
147
+ use:clickOutside={{ onclickoutside: onClickOutside }}
148
+ >
149
+ {@render children?.()}
150
+ </div>
151
+ <Popover {reference} {open} placement={popoverPlacement}>
152
+ <Menu bind:this={menuRef} id={menuId} class={menuClass}>
153
+ {@render items?.()}
154
+ </Menu>
155
+ </Popover>
156
+ </Button>
@@ -0,0 +1,20 @@
1
+ import { type Snippet } from 'svelte';
2
+ import type { HTMLButtonAttributes } from 'svelte/elements';
3
+ import type { PopoverPlacement } from './Popover.types';
4
+ type Props = HTMLButtonAttributes & {
5
+ items: Snippet;
6
+ menuClass?: string;
7
+ onOpen?: (value: string) => void;
8
+ onClose?: (value: string) => void;
9
+ onSelect?: (value: string) => void;
10
+ open?: boolean | null | undefined;
11
+ popoverPlacement?: PopoverPlacement;
12
+ value?: string;
13
+ };
14
+ declare const MenuButton: import("svelte").Component<Props, {
15
+ click: () => void;
16
+ blur: () => void;
17
+ focus: (options?: FocusOptions) => void;
18
+ }, "open">;
19
+ type MenuButton = ReturnType<typeof MenuButton>;
20
+ export default MenuButton;
@@ -0,0 +1,2 @@
1
+ export declare const MENU_ITEM_CONTEXT_KEY = "sterlingMenuItem";
2
+ export declare const MENU_ITEM_ROLES: readonly ["menuitem", "menuitemcheckbox", "menuitemradio"];
@@ -0,0 +1,2 @@
1
+ export const MENU_ITEM_CONTEXT_KEY = 'sterlingMenuItem';
2
+ export const MENU_ITEM_ROLES = ['menuitem', 'menuitemcheckbox', 'menuitemradio'];
@@ -0,0 +1,431 @@
1
+ <svelte:options runes={true} />
2
+
3
+ <script lang="ts">
4
+ import { getContext, setContext, tick, type Snippet } from 'svelte';
5
+ import type {
6
+ HTMLButtonAttributes,
7
+ KeyboardEventHandler,
8
+ MouseEventHandler
9
+ } from 'svelte/elements';
10
+ import Menu from './Menu.svelte';
11
+ import { MENU_BAR_CONTEXT_KEY } from './MenuBar.constants';
12
+ import type { MenuBarContext } from './MenuBar.types';
13
+ import { MENU_ITEM_CONTEXT_KEY } from './MenuItem.constants';
14
+ import type { MenuItemContext, MenuItemRole } from './MenuItem.types';
15
+ import { isElementEnabledMenuItem } from './MenuItem.utils';
16
+ import Popover from './Popover.svelte';
17
+ import { usingKeyboard } from './mediaQueries/usingKeyboard';
18
+
19
+ const uuid = $props.id();
20
+
21
+ type Props = HTMLButtonAttributes & {
22
+ checked?: boolean | null;
23
+ item?: Snippet;
24
+ menuClass?: string;
25
+ onClose?: (value: string) => void;
26
+ onOpen?: (value: string) => void;
27
+ onSelect?: (value: string) => void;
28
+ role?: MenuItemRole;
29
+ shortcut?: string | Snippet;
30
+ text?: string | Snippet;
31
+ value: string;
32
+ };
33
+
34
+ let {
35
+ checked,
36
+ children,
37
+ class: _class,
38
+ disabled,
39
+ item,
40
+ menuClass,
41
+ onClose,
42
+ onOpen,
43
+ onSelect,
44
+ role = 'menuitem',
45
+ text,
46
+ shortcut,
47
+ value,
48
+ ...rest
49
+ }: Props = $props();
50
+
51
+ const menuItemContext = getContext<MenuItemContext>(MENU_ITEM_CONTEXT_KEY) || {};
52
+
53
+ const menuBarContext = getContext<MenuBarContext>(MENU_BAR_CONTEXT_KEY) || {};
54
+
55
+ const instanceId = `MenuItem-${uuid}`;
56
+
57
+ let displayId = $derived(`${value}-display-${instanceId}`);
58
+ let open = $derived(menuItemContext.openValues?.includes(value));
59
+ let prevOpen = $state(menuItemContext.openValues?.includes(value));
60
+ let menuId = $derived(`${value}-menu-${instanceId}`);
61
+
62
+ let menuItemRef: HTMLButtonElement | undefined = $state();
63
+ let menuRef: Menu | undefined = $state();
64
+
65
+ //#region methods
66
+
67
+ export const blur = () => {
68
+ menuItemRef?.blur();
69
+ };
70
+
71
+ export const click = () => {
72
+ menuItemRef?.click();
73
+ };
74
+
75
+ export const focus = (options?: FocusOptions) => {
76
+ menuItemRef?.focus(options);
77
+ };
78
+
79
+ //#endregion
80
+
81
+ //#region events
82
+
83
+ const raiseClose = (value: string) => {
84
+ onClose?.(value);
85
+ menuItemContext.onClose?.(value);
86
+ };
87
+
88
+ const raiseOpen = (value: string) => {
89
+ onOpen?.(value);
90
+ menuItemContext.onOpen?.(value);
91
+ };
92
+
93
+ const raiseSelect = (value: string) => {
94
+ onSelect?.(value);
95
+ menuItemContext.onSelect?.(value);
96
+ };
97
+
98
+ $effect(() => {
99
+ if (open !== prevOpen) {
100
+ open ? raiseOpen(value) : raiseClose(value);
101
+ }
102
+ prevOpen = open;
103
+ });
104
+
105
+ //#endregion
106
+
107
+ //#region focus
108
+
109
+ const focusPreviousMenuItem = () => {
110
+ let candidate =
111
+ menuItemRef?.previousElementSibling || menuItemRef?.parentElement?.lastElementChild;
112
+ while (candidate && !isElementEnabledMenuItem(candidate)) {
113
+ candidate = candidate.previousElementSibling || menuItemRef?.parentElement?.lastElementChild;
114
+
115
+ if (candidate === menuItemRef) {
116
+ return false;
117
+ }
118
+ }
119
+ (candidate as HTMLElement)?.focus();
120
+ return !!candidate;
121
+ };
122
+
123
+ const focusNextMenuItem = () => {
124
+ let candidate =
125
+ menuItemRef?.nextElementSibling || menuItemRef?.parentElement?.firstElementChild;
126
+ while (candidate && !isElementEnabledMenuItem(candidate)) {
127
+ candidate = candidate.nextElementSibling || menuItemRef?.parentElement?.firstElementChild;
128
+
129
+ if (candidate === menuItemRef) {
130
+ return false;
131
+ }
132
+ }
133
+ (candidate as HTMLElement)?.focus();
134
+ return !!candidate;
135
+ };
136
+
137
+ //#endregion
138
+
139
+ //#region open/close
140
+
141
+ // opens the menu for this menu item
142
+ const openMenu = () => {
143
+ if (!menuItemContext.openValues.includes(value)) {
144
+ // slice to depth to close any sibling menus that are open
145
+ menuItemContext.openValues = [
146
+ ...menuItemContext.openValues.slice(0, menuItemContext.depth),
147
+ value
148
+ ];
149
+ }
150
+ };
151
+
152
+ // closes the menu for this menu item
153
+ const closeMenu = async () => {
154
+ const index = menuItemContext.openValues.indexOf(value);
155
+ if (index !== -1) {
156
+ menuItemContext.openValues = [...menuItemContext.openValues.slice(0, index)];
157
+ await tick();
158
+ menuItemRef?.focus();
159
+ }
160
+ };
161
+
162
+ const closeAllMenus = () => {
163
+ menuItemContext.openValues = [];
164
+ };
165
+
166
+ //#endregion
167
+
168
+ //#region event handlers
169
+
170
+ const onKeyDown: KeyboardEventHandler<HTMLButtonElement> = async (event) => {
171
+ if (!disabled && !event.altKey && !event.ctrlKey && !event.shiftKey) {
172
+ switch (event.key) {
173
+ case 'ArrowDown':
174
+ // ARIA menubar/menuitem:
175
+ // If the currently focused menuitem has a submenu,
176
+ // opens the submenu and places focus on the first item in the submenu.
177
+ if (menuItemContext.isMenuBarItem && children) {
178
+ openMenu();
179
+ setTimeout(async () => {
180
+ await tick();
181
+ menuRef?.focusFirstMenuItem();
182
+ }, 10);
183
+ event.preventDefault();
184
+ event.stopPropagation();
185
+ return false;
186
+ }
187
+
188
+ if (!menuItemContext.isMenuBarItem) {
189
+ // ARIA menuitem:
190
+ // Moves focus to the next item, optionally wrapping from the last to the first.
191
+ focusNextMenuItem();
192
+ event.preventDefault();
193
+ event.stopPropagation();
194
+ return false;
195
+ }
196
+ break;
197
+ case 'ArrowLeft':
198
+ // ARIA menubar/menuitem:
199
+ // Moves focus to the previous item, optionally wrapping from the first to the last.
200
+ if (menuItemContext.isMenuBarItem) {
201
+ focusPreviousMenuItem();
202
+ event.preventDefault();
203
+ event.stopPropagation();
204
+ return false;
205
+ }
206
+
207
+ // ARIA menuitem:
208
+ // When focus is in a submenu of an item in a menu,
209
+ // closes the submenu and returns focus to the parent menuitem.
210
+ if (menuItemContext.depth && menuItemContext.depth > 1) {
211
+ menuItemContext.closeContainingMenu?.();
212
+ event.preventDefault();
213
+ event.stopPropagation();
214
+ return false;
215
+ }
216
+
217
+ // ARIA menubar/menuitem:
218
+ // When focus is in a submenu of an item in a menubar,
219
+ // closes the submenu,
220
+ // moves focus to the previous item in the menubar,
221
+ // and,
222
+ // if focus is now on a menuitem with a submenu,
223
+ // either opens the submenu of that menuitem without moving focus into the submenu,
224
+ // or opens the submenu of that menuitem and places focus on the first item in the submenu.
225
+ menuBarContext.openPreviousMenuBarItem?.();
226
+ event.preventDefault();
227
+ event.stopPropagation();
228
+ return false;
229
+ case 'ArrowRight':
230
+ // ARIA menubar:
231
+ // Moves focus to the next item, optionally wrapping from the last to the first.
232
+ if (menuItemContext.isMenuBarItem) {
233
+ focusNextMenuItem();
234
+ event.preventDefault();
235
+ event.stopPropagation();
236
+ return false;
237
+ }
238
+
239
+ // ARIA menuitem:
240
+ // When focus is in a menu and on a menuitem that has a submenu,
241
+ // opens the submenu and places focus on its first item
242
+ if (children) {
243
+ openMenu();
244
+ setTimeout(async () => {
245
+ await tick();
246
+ menuRef?.focusFirstMenuItem();
247
+ }, 10);
248
+ event.preventDefault();
249
+ event.stopPropagation();
250
+ return false;
251
+ }
252
+
253
+ // ARIA menubar/menuitem:
254
+ // When focus is in a menu and on an item that does not have a submenu,
255
+ // closes the submenu and any parent menus,
256
+ // moves focus to the next item in the menubar,
257
+ // and,
258
+ // if focus is now on a menuitem with a submenu,
259
+ // either opens the submenu of that menuitem without moving focus into the submenu,
260
+ // or opens the submenu of that menuitem and places focus on the first item in the submenu.
261
+ if (menuBarContext.openNextMenuBarItem) {
262
+ menuBarContext.openNextMenuBarItem();
263
+ event.preventDefault();
264
+ event.stopPropagation();
265
+ return false;
266
+ }
267
+ break;
268
+ case 'ArrowUp':
269
+ // ARIA menubar/menuitem:
270
+ // If the currently focused menuitem has a submenu,
271
+ // opens the submenu and places focus on the last item in the submenu.
272
+ if (menuItemContext.isMenuBarItem && children) {
273
+ openMenu();
274
+ setTimeout(async () => {
275
+ await tick();
276
+ menuRef?.focusLastMenuItem();
277
+ }, 10);
278
+ event.preventDefault();
279
+ event.stopPropagation();
280
+ return false;
281
+ }
282
+
283
+ // ARIA menuitem:
284
+ // Moves focus to the previous item, optionally wrapping from the first to the last.
285
+ if (!menuItemContext.isMenuBarItem) {
286
+ focusPreviousMenuItem();
287
+ event.preventDefault();
288
+ event.stopPropagation();
289
+ return false;
290
+ }
291
+ break;
292
+ case 'Escape':
293
+ // ARIA menuitem:
294
+ // Close the menu that contains focus and return focus to the element or context,
295
+ // e.g., menu button or parent menuitem, from which the menu was opened.
296
+ // open = false;
297
+ closeAllMenus();
298
+ event.preventDefault();
299
+ event.stopPropagation();
300
+ return false;
301
+ }
302
+ }
303
+ rest.onkeydown?.(event);
304
+ };
305
+
306
+ const onMouseEnter: MouseEventHandler<HTMLButtonElement> = (event) => {
307
+ menuItemRef?.focus();
308
+ rest.onmouseenter?.(event);
309
+ };
310
+
311
+ const onClick: MouseEventHandler<HTMLButtonElement> = (event) => {
312
+ if (!disabled) {
313
+ if (children) {
314
+ if (!menuItemContext.openValues.includes(value)) {
315
+ openMenu();
316
+ if ($usingKeyboard) {
317
+ setTimeout(async () => {
318
+ await tick();
319
+ menuRef?.focusFirstMenuItem();
320
+ }, 10);
321
+ }
322
+ } else {
323
+ closeMenu();
324
+ }
325
+ event.preventDefault();
326
+ event.stopPropagation();
327
+ return false;
328
+ } else {
329
+ raiseSelect(value);
330
+ closeAllMenus();
331
+ event.preventDefault();
332
+ event.stopPropagation();
333
+ return false;
334
+ }
335
+ }
336
+ rest.onclick?.(event);
337
+ };
338
+
339
+ //#endregion
340
+
341
+ //#region set context
342
+ let menuItemChildContext: MenuItemContext = {
343
+ isMenuBarItem: false,
344
+ get openValues() {
345
+ return menuItemContext.openValues;
346
+ },
347
+ set openValues(value: string[]) {
348
+ menuItemContext.openValues = value;
349
+ },
350
+ rootValue: menuItemContext.rootValue || value,
351
+ depth: menuItemContext.depth ? menuItemContext.depth + 1 : 1,
352
+ closeContainingMenu: closeMenu,
353
+ onOpen: raiseOpen,
354
+ onClose: raiseClose,
355
+ onSelect: raiseSelect
356
+ };
357
+
358
+ setContext<MenuItemContext>(MENU_ITEM_CONTEXT_KEY, menuItemChildContext);
359
+
360
+ //#endregion
361
+ </script>
362
+
363
+ {#snippet renderDefaultItem()}
364
+ <div class="default-item sterling-menu-item-display" class:disabled>
365
+ <div
366
+ class="check"
367
+ class:checkmark={role === 'menuitemcheckbox'}
368
+ class:bullet={role === 'menuitemradio'}
369
+ class:checked
370
+ ></div>
371
+ <div class="content">
372
+ {#if text}
373
+ {#if typeof text === 'string'}
374
+ {text}
375
+ {:else}
376
+ {@render text()}
377
+ {/if}
378
+ {/if}
379
+ </div>
380
+ <div class="shortcut">
381
+ {#if shortcut}
382
+ {#if typeof shortcut === 'string'}
383
+ {shortcut}
384
+ {:else}
385
+ {@render shortcut()}
386
+ {/if}
387
+ {/if}
388
+ </div>
389
+ <div class="chevron" class:has-children={!menuItemContext.isMenuBarItem && !!children}></div>
390
+ </div>
391
+ {/snippet}
392
+
393
+ <button
394
+ bind:this={menuItemRef}
395
+ aria-controls={menuId}
396
+ aria-disabled={disabled}
397
+ aria-expanded={open}
398
+ aria-haspopup={!!children}
399
+ aria-owns={menuId}
400
+ class={['sterling-menu-item', _class]}
401
+ class:using-keyboard={usingKeyboard}
402
+ data-value={value}
403
+ data-root-value={menuItemContext.rootValue}
404
+ {disabled}
405
+ {role}
406
+ tabindex={0}
407
+ type="button"
408
+ {...rest}
409
+ onclick={onClick}
410
+ onkeydown={onKeyDown}
411
+ onmouseenter={onMouseEnter}
412
+ >
413
+ <div class="item" id={displayId}>
414
+ {#if item}
415
+ {@render item()}
416
+ {:else}
417
+ {@render renderDefaultItem()}
418
+ {/if}
419
+ </div>
420
+ {#if menuItemRef && open && children}
421
+ <Popover
422
+ reference={menuItemRef}
423
+ placement={menuItemContext.isMenuBarItem ? 'bottom-start' : 'right-start'}
424
+ {open}
425
+ >
426
+ <Menu bind:this={menuRef} id={menuId} class={menuClass}>
427
+ {@render children()}
428
+ </Menu>
429
+ </Popover>
430
+ {/if}
431
+ </button>
@@ -0,0 +1,22 @@
1
+ import { type Snippet } from 'svelte';
2
+ import type { HTMLButtonAttributes } from 'svelte/elements';
3
+ import type { MenuItemRole } from './MenuItem.types';
4
+ type Props = HTMLButtonAttributes & {
5
+ checked?: boolean | null;
6
+ item?: Snippet;
7
+ menuClass?: string;
8
+ onClose?: (value: string) => void;
9
+ onOpen?: (value: string) => void;
10
+ onSelect?: (value: string) => void;
11
+ role?: MenuItemRole;
12
+ shortcut?: string | Snippet;
13
+ text?: string | Snippet;
14
+ value: string;
15
+ };
16
+ declare const MenuItem: import("svelte").Component<Props, {
17
+ blur: () => void;
18
+ click: () => void;
19
+ focus: (options?: FocusOptions) => void;
20
+ }, "">;
21
+ type MenuItem = ReturnType<typeof MenuItem>;
22
+ export default MenuItem;
@@ -0,0 +1,20 @@
1
+ import type { MENU_ITEM_ROLES } from './MenuItem.constants';
2
+ type MenuItemRoleTuple = typeof MENU_ITEM_ROLES;
3
+ export type MenuItemRole = MenuItemRoleTuple[number];
4
+ export type MenuItemRegistration = {
5
+ value: string;
6
+ open: () => void;
7
+ close: () => void;
8
+ focus: () => void;
9
+ };
10
+ export type MenuItemContext = {
11
+ isMenuBarItem?: boolean;
12
+ openValues: string[];
13
+ rootValue?: string;
14
+ depth?: number;
15
+ closeContainingMenu?: () => void;
16
+ onOpen?: (value: string) => void;
17
+ onClose?: (value: string) => void;
18
+ onSelect?: (value: string) => void;
19
+ };
20
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { MenuItemRegistration } from './MenuItem.types';
2
+ export declare const focusPreviousChild: (children: MenuItemRegistration[], currentValue: string) => void;
3
+ export declare const focusNextChild: (children: MenuItemRegistration[], currentValue: string) => void;
4
+ export declare const focusFirstChild: (children: MenuItemRegistration[]) => void;
5
+ export declare const focusLastChild: (children: MenuItemRegistration[]) => void;
6
+ export declare const isElementMenuItem: (candidate: Element | null | undefined) => boolean;
7
+ export declare const isElementEnabledMenuItem: (candidate: Element | null | undefined) => boolean;
@@ -0,0 +1,36 @@
1
+ export const focusPreviousChild = (children, currentValue) => {
2
+ const index = children?.findIndex((menuItem) => menuItem.value === currentValue);
3
+ if (index !== -1) {
4
+ const focusIndex = index === 0 ? children.length - 1 : index - 1;
5
+ children[focusIndex].focus();
6
+ }
7
+ };
8
+ export const focusNextChild = (children, currentValue) => {
9
+ const index = children?.findIndex((menuItem) => menuItem.value === currentValue);
10
+ if (index !== -1) {
11
+ const focusIndex = (index + 1) % children.length;
12
+ children[focusIndex].focus();
13
+ }
14
+ };
15
+ export const focusFirstChild = (children) => {
16
+ children?.[0]?.focus();
17
+ };
18
+ export const focusLastChild = (children) => {
19
+ children?.[children.length - 1]?.focus();
20
+ };
21
+ export const isElementMenuItem = (candidate) => {
22
+ if (!candidate)
23
+ return false;
24
+ const dataValue = candidate?.getAttribute('data-value');
25
+ const role = candidate?.getAttribute('role');
26
+ return ((role === 'menuitem' || role === 'menuitemcheckbox' || role === 'menuitemradio') &&
27
+ dataValue !== null &&
28
+ dataValue !== undefined);
29
+ };
30
+ export const isElementEnabledMenuItem = (candidate) => {
31
+ if (!isElementMenuItem(candidate)) {
32
+ return false;
33
+ }
34
+ const disabled = candidate?.getAttribute('disabled');
35
+ return disabled === null || disabled === 'false';
36
+ };