@rcarls/rc-common 0.3.2 → 0.4.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 +106 -64
- package/dist/rc-common.js +603 -215
- package/dist/rc-common.umd.cjs +14 -14
- package/dist/types/DragController.d.ts +5 -12
- package/dist/types/DragGestureController.d.ts +110 -0
- package/dist/types/DragGestureController.test.d.ts +1 -0
- package/dist/types/NavigationIndicatorController.d.ts +33 -0
- package/dist/types/ResizeController.d.ts +71 -15
- package/dist/types/index.d.ts +3 -0
- package/dist/types/snapUtils.d.ts +10 -0
- package/dist/types/snapUtils.test.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,7 +15,12 @@ npm install @rcarls/rc-common
|
|
|
15
15
|
## Import
|
|
16
16
|
|
|
17
17
|
```js
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
AnchorController,
|
|
20
|
+
DragController,
|
|
21
|
+
DragGestureController,
|
|
22
|
+
ResizeController,
|
|
23
|
+
} from '@rcarls/rc-common';
|
|
19
24
|
import {
|
|
20
25
|
ActiveDescendantController,
|
|
21
26
|
MutationObserverController,
|
|
@@ -25,7 +30,10 @@ import {
|
|
|
25
30
|
} from '@rcarls/rc-common';
|
|
26
31
|
import { RovingTabIndexMixin, keyInteraction, keyNavigation, mouseMove } from '@rcarls/rc-common';
|
|
27
32
|
import {
|
|
33
|
+
findExtremeSnapIndex,
|
|
28
34
|
findNearestScrollAncestor,
|
|
35
|
+
findNearestSnapIndex,
|
|
36
|
+
findNextSnapIndex,
|
|
29
37
|
getDirectChild,
|
|
30
38
|
getDirectChildren,
|
|
31
39
|
isFocusable,
|
|
@@ -41,32 +49,48 @@ All exports are tree-shakeable (`sideEffects: false`).
|
|
|
41
49
|
|
|
42
50
|
Check this inventory before adding package-local interaction or DOM utility code.
|
|
43
51
|
|
|
44
|
-
| Export
|
|
45
|
-
|
|
46
|
-
| `ActiveDescendantController`
|
|
47
|
-
| `AnchorController`
|
|
48
|
-
| `DragController`
|
|
49
|
-
| `
|
|
50
|
-
| `
|
|
51
|
-
| `
|
|
52
|
-
| `
|
|
53
|
-
| `
|
|
54
|
-
| `
|
|
55
|
-
| `
|
|
56
|
-
| `
|
|
57
|
-
| `
|
|
58
|
-
| `
|
|
59
|
-
| `
|
|
60
|
-
| `
|
|
61
|
-
| `
|
|
52
|
+
| Export | Use for |
|
|
53
|
+
| ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
|
|
54
|
+
| `ActiveDescendantController` | Managing `aria-activedescendant` virtual focus in listbox / combobox patterns |
|
|
55
|
+
| `AnchorController` | Positioning floating UI relative to an anchor with native CSS anchor positioning and polyfill fallback |
|
|
56
|
+
| `DragController` | Pointer and keyboard drag-to-move behavior |
|
|
57
|
+
| `DragGestureController` | Single-pointer activation, capture, deltas, duration, and recent velocity |
|
|
58
|
+
| `KeyboardInteractionDirective` / `keyInteraction` | Tracking pointer vs keyboard interaction mode on a rendered element |
|
|
59
|
+
| `KeyboardNavigationDirective` / `keyNavigation` | Mapping APG arrow-key models to navigation actions |
|
|
60
|
+
| `MutationObserverController` | Lifecycle-safe `MutationObserver` wiring for light-DOM or shadow-DOM changes |
|
|
61
|
+
| `MouseMoveDirective` / `mouseMove` | Pointermove callbacks while a pointer drag is active |
|
|
62
|
+
| `NativeChildController`, `getDirectChild`, `getDirectChildren`, `warnMissingDirectChild` | Finding, validating, and tracking consumer-provided native light-DOM children |
|
|
63
|
+
| `RafScheduler` | Coalescing repeated work into one animation frame and canceling on disconnect |
|
|
64
|
+
| `ResizeController` | Pointer and keyboard resize behavior |
|
|
65
|
+
| `RovingTabIndexMixin` | Roving tabindex for components whose child controls receive real DOM focus |
|
|
66
|
+
| `ScrollObserverController` | Scroll threshold and delta observation for scroll-driven component state |
|
|
67
|
+
| `TypeaheadController` | Printable-key buffering for composite widget typeahead |
|
|
68
|
+
| `findNearestScrollAncestor` | Locating the nearest scrollable ancestor for scroll-driven behavior |
|
|
69
|
+
| `findNearestSnapIndex`, `findNextSnapIndex`, `findExtremeSnapIndex` | Choosing numeric anchor indices without assigning component meaning |
|
|
70
|
+
| `isFocusable` | Filtering focusable items for keyboard navigation |
|
|
71
|
+
| `snapToStep`, `valueToPercent` | Slider and range-slider numeric helpers |
|
|
62
72
|
|
|
63
73
|
---
|
|
64
74
|
|
|
65
75
|
## API
|
|
66
76
|
|
|
77
|
+
### `DragGestureController`
|
|
78
|
+
|
|
79
|
+
A low-level `ReactiveController` for one primary pointer. Dedicated handles can
|
|
80
|
+
activate immediately, while scrollable surfaces can wait for axis intent and
|
|
81
|
+
reject movement dominated by the other axis. Lifecycle details include
|
|
82
|
+
coordinates, cumulative deltas, duration, pointer metadata, and velocity from a
|
|
83
|
+
100 ms recent-sample window.
|
|
84
|
+
|
|
85
|
+
The controller owns pointer capture and cleanup, but leaves state changes,
|
|
86
|
+
thresholds, logical direction, snapping, and action confirmation to its
|
|
87
|
+
consumer.
|
|
88
|
+
|
|
67
89
|
### `DragController`
|
|
68
90
|
|
|
69
|
-
A `ReactiveController` that adds pointer-driven drag-to-move to any `LitElement` host.
|
|
91
|
+
A `ReactiveController` that adds pointer-driven drag-to-move to any `LitElement` host. It binds
|
|
92
|
+
to a configurable handle and constrains movement to the viewport, parent, or a custom bounding
|
|
93
|
+
element.
|
|
70
94
|
|
|
71
95
|
```ts
|
|
72
96
|
import { LitElement } from 'lit';
|
|
@@ -75,29 +99,33 @@ import { DragController } from '@rcarls/rc-common';
|
|
|
75
99
|
class MyPanel extends LitElement {
|
|
76
100
|
private _drag = new DragController(this, {
|
|
77
101
|
handle: () => this.shadowRoot!.querySelector('.titlebar')!,
|
|
78
|
-
bounds: 'viewport',
|
|
79
|
-
step: 4,
|
|
102
|
+
bounds: 'viewport', // 'viewport' | 'parent' | HTMLElement
|
|
103
|
+
step: 4, // px per arrow-key press; Shift = 10×
|
|
80
104
|
});
|
|
81
105
|
}
|
|
82
106
|
```
|
|
83
107
|
|
|
84
|
-
The controller pins explicit `top
|
|
108
|
+
The controller pins explicit `top` and `left` values on the host the first time the user drags,
|
|
109
|
+
clearing centering transforms that would fight manual positioning. Pointer capture provides
|
|
110
|
+
reliable drag-end detection across shadow boundaries.
|
|
85
111
|
|
|
86
112
|
**Keyboard:** Arrow keys move by `step` px; Shift multiplies by 10.
|
|
87
113
|
|
|
88
114
|
**Options:**
|
|
89
115
|
|
|
90
|
-
| Option
|
|
91
|
-
|
|
92
|
-
| `handle` | `() => HTMLElement \| null`
|
|
93
|
-
| `bounds` | `'viewport' \| 'parent' \| HTMLElement` | `'viewport'` | Movement constraint boundary
|
|
94
|
-
| `step`
|
|
116
|
+
| Option | Type | Default | Description |
|
|
117
|
+
| -------- | --------------------------------------- | ------------ | ----------------------------- |
|
|
118
|
+
| `handle` | `() => HTMLElement \| null` | host element | Drag handle element or getter |
|
|
119
|
+
| `bounds` | `'viewport' \| 'parent' \| HTMLElement` | `'viewport'` | Movement constraint boundary |
|
|
120
|
+
| `step` | `number` | `4` | Arrow-key step in px |
|
|
95
121
|
|
|
96
122
|
---
|
|
97
123
|
|
|
98
124
|
### `ResizeController`
|
|
99
125
|
|
|
100
|
-
A `ReactiveController` that adds pointer- and keyboard-driven resizing to any `LitElement` host.
|
|
126
|
+
A `ReactiveController` that adds pointer- and keyboard-driven resizing to any `LitElement` host.
|
|
127
|
+
It detects the cursor relative to all eight edges and corners with a configurable threshold band.
|
|
128
|
+
It also injects a small keyboard-accessible handle in the bottom-right corner.
|
|
101
129
|
|
|
102
130
|
```ts
|
|
103
131
|
import { LitElement } from 'lit';
|
|
@@ -105,26 +133,30 @@ import { ResizeController } from '@rcarls/rc-common';
|
|
|
105
133
|
|
|
106
134
|
class MyPanel extends LitElement {
|
|
107
135
|
private _resize = new ResizeController(this, {
|
|
108
|
-
threshold: 8,
|
|
109
|
-
step: 4,
|
|
136
|
+
threshold: 8, // edge hit-test band in px (half inside, half outside)
|
|
137
|
+
step: 4, // px per arrow-key press; Shift = 10×
|
|
110
138
|
});
|
|
111
139
|
}
|
|
112
140
|
```
|
|
113
141
|
|
|
114
|
-
|
|
142
|
+
It works alongside `DragController`: `ResizeController` registers a capture-phase listener that
|
|
143
|
+
intercepts pointer events before the drag handler.
|
|
115
144
|
|
|
116
145
|
**Options:**
|
|
117
146
|
|
|
118
|
-
| Option
|
|
119
|
-
|
|
120
|
-
| `threshold` | `number` | `8`
|
|
121
|
-
| `step`
|
|
147
|
+
| Option | Type | Default | Description |
|
|
148
|
+
| ----------- | -------- | ------- | ------------------------- |
|
|
149
|
+
| `threshold` | `number` | `8` | Edge detection band in px |
|
|
150
|
+
| `step` | `number` | `4` | Arrow-key step in px |
|
|
122
151
|
|
|
123
152
|
---
|
|
124
153
|
|
|
125
154
|
### `AnchorController`
|
|
126
155
|
|
|
127
|
-
A `ReactiveController` that positions a floating element relative to an anchor using
|
|
156
|
+
A `ReactiveController` that positions a floating element relative to an anchor using
|
|
157
|
+
[CSS Anchor Positioning](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_anchor_positioning),
|
|
158
|
+
with automatic fallback through
|
|
159
|
+
[`@oddbird/css-anchor-positioning`](https://github.com/oddbird/css-anchor-positioning).
|
|
128
160
|
|
|
129
161
|
```ts
|
|
130
162
|
import { LitElement } from 'lit';
|
|
@@ -138,21 +170,24 @@ class MyTooltip extends LitElement {
|
|
|
138
170
|
}
|
|
139
171
|
```
|
|
140
172
|
|
|
141
|
-
|
|
173
|
+
It injects a `<style>` block into `<head>` for the `anchor-name` and `position-anchor`
|
|
174
|
+
declarations. It supports `@position-try` fallback blocks for automatic flip behavior in native
|
|
175
|
+
browsers; the polyfill does not support `position-try`.
|
|
142
176
|
|
|
143
177
|
**Options:**
|
|
144
178
|
|
|
145
|
-
| Option
|
|
146
|
-
|
|
147
|
-
| `anchor`
|
|
148
|
-
| `floating`
|
|
149
|
-
| `positionArea` | `string`
|
|
179
|
+
| Option | Type | Description |
|
|
180
|
+
| -------------- | ----------------------- | ---------------------------------------------------------- |
|
|
181
|
+
| `anchor` | `() => Element \| null` | The anchor element getter |
|
|
182
|
+
| `floating` | `() => Element \| null` | The floating element getter |
|
|
183
|
+
| `positionArea` | `string` | CSS `position-area` value (for example, `'block-end span-inline'`) |
|
|
150
184
|
|
|
151
185
|
---
|
|
152
186
|
|
|
153
187
|
### `keyNavigation` directive
|
|
154
188
|
|
|
155
|
-
An `AsyncDirective` that adds WAI-ARIA keyboard navigation to any element part.
|
|
189
|
+
An `AsyncDirective` that adds WAI-ARIA keyboard navigation to any element part. It detects the
|
|
190
|
+
element's `role` to choose the navigation axis and delegates action names to a callback.
|
|
156
191
|
|
|
157
192
|
```ts
|
|
158
193
|
import { keyNavigation } from '@rcarls/rc-common';
|
|
@@ -184,27 +219,29 @@ private _onNavigate(action: KeyboardNavigationAction) {
|
|
|
184
219
|
|
|
185
220
|
Navigation axis is inferred automatically from the element's `role`:
|
|
186
221
|
|
|
187
|
-
| Role
|
|
188
|
-
|
|
189
|
-
| `menu`, `listbox`, `tree`
|
|
222
|
+
| Role | Default axis |
|
|
223
|
+
| ------------------------------- | ----------------------- |
|
|
224
|
+
| `menu`, `listbox`, `tree` | Vertical (Up/Down) |
|
|
190
225
|
| `menubar`, `toolbar`, `tablist` | Horizontal (Left/Right) |
|
|
191
|
-
| `grid`, `treegrid`
|
|
226
|
+
| `grid`, `treegrid` | Both |
|
|
192
227
|
|
|
193
228
|
**Options:**
|
|
194
229
|
|
|
195
|
-
| Option
|
|
196
|
-
|
|
197
|
-
| `navigationAxis` | `'horizontal' \| 'vertical'` | auto from role | Override the navigation axis
|
|
198
|
-
| `handleNavAxis`
|
|
199
|
-
| `handleOpenAxis` | `boolean`
|
|
200
|
-
| `handleEscape`
|
|
201
|
-
| `handleActivate` | `boolean`
|
|
230
|
+
| Option | Type | Default | Description |
|
|
231
|
+
| ---------------- | ---------------------------- | -------------- | ------------------------------------------------------------ |
|
|
232
|
+
| `navigationAxis` | `'horizontal' \| 'vertical'` | auto from role | Override the navigation axis |
|
|
233
|
+
| `handleNavAxis` | `boolean` | `true` | Respond to arrow keys on the nav axis |
|
|
234
|
+
| `handleOpenAxis` | `boolean` | `false` | Respond to arrow keys on the perpendicular (open/close) axis |
|
|
235
|
+
| `handleEscape` | `boolean` | `false` | Fire `'escape'` action on Escape key |
|
|
236
|
+
| `handleActivate` | `boolean` | `false` | Fire `'activate'` action on Enter/Space |
|
|
202
237
|
|
|
203
238
|
---
|
|
204
239
|
|
|
205
240
|
### `mouseMove` directive
|
|
206
241
|
|
|
207
|
-
An `AsyncDirective` that fires a callback on pointer movement while a pointer drag is active.
|
|
242
|
+
An `AsyncDirective` that fires a callback on pointer movement while a pointer drag is active. It
|
|
243
|
+
supports drag-to-resize handles that need continuous delta tracking while preserving the
|
|
244
|
+
historical `mouseMove` export name.
|
|
208
245
|
|
|
209
246
|
```ts
|
|
210
247
|
import { mouseMove } from '@rcarls/rc-common';
|
|
@@ -219,13 +256,17 @@ private _onDrag(e: MouseEvent) {
|
|
|
219
256
|
}
|
|
220
257
|
```
|
|
221
258
|
|
|
222
|
-
|
|
259
|
+
It attaches `pointerdown` to the element, captures that pointer, then listens for `pointermove`,
|
|
260
|
+
`pointerup`, `pointercancel`, and `lostpointercapture` on the same element for the active drag
|
|
261
|
+
cycle. It focuses the handle on pointerdown.
|
|
223
262
|
|
|
224
263
|
---
|
|
225
264
|
|
|
226
265
|
### `isFocusable`
|
|
227
266
|
|
|
228
|
-
A utility function that returns `true` if an element should participate in keyboard navigation.
|
|
267
|
+
A utility function that returns `true` if an element should participate in keyboard navigation.
|
|
268
|
+
It checks native element types, including `<button>`, `<input>`, and `<a href>`, plus the presence
|
|
269
|
+
of a `tabindex` attribute. Disabled elements are always excluded.
|
|
229
270
|
|
|
230
271
|
```ts
|
|
231
272
|
import { isFocusable, type FocusableElement } from '@rcarls/rc-common';
|
|
@@ -233,14 +274,15 @@ import { isFocusable, type FocusableElement } from '@rcarls/rc-common';
|
|
|
233
274
|
const items = [...slot.assignedElements()].filter(isFocusable);
|
|
234
275
|
```
|
|
235
276
|
|
|
236
|
-
|
|
277
|
+
It does **not** check whether `tabindex` is at least zero because rc-common components mutate
|
|
278
|
+
tabindex values during roving-tabindex management. Any `tabindex` attribute is the correct signal.
|
|
237
279
|
|
|
238
280
|
---
|
|
239
281
|
|
|
240
282
|
## Browser support
|
|
241
283
|
|
|
242
|
-
| Feature
|
|
243
|
-
|
|
244
|
-
| Controllers and directives
|
|
245
|
-
| `AnchorController` (native)
|
|
246
|
-
| `AnchorController` (polyfilled) | Chrome 80+, Firefox 85+, Safari 14+
|
|
284
|
+
| Feature | Requirement |
|
|
285
|
+
| ------------------------------- | ------------------------------------------------------ |
|
|
286
|
+
| Controllers and directives | Any browser supporting Web Components + Pointer Events |
|
|
287
|
+
| `AnchorController` (native) | Chrome 125+, Safari 18+, Firefox (in development) |
|
|
288
|
+
| `AnchorController` (polyfilled) | Chrome 80+, Firefox 85+, Safari 14+ |
|