@wentools/overlay 0.2.0 → 0.3.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 +177 -43
- package/package.json +4 -1
- package/src/autocomplete_popover.svelte +31 -14
- package/src/click_popover.svelte +124 -71
- package/src/dismissal.ts +99 -0
- package/src/dropdown_menu.svelte +139 -146
- package/src/focus.ts +38 -0
- package/src/hover_popover.svelte +21 -5
- package/src/menu_item.svelte +4 -1
- package/src/mod.ts +7 -1
- package/src/popover.svelte +84 -253
- package/src/positioning.ts +81 -0
- package/src/types.ts +11 -0
package/src/dismissal.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { CloseReason } from './types'
|
|
2
|
+
|
|
3
|
+
type DismissalEntry = {
|
|
4
|
+
trigger: () => HTMLElement | undefined
|
|
5
|
+
content: () => HTMLElement | undefined
|
|
6
|
+
isOpen: () => boolean
|
|
7
|
+
outside: () => boolean
|
|
8
|
+
escape: () => boolean
|
|
9
|
+
close: (reason: CloseReason) => void
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const coordinators = new WeakMap<Document, ReturnType<typeof createCoordinator>>()
|
|
13
|
+
|
|
14
|
+
const createCoordinator = (document: Document) => {
|
|
15
|
+
const entries = new Set<DismissalEntry>()
|
|
16
|
+
let gesture: { eligible: Set<DismissalEntry>; path: EventTarget[] } | undefined
|
|
17
|
+
const inside = (entry: DismissalEntry, path: EventTarget[]) =>
|
|
18
|
+
path.some((node) => node === entry.trigger() || node === entry.content())
|
|
19
|
+
const openEntries = () => [...entries].filter((entry) => entry.isOpen())
|
|
20
|
+
const pointerdown = (event: PointerEvent) => {
|
|
21
|
+
if (event.button !== 0 || event.isPrimary === false) return
|
|
22
|
+
// Capture before a consumer opens a popover from this pointerdown.
|
|
23
|
+
gesture = { eligible: new Set(openEntries()), path: event.composedPath() }
|
|
24
|
+
}
|
|
25
|
+
const pointercancel = () => {
|
|
26
|
+
gesture = undefined
|
|
27
|
+
}
|
|
28
|
+
const click = (event: MouseEvent) => {
|
|
29
|
+
const down = event.detail === 0 ? undefined : gesture
|
|
30
|
+
gesture = undefined
|
|
31
|
+
const path = event.composedPath()
|
|
32
|
+
// Opening another registered overlay does not impose global exclusivity.
|
|
33
|
+
if ([...entries].some((entry) => path.includes(entry.trigger()!))) return
|
|
34
|
+
for (const entry of openEntries().reverse()) {
|
|
35
|
+
if (!entry.outside() || inside(entry, path)) continue
|
|
36
|
+
if (down && (!down.eligible.has(entry) || inside(entry, down.path))) continue
|
|
37
|
+
entry.close('outside')
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const keydown = (event: KeyboardEvent) => {
|
|
41
|
+
if (event.key !== 'Escape' || event.defaultPrevented) return
|
|
42
|
+
const path = event.composedPath()
|
|
43
|
+
const targetDialog = path.find((node) => node instanceof Element && node.tagName === 'DIALOG')
|
|
44
|
+
const eligible = openEntries().filter((entry) => {
|
|
45
|
+
if (!entry.escape()) return false
|
|
46
|
+
return (
|
|
47
|
+
!targetDialog || entry.trigger()?.closest('dialog') === targetDialog || inside(entry, path)
|
|
48
|
+
)
|
|
49
|
+
})
|
|
50
|
+
// DOM containment wins over registration order for nested popovers.
|
|
51
|
+
const containing = eligible.filter((entry) => inside(entry, path))
|
|
52
|
+
const selected =
|
|
53
|
+
containing.find(
|
|
54
|
+
(entry) =>
|
|
55
|
+
!containing.some(
|
|
56
|
+
(other) => other !== entry && entry.content()?.contains(other.trigger() ?? null),
|
|
57
|
+
),
|
|
58
|
+
) ?? eligible.at(-1)
|
|
59
|
+
if (!selected) return
|
|
60
|
+
event.preventDefault()
|
|
61
|
+
event.stopImmediatePropagation()
|
|
62
|
+
selected.close('escape')
|
|
63
|
+
}
|
|
64
|
+
document.addEventListener('pointerdown', pointerdown, true)
|
|
65
|
+
document.addEventListener('pointercancel', pointercancel, true)
|
|
66
|
+
document.addEventListener('click', click, true)
|
|
67
|
+
document.defaultView?.addEventListener('keydown', keydown, true)
|
|
68
|
+
return {
|
|
69
|
+
add: (entry: DismissalEntry) => entries.add(entry),
|
|
70
|
+
activate: (entry: DismissalEntry) => {
|
|
71
|
+
entries.delete(entry)
|
|
72
|
+
entries.add(entry)
|
|
73
|
+
},
|
|
74
|
+
remove: (entry: DismissalEntry) => {
|
|
75
|
+
entries.delete(entry)
|
|
76
|
+
if (entries.size) return
|
|
77
|
+
document.removeEventListener('pointerdown', pointerdown, true)
|
|
78
|
+
document.removeEventListener('pointercancel', pointercancel, true)
|
|
79
|
+
document.removeEventListener('click', click, true)
|
|
80
|
+
document.defaultView?.removeEventListener('keydown', keydown, true)
|
|
81
|
+
coordinators.delete(document)
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const registerDismissal = (document: Document, entry: DismissalEntry) => {
|
|
87
|
+
let coordinator = coordinators.get(document)
|
|
88
|
+
if (!coordinator) {
|
|
89
|
+
coordinator = createCoordinator(document)
|
|
90
|
+
coordinators.set(document, coordinator)
|
|
91
|
+
}
|
|
92
|
+
coordinator.add(entry)
|
|
93
|
+
return {
|
|
94
|
+
activate: () => coordinator.activate(entry),
|
|
95
|
+
destroy: () => coordinator.remove(entry),
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export { registerDismissal }
|
package/src/dropdown_menu.svelte
CHANGED
|
@@ -1,67 +1,36 @@
|
|
|
1
|
-
<!--
|
|
2
|
-
@component
|
|
3
|
-
DropdownMenu - Accessible dropdown menu with keyboard navigation
|
|
4
|
-
|
|
5
|
-
Provides a click-triggered menu with full keyboard navigation (arrow keys, home/end, enter).
|
|
6
|
-
Follows ARIA menu patterns and manages focus automatically.
|
|
7
|
-
|
|
8
|
-
Perfect for: context menus, action menus, navigation dropdowns.
|
|
9
|
-
|
|
10
|
-
@example
|
|
11
|
-
```svelte
|
|
12
|
-
<DropdownMenu>
|
|
13
|
-
{#snippet trigger({ open })}
|
|
14
|
-
<span>Menu {open ? '▼' : '▶'}</span>
|
|
15
|
-
{/snippet}
|
|
16
|
-
|
|
17
|
-
{#snippet children({ close })}
|
|
18
|
-
<MenuItem onclick={() => { action1(); close(); }}>
|
|
19
|
-
Action 1
|
|
20
|
-
</MenuItem>
|
|
21
|
-
<MenuItem onclick={() => { action2(); close(); }}>
|
|
22
|
-
Action 2
|
|
23
|
-
</MenuItem>
|
|
24
|
-
{/snippet}
|
|
25
|
-
</DropdownMenu>
|
|
26
|
-
```
|
|
27
|
-
-->
|
|
28
1
|
<script lang="ts">
|
|
29
|
-
import {
|
|
2
|
+
import { onDestroy, type Snippet } from 'svelte'
|
|
30
3
|
import Popover from './popover.svelte'
|
|
31
|
-
import
|
|
4
|
+
import { claimInitialFocus, createFocusReturn } from './focus'
|
|
5
|
+
import type { CloseOptions, Position, RenderMode } from './types'
|
|
32
6
|
|
|
33
7
|
interface Props {
|
|
34
|
-
/** Position of the dropdown */
|
|
35
8
|
position?: Position
|
|
36
|
-
/** Offset from trigger element */
|
|
37
9
|
offset?: number
|
|
38
|
-
|
|
10
|
+
viewportMargin?: number
|
|
11
|
+
flip?: boolean
|
|
12
|
+
renderMode?: RenderMode
|
|
39
13
|
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
14
|
minWidth?: string
|
|
44
|
-
|
|
15
|
+
maxHeight?: string
|
|
45
16
|
class?: string
|
|
46
|
-
/** Whether the dropdown is disabled */
|
|
47
17
|
disabled?: boolean
|
|
48
|
-
/** Index of item to focus when opening. Defaults to 0 (first item). */
|
|
49
18
|
initialFocusIndex?: number
|
|
50
|
-
/** Callback when open state changes */
|
|
51
19
|
onOpenChange?: (open: boolean) => void
|
|
52
|
-
/** Accessible name for the menu content */
|
|
53
20
|
ariaLabel?: string
|
|
54
|
-
/** Non-interactive
|
|
21
|
+
/** Non-interactive content; the component supplies a native button. */
|
|
55
22
|
trigger: Snippet<[{ open: boolean }]>
|
|
56
|
-
|
|
57
|
-
children: Snippet<[{ close: () => void; open: boolean }]>
|
|
23
|
+
children: Snippet<[{ close: (options?: CloseOptions) => void; open: boolean }]>
|
|
58
24
|
}
|
|
59
|
-
|
|
60
25
|
let {
|
|
61
26
|
position = 'bottom-start',
|
|
62
27
|
offset = 4,
|
|
28
|
+
viewportMargin = 0,
|
|
29
|
+
flip = true,
|
|
30
|
+
renderMode = 'inline',
|
|
63
31
|
maxWidth = '60ch',
|
|
64
32
|
minWidth,
|
|
33
|
+
maxHeight = 'none',
|
|
65
34
|
class: className = '',
|
|
66
35
|
disabled = false,
|
|
67
36
|
initialFocusIndex = 0,
|
|
@@ -70,153 +39,157 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
70
39
|
trigger: triggerSnippet,
|
|
71
40
|
children,
|
|
72
41
|
}: Props = $props()
|
|
73
|
-
|
|
74
42
|
let open = $state(false)
|
|
75
|
-
let triggerElement:
|
|
76
|
-
let menuElement:
|
|
77
|
-
let
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
43
|
+
let triggerElement: HTMLButtonElement | undefined
|
|
44
|
+
let menuElement: HTMLDivElement | undefined
|
|
45
|
+
let pendingFocus: number | undefined
|
|
46
|
+
let mayFocus = () => false
|
|
47
|
+
let tabTimer: ReturnType<typeof setTimeout> | undefined
|
|
48
|
+
const focusReturn = createFocusReturn()
|
|
49
|
+
const getMenuItems = () =>
|
|
50
|
+
menuElement
|
|
51
|
+
? Array.from(
|
|
52
|
+
menuElement.querySelectorAll<HTMLElement>(
|
|
53
|
+
'[role="menuitem"]:not([disabled]):not([aria-disabled="true"])',
|
|
54
|
+
),
|
|
55
|
+
).filter((item) => item.closest('[role="menu"]') === menuElement)
|
|
56
|
+
: []
|
|
57
|
+
const focusItem = (index: number) => {
|
|
58
|
+
const items = getMenuItems()
|
|
59
|
+
const item = items[index < 0 ? items.length - 1 : Math.min(index, items.length - 1)]
|
|
60
|
+
item?.focus({ preventScroll: true })
|
|
61
|
+
if (item && menuElement) {
|
|
62
|
+
// Reveal the command within the panel without scrolling its owning page.
|
|
63
|
+
const bounds = menuElement.getBoundingClientRect()
|
|
64
|
+
const command = item.getBoundingClientRect()
|
|
65
|
+
const top = bounds.top + menuElement.clientTop
|
|
66
|
+
const bottom = top + menuElement.clientHeight
|
|
67
|
+
if (command.top < top || command.height > menuElement.clientHeight)
|
|
68
|
+
menuElement.scrollTop += command.top - top
|
|
69
|
+
else if (command.bottom > bottom) menuElement.scrollTop += command.bottom - bottom
|
|
90
70
|
}
|
|
71
|
+
if (!item) menuElement?.focus({ preventScroll: true })
|
|
91
72
|
}
|
|
92
|
-
|
|
93
|
-
|
|
73
|
+
const cancelPending = () => {
|
|
74
|
+
pendingFocus = undefined
|
|
75
|
+
clearTimeout(tabTimer)
|
|
76
|
+
focusReturn.cancel()
|
|
77
|
+
}
|
|
78
|
+
const close = (options: CloseOptions = {}) => {
|
|
79
|
+
if (!open) return
|
|
80
|
+
cancelPending()
|
|
94
81
|
open = false
|
|
95
82
|
onOpenChange?.(false)
|
|
96
|
-
|
|
97
|
-
tick().then(() => triggerElement?.focus())
|
|
83
|
+
if (options.restoreFocus !== false) focusReturn.schedule(triggerElement, menuElement)
|
|
98
84
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
85
|
+
const show = (index = initialFocusIndex) => {
|
|
86
|
+
if (disabled || open) return
|
|
87
|
+
cancelPending()
|
|
88
|
+
pendingFocus = index
|
|
89
|
+
mayFocus = claimInitialFocus(triggerElement)
|
|
90
|
+
open = true
|
|
91
|
+
onOpenChange?.(true)
|
|
103
92
|
}
|
|
104
|
-
|
|
105
|
-
const focusItem = (index: number) => {
|
|
106
|
-
const items = getMenuItems()
|
|
107
|
-
if (items[index]) {
|
|
108
|
-
items[index].focus()
|
|
109
|
-
focusedIndex = index
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
93
|
+
const toggle = () => (open ? close({ restoreFocus: false }) : show())
|
|
113
94
|
const handleKeydown = (event: KeyboardEvent) => {
|
|
114
|
-
if (!open) return
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
95
|
+
if (!open || event.defaultPrevented) return
|
|
96
|
+
if (event.key === 'Tab') {
|
|
97
|
+
// Let native traversal finish before removing the focused menu item.
|
|
98
|
+
clearTimeout(tabTimer)
|
|
99
|
+
tabTimer = setTimeout(() => close({ restoreFocus: false }), 0)
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
const target = event.target
|
|
103
|
+
if (!(target instanceof HTMLElement) || target.closest('[role="menu"]') !== menuElement) return
|
|
104
|
+
if (target !== menuElement && target.getAttribute('role') !== 'menuitem') return
|
|
118
105
|
const items = getMenuItems()
|
|
119
|
-
const
|
|
120
|
-
|
|
106
|
+
const index = items.indexOf(target)
|
|
121
107
|
switch (event.key) {
|
|
122
|
-
case 'ArrowDown':
|
|
108
|
+
case 'ArrowDown':
|
|
123
109
|
event.preventDefault()
|
|
124
|
-
|
|
125
|
-
focusItem(nextIndex)
|
|
110
|
+
focusItem((index + 1) % items.length)
|
|
126
111
|
break
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
case 'ArrowUp': {
|
|
112
|
+
case 'ArrowUp':
|
|
130
113
|
event.preventDefault()
|
|
131
|
-
|
|
132
|
-
focusItem(prevIndex)
|
|
114
|
+
focusItem(index > 0 ? index - 1 : items.length - 1)
|
|
133
115
|
break
|
|
134
|
-
}
|
|
135
|
-
|
|
136
116
|
case 'Home':
|
|
137
117
|
event.preventDefault()
|
|
138
118
|
focusItem(0)
|
|
139
119
|
break
|
|
140
|
-
|
|
141
120
|
case 'End':
|
|
142
121
|
event.preventDefault()
|
|
143
|
-
focusItem(
|
|
122
|
+
focusItem(-1)
|
|
144
123
|
break
|
|
145
|
-
|
|
146
124
|
case 'Enter':
|
|
147
125
|
case ' ':
|
|
148
126
|
event.preventDefault()
|
|
149
|
-
|
|
150
|
-
items[currentIndex].click()
|
|
151
|
-
}
|
|
152
|
-
break
|
|
153
|
-
|
|
154
|
-
case 'Escape':
|
|
155
|
-
event.preventDefault()
|
|
156
|
-
event.stopImmediatePropagation()
|
|
157
|
-
close()
|
|
127
|
+
items[index]?.click()
|
|
158
128
|
break
|
|
159
129
|
}
|
|
160
130
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
131
|
+
const triggerKeydown = (event: KeyboardEvent) => {
|
|
132
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
133
|
+
event.preventDefault()
|
|
134
|
+
if (open) focusItem(event.key === 'ArrowUp' ? -1 : 0)
|
|
135
|
+
else show(event.key === 'ArrowUp' ? -1 : 0)
|
|
136
|
+
} else if (event.key === 'Enter' || event.key === ' ') {
|
|
137
|
+
event.preventDefault()
|
|
138
|
+
toggle()
|
|
169
139
|
}
|
|
170
140
|
}
|
|
141
|
+
onDestroy(cancelPending)
|
|
171
142
|
</script>
|
|
172
143
|
|
|
173
|
-
<svelte:window onkeydowncapture={handleKeydown} />
|
|
174
|
-
|
|
175
144
|
<Popover
|
|
176
145
|
bind:open
|
|
177
146
|
{position}
|
|
178
147
|
{offset}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
148
|
+
{viewportMargin}
|
|
149
|
+
{flip}
|
|
150
|
+
{renderMode}
|
|
151
|
+
onOpenChange={(value) => {
|
|
152
|
+
cancelPending()
|
|
153
|
+
onOpenChange?.(value)
|
|
154
|
+
}}
|
|
155
|
+
onClose={(reason) => {
|
|
156
|
+
if (reason === 'escape') focusReturn.schedule(triggerElement, menuElement)
|
|
157
|
+
}}
|
|
158
|
+
onPositioned={() => {
|
|
159
|
+
if (pendingFocus === undefined) return
|
|
160
|
+
const index = pendingFocus
|
|
161
|
+
pendingFocus = undefined
|
|
162
|
+
if (mayFocus()) focusItem(index)
|
|
187
163
|
}}
|
|
188
164
|
class="dropdown-menu {className}"
|
|
189
165
|
>
|
|
190
166
|
{#snippet trigger()}
|
|
191
|
-
<
|
|
167
|
+
<button
|
|
192
168
|
bind:this={triggerElement}
|
|
193
169
|
class="dropdown-trigger"
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
e.preventDefault()
|
|
198
|
-
toggle()
|
|
199
|
-
}
|
|
200
|
-
}}
|
|
201
|
-
tabindex={disabled ? undefined : 0}
|
|
202
|
-
role="button"
|
|
170
|
+
type="button"
|
|
171
|
+
{disabled}
|
|
172
|
+
tabindex={disabled ? -1 : 0}
|
|
203
173
|
aria-haspopup="menu"
|
|
204
174
|
aria-expanded={open}
|
|
205
175
|
aria-disabled={disabled || undefined}
|
|
176
|
+
onclick={toggle}
|
|
177
|
+
onkeydown={triggerKeydown}
|
|
206
178
|
>
|
|
207
179
|
{@render triggerSnippet({ open })}
|
|
208
|
-
</
|
|
180
|
+
</button>
|
|
209
181
|
{/snippet}
|
|
210
|
-
|
|
211
182
|
{#snippet content()}
|
|
212
183
|
<div
|
|
213
184
|
bind:this={menuElement}
|
|
214
185
|
class="dropdown-menu-content"
|
|
215
|
-
style:max-width={maxWidth}
|
|
216
|
-
style:min-width={minWidth}
|
|
217
186
|
role="menu"
|
|
187
|
+
tabindex="-1"
|
|
218
188
|
aria-label={ariaLabel}
|
|
219
|
-
|
|
189
|
+
onkeydown={handleKeydown}
|
|
190
|
+
style:--menu-max-width={maxWidth === 'none' ? '100vw' : maxWidth}
|
|
191
|
+
style:--menu-min-width={minWidth ?? '0px'}
|
|
192
|
+
style:--menu-max-height={maxHeight === 'none' ? '100dvh' : maxHeight}
|
|
220
193
|
>
|
|
221
194
|
{@render children({ close, open })}
|
|
222
195
|
</div>
|
|
@@ -225,26 +198,46 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
225
198
|
|
|
226
199
|
<style>
|
|
227
200
|
.dropdown-trigger {
|
|
201
|
+
padding: 0;
|
|
202
|
+
border: 0;
|
|
203
|
+
background: transparent;
|
|
204
|
+
color: inherit;
|
|
205
|
+
font: inherit;
|
|
206
|
+
cursor: pointer;
|
|
228
207
|
border-radius: var(--popover-radius-sm, var(--radius-s, 6px));
|
|
229
208
|
}
|
|
230
|
-
|
|
209
|
+
.dropdown-trigger:disabled {
|
|
210
|
+
cursor: default;
|
|
211
|
+
}
|
|
231
212
|
.dropdown-trigger:focus-visible {
|
|
232
213
|
outline: var(--popover-focus-width, var(--focus-width, 2px)) solid
|
|
233
214
|
var(--popover-focus-color, var(--focus-color, currentColor));
|
|
234
215
|
outline-offset: var(--popover-focus-offset, var(--focus-offset, 2px));
|
|
235
216
|
}
|
|
236
|
-
|
|
237
217
|
.dropdown-menu-content {
|
|
238
|
-
--menu-item-padding: var(--popover-padding, var(--space-xs, 4px))
|
|
239
|
-
|
|
218
|
+
--menu-item-padding: var(--popover-padding, var(--space-xs, 4px))
|
|
219
|
+
var(--popover-padding-lg, var(--space-s, 8px));
|
|
220
|
+
--menu-item-hover-bg: var(
|
|
221
|
+
--popover-item-hover-bg,
|
|
222
|
+
var(--color-surface-hover, rgba(0, 0, 0, 0.04))
|
|
223
|
+
);
|
|
240
224
|
--menu-item-border-radius: var(--popover-radius-sm, var(--radius-s, 6px));
|
|
241
225
|
--menu-separator-color: var(--popover-separator-color, var(--color-border-subtle, #e0e0e0));
|
|
242
|
-
|
|
226
|
+
box-sizing: border-box;
|
|
243
227
|
background: var(--popover-bg, var(--color-bg-elevated, #fff));
|
|
244
|
-
border: var(--popover-border-width, var(--border-thickness-m, 1px)) solid
|
|
228
|
+
border: var(--popover-border-width, var(--border-thickness-m, 1px)) solid
|
|
229
|
+
var(--popover-border-color, var(--color-border-default, #e0e0e0));
|
|
245
230
|
border-radius: var(--popover-radius, var(--radius-m, 8px));
|
|
246
231
|
padding: var(--popover-padding, var(--space-xs, 4px));
|
|
247
|
-
box-shadow: var(--popover-shadow, var(--shadow-layer-2, 0 4px 12px rgba(0,0,0,0.12)));
|
|
248
|
-
min-width:
|
|
232
|
+
box-shadow: var(--popover-shadow, var(--shadow-layer-2, 0 4px 12px rgba(0, 0, 0, 0.12)));
|
|
233
|
+
min-width: min(
|
|
234
|
+
var(--menu-min-width),
|
|
235
|
+
var(--menu-max-width),
|
|
236
|
+
var(--popover-available-width, 100vw)
|
|
237
|
+
);
|
|
238
|
+
max-width: min(var(--menu-max-width), var(--popover-available-width, 100vw));
|
|
239
|
+
max-height: min(var(--menu-max-height), var(--popover-available-height, 100dvh));
|
|
240
|
+
overflow: auto;
|
|
241
|
+
overflow-wrap: anywhere;
|
|
249
242
|
}
|
|
250
243
|
</style>
|
package/src/focus.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { tick } from 'svelte'
|
|
2
|
+
|
|
3
|
+
const initialFocusRequests = new WeakMap<Document, object>()
|
|
4
|
+
|
|
5
|
+
const claimInitialFocus = (trigger: HTMLElement | undefined) => {
|
|
6
|
+
const document = trigger?.ownerDocument
|
|
7
|
+
const request = {}
|
|
8
|
+
const origin = document?.activeElement
|
|
9
|
+
if (document) initialFocusRequests.set(document, request)
|
|
10
|
+
return () =>
|
|
11
|
+
!!document &&
|
|
12
|
+
initialFocusRequests.get(document) === request &&
|
|
13
|
+
(document.activeElement === origin ||
|
|
14
|
+
document.activeElement === trigger ||
|
|
15
|
+
document.activeElement === document.body)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const createFocusReturn = () => {
|
|
19
|
+
let version = 0
|
|
20
|
+
return {
|
|
21
|
+
cancel: () => {
|
|
22
|
+
version++
|
|
23
|
+
},
|
|
24
|
+
schedule: (trigger: HTMLElement | undefined, content: HTMLElement | undefined) => {
|
|
25
|
+
const current = ++version
|
|
26
|
+
void tick().then(() => {
|
|
27
|
+
if (current !== version || !trigger?.isConnected) return
|
|
28
|
+
const document = trigger.ownerDocument
|
|
29
|
+
const focused = document.activeElement
|
|
30
|
+
// Do not undo an action's deliberate transfer into a dialog or editor.
|
|
31
|
+
if (focused !== document.body && focused !== trigger && !content?.contains(focused)) return
|
|
32
|
+
trigger.focus({ preventScroll: true })
|
|
33
|
+
})
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { claimInitialFocus, createFocusReturn }
|
package/src/hover_popover.svelte
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import type { Snippet } from 'svelte'
|
|
3
3
|
import Popover from './popover.svelte'
|
|
4
4
|
import { debounce } from '@wentools/simmer'
|
|
5
|
-
import type { Position } from './types'
|
|
5
|
+
import type { Position, RenderMode } from './types'
|
|
6
6
|
|
|
7
7
|
interface Props {
|
|
8
8
|
/** Delay before showing in milliseconds */
|
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
hideDelay?: number
|
|
12
12
|
/** Position of the popover */
|
|
13
13
|
position?: Position
|
|
14
|
+
viewportMargin?: number
|
|
15
|
+
flip?: boolean
|
|
16
|
+
renderMode?: RenderMode
|
|
14
17
|
/** Offset from trigger element */
|
|
15
18
|
offset?: number
|
|
16
19
|
/** Maximum width of tooltip content */
|
|
@@ -29,6 +32,9 @@
|
|
|
29
32
|
showDelay = 400,
|
|
30
33
|
hideDelay = 200,
|
|
31
34
|
position = 'top',
|
|
35
|
+
viewportMargin = 0,
|
|
36
|
+
flip = true,
|
|
37
|
+
renderMode = 'inline',
|
|
32
38
|
offset = 8,
|
|
33
39
|
maxWidth = '35ch',
|
|
34
40
|
class: className = '',
|
|
@@ -70,6 +76,9 @@
|
|
|
70
76
|
<Popover
|
|
71
77
|
bind:open
|
|
72
78
|
{position}
|
|
79
|
+
{viewportMargin}
|
|
80
|
+
{flip}
|
|
81
|
+
{renderMode}
|
|
73
82
|
{offset}
|
|
74
83
|
closeOnClickOutside={false}
|
|
75
84
|
closeOnEscape={false}
|
|
@@ -93,7 +102,7 @@
|
|
|
93
102
|
class="hover-popover-content"
|
|
94
103
|
onmouseenter={handleMouseEnter}
|
|
95
104
|
onmouseleave={handleMouseLeave}
|
|
96
|
-
style
|
|
105
|
+
style:--tooltip-max-width={maxWidth === 'none' ? '100vw' : maxWidth}
|
|
97
106
|
role="presentation"
|
|
98
107
|
>
|
|
99
108
|
{@render _content({ close, open })}
|
|
@@ -103,11 +112,18 @@
|
|
|
103
112
|
|
|
104
113
|
<style>
|
|
105
114
|
.hover-popover-content {
|
|
115
|
+
box-sizing: border-box;
|
|
116
|
+
max-width: min(var(--tooltip-max-width), var(--popover-available-width, 100vw));
|
|
117
|
+
max-height: var(--popover-available-height, 100dvh);
|
|
118
|
+
overflow: auto;
|
|
119
|
+
overflow-wrap: anywhere;
|
|
106
120
|
background: var(--popover-bg, var(--color-bg-elevated, #fff));
|
|
107
121
|
color: var(--popover-color, var(--color-text-primary, inherit));
|
|
108
|
-
border: var(--popover-border-width, var(--border-thickness-m, 1px)) solid
|
|
109
|
-
|
|
122
|
+
border: var(--popover-border-width, var(--border-thickness-m, 1px)) solid
|
|
123
|
+
var(--popover-border-color, var(--color-border-default, #e0e0e0));
|
|
124
|
+
padding: var(--popover-padding, var(--space-xs, 4px))
|
|
125
|
+
var(--popover-padding-lg, var(--space-s, 8px));
|
|
110
126
|
border-radius: var(--popover-radius-sm, var(--radius-s, 6px));
|
|
111
|
-
box-shadow: var(--popover-shadow-sm, var(--shadow-layer-1, 0 2px 8px rgba(0,0,0,0.08)));
|
|
127
|
+
box-shadow: var(--popover-shadow-sm, var(--shadow-layer-1, 0 2px 8px rgba(0, 0, 0, 0.08)));
|
|
112
128
|
}
|
|
113
129
|
</style>
|
package/src/menu_item.svelte
CHANGED
|
@@ -68,6 +68,7 @@ Perfect for: actions in dropdown menus, selectable options, navigation links.
|
|
|
68
68
|
onclick={(e) => {
|
|
69
69
|
if (disabled) {
|
|
70
70
|
e.preventDefault()
|
|
71
|
+
return
|
|
71
72
|
}
|
|
72
73
|
onclick?.()
|
|
73
74
|
}}
|
|
@@ -76,6 +77,7 @@ Perfect for: actions in dropdown menus, selectable options, navigation links.
|
|
|
76
77
|
</a>
|
|
77
78
|
{:else}
|
|
78
79
|
<button
|
|
80
|
+
type="button"
|
|
79
81
|
class="menu-item {className}"
|
|
80
82
|
class:highlighted
|
|
81
83
|
class:disabled
|
|
@@ -95,6 +97,7 @@ Perfect for: actions in dropdown menus, selectable options, navigation links.
|
|
|
95
97
|
|
|
96
98
|
<style>
|
|
97
99
|
.menu-item {
|
|
100
|
+
box-sizing: border-box;
|
|
98
101
|
/* Use CSS custom properties set by parent DropdownMenu */
|
|
99
102
|
padding: var(--menu-item-padding, var(--space-xs, 4px) var(--space-s, 8px));
|
|
100
103
|
border-radius: var(--menu-item-border-radius, var(--radius-s, 6px));
|
|
@@ -116,7 +119,7 @@ Perfect for: actions in dropdown menus, selectable options, navigation links.
|
|
|
116
119
|
.menu-item:hover,
|
|
117
120
|
.menu-item:focus,
|
|
118
121
|
.menu-item.highlighted {
|
|
119
|
-
background: var(--menu-item-hover-bg, var(--color-surface-hover, rgba(0,0,0,0.04)));
|
|
122
|
+
background: var(--menu-item-hover-bg, var(--color-surface-hover, rgba(0, 0, 0, 0.04)));
|
|
120
123
|
outline: none;
|
|
121
124
|
}
|
|
122
125
|
|
package/src/mod.ts
CHANGED
|
@@ -5,4 +5,10 @@ export { default as AutocompletePopover } from './autocomplete_popover.svelte'
|
|
|
5
5
|
export { default as DropdownMenu } from './dropdown_menu.svelte'
|
|
6
6
|
export { default as MenuItem } from './menu_item.svelte'
|
|
7
7
|
export { default as MenuSeparator } from './menu_separator.svelte'
|
|
8
|
-
export type {
|
|
8
|
+
export type {
|
|
9
|
+
ClickPopoverTrigger,
|
|
10
|
+
CloseOptions,
|
|
11
|
+
CloseReason,
|
|
12
|
+
Position,
|
|
13
|
+
RenderMode,
|
|
14
|
+
} from './types'
|