@wentools/overlay 0.1.6 → 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 -39
- 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 +155 -144
- 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,214 +1,195 @@
|
|
|
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({ toggle, open })}
|
|
14
|
-
<button onclick={toggle}>
|
|
15
|
-
Menu {open ? '▼' : '▶'}
|
|
16
|
-
</button>
|
|
17
|
-
{/snippet}
|
|
18
|
-
|
|
19
|
-
{#snippet children({ close })}
|
|
20
|
-
<MenuItem onclick={() => { action1(); close(); }}>
|
|
21
|
-
Action 1
|
|
22
|
-
</MenuItem>
|
|
23
|
-
<MenuItem onclick={() => { action2(); close(); }}>
|
|
24
|
-
Action 2
|
|
25
|
-
</MenuItem>
|
|
26
|
-
{/snippet}
|
|
27
|
-
</DropdownMenu>
|
|
28
|
-
```
|
|
29
|
-
-->
|
|
30
1
|
<script lang="ts">
|
|
31
|
-
import type
|
|
2
|
+
import { onDestroy, type Snippet } from 'svelte'
|
|
32
3
|
import Popover from './popover.svelte'
|
|
33
|
-
import
|
|
4
|
+
import { claimInitialFocus, createFocusReturn } from './focus'
|
|
5
|
+
import type { CloseOptions, Position, RenderMode } from './types'
|
|
34
6
|
|
|
35
7
|
interface Props {
|
|
36
|
-
/** Position of the dropdown */
|
|
37
8
|
position?: Position
|
|
38
|
-
/** Offset from trigger element */
|
|
39
9
|
offset?: number
|
|
40
|
-
|
|
10
|
+
viewportMargin?: number
|
|
11
|
+
flip?: boolean
|
|
12
|
+
renderMode?: RenderMode
|
|
41
13
|
maxWidth?: string
|
|
42
|
-
/** Minimum width of dropdown content. Unset by default, so the menu
|
|
43
|
-
* shrink-wraps to its widest item. Set this (e.g. '14rem') to floor the
|
|
44
|
-
* width on larger screens where a thin menu of short labels looks dinky. */
|
|
45
14
|
minWidth?: string
|
|
46
|
-
|
|
15
|
+
maxHeight?: string
|
|
47
16
|
class?: string
|
|
48
|
-
/** Whether the dropdown is disabled */
|
|
49
17
|
disabled?: boolean
|
|
50
|
-
/** Index of item to focus when opening. Defaults to 0 (first item). */
|
|
51
18
|
initialFocusIndex?: number
|
|
52
|
-
/** Callback when open state changes */
|
|
53
19
|
onOpenChange?: (open: boolean) => void
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
children: Snippet<[{ close: () => void; open: boolean }]>
|
|
20
|
+
ariaLabel?: string
|
|
21
|
+
/** Non-interactive content; the component supplies a native button. */
|
|
22
|
+
trigger: Snippet<[{ 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,
|
|
68
37
|
onOpenChange,
|
|
38
|
+
ariaLabel = 'Menu',
|
|
69
39
|
trigger: triggerSnippet,
|
|
70
40
|
children,
|
|
71
41
|
}: Props = $props()
|
|
72
|
-
|
|
73
42
|
let open = $state(false)
|
|
74
|
-
let
|
|
75
|
-
let
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
|
88
70
|
}
|
|
71
|
+
if (!item) menuElement?.focus({ preventScroll: true })
|
|
89
72
|
}
|
|
90
|
-
|
|
91
|
-
|
|
73
|
+
const cancelPending = () => {
|
|
74
|
+
pendingFocus = undefined
|
|
75
|
+
clearTimeout(tabTimer)
|
|
76
|
+
focusReturn.cancel()
|
|
77
|
+
}
|
|
78
|
+
const close = (options: CloseOptions = {}) => {
|
|
79
|
+
if (!open) return
|
|
80
|
+
cancelPending()
|
|
92
81
|
open = false
|
|
93
82
|
onOpenChange?.(false)
|
|
94
|
-
|
|
83
|
+
if (options.restoreFocus !== false) focusReturn.schedule(triggerElement, menuElement)
|
|
95
84
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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)
|
|
100
92
|
}
|
|
101
|
-
|
|
102
|
-
const focusItem = (index: number) => {
|
|
103
|
-
const items = getMenuItems()
|
|
104
|
-
if (items[index]) {
|
|
105
|
-
items[index].focus()
|
|
106
|
-
focusedIndex = index
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
93
|
+
const toggle = () => (open ? close({ restoreFocus: false }) : show())
|
|
110
94
|
const handleKeydown = (event: KeyboardEvent) => {
|
|
111
|
-
if (!open) return
|
|
112
|
-
|
|
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
|
|
113
105
|
const items = getMenuItems()
|
|
114
|
-
const
|
|
115
|
-
|
|
106
|
+
const index = items.indexOf(target)
|
|
116
107
|
switch (event.key) {
|
|
117
|
-
case 'ArrowDown':
|
|
108
|
+
case 'ArrowDown':
|
|
118
109
|
event.preventDefault()
|
|
119
|
-
|
|
120
|
-
focusItem(nextIndex)
|
|
110
|
+
focusItem((index + 1) % items.length)
|
|
121
111
|
break
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
case 'ArrowUp': {
|
|
112
|
+
case 'ArrowUp':
|
|
125
113
|
event.preventDefault()
|
|
126
|
-
|
|
127
|
-
focusItem(prevIndex)
|
|
114
|
+
focusItem(index > 0 ? index - 1 : items.length - 1)
|
|
128
115
|
break
|
|
129
|
-
}
|
|
130
|
-
|
|
131
116
|
case 'Home':
|
|
132
117
|
event.preventDefault()
|
|
133
118
|
focusItem(0)
|
|
134
119
|
break
|
|
135
|
-
|
|
136
120
|
case 'End':
|
|
137
121
|
event.preventDefault()
|
|
138
|
-
focusItem(
|
|
122
|
+
focusItem(-1)
|
|
139
123
|
break
|
|
140
|
-
|
|
141
124
|
case 'Enter':
|
|
142
125
|
case ' ':
|
|
143
126
|
event.preventDefault()
|
|
144
|
-
|
|
145
|
-
items[currentIndex].click()
|
|
146
|
-
}
|
|
147
|
-
break
|
|
148
|
-
|
|
149
|
-
case 'Escape':
|
|
150
|
-
event.preventDefault()
|
|
151
|
-
close()
|
|
127
|
+
items[index]?.click()
|
|
152
128
|
break
|
|
153
129
|
}
|
|
154
130
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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()
|
|
163
139
|
}
|
|
164
140
|
}
|
|
141
|
+
onDestroy(cancelPending)
|
|
165
142
|
</script>
|
|
166
143
|
|
|
167
|
-
<svelte:window onkeydown={handleKeydown} />
|
|
168
|
-
|
|
169
144
|
<Popover
|
|
170
145
|
bind:open
|
|
171
146
|
{position}
|
|
172
147
|
{offset}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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)
|
|
181
163
|
}}
|
|
182
|
-
role="menu"
|
|
183
|
-
ariaLabel="Menu"
|
|
184
164
|
class="dropdown-menu {className}"
|
|
185
165
|
>
|
|
186
166
|
{#snippet trigger()}
|
|
187
|
-
<
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
}}
|
|
195
|
-
tabindex="0"
|
|
196
|
-
role="button"
|
|
167
|
+
<button
|
|
168
|
+
bind:this={triggerElement}
|
|
169
|
+
class="dropdown-trigger"
|
|
170
|
+
type="button"
|
|
171
|
+
{disabled}
|
|
172
|
+
tabindex={disabled ? -1 : 0}
|
|
197
173
|
aria-haspopup="menu"
|
|
198
174
|
aria-expanded={open}
|
|
175
|
+
aria-disabled={disabled || undefined}
|
|
176
|
+
onclick={toggle}
|
|
177
|
+
onkeydown={triggerKeydown}
|
|
199
178
|
>
|
|
200
|
-
{@render triggerSnippet({
|
|
201
|
-
</
|
|
179
|
+
{@render triggerSnippet({ open })}
|
|
180
|
+
</button>
|
|
202
181
|
{/snippet}
|
|
203
|
-
|
|
204
182
|
{#snippet content()}
|
|
205
183
|
<div
|
|
206
184
|
bind:this={menuElement}
|
|
207
185
|
class="dropdown-menu-content"
|
|
208
|
-
style:max-width={maxWidth}
|
|
209
|
-
style:min-width={minWidth}
|
|
210
186
|
role="menu"
|
|
211
|
-
|
|
187
|
+
tabindex="-1"
|
|
188
|
+
aria-label={ariaLabel}
|
|
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}
|
|
212
193
|
>
|
|
213
194
|
{@render children({ close, open })}
|
|
214
195
|
</div>
|
|
@@ -216,17 +197,47 @@ Perfect for: context menus, action menus, navigation dropdowns.
|
|
|
216
197
|
</Popover>
|
|
217
198
|
|
|
218
199
|
<style>
|
|
200
|
+
.dropdown-trigger {
|
|
201
|
+
padding: 0;
|
|
202
|
+
border: 0;
|
|
203
|
+
background: transparent;
|
|
204
|
+
color: inherit;
|
|
205
|
+
font: inherit;
|
|
206
|
+
cursor: pointer;
|
|
207
|
+
border-radius: var(--popover-radius-sm, var(--radius-s, 6px));
|
|
208
|
+
}
|
|
209
|
+
.dropdown-trigger:disabled {
|
|
210
|
+
cursor: default;
|
|
211
|
+
}
|
|
212
|
+
.dropdown-trigger:focus-visible {
|
|
213
|
+
outline: var(--popover-focus-width, var(--focus-width, 2px)) solid
|
|
214
|
+
var(--popover-focus-color, var(--focus-color, currentColor));
|
|
215
|
+
outline-offset: var(--popover-focus-offset, var(--focus-offset, 2px));
|
|
216
|
+
}
|
|
219
217
|
.dropdown-menu-content {
|
|
220
|
-
--menu-item-padding: var(--popover-padding, var(--space-xs, 4px))
|
|
221
|
-
|
|
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
|
+
);
|
|
222
224
|
--menu-item-border-radius: var(--popover-radius-sm, var(--radius-s, 6px));
|
|
223
225
|
--menu-separator-color: var(--popover-separator-color, var(--color-border-subtle, #e0e0e0));
|
|
224
|
-
|
|
226
|
+
box-sizing: border-box;
|
|
225
227
|
background: var(--popover-bg, var(--color-bg-elevated, #fff));
|
|
226
|
-
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));
|
|
227
230
|
border-radius: var(--popover-radius, var(--radius-m, 8px));
|
|
228
231
|
padding: var(--popover-padding, var(--space-xs, 4px));
|
|
229
|
-
box-shadow: var(--popover-shadow, var(--shadow-layer-2, 0 4px 12px rgba(0,0,0,0.12)));
|
|
230
|
-
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;
|
|
231
242
|
}
|
|
232
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'
|