@wentools/overlay 0.1.6 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/package.json +1 -1
- package/src/dropdown_menu.svelte +32 -14
package/README.md
CHANGED
|
@@ -30,8 +30,9 @@ Peer dependencies: `svelte >=5.0.0`, `@wentools/spatial >=0.2.0`, `@wentools/sim
|
|
|
30
30
|
</script>
|
|
31
31
|
|
|
32
32
|
<DropdownMenu>
|
|
33
|
-
{#snippet trigger({
|
|
34
|
-
|
|
33
|
+
{#snippet trigger({ open })}
|
|
34
|
+
<!-- DropdownMenu supplies the focusable menu-button wrapper. -->
|
|
35
|
+
<span>Menu {open ? 'open' : ''}</span>
|
|
35
36
|
{/snippet}
|
|
36
37
|
|
|
37
38
|
{#snippet children({ close })}
|
|
@@ -72,6 +73,9 @@ This means:
|
|
|
72
73
|
| `--popover-shadow-sm` | `--shadow-layer-1` → `0 2px 8px rgba(0,0,0,0.08)` | Hover |
|
|
73
74
|
| `--popover-item-hover-bg` | `--color-surface-hover` → `rgba(0,0,0,0.04)` | Dropdown, AC |
|
|
74
75
|
| `--popover-separator-color` | `--color-border-subtle` → `#e0e0e0` | Dropdown, MenuSeparator |
|
|
76
|
+
| `--popover-focus-width` | `--focus-width` → `2px` | Dropdown trigger |
|
|
77
|
+
| `--popover-focus-color` | `--focus-color` → `currentColor` | Dropdown trigger |
|
|
78
|
+
| `--popover-focus-offset` | `--focus-offset` → `2px` | Dropdown trigger |
|
|
75
79
|
|
|
76
80
|
### Consumer Scenarios
|
|
77
81
|
|
package/package.json
CHANGED
package/src/dropdown_menu.svelte
CHANGED
|
@@ -10,10 +10,8 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
10
10
|
@example
|
|
11
11
|
```svelte
|
|
12
12
|
<DropdownMenu>
|
|
13
|
-
{#snippet trigger({
|
|
14
|
-
<
|
|
15
|
-
Menu {open ? '▼' : '▶'}
|
|
16
|
-
</button>
|
|
13
|
+
{#snippet trigger({ open })}
|
|
14
|
+
<span>Menu {open ? '▼' : '▶'}</span>
|
|
17
15
|
{/snippet}
|
|
18
16
|
|
|
19
17
|
{#snippet children({ close })}
|
|
@@ -28,7 +26,7 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
28
26
|
```
|
|
29
27
|
-->
|
|
30
28
|
<script lang="ts">
|
|
31
|
-
import type
|
|
29
|
+
import { tick, type Snippet } from 'svelte'
|
|
32
30
|
import Popover from './popover.svelte'
|
|
33
31
|
import type { Position } from './types'
|
|
34
32
|
|
|
@@ -51,8 +49,10 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
51
49
|
initialFocusIndex?: number
|
|
52
50
|
/** Callback when open state changes */
|
|
53
51
|
onOpenChange?: (open: boolean) => void
|
|
54
|
-
/**
|
|
55
|
-
|
|
52
|
+
/** Accessible name for the menu content */
|
|
53
|
+
ariaLabel?: string
|
|
54
|
+
/** Non-interactive trigger content. DropdownMenu supplies the focusable menu button. */
|
|
55
|
+
trigger: Snippet<[{ open: boolean }]>
|
|
56
56
|
/** Dropdown content - should contain MenuItem components */
|
|
57
57
|
children: Snippet<[{ close: () => void; open: boolean }]>
|
|
58
58
|
}
|
|
@@ -66,11 +66,13 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
66
66
|
disabled = false,
|
|
67
67
|
initialFocusIndex = 0,
|
|
68
68
|
onOpenChange,
|
|
69
|
+
ariaLabel = 'Menu',
|
|
69
70
|
trigger: triggerSnippet,
|
|
70
71
|
children,
|
|
71
72
|
}: Props = $props()
|
|
72
73
|
|
|
73
74
|
let open = $state(false)
|
|
75
|
+
let triggerElement: HTMLElement | undefined = undefined
|
|
74
76
|
let menuElement: HTMLElement | undefined = undefined
|
|
75
77
|
let focusedIndex = $state(-1)
|
|
76
78
|
|
|
@@ -92,6 +94,7 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
92
94
|
open = false
|
|
93
95
|
onOpenChange?.(false)
|
|
94
96
|
focusedIndex = -1
|
|
97
|
+
tick().then(() => triggerElement?.focus())
|
|
95
98
|
}
|
|
96
99
|
|
|
97
100
|
const getMenuItems = (): HTMLElement[] => {
|
|
@@ -109,6 +112,8 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
109
112
|
|
|
110
113
|
const handleKeydown = (event: KeyboardEvent) => {
|
|
111
114
|
if (!open) return
|
|
115
|
+
const target = event.target as Node
|
|
116
|
+
if (!menuElement?.contains(target) && !triggerElement?.contains(target)) return
|
|
112
117
|
|
|
113
118
|
const items = getMenuItems()
|
|
114
119
|
const currentIndex = focusedIndex
|
|
@@ -148,6 +153,7 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
148
153
|
|
|
149
154
|
case 'Escape':
|
|
150
155
|
event.preventDefault()
|
|
156
|
+
event.stopImmediatePropagation()
|
|
151
157
|
close()
|
|
152
158
|
break
|
|
153
159
|
}
|
|
@@ -164,14 +170,14 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
164
170
|
}
|
|
165
171
|
</script>
|
|
166
172
|
|
|
167
|
-
<svelte:window
|
|
173
|
+
<svelte:window onkeydowncapture={handleKeydown} />
|
|
168
174
|
|
|
169
175
|
<Popover
|
|
170
176
|
bind:open
|
|
171
177
|
{position}
|
|
172
178
|
{offset}
|
|
173
179
|
closeOnClickOutside={true}
|
|
174
|
-
closeOnEscape={
|
|
180
|
+
closeOnEscape={false}
|
|
175
181
|
onOpenChange={(newOpen) => {
|
|
176
182
|
open = newOpen
|
|
177
183
|
onOpenChange?.(newOpen)
|
|
@@ -179,12 +185,12 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
179
185
|
focusedIndex = -1
|
|
180
186
|
}
|
|
181
187
|
}}
|
|
182
|
-
role="menu"
|
|
183
|
-
ariaLabel="Menu"
|
|
184
188
|
class="dropdown-menu {className}"
|
|
185
189
|
>
|
|
186
190
|
{#snippet trigger()}
|
|
187
191
|
<div
|
|
192
|
+
bind:this={triggerElement}
|
|
193
|
+
class="dropdown-trigger"
|
|
188
194
|
onclick={toggle}
|
|
189
195
|
onkeydown={(e) => {
|
|
190
196
|
if (e.key === 'Enter' || e.key === ' ') {
|
|
@@ -192,12 +198,13 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
192
198
|
toggle()
|
|
193
199
|
}
|
|
194
200
|
}}
|
|
195
|
-
tabindex=
|
|
201
|
+
tabindex={disabled ? undefined : 0}
|
|
196
202
|
role="button"
|
|
197
203
|
aria-haspopup="menu"
|
|
198
204
|
aria-expanded={open}
|
|
205
|
+
aria-disabled={disabled || undefined}
|
|
199
206
|
>
|
|
200
|
-
{@render triggerSnippet({
|
|
207
|
+
{@render triggerSnippet({ open })}
|
|
201
208
|
</div>
|
|
202
209
|
{/snippet}
|
|
203
210
|
|
|
@@ -208,7 +215,8 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
208
215
|
style:max-width={maxWidth}
|
|
209
216
|
style:min-width={minWidth}
|
|
210
217
|
role="menu"
|
|
211
|
-
|
|
218
|
+
aria-label={ariaLabel}
|
|
219
|
+
onfocusin={handleItemFocus}
|
|
212
220
|
>
|
|
213
221
|
{@render children({ close, open })}
|
|
214
222
|
</div>
|
|
@@ -216,6 +224,16 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
216
224
|
</Popover>
|
|
217
225
|
|
|
218
226
|
<style>
|
|
227
|
+
.dropdown-trigger {
|
|
228
|
+
border-radius: var(--popover-radius-sm, var(--radius-s, 6px));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
.dropdown-trigger:focus-visible {
|
|
232
|
+
outline: var(--popover-focus-width, var(--focus-width, 2px)) solid
|
|
233
|
+
var(--popover-focus-color, var(--focus-color, currentColor));
|
|
234
|
+
outline-offset: var(--popover-focus-offset, var(--focus-offset, 2px));
|
|
235
|
+
}
|
|
236
|
+
|
|
219
237
|
.dropdown-menu-content {
|
|
220
238
|
--menu-item-padding: var(--popover-padding, var(--space-xs, 4px)) var(--popover-padding-lg, var(--space-s, 8px));
|
|
221
239
|
--menu-item-hover-bg: var(--popover-item-hover-bg, var(--color-surface-hover, rgba(0,0,0,0.04)));
|