@rcarls/rc-common 0.1.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 ADDED
@@ -0,0 +1,206 @@
1
+ # `@rcarls/rc-common`
2
+
3
+ Shared controllers and directives for the `@rcarls` web components library. Not a component itself — a set of composable Lit primitives for drag, resize, anchor positioning, keyboard navigation, and focusability testing.
4
+
5
+ ---
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @rcarls/rc-common
11
+ ```
12
+
13
+ ## Import
14
+
15
+ ```js
16
+ import { DragController, ResizeController, AnchorController } from '@rcarls/rc-common';
17
+ import { keyNavigation, mouseMove } from '@rcarls/rc-common';
18
+ import { isFocusable, type FocusableElement } from '@rcarls/rc-common';
19
+ ```
20
+
21
+ All exports are tree-shakeable (`sideEffects: false`).
22
+
23
+ ---
24
+
25
+ ## API
26
+
27
+ ### `DragController`
28
+
29
+ A `ReactiveController` that adds pointer-driven drag-to-move to any `LitElement` host. Binds to a configurable handle element and constrains movement to viewport, parent, or a custom bounding element.
30
+
31
+ ```ts
32
+ import { LitElement } from 'lit';
33
+ import { DragController } from '@rcarls/rc-common';
34
+
35
+ class MyPanel extends LitElement {
36
+ private _drag = new DragController(this, {
37
+ handle: () => this.shadowRoot!.querySelector('.titlebar')!,
38
+ bounds: 'viewport', // 'viewport' | 'parent' | HTMLElement
39
+ step: 4, // px per arrow-key press; Shift = 10×
40
+ });
41
+ }
42
+ ```
43
+
44
+ The controller pins explicit `top`/`left` on the host element the first time the user drags, clearing any centering transforms that would fight manual positioning. Pointer capture is used for reliable drag-end detection across shadow boundaries.
45
+
46
+ **Keyboard:** Arrow keys move by `step` px; Shift multiplies by 10.
47
+
48
+ **Options:**
49
+
50
+ | Option | Type | Default | Description |
51
+ |---|---|---|---|
52
+ | `handle` | `() => HTMLElement \| null` | host element | Drag handle element or getter |
53
+ | `bounds` | `'viewport' \| 'parent' \| HTMLElement` | `'viewport'` | Movement constraint boundary |
54
+ | `step` | `number` | `4` | Arrow-key step in px |
55
+
56
+ ---
57
+
58
+ ### `ResizeController`
59
+
60
+ A `ReactiveController` that adds pointer- and keyboard-driven resizing to any `LitElement` host. Detects cursor position relative to all 8 edges/corners and adjusts a configurable threshold band. Injects a small keyboard-accessible resize handle in the bottom-right corner.
61
+
62
+ ```ts
63
+ import { LitElement } from 'lit';
64
+ import { ResizeController } from '@rcarls/rc-common';
65
+
66
+ class MyPanel extends LitElement {
67
+ private _resize = new ResizeController(this, {
68
+ threshold: 8, // edge hit-test band in px (half inside, half outside)
69
+ step: 4, // px per arrow-key press; Shift = 10×
70
+ });
71
+ }
72
+ ```
73
+
74
+ Works alongside `DragController` — `ResizeController` registers a capture-phase listener to intercept pointer events before the drag handler.
75
+
76
+ **Options:**
77
+
78
+ | Option | Type | Default | Description |
79
+ |---|---|---|---|
80
+ | `threshold` | `number` | `8` | Edge detection band in px |
81
+ | `step` | `number` | `4` | Arrow-key step in px |
82
+
83
+ ---
84
+
85
+ ### `AnchorController`
86
+
87
+ A `ReactiveController` that positions a floating element relative to an anchor using [CSS Anchor Positioning](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_anchor_positioning) with automatic polyfill fallback via [`@oddbird/css-anchor-positioning`](https://github.com/oddbird/css-anchor-positioning).
88
+
89
+ ```ts
90
+ import { LitElement } from 'lit';
91
+ import { AnchorController } from '@rcarls/rc-common';
92
+
93
+ class MyTooltip extends LitElement {
94
+ private _anchor = new AnchorController(this, {
95
+ anchor: () => document.querySelector('#trigger')!,
96
+ floating: () => this.shadowRoot!.querySelector('#popup')!,
97
+ });
98
+ }
99
+ ```
100
+
101
+ Injects a `<style>` block into `<head>` for the anchor-name / position-anchor declarations. Supports `@position-try` fallback blocks for automatic flip behavior (native browsers only; polyfill does not support position-try).
102
+
103
+ **Options:**
104
+
105
+ | Option | Type | Description |
106
+ |---|---|---|
107
+ | `anchor` | `() => Element \| null` | The anchor element getter |
108
+ | `floating` | `() => Element \| null` | The floating element getter |
109
+ | `positionArea` | `string` | CSS `position-area` value (e.g. `'block-end span-inline'`) |
110
+
111
+ ---
112
+
113
+ ### `keyNavigation` directive
114
+
115
+ An `AsyncDirective` that adds WAI-ARIA keyboard navigation to any element part. Detects the element's `role` to determine the correct navigation axis and delegates action names to a callback.
116
+
117
+ ```ts
118
+ import { keyNavigation } from '@rcarls/rc-common';
119
+ import type { KeyboardNavigationAction } from '@rcarls/rc-common';
120
+
121
+ // Inside a Lit render() method:
122
+ html`
123
+ <div role="menu" ${keyNavigation(this._onNavigate, {
124
+ handleEscape: true,
125
+ handleActivate: true,
126
+ })}>
127
+ <slot></slot>
128
+ </div>
129
+ `;
130
+
131
+ private _onNavigate(action: KeyboardNavigationAction) {
132
+ switch (action) {
133
+ case 'next': this.focusNext(); break;
134
+ case 'prev': this.focusPrev(); break;
135
+ case 'start': this.focusFirst(); break;
136
+ case 'end': this.focusLast(); break;
137
+ case 'escape': this.close(); break;
138
+ case 'activate': this.activate(); break;
139
+ case 'open-to-first': this.open('first'); break;
140
+ case 'open-to-last': this.open('last'); break;
141
+ }
142
+ }
143
+ ```
144
+
145
+ Navigation axis is inferred automatically from the element's `role`:
146
+
147
+ | Role | Default axis |
148
+ |---|---|
149
+ | `menu`, `listbox`, `tree` | Vertical (Up/Down) |
150
+ | `menubar`, `toolbar`, `tablist` | Horizontal (Left/Right) |
151
+ | `grid`, `treegrid` | Both |
152
+
153
+ **Options:**
154
+
155
+ | Option | Type | Default | Description |
156
+ |---|---|---|---|
157
+ | `navigationAxis` | `'horizontal' \| 'vertical'` | auto from role | Override the navigation axis |
158
+ | `handleNavAxis` | `boolean` | `true` | Respond to arrow keys on the nav axis |
159
+ | `handleOpenAxis` | `boolean` | `false` | Respond to arrow keys on the perpendicular (open/close) axis |
160
+ | `handleEscape` | `boolean` | `false` | Fire `'escape'` action on Escape key |
161
+ | `handleActivate` | `boolean` | `false` | Fire `'activate'` action on Enter/Space |
162
+
163
+ ---
164
+
165
+ ### `mouseMove` directive
166
+
167
+ An `AsyncDirective` that fires a callback on pointer movement while a pointer drag is active. Designed for drag-to-resize handles that need continuous delta tracking while preserving the historical `mouseMove` export name.
168
+
169
+ ```ts
170
+ import { mouseMove } from '@rcarls/rc-common';
171
+
172
+ // Inside a Lit render() method:
173
+ html`
174
+ <div id="handle" ${mouseMove(this._onDrag)}></div>
175
+ `;
176
+
177
+ private _onDrag(e: MouseEvent) {
178
+ this._resize(e.movementX, e.movementY);
179
+ }
180
+ ```
181
+
182
+ Attaches `pointerdown` on the element, captures that pointer, then listens for `pointermove`, `pointerup`, `pointercancel`, and `lostpointercapture` on the same element for the active drag cycle. Focuses the handle element on pointerdown.
183
+
184
+ ---
185
+
186
+ ### `isFocusable`
187
+
188
+ A utility function that returns `true` if an element should participate in keyboard navigation. Checks native element types (`<button>`, `<input>`, `<a href>`, etc.) and the presence of a `tabindex` attribute. Disabled elements are always excluded.
189
+
190
+ ```ts
191
+ import { isFocusable, type FocusableElement } from '@rcarls/rc-common';
192
+
193
+ const items = [...slot.assignedElements()].filter(isFocusable);
194
+ ```
195
+
196
+ Does **not** check `tabindex >= 0` — rc-common components mutate tabindex values during roving-tabindex management, so any `tabindex` attribute presence is the correct signal.
197
+
198
+ ---
199
+
200
+ ## Browser support
201
+
202
+ | Feature | Requirement |
203
+ |---|---|
204
+ | Controllers and directives | Any browser supporting Web Components + Pointer Events |
205
+ | `AnchorController` (native) | Chrome 125+, Safari 18+, Firefox (in development) |
206
+ | `AnchorController` (polyfilled) | Chrome 80+, Firefox 85+, Safari 14+ |