@placeholderco/placeholder-ui 3.2.8 → 3.2.9

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.
@@ -14,7 +14,7 @@
14
14
  separator?: boolean;
15
15
  /** Right-aligned shortcut hint (e.g. `⌘C`, `Del`). Purely visual. */
16
16
  shortcut?: string;
17
- /** Nested submenu items — opens on hover. */
17
+ /** Nested submenu items — opens on hover, click, Enter/Space or ArrowRight. */
18
18
  children?: ContextMenuItem[];
19
19
  }
20
20
 
@@ -37,6 +37,7 @@
37
37
  </script>
38
38
 
39
39
  <script lang="ts">
40
+ import { tick } from 'svelte';
40
41
  import ContextMenuList from './ContextMenuList.svelte';
41
42
  import { topLayer } from '../util/Floating.js';
42
43
 
@@ -87,6 +88,32 @@
87
88
  placed = true;
88
89
  });
89
90
 
91
+ // Move focus into the menu once it is visible (hidden elements can't take focus), and hand it
92
+ // back to whatever had it before when the menu closes — however it closes. Keyed on `placed`
93
+ // rather than x/y so repositioning an open menu doesn't yank focus back to the first item.
94
+ $effect(() => {
95
+ if (!open || !placed || !menuEl) return;
96
+ const node = menuEl;
97
+ const previous = document.activeElement;
98
+ let active = true;
99
+ void tick().then(() => {
100
+ if (!active) return;
101
+ node
102
+ .querySelector<HTMLButtonElement>('button:not(:disabled)')
103
+ ?.focus({ preventScroll: true });
104
+ });
105
+ return () => {
106
+ active = false;
107
+ // Only restore if focus is still in (or was dropped by) the menu; an outside click
108
+ // that focused something else keeps that focus.
109
+ const current = document.activeElement;
110
+ const lost = !current || current === document.body || node.contains(current);
111
+ if (lost && previous instanceof HTMLElement && previous.isConnected) {
112
+ previous.focus({ preventScroll: true });
113
+ }
114
+ };
115
+ });
116
+
90
117
  // Dismiss the menu on outside interaction while it's open.
