@human-kit/ui 1.0.0-beta.3 → 1.0.0-beta.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 (74) hide show
  1. package/README.md +15 -6
  2. package/dist/FOCUS_STATE_CONTRACT.md +2 -0
  3. package/dist/autocomplete/list/autocomplete-list.svelte.d.ts +3 -1
  4. package/dist/button/root/button-root.svelte +19 -3
  5. package/dist/button/root/button-root.svelte.d.ts +10 -0
  6. package/dist/combobox/list/combobox-listbox.svelte.d.ts +3 -1
  7. package/dist/index.d.ts +2 -0
  8. package/dist/index.js +2 -0
  9. package/dist/internal/environment.d.ts +11 -0
  10. package/dist/internal/environment.js +14 -0
  11. package/dist/internal/localized-strings.d.ts +80 -0
  12. package/dist/internal/localized-strings.js +80 -0
  13. package/dist/listbox/README.md +27 -1
  14. package/dist/listbox/item/listbox-item.svelte +11 -2
  15. package/dist/listbox/root/context.d.ts +25 -0
  16. package/dist/listbox/root/context.js +108 -1
  17. package/dist/listbox/root/listbox.svelte +85 -7
  18. package/dist/listbox/root/listbox.svelte.d.ts +13 -1
  19. package/dist/menu/README.md +46 -0
  20. package/dist/menu/content/menu-content.svelte +16 -5
  21. package/dist/menu/content/menu-content.svelte.d.ts +2 -1
  22. package/dist/menu/context-trigger/menu-context-trigger.svelte +195 -0
  23. package/dist/menu/context-trigger/menu-context-trigger.svelte.d.ts +36 -0
  24. package/dist/menu/index.d.ts +4 -1
  25. package/dist/menu/index.js +1 -0
  26. package/dist/menu/index.parts.d.ts +1 -0
  27. package/dist/menu/index.parts.js +1 -0
  28. package/dist/menu/root/context.d.ts +18 -0
  29. package/dist/menu/root/menu-state.svelte.js +19 -0
  30. package/dist/popover/content/popover-content.svelte +2 -1
  31. package/dist/primitives/floating.d.ts +33 -5
  32. package/dist/primitives/floating.js +25 -0
  33. package/dist/primitives/index.d.ts +1 -0
  34. package/dist/primitives/index.js +1 -0
  35. package/dist/primitives/keyboard-navigation.d.ts +23 -5
  36. package/dist/primitives/keyboard-navigation.js +22 -15
  37. package/dist/primitives/long-press.d.ts +59 -0
  38. package/dist/primitives/long-press.js +142 -0
  39. package/dist/primitives/scroll-lock.d.ts +24 -0
  40. package/dist/primitives/scroll-lock.js +40 -0
  41. package/dist/transfer-list/README.md +121 -0
  42. package/dist/transfer-list/index.d.ts +33 -0
  43. package/dist/transfer-list/index.js +18 -0
  44. package/dist/transfer-list/index.parts.d.ts +9 -0
  45. package/dist/transfer-list/index.parts.js +10 -0
  46. package/dist/transfer-list/item/transfer-list-item.svelte +74 -0
  47. package/dist/transfer-list/item/transfer-list-item.svelte.d.ts +35 -0
  48. package/dist/transfer-list/list/transfer-list-list.svelte +162 -0
  49. package/dist/transfer-list/list/transfer-list-list.svelte.d.ts +56 -0
  50. package/dist/transfer-list/list/transfer-list-source.svelte +13 -0
  51. package/dist/transfer-list/list/transfer-list-source.svelte.d.ts +36 -0
  52. package/dist/transfer-list/list/transfer-list-target.svelte +14 -0
  53. package/dist/transfer-list/list/transfer-list-target.svelte.d.ts +36 -0
  54. package/dist/transfer-list/move/transfer-list-move-all.svelte +14 -0
  55. package/dist/transfer-list/move/transfer-list-move-all.svelte.d.ts +10 -0
  56. package/dist/transfer-list/move/transfer-list-move-button.svelte +103 -0
  57. package/dist/transfer-list/move/transfer-list-move-button.svelte.d.ts +25 -0
  58. package/dist/transfer-list/move/transfer-list-move-down.svelte +14 -0
  59. package/dist/transfer-list/move/transfer-list-move-down.svelte.d.ts +10 -0
  60. package/dist/transfer-list/move/transfer-list-move-selected.svelte +14 -0
  61. package/dist/transfer-list/move/transfer-list-move-selected.svelte.d.ts +10 -0
  62. package/dist/transfer-list/move/transfer-list-move-up.svelte +14 -0
  63. package/dist/transfer-list/move/transfer-list-move-up.svelte.d.ts +10 -0
  64. package/dist/transfer-list/move/transfer-list-reorder-button.svelte +83 -0
  65. package/dist/transfer-list/move/transfer-list-reorder-button.svelte.d.ts +21 -0
  66. package/dist/transfer-list/root/context.d.ts +70 -0
  67. package/dist/transfer-list/root/context.js +24 -0
  68. package/dist/transfer-list/root/transfer-list-root.svelte +342 -0
  69. package/dist/transfer-list/root/transfer-list-root.svelte.d.ts +65 -0
  70. package/dist/transfer-list/root/types.d.ts +22 -0
  71. package/dist/transfer-list/root/types.js +4 -0
  72. package/dist/transfer-list/status/transfer-list-status.svelte +70 -0
  73. package/dist/transfer-list/status/transfer-list-status.svelte.d.ts +15 -0
  74. package/package.json +6 -1
