@prson/login-sdk 0.0.0-stage → 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.
@@ -0,0 +1,426 @@
1
+ /**
2
+ * Virtual-cursor-friendly overlay for native `<select>` elements.
3
+ *
4
+ * Why this exists: native `<select>` dropdowns are rendered by the OS
5
+ * shell, not the DOM. Synthetic `click` events from `bindMouse`'s
6
+ * virtual cursor cannot open them, and even if they did the OS-drawn
7
+ * option list is outside the page's coordinate system — the virtual
8
+ * cursor couldn't navigate it. We replace the native dropdown with a
9
+ * pure-DOM overlay sized for cursor accuracy:
10
+ *
11
+ * * Each option is a large (≥56 px tall) button — easy to land on
12
+ * with the wand or trackpad.
13
+ * * "Previous page" / "Next page" buttons step through long option
14
+ * lists one viewport-sized chunk at a time.
15
+ * * An "X" button closes without committing a choice.
16
+ * * The overlay sits at the top of the body z-index stack and
17
+ * traps clicks: while it's open, the rest of the page is
18
+ * non-interactive — the cursor's click router only considers
19
+ * overlay elements.
20
+ * * The overlay is rendered inside a Shadow DOM root with
21
+ * `:host { all: initial; }` so the host page's stylesheets
22
+ * cannot bleed in. The page can neither restyle nor read the
23
+ * overlay (closed-mode shadow), beyond what we deliberately
24
+ * expose via attributes.
25
+ *
26
+ * Wiring from `bindMouse`:
27
+ *
28
+ * * Move frames re-route the snap target into the overlay via
29
+ * [findNearestClickableInOverlay] when the overlay is open.
30
+ * * Click frames detect `<select>` snap targets and open the
31
+ * overlay instead of dispatching a click. When the overlay is
32
+ * open, subsequent clicks route through it.
33
+ * * `bindMouse.dispose()` calls [closeAllOverlays] so a torn-down
34
+ * cursor doesn't leave the overlay orphaned.
35
+ */
36
+ let activeOverlay = null;
37
+ /** True when a select overlay is currently open + accepting input. */
38
+ export function isSelectOverlayActive() {
39
+ return activeOverlay !== null;
40
+ }
41
+ /** Close any currently-open overlay. No-op when none is open. */
42
+ export function closeAllOverlays() {
43
+ activeOverlay?.close();
44
+ }
45
+ /**
46
+ * Mount the overlay for [select]. Replaces any existing overlay.
47
+ * Returns the handle so callers can close it programmatically (e.g.
48
+ * if the cursor disposes while the overlay is still up). The
49
+ * overlay self-closes when the user picks an option or taps X.
50
+ */
51
+ export function openSelectOverlay(select) {
52
+ if (activeOverlay)
53
+ activeOverlay.close();
54
+ const host = document.createElement("div");
55
+ host.setAttribute("data-prson-select-overlay", "");
56
+ // Pin the host to the viewport. Inline styles win over any page
57
+ // CSS that might try to position `div` differently (Shadow DOM
58
+ // protects the inside; the host element itself is still in the
59
+ // light tree).
60
+ host.style.position = "fixed";
61
+ host.style.inset = "0";
62
+ // z-index sits just below the virtual cursor (2147483647) so the
63
+ // cursor still paints on top of the overlay.
64
+ host.style.zIndex = "2147483646";
65
+ document.body.appendChild(host);
66
+ const shadow = host.attachShadow({ mode: "open" });
67
+ const style = document.createElement("style");
68
+ style.textContent = OVERLAY_CSS;
69
+ shadow.appendChild(style);
70
+ // Title label: prefer the matching `<label>`, then aria-label,
71
+ // then name, then a generic string. Done once at open time so
72
+ // late `label` mutations don't change the title mid-life.
73
+ const title = pickTitle(select);
74
+ const backdrop = document.createElement("div");
75
+ backdrop.className = "backdrop";
76
+ backdrop.setAttribute("data-prson-select-backdrop", "");
77
+ const panel = document.createElement("div");
78
+ panel.className = "panel";
79
+ // ── header ────
80
+ const header = document.createElement("div");
81
+ header.className = "header";
82
+ const titleEl = document.createElement("div");
83
+ titleEl.className = "title";
84
+ titleEl.textContent = title;
85
+ const closeBtn = document.createElement("button");
86
+ closeBtn.className = "close";
87
+ closeBtn.setAttribute("type", "button");
88
+ closeBtn.setAttribute("data-prson-select-close", "");
89
+ closeBtn.setAttribute("aria-label", "Close");
90
+ closeBtn.textContent = "✕";
91
+ header.appendChild(titleEl);
92
+ header.appendChild(closeBtn);
93
+ panel.appendChild(header);
94
+ // ── pagination row + options ────
95
+ const optionsWrap = document.createElement("div");
96
+ optionsWrap.className = "options-wrap";
97
+ const pageUp = document.createElement("button");
98
+ pageUp.className = "page-btn";
99
+ pageUp.setAttribute("type", "button");
100
+ pageUp.setAttribute("data-prson-select-page", "up");
101
+ pageUp.innerHTML = '<span class="chev">▲</span> Previous';
102
+ const list = document.createElement("div");
103
+ list.className = "options-list";
104
+ const pageDown = document.createElement("button");
105
+ pageDown.className = "page-btn";
106
+ pageDown.setAttribute("type", "button");
107
+ pageDown.setAttribute("data-prson-select-page", "down");
108
+ pageDown.innerHTML = 'Next <span class="chev">▼</span>';
109
+ optionsWrap.appendChild(pageUp);
110
+ optionsWrap.appendChild(list);
111
+ optionsWrap.appendChild(pageDown);
112
+ panel.appendChild(optionsWrap);
113
+ backdrop.appendChild(panel);
114
+ shadow.appendChild(backdrop);
115
+ // ── options paging ────
116
+ const options = Array.from(select.options);
117
+ const PAGE_SIZE = 6;
118
+ const numPages = Math.max(1, Math.ceil(options.length / PAGE_SIZE));
119
+ let currentPage = pageOfValue(select.value, options, PAGE_SIZE);
120
+ function renderPage() {
121
+ list.replaceChildren();
122
+ const start = currentPage * PAGE_SIZE;
123
+ const end = Math.min(start + PAGE_SIZE, options.length);
124
+ for (let i = start; i < end; i++) {
125
+ const opt = options[i];
126
+ const btn = document.createElement("button");
127
+ btn.className = "option";
128
+ btn.setAttribute("type", "button");
129
+ btn.setAttribute("data-prson-select-option", String(i));
130
+ btn.textContent = opt.label || opt.value || `Option ${i + 1}`;
131
+ if (opt.value === select.value) {
132
+ btn.setAttribute("data-selected", "");
133
+ }
134
+ list.appendChild(btn);
135
+ }
136
+ // Toggle paging button state. Visible-but-disabled rather than
137
+ // removed so the layout stays stable (cursor doesn't have to
138
+ // re-find the button when there's more to page through).
139
+ if (currentPage <= 0) {
140
+ pageUp.setAttribute("disabled", "");
141
+ }
142
+ else {
143
+ pageUp.removeAttribute("disabled");
144
+ }
145
+ if (currentPage >= numPages - 1) {
146
+ pageDown.setAttribute("disabled", "");
147
+ }
148
+ else {
149
+ pageDown.removeAttribute("disabled");
150
+ }
151
+ }
152
+ renderPage();
153
+ const handle = {
154
+ host,
155
+ shadow,
156
+ close: () => {
157
+ host.remove();
158
+ if (activeOverlay === handle)
159
+ activeOverlay = null;
160
+ },
161
+ };
162
+ // Click handler — works for both real-finger taps AND synthetic
163
+ // events from the virtual cursor. `composedPath()` is required to
164
+ // reach into the shadow tree from the listener on `shadow` itself.
165
+ shadow.addEventListener("click", (e) => {
166
+ const path = e.composedPath();
167
+ for (const node of path) {
168
+ if (!(node instanceof Element))
169
+ continue;
170
+ // Close button.
171
+ if (node.getAttribute?.("data-prson-select-close") !== null) {
172
+ handle.close();
173
+ return;
174
+ }
175
+ // Page buttons.
176
+ const page = node.getAttribute?.("data-prson-select-page");
177
+ if (page === "up") {
178
+ if (currentPage > 0) {
179
+ currentPage -= 1;
180
+ renderPage();
181
+ }
182
+ return;
183
+ }
184
+ if (page === "down") {
185
+ if (currentPage < numPages - 1) {
186
+ currentPage += 1;
187
+ renderPage();
188
+ }
189
+ return;
190
+ }
191
+ // Option.
192
+ const optAttr = node.getAttribute?.("data-prson-select-option");
193
+ if (optAttr !== null && optAttr !== undefined) {
194
+ const idx = Number.parseInt(optAttr, 10);
195
+ const chosen = options[idx];
196
+ if (chosen) {
197
+ select.value = chosen.value;
198
+ select.dispatchEvent(new Event("input", { bubbles: true }));
199
+ select.dispatchEvent(new Event("change", { bubbles: true }));
200
+ }
201
+ handle.close();
202
+ return;
203
+ }
204
+ // Backdrop tap (anywhere outside the panel) closes too — the
205
+ // virtual cursor can't escape to it easily, but a real finger
206
+ // can.
207
+ if (node.getAttribute?.("data-prson-select-backdrop") !== null) {
208
+ handle.close();
209
+ return;
210
+ }
211
+ }
212
+ });
213
+ activeOverlay = handle;
214
+ return handle;
215
+ }
216
+ /**
217
+ * Snap-target lookup *inside* the active overlay. Bind.ts swaps
218
+ * the regular `findNearestClickable` for this when an overlay is
219
+ * open so the magic-mode highlight tracks the overlay's option
220
+ * buttons instead of the (no-longer-interactive) page underneath.
221
+ *
222
+ * No radius cap — when the overlay is open the cursor MUST land on
223
+ * something useful, so we always return a snap target if any
224
+ * candidate exists.
225
+ */
226
+ export function findNearestClickableInOverlay(x, y) {
227
+ if (!activeOverlay)
228
+ return null;
229
+ const candidates = activeOverlay.shadow.querySelectorAll("[data-prson-select-close]," +
230
+ "[data-prson-select-page]:not([disabled])," +
231
+ "[data-prson-select-option]");
232
+ let best = null;
233
+ let bestDist = Infinity;
234
+ for (const el of Array.from(candidates)) {
235
+ const rect = el.getBoundingClientRect();
236
+ if (rect.width === 0 || rect.height === 0)
237
+ continue;
238
+ const dx = x < rect.left ? rect.left - x : x > rect.right ? x - rect.right : 0;
239
+ const dy = y < rect.top ? rect.top - y : y > rect.bottom ? y - rect.bottom : 0;
240
+ const dist = Math.sqrt(dx * dx + dy * dy);
241
+ if (dist < bestDist) {
242
+ bestDist = dist;
243
+ best = el;
244
+ }
245
+ }
246
+ return best;
247
+ }
248
+ /**
249
+ * Highlight an element inside the active overlay by setting the
250
+ * shared `data-prson-magic-snap` attribute. The overlay's own CSS
251
+ * (inside the shadow) provides the visible style — the page's
252
+ * stylesheet for that attribute cannot reach in.
253
+ */
254
+ export function setOverlaySnap(target) {
255
+ if (!activeOverlay)
256
+ return;
257
+ activeOverlay.shadow
258
+ .querySelectorAll("[data-prson-magic-snap]")
259
+ .forEach((el) => el.removeAttribute("data-prson-magic-snap"));
260
+ if (target)
261
+ target.setAttribute("data-prson-magic-snap", "");
262
+ }
263
+ // ─── internals ─────────────────────────────────────────────────────
264
+ function pickTitle(select) {
265
+ // Prefer an explicit label[for=id] OR a wrapping <label>.
266
+ if (select.id) {
267
+ const lbl = document.querySelector(`label[for="${cssEscapeLocal(select.id)}"]`);
268
+ if (lbl?.textContent)
269
+ return lbl.textContent.trim();
270
+ }
271
+ const wrap = select.closest("label");
272
+ if (wrap && wrap.textContent) {
273
+ // Strip out the select's own rendered text from the label's
274
+ // text content; label.textContent concatenates everything.
275
+ const txt = wrap.textContent
276
+ .replace(select.textContent ?? "", "")
277
+ .trim();
278
+ if (txt)
279
+ return txt;
280
+ }
281
+ const aria = select.getAttribute("aria-label");
282
+ if (aria)
283
+ return aria;
284
+ const name = select.getAttribute("name");
285
+ if (name)
286
+ return name;
287
+ return "Select an option";
288
+ }
289
+ function pageOfValue(current, options, pageSize) {
290
+ const idx = options.findIndex((o) => o.value === current);
291
+ if (idx < 0)
292
+ return 0;
293
+ return Math.floor(idx / pageSize);
294
+ }
295
+ function cssEscapeLocal(s) {
296
+ if (typeof CSS !== "undefined" && typeof CSS.escape === "function") {
297
+ return CSS.escape(s);
298
+ }
299
+ return s.replace(/[^a-zA-Z0-9_-]/g, "\\$&");
300
+ }
301
+ // All overlay styling is in the shadow root, so it cannot collide
302
+ // with the host page's CSS and the host page cannot override it
303
+ // without explicitly piercing the shadow boundary. Sized for thumb
304
+ // + virtual-cursor accuracy: every interactive element is at least
305
+ // 56 px tall and 44 px wide.
306
+ const OVERLAY_CSS = `
307
+ :host {
308
+ all: initial;
309
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
310
+ color: #0f172a;
311
+ }
312
+ * { box-sizing: border-box; }
313
+ .backdrop {
314
+ position: fixed;
315
+ inset: 0;
316
+ background: rgba(15, 23, 42, 0.55);
317
+ display: flex;
318
+ flex-direction: column;
319
+ align-items: center;
320
+ justify-content: center;
321
+ padding: 24px;
322
+ }
323
+ .panel {
324
+ width: min(440px, 100%);
325
+ max-height: 88vh;
326
+ background: #ffffff;
327
+ border-radius: 20px;
328
+ overflow: hidden;
329
+ display: flex;
330
+ flex-direction: column;
331
+ box-shadow: 0 24px 56px rgba(15, 23, 42, 0.32);
332
+ }
333
+ .header {
334
+ display: flex;
335
+ align-items: center;
336
+ gap: 12px;
337
+ padding: 18px 20px;
338
+ border-bottom: 1px solid #e2e8f0;
339
+ }
340
+ .title {
341
+ flex: 1;
342
+ font-size: 18px;
343
+ font-weight: 700;
344
+ color: #0f172a;
345
+ overflow: hidden;
346
+ text-overflow: ellipsis;
347
+ white-space: nowrap;
348
+ }
349
+ .close {
350
+ width: 48px;
351
+ height: 48px;
352
+ border-radius: 50%;
353
+ border: 1px solid #e2e8f0;
354
+ background: #f1f5f9;
355
+ color: #475569;
356
+ font-size: 18px;
357
+ font-weight: 700;
358
+ cursor: pointer;
359
+ display: flex;
360
+ align-items: center;
361
+ justify-content: center;
362
+ }
363
+ .close:hover { background: #e2e8f0; }
364
+ .options-wrap {
365
+ display: flex;
366
+ flex-direction: column;
367
+ overflow: hidden;
368
+ }
369
+ .page-btn {
370
+ width: 100%;
371
+ padding: 18px 20px;
372
+ border: none;
373
+ background: #f8fafc;
374
+ color: #475569;
375
+ font-size: 15px;
376
+ font-weight: 600;
377
+ cursor: pointer;
378
+ border-bottom: 1px solid #e2e8f0;
379
+ display: flex;
380
+ align-items: center;
381
+ justify-content: center;
382
+ gap: 10px;
383
+ }
384
+ .page-btn .chev { font-size: 13px; opacity: 0.8; }
385
+ .page-btn:hover { background: #f1f5f9; color: #1e293b; }
386
+ .page-btn[disabled] {
387
+ opacity: 0.35;
388
+ cursor: default;
389
+ background: #f8fafc;
390
+ color: #475569;
391
+ }
392
+ .options-list {
393
+ display: flex;
394
+ flex-direction: column;
395
+ overflow-y: auto;
396
+ }
397
+ .option {
398
+ appearance: none;
399
+ width: 100%;
400
+ padding: 18px 20px;
401
+ border: none;
402
+ background: #ffffff;
403
+ color: #0f172a;
404
+ font-size: 16px;
405
+ font-weight: 500;
406
+ text-align: left;
407
+ cursor: pointer;
408
+ border-bottom: 1px solid #f1f5f9;
409
+ min-height: 60px;
410
+ }
411
+ .option:hover { background: #eff6ff; color: #1d4ed8; }
412
+ .option[data-selected] {
413
+ background: #dbeafe;
414
+ color: #1d4ed8;
415
+ font-weight: 700;
416
+ }
417
+ /* Snap-preview highlight, shared attribute name with the cursor.
418
+ Lives inside the shadow root so the page's selector (if any)
419
+ can't override it. */
420
+ [data-prson-magic-snap] {
421
+ outline: 3px solid #3b82f6;
422
+ outline-offset: -3px;
423
+ background: #eff6ff;
424
+ }
425
+ .option[data-prson-magic-snap] { color: #1d4ed8; }
426
+ `;