@spectral_labs/ui 1.5.1 → 1.5.3

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 (62) hide show
  1. package/dist/components/context-menu/components/context-menu-content.svelte +8 -7
  2. package/dist/components/context-menu/components/context-menu-content.svelte.d.ts.map +1 -1
  3. package/dist/components/context-menu/components/context-menu-root.svelte +9 -7
  4. package/dist/components/context-menu/components/context-menu-root.svelte.d.ts.map +1 -1
  5. package/dist/components/context-menu/index.d.ts +3 -0
  6. package/dist/components/context-menu/index.d.ts.map +1 -1
  7. package/dist/components/context-menu/index.js +2 -0
  8. package/dist/components/dropdown-menu/__tests__/DropdownMenuRadioTest.svelte +90 -0
  9. package/dist/components/dropdown-menu/__tests__/DropdownMenuTest.svelte +22 -1
  10. package/dist/components/dropdown-menu/__tests__/dropdown-menu-radio-group.test.svelte.js +306 -0
  11. package/dist/components/dropdown-menu/__tests__/dropdown-menu.test.svelte.js +438 -1
  12. package/dist/components/dropdown-menu/components/dropdown-menu-checkbox-item.svelte +10 -0
  13. package/dist/components/dropdown-menu/components/dropdown-menu-checkbox-item.svelte.d.ts.map +1 -1
  14. package/dist/components/dropdown-menu/components/dropdown-menu-content.svelte +8 -7
  15. package/dist/components/dropdown-menu/components/dropdown-menu-content.svelte.d.ts.map +1 -1
  16. package/dist/components/dropdown-menu/components/dropdown-menu-item.svelte +8 -0
  17. package/dist/components/dropdown-menu/components/dropdown-menu-item.svelte.d.ts.map +1 -1
  18. package/dist/components/dropdown-menu/components/dropdown-menu-radio-group.svelte +45 -0
  19. package/dist/components/dropdown-menu/components/dropdown-menu-radio-group.svelte.d.ts +5 -0
  20. package/dist/components/dropdown-menu/components/dropdown-menu-radio-group.svelte.d.ts.map +1 -0
  21. package/dist/components/dropdown-menu/components/dropdown-menu-radio-item.svelte +143 -0
  22. package/dist/components/dropdown-menu/components/dropdown-menu-radio-item.svelte.d.ts +5 -0
  23. package/dist/components/dropdown-menu/components/dropdown-menu-radio-item.svelte.d.ts.map +1 -0
  24. package/dist/components/dropdown-menu/components/dropdown-menu-root.svelte +9 -7
  25. package/dist/components/dropdown-menu/components/dropdown-menu-root.svelte.d.ts.map +1 -1
  26. package/dist/components/dropdown-menu/components/dropdown-menu-sub-content.svelte +94 -4
  27. package/dist/components/dropdown-menu/components/dropdown-menu-sub-content.svelte.d.ts.map +1 -1
  28. package/dist/components/dropdown-menu/components/dropdown-menu-sub-trigger.svelte +32 -7
  29. package/dist/components/dropdown-menu/components/dropdown-menu-sub-trigger.svelte.d.ts.map +1 -1
  30. package/dist/components/dropdown-menu/components/dropdown-menu-sub.svelte +180 -6
  31. package/dist/components/dropdown-menu/components/dropdown-menu-sub.svelte.d.ts.map +1 -1
  32. package/dist/components/dropdown-menu/dropdown-menu.svelte.d.ts +48 -2
  33. package/dist/components/dropdown-menu/dropdown-menu.svelte.d.ts.map +1 -1
  34. package/dist/components/dropdown-menu/dropdown-menu.svelte.js +16 -0
  35. package/dist/components/dropdown-menu/index.d.ts +3 -1
  36. package/dist/components/dropdown-menu/index.d.ts.map +1 -1
  37. package/dist/components/dropdown-menu/index.js +2 -0
  38. package/dist/components/dropdown-menu/types.d.ts +37 -1
  39. package/dist/components/dropdown-menu/types.d.ts.map +1 -1
  40. package/dist/components/menubar/__tests__/menubar.test.svelte.js +95 -0
  41. package/dist/components/menubar/components/menubar-content.svelte +8 -7
  42. package/dist/components/menubar/components/menubar-content.svelte.d.ts.map +1 -1
  43. package/dist/components/menubar/components/menubar-menu.svelte +8 -6
  44. package/dist/components/menubar/components/menubar-menu.svelte.d.ts.map +1 -1
  45. package/dist/components/menubar/index.d.ts +3 -1
  46. package/dist/components/menubar/index.d.ts.map +1 -1
  47. package/dist/components/menubar/index.js +2 -0
  48. package/dist/internal/escape-layer/__tests__/escape-layer.test.js +29 -0
  49. package/dist/internal/escape-layer/escape-layer.svelte.d.ts +5 -1
  50. package/dist/internal/escape-layer/escape-layer.svelte.d.ts.map +1 -1
  51. package/dist/internal/escape-layer/escape-layer.svelte.js +20 -13
  52. package/dist/internal/menu-context/index.d.ts +2 -2
  53. package/dist/internal/menu-context/index.d.ts.map +1 -1
  54. package/dist/internal/menu-context/index.js +1 -1
  55. package/dist/internal/menu-context/menu-context.svelte.d.ts +64 -2
  56. package/dist/internal/menu-context/menu-context.svelte.d.ts.map +1 -1
  57. package/dist/internal/menu-context/menu-context.svelte.js +59 -0
  58. package/dist/internal/roving-focus/roving-focus.svelte.d.ts.map +1 -1
  59. package/dist/internal/roving-focus/roving-focus.svelte.js +2 -1
  60. package/dist/internal/roving-focus/types.d.ts +6 -0
  61. package/dist/internal/roving-focus/types.d.ts.map +1 -1
  62. package/package.json +1 -1
