@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/README.md
CHANGED
|
@@ -8,38 +8,171 @@ Svelte 5 popover, dropdown menu, and tooltip components with CSS custom property
|
|
|
8
8
|
npm install @wentools/overlay
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
Peer dependencies: `svelte >=5.0.0`, `@wentools/spatial >=0.
|
|
11
|
+
Peer dependencies: `svelte >=5.0.0`, `@wentools/spatial >=0.1.0`, `@wentools/simmer >=0.1.0`.
|
|
12
|
+
The spatial peer is retained for compatibility with the existing package metadata;
|
|
13
|
+
positioning no longer imports it.
|
|
14
|
+
|
|
15
|
+
## Version 0.3 behavior
|
|
16
|
+
|
|
17
|
+
Positioning uses the DOM-only `@floating-ui/dom` runtime dependency. Existing
|
|
18
|
+
component exports and snippets remain available. Placements are now preferences:
|
|
19
|
+
an overlay flips when its preferred side does not fit, then shifts within its
|
|
20
|
+
bounds. Set `flip={false}` for an explicit fixed-side placement. `position="auto"`
|
|
21
|
+
still tries bottom first, then top, right and left.
|
|
22
|
+
|
|
23
|
+
Position updates track ancestor scrolling, element resizing and layout shifts
|
|
24
|
+
while open. Visual viewport changes also update the position. Observers are
|
|
25
|
+
removed on close or destruction; there is no permanent animation-frame loop.
|
|
26
|
+
|
|
27
|
+
### Shared positioning options
|
|
28
|
+
|
|
29
|
+
All wrappers forward `position`, `offset`, `viewportMargin`, `flip` and `renderMode`
|
|
30
|
+
to `Popover`. Existing offset and viewport margin values are pixels. Use the
|
|
31
|
+
application's spacing tokens when choosing those values.
|
|
32
|
+
|
|
33
|
+
| Option | Default | Meaning |
|
|
34
|
+
| ---------------- | ---------- | ---------------------------------------------------------- |
|
|
35
|
+
| `flip` | `true` | Try another side when the preferred placement does not fit |
|
|
36
|
+
| `renderMode` | `'inline'` | Use existing inline DOM rendering or opt-in `'top-layer'` |
|
|
37
|
+
| `viewportMargin` | `0` | Minimum space between content and its clipping bounds |
|
|
38
|
+
|
|
39
|
+
`renderMode="top-layer"` uses native `popover="manual"`, not a body portal. Content
|
|
40
|
+
keeps its DOM ancestry, theme inheritance and relationship to an owning modal
|
|
41
|
+
dialog, but escapes clipping and transformed containing blocks. Dismissal is
|
|
42
|
+
still owned by this package. It does not make a panel modal or impose menu roles.
|
|
43
|
+
|
|
44
|
+
Browsers without the native Popover API fall back to inline rendering. That
|
|
45
|
+
fallback does **not** guarantee escape from clipping. No new minimum browser
|
|
46
|
+
version is imposed by default; consumers requiring top-layer behavior must check
|
|
47
|
+
their target devices. Closing an owning dialog also closes its overlay.
|
|
48
|
+
|
|
49
|
+
`Popover` itself does not move focus. It exposes `onClose(reason)` with
|
|
50
|
+
`'escape'`, `'outside'`, `'programmatic'` or `'owner-closed'`, and an optional
|
|
51
|
+
`onPositioned()` callback after a valid position is applied. The existing
|
|
52
|
+
`onOpenChange(boolean)` callback remains unchanged. Parent-driven changes to
|
|
53
|
+
bindable `open` do not echo that callback.
|
|
54
|
+
|
|
55
|
+
### Sizing
|
|
56
|
+
|
|
57
|
+
`DropdownMenu` and `ClickPopover` accept `minWidth`, `maxWidth` and `maxHeight`.
|
|
58
|
+
Minimum sizes yield to available space; large panels scroll and long labels
|
|
59
|
+
wrap. `AutocompletePopover` retains its existing sizing props. Hover content is
|
|
60
|
+
also viewport bounded. App-specific width and touch-target choices remain with
|
|
61
|
+
the consumer rather than becoming a mandatory package-wide style.
|
|
62
|
+
|
|
63
|
+
For custom content inside the low-level `Popover`, consume the inherited
|
|
64
|
+
`--popover-available-width` and `--popover-available-height` properties yourself:
|
|
65
|
+
|
|
66
|
+
```css
|
|
67
|
+
.my-panel {
|
|
68
|
+
box-sizing: border-box;
|
|
69
|
+
max-width: var(--popover-available-width, 100vw);
|
|
70
|
+
max-height: var(--popover-available-height, 100dvh);
|
|
71
|
+
overflow: auto;
|
|
72
|
+
overflow-wrap: anywhere;
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Dismissal and focus
|
|
77
|
+
|
|
78
|
+
- Outside clicks dismiss without cancelling the clicked control's action or
|
|
79
|
+
returning focus to the old trigger. The gesture that opened an overlay is
|
|
80
|
+
ignored, not a blanket time window: a distinct outside tap can dismiss it
|
|
81
|
+
immediately, including on touch devices.
|
|
82
|
+
- Interacting with another registered overlay trigger does not impose global
|
|
83
|
+
exclusivity. Independent overlays can remain open.
|
|
84
|
+
- Escape closes the relevant innermost overlay without also cancelling its
|
|
85
|
+
owning dialog. If focus is outside all overlays, the most recently opened
|
|
86
|
+
eligible overlay handles Escape. A different dialog does not dismiss a
|
|
87
|
+
background overlay.
|
|
88
|
+
- `DropdownMenu` owns a native button. Keep its `trigger` snippet non-interactive.
|
|
89
|
+
Opening focuses the requested available item; arrows, Home/End and Enter/Space
|
|
90
|
+
navigate/activate commands. Tab/Shift+Tab closes without trapping traversal.
|
|
91
|
+
- Escape returns menu focus to the trigger. Selection retains that default;
|
|
92
|
+
call `close({ restoreFocus: false })` when transferring focus to an editor or
|
|
93
|
+
another dialog. Deliberate focus transfers are not overwritten by delayed work.
|
|
94
|
+
- Generic `ClickPopover` panels use normal input, selector and Tab behavior.
|
|
95
|
+
They do not use ARIA menu navigation. `initialFocus={() => element}` optionally
|
|
96
|
+
chooses an initial control. Escape returns focus only if it had entered the panel.
|
|
97
|
+
- Autocomplete retains input focus; Hover retains its show/hide delays and does
|
|
98
|
+
not participate in outside-click or Escape dismissal.
|
|
99
|
+
|
|
100
|
+
### One caller-owned ClickPopover trigger
|
|
101
|
+
|
|
102
|
+
Prefer the explicit trigger API for panels containing selectors, inputs or other
|
|
103
|
+
non-menu controls:
|
|
104
|
+
|
|
105
|
+
```svelte
|
|
106
|
+
<ClickPopover renderMode="top-layer" minWidth="14rem">
|
|
107
|
+
{#snippet trigger({ triggerProps, registerTrigger })}
|
|
108
|
+
<button {...triggerProps} use:registerTrigger>Options</button>
|
|
109
|
+
{/snippet}
|
|
110
|
+
{#snippet content({ close })}
|
|
111
|
+
<label>Terminal <select><option>Desk</option><option>Laptop</option></select></label>
|
|
112
|
+
<button type="button" onclick={() => close()}>Done</button>
|
|
113
|
+
{/snippet}
|
|
114
|
+
</ClickPopover>
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Spread `triggerProps` and apply `registerTrigger` to the same native button.
|
|
118
|
+
The legacy `children({ toggle, open })` trigger API remains available, but supplies
|
|
119
|
+
its own focusable wrapper. Use non-interactive visual content there, not another
|
|
120
|
+
button. The `content` snippet remains the popup body in both APIs.
|
|
121
|
+
|
|
122
|
+
## Development and verification
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
pnpm install
|
|
126
|
+
deno task check
|
|
127
|
+
deno task test
|
|
128
|
+
pnpm test:browser
|
|
129
|
+
pnpm test:package
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Browser tests use synthetic content on PC loopback only, including real native
|
|
133
|
+
modal dialogs, top-layer rendering and touch sequences. Set Playwright's browser
|
|
134
|
+
installation up before running them. Device keyboard, touch and readability
|
|
135
|
+
acceptance still need testing on the actual target device.
|
|
136
|
+
|
|
137
|
+
The npm build is generated by `build_npm.ts` into ignored `npm/`. Runtime
|
|
138
|
+
dependencies come from the root manifest. Package verification inspects that
|
|
139
|
+
artifact and builds an isolated Svelte consumer; it does not publish anything.
|
|
12
140
|
|
|
13
141
|
## Components
|
|
14
142
|
|
|
15
|
-
| Component
|
|
16
|
-
|
|
|
17
|
-
| `Popover`
|
|
18
|
-
| `ClickPopover`
|
|
19
|
-
| `HoverPopover`
|
|
20
|
-
| `DropdownMenu`
|
|
21
|
-
| `AutocompletePopover`
|
|
22
|
-
| `MenuItem`
|
|
23
|
-
| `MenuSeparator`
|
|
143
|
+
| Component | Description |
|
|
144
|
+
| --------------------- | ---------------------------------------------- |
|
|
145
|
+
| `Popover` | Base positioned overlay (positioning only) |
|
|
146
|
+
| `ClickPopover` | Click-triggered popover with toggle |
|
|
147
|
+
| `HoverPopover` | Hover/focus-triggered tooltip with debounce |
|
|
148
|
+
| `DropdownMenu` | Accessible menu with keyboard navigation |
|
|
149
|
+
| `AutocompletePopover` | Input-based suggestions popover |
|
|
150
|
+
| `MenuItem` | Menu item for DropdownMenu/AutocompletePopover |
|
|
151
|
+
| `MenuSeparator` | Divider line between menu items |
|
|
24
152
|
|
|
25
153
|
## Usage
|
|
26
154
|
|
|
27
155
|
```svelte
|
|
28
156
|
<script>
|
|
29
|
-
|
|
157
|
+
import { DropdownMenu, MenuItem, MenuSeparator } from '@wentools/overlay'
|
|
30
158
|
</script>
|
|
31
159
|
|
|
32
160
|
<DropdownMenu>
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
161
|
+
{#snippet trigger({ open })}
|
|
162
|
+
<!-- DropdownMenu supplies the focusable menu-button wrapper. -->
|
|
163
|
+
<span>Menu {open ? 'open' : ''}</span>
|
|
164
|
+
{/snippet}
|
|
165
|
+
|
|
166
|
+
{#snippet children({ close })}
|
|
167
|
+
<MenuItem
|
|
168
|
+
onclick={() => {
|
|
169
|
+
doAction()
|
|
170
|
+
close()
|
|
171
|
+
}}>Action</MenuItem
|
|
172
|
+
>
|
|
173
|
+
<MenuSeparator />
|
|
174
|
+
<MenuItem onclick={close}>Cancel</MenuItem>
|
|
175
|
+
{/snippet}
|
|
43
176
|
</DropdownMenu>
|
|
44
177
|
```
|
|
45
178
|
|
|
@@ -52,30 +185,31 @@ var(--popover-*, var(--design-system-*, hardcoded-fallback))
|
|
|
52
185
|
```
|
|
53
186
|
|
|
54
187
|
This means:
|
|
188
|
+
|
|
55
189
|
1. **Library tokens** (`--popover-*`) — set these to theme the library
|
|
56
190
|
2. **Design system tokens** (`--color-bg-elevated`, etc.) — automatically picked up if your app defines them
|
|
57
191
|
3. **Hardcoded fallbacks** — sensible defaults when nothing else is set
|
|
58
192
|
|
|
59
193
|
### Token Reference
|
|
60
194
|
|
|
61
|
-
| Token
|
|
62
|
-
|
|
|
63
|
-
| `--popover-z-index`
|
|
64
|
-
| `--popover-bg`
|
|
65
|
-
| `--popover-color`
|
|
66
|
-
| `--popover-border-width`
|
|
67
|
-
| `--popover-border-color`
|
|
68
|
-
| `--popover-radius`
|
|
69
|
-
| `--popover-radius-sm`
|
|
70
|
-
| `--popover-padding`
|
|
71
|
-
| `--popover-padding-lg`
|
|
72
|
-
| `--popover-shadow`
|
|
73
|
-
| `--popover-shadow-sm`
|
|
74
|
-
| `--popover-item-hover-bg`
|
|
75
|
-
| `--popover-separator-color`
|
|
76
|
-
| `--popover-focus-width`
|
|
77
|
-
| `--popover-focus-color`
|
|
78
|
-
| `--popover-focus-offset`
|
|
195
|
+
| Token | Fallback chain | Used by |
|
|
196
|
+
| --------------------------- | -------------------------------------------------- | -------------------------- |
|
|
197
|
+
| `--popover-z-index` | `--layer-tooltip` → `1000` | Popover |
|
|
198
|
+
| `--popover-bg` | `--color-bg-elevated` → `#fff` | Click, Hover, Dropdown, AC |
|
|
199
|
+
| `--popover-color` | `--color-text-primary` → `inherit` | Hover |
|
|
200
|
+
| `--popover-border-width` | `--border-thickness-m` → `1px` | Click, Hover, Dropdown, AC |
|
|
201
|
+
| `--popover-border-color` | `--color-border-default` → `#e0e0e0` | Click, Hover, Dropdown, AC |
|
|
202
|
+
| `--popover-radius` | `--radius-m` → `8px` | Click, Dropdown, AC |
|
|
203
|
+
| `--popover-radius-sm` | `--radius-s` → `6px` | Hover, menu items |
|
|
204
|
+
| `--popover-padding` | `--space-xs` → `4px` | Hover, Dropdown, AC |
|
|
205
|
+
| `--popover-padding-lg` | `--space-s` → `8px` | Click, Hover, Dropdown, AC |
|
|
206
|
+
| `--popover-shadow` | `--shadow-layer-2` → `0 4px 12px rgba(0,0,0,0.12)` | Click, Dropdown, AC |
|
|
207
|
+
| `--popover-shadow-sm` | `--shadow-layer-1` → `0 2px 8px rgba(0,0,0,0.08)` | Hover |
|
|
208
|
+
| `--popover-item-hover-bg` | `--color-surface-hover` → `rgba(0,0,0,0.04)` | Dropdown, AC |
|
|
209
|
+
| `--popover-separator-color` | `--color-border-subtle` → `#e0e0e0` | Dropdown, MenuSeparator |
|
|
210
|
+
| `--popover-focus-width` | `--focus-width` → `2px` | Dropdown trigger |
|
|
211
|
+
| `--popover-focus-color` | `--focus-color` → `currentColor` | Dropdown trigger |
|
|
212
|
+
| `--popover-focus-offset` | `--focus-offset` → `2px` | Dropdown trigger |
|
|
79
213
|
|
|
80
214
|
### Consumer Scenarios
|
|
81
215
|
|
|
@@ -87,10 +221,10 @@ This means:
|
|
|
87
221
|
|
|
88
222
|
```css
|
|
89
223
|
:root {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
224
|
+
--popover-bg: #1a1a2e;
|
|
225
|
+
--popover-border-color: #333;
|
|
226
|
+
--popover-radius: 12px;
|
|
227
|
+
--popover-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
|
|
94
228
|
}
|
|
95
229
|
```
|
|
96
230
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wentools/overlay",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Svelte 5 popover, dropdown menu, and tooltip components with CSS custom property theming",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -29,5 +29,8 @@
|
|
|
29
29
|
"svelte": ">=5.0.0",
|
|
30
30
|
"@wentools/spatial": ">=0.1.0",
|
|
31
31
|
"@wentools/simmer": ">=0.1.0"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@floating-ui/dom": "1.8.0"
|
|
32
35
|
}
|
|
33
36
|
}
|
|
@@ -31,7 +31,7 @@ Perfect for: search autocomplete, tag pickers, user selectors, command palettes.
|
|
|
31
31
|
<script lang="ts">
|
|
32
32
|
import type { Snippet } from 'svelte'
|
|
33
33
|
import Popover from './popover.svelte'
|
|
34
|
-
import type { Position } from './types'
|
|
34
|
+
import type { Position, RenderMode } from './types'
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* AutocompletePopover component props
|
|
@@ -41,6 +41,9 @@ Perfect for: search autocomplete, tag pickers, user selectors, command palettes.
|
|
|
41
41
|
open?: boolean
|
|
42
42
|
/** Position of the popover relative to input */
|
|
43
43
|
position?: Position
|
|
44
|
+
viewportMargin?: number
|
|
45
|
+
flip?: boolean
|
|
46
|
+
renderMode?: RenderMode
|
|
44
47
|
/** Offset from the input element in pixels */
|
|
45
48
|
offset?: number
|
|
46
49
|
/** Maximum width of suggestions content (scales with typography) */
|
|
@@ -72,6 +75,9 @@ Perfect for: search autocomplete, tag pickers, user selectors, command palettes.
|
|
|
72
75
|
let {
|
|
73
76
|
open = $bindable(false),
|
|
74
77
|
position = 'bottom-start',
|
|
78
|
+
viewportMargin = 0,
|
|
79
|
+
flip = true,
|
|
80
|
+
renderMode = 'inline',
|
|
75
81
|
offset = 4,
|
|
76
82
|
maxWidth = '60ch',
|
|
77
83
|
minWidth = '30ch',
|
|
@@ -112,12 +118,6 @@ Perfect for: search autocomplete, tag pickers, user selectors, command palettes.
|
|
|
112
118
|
onSelect?.(event)
|
|
113
119
|
}
|
|
114
120
|
break
|
|
115
|
-
case 'Escape':
|
|
116
|
-
if (open && closeOnEscape) {
|
|
117
|
-
event.preventDefault()
|
|
118
|
-
close()
|
|
119
|
-
}
|
|
120
|
-
break
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
|
|
@@ -135,6 +135,9 @@ Perfect for: search autocomplete, tag pickers, user selectors, command palettes.
|
|
|
135
135
|
<Popover
|
|
136
136
|
bind:open
|
|
137
137
|
{position}
|
|
138
|
+
{viewportMargin}
|
|
139
|
+
{flip}
|
|
140
|
+
{renderMode}
|
|
138
141
|
{offset}
|
|
139
142
|
{triggerDisplay}
|
|
140
143
|
{closeOnClickOutside}
|
|
@@ -156,9 +159,9 @@ Perfect for: search autocomplete, tag pickers, user selectors, command palettes.
|
|
|
156
159
|
{#snippet content({ close })}
|
|
157
160
|
<div
|
|
158
161
|
class="autocomplete-suggestions"
|
|
159
|
-
style
|
|
160
|
-
style
|
|
161
|
-
style
|
|
162
|
+
style:--suggestions-max-width={maxWidth === 'none' ? '100vw' : maxWidth}
|
|
163
|
+
style:--suggestions-min-width={minWidth}
|
|
164
|
+
style:--suggestions-max-height={maxHeight === 'none' ? '100dvh' : maxHeight}
|
|
162
165
|
>
|
|
163
166
|
{@render suggestions({ close })}
|
|
164
167
|
</div>
|
|
@@ -173,15 +176,29 @@ Perfect for: search autocomplete, tag pickers, user selectors, command palettes.
|
|
|
173
176
|
}
|
|
174
177
|
|
|
175
178
|
.autocomplete-suggestions {
|
|
176
|
-
|
|
177
|
-
|
|
179
|
+
box-sizing: border-box;
|
|
180
|
+
min-width: min(
|
|
181
|
+
var(--suggestions-min-width),
|
|
182
|
+
var(--suggestions-max-width),
|
|
183
|
+
var(--popover-available-width, 100vw)
|
|
184
|
+
);
|
|
185
|
+
max-width: min(var(--suggestions-max-width), var(--popover-available-width, 100vw));
|
|
186
|
+
max-height: min(var(--suggestions-max-height), var(--popover-available-height, 100dvh));
|
|
187
|
+
overflow-wrap: anywhere;
|
|
188
|
+
--menu-item-padding: var(--popover-padding, var(--space-xs, 4px))
|
|
189
|
+
var(--popover-padding-lg, var(--space-s, 8px));
|
|
190
|
+
--menu-item-hover-bg: var(
|
|
191
|
+
--popover-item-hover-bg,
|
|
192
|
+
var(--color-surface-hover, rgba(0, 0, 0, 0.04))
|
|
193
|
+
);
|
|
178
194
|
--menu-item-border-radius: var(--popover-radius-sm, var(--radius-s, 6px));
|
|
179
195
|
|
|
180
196
|
background: var(--popover-bg, var(--color-bg-elevated, #fff));
|
|
181
|
-
border: var(--popover-border-width, var(--border-thickness-m, 1px)) solid
|
|
197
|
+
border: var(--popover-border-width, var(--border-thickness-m, 1px)) solid
|
|
198
|
+
var(--popover-border-color, var(--color-border-default, #e0e0e0));
|
|
182
199
|
border-radius: var(--popover-radius, var(--radius-m, 8px));
|
|
183
200
|
padding: var(--popover-padding, var(--space-xs, 4px));
|
|
184
|
-
box-shadow: var(--popover-shadow, var(--shadow-layer-2, 0 4px 12px rgba(0,0,0,0.12)));
|
|
201
|
+
box-shadow: var(--popover-shadow, var(--shadow-layer-2, 0 4px 12px rgba(0, 0, 0, 0.12)));
|
|
185
202
|
overflow-y: auto;
|
|
186
203
|
}
|
|
187
204
|
</style>
|
package/src/click_popover.svelte
CHANGED
|
@@ -1,116 +1,158 @@
|
|
|
1
|
-
<!--
|
|
2
|
-
@component
|
|
3
|
-
ClickPopover - Click-triggered popover component
|
|
4
|
-
|
|
5
|
-
Provides a popover that opens/closes when the trigger is clicked.
|
|
6
|
-
Handles focus management and provides toggle functionality.
|
|
7
|
-
|
|
8
|
-
Perfect for: action menus, info popovers, quick settings panels.
|
|
9
|
-
|
|
10
|
-
@example
|
|
11
|
-
```svelte
|
|
12
|
-
<ClickPopover>
|
|
13
|
-
{#snippet children({ toggle, open })}
|
|
14
|
-
<button onclick={toggle}>
|
|
15
|
-
Options {open ? '▼' : '▶'}
|
|
16
|
-
</button>
|
|
17
|
-
{/snippet}
|
|
18
|
-
|
|
19
|
-
{#snippet content({ close })}
|
|
20
|
-
<div>
|
|
21
|
-
<button onclick={close}>Action 1</button>
|
|
22
|
-
<button onclick={close}>Action 2</button>
|
|
23
|
-
</div>
|
|
24
|
-
{/snippet}
|
|
25
|
-
</ClickPopover>
|
|
26
|
-
```
|
|
27
|
-
-->
|
|
28
1
|
<script lang="ts">
|
|
29
|
-
import type
|
|
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 { ClickPopoverTrigger, CloseOptions, Position, RenderMode } from './types'
|
|
32
6
|
|
|
33
7
|
interface Props {
|
|
34
|
-
/** Position of the popover */
|
|
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
|
-
|
|
14
|
+
minWidth?: string
|
|
15
|
+
maxHeight?: string
|
|
41
16
|
class?: string
|
|
42
|
-
/** Whether the popover is disabled */
|
|
43
17
|
disabled?: boolean
|
|
44
|
-
/** Whether to close on click outside (default true) */
|
|
45
18
|
closeOnClickOutside?: boolean
|
|
46
|
-
/** Whether to close on escape key (default true) */
|
|
47
19
|
closeOnEscape?: boolean
|
|
48
|
-
/** Callback when open state changes */
|
|
49
20
|
onOpenChange?: (open: boolean) => void
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
21
|
+
initialFocus?: () => HTMLElement | null | undefined
|
|
22
|
+
/** Preferred API: one caller-owned button, with props and registration action. */
|
|
23
|
+
trigger?: Snippet<[ClickPopoverTrigger]>
|
|
24
|
+
/** Legacy API: non-interactive content inside the supplied trigger wrapper. */
|
|
25
|
+
children?: Snippet<[{ toggle: () => void; open: boolean }]>
|
|
26
|
+
content: Snippet<[{ close: (options?: CloseOptions) => void; open: boolean }]>
|
|
54
27
|
}
|
|
55
|
-
|
|
56
28
|
let {
|
|
57
29
|
position = 'bottom-start',
|
|
58
30
|
offset = 4,
|
|
31
|
+
viewportMargin = 0,
|
|
32
|
+
flip = true,
|
|
33
|
+
renderMode = 'inline',
|
|
59
34
|
maxWidth = '50ch',
|
|
35
|
+
minWidth = '0px',
|
|
36
|
+
maxHeight = 'none',
|
|
60
37
|
class: className = '',
|
|
61
38
|
disabled = false,
|
|
62
39
|
closeOnClickOutside = true,
|
|
63
40
|
closeOnEscape = true,
|
|
64
41
|
onOpenChange,
|
|
42
|
+
initialFocus,
|
|
43
|
+
trigger: triggerSnippet,
|
|
65
44
|
children,
|
|
66
45
|
content: _content,
|
|
67
46
|
}: Props = $props()
|
|
68
|
-
|
|
69
47
|
let open = $state(false)
|
|
70
|
-
|
|
48
|
+
let triggerElement: HTMLElement | undefined = $state()
|
|
49
|
+
let contentElement: HTMLDivElement | undefined
|
|
50
|
+
let pendingFocus = false
|
|
51
|
+
let mayFocus = () => false
|
|
52
|
+
const focusReturn = createFocusReturn()
|
|
53
|
+
const close = (options: CloseOptions = {}) => {
|
|
54
|
+
if (!open) return
|
|
55
|
+
pendingFocus = false
|
|
56
|
+
focusReturn.cancel()
|
|
57
|
+
open = false
|
|
58
|
+
onOpenChange?.(false)
|
|
59
|
+
if (
|
|
60
|
+
options.restoreFocus !== false &&
|
|
61
|
+
contentElement?.contains(contentElement.ownerDocument.activeElement)
|
|
62
|
+
)
|
|
63
|
+
focusReturn.schedule(triggerElement, contentElement)
|
|
64
|
+
}
|
|
71
65
|
const toggle = () => {
|
|
72
66
|
if (disabled) return
|
|
73
|
-
open
|
|
74
|
-
|
|
67
|
+
if (open) close({ restoreFocus: false })
|
|
68
|
+
else {
|
|
69
|
+
focusReturn.cancel()
|
|
70
|
+
pendingFocus = true
|
|
71
|
+
mayFocus = claimInitialFocus(triggerElement)
|
|
72
|
+
open = true
|
|
73
|
+
onOpenChange?.(true)
|
|
74
|
+
}
|
|
75
75
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
76
|
+
const registerTrigger = (node: HTMLButtonElement) => {
|
|
77
|
+
triggerElement = node
|
|
78
|
+
return {
|
|
79
|
+
destroy: () => {
|
|
80
|
+
if (triggerElement === node) triggerElement = undefined
|
|
81
|
+
},
|
|
82
|
+
}
|
|
80
83
|
}
|
|
84
|
+
const triggerProps = $derived<ClickPopoverTrigger['triggerProps']>({
|
|
85
|
+
type: 'button',
|
|
86
|
+
disabled,
|
|
87
|
+
'aria-expanded': open,
|
|
88
|
+
onclick: toggle,
|
|
89
|
+
})
|
|
90
|
+
onDestroy(() => focusReturn.cancel())
|
|
81
91
|
</script>
|
|
82
92
|
|
|
83
93
|
<Popover
|
|
84
94
|
bind:open
|
|
85
95
|
{position}
|
|
86
96
|
{offset}
|
|
97
|
+
{viewportMargin}
|
|
98
|
+
{flip}
|
|
99
|
+
{renderMode}
|
|
87
100
|
{closeOnClickOutside}
|
|
88
101
|
{closeOnEscape}
|
|
89
|
-
onOpenChange={(
|
|
90
|
-
|
|
91
|
-
|
|
102
|
+
onOpenChange={(value) => {
|
|
103
|
+
pendingFocus = false
|
|
104
|
+
focusReturn.cancel()
|
|
105
|
+
onOpenChange?.(value)
|
|
106
|
+
}}
|
|
107
|
+
onClose={(reason) => {
|
|
108
|
+
if (reason === 'escape' && contentElement?.contains(contentElement.ownerDocument.activeElement))
|
|
109
|
+
focusReturn.schedule(triggerElement, contentElement)
|
|
110
|
+
}}
|
|
111
|
+
onPositioned={() => {
|
|
112
|
+
if (!pendingFocus) return
|
|
113
|
+
pendingFocus = false
|
|
114
|
+
if (mayFocus()) initialFocus?.()?.focus({ preventScroll: true })
|
|
92
115
|
}}
|
|
93
116
|
class="click-popover {className}"
|
|
94
117
|
>
|
|
95
118
|
{#snippet trigger()}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
119
|
+
{#if triggerSnippet}
|
|
120
|
+
{@render triggerSnippet({ open, triggerProps, registerTrigger })}
|
|
121
|
+
{:else}
|
|
122
|
+
<div
|
|
123
|
+
bind:this={triggerElement}
|
|
124
|
+
role="button"
|
|
125
|
+
tabindex={disabled ? -1 : 0}
|
|
126
|
+
aria-disabled={disabled || undefined}
|
|
127
|
+
aria-expanded={open}
|
|
128
|
+
onclick={(event) => {
|
|
129
|
+
if (
|
|
130
|
+
event.target === event.currentTarget ||
|
|
131
|
+
!(event.target instanceof Element) ||
|
|
132
|
+
!event.target.closest('button, a, input, select, textarea')
|
|
133
|
+
)
|
|
134
|
+
toggle()
|
|
135
|
+
}}
|
|
136
|
+
onkeydown={(event) => {
|
|
137
|
+
if (event.target !== event.currentTarget) return
|
|
138
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
139
|
+
event.preventDefault()
|
|
140
|
+
toggle()
|
|
141
|
+
}
|
|
142
|
+
}}
|
|
143
|
+
>
|
|
144
|
+
{@render children?.({ toggle, open })}
|
|
145
|
+
</div>
|
|
146
|
+
{/if}
|
|
110
147
|
{/snippet}
|
|
111
|
-
|
|
112
148
|
{#snippet content()}
|
|
113
|
-
<div
|
|
149
|
+
<div
|
|
150
|
+
bind:this={contentElement}
|
|
151
|
+
class="click-popover-content"
|
|
152
|
+
style:--panel-max-width={maxWidth === 'none' ? '100vw' : maxWidth}
|
|
153
|
+
style:--panel-min-width={minWidth}
|
|
154
|
+
style:--panel-max-height={maxHeight === 'none' ? '100dvh' : maxHeight}
|
|
155
|
+
>
|
|
114
156
|
{@render _content({ close, open })}
|
|
115
157
|
</div>
|
|
116
158
|
{/snippet}
|
|
@@ -118,10 +160,21 @@ Perfect for: action menus, info popovers, quick settings panels.
|
|
|
118
160
|
|
|
119
161
|
<style>
|
|
120
162
|
.click-popover-content {
|
|
163
|
+
box-sizing: border-box;
|
|
164
|
+
min-width: min(
|
|
165
|
+
var(--panel-min-width),
|
|
166
|
+
var(--panel-max-width),
|
|
167
|
+
var(--popover-available-width, 100vw)
|
|
168
|
+
);
|
|
169
|
+
max-width: min(var(--panel-max-width), var(--popover-available-width, 100vw));
|
|
170
|
+
max-height: min(var(--panel-max-height), var(--popover-available-height, 100dvh));
|
|
171
|
+
overflow: auto;
|
|
172
|
+
overflow-wrap: anywhere;
|
|
121
173
|
background: var(--popover-bg, var(--color-bg-elevated, #fff));
|
|
122
|
-
border: var(--popover-border-width, var(--border-thickness-m, 1px)) solid
|
|
174
|
+
border: var(--popover-border-width, var(--border-thickness-m, 1px)) solid
|
|
175
|
+
var(--popover-border-color, var(--color-border-default, #e0e0e0));
|
|
123
176
|
border-radius: var(--popover-radius, var(--radius-m, 8px));
|
|
124
177
|
padding: var(--popover-padding-lg, var(--space-s, 8px));
|
|
125
|
-
box-shadow: var(--popover-shadow, var(--shadow-layer-2, 0 4px 12px rgba(0,0,0,0.12)));
|
|
178
|
+
box-shadow: var(--popover-shadow, var(--shadow-layer-2, 0 4px 12px rgba(0, 0, 0, 0.12)));
|
|
126
179
|
}
|
|
127
180
|
</style>
|