@wentools/overlay 0.1.5 → 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 +38 -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
|
|
|
@@ -39,6 +37,10 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
39
37
|
offset?: number
|
|
40
38
|
/** Maximum width of dropdown content */
|
|
41
39
|
maxWidth?: string
|
|
40
|
+
/** Minimum width of dropdown content. Unset by default, so the menu
|
|
41
|
+
* shrink-wraps to its widest item. Set this (e.g. '14rem') to floor the
|
|
42
|
+
* width on larger screens where a thin menu of short labels looks dinky. */
|
|
43
|
+
minWidth?: string
|
|
42
44
|
/** CSS class for styling the dropdown */
|
|
43
45
|
class?: string
|
|
44
46
|
/** Whether the dropdown is disabled */
|
|
@@ -47,8 +49,10 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
47
49
|
initialFocusIndex?: number
|
|
48
50
|
/** Callback when open state changes */
|
|
49
51
|
onOpenChange?: (open: boolean) => void
|
|
50
|
-
/**
|
|
51
|
-
|
|
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 }]>
|
|
52
56
|
/** Dropdown content - should contain MenuItem components */
|
|
53
57
|
children: Snippet<[{ close: () => void; open: boolean }]>
|
|
54
58
|
}
|
|
@@ -57,15 +61,18 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
57
61
|
position = 'bottom-start',
|
|
58
62
|
offset = 4,
|
|
59
63
|
maxWidth = '60ch',
|
|
64
|
+
minWidth,
|
|
60
65
|
class: className = '',
|
|
61
66
|
disabled = false,
|
|
62
67
|
initialFocusIndex = 0,
|
|
63
68
|
onOpenChange,
|
|
69
|
+
ariaLabel = 'Menu',
|
|
64
70
|
trigger: triggerSnippet,
|
|
65
71
|
children,
|
|
66
72
|
}: Props = $props()
|
|
67
73
|
|
|
68
74
|
let open = $state(false)
|
|
75
|
+
let triggerElement: HTMLElement | undefined = undefined
|
|
69
76
|
let menuElement: HTMLElement | undefined = undefined
|
|
70
77
|
let focusedIndex = $state(-1)
|
|
71
78
|
|
|
@@ -87,6 +94,7 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
87
94
|
open = false
|
|
88
95
|
onOpenChange?.(false)
|
|
89
96
|
focusedIndex = -1
|
|
97
|
+
tick().then(() => triggerElement?.focus())
|
|
90
98
|
}
|
|
91
99
|
|
|
92
100
|
const getMenuItems = (): HTMLElement[] => {
|
|
@@ -104,6 +112,8 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
104
112
|
|
|
105
113
|
const handleKeydown = (event: KeyboardEvent) => {
|
|
106
114
|
if (!open) return
|
|
115
|
+
const target = event.target as Node
|
|
116
|
+
if (!menuElement?.contains(target) && !triggerElement?.contains(target)) return
|
|
107
117
|
|
|
108
118
|
const items = getMenuItems()
|
|
109
119
|
const currentIndex = focusedIndex
|
|
@@ -143,6 +153,7 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
143
153
|
|
|
144
154
|
case 'Escape':
|
|
145
155
|
event.preventDefault()
|
|
156
|
+
event.stopImmediatePropagation()
|
|
146
157
|
close()
|
|
147
158
|
break
|
|
148
159
|
}
|
|
@@ -159,14 +170,14 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
159
170
|
}
|
|
160
171
|
</script>
|
|
161
172
|
|
|
162
|
-
<svelte:window
|
|
173
|
+
<svelte:window onkeydowncapture={handleKeydown} />
|
|
163
174
|
|
|
164
175
|
<Popover
|
|
165
176
|
bind:open
|
|
166
177
|
{position}
|
|
167
178
|
{offset}
|
|
168
179
|
closeOnClickOutside={true}
|
|
169
|
-
closeOnEscape={
|
|
180
|
+
closeOnEscape={false}
|
|
170
181
|
onOpenChange={(newOpen) => {
|
|
171
182
|
open = newOpen
|
|
172
183
|
onOpenChange?.(newOpen)
|
|
@@ -174,12 +185,12 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
174
185
|
focusedIndex = -1
|
|
175
186
|
}
|
|
176
187
|
}}
|
|
177
|
-
role="menu"
|
|
178
|
-
ariaLabel="Menu"
|
|
179
188
|
class="dropdown-menu {className}"
|
|
180
189
|
>
|
|
181
190
|
{#snippet trigger()}
|
|
182
191
|
<div
|
|
192
|
+
bind:this={triggerElement}
|
|
193
|
+
class="dropdown-trigger"
|
|
183
194
|
onclick={toggle}
|
|
184
195
|
onkeydown={(e) => {
|
|
185
196
|
if (e.key === 'Enter' || e.key === ' ') {
|
|
@@ -187,12 +198,13 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
187
198
|
toggle()
|
|
188
199
|
}
|
|
189
200
|
}}
|
|
190
|
-
tabindex=
|
|
201
|
+
tabindex={disabled ? undefined : 0}
|
|
191
202
|
role="button"
|
|
192
203
|
aria-haspopup="menu"
|
|
193
204
|
aria-expanded={open}
|
|
205
|
+
aria-disabled={disabled || undefined}
|
|
194
206
|
>
|
|
195
|
-
{@render triggerSnippet({
|
|
207
|
+
{@render triggerSnippet({ open })}
|
|
196
208
|
</div>
|
|
197
209
|
{/snippet}
|
|
198
210
|
|
|
@@ -201,8 +213,10 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
201
213
|
bind:this={menuElement}
|
|
202
214
|
class="dropdown-menu-content"
|
|
203
215
|
style:max-width={maxWidth}
|
|
216
|
+
style:min-width={minWidth}
|
|
204
217
|
role="menu"
|
|
205
|
-
|
|
218
|
+
aria-label={ariaLabel}
|
|
219
|
+
onfocusin={handleItemFocus}
|
|
206
220
|
>
|
|
207
221
|
{@render children({ close, open })}
|
|
208
222
|
</div>
|
|
@@ -210,6 +224,16 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
210
224
|
</Popover>
|
|
211
225
|
|
|
212
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
|
+
|
|
213
237
|
.dropdown-menu-content {
|
|
214
238
|
--menu-item-padding: var(--popover-padding, var(--space-xs, 4px)) var(--popover-padding-lg, var(--space-s, 8px));
|
|
215
239
|
--menu-item-hover-bg: var(--popover-item-hover-bg, var(--color-surface-hover, rgba(0,0,0,0.04)));
|