@@ -4,6 +4,7 @@
4
4
  import { EscapeLayerState } from '../../../internal/escape-layer/index.js'
5
5
  import { FloatingLayerState } from '../../../internal/floating-layer/index.js'
6
6
  import { isBrowser, kbd } from '../../../internal/helpers/index.js'
7
+ import { getOwnLevelMenuItems, isOwnLevelMenuItem } from '../../../internal/menu-context/index.js'
7
8
  import { Portal } from '../../../internal/portal/index.js'
8
9
  import { PresenceLayerState } from '../../../internal/presence-layer/index.js'
9
10
  import { RovingFocusGroup } from '../../../internal/roving-focus/index.js'
@@ -19,6 +20,9 @@
19
20
  orientation: 'vertical',
20
21
  loop: true,
21
22
  candidateSelector: '[data-roving-focus-item]',
23
+ // Sub content renders inline inside this content: exclude its rows from
24
+ // the parent collection (the sub content has its own roving group).
25
+ candidateFilter: (el) => !contentEl || isOwnLevelMenuItem(contentEl, el),
22
26
  })
23
27
 
24
28
  const presence = new PresenceLayerState({
@@ -85,10 +89,8 @@
85
89
  floating.setElements(virtualRef, contentEl)
86
90
  floating.startAutoUpdate()
87
91
 
88
- // Focus first interactive item.
89
- const firstItem = contentEl.querySelector<HTMLElement>(
90
- '[data-roving-focus-item]:not([data-disabled])',
91
- )
92
+ // Focus first interactive item (own level only — never a sub row).
93
+ const firstItem = getOwnLevelMenuItems(contentEl)[0]
92
94
  firstItem?.focus()
93
95
  }
94
96
 
@@ -106,9 +108,8 @@
106
108
  function onkeydown(event: KeyboardEvent) {
107
109
  if (!contentEl) return
108
110
 
109
- const items = Array.from(
110
- contentEl.querySelectorAll<HTMLElement>('[data-roving-focus-item]:not([data-disabled])'),
111
- )
111
+ // Typeahead collection scoped to this level (sub rows excluded).
112
+ const items = getOwnLevelMenuItems(contentEl)
112
113
 
113
114
  switch (event.key) {
114
115
  case kbd.ARROW_DOWN: {
@@ -1 +1 @@
1
- {"version":3,"file":"context-menu-content.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/context-menu/components/context-menu-content.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAyK1D,QAAA,MAAM,kBAAkB,6DAAwC,CAAC;AACjE,KAAK,kBAAkB,GAAG,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAChE,eAAe,kBAAkB,CAAC"}
1
+ {"version":3,"file":"context-menu-content.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/context-menu/components/context-menu-content.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AA2K1D,QAAA,MAAM,kBAAkB,6DAAwC,CAAC;AACjE,KAAK,kBAAkB,GAAG,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAChE,eAAe,kBAAkB,CAAC"}
@@ -1,6 +1,6 @@
1
1
  <script lang="ts">
2
2
  import type { ContextMenuProps } from '../types.js'
3
- import { setMenuContext } from '../../../internal/menu-context/index.js'
3
+ import { createMenuContext, setMenuContext } from '../../../internal/menu-context/index.js'
4
4
  import { createContextMenuIds, setContextMenuContext } from '../context-menu.svelte.js'
5
5
 
6
6
  let {
@@ -13,12 +13,14 @@
13
13
  const ids = createContextMenuIds()
14
14
  let position = $state({ x: 0, y: 0 })
15
15
 
16
- setMenuContext({
17
- close() {
18
- open = false
19
- onOpenChange?.(false)
20
- },
21
- })
16
+ setMenuContext(
17
+ createMenuContext({
18
+ close() {
19
+ open = false
20
+ onOpenChange?.(false)
21
+ },
22
+ }),
23
+ )
22
24
 
23
25
  setContextMenuContext({
24
26
  get open() {
@@ -1 +1 @@
1
- {"version":3,"file":"context-menu-root.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/context-menu/components/context-menu-root.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAoDnD,QAAA,MAAM,eAAe,0DAAwC,CAAC;AAC9D,KAAK,eAAe,GAAG,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC;AAC1D,eAAe,eAAe,CAAC"}
1
+ {"version":3,"file":"context-menu-root.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/context-menu/components/context-menu-root.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAsDnD,QAAA,MAAM,eAAe,0DAAwC,CAAC;AAC9D,KAAK,eAAe,GAAG,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC;AAC1D,eAAe,eAAe,CAAC"}
@@ -1,7 +1,10 @@
1
1
  export { default as CheckboxItem } from '../dropdown-menu/components/dropdown-menu-checkbox-item.svelte';
2
2
  export { default as Item } from '../dropdown-menu/components/dropdown-menu-item.svelte';
3
3
  export { default as Label } from '../dropdown-menu/components/dropdown-menu-label.svelte';
4
+ export { default as RadioGroup } from '../dropdown-menu/components/dropdown-menu-radio-group.svelte';
5
+ export { default as RadioItem } from '../dropdown-menu/components/dropdown-menu-radio-item.svelte';
4
6
  export { default as Separator } from '../dropdown-menu/components/dropdown-menu-separator.svelte';
7
+ export type { DropdownMenuRadioGroupProps as ContextMenuRadioGroupProps, DropdownMenuRadioItemProps as ContextMenuRadioItemProps, } from '../dropdown-menu/types.js';
5
8
  export { default as Content } from './components/context-menu-content.svelte';
6
9
  export { default as Root } from './components/context-menu-root.svelte';
7
10
  export { default as Trigger } from './components/context-menu-trigger.svelte';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/components/context-menu/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,gEAAgE,CAAA;AACxG,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,uDAAuD,CAAA;AACvF,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,wDAAwD,CAAA;AACzF,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,4DAA4D,CAAA;AACjG,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,0CAA0C,CAAA;AAC7E,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,uCAAuC,CAAA;AACvE,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,0CAA0C,CAAA;AAC7E,YAAY,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/components/context-menu/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,gEAAgE,CAAA;AACxG,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,uDAAuD,CAAA;AACvF,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,wDAAwD,CAAA;AACzF,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,8DAA8D,CAAA;AACpG,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,6DAA6D,CAAA;AAClG,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,4DAA4D,CAAA;AACjG,YAAY,EACV,2BAA2B,IAAI,0BAA0B,EACzD,0BAA0B,IAAI,yBAAyB,GACxD,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,0CAA0C,CAAA;AAC7E,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,uCAAuC,CAAA;AACvE,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,0CAA0C,CAAA;AAC7E,YAAY,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA"}
@@ -2,6 +2,8 @@
2
2
  export { default as CheckboxItem } from '../dropdown-menu/components/dropdown-menu-checkbox-item.svelte';
3
3
  export { default as Item } from '../dropdown-menu/components/dropdown-menu-item.svelte';
4
4
  export { default as Label } from '../dropdown-menu/components/dropdown-menu-label.svelte';
5
+ export { default as RadioGroup } from '../dropdown-menu/components/dropdown-menu-radio-group.svelte';
6
+ export { default as RadioItem } from '../dropdown-menu/components/dropdown-menu-radio-item.svelte';
5
7
  export { default as Separator } from '../dropdown-menu/components/dropdown-menu-separator.svelte';
6
8
  export { default as Content } from './components/context-menu-content.svelte';
7
9
  export { default as Root } from './components/context-menu-root.svelte';
@@ -0,0 +1,90 @@
1
+ <script lang="ts">
2
+ import DropdownMenuContent from '../components/dropdown-menu-content.svelte'
3
+ import DropdownMenuItem from '../components/dropdown-menu-item.svelte'
4
+ import DropdownMenuRadioGroup from '../components/dropdown-menu-radio-group.svelte'
5
+ import DropdownMenuRadioItem from '../components/dropdown-menu-radio-item.svelte'
6
+ import DropdownMenuRoot from '../components/dropdown-menu-root.svelte'
7
+ import DropdownMenuTrigger from '../components/dropdown-menu-trigger.svelte'
8
+
9
+ let {
10
+ open = $bindable(true),
11
+ value = $bindable('document'),
12
+ portalTo,
13
+ onValueChange,
14
+ closeOnSelect = undefined,
15
+ disabledValue = undefined,
16
+ textValues = undefined,
17
+ itemAttrs = undefined,
18
+ onItemClick = undefined,
19
+ withLeadingItem = false,
20
+ orphan = false,
21
+ groupAttrs = undefined,
22
+ onItemKeydown = undefined,
23
+ onItemSelect = undefined,
24
+ parentTarget = 'bibliographie',
25
+ }: {
26
+ open?: boolean
27
+ value?: string
28
+ portalTo?: HTMLElement | string
29
+ onValueChange?: (value: string) => void
30
+ closeOnSelect?: boolean
31
+ /** Value of the single item rendered disabled. */
32
+ disabledValue?: string
33
+ /** Per-value `textValue` overrides for typeahead. */
34
+ textValues?: Record<string, string>
35
+ /** Arbitrary HTML attributes spread onto the `placeholders` item. */
36
+ itemAttrs?: Record<string, unknown>
37
+ /** Consumer-supplied click handler on the `placeholders` item. */
38
+ onItemClick?: (event: MouseEvent) => void
39
+ /** A plain Item BEFORE the group — proves roving spans both kinds. */
40
+ withLeadingItem?: boolean
41
+ /** Renders a RadioItem with NO enclosing RadioGroup (guard-rail case). */
42
+ orphan?: boolean
43
+ /** Arbitrary HTML attributes spread onto the `role="group"` node. */
44
+ groupAttrs?: Record<string, unknown>
45
+ /** Consumer-supplied keydown handler on the `placeholders` item. */
46
+ onItemKeydown?: (event: KeyboardEvent) => void
47
+ /** Consumer-supplied `onSelect` on the `placeholders` item. */
48
+ onItemSelect?: (event: Event) => void
49
+ /** Value the PARENT-driven button assigns (see `parent-set` below). */
50
+ parentTarget?: string
51
+ } = $props()
52
+
53
+ const OPTIONS = [
54
+ { value: 'document', label: 'Document' },
55
+ { value: 'placeholders', label: 'Placeholders' },
56
+ { value: 'bibliographie', label: 'Bibliographie' },
57
+ ]
58
+ </script>
59
+
60
+ <!-- Lives OUTSIDE the menu: clicking it is the PARENT moving `value`, the
61
+ one path that must move the checked row without any notification. -->
62
+ <button data-testid="parent-set" onclick={() => (value = parentTarget)}>set from parent</button>
63
+
64
+ <DropdownMenuRoot bind:open>
65
+ <DropdownMenuTrigger>Affichage</DropdownMenuTrigger>
66
+ <DropdownMenuContent {portalTo}>
67
+ {#if withLeadingItem}
68
+ <DropdownMenuItem>Plan</DropdownMenuItem>
69
+ {/if}
70
+ {#if orphan}
71
+ <DropdownMenuRadioItem value="orphan">Orphan</DropdownMenuRadioItem>
72
+ {/if}
73
+ <DropdownMenuRadioGroup bind:value {onValueChange} {...groupAttrs ?? {}}>
74
+ {#each OPTIONS as option (option.value)}
75
+ <DropdownMenuRadioItem
76
+ value={option.value}
77
+ disabled={option.value === disabledValue}
78
+ textValue={textValues?.[option.value]}
79
+ {closeOnSelect}
80
+ onclick={option.value === 'placeholders' ? onItemClick : undefined}
81
+ onkeydown={option.value === 'placeholders' ? onItemKeydown : undefined}
82
+ onSelect={option.value === 'placeholders' ? onItemSelect : undefined}
83
+ {...option.value === 'placeholders' ? (itemAttrs ?? {}) : {}}
84
+ >
85
+ {option.label}
86
+ </DropdownMenuRadioItem>
87
+ {/each}
88
+ </DropdownMenuRadioGroup>
89
+ </DropdownMenuContent>
90
+ </DropdownMenuRoot>
@@ -1,4 +1,5 @@
1
1
  <script lang="ts">
2
+ import DropdownMenuCheckboxItem from '../components/dropdown-menu-checkbox-item.svelte'
2
3
  import DropdownMenuContent from '../components/dropdown-menu-content.svelte'
3
4
  import DropdownMenuItem from '../components/dropdown-menu-item.svelte'
4
5
  import DropdownMenuRoot from '../components/dropdown-menu-root.svelte'
@@ -14,6 +15,9 @@
14
15
  withSub = false,
15
16
  subFirst = false,
16
17
  subDisabled = false,
18
+ subTextValue = undefined,
19
+ withCheckbox = false,
20
+ withSecondSub = false,
17
21
  }: {
18
22
  open?: boolean
19
23
  portalTo?: HTMLElement | string
@@ -21,12 +25,17 @@
21
25
  withSub?: boolean
22
26
  subFirst?: boolean
23
27
  subDisabled?: boolean
28
+ subTextValue?: string
29
+ withCheckbox?: boolean
30
+ withSecondSub?: boolean
24
31
  } = $props()
25
32
  </script>
26
33
 
27
34
  {#snippet subBlock()}
28
35
  <DropdownMenuSub>
29
- <DropdownMenuSubTrigger disabled={subDisabled}>More options</DropdownMenuSubTrigger>
36
+ <DropdownMenuSubTrigger disabled={subDisabled} textValue={subTextValue}
37
+ >More options</DropdownMenuSubTrigger
38
+ >
30
39
  <DropdownMenuSubContent>
31
40
  <DropdownMenuItem>Sub Item 1</DropdownMenuItem>
32
41
  <DropdownMenuItem>Sub Item 2</DropdownMenuItem>
@@ -43,8 +52,20 @@
43
52
  <DropdownMenuItem>Item 1</DropdownMenuItem>
44
53
  <DropdownMenuItem>Item 2</DropdownMenuItem>
45
54
  <DropdownMenuItem>Item 3</DropdownMenuItem>
55
+ {#if withCheckbox}
56
+ <DropdownMenuCheckboxItem>Notifications</DropdownMenuCheckboxItem>
57
+ {/if}
46
58
  {#if withSub && !subFirst}
47
59
  {@render subBlock()}
48
60
  {/if}
61
+ {#if withSecondSub}
62
+ <DropdownMenuSub>
63
+ <DropdownMenuSubTrigger>Other options</DropdownMenuSubTrigger>
64
+ <DropdownMenuSubContent>
65
+ <DropdownMenuItem>Sub B Item 1</DropdownMenuItem>
66
+ <DropdownMenuItem>Sub B Item 2</DropdownMenuItem>
67
+ </DropdownMenuSubContent>
68
+ </DropdownMenuSub>
69
+ {/if}
49
70
  </DropdownMenuContent>
50
71
  </DropdownMenuRoot>
@@ -0,0 +1,306 @@
1
+ import { flushSync, mount, unmount } from 'svelte';
2
+ import { afterEach, describe, expect, it, vi } from 'vitest';
3
+ import DropdownMenuRadioTest from './DropdownMenuRadioTest.svelte';
4
+ /**
5
+ * `DropdownMenu.RadioGroup` / `RadioItem` — the single-choice row family.
6
+ *
7
+ * These rows exist because a menu that offers "pick exactly one of N" had no
8
+ * honest markup here: `CheckboxItem` announces `menuitemcheckbox`, which tells
9
+ * a screen-reader user the options are independent when they are not.
10
+ *
11
+ * The suite also pins the two properties this family is REQUIRED to have from
12
+ * birth, and which the older rows lack (spectral-ui#201): consumer HTML
13
+ * attributes reach the DOM node, and a consumer event handler COMPOSES with
14
+ * the built-in one instead of being dropped.
15
+ */
16
+ describe('dropdown-menu radio group', () => {
17
+ let target;
18
+ let instance;
19
+ function setup(props = {}) {
20
+ target = document.createElement('div');
21
+ document.body.appendChild(target);
22
+ instance = mount(DropdownMenuRadioTest, {
23
+ target,
24
+ props: { portalTo: target, ...props },
25
+ });
26
+ flushSync();
27
+ return target;
28
+ }
29
+ function radios() {
30
+ return Array.from(target.querySelectorAll('[role="menuitemradio"]'));
31
+ }
32
+ function radio(value) {
33
+ const el = target.querySelector(`[role="menuitemradio"][data-value="${value}"]`);
34
+ if (!el)
35
+ throw new Error(`no radio item for value "${value}"`);
36
+ return el;
37
+ }
38
+ /** Same idiom as `dropdown-menu.test.svelte.ts`: drive the REAL focused
39
+ * element, so the Content -> collection -> focus chain is exercised rather
40
+ * than merely inspected. */
41
+ function pressKey(key) {
42
+ const el = document.activeElement;
43
+ el.dispatchEvent(new KeyboardEvent('keydown', { key, bubbles: true }));
44
+ flushSync();
45
+ }
46
+ function activeValue() {
47
+ return document.activeElement?.getAttribute('data-value') ?? null;
48
+ }
49
+ afterEach(() => {
50
+ if (instance)
51
+ unmount(instance);
52
+ instance = undefined;
53
+ target?.remove();
54
+ });
55
+ // ── ARIA ──────────────────────────────────────────────────────────────
56
+ it('wraps its rows in role="group" — never role="radiogroup", invalid inside a menu', () => {
57
+ setup();
58
+ expect(target.querySelector('.sp-dropdown-menu-radio-group')?.getAttribute('role')).toBe('group');
59
+ // The ratified contract FORBIDS `radiogroup`: a `menu` may only own
60
+ // menuitem / menuitemcheckbox / menuitemradio / group / separator.
61
+ expect(target.querySelector('[role="radiogroup"]')).toBeNull();
62
+ const items = radios();
63
+ expect(items).toHaveLength(3);
64
+ expect(items.map((el) => el.getAttribute('aria-checked'))).toEqual(['true', 'false', 'false']);
65
+ });
66
+ it('reflects the selected value in data-state, for styling parity with CheckboxItem', () => {
67
+ setup();
68
+ expect(radio('document').getAttribute('data-state')).toBe('checked');
69
+ expect(radio('placeholders').getAttribute('data-state')).toBe('unchecked');
70
+ });
71
+ it('moves the checked row when the PARENT changes `value` after mount', () => {
72
+ // Mounting straight at the target value would stay green even if the group
73
+ // snapshotted the prop at init — the move has to happen AFTER mount, driven
74
+ // from outside, with the menu open.
75
+ const calls = [];
76
+ setup({ onValueChange: (v) => calls.push(v) });
77
+ expect(radios().map((el) => el.getAttribute('aria-checked'))).toEqual([
78
+ 'true',
79
+ 'false',
80
+ 'false',
81
+ ]);
82
+ target.querySelector('[data-testid="parent-set"]').click();
83
+ flushSync();
84
+ expect(radios().map((el) => el.getAttribute('aria-checked'))).toEqual([
85
+ 'false',
86
+ 'false',
87
+ 'true',
88
+ ]);
89
+ expect(radio('bibliographie').getAttribute('data-state')).toBe('checked');
90
+ // The parent moved it; the group has nothing to report back.
91
+ expect(calls).toEqual([]);
92
+ });
93
+ // ── Selection ─────────────────────────────────────────────────────────
94
+ // `closeOnSelect: false` in the selection tests below is not incidental: the
95
+ // default closes the menu, which unmounts the rows — a post-click assertion
96
+ // on `aria-checked` would then fail for the wrong reason. Closing has its
97
+ // own test.
98
+ it('selects on click and notifies once with the new value', () => {
99
+ const calls = [];
100
+ let selects = 0;
101
+ setup({
102
+ closeOnSelect: false,
103
+ onItemSelect: () => selects++,
104
+ onValueChange: (v) => calls.push(v),
105
+ });
106
+ radio('placeholders').click();
107
+ flushSync();
108
+ expect(calls).toEqual(['placeholders']);
109
+ expect(radio('placeholders').getAttribute('aria-checked')).toBe('true');
110
+ expect(radio('document').getAttribute('aria-checked')).toBe('false');
111
+ // Proves the `onSelect` wiring is live — without this, the `select: 0` of
112
+ // the disabled test below would hold no matter what the component did.
113
+ expect(selects).toBe(1);
114
+ });
115
+ it('does NOT re-notify when the already-selected row is chosen again', () => {
116
+ const calls = [];
117
+ setup({ onValueChange: (v) => calls.push(v) });
118
+ radio('document').click();
119
+ flushSync();
120
+ expect(calls).toEqual([]);
121
+ });
122
+ it('selects on Enter and on Space', () => {
123
+ const calls = [];
124
+ setup({ closeOnSelect: false, onValueChange: (v) => calls.push(v) });
125
+ radio('placeholders').dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
126
+ flushSync();
127
+ expect(calls).toEqual(['placeholders']);
128
+ radio('bibliographie').dispatchEvent(new KeyboardEvent('keydown', { key: ' ', bubbles: true }));
129
+ flushSync();
130
+ expect(calls).toEqual(['placeholders', 'bibliographie']);
131
+ });
132
+ it('closes the menu after a selection, and keeps it open when closeOnSelect is false', () => {
133
+ setup();
134
+ radio('placeholders').click();
135
+ flushSync();
136
+ expect(target.querySelector('[role="menu"]')).toBeNull();
137
+ unmount(instance);
138
+ instance = undefined;
139
+ target.remove();
140
+ setup({ closeOnSelect: false });
141
+ radio('placeholders').click();
142
+ flushSync();
143
+ expect(target.querySelector('[role="menu"]')).toBeTruthy();
144
+ });
145
+ // ── Disabled ──────────────────────────────────────────────────────────
146
+ it('a disabled row is TOTALLY inert — the consumer handler does not run either', () => {
147
+ const counts = { click: 0, keydown: 0, select: 0, change: 0 };
148
+ setup({
149
+ disabledValue: 'placeholders',
150
+ onItemClick: () => counts.click++,
151
+ onItemKeydown: () => counts.keydown++,
152
+ onItemSelect: () => counts.select++,
153
+ onValueChange: () => counts.change++,
154
+ });
155
+ const disabled = radio('placeholders');
156
+ expect(disabled.getAttribute('aria-disabled')).toBe('true');
157
+ expect(disabled.hasAttribute('data-disabled')).toBe(true);
158
+ disabled.click();
159
+ flushSync();
160
+ disabled.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
161
+ flushSync();
162
+ disabled.dispatchEvent(new KeyboardEvent('keydown', { key: ' ', bubbles: true }));
163
+ flushSync();
164
+ // A dimmed, unselectable row that still fires an application effect is not
165
+ // disabled — it only LOOKS disabled.
166
+ expect(counts).toEqual({ click: 0, keydown: 0, select: 0, change: 0 });
167
+ expect(disabled.getAttribute('aria-checked')).toBe('false');
168
+ // ... and none of it dismissed the menu.
169
+ expect(target.querySelector('[role="menu"]')).toBeTruthy();
170
+ // `getOwnLevelMenuItems` filters on `[data-roving-focus-item]:not([data-disabled])`.
171
+ const rovable = target.querySelectorAll('[data-roving-focus-item]:not([data-disabled])');
172
+ expect(Array.from(rovable)).not.toContain(disabled);
173
+ });
174
+ // ── Keyboard: the chain, not the wiring ───────────────────────────────
175
+ //
176
+ // The `data-*` hooks below can all be present while the
177
+ // Content -> collection -> focus chain is broken, so these drive the real
178
+ // focused element instead of asserting attributes.
179
+ it('is reachable by ArrowDown from a plain Item, and Enter selects the FOCUSED row', () => {
180
+ const calls = [];
181
+ setup({
182
+ withLeadingItem: true,
183
+ closeOnSelect: false,
184
+ onValueChange: (v) => calls.push(v),
185
+ });
186
+ // Opening autofocuses the first roving row — the plain Item.
187
+ expect(document.activeElement?.textContent?.trim()).toBe('Plan');
188
+ pressKey('ArrowDown');
189
+ expect(document.activeElement?.getAttribute('role')).toBe('menuitemradio');
190
+ expect(activeValue()).toBe('document');
191
+ pressKey('ArrowDown');
192
+ expect(activeValue()).toBe('placeholders');
193
+ pressKey('Enter');
194
+ expect(calls).toEqual(['placeholders']);
195
+ expect(radio('placeholders').getAttribute('aria-checked')).toBe('true');
196
+ });
197
+ it('roving SKIPS a disabled row on ArrowDown', () => {
198
+ setup({ withLeadingItem: true, disabledValue: 'placeholders' });
199
+ expect(document.activeElement?.textContent?.trim()).toBe('Plan');
200
+ pressKey('ArrowDown');
201
+ expect(activeValue()).toBe('document');
202
+ pressKey('ArrowDown');
203
+ // `placeholders` is disabled — roving lands on the next enabled row.
204
+ expect(activeValue()).toBe('bibliographie');
205
+ });
206
+ it('typeahead focuses the matching row, and `textValue` redirects the match', () => {
207
+ setup({ withLeadingItem: true });
208
+ pressKey('b');
209
+ expect(activeValue()).toBe('bibliographie');
210
+ unmount(instance);
211
+ instance = undefined;
212
+ target.remove();
213
+ // With the override, `Bibliographie` no longer answers to `b` — and no
214
+ // other row does, so focus stays put.
215
+ setup({ withLeadingItem: true, textValues: { bibliographie: 'zzz' } });
216
+ const before = document.activeElement;
217
+ pressKey('b');
218
+ expect(document.activeElement).toBe(before);
219
+ // Remount before the second probe: typeahead ACCUMULATES a buffer, so a
220
+ // `z` typed after a failed `b` searches for "bz" and would match nothing —
221
+ // the test would then pass or fail for a reason unrelated to `textValue`.
222
+ unmount(instance);
223
+ instance = undefined;
224
+ target.remove();
225
+ setup({ withLeadingItem: true, textValues: { bibliographie: 'zzz' } });
226
+ pressKey('z');
227
+ expect(activeValue()).toBe('bibliographie');
228
+ });
229
+ // ── Roving focus + typeahead wiring ───────────────────────────────────
230
+ it('joins the roving collection alongside plain items', () => {
231
+ setup({ withLeadingItem: true });
232
+ const rovable = Array.from(target.querySelectorAll('[data-roving-focus-item]:not([data-disabled])'));
233
+ // Plan + the three radio rows, in document order.
234
+ expect(rovable).toHaveLength(4);
235
+ expect(rovable[0].getAttribute('role')).toBe('menuitem');
236
+ expect(rovable.slice(1).map((el) => el.getAttribute('role'))).toEqual([
237
+ 'menuitemradio',
238
+ 'menuitemradio',
239
+ 'menuitemradio',
240
+ ]);
241
+ });
242
+ it('exposes its label to typeahead, and lets `textValue` override it', () => {
243
+ setup({ textValues: { bibliographie: 'zzz' } });
244
+ // No override → the content's typeahead falls back to textContent.
245
+ expect(radio('document').getAttribute('data-text-value')).toBeNull();
246
+ expect(radio('document').textContent?.trim()).toBe('Document');
247
+ // Override → the attribute the content reads FIRST.
248
+ expect(radio('bibliographie').getAttribute('data-text-value')).toBe('zzz');
249
+ });
250
+ // ── The two properties #201 costs the older rows ───────────────────────
251
+ it('forwards consumer HTML attributes to the GROUP element', () => {
252
+ setup({ groupAttrs: { 'aria-label': 'Panneau', 'data-testid': 'view-group' } });
253
+ const group = target.querySelector('.sp-dropdown-menu-radio-group');
254
+ expect(group.getAttribute('aria-label')).toBe('Panneau');
255
+ expect(group.getAttribute('data-testid')).toBe('view-group');
256
+ // The role stays ours.
257
+ expect(group.getAttribute('role')).toBe('group');
258
+ });
259
+ it('forwards consumer HTML attributes to the row element', () => {
260
+ setup({ itemAttrs: { 'data-testid': 'view-placeholders', 'aria-keyshortcuts': 'p' } });
261
+ const el = radio('placeholders');
262
+ expect(el.getAttribute('data-testid')).toBe('view-placeholders');
263
+ expect(el.getAttribute('aria-keyshortcuts')).toBe('p');
264
+ });
265
+ it('keeps its own class alongside a consumer class', () => {
266
+ setup({ itemAttrs: { class: 'view-row' } });
267
+ const el = radio('placeholders');
268
+ expect(el.classList.contains('view-row')).toBe(true);
269
+ expect(el.classList.contains('sp-dropdown-menu-item')).toBe(true);
270
+ });
271
+ it('cOMPOSES a consumer handler with the built-in selection', () => {
272
+ const seen = [];
273
+ setup({
274
+ closeOnSelect: false,
275
+ onItemClick: () => seen.push('consumer'),
276
+ onValueChange: (v) => seen.push(`selected:${v}`),
277
+ });
278
+ radio('placeholders').click();
279
+ flushSync();
280
+ // Both ran, consumer first — and selection actually happened.
281
+ expect(seen).toEqual(['consumer', 'selected:placeholders']);
282
+ });
283
+ it('lets a consumer handler cancel the selection with preventDefault', () => {
284
+ const calls = [];
285
+ setup({
286
+ onItemClick: (event) => event.preventDefault(),
287
+ onValueChange: (v) => calls.push(v),
288
+ });
289
+ radio('placeholders').click();
290
+ flushSync();
291
+ expect(calls).toEqual([]);
292
+ expect(radio('placeholders').getAttribute('aria-checked')).toBe('false');
293
+ // A cancelled selection must not close the menu either.
294
+ expect(target.querySelector('[role="menu"]')).toBeTruthy();
295
+ });
296
+ // ── Guard rail ────────────────────────────────────────────────────────
297
+ it('throws a NAMED error when a RadioItem is used outside a RadioGroup', () => {
298
+ const spy = vi.spyOn(console, 'error').mockImplementation(() => { });
299
+ try {
300
+ expect(() => setup({ orphan: true })).toThrow(/RadioGroup/);
301
+ }
302
+ finally {
303
+ spy.mockRestore();
304
+ }
305
+ });
306
+ });