@human-kit/ui 1.0.0-beta.3 → 1.0.0-beta.5
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 +15 -6
- package/dist/FOCUS_STATE_CONTRACT.md +2 -0
- package/dist/autocomplete/list/autocomplete-list.svelte.d.ts +3 -1
- package/dist/button/root/button-root.svelte +19 -3
- package/dist/button/root/button-root.svelte.d.ts +10 -0
- package/dist/combobox/list/combobox-listbox.svelte.d.ts +3 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/internal/environment.d.ts +11 -0
- package/dist/internal/environment.js +14 -0
- package/dist/internal/localized-strings.d.ts +80 -0
- package/dist/internal/localized-strings.js +80 -0
- package/dist/listbox/README.md +27 -1
- package/dist/listbox/item/listbox-item.svelte +11 -2
- package/dist/listbox/root/context.d.ts +25 -0
- package/dist/listbox/root/context.js +108 -1
- package/dist/listbox/root/listbox.svelte +85 -7
- package/dist/listbox/root/listbox.svelte.d.ts +13 -1
- package/dist/menu/README.md +46 -0
- package/dist/menu/content/menu-content.svelte +16 -5
- package/dist/menu/content/menu-content.svelte.d.ts +2 -1
- package/dist/menu/context-trigger/menu-context-trigger.svelte +195 -0
- package/dist/menu/context-trigger/menu-context-trigger.svelte.d.ts +36 -0
- package/dist/menu/index.d.ts +4 -1
- package/dist/menu/index.js +1 -0
- package/dist/menu/index.parts.d.ts +1 -0
- package/dist/menu/index.parts.js +1 -0
- package/dist/menu/root/context.d.ts +18 -0
- package/dist/menu/root/menu-state.svelte.js +19 -0
- package/dist/popover/content/popover-content.svelte +2 -1
- package/dist/primitives/floating.d.ts +33 -5
- package/dist/primitives/floating.js +25 -0
- package/dist/primitives/index.d.ts +1 -0
- package/dist/primitives/index.js +1 -0
- package/dist/primitives/keyboard-navigation.d.ts +23 -5
- package/dist/primitives/keyboard-navigation.js +22 -15
- package/dist/primitives/long-press.d.ts +59 -0
- package/dist/primitives/long-press.js +142 -0
- package/dist/primitives/scroll-lock.d.ts +24 -0
- package/dist/primitives/scroll-lock.js +40 -0
- package/dist/toggle-group/README.md +10 -0
- package/dist/toggle-group/root/context.svelte.d.ts +1 -1
- package/dist/toggle-group/root/context.svelte.js +20 -11
- package/dist/toggle-group/root/toggle-group-root.svelte +20 -15
- package/dist/toggle-group/root/toggle-group-teardown-order-child.svelte +13 -0
- package/dist/toggle-group/root/toggle-group-teardown-order-child.svelte.d.ts +7 -0
- package/dist/toggle-group/types.d.ts +7 -8
- package/dist/transfer-list/README.md +121 -0
- package/dist/transfer-list/index.d.ts +33 -0
- package/dist/transfer-list/index.js +18 -0
- package/dist/transfer-list/index.parts.d.ts +9 -0
- package/dist/transfer-list/index.parts.js +10 -0
- package/dist/transfer-list/item/transfer-list-item.svelte +74 -0
- package/dist/transfer-list/item/transfer-list-item.svelte.d.ts +35 -0
- package/dist/transfer-list/list/transfer-list-list.svelte +162 -0
- package/dist/transfer-list/list/transfer-list-list.svelte.d.ts +56 -0
- package/dist/transfer-list/list/transfer-list-source.svelte +13 -0
- package/dist/transfer-list/list/transfer-list-source.svelte.d.ts +36 -0
- package/dist/transfer-list/list/transfer-list-target.svelte +14 -0
- package/dist/transfer-list/list/transfer-list-target.svelte.d.ts +36 -0
- package/dist/transfer-list/move/transfer-list-move-all.svelte +14 -0
- package/dist/transfer-list/move/transfer-list-move-all.svelte.d.ts +10 -0
- package/dist/transfer-list/move/transfer-list-move-button.svelte +103 -0
- package/dist/transfer-list/move/transfer-list-move-button.svelte.d.ts +25 -0
- package/dist/transfer-list/move/transfer-list-move-down.svelte +14 -0
- package/dist/transfer-list/move/transfer-list-move-down.svelte.d.ts +10 -0
- package/dist/transfer-list/move/transfer-list-move-selected.svelte +14 -0
- package/dist/transfer-list/move/transfer-list-move-selected.svelte.d.ts +10 -0
- package/dist/transfer-list/move/transfer-list-move-up.svelte +14 -0
- package/dist/transfer-list/move/transfer-list-move-up.svelte.d.ts +10 -0
- package/dist/transfer-list/move/transfer-list-reorder-button.svelte +83 -0
- package/dist/transfer-list/move/transfer-list-reorder-button.svelte.d.ts +21 -0
- package/dist/transfer-list/root/context.d.ts +70 -0
- package/dist/transfer-list/root/context.js +24 -0
- package/dist/transfer-list/root/transfer-list-root.svelte +342 -0
- package/dist/transfer-list/root/transfer-list-root.svelte.d.ts +65 -0
- package/dist/transfer-list/root/types.d.ts +22 -0
- package/dist/transfer-list/root/types.js +4 -0
- package/dist/transfer-list/status/transfer-list-status.svelte +70 -0
- package/dist/transfer-list/status/transfer-list-status.svelte.d.ts +15 -0
- package/package.json +6 -1
|
@@ -15,6 +15,11 @@ export type MenuOpenReason = 'trigger-press' | 'imperative-action' | 'none';
|
|
|
15
15
|
export type MenuChangeReason = MenuOpenReason | MenuCloseReason;
|
|
16
16
|
/** Where focus should land when the menu content opens. */
|
|
17
17
|
export type MenuOpenFocusIntent = 'first' | 'last';
|
|
18
|
+
/** A viewport point (`clientX`/`clientY`) the content can be anchored to instead of an element. */
|
|
19
|
+
export type MenuAnchorPoint = {
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
};
|
|
18
23
|
export type MenuOpenChangeDetails = {
|
|
19
24
|
reason: MenuChangeReason;
|
|
20
25
|
event?: Event;
|
|
@@ -47,6 +52,15 @@ export type MenuContext = {
|
|
|
47
52
|
triggerRef: HTMLElement | null;
|
|
48
53
|
/** Reference to the content (role="menu") element. */
|
|
49
54
|
contentRef: HTMLElement | null;
|
|
55
|
+
/**
|
|
56
|
+
* Viewport point the content should be anchored to, or `null` to anchor to the
|
|
57
|
+
* trigger element. Set by `Menu.ContextTrigger` from the pointer that opened the
|
|
58
|
+
* menu; a keyboard open clears it so the panel lands on the surface itself
|
|
59
|
+
* rather than wherever the pointer happened to be.
|
|
60
|
+
*/
|
|
61
|
+
anchorPoint: MenuAnchorPoint | null;
|
|
62
|
+
/** Whether this menu is opened as a context menu (right click / long press). */
|
|
63
|
+
isContextMenu: boolean;
|
|
50
64
|
/** Whether arrow navigation wraps around the ends. */
|
|
51
65
|
loop: boolean;
|
|
52
66
|
/** Whether typeahead is enabled. */
|
|
@@ -63,6 +77,10 @@ export type MenuContext = {
|
|
|
63
77
|
setTriggerRef: (el: HTMLElement | null) => void;
|
|
64
78
|
/** Set the content ref (used by Content). */
|
|
65
79
|
setContentRef: (el: HTMLElement | null) => void;
|
|
80
|
+
/** Anchors the content to a viewport point, or back to the trigger with `null`. */
|
|
81
|
+
setAnchorPoint: (point: MenuAnchorPoint | null) => void;
|
|
82
|
+
/** Declares this menu a context menu (used by ContextTrigger on mount/unmount). */
|
|
83
|
+
setContextMenu: (value: boolean) => void;
|
|
66
84
|
/** Toggle the menu open state. */
|
|
67
85
|
toggle: (reason?: MenuOpenReason, event?: Event) => void;
|
|
68
86
|
/** Open the menu. */
|
|
@@ -10,6 +10,8 @@ import { addTriggerBlurCleanup, applyTriggerCloseFocusState, clearTriggerFocusSt
|
|
|
10
10
|
export function createMenuState(options) {
|
|
11
11
|
let closeReason = $state('none');
|
|
12
12
|
let contentRef = $state(null);
|
|
13
|
+
let anchorPoint = $state(null);
|
|
14
|
+
let isContextMenu = $state(false);
|
|
13
15
|
let cleanupTriggerBlurListener;
|
|
14
16
|
let pendingTriggerCloseFocusFrame;
|
|
15
17
|
// Capture initial value only (not reactive).
|
|
@@ -112,6 +114,9 @@ export function createMenuState(options) {
|
|
|
112
114
|
// cancelled by the consumer must leave the menu's interaction state untouched.
|
|
113
115
|
keyboardNav.setCurrentId(null);
|
|
114
116
|
submenuIntent.cancel();
|
|
117
|
+
// Drop the pointer point too, so the next open doesn't reuse a stale cursor
|
|
118
|
+
// position — a keyboard reopen has to land on the trigger, not where the mouse was.
|
|
119
|
+
anchorPoint = null;
|
|
115
120
|
const triggerRef = options.getTriggerRef();
|
|
116
121
|
if (!triggerRef)
|
|
117
122
|
return;
|
|
@@ -144,6 +149,12 @@ export function createMenuState(options) {
|
|
|
144
149
|
function setContentRef(el) {
|
|
145
150
|
contentRef = el;
|
|
146
151
|
}
|
|
152
|
+
function setAnchorPoint(point) {
|
|
153
|
+
anchorPoint = point;
|
|
154
|
+
}
|
|
155
|
+
function setContextMenu(value) {
|
|
156
|
+
isContextMenu = value;
|
|
157
|
+
}
|
|
147
158
|
let openFocusIntent = null;
|
|
148
159
|
function requestOpenFocus(intent) {
|
|
149
160
|
openFocusIntent = intent;
|
|
@@ -214,6 +225,12 @@ export function createMenuState(options) {
|
|
|
214
225
|
get contentRef() {
|
|
215
226
|
return contentRef;
|
|
216
227
|
},
|
|
228
|
+
get anchorPoint() {
|
|
229
|
+
return anchorPoint;
|
|
230
|
+
},
|
|
231
|
+
get isContextMenu() {
|
|
232
|
+
return isContextMenu;
|
|
233
|
+
},
|
|
217
234
|
get loop() {
|
|
218
235
|
return options.getLoop();
|
|
219
236
|
},
|
|
@@ -228,6 +245,8 @@ export function createMenuState(options) {
|
|
|
228
245
|
keyboardNav,
|
|
229
246
|
setTriggerRef,
|
|
230
247
|
setContentRef,
|
|
248
|
+
setAnchorPoint,
|
|
249
|
+
setContextMenu,
|
|
231
250
|
toggle,
|
|
232
251
|
open: openMenu,
|
|
233
252
|
close: closeMenu,
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { browser } from '../../internal/environment';
|
|
6
6
|
import { floating, type ExtendedPlacement } from '../../primitives/floating';
|
|
7
7
|
import { focusTrap, type FocusTrapOptions } from '../../primitives/focus-trap';
|
|
8
|
-
import { scrollLock } from '../../primitives/scroll-lock';
|
|
8
|
+
import { allowScrollWithin, scrollLock } from '../../primitives/scroll-lock';
|
|
9
9
|
import { clickOutside } from '../../primitives/click-outside';
|
|
10
10
|
import { ariaHideOutside } from '../../primitives/aria-hide-outside';
|
|
11
11
|
import { releaseFocusedDescendant } from '../../primitives/release-focused-descendant';
|
|
@@ -350,6 +350,7 @@
|
|
|
350
350
|
}}
|
|
351
351
|
use:focusTrap={{ enabled: isOpen && isModal, restoreFocus: false, initialFocus }}
|
|
352
352
|
use:scrollLock={(isOpen || presence.isExiting) && isModal}
|
|
353
|
+
use:allowScrollWithin={isOpen && !isModal}
|
|
353
354
|
use:ariaHideOutside={isOpen && isModal}
|
|
354
355
|
style="position: fixed; z-index: {zIndex};"
|
|
355
356
|
{...restProps}
|
|
@@ -1,4 +1,32 @@
|
|
|
1
|
-
import { type Placement as FloatingPlacement } from '@floating-ui/dom';
|
|
1
|
+
import { type Placement as FloatingPlacement, type VirtualElement } from '@floating-ui/dom';
|
|
2
|
+
/**
|
|
3
|
+
* What a floating panel can be positioned against: a real element, or a virtual
|
|
4
|
+
* one — an object that only knows how to report a rect. Floating UI supports
|
|
5
|
+
* both natively, in `computePosition` and in `autoUpdate` (which checks for a
|
|
6
|
+
* real element before observing it for resizes, and falls back to the virtual
|
|
7
|
+
* element's `contextElement` for the ancestor scroll listeners).
|
|
8
|
+
*/
|
|
9
|
+
export type FloatingAnchor = HTMLElement | VirtualElement | null;
|
|
10
|
+
/**
|
|
11
|
+
* A zero-size anchor at a viewport point, for panels that follow the pointer
|
|
12
|
+
* instead of an element — a context menu at the cursor, a toolbar at a text
|
|
13
|
+
* selection.
|
|
14
|
+
*
|
|
15
|
+
* `contextElement` should be the surface the point was measured on: without it
|
|
16
|
+
* `autoUpdate` has no node whose scroll ancestors it can watch, so the panel
|
|
17
|
+
* would not track a scrolling container.
|
|
18
|
+
*
|
|
19
|
+
* Coordinates are viewport-relative (`clientX`/`clientY`) and positioning uses
|
|
20
|
+
* the `fixed` strategy, so the panel stays where the pointer was — which is what
|
|
21
|
+
* a native context menu does.
|
|
22
|
+
*
|
|
23
|
+
* Because the rect has no size, `--trigger-width` / `--trigger-height` resolve to
|
|
24
|
+
* `0px` on the panel. Everything else (`flip`, `shift`, `size`, the
|
|
25
|
+
* `--available-*` custom properties and `--transform-origin`) works unchanged,
|
|
26
|
+
* which is the whole reason to go through Floating UI rather than assigning
|
|
27
|
+
* `left`/`top` by hand.
|
|
28
|
+
*/
|
|
29
|
+
export declare function createPointAnchor(x: number, y: number, contextElement?: Element | null): VirtualElement;
|
|
2
30
|
/**
|
|
3
31
|
* Placement options for floating elements.
|
|
4
32
|
* Follows the specification with logical 'start'/'end' values.
|
|
@@ -40,9 +68,9 @@ export type FloatingOptions = {
|
|
|
40
68
|
* - `--available-height`: Available height between trigger and viewport edge
|
|
41
69
|
* - `--transform-origin`: Coordinates for animations (e.g., "center top")
|
|
42
70
|
*/
|
|
43
|
-
export declare function createFloating(anchorElement:
|
|
71
|
+
export declare function createFloating(anchorElement: FloatingAnchor, options?: FloatingOptions): (floatingElement: HTMLElement) => {
|
|
44
72
|
update(newOptions: {
|
|
45
|
-
anchor:
|
|
73
|
+
anchor: FloatingAnchor;
|
|
46
74
|
} & FloatingOptions): void;
|
|
47
75
|
destroy(): void;
|
|
48
76
|
};
|
|
@@ -58,10 +86,10 @@ export declare function createFloating(anchorElement: HTMLElement | null, option
|
|
|
58
86
|
* - `--transform-origin`: Coordinates for animations (e.g., "center top")
|
|
59
87
|
*/
|
|
60
88
|
export declare function floating(floatingElement: HTMLElement, options: {
|
|
61
|
-
anchor:
|
|
89
|
+
anchor: FloatingAnchor;
|
|
62
90
|
} & FloatingOptions): {
|
|
63
91
|
update(newOptions: {
|
|
64
|
-
anchor:
|
|
92
|
+
anchor: FloatingAnchor;
|
|
65
93
|
} & FloatingOptions): void;
|
|
66
94
|
destroy(): void;
|
|
67
95
|
};
|
|
@@ -1,4 +1,29 @@
|
|
|
1
1
|
import { computePosition, flip, shift, limitShift, offset as offsetMiddleware, size, autoUpdate } from '@floating-ui/dom';
|
|
2
|
+
/**
|
|
3
|
+
* A zero-size anchor at a viewport point, for panels that follow the pointer
|
|
4
|
+
* instead of an element — a context menu at the cursor, a toolbar at a text
|
|
5
|
+
* selection.
|
|
6
|
+
*
|
|
7
|
+
* `contextElement` should be the surface the point was measured on: without it
|
|
8
|
+
* `autoUpdate` has no node whose scroll ancestors it can watch, so the panel
|
|
9
|
+
* would not track a scrolling container.
|
|
10
|
+
*
|
|
11
|
+
* Coordinates are viewport-relative (`clientX`/`clientY`) and positioning uses
|
|
12
|
+
* the `fixed` strategy, so the panel stays where the pointer was — which is what
|
|
13
|
+
* a native context menu does.
|
|
14
|
+
*
|
|
15
|
+
* Because the rect has no size, `--trigger-width` / `--trigger-height` resolve to
|
|
16
|
+
* `0px` on the panel. Everything else (`flip`, `shift`, `size`, the
|
|
17
|
+
* `--available-*` custom properties and `--transform-origin`) works unchanged,
|
|
18
|
+
* which is the whole reason to go through Floating UI rather than assigning
|
|
19
|
+
* `left`/`top` by hand.
|
|
20
|
+
*/
|
|
21
|
+
export function createPointAnchor(x, y, contextElement) {
|
|
22
|
+
return {
|
|
23
|
+
getBoundingClientRect: () => new DOMRect(x, y, 0, 0),
|
|
24
|
+
...(contextElement ? { contextElement } : {})
|
|
25
|
+
};
|
|
26
|
+
}
|
|
2
27
|
/** Viewport gutter applied when no `collisionPadding` is given. */
|
|
3
28
|
const DEFAULT_COLLISION_PADDING = 8;
|
|
4
29
|
/**
|
package/dist/primitives/index.js
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
import { type Writable } from 'svelte/store';
|
|
2
2
|
export type KeyboardNavigationOrientation = 'vertical' | 'horizontal' | 'both';
|
|
3
|
+
/**
|
|
4
|
+
* Why the focus moved, for consumers that do more than move a highlight.
|
|
5
|
+
*
|
|
6
|
+
* `extend` is set when the key was pressed with Shift held — the multi-select
|
|
7
|
+
* listbox pattern, where arrowing extends the selection from an anchor instead of
|
|
8
|
+
* just walking it. Programmatic moves (`focusNext()`, `focusById()`) carry no
|
|
9
|
+
* intent at all, so a consumer that ignores this argument behaves exactly as before.
|
|
10
|
+
*/
|
|
11
|
+
export type NavigationIntent = {
|
|
12
|
+
extend?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* The item focus moved away from. Reported because the consumer cannot recover it
|
|
15
|
+
* afterwards: `focusItem` focuses the element before it notifies, and the focused
|
|
16
|
+
* element's own `focus` handler has already overwritten the consumer's idea of where
|
|
17
|
+
* focus was by the time this runs.
|
|
18
|
+
*/
|
|
19
|
+
from?: string | number | null;
|
|
20
|
+
};
|
|
3
21
|
export type KeyboardNavigationOptions = {
|
|
4
22
|
/**
|
|
5
23
|
* Orientation of the navigation
|
|
@@ -26,7 +44,7 @@ export type KeyboardNavigationOptions = {
|
|
|
26
44
|
/**
|
|
27
45
|
* Callback when focused item changes
|
|
28
46
|
*/
|
|
29
|
-
onFocusChange?: (id: string | number | null, element: HTMLElement | null) => void;
|
|
47
|
+
onFocusChange?: (id: string | number | null, element: HTMLElement | null, intent?: NavigationIntent) => void;
|
|
30
48
|
/**
|
|
31
49
|
* Callback for Ctrl+A (select all)
|
|
32
50
|
*/
|
|
@@ -53,10 +71,10 @@ export type KeyboardNavigationReturn = {
|
|
|
53
71
|
destroy: () => void;
|
|
54
72
|
};
|
|
55
73
|
/** Programmatic navigation methods */
|
|
56
|
-
focusNext: () => void;
|
|
57
|
-
focusPrevious: () => void;
|
|
58
|
-
focusFirst: () => void;
|
|
59
|
-
focusLast: () => void;
|
|
74
|
+
focusNext: (intent?: NavigationIntent) => void;
|
|
75
|
+
focusPrevious: (intent?: NavigationIntent) => void;
|
|
76
|
+
focusFirst: (intent?: NavigationIntent) => void;
|
|
77
|
+
focusLast: (intent?: NavigationIntent) => void;
|
|
60
78
|
focusById: (id: string | number) => void;
|
|
61
79
|
setCurrentId: (id: string | number | null) => void;
|
|
62
80
|
/** Update items (call after DOM changes) */
|
|
@@ -95,11 +95,14 @@ export function createKeyboardNavigation(options = {}) {
|
|
|
95
95
|
}
|
|
96
96
|
return rawId;
|
|
97
97
|
}
|
|
98
|
-
function focusItem(element) {
|
|
98
|
+
function focusItem(element, intent) {
|
|
99
|
+
let previousId = null;
|
|
100
|
+
focusedId.subscribe((value) => (previousId = value))();
|
|
101
|
+
const resolvedIntent = intent ? { ...intent, from: previousId } : undefined;
|
|
99
102
|
if (!element) {
|
|
100
103
|
focusedId.set(null);
|
|
101
104
|
focusedElement.set(null);
|
|
102
|
-
onFocusChange?.(null, null);
|
|
105
|
+
onFocusChange?.(null, null, resolvedIntent);
|
|
103
106
|
return;
|
|
104
107
|
}
|
|
105
108
|
const id = getItemId(element);
|
|
@@ -108,7 +111,7 @@ export function createKeyboardNavigation(options = {}) {
|
|
|
108
111
|
// Note: tabIndex is controlled by Svelte via isFocused state
|
|
109
112
|
// We just call focus() and notify - the component will react to onFocusChange
|
|
110
113
|
element.focus();
|
|
111
|
-
onFocusChange?.(id, element);
|
|
114
|
+
onFocusChange?.(id, element, resolvedIntent);
|
|
112
115
|
}
|
|
113
116
|
function getCurrentIndex() {
|
|
114
117
|
// The live DOM focus wins: a native focus change (mouse click, script `.focus()`, focus
|
|
@@ -126,7 +129,7 @@ export function createKeyboardNavigation(options = {}) {
|
|
|
126
129
|
focusedElement.subscribe((el) => (currentElement = el))();
|
|
127
130
|
return currentElement ? items.indexOf(currentElement) : -1;
|
|
128
131
|
}
|
|
129
|
-
function focusNext() {
|
|
132
|
+
function focusNext(intent) {
|
|
130
133
|
items = getItems();
|
|
131
134
|
if (items.length === 0)
|
|
132
135
|
return;
|
|
@@ -141,9 +144,9 @@ export function createKeyboardNavigation(options = {}) {
|
|
|
141
144
|
else {
|
|
142
145
|
nextIdx = Math.min(currentIdx + 1, items.length - 1);
|
|
143
146
|
}
|
|
144
|
-
focusItem(items[nextIdx]);
|
|
147
|
+
focusItem(items[nextIdx], intent);
|
|
145
148
|
}
|
|
146
|
-
function focusPrevious() {
|
|
149
|
+
function focusPrevious(intent) {
|
|
147
150
|
items = getItems();
|
|
148
151
|
if (items.length === 0)
|
|
149
152
|
return;
|
|
@@ -158,19 +161,19 @@ export function createKeyboardNavigation(options = {}) {
|
|
|
158
161
|
else {
|
|
159
162
|
prevIdx = Math.max(currentIdx - 1, 0);
|
|
160
163
|
}
|
|
161
|
-
focusItem(items[prevIdx]);
|
|
164
|
+
focusItem(items[prevIdx], intent);
|
|
162
165
|
}
|
|
163
|
-
function focusFirst() {
|
|
166
|
+
function focusFirst(intent) {
|
|
164
167
|
items = getItems();
|
|
165
168
|
if (items.length === 0)
|
|
166
169
|
return;
|
|
167
|
-
focusItem(items[0]);
|
|
170
|
+
focusItem(items[0], intent);
|
|
168
171
|
}
|
|
169
|
-
function focusLast() {
|
|
172
|
+
function focusLast(intent) {
|
|
170
173
|
items = getItems();
|
|
171
174
|
if (items.length === 0)
|
|
172
175
|
return;
|
|
173
|
-
focusItem(items[items.length - 1]);
|
|
176
|
+
focusItem(items[items.length - 1], intent);
|
|
174
177
|
}
|
|
175
178
|
function focusById(id) {
|
|
176
179
|
items = getItems();
|
|
@@ -248,14 +251,18 @@ export function createKeyboardNavigation(options = {}) {
|
|
|
248
251
|
: orientation === 'vertical'
|
|
249
252
|
? ['ArrowUp']
|
|
250
253
|
: ['ArrowUp', 'ArrowLeft'];
|
|
254
|
+
// Shift turns a move into an extension of the selection from the anchor — the
|
|
255
|
+
// multi-select listbox pattern. The move itself is unchanged; only consumers that
|
|
256
|
+
// read the intent do anything extra with it.
|
|
257
|
+
const intent = event.shiftKey ? { extend: true } : undefined;
|
|
251
258
|
if (nextKeys.includes(key)) {
|
|
252
259
|
event.preventDefault();
|
|
253
|
-
focusNext();
|
|
260
|
+
focusNext(intent);
|
|
254
261
|
return;
|
|
255
262
|
}
|
|
256
263
|
if (prevKeys.includes(key)) {
|
|
257
264
|
event.preventDefault();
|
|
258
|
-
focusPrevious();
|
|
265
|
+
focusPrevious(intent);
|
|
259
266
|
return;
|
|
260
267
|
}
|
|
261
268
|
// NOTE: key repeat is only suppressed for keys this handler actually consumes
|
|
@@ -265,12 +272,12 @@ export function createKeyboardNavigation(options = {}) {
|
|
|
265
272
|
if (homeEndKeys) {
|
|
266
273
|
if (key === 'Home') {
|
|
267
274
|
event.preventDefault();
|
|
268
|
-
focusFirst();
|
|
275
|
+
focusFirst(intent);
|
|
269
276
|
return;
|
|
270
277
|
}
|
|
271
278
|
if (key === 'End') {
|
|
272
279
|
event.preventDefault();
|
|
273
|
-
focusLast();
|
|
280
|
+
focusLast(intent);
|
|
274
281
|
return;
|
|
275
282
|
}
|
|
276
283
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Long press on a touch or pen pointer, for surfaces that expose an alternative
|
|
3
|
+
* action the way a right click does on a mouse.
|
|
4
|
+
*
|
|
5
|
+
* It exists because touch has no `contextmenu` worth relying on: Android Chrome
|
|
6
|
+
* fires it, iOS Safari only does so for a few built-in element types, so a
|
|
7
|
+
* context menu that waits for that event is unreachable on a phone.
|
|
8
|
+
*
|
|
9
|
+
* Two details are what make it survive contact with a real device:
|
|
10
|
+
*
|
|
11
|
+
* 1. The press is abandoned as soon as the finger travels — a long press and the
|
|
12
|
+
* start of a scroll look identical for the first few hundred milliseconds, and
|
|
13
|
+
* the one thing worse than no menu is a menu that opens whenever the page is
|
|
14
|
+
* slow to scroll. Nothing captures the pointer either: capturing it would take
|
|
15
|
+
* the scroll away from the browser for the whole delay.
|
|
16
|
+
* 2. The `click` and `contextmenu` that the platform emits around the same
|
|
17
|
+
* gesture are swallowed afterwards. Without that, the same finger press opens
|
|
18
|
+
* the menu and then immediately dismisses it.
|
|
19
|
+
*/
|
|
20
|
+
export type LongPressPoint = {
|
|
21
|
+
x: number;
|
|
22
|
+
y: number;
|
|
23
|
+
};
|
|
24
|
+
/** How long the pointer has to stay down. Matches the platform convention. */
|
|
25
|
+
export declare const LONG_PRESS_DELAY_MS = 500;
|
|
26
|
+
/** How far the pointer may drift before the press is treated as a drag/scroll. */
|
|
27
|
+
export declare const LONG_PRESS_MOVE_TOLERANCE_PX = 10;
|
|
28
|
+
/** Whether the pointer moved far enough from where it went down to abandon the press. */
|
|
29
|
+
export declare function hasMovedBeyond(start: LongPressPoint, current: LongPressPoint, tolerance: number): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Whether this pointer should be considered for a long press at all.
|
|
32
|
+
*
|
|
33
|
+
* A mouse is excluded on purpose: it already has `contextmenu`, and treating a
|
|
34
|
+
* held left button as a long press would fire on every slow drag.
|
|
35
|
+
*/
|
|
36
|
+
export declare function isLongPressPointer(event: PointerEvent): boolean;
|
|
37
|
+
export type LongPressOptions = {
|
|
38
|
+
/** Whether the action listens at all. */
|
|
39
|
+
enabled?: boolean;
|
|
40
|
+
/** How long the pointer must stay down. Defaults to `LONG_PRESS_DELAY_MS`. */
|
|
41
|
+
delay?: number;
|
|
42
|
+
/** Movement tolerance in px. Defaults to `LONG_PRESS_MOVE_TOLERANCE_PX`. */
|
|
43
|
+
tolerance?: number;
|
|
44
|
+
/** Called once the press is held long enough, with the viewport point of the pointer. */
|
|
45
|
+
onLongPress: (point: LongPressPoint, event: PointerEvent) => void;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Svelte action: calls `onLongPress` when a touch or pen pointer is held on the
|
|
49
|
+
* node without travelling.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```svelte
|
|
53
|
+
* <div use:longPress={{ onLongPress: (point) => openAt(point) }}>…</div>
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function longPress(node: HTMLElement, options: LongPressOptions): {
|
|
57
|
+
update(newOptions: LongPressOptions): void;
|
|
58
|
+
destroy(): void;
|
|
59
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Long press on a touch or pen pointer, for surfaces that expose an alternative
|
|
3
|
+
* action the way a right click does on a mouse.
|
|
4
|
+
*
|
|
5
|
+
* It exists because touch has no `contextmenu` worth relying on: Android Chrome
|
|
6
|
+
* fires it, iOS Safari only does so for a few built-in element types, so a
|
|
7
|
+
* context menu that waits for that event is unreachable on a phone.
|
|
8
|
+
*
|
|
9
|
+
* Two details are what make it survive contact with a real device:
|
|
10
|
+
*
|
|
11
|
+
* 1. The press is abandoned as soon as the finger travels — a long press and the
|
|
12
|
+
* start of a scroll look identical for the first few hundred milliseconds, and
|
|
13
|
+
* the one thing worse than no menu is a menu that opens whenever the page is
|
|
14
|
+
* slow to scroll. Nothing captures the pointer either: capturing it would take
|
|
15
|
+
* the scroll away from the browser for the whole delay.
|
|
16
|
+
* 2. The `click` and `contextmenu` that the platform emits around the same
|
|
17
|
+
* gesture are swallowed afterwards. Without that, the same finger press opens
|
|
18
|
+
* the menu and then immediately dismisses it.
|
|
19
|
+
*/
|
|
20
|
+
/** How long the pointer has to stay down. Matches the platform convention. */
|
|
21
|
+
export const LONG_PRESS_DELAY_MS = 500;
|
|
22
|
+
/** How far the pointer may drift before the press is treated as a drag/scroll. */
|
|
23
|
+
export const LONG_PRESS_MOVE_TOLERANCE_PX = 10;
|
|
24
|
+
/**
|
|
25
|
+
* How long the synthetic events that follow a long press are swallowed. Only has
|
|
26
|
+
* to outlive the platform's own emulation, which is immediate in practice; the
|
|
27
|
+
* timeout is there so a gesture that never produces them doesn't leave the
|
|
28
|
+
* listeners armed.
|
|
29
|
+
*/
|
|
30
|
+
const SUPPRESSION_WINDOW_MS = 700;
|
|
31
|
+
/** Whether the pointer moved far enough from where it went down to abandon the press. */
|
|
32
|
+
export function hasMovedBeyond(start, current, tolerance) {
|
|
33
|
+
return Math.hypot(current.x - start.x, current.y - start.y) > tolerance;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Whether this pointer should be considered for a long press at all.
|
|
37
|
+
*
|
|
38
|
+
* A mouse is excluded on purpose: it already has `contextmenu`, and treating a
|
|
39
|
+
* held left button as a long press would fire on every slow drag.
|
|
40
|
+
*/
|
|
41
|
+
export function isLongPressPointer(event) {
|
|
42
|
+
return event.isPrimary && event.button === 0 && event.pointerType !== 'mouse';
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Svelte action: calls `onLongPress` when a touch or pen pointer is held on the
|
|
46
|
+
* node without travelling.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```svelte
|
|
50
|
+
* <div use:longPress={{ onLongPress: (point) => openAt(point) }}>…</div>
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function longPress(node, options) {
|
|
54
|
+
let currentOptions = options;
|
|
55
|
+
let timer = null;
|
|
56
|
+
let start = null;
|
|
57
|
+
let pointerId = null;
|
|
58
|
+
let releaseSuppression = null;
|
|
59
|
+
function isEnabled() {
|
|
60
|
+
return currentOptions.enabled ?? true;
|
|
61
|
+
}
|
|
62
|
+
function clearTimer() {
|
|
63
|
+
if (timer === null)
|
|
64
|
+
return;
|
|
65
|
+
clearTimeout(timer);
|
|
66
|
+
timer = null;
|
|
67
|
+
}
|
|
68
|
+
function stopTracking() {
|
|
69
|
+
clearTimer();
|
|
70
|
+
start = null;
|
|
71
|
+
pointerId = null;
|
|
72
|
+
window.removeEventListener('pointermove', handlePointerMove);
|
|
73
|
+
window.removeEventListener('pointerup', stopTracking);
|
|
74
|
+
window.removeEventListener('pointercancel', stopTracking);
|
|
75
|
+
// Capture, so a scroll inside any container — not just the page — abandons the press.
|
|
76
|
+
window.removeEventListener('scroll', stopTracking, true);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Swallows the `click` / `contextmenu` the platform emits for the same gesture.
|
|
80
|
+
* Registered in the capture phase so they never reach the node's own handlers.
|
|
81
|
+
*/
|
|
82
|
+
function suppressFollowingEvents() {
|
|
83
|
+
releaseSuppression?.();
|
|
84
|
+
const suppress = (event) => {
|
|
85
|
+
event.preventDefault();
|
|
86
|
+
event.stopPropagation();
|
|
87
|
+
release();
|
|
88
|
+
};
|
|
89
|
+
function release() {
|
|
90
|
+
clearTimeout(expiry);
|
|
91
|
+
releaseSuppression = null;
|
|
92
|
+
window.removeEventListener('click', suppress, true);
|
|
93
|
+
window.removeEventListener('contextmenu', suppress, true);
|
|
94
|
+
}
|
|
95
|
+
window.addEventListener('click', suppress, true);
|
|
96
|
+
window.addEventListener('contextmenu', suppress, true);
|
|
97
|
+
const expiry = setTimeout(release, SUPPRESSION_WINDOW_MS);
|
|
98
|
+
// Held so `destroy` can drop it: these listeners are on the window, and a
|
|
99
|
+
// surface that goes away mid-gesture must not keep swallowing the page's events.
|
|
100
|
+
releaseSuppression = release;
|
|
101
|
+
}
|
|
102
|
+
function handlePointerMove(event) {
|
|
103
|
+
if (start === null || event.pointerId !== pointerId)
|
|
104
|
+
return;
|
|
105
|
+
const tolerance = currentOptions.tolerance ?? LONG_PRESS_MOVE_TOLERANCE_PX;
|
|
106
|
+
if (hasMovedBeyond(start, { x: event.clientX, y: event.clientY }, tolerance)) {
|
|
107
|
+
stopTracking();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function handlePointerDown(event) {
|
|
111
|
+
if (!isEnabled() || !isLongPressPointer(event))
|
|
112
|
+
return;
|
|
113
|
+
stopTracking();
|
|
114
|
+
start = { x: event.clientX, y: event.clientY };
|
|
115
|
+
pointerId = event.pointerId;
|
|
116
|
+
window.addEventListener('pointermove', handlePointerMove);
|
|
117
|
+
window.addEventListener('pointerup', stopTracking);
|
|
118
|
+
window.addEventListener('pointercancel', stopTracking);
|
|
119
|
+
window.addEventListener('scroll', stopTracking, true);
|
|
120
|
+
timer = setTimeout(() => {
|
|
121
|
+
const point = start;
|
|
122
|
+
stopTracking();
|
|
123
|
+
if (point === null)
|
|
124
|
+
return;
|
|
125
|
+
suppressFollowingEvents();
|
|
126
|
+
currentOptions.onLongPress(point, event);
|
|
127
|
+
}, currentOptions.delay ?? LONG_PRESS_DELAY_MS);
|
|
128
|
+
}
|
|
129
|
+
node.addEventListener('pointerdown', handlePointerDown);
|
|
130
|
+
return {
|
|
131
|
+
update(newOptions) {
|
|
132
|
+
currentOptions = newOptions;
|
|
133
|
+
if (!isEnabled())
|
|
134
|
+
stopTracking();
|
|
135
|
+
},
|
|
136
|
+
destroy() {
|
|
137
|
+
stopTracking();
|
|
138
|
+
releaseSuppression?.();
|
|
139
|
+
node.removeEventListener('pointerdown', handlePointerDown);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
@@ -37,3 +37,27 @@ export declare function scrollLock(node: HTMLElement, enabled?: boolean): {
|
|
|
37
37
|
update(newEnabled: boolean): void;
|
|
38
38
|
destroy(): void;
|
|
39
39
|
};
|
|
40
|
+
/**
|
|
41
|
+
* Svelte action that keeps a node scrollable while *someone else* holds the lock, without
|
|
42
|
+
* locking anything itself.
|
|
43
|
+
*
|
|
44
|
+
* A non-modal overlay — a combobox listbox, a select menu — locks nothing, so it never
|
|
45
|
+
* registers as a live scroll region. That is fine on its own, and broken inside a modal:
|
|
46
|
+
* the dialog's lock cancels every wheel event outside its own node, and the listbox is
|
|
47
|
+
* portalled to the body rather than nested in the dialog. The list then renders with a
|
|
48
|
+
* scrollbar it refuses to move.
|
|
49
|
+
*
|
|
50
|
+
* Registering the node makes the existing "is this scroll inside an overlay?" check say
|
|
51
|
+
* yes for it too. The lock count is untouched, so this can never keep the page frozen.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```svelte
|
|
55
|
+
* <div use:allowScrollWithin={isOpen}>
|
|
56
|
+
* Listbox portalled out of the dialog
|
|
57
|
+
* </div>
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function allowScrollWithin(node: HTMLElement, enabled?: boolean): {
|
|
61
|
+
update(newEnabled: boolean): void;
|
|
62
|
+
destroy(): void;
|
|
63
|
+
};
|
|
@@ -211,3 +211,43 @@ export function scrollLock(node, enabled = true) {
|
|
|
211
211
|
}
|
|
212
212
|
};
|
|
213
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Svelte action that keeps a node scrollable while *someone else* holds the lock, without
|
|
216
|
+
* locking anything itself.
|
|
217
|
+
*
|
|
218
|
+
* A non-modal overlay — a combobox listbox, a select menu — locks nothing, so it never
|
|
219
|
+
* registers as a live scroll region. That is fine on its own, and broken inside a modal:
|
|
220
|
+
* the dialog's lock cancels every wheel event outside its own node, and the listbox is
|
|
221
|
+
* portalled to the body rather than nested in the dialog. The list then renders with a
|
|
222
|
+
* scrollbar it refuses to move.
|
|
223
|
+
*
|
|
224
|
+
* Registering the node makes the existing "is this scroll inside an overlay?" check say
|
|
225
|
+
* yes for it too. The lock count is untouched, so this can never keep the page frozen.
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* ```svelte
|
|
229
|
+
* <div use:allowScrollWithin={isOpen}>
|
|
230
|
+
* Listbox portalled out of the dialog
|
|
231
|
+
* </div>
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
export function allowScrollWithin(node, enabled = true) {
|
|
235
|
+
const register = (on) => {
|
|
236
|
+
if (on) {
|
|
237
|
+
overlayNodes.add(node);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
overlayNodes.delete(node);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
register(enabled);
|
|
244
|
+
return {
|
|
245
|
+
update(newEnabled) {
|
|
246
|
+
register(newEnabled);
|
|
247
|
+
enabled = newEnabled;
|
|
248
|
+
},
|
|
249
|
+
destroy() {
|
|
250
|
+
register(false);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
}
|
|
@@ -19,6 +19,12 @@
|
|
|
19
19
|
## Usage guidelines
|
|
20
20
|
|
|
21
21
|
- Use `value` / `defaultValue` arrays for both single and multiple selection.
|
|
22
|
+
- Use `bind:value` to hand the group your state, or plain `value` + `onChange` to own it
|
|
23
|
+
yourself. There is no flag to declare which: supplying `value` makes it the source of
|
|
24
|
+
truth either way, and `onChange` always reports.
|
|
25
|
+
- A parent that owns `value` and rejects a change — never flowing a new one down — still
|
|
26
|
+
sees the group move, and the group snaps back to the supplied `value` on the parent's
|
|
27
|
+
next render. Reject by rendering, not by staying silent.
|
|
22
28
|
- Use `selectionMode="single"` when only one toggle can be selected.
|
|
23
29
|
- Use `selectionMode="multiple"` when several toggles can be selected.
|
|
24
30
|
- Use `disallowEmptySelection` when at least one enabled toggle must remain selected.
|
|
@@ -48,4 +54,8 @@
|
|
|
48
54
|
## Notes
|
|
49
55
|
|
|
50
56
|
- Grouped toggles ignore their standalone `selected` and `defaultSelected` props.
|
|
57
|
+
- Removing one `Toggle.Root` from a mounted group is a real selection change: with
|
|
58
|
+
`disallowEmptySelection` the group picks a fallback and fires `onChange`. Unmounting the
|
|
59
|
+
whole group is not, and reports nothing — otherwise leaving a screen would look like the
|
|
60
|
+
user pressing the last toggle on the way out.
|
|
51
61
|
- Form serialization is out of scope for this primitive.
|