@@ -1,5 +1,6 @@
1
1
  <script lang="ts" generics="T extends object = object">
2
2
  import type { Snippet } from 'svelte';
3
+ import type { HTMLAttributes } from 'svelte/elements';
3
4
  import { onMount, tick, untrack } from 'svelte';
4
5
  import { createListBoxContext, type ListBoxContext } from './context';
5
6
  import { trackInteractionModality } from '../../primitives/input-modality';
@@ -16,6 +17,14 @@
16
17
  emptyPlaceholder?: string | Snippet;
17
18
  /** Iterable of items for dynamic rendering. Used with a snippet that receives each item. */
18
19
  items?: Iterable<T>;
20
+ /**
21
+ * Reads an item's key — the same value passed as `ListBox.Item`'s `id`.
22
+ *
23
+ * Only needed together with `virtualizer`: a Shift range is otherwise measured over
24
+ * the options in the DOM, which for a virtualized list is just the rendered window,
25
+ * so the range stops wherever the user happened to have scrolled.
26
+ */
27
+ getItemKey?: (item: T) => string | number;
19
28
  /** Keys of items that should be disabled and non-selectable. */
20
29
  disabledKeys?: Iterable<string | number>;
21
30
  /** Selection mode: 'single' allows one selection, 'multiple' allows many. */
@@ -48,6 +57,9 @@
48
57
  id?: string;
49
58
  /** Accessible label for the listbox. Announced by screen readers. */
50
59
  'aria-label'?: string;
60
+ /** Id of a visible element that labels the listbox. Use instead of `aria-label`
61
+ * when the list already has a heading on screen. */
62
+ 'aria-labelledby'?: string;
51
63
  /** Callback fired when the selection changes. */
52
64
  onChange?: (value: Set<string | number>) => void;
53
65
  /** Disable DOM focus handling on the root container for virtual-focus compositions. */
@@ -70,12 +82,16 @@
70
82
  * `overflow-y: auto`, as an unvirtualized long list would need anyway.
71
83
  */
72
84
  virtualizer?: { rowHeight?: number; overscan?: number };
73
- };
85
+ } & Omit<
86
+ HTMLAttributes<HTMLDivElement>,
87
+ 'class' | 'children' | 'role' | 'id' | 'aria-label' | 'aria-labelledby' | 'onchange'
88
+ >;
74
89
 
75
90
  let {
76
91
  selectionBehavior = 'toggle',
77
92
  emptyPlaceholder = 'No items selected',
78
93
  items,
94
+ getItemKey,
79
95
  disabledKeys,
80
96
  selectionMode = 'single',
81
97
  value = $bindable(),
@@ -86,13 +102,20 @@
86
102
  class: className = '',
87
103
  id,
88
104
  'aria-label': ariaLabel,
105
+ 'aria-labelledby': ariaLabelledBy,
89
106
  onChange,
90
107
  disableFocusHandling = false,
91
108
  loop = false,
92
109
  typeahead = false,
93
110
  virtualizer,
94
111
  context = $bindable(),
95
- element = $bindable()
112
+ element = $bindable(),
113
+ onkeydown: onKeyDownExternal,
114
+ onmousedown: onMouseDownExternal,
115
+ onfocusin: onFocusInExternal,
116
+ onfocusout: onFocusOutExternal,
117
+ onscroll: onScrollExternal,
118
+ ...restProps
96
119
  }: ListBoxProps & { context?: ListBoxContext; element?: HTMLElement } = $props();
97
120
 
98
121
  let listboxElement: HTMLElement;
@@ -312,6 +335,29 @@
312
335
  });
313
336
  }
314
337
 
338
+ /**
339
+ * Tells assistive technology how big the list really is.
340
+ *
341
+ * A virtualized listbox holds a handful of options, so the position and count a browser
342
+ * computes from the DOM describe the window rather than the collection — a screen reader
343
+ * announces "1 of 8" for a list of two thousand. ARIA has `aria-setsize` /
344
+ * `aria-posinset` for exactly this case, and only the list knows the real numbers.
345
+ *
346
+ * Written onto the rendered rows rather than passed down because the rows come from the
347
+ * consumer's snippet; it is the same set of nodes the measurement above already reads,
348
+ * and it re-runs whenever the window moves or the collection changes.
349
+ */
350
+ function labelPositions() {
351
+ if (!listboxElement || !isVirtual) return;
352
+ const rows = listboxElement.querySelectorAll<HTMLElement>('[data-listbox-window] > *');
353
+ const setSize = String(itemsArray.length);
354
+ const from = listWindow?.from ?? 0;
355
+ rows.forEach((row, offset) => {
356
+ row.setAttribute('aria-setsize', setSize);
357
+ row.setAttribute('aria-posinset', String(from + offset + 1));
358
+ });
359
+ }
360
+
315
361
  $effect(() => {
316
362
  if (!isVirtual || !listboxElement) return;
317
363
 
@@ -324,6 +370,7 @@
324
370
  const element = listboxElement;
325
371
 
326
372
  measureRows();
373
+ labelPositions();
327
374
 
328
375
  const observer = new ResizeObserver(() => {
329
376
  untrack(() => {
@@ -332,6 +379,7 @@
332
379
  }
333
380
  });
334
381
  measureRows();
382
+ labelPositions();
335
383
  });
336
384
 
337
385
  observer.observe(element);
@@ -400,6 +448,16 @@
400
448
  // on its input) can reach a row the window hasn't rendered yet.
401
449
  ctx.setScrollIndexIntoView(scrollIndexIntoView);
402
450
  ctx.setVirtualized(() => isVirtual);
451
+
452
+ // Only for a virtualized list: an unvirtualized one has every row on the page, and DOM
453
+ // order is the more truthful source there — it survives a keyed reorder that the items
454
+ // array has not caught up with.
455
+ $effect(() => {
456
+ const read = getItemKey;
457
+ if (!isVirtual || !read) return;
458
+ ctx.setOrderedKeys(() => itemsArray.map(read));
459
+ return () => ctx.setOrderedKeys(null);
460
+ });
403
461
  let hasMounted = $state(false);
404
462
  const registeredItemCount = $derived(hasMounted ? itemCount : ctx.getItemCount());
405
463
  const shouldShowEmptyPlaceholder = $derived(
@@ -441,6 +499,24 @@
441
499
  ctx.setFocusVisible(true);
442
500
  }
443
501
  }
502
+
503
+ /**
504
+ * Runs the listbox's own handler and then the consumer's.
505
+ *
506
+ * The rest props are spread last so a consumer can reach the element, but that also
507
+ * means an `onkeydown` of theirs would replace the one driving focus modality and
508
+ * keyboard navigation. Composing keeps both, with the consumer able to opt out of the
509
+ * rest by calling `stopImmediatePropagation`.
510
+ */
511
+ function compose<E extends Event>(
512
+ internal: (event: E) => void,
513
+ external: ((event: E) => void) | null | undefined
514
+ ) {
515
+ return (event: E) => {
516
+ internal(event);
517
+ external?.(event);
518
+ };
519
+ }
444
520
  </script>
445
521
 
446
522
  <div
@@ -449,15 +525,17 @@
449
525
  {id}
450
526
  aria-multiselectable={selectionMode === 'multiple'}
451
527
  aria-label={ariaLabel}
528
+ aria-labelledby={ariaLabelledBy}
452
529
  class={className}
453
530
  tabindex={disableFocusHandling ? undefined : focusWithin ? -1 : 0}
454
531
  data-focus-within={focusWithin || undefined}
455
532
  use:keyboardAction
456
- onfocusin={handleFocusIn}
457
- onfocusout={handleFocusOut}
458
- onmousedown={handleMouseDown}
459
- onkeydown={handleKeyDown}
460
- onscroll={handleScroll}
533
+ onfocusin={compose(handleFocusIn, onFocusInExternal)}
534
+ onfocusout={compose(handleFocusOut, onFocusOutExternal)}
535
+ onmousedown={compose(handleMouseDown, onMouseDownExternal)}
536
+ onkeydown={compose(handleKeyDown, onKeyDownExternal)}
537
+ onscroll={compose(handleScroll, onScrollExternal)}
538
+ {...restProps}
461
539
  >