91
118
  $effect(() => {
92
119
  if (!open) return;
@@ -94,9 +121,16 @@
94
121
  if (menuEl && !menuEl.contains(e.target as Node)) close();
95
122
  };
96
123
  const onKey = (e: KeyboardEvent) => {
124
+ const target = e.target instanceof Element ? e.target : null;
125
+ const inMenu = !!(target && menuEl?.contains(target));
97
126
  if (e.key === 'Escape') {
127
+ // Inside a submenu, Escape closes just that level (handled by ContextMenuList).
128
+ if (inMenu && target?.closest('.pui-ctxmenu-submenu')) return;
98
129
  e.preventDefault();
99
130
  close();
131
+ } else if (e.key === 'Tab' && inMenu) {
132
+ // Let Tab proceed: focus is restored to the trigger first, so Tab moves on from there.
133
+ close();
100
134
  }
101
135
  };
102
136
  const onScroll = (e: Event) => {
@@ -13,7 +13,7 @@ export interface ContextMenuItem {
13
13
  separator?: boolean;
14
14
  /** Right-aligned shortcut hint (e.g. `⌘C`, `Del`). Purely visual. */
15
15
  shortcut?: string;
16
- /** Nested submenu items — opens on hover. */
16
+ /** Nested submenu items — opens on hover, click, Enter/Space or ArrowRight. */
17
17
  children?: ContextMenuItem[];
18
18
  }
19
19
  export interface ContextMenuProps {
@@ -3,6 +3,7 @@
3
3
  </script>
4
4
 
5
5
  <script lang="ts">
6
+ import { tick } from 'svelte';
6
7
  import Icon from '../icon/Icon.svelte';
7
8
  import { iconChevronRight } from '../icon/index.js';
8
9
  import Self from './ContextMenuList.svelte';
@@ -14,24 +15,94 @@
14
15
  /** Nesting level — only the top level applies `minWidth`. */
15
16
  depth?: number;
16
17
  minWidth?: string;
18
+ /** Set on submenus: closes this level and returns focus to its parent item. */
19
+ onBack?: () => void;
17
20
  }
18
21
 
19
- let { items, onSelect, depth = 0, minWidth = '' }: Props = $props();
22
+ let { items, onSelect, depth = 0, minWidth = '', onBack }: Props = $props();
23
+ let listEl = $state<HTMLUListElement | null>(null);
20
24
 
21
- // Only one submenu per list can be open at a time (the hovered item).
25
+ // Only one submenu per list can be open at a time.
22
26
  let hoverIndex = $state<number | null>(null);
23
27
  let submenuEl = $state<HTMLElement | null>(null);
24
28
  let flipLeft = $state(false);
25
29
 
26
- function onItemEnter(i: number, item: ContextMenuItem) {
30
+ const focusOpts = { preventScroll: true };
31
+
32
+ function onItemEnter(i: number, item: ContextMenuItem, li: HTMLElement) {
27
33
  hoverIndex = item.children?.length ? i : null;
34
+ // Keep focus with the pointer so a submenu closing under it never drops focus to <body>.
35
+ // Disabled rows can't take focus, so park it on the list itself.
36
+ const button = li.querySelector<HTMLButtonElement>(':scope > button');
37
+ (button && !button.disabled ? button : listEl)?.focus(focusOpts);
38
+ }
39
+
40
+ async function openSubmenu(i: number) {
41
+ hoverIndex = i;
42
+ await tick();
43
+ submenuEl?.querySelector<HTMLButtonElement>('button:not(:disabled)')?.focus(focusOpts);
28
44
  }
29
45
 
30
- function handleClick(item: ContextMenuItem) {
31
- if (item.disabled || item.separator || item.children?.length) return;
46
+ function closeSubmenu(i: number) {
47
+ hoverIndex = null;
48
+ listEl?.children.item(i)?.querySelector<HTMLButtonElement>(':scope > button')?.focus(focusOpts);
49
+ }
50
+
51
+ function handleClick(item: ContextMenuItem, i: number) {
52
+ if (item.disabled || item.separator) return;
53
+ if (item.children?.length) {
54
+ void openSubmenu(i);
55
+ return;
56
+ }
32
57
  onSelect(item);
33
58
  }
34
59
 
60
+ function onKey(event: KeyboardEvent) {
61
+ const target = event.target;
62
+ // Only handle keys aimed at this level (the list itself or one of its own items);
63
+ // nested lists handle their own.
64
+ if (
65
+ !listEl ||
66
+ !(target instanceof HTMLElement) ||
67
+ (target !== listEl && target.closest('[role="menu"]') !== listEl)
68
+ )
69
+ return;
70
+
71
+ const handled = () => {
72
+ event.preventDefault();
73
+ event.stopPropagation();
74
+ };
75
+
76
+ if (event.key === 'ArrowRight') {
77
+ const index = Array.from(listEl.children).indexOf(target.parentElement as Element);
78
+ if (!items[index]?.children?.length) return;
79
+ handled();
80
+ void openSubmenu(index);
81
+ return;
82
+ }
83
+ if ((event.key === 'ArrowLeft' || event.key === 'Escape') && onBack) {
84
+ handled();
85
+ onBack();
86
+ return;
87
+ }
88
+
89
+ const buttons = Array.from(
90
+ listEl.querySelectorAll<HTMLButtonElement>(':scope > li > button:not(:disabled)')
91
+ );
92
+ if (!buttons.length) return;
93
+ const last = buttons.length - 1;
94
+ const current = buttons.indexOf(target as HTMLButtonElement);
95
+ let next: number;
96
+ if (event.key === 'ArrowDown') next = current < 0 || current === last ? 0 : current + 1;
97
+ else if (event.key === 'ArrowUp') next = current <= 0 ? last : current - 1;
98
+ else if (event.key === 'Home') next = 0;
99
+ else if (event.key === 'End') next = last;
100
+ else return;
101
+ handled();
102
+ hoverIndex = null;
103
+ buttons[next].focus(focusOpts);
104
+ }
105
+
35
106
  // Decide whether the open submenu should flip to the left edge of its parent.
36
107
  // Measured from the parent <li> right edge so the result is stable once flipped.
37
108
  $effect(() => {
@@ -47,6 +118,9 @@
47
118
  <ul
48
119
  class="pui-ctxmenu-list"
49
120
  role="menu"
121
+ tabindex="-1"
122
+ bind:this={listEl}
123
+ onkeydown={onKey}
50
124
  style={depth === 0 && minWidth ? `min-width: ${minWidth};` : ''}
51
125
  >
52
126
  {#each items as item, i (i)}
@@ -54,14 +128,21 @@
54
128
  <li class="pui-ctxmenu-sep" role="separator"></li>
55
129
  {:else}
56
130
  <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
57
- <li class="pui-ctxmenu-li" role="none" onmouseenter={() => onItemEnter(i, item)}>
131
+ <li
132
+ class="pui-ctxmenu-li"
133
+ role="none"
134
+ onmouseenter={(e) => onItemEnter(i, item, e.currentTarget)}
135
+ >
58
136
  <button
59
137
  type="button"
60
138
  role="menuitem"
61
139
  class="pui-ctxmenu-item"
62
140
  class:danger={item.danger}
141
+ tabindex="-1"
142
+ aria-haspopup={item.children?.length ? 'menu' : undefined}
143
+ aria-expanded={item.children?.length ? hoverIndex === i : undefined}
63
144
  disabled={item.disabled}
64
- onclick={() => handleClick(item)}
145
+ onclick={() => handleClick(item, i)}
65
146
  >
66
147
  <span class="pui-ctxmenu-ico">
67
148
  {#if item.icon}<Icon svg={item.icon} size="1rem" />{/if}
@@ -77,7 +158,12 @@
77
158
 
78
159
  {#if item.children?.length && hoverIndex === i}
79
160
  <div class="pui-ctxmenu-submenu" class:flip-left={flipLeft} bind:this={submenuEl}>
80
- <Self items={item.children} {onSelect} depth={depth + 1} />
161
+ <Self
162
+ items={item.children}
163
+ {onSelect}
164
+ depth={depth + 1}
165
+ onBack={() => closeSubmenu(i)}
166
+ />
81
167
  </div>
82
168
  {/if}
83
169
  </li>
@@ -99,6 +185,10 @@
99
185
  user-select: none;
100
186
  }
101
187
 
188
+ .pui-ctxmenu-list:focus {
189
+ outline: none;
190
+ }
191
+
102
192
  .pui-ctxmenu-li {
103
193
  position: relative;
104
194
  }
@@ -6,6 +6,8 @@ interface Props {
6
6
  /** Nesting level — only the top level applies `minWidth`. */
7
7
  depth?: number;
8
8
  minWidth?: string;
9
+ /** Set on submenus: closes this level and returns focus to its parent item. */
10
+ onBack?: () => void;
9
11
  }
10
12
  declare const ContextMenuList: import("svelte").Component<Props, {}, "">;
11
13
  type ContextMenuList = ReturnType<typeof ContextMenuList>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@placeholderco/placeholder-ui",
3
- "version": "3.2.8",
3
+ "version": "3.2.9",
4
4
  "description": "A modern, customizable Svelte 5 UI component library with comprehensive theming support. Re-brand it with a single config object.",
5
5
  "license": "MIT",
6
6
  "author": "Placeholderco",
@@ -78,7 +78,7 @@
78
78
  "prettier": "^3.9.10",
79
79
  "prettier-plugin-svelte": "^4.1.1",
80
80
  "publint": "^0.3.25",
81
- "rimraf": "^6.1.3",
81
+ "rimraf": "^6.1.4",
82
82
  "svelte": "^5.57.3",
83
83
  "svelte-check": "^4.7.8",
84
84
  "typescript": "^6.0.3",