462
540
  {#if header}
463
541
  {@render header()}
@@ -1,4 +1,5 @@
1
1
  import type { Snippet } from 'svelte';
2
+ import type { HTMLAttributes } from 'svelte/elements';
2
3
  import { type ListBoxContext } from './context';
3
4
  declare function $$render<T extends object = object>(): {
4
5
  props: {
@@ -8,6 +9,14 @@ declare function $$render<T extends object = object>(): {
8
9
  emptyPlaceholder?: string | Snippet;
9
10
  /** Iterable of items for dynamic rendering. Used with a snippet that receives each item. */
10
11
  items?: Iterable<T>;
12
+ /**
13
+ * Reads an item's key — the same value passed as `ListBox.Item`'s `id`.
14
+ *
15
+ * Only needed together with `virtualizer`: a Shift range is otherwise measured over
16
+ * the options in the DOM, which for a virtualized list is just the rendered window,
17
+ * so the range stops wherever the user happened to have scrolled.
18
+ */
19
+ getItemKey?: (item: T) => string | number;
11
20
  /** Keys of items that should be disabled and non-selectable. */
12
21
  disabledKeys?: Iterable<string | number>;
13
22
  /** Selection mode: 'single' allows one selection, 'multiple' allows many. */
@@ -40,6 +49,9 @@ declare function $$render<T extends object = object>(): {
40
49
  id?: string;
41
50
  /** Accessible label for the listbox. Announced by screen readers. */
42
51
  'aria-label'?: string;
52
+ /** Id of a visible element that labels the listbox. Use instead of `aria-label`
53
+ * when the list already has a heading on screen. */
54
+ 'aria-labelledby'?: string;
43
55
  /** Callback fired when the selection changes. */
44
56
  onChange?: (value: Set<string | number>) => void;
45
57
  /** Disable DOM focus handling on the root container for virtual-focus compositions. */
@@ -65,7 +77,7 @@ declare function $$render<T extends object = object>(): {
65
77
  rowHeight?: number;
66
78
  overscan?: number;
67
79
  };
68
- } & {
80
+ } & Omit<HTMLAttributes<HTMLDivElement>, "children" | "class" | "id" | "role" | "aria-label" | "aria-labelledby" | "onchange"> & {
69
81
  context?: ListBoxContext;
70
82
  element?: HTMLElement;
71
83
  };
@@ -12,6 +12,8 @@ primitives as `Popover`.
12
12
  - Use `Menu.Root` to share open state and the trigger reference.
13
13
  - Use `Menu.Trigger` as the opener button. `ArrowDown`/`Enter`/`Space` open the menu and focus the
14
14
  first item; `ArrowUp` opens and focuses the last item.
15
+ - Use `Menu.ContextTrigger` instead of `Menu.Trigger` to open the menu from a surface rather than
16
+ a button: right click, long press on touch, or `Shift+F10` / the `ContextMenu` key.
15
17
  - Use `Menu.Content` inside `Menu.Root`. It renders the `role="menu"` panel in a portal and
16
18
  positions it against the trigger (default placement `bottom-start`).
17
19
  - Use `Menu.Item` for actions. Provide `onAction`, and optionally `disabled`, `closeOnSelect`, or
@@ -33,6 +35,38 @@ primitives as `Popover`.
33
35
  open submenu, hovering the sibling items it passes over does not close the submenu for a short
34
36
  grace period, so the user can reach it without the submenu collapsing mid-path.
35
37
 
38
+ ## Context menus
39
+
40
+ `Menu.ContextTrigger` is a surface, not a button: it renders a plain element so it can wrap
41
+ arbitrary — sometimes interactive — content. It opens the menu three ways, and each anchors the
42
+ panel differently:
43
+
44
+ - **Right click** — anchored at the pointer, flush against it (`Menu.Content` drops its default
45
+ `offset` to `0` for a context menu), unfolding down and to the right like a native one. Right
46
+ clicking again re-anchors the open menu instead of closing and reopening it.
47
+ - **Long press** on touch or pen — anchored at the finger. It is what makes the menu reachable at
48
+ all on a phone, where `contextmenu` is unreliable. The action suppresses the `click` and
49
+ `contextmenu` the platform emits for the same gesture, and the surface sets
50
+ `-webkit-touch-callout: none; user-select: none` inline so iOS raises the menu instead of the
51
+ text callout. Turn either off with `longPress={false}` / `preventTouchCallout={false}`.
52
+ - **`Shift+F10` or the `ContextMenu` key** — anchored to the surface itself, with the first item
53
+ focused. There is no pointer involved, so reusing the last cursor position would put the panel
54
+ somewhere the keyboard user never pointed at.
55
+
56
+ A left press anywhere — including on the surface itself — dismisses it, like a native menu.
57
+
58
+ ### Accessibility
59
+
60
+ The surface is a tab stop by default (`tabindex={0}`) so a keyboard user can reach it; pass
61
+ `tabindex={-1}` when it lives inside a composite that already manages focus with a roving
62
+ tabindex, such as a table or a tree.
63
+
64
+ It carries `aria-keyshortcuts="Shift+F10"` and nothing else. `aria-haspopup` and `aria-expanded`
65
+ are **not** global ARIA properties: on a generic element they would be invalid, so the component
66
+ does not claim them. That is a real limit, not an oversight — **a context menu must never be the
67
+ only route to an action.** Give the same `Menu.Root` a visible `Menu.Trigger`, or expose the same
68
+ actions elsewhere in the UI.
69
+
36
70
  ## onOpenChange details
37
71
 
38
72
  `Menu.Root` and `Menu.SubmenuRoot` use:
@@ -47,6 +81,17 @@ primitives as `Popover`.
47
81
 
48
82
  Import the component and compose its parts:
49
83
 
84
+ A context menu swaps the trigger and keeps everything else:
85
+
86
+ ```svelte
87
+ <Menu.Root>
88
+ <Menu.ContextTrigger>Right click anywhere in this card</Menu.ContextTrigger>
89
+ <Menu.Content>
90
+ <Menu.Item onAction={edit}>Edit</Menu.Item>
91
+ </Menu.Content>
92
+ </Menu.Root>
93
+ ```
94
+
50
95
  ```svelte
51
96
  <Menu.Root>
52
97
  <Menu.Trigger>Options</Menu.Trigger>
@@ -74,6 +119,7 @@ Import the component and compose its parts:
74
119
 
75
120
  - `Menu.Root`
76
121
  - `Menu.Trigger`
122
+ - `Menu.ContextTrigger`
77
123
  - `Menu.Content`
78
124
  - `Menu.Item`
79
125
  - `Menu.Separator`
@@ -3,7 +3,7 @@
3
3
  import type { HTMLAttributes } from 'svelte/elements';
4
4
  import { onDestroy, tick } from 'svelte';
5
5
  import { browser } from '../../internal/environment';
6
- import { floating, type ExtendedPlacement } from '../../primitives/floating';
6
+ import { createPointAnchor, floating, type ExtendedPlacement } from '../../primitives/floating';
7
7
  import { clickOutside, isTargetInTopLayerAbove } from '../../primitives/click-outside';
8
8
  import { scrollLock } from '../../primitives/scroll-lock';
9
9
  import { releaseFocusedDescendant } from '../../primitives/release-focused-descendant';
@@ -18,7 +18,8 @@
18
18
  * Rendered in a Portal, positioned against the trigger.
19
19
  */
20
20
  type MenuContentProps = {
21
- /** Offset along the main axis from the trigger element. */
21
+ /** Offset along the main axis from the trigger element. Defaults to `4`, or to `0`
22
+ * for a context menu, which sits at the pointer like a native one. */
22
23
  offset?: number;
23
24
  /** Placement relative to the trigger element. Defaults to `bottom-start`
24
25
  * for a root menu and `right-start` for a submenu. */
@@ -38,7 +39,7 @@
38
39
  } & Omit<HTMLAttributes<HTMLDivElement>, 'class' | 'children' | 'role'>;
39
40
 
40
41
  let {
41
- offset = 4,
42
+ offset,
42
43
  placement,
43
44
  shouldFlip = true,
44
45
  boundaryElement = null,
@@ -56,6 +57,16 @@
56
57
  const resolvedPlacementProp = $derived(
57
58
  placement ?? (ctx.parent ? 'right-start' : 'bottom-start')
58
59
  );
60
+ // A context menu opens AT the pointer, so the anchor is a zero-size point rather than
61
+ // the surface, and it sits flush against it — the 4px gap a dropdown keeps from its
62
+ // button would read as the panel drifting away from the cursor. The default placement
63
+ // (`bottom-start`) already matches how a native context menu unfolds down and to the
64
+ // right, and `flip`/`shift` handle the viewport edges.
65
+ const resolvedOffset = $derived(offset ?? (ctx.isContextMenu ? 0 : 4));
66
+ const anchorPoint = $derived(ctx.anchorPoint);
67
+ const resolvedAnchor = $derived(
68
+ anchorPoint ? createPointAnchor(anchorPoint.x, anchorPoint.y, triggerRef) : triggerRef
69
+ );
59
70
 
60
71
  let menuRef: HTMLElement | undefined = $state();
61
72
  let previousOpen = $state(false);
@@ -261,8 +272,8 @@
261
272
  data-exiting={presence.isExiting || undefined}
262
273
  data-placement={resolvedPlacement}
263
274
  use:floating={{
264
- anchor: triggerRef,
265
- offset,
275
+ anchor: resolvedAnchor,
276
+ offset: resolvedOffset,
266
277
  placement: resolvedPlacementProp,
267
278
  shouldFlip,
268
279
  boundaryElement,
@@ -6,7 +6,8 @@ import { type ExtendedPlacement } from '../../primitives/floating';
6
6
  * Rendered in a Portal, positioned against the trigger.
7
7
  */
8
8
  type MenuContentProps = {
9
- /** Offset along the main axis from the trigger element. */
9
+ /** Offset along the main axis from the trigger element. Defaults to `4`, or to `0`
10
+ * for a context menu, which sits at the pointer like a native one. */
10
11
  offset?: number;
11
12
  /** Placement relative to the trigger element. Defaults to `bottom-start`
12
13
  * for a root menu and `right-start` for a submenu. */
@@ -0,0 +1,195 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte';
3
+ import type { HTMLAttributes } from 'svelte/elements';
4
+ import { onDestroy, untrack } from 'svelte';
5
+ import { longPress, LONG_PRESS_DELAY_MS } from '../../primitives/long-press';
6
+ import { useMenuContext } from '../root/context';
7
+
8
+ /**
9
+ * Menu.ContextTrigger - A surface that opens the menu on right click, long press,
10
+ * or the platform's context-menu keystroke, anchored at the pointer.
11
+ *
12
+ * Unlike Menu.Trigger it renders a plain element rather than a button: it wraps
13
+ * arbitrary — sometimes interactive — content, so it must not claim button
14
+ * semantics.
15
+ */
16
+ export type MenuContextTriggerProps = {
17
+ /** Whether the surface is disabled. The browser's own context menu is left alone. */
18
+ disabled?: boolean;
19
+ /** Whether a long press opens the menu on touch and pen. */
20
+ longPress?: boolean;
21
+ /** How long a touch has to be held, in ms. */
22
+ longPressDelay?: number;
23
+ /**
24
+ * Whether to suppress the iOS text callout / selection on the surface. Long press
25
+ * is unusable there without it; turn it off if the surface contains text the user
26
+ * is meant to select.
27
+ */
28
+ preventTouchCallout?: boolean;
29
+ /**
30
+ * Tab order position. Defaults to `0` so keyboard users can reach the surface and
31
+ * open the menu with Shift+F10. Pass `-1` when the surface lives inside a composite
32
+ * that already manages focus with a roving tabindex (a table, a tree).
33
+ */
34
+ tabindex?: number;
35
+ children?: Snippet;
36
+ class?: string;
37
+ element?: HTMLElement | null;
38
+ } & Omit<HTMLAttributes<HTMLDivElement>, 'class' | 'children' | 'tabindex'>;
39
+
40
+ let {
41
+ disabled = false,
42
+ longPress: longPressEnabled = true,
43
+ longPressDelay = LONG_PRESS_DELAY_MS,
44
+ preventTouchCallout = true,
45
+ tabindex = 0,
46
+ children,
47
+ class: className = '',
48
+ element = $bindable<HTMLElement | null>(null),
49
+ style: styleExternal,
50
+ oncontextmenu: onContextMenuExternal,
51
+ onkeydown: onKeydownExternal,
52
+ onpointerdown: onPointerDownExternal,
53
+ ...restProps
54
+ }: MenuContextTriggerProps = $props();
55
+
56
+ const ctx = useMenuContext('Menu.ContextTrigger');
57
+
58
+ let surfaceRef: HTMLElement | null = $state(null);
59
+
60
+ /**
61
+ * A `contextmenu` event the browser may synthesize right after we already handled
62
+ * the keystroke ourselves — some browsers raise it on keyup, past our
63
+ * `preventDefault`. It carries no useful coordinates, so letting it through would
64
+ * re-anchor the open menu to the viewport origin. The window is short and always
65
+ * released, so a genuine right click a moment later is never swallowed.
66
+ */
67
+ let ignoreNextContextMenu = false;
68
+ let ignoreContextMenuTimer: ReturnType<typeof setTimeout> | null = null;
69
+
70
+ const SYNTHETIC_CONTEXT_MENU_WINDOW_MS = 100;
71
+
72
+ function releaseSyntheticContextMenu() {
73
+ ignoreNextContextMenu = false;
74
+ if (ignoreContextMenuTimer === null) return;
75
+ clearTimeout(ignoreContextMenuTimer);
76
+ ignoreContextMenuTimer = null;
77
+ }
78
+
79
+ function ignoreSyntheticContextMenu() {
80
+ releaseSyntheticContextMenu();
81
+ ignoreNextContextMenu = true;
82
+ ignoreContextMenuTimer = setTimeout(
83
+ releaseSyntheticContextMenu,
84
+ SYNTHETIC_CONTEXT_MENU_WINDOW_MS
85
+ );
86
+ }
87
+
88
+ onDestroy(releaseSyntheticContextMenu);
89
+
90
+ const styleValue = $derived(
91
+ [
92
+ // Both are needed for long press on iOS: without them the press raises the
93
+ // text callout and selection handles instead.
94
+ preventTouchCallout ? '-webkit-touch-callout: none; user-select: none;' : '',
95
+ styleExternal ?? ''
96
+ ]
97
+ .filter(Boolean)
98
+ .join(' ')
99
+ );
100
+
101
+ function openAtPoint(x: number, y: number, event: Event) {
102
+ ctx.setAnchorPoint({ x, y });
103
+ // Already open (a second right click elsewhere on the surface): the point above
104
+ // re-anchors it, and `open` is a no-op that reports nothing to the consumer.
105
+ ctx.open('trigger-press', event);
106
+ }
107
+
108
+ function handleContextMenu(event: MouseEvent & { currentTarget: EventTarget & HTMLDivElement }) {
109
+ if (!disabled) {
110
+ // Always suppress the browser's menu when this surface owns one — otherwise
111
+ // both appear stacked.
112
+ event.preventDefault();
113
+ if (ignoreNextContextMenu) {
114
+ releaseSyntheticContextMenu();
115
+ } else {
116
+ openAtPoint(event.clientX, event.clientY, event);
117
+ }
118
+ }
119
+ onContextMenuExternal?.(event);
120
+ }
121
+
122
+ function handleKeydown(event: KeyboardEvent & { currentTarget: EventTarget & HTMLDivElement }) {
123
+ // Shift+F10 everywhere, plus the dedicated key on keyboards that have one.
124
+ const isContextMenuKey = event.key === 'ContextMenu' || (event.shiftKey && event.key === 'F10');
125
+ if (!disabled && isContextMenuKey && !ctx.isOpen) {
126
+ event.preventDefault();
127
+ ignoreSyntheticContextMenu();
128
+ // Anchor to the surface itself: there is no pointer, and a stale cursor
129
+ // position would put the menu somewhere the keyboard user never pointed at.
130
+ ctx.setAnchorPoint(null);
131
+ ctx.requestOpenFocus('first');
132
+ ctx.open('trigger-press', event);
133
+ }
134
+ onKeydownExternal?.(event);
135
+ }
136
+
137
+ function handlePointerDown(
138
+ event: PointerEvent & { currentTarget: EventTarget & HTMLDivElement }
139
+ ) {
140
+ // A left click anywhere dismisses a context menu, including on its own surface.
141
+ // `Menu.Content` tells `clickOutside` to ignore the trigger — which is right for a
142
+ // button that toggles — so the surface has to close itself. Only for the mouse:
143
+ // on touch this same press is the start of the long press that opens it.
144
+ if (event.pointerType === 'mouse' && event.button === 0 && ctx.isOpen) {
145
+ ctx.close('outside-press', event);
146
+ }
147
+ onPointerDownExternal?.(event);
148
+ }
149
+
150
+ // `untrack` keeps the shared trigger ref out of this effect's dependencies — the
151
+ // cleanup writes it to null and the body back to the node, which would otherwise loop.
152
+ $effect(() => {
153
+ const node = surfaceRef;
154
+ element = node;
155
+ if (node) {
156
+ untrack(() => {
157
+ ctx.setTriggerRef(node);
158
+ ctx.setContextMenu(true);
159
+ });
160
+ }
161
+ return () => {
162
+ // Drop the stale ref on unmount so no close path tries to refocus a removed node.
163
+ untrack(() => {
164
+ if (node && ctx.triggerRef === node) {
165
+ ctx.setTriggerRef(null);
166
+ ctx.setContextMenu(false);
167
+ }
168
+ });
169
+ };
170
+ });
171
+ </script>
172
+
173
+ <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
174
+ <div
175
+ bind:this={surfaceRef}
176
+ class={className}
177
+ {tabindex}
178
+ data-menu-trigger="true"
179
+ data-context-trigger="true"
180
+ data-state={ctx.isOpen ? 'open' : 'closed'}
181
+ data-disabled={disabled || undefined}
182
+ aria-keyshortcuts={disabled ? undefined : 'Shift+F10'}
183
+ style={styleValue || undefined}
184
+ oncontextmenu={handleContextMenu}
185
+ onkeydown={handleKeydown}
186
+ onpointerdown={handlePointerDown}
187
+ use:longPress={{
188
+ enabled: longPressEnabled && !disabled,
189
+ delay: longPressDelay,
190
+ onLongPress: (point, event) => openAtPoint(point.x, point.y, event)
191
+ }}
192
+ {...restProps}
193
+ >
194
+ {@render children?.()}
195
+ </div>
@@ -0,0 +1,36 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type { HTMLAttributes } from 'svelte/elements';
3
+ /**
4
+ * Menu.ContextTrigger - A surface that opens the menu on right click, long press,
5
+ * or the platform's context-menu keystroke, anchored at the pointer.
6
+ *
7
+ * Unlike Menu.Trigger it renders a plain element rather than a button: it wraps
8
+ * arbitrary — sometimes interactive — content, so it must not claim button
9
+ * semantics.
10
+ */
11
+ export type MenuContextTriggerProps = {
12
+ /** Whether the surface is disabled. The browser's own context menu is left alone. */
13
+ disabled?: boolean;
14
+ /** Whether a long press opens the menu on touch and pen. */
15
+ longPress?: boolean;
16
+ /** How long a touch has to be held, in ms. */
17
+ longPressDelay?: number;
18
+ /**
19
+ * Whether to suppress the iOS text callout / selection on the surface. Long press
20
+ * is unusable there without it; turn it off if the surface contains text the user
21
+ * is meant to select.
22
+ */
23
+ preventTouchCallout?: boolean;
24
+ /**
25
+ * Tab order position. Defaults to `0` so keyboard users can reach the surface and
26
+ * open the menu with Shift+F10. Pass `-1` when the surface lives inside a composite
27
+ * that already manages focus with a roving tabindex (a table, a tree).
28
+ */
29
+ tabindex?: number;
30
+ children?: Snippet;
31
+ class?: string;
32
+ element?: HTMLElement | null;
33
+ } & Omit<HTMLAttributes<HTMLDivElement>, 'class' | 'children' | 'tabindex'>;
34
+ declare const MenuContextTrigger: import("svelte").Component<MenuContextTriggerProps, {}, "element">;
35
+ type MenuContextTrigger = ReturnType<typeof MenuContextTrigger>;
36
+ export default MenuContextTrigger;
@@ -1,6 +1,7 @@
1
1
  import type { ComponentProps } from 'svelte';
2
2
  import type MenuRootComponent from './root/menu-root.svelte';
3
3
  import type MenuTriggerComponent from './trigger/menu-trigger.svelte';
4
+ import type MenuContextTriggerComponent from './context-trigger/menu-context-trigger.svelte';
4
5
  import type MenuContentComponent from './content/menu-content.svelte';
5
6
  import type MenuOverlayComponent from './overlay/menu-overlay.svelte';
6
7
  import type MenuItemComponent from './item/menu-item.svelte';
@@ -12,6 +13,7 @@ import type MenuSubmenuTriggerComponent from './submenu/menu-submenu-trigger.sve
12
13
  export * as Menu from './index.parts.js';
13
14
  export { default as MenuRoot } from './root/menu-root.svelte';
14
15
  export { default as MenuTrigger } from './trigger/menu-trigger.svelte';
16
+ export { default as MenuContextTrigger } from './context-trigger/menu-context-trigger.svelte';
15
17
  export { default as MenuContent } from './content/menu-content.svelte';
16
18
  export { default as MenuOverlay } from './overlay/menu-overlay.svelte';
17
19
  export { default as MenuItem } from './item/menu-item.svelte';
@@ -22,6 +24,7 @@ export { default as MenuSubmenuRoot } from './submenu/menu-submenu-root.svelte';
22
24
  export { default as MenuSubmenuTrigger } from './submenu/menu-submenu-trigger.svelte';
23
25
  export type MenuRootProps = ComponentProps<typeof MenuRootComponent>;
24
26
  export type MenuTriggerProps = ComponentProps<typeof MenuTriggerComponent>;
27
+ export type MenuContextTriggerProps = ComponentProps<typeof MenuContextTriggerComponent>;
25
28
  export type MenuContentProps = ComponentProps<typeof MenuContentComponent>;
26
29
  export type MenuOverlayProps = ComponentProps<typeof MenuOverlayComponent>;
27
30
  export type MenuItemProps = ComponentProps<typeof MenuItemComponent>;
@@ -30,6 +33,6 @@ export type MenuGroupProps = ComponentProps<typeof MenuGroupComponent>;
30
33
  export type MenuGroupLabelProps = ComponentProps<typeof MenuGroupLabelComponent>;
31
34
  export type MenuSubmenuRootProps = ComponentProps<typeof MenuSubmenuRootComponent>;
32
35
  export type MenuSubmenuTriggerProps = ComponentProps<typeof MenuSubmenuTriggerComponent>;
33
- export { getMenuContext, setMenuContext, useMenuContext, type MenuContext, type MenuItemData, type MenuCanonicalCloseReason, type MenuCloseReason, type MenuOpenReason, type MenuChangeReason, type MenuOpenChangeDetails, type MenuOpenFocusIntent } from './root/context.js';
36
+ export { getMenuContext, setMenuContext, useMenuContext, type MenuAnchorPoint, type MenuContext, type MenuItemData, type MenuCanonicalCloseReason, type MenuCloseReason, type MenuOpenReason, type MenuChangeReason, type MenuOpenChangeDetails, type MenuOpenFocusIntent } from './root/context.js';
34
37
  import * as MenuParts from './index.parts.js';
35
38
  export default MenuParts;
@@ -3,6 +3,7 @@ export * as Menu from './index.parts.js';
3
3
  // Direct named exports for individual imports
4
4
  export { default as MenuRoot } from './root/menu-root.svelte';
5
5
  export { default as MenuTrigger } from './trigger/menu-trigger.svelte';
6
+ export { default as MenuContextTrigger } from './context-trigger/menu-context-trigger.svelte';
6
7
  export { default as MenuContent } from './content/menu-content.svelte';
7
8
  export { default as MenuOverlay } from './overlay/menu-overlay.svelte';
8
9
  export { default as MenuItem } from './item/menu-item.svelte';
@@ -1,5 +1,6 @@
1
1
  export { default as Root } from './root/menu-root.svelte';
2
2
  export { default as Trigger } from './trigger/menu-trigger.svelte';
3
+ export { default as ContextTrigger } from './context-trigger/menu-context-trigger.svelte';
3
4
  export { default as Content } from './content/menu-content.svelte';
4
5
  export { default as Overlay } from './overlay/menu-overlay.svelte';
5
6
  export { default as Item } from './item/menu-item.svelte';
@@ -1,6 +1,7 @@
1
1
  // Namespace parts for <Menu.Root>, <Menu.Content>, etc.
2
2
  export { default as Root } from './root/menu-root.svelte';
3
3
  export { default as Trigger } from './trigger/menu-trigger.svelte';
4
+ export { default as ContextTrigger } from './context-trigger/menu-context-trigger.svelte';
4
5
  export { default as Content } from './content/menu-content.svelte';
5
6
  export { default as Overlay } from './overlay/menu-overlay.svelte';
6
7
  export { default as Item } from './item/menu-item.svelte';