@jsenv/navi 0.29.404 → 0.29.407
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/dist/dev/jsenv_navi.js +2878 -5
- package/dist/dev/jsenv_navi.js.map +25 -2
- package/dist/dev/jsenv_navi_side_effects.js +14 -2845
- package/dist/dev/jsenv_navi_side_effects.js.map +3 -25
- package/dist/jsenv_navi.js +2790 -5
- package/dist/jsenv_navi.js.map +25 -2
- package/dist/jsenv_navi_side_effects.js +14 -2757
- package/dist/jsenv_navi_side_effects.js.map +3 -25
- package/docs/AI_INSTRUCTIONS.md +2 -2
- package/docs/safe_area.md +16 -12
- package/package.json +2 -2
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { getVirtualKeyboardOverlayHeight, subscribeWindowResizeSettled, subscribeVisualViewportResizeSettled,
|
|
2
|
-
import { CHAR_CLASS_PRESETS } from "@jsenv/validity";
|
|
1
|
+
import { getVirtualKeyboardOverlayHeight, subscribeWindowResizeSettled, subscribeVisualViewportResizeSettled, setPlacementViewportInsets } from "@jsenv/dom";
|
|
3
2
|
import { signal, computed, effect } from "@preact/signals";
|
|
4
3
|
|
|
5
4
|
const installImportMetaCssBuild = (importMeta) => {
|
|
@@ -322,6 +321,8 @@ const SAFE_AREA_CSS = /* css */ `@property --navi-app-inset-top {
|
|
|
322
321
|
--navi-fixed-bar-space-bottom: 0px;
|
|
323
322
|
--navi-fixed-bar-space-left: 0px;
|
|
324
323
|
--navi-keyboard-inset-bottom: env(keyboard-inset-height, 0px);
|
|
324
|
+
--navi-keyboard-strip-allowance: min(var(--navi-keyboard-inset-bottom),
|
|
325
|
+
64px);
|
|
325
326
|
--navi-app-inset-top: max(0px,
|
|
326
327
|
(var(--navi-vvh) - var(--navi-app-max-height, var(--navi-vvh))) / 2);
|
|
327
328
|
--navi-app-inset-bottom: calc(var(--navi-app-inset-top) + var(--navi-keyboard-inset-bottom));
|
|
@@ -338,23 +339,26 @@ const SAFE_AREA_CSS = /* css */ `@property --navi-app-inset-top {
|
|
|
338
339
|
max(env(safe-area-inset-left), var(--navi-fixed-bar-space-left)));
|
|
339
340
|
scroll-padding-top: var(--navi-safe-area-inset-top);
|
|
340
341
|
scroll-padding-right: var(--navi-safe-area-inset-right);
|
|
341
|
-
scroll-padding-bottom: var(--navi-safe-area-inset-bottom)
|
|
342
|
+
scroll-padding-bottom: calc(var(--navi-safe-area-inset-bottom) +
|
|
343
|
+
var(--navi-keyboard-strip-allowance));
|
|
342
344
|
scroll-padding-left: var(--navi-safe-area-inset-left);
|
|
343
345
|
}
|
|
344
346
|
|
|
345
347
|
[data-navi-safe-area] {
|
|
346
348
|
padding-top: var(--navi-safe-area-inset-top);
|
|
347
349
|
padding-right: var(--navi-safe-area-inset-right);
|
|
348
|
-
padding-bottom: var(--navi-safe-area-inset-bottom)
|
|
350
|
+
padding-bottom: calc(var(--navi-safe-area-inset-bottom) +
|
|
351
|
+
var(--navi-keyboard-strip-allowance));
|
|
349
352
|
padding-left: var(--navi-safe-area-inset-left);
|
|
350
353
|
scroll-padding-top: var(--navi-safe-area-inset-top);
|
|
351
354
|
scroll-padding-right: var(--navi-safe-area-inset-right);
|
|
352
|
-
scroll-padding-bottom: var(--navi-safe-area-inset-bottom)
|
|
355
|
+
scroll-padding-bottom: calc(var(--navi-safe-area-inset-bottom) +
|
|
356
|
+
var(--navi-keyboard-strip-allowance));
|
|
353
357
|
scroll-padding-left: var(--navi-safe-area-inset-left);
|
|
354
358
|
}
|
|
355
359
|
|
|
356
360
|
:root:not(:has([data-navi-safe-area])):after {
|
|
357
|
-
height: var(--navi-keyboard-inset-bottom);
|
|
361
|
+
height: calc(var(--navi-keyboard-inset-bottom) + var(--navi-keyboard-strip-allowance));
|
|
358
362
|
content: "";
|
|
359
363
|
display: block;
|
|
360
364
|
}
|
|
@@ -362,2841 +366,6 @@ const SAFE_AREA_CSS = /* css */ `@property --navi-app-inset-top {
|
|
|
362
366
|
`;
|
|
363
367
|
import.meta.css = [SAFE_AREA_CSS, "@jsenv/navi/src/layout/safe_area.js"];
|
|
364
368
|
|
|
365
|
-
/**
|
|
366
|
-
* DOM utilities for navigating the control element hierarchy.
|
|
367
|
-
*
|
|
368
|
-
* A control is a self-contained interactive widget. Its DOM structure can be
|
|
369
|
-
* either flat (host only) or layered (wrapper + host):
|
|
370
|
-
*
|
|
371
|
-
* Flat — the element is both the root and the host:
|
|
372
|
-
* ```html
|
|
373
|
-
* <button navi-control navi-control-host>Click me</button>
|
|
374
|
-
* ```
|
|
375
|
-
*
|
|
376
|
-
* Layered — a visual wrapper surrounds a native input that is the real host:
|
|
377
|
-
* ```html
|
|
378
|
-
* <span navi-control> ← wrapper: root of the control's DOM subtree
|
|
379
|
-
* <input navi-control-host /> ← host: holds controlProps, value, UI state, constraints
|
|
380
|
-
* </span>
|
|
381
|
-
* ```
|
|
382
|
-
*
|
|
383
|
-
* Attribute roles:
|
|
384
|
-
* - `navi-control` boolean, on the wrapper/root; marks the control boundary
|
|
385
|
-
* - `navi-control-host` boolean, on the host; set automatically by `useInteractiveProps`
|
|
386
|
-
*
|
|
387
|
-
* See control_proxy.js for the `navi-control-proxy-for` pattern.
|
|
388
|
-
*/
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* Returns the host element inside `el` — the element that holds the control's
|
|
392
|
-
* value, UI state, and constraints (i.e. the element onto which
|
|
393
|
-
* `useInteractiveProps` spreads `controlProps` and its event handlers).
|
|
394
|
-
*
|
|
395
|
-
* Returns `null` when `el` is itself the host (no separate wrapper).
|
|
396
|
-
*/
|
|
397
|
-
const findControlHost = (el) => {
|
|
398
|
-
if (el.hasAttribute("navi-control-host")) {
|
|
399
|
-
return el;
|
|
400
|
-
}
|
|
401
|
-
return el.querySelector("[navi-control-host]");
|
|
402
|
-
};
|
|
403
|
-
/**
|
|
404
|
-
* The control this element belongs to, wherever it sits relative to it.
|
|
405
|
-
*
|
|
406
|
-
* `findControlHost` above answers for a control's own DOM (itself, or the native
|
|
407
|
-
* element it wraps). This one is for something that is not part of a control but
|
|
408
|
-
* has to reach one — an interaction declared on a box (see
|
|
409
|
-
* interaction/interactions.js), which may be the control, may sit inside it, or
|
|
410
|
-
* may be a wrapper around exactly one. Nearest wins in that order, and the answer
|
|
411
|
-
* is null when there is no control the box belongs to: not every box lives in
|
|
412
|
-
* one, and a box laying out several controls (a row of badges, a toolbar) is
|
|
413
|
-
* held by none of them — its interactions are its own, and a press on its empty
|
|
414
|
-
* part is not a press on the first control it happens to contain.
|
|
415
|
-
*/
|
|
416
|
-
const findNearestControlHost = (el) => {
|
|
417
|
-
// Itself, then upwards — the box is inside a button, or is one.
|
|
418
|
-
const selfOrAncestor = el.closest("[navi-control-host]");
|
|
419
|
-
if (selfOrAncestor) {
|
|
420
|
-
return selfOrAncestor;
|
|
421
|
-
}
|
|
422
|
-
// …then downwards, only when the box wraps a single control. Counted by
|
|
423
|
-
// control, not by host: a picker holds the hosts of its popup content, and a
|
|
424
|
-
// box around that one picker is still around one control.
|
|
425
|
-
let single = null;
|
|
426
|
-
for (const host of el.querySelectorAll("[navi-control-host]")) {
|
|
427
|
-
const controlRoot = host.closest("[navi-control]") || host;
|
|
428
|
-
const controlAbove = controlRoot.parentElement.closest("[navi-control]");
|
|
429
|
-
if (controlAbove && el.contains(controlAbove)) {
|
|
430
|
-
continue;
|
|
431
|
-
}
|
|
432
|
-
if (single && single !== controlRoot) {
|
|
433
|
-
return null;
|
|
434
|
-
}
|
|
435
|
-
single = controlRoot;
|
|
436
|
-
}
|
|
437
|
-
return single ? findControlHost(single) : null;
|
|
438
|
-
};
|
|
439
|
-
const isControlRoot = (el) => {
|
|
440
|
-
return el.hasAttribute("navi-control");
|
|
441
|
-
};
|
|
442
|
-
const isControlHost = (el) => {
|
|
443
|
-
return el.hasAttribute("navi-control-host");
|
|
444
|
-
};
|
|
445
|
-
const isControl = (el) => {
|
|
446
|
-
return isControlRoot(el) || isControlHost(el);
|
|
447
|
-
};
|
|
448
|
-
|
|
449
|
-
/**
|
|
450
|
-
* Returns the nearest ancestor of `el` (exclusive of `el`'s own control) that
|
|
451
|
-
* has a `[data-action]` attribute.
|
|
452
|
-
*
|
|
453
|
-
* The search walks up `parentElement` manually (rather than using `.closest()`)
|
|
454
|
-
* so it can stop at hard boundaries.
|
|
455
|
-
*
|
|
456
|
-
* **`[navi-control="picker"]` boundary**: a picker is a hard stop for what is
|
|
457
|
-
* inside its popup. Elements in there can reach the picker itself, but nothing
|
|
458
|
-
* above it — an input inside a picker must not submit the form around it. The
|
|
459
|
-
* picker's own trigger is not inside it: a picker pressed as a whole (a confirm
|
|
460
|
-
* picker sending the form around it, see picker_confirm.jsx) stands in that
|
|
461
|
-
* form like any control there, and reaches it.
|
|
462
|
-
*
|
|
463
|
-
* ```html
|
|
464
|
-
* <form data-action="outer"> ← found from the picker's own input,
|
|
465
|
-
* <button navi-control="picker" data-action="p"> NOT from inside its popup
|
|
466
|
-
* <input navi-control-host /> ← el (the picker's own input)
|
|
467
|
-
* <div popover data-picker-content>
|
|
468
|
-
* <input navi-control-host /> ← el (in picker popover): stops at the picker
|
|
469
|
-
* </div>
|
|
470
|
-
* </button>
|
|
471
|
-
* </form>
|
|
472
|
-
* ```
|
|
473
|
-
*/
|
|
474
|
-
const findClosestControlWithAction = (el) => {
|
|
475
|
-
let current = el;
|
|
476
|
-
while (current) {
|
|
477
|
-
if (current.hasAttribute("data-action")) {
|
|
478
|
-
return current;
|
|
479
|
-
}
|
|
480
|
-
if (isPickerBoundaryFor(current, el)) {
|
|
481
|
-
return undefined;
|
|
482
|
-
}
|
|
483
|
-
current = current.parentElement;
|
|
484
|
-
}
|
|
485
|
-
return undefined;
|
|
486
|
-
};
|
|
487
|
-
const isPickerBoundaryFor = (candidate, el) => {
|
|
488
|
-
if (candidate.getAttribute("navi-control") !== "picker") {
|
|
489
|
-
return false;
|
|
490
|
-
}
|
|
491
|
-
const content = el.closest("[data-picker-content]");
|
|
492
|
-
return Boolean(content) && candidate.contains(content);
|
|
493
|
-
};
|
|
494
|
-
|
|
495
|
-
/**
|
|
496
|
-
* Returns the closest ancestor control element of `el` — i.e. the nearest
|
|
497
|
-
* `[navi-control]` element that is not the control `el` belongs to.
|
|
498
|
-
*
|
|
499
|
-
* `navi-control` is only on wrapper elements, never on hosts. So
|
|
500
|
-
* `el.closest("[navi-control]")` from a host returns that host's own wrapper,
|
|
501
|
-
* and one more `.parentNode.closest("[navi-control]")` reaches a true ancestor:
|
|
502
|
-
*
|
|
503
|
-
* ```html
|
|
504
|
-
* <button navi-control> ← outer control (returned)
|
|
505
|
-
* <span navi-control> ← inner wrapper (skipped via parentNode)
|
|
506
|
-
* <input navi-control-host /> ← el
|
|
507
|
-
* </span>
|
|
508
|
-
* </button>
|
|
509
|
-
* ```
|
|
510
|
-
*/
|
|
511
|
-
const getParentControl = (el) => {
|
|
512
|
-
const ownControlRoot = el.closest("[navi-control]");
|
|
513
|
-
const parentControlRoot = ownControlRoot.parentNode.closest("[navi-control]");
|
|
514
|
-
return parentControlRoot;
|
|
515
|
-
};
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
* Returns the root element of the control that `el` belongs to, or `null` if
|
|
519
|
-
* `el` is not part of a control.
|
|
520
|
-
*
|
|
521
|
-
* Use this when you have an element that may be a host (inner input) and need
|
|
522
|
-
* the visual boundary of its control — e.g. to anchor a callout, track
|
|
523
|
-
* mousedown interactions, or measure the control's bounding box.
|
|
524
|
-
*/
|
|
525
|
-
const findControlRoot = (el) => {
|
|
526
|
-
if (el.hasAttribute("navi-control")) {
|
|
527
|
-
return el;
|
|
528
|
-
}
|
|
529
|
-
if (el.hasAttribute("navi-control-host")) {
|
|
530
|
-
return el.closest("[navi-control]");
|
|
531
|
-
}
|
|
532
|
-
return null;
|
|
533
|
-
};
|
|
534
|
-
|
|
535
|
-
const isSignal = (value) => {
|
|
536
|
-
return getSignalType(value) !== null;
|
|
537
|
-
};
|
|
538
|
-
|
|
539
|
-
const BRAND_SYMBOL = Symbol.for("preact-signals");
|
|
540
|
-
const getSignalType = (value) => {
|
|
541
|
-
if (!value || typeof value !== "object") {
|
|
542
|
-
return null;
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
if (value.brand !== BRAND_SYMBOL) {
|
|
546
|
-
return null;
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
if (typeof value._fn === "function") {
|
|
550
|
-
return "computed";
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
return "signal";
|
|
554
|
-
};
|
|
555
|
-
|
|
556
|
-
/**
|
|
557
|
-
* Parses a time string into seconds.
|
|
558
|
-
* Accepts:
|
|
559
|
-
* - number: returned as-is (already in seconds)
|
|
560
|
-
* - "HH:MM" string: converted to seconds (e.g. "00:30" → 1800, "01:00" → 3600)
|
|
561
|
-
* - undefined/null: returned as-is
|
|
562
|
-
*/
|
|
563
|
-
const timeStringToSeconds = (timeString) => {
|
|
564
|
-
if (typeof timeString !== "string") {
|
|
565
|
-
return timeString;
|
|
566
|
-
}
|
|
567
|
-
const colonIndex = timeString.indexOf(":");
|
|
568
|
-
if (colonIndex === -1) {
|
|
569
|
-
return Number(timeString);
|
|
570
|
-
}
|
|
571
|
-
const hours = parseInt(timeString.slice(0, colonIndex), 10);
|
|
572
|
-
const minutes = parseInt(timeString.slice(colonIndex + 1), 10);
|
|
573
|
-
return (hours * 60 + minutes) * 60;
|
|
574
|
-
};
|
|
575
|
-
|
|
576
|
-
const isToday = (value) => {
|
|
577
|
-
if (!value) {
|
|
578
|
-
return false;
|
|
579
|
-
}
|
|
580
|
-
const now = new Date();
|
|
581
|
-
const todayStr = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}`;
|
|
582
|
-
if (typeof value === "string") {
|
|
583
|
-
return value === todayStr;
|
|
584
|
-
}
|
|
585
|
-
if (typeof value === "number") {
|
|
586
|
-
const d = new Date(value);
|
|
587
|
-
const s = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
|
588
|
-
return s === todayStr;
|
|
589
|
-
}
|
|
590
|
-
if (value instanceof Date) {
|
|
591
|
-
const s = `${value.getFullYear()}-${String(value.getMonth() + 1).padStart(2, "0")}-${String(value.getDate()).padStart(2, "0")}`;
|
|
592
|
-
return s === todayStr;
|
|
593
|
-
}
|
|
594
|
-
return false;
|
|
595
|
-
};
|
|
596
|
-
|
|
597
|
-
/**
|
|
598
|
-
* Returns the current time as "HH:MM", with an optional minute offset.
|
|
599
|
-
*
|
|
600
|
-
* @param {number} [offsetMinutes=0] - Minutes to add (negative = subtract).
|
|
601
|
-
* E.g. getNowHours(-5) returns "now minus 5 minutes".
|
|
602
|
-
*
|
|
603
|
-
* @example
|
|
604
|
-
* getNowHours() // "14:30"
|
|
605
|
-
* getNowHours(-5) // "14:25"
|
|
606
|
-
*/
|
|
607
|
-
const getNowHours = (offsetMinutes = 0) => {
|
|
608
|
-
const now = new Date();
|
|
609
|
-
const totalMinutes = now.getHours() * 60 + now.getMinutes() + offsetMinutes;
|
|
610
|
-
const clamped =
|
|
611
|
-
totalMinutes < 0
|
|
612
|
-
? 0
|
|
613
|
-
: totalMinutes > 23 * 60 + 59
|
|
614
|
-
? 23 * 60 + 59
|
|
615
|
-
: totalMinutes;
|
|
616
|
-
const h = Math.floor(clamped / 60);
|
|
617
|
-
const m = clamped % 60;
|
|
618
|
-
return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}`;
|
|
619
|
-
};
|
|
620
|
-
|
|
621
|
-
/**
|
|
622
|
-
* Returns the current time rounded up to the nearest step boundary,
|
|
623
|
-
* with an optional minute offset applied first.
|
|
624
|
-
*
|
|
625
|
-
* This is useful to compute a step-aligned `min` for a time picker:
|
|
626
|
-
* passing it ensures the first available slot is always on a step boundary.
|
|
627
|
-
*
|
|
628
|
-
* @param {number} stepMinutes - Step size in minutes (e.g. 30).
|
|
629
|
-
* @param {number} [offsetMinutes=0] - Minutes to add before rounding (negative = subtract).
|
|
630
|
-
*
|
|
631
|
-
* @example
|
|
632
|
-
* // At 9:32, step 30, offset -5 → raw = 9:27 → ceil to 30 → "09:30"
|
|
633
|
-
* // At 9:38, step 30, offset -5 → raw = 9:33 → ceil to 30 → "10:00"
|
|
634
|
-
* getNowHoursRoundedToStep(30, -5)
|
|
635
|
-
*/
|
|
636
|
-
const getNowHoursRoundedToStep = (stepMinutes, offsetMinutes = 0) => {
|
|
637
|
-
const now = new Date();
|
|
638
|
-
const totalMinutes = now.getHours() * 60 + now.getMinutes() + offsetMinutes;
|
|
639
|
-
const aligned = Math.ceil(totalMinutes / stepMinutes) * stepMinutes;
|
|
640
|
-
const clamped =
|
|
641
|
-
aligned < 0 ? 0 : aligned > 23 * 60 + 59 ? 23 * 60 + 59 : aligned;
|
|
642
|
-
const h = Math.floor(clamped / 60);
|
|
643
|
-
const m = clamped % 60;
|
|
644
|
-
return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}`;
|
|
645
|
-
};
|
|
646
|
-
|
|
647
|
-
/**
|
|
648
|
-
* "HH:MM" and its two numbers, in both directions — what any control made of an
|
|
649
|
-
* hour beside a minute (fields, wheels) aggregates to and is placed from. Held
|
|
650
|
-
* as numbers, written on two digits: how they are shown is each control's own
|
|
651
|
-
* business.
|
|
652
|
-
*/
|
|
653
|
-
const parseTimeParts = (time) => {
|
|
654
|
-
if (typeof time !== "string") {
|
|
655
|
-
return null;
|
|
656
|
-
}
|
|
657
|
-
const match = /^(\d{1,2}):(\d{1,2})/.exec(time);
|
|
658
|
-
if (!match) {
|
|
659
|
-
return null;
|
|
660
|
-
}
|
|
661
|
-
return { hour: Number(match[1]), minute: Number(match[2]) };
|
|
662
|
-
};
|
|
663
|
-
|
|
664
|
-
// Half a time is not a time: a control holding one of the two and nothing in
|
|
665
|
-
// the other has no value at all, and a form has nothing to send about it.
|
|
666
|
-
const formatTimeParts = (hour, minute) => {
|
|
667
|
-
if (
|
|
668
|
-
hour === "" ||
|
|
669
|
-
hour === undefined ||
|
|
670
|
-
minute === "" ||
|
|
671
|
-
minute === undefined
|
|
672
|
-
) {
|
|
673
|
-
return undefined;
|
|
674
|
-
}
|
|
675
|
-
return `${padTwo(hour)}:${padTwo(minute)}`;
|
|
676
|
-
};
|
|
677
|
-
|
|
678
|
-
const minutesFromTime = (time) => {
|
|
679
|
-
const parts = parseTimeParts(time);
|
|
680
|
-
if (!parts) {
|
|
681
|
-
return null;
|
|
682
|
-
}
|
|
683
|
-
return parts.hour * 60 + parts.minute;
|
|
684
|
-
};
|
|
685
|
-
|
|
686
|
-
// Not folded back into the day: 1440 is "24:00", the end of the day a span can
|
|
687
|
-
// run into.
|
|
688
|
-
const timeFromMinutes = (minutes) => {
|
|
689
|
-
return `${padTwo(Math.floor(minutes / 60))}:${padTwo(minutes % 60)}`;
|
|
690
|
-
};
|
|
691
|
-
|
|
692
|
-
const padTwo = (value) => String(value).padStart(2, "0");
|
|
693
|
-
|
|
694
|
-
// What a bound signal's type says is what its VALUE is (a validity type);
|
|
695
|
-
// what a control's `type` says is what the control IS. This table is the whole
|
|
696
|
-
// bridge between the two vocabularies: a value type with a row here asks for
|
|
697
|
-
// that control, one without a row asks for nothing and the control keeps its
|
|
698
|
-
// own default. "string" has no row on purpose — it is the value every typeless
|
|
699
|
-
// control already holds — and a name only one side knows must never cross as
|
|
700
|
-
// is: a control type nobody implements is a plain text field that writes an
|
|
701
|
-
// object as "[object Object]" (see warnOnUnknownPickerType in picker.jsx).
|
|
702
|
-
// Numeric value types map to navi_number rather than the native type="number"
|
|
703
|
-
// (spinner buttons, poor UX).
|
|
704
|
-
const CONTROL_TYPE_BY_SIGNAL_TYPE = {
|
|
705
|
-
boolean: "checkbox",
|
|
706
|
-
number: "navi_number",
|
|
707
|
-
integer: "navi_number",
|
|
708
|
-
float: "navi_number",
|
|
709
|
-
ratio: "navi_number",
|
|
710
|
-
longitude: "navi_number",
|
|
711
|
-
latitude: "navi_number",
|
|
712
|
-
second: "navi_number",
|
|
713
|
-
minute: "navi_number",
|
|
714
|
-
hour: "navi_number",
|
|
715
|
-
year: "navi_number",
|
|
716
|
-
percentage: "navi_percentage",
|
|
717
|
-
date: "date",
|
|
718
|
-
month: "month",
|
|
719
|
-
week: "week",
|
|
720
|
-
time: "time",
|
|
721
|
-
datetime: "datetime",
|
|
722
|
-
duration: "duration",
|
|
723
|
-
color: "color",
|
|
724
|
-
email: "email",
|
|
725
|
-
url: "url",
|
|
726
|
-
array: "array",
|
|
727
|
-
object: "object",
|
|
728
|
-
};
|
|
729
|
-
|
|
730
|
-
// Conceptual navi types: defaults, plus the host type they resolve to.
|
|
731
|
-
// `navi-input-type` keeps the type the caller asked for once the host has
|
|
732
|
-
// become something plainer — it is what says the value is a number (see
|
|
733
|
-
// isNumberInput), and what lets constraint messages use domain-specific
|
|
734
|
-
// wording instead of the generic "Ce nombre doit être...".
|
|
735
|
-
// A type a picker preset draws (navi_time, navi_minute: see picker_preset.jsx)
|
|
736
|
-
// has no row: resolveInputProps runs before the preset resolver, and a row
|
|
737
|
-
// would turn the type into its host before the preset resolver reads it. The
|
|
738
|
-
// preset hands the host type on itself.
|
|
739
|
-
const NAVI_TYPE_DEFAULTS = {
|
|
740
|
-
navi_percentage: {
|
|
741
|
-
"type": "navi_number",
|
|
742
|
-
"navi-input-type": "percentage",
|
|
743
|
-
"min": 0,
|
|
744
|
-
"max": 100,
|
|
745
|
-
"step": 1,
|
|
746
|
-
},
|
|
747
|
-
navi_number: {
|
|
748
|
-
"type": "text",
|
|
749
|
-
"navi-input-type": "number",
|
|
750
|
-
"autoCorrect": "off",
|
|
751
|
-
"spellcheck": false,
|
|
752
|
-
"autoComplete": "off",
|
|
753
|
-
},
|
|
754
|
-
};
|
|
755
|
-
|
|
756
|
-
// The navi input types whose value IS a number.
|
|
757
|
-
const NUMBER_NAVI_INPUT_TYPE_SET = new Set([
|
|
758
|
-
"number",
|
|
759
|
-
"percentage",
|
|
760
|
-
"hour",
|
|
761
|
-
"minute",
|
|
762
|
-
"second",
|
|
763
|
-
]);
|
|
764
|
-
|
|
765
|
-
/**
|
|
766
|
-
* Whether the control holds a number — asked of what the control IS, never of
|
|
767
|
-
* `inputMode`. In HTML `inputmode` picks the on-screen keyboard and says
|
|
768
|
-
* nothing about the value: a licence number, a postal code or a card number
|
|
769
|
-
* with a check letter all want the digit keypad while staying strings. What a
|
|
770
|
-
* number field is spelled `type="number"`, or a navi type that resolves to a
|
|
771
|
-
* text host and leaves `navi-input-type` behind to say what it was.
|
|
772
|
-
*/
|
|
773
|
-
const isNumberInput = (type, naviInputType) => {
|
|
774
|
-
if (type === "number") {
|
|
775
|
-
return true;
|
|
776
|
-
}
|
|
777
|
-
return NUMBER_NAVI_INPUT_TYPE_SET.has(naviInputType);
|
|
778
|
-
};
|
|
779
|
-
|
|
780
|
-
/**
|
|
781
|
-
* resolveInputProps — normalizes input-related props that are shared across
|
|
782
|
-
* `<Picker>`, `<Input>` (textual) and `<Range>`. Mutates the props object in place.
|
|
783
|
-
*
|
|
784
|
-
* Normalization is applied recursively: a navi type may resolve to another navi
|
|
785
|
-
* type (e.g. `navi_percentage` → `navi_number` → `text`), and each step applies its
|
|
786
|
-
* own formatters and defaults before moving to the next.
|
|
787
|
-
*
|
|
788
|
-
* Steps applied for each type:
|
|
789
|
-
* 1. Record the original navi type in `props["navi-input-type"]` (first call only).
|
|
790
|
-
* 2. Apply defaults for the current type (min, max, step, and any other props),
|
|
791
|
-
* only when the prop is not already set.
|
|
792
|
-
* 3. Apply min/max formatters (e.g. HH:MM string → number for duration types,
|
|
793
|
-
* Date → formatted string for date/time types).
|
|
794
|
-
* 4. Apply step formatter (same conversion rules).
|
|
795
|
-
* 5. Remap `props.type` to the target type defined by the current type's defaults,
|
|
796
|
-
* then recurse.
|
|
797
|
-
*
|
|
798
|
-
* Supported navi types and their targets:
|
|
799
|
-
* - `navi_percentage` → `navi_number` (0–100, step 1)
|
|
800
|
-
* - `navi_number` → `text` (inputMode="numeric", no spin buttons implied)
|
|
801
|
-
*
|
|
802
|
-
* Standard HTML input types with formatters:
|
|
803
|
-
* - `date`, `month`, `week`, `time`, `datetime-local`, `datetime`:
|
|
804
|
-
* min/max accept `Date` instances or timestamps and are converted to the
|
|
805
|
-
* string format expected by the native input.
|
|
806
|
-
* - `time`, `datetime-local`, `datetime`:
|
|
807
|
-
* step accepts `"HH:MM"` and is converted to seconds.
|
|
808
|
-
*/
|
|
809
|
-
/**
|
|
810
|
-
* A bound signal that carries a default of its own says the same thing on every
|
|
811
|
-
* control: the control starts there, uncontrolled, and a reset goes back to it.
|
|
812
|
-
* Uncontrolled here is about where the control starts, not about whether it
|
|
813
|
-
* follows the signal — the binding stays two-way either way (see
|
|
814
|
-
* stateFromSignal in control_hooks.jsx) — nor about what a form counts as
|
|
815
|
-
* held: that is the signal's content (isUIStateHeld in held_ui_state.js), and a
|
|
816
|
-
* stateSignal holds its default. Written once and used by everything
|
|
817
|
-
* that takes a `signal`, so one signal cannot mean two different things
|
|
818
|
-
* depending on which control it was handed to.
|
|
819
|
-
*/
|
|
820
|
-
const seedDefaultValueFromSignal = (props) => {
|
|
821
|
-
const signalOptions = props.signal?.options;
|
|
822
|
-
if (!signalOptions) {
|
|
823
|
-
return;
|
|
824
|
-
}
|
|
825
|
-
if (Object.hasOwn(props, "defaultValue")) {
|
|
826
|
-
// explicit defaultValue prop prevails
|
|
827
|
-
return;
|
|
828
|
-
}
|
|
829
|
-
// Snapshot the signal's current default so that resetUIState restores to the
|
|
830
|
-
// original default — not the value the signal had at the time of the last
|
|
831
|
-
// re-render.
|
|
832
|
-
const defaultValue = signalOptions.getDefaultValue(false);
|
|
833
|
-
if (defaultValue !== undefined) {
|
|
834
|
-
props.defaultValue = defaultValue;
|
|
835
|
-
}
|
|
836
|
-
};
|
|
837
|
-
|
|
838
|
-
const resolveInputProps = (props, { controlType = "input" } = {}) => {
|
|
839
|
-
// `signal` carries a bound state signal. It is left on `props` on purpose:
|
|
840
|
-
// `createControlInfo` (control_hooks.jsx) reads it to seed the state and to
|
|
841
|
-
// follow it, and `onUIAction` (ui_state_controller.js) writes user
|
|
842
|
-
// interactions back into it. Here we only derive input defaults (type/min/max,
|
|
843
|
-
// defaultValue/defaultChecked) from the signal's `options`, so the control
|
|
844
|
-
// ends up uncontrolled-with-default while still bound to the signal.
|
|
845
|
-
const signal = props.signal;
|
|
846
|
-
if (signal) {
|
|
847
|
-
const signalOptions = signal.options;
|
|
848
|
-
if (signalOptions) {
|
|
849
|
-
for (const key of ["min", "max", "step"]) {
|
|
850
|
-
if (props[key] === undefined && signalOptions[key] !== undefined) {
|
|
851
|
-
props[key] = signalOptions[key];
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
if (props.type === undefined && signalOptions.type !== undefined) {
|
|
855
|
-
const typeFromSignal = CONTROL_TYPE_BY_SIGNAL_TYPE[signalOptions.type];
|
|
856
|
-
// A value type and the control for it usually agree — a date-typed
|
|
857
|
-
// signal wants a date field — but a boolean one maps to a checkbox, and
|
|
858
|
-
// a picker made into a checkbox is not a picker with a different look:
|
|
859
|
-
// it is another control, with no popup to open. A picker asked to hold
|
|
860
|
-
// a yes/no keeps its two rows and stays itself.
|
|
861
|
-
const wouldChangeWhatTheControlIs =
|
|
862
|
-
controlType === "picker" &&
|
|
863
|
-
(typeFromSignal === "checkbox" || typeFromSignal === "radio");
|
|
864
|
-
if (typeFromSignal !== undefined && !wouldChangeWhatTheControlIs) {
|
|
865
|
-
props.type = typeFromSignal;
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
|
|
870
|
-
const isCheckable = props.type === "checkbox" || props.type === "radio";
|
|
871
|
-
if (isCheckable) {
|
|
872
|
-
if (Object.hasOwn(props, "defaultChecked")) ; else {
|
|
873
|
-
// If no explicit defaultChecked, derive it from the signal's default
|
|
874
|
-
// value so that resetUIState restores to the original default.
|
|
875
|
-
// Only a stateSignal carries a default of its own; a plain signal has
|
|
876
|
-
// no `options` at all, hence the optional read.
|
|
877
|
-
const defaultVal = signalOptions?.getDefaultValue(false);
|
|
878
|
-
if (defaultVal === undefined) ; else if (props.type === "radio") {
|
|
879
|
-
if (defaultVal === true) {
|
|
880
|
-
props.defaultChecked = true;
|
|
881
|
-
} else if (
|
|
882
|
-
Object.hasOwn(props, "value") &&
|
|
883
|
-
defaultVal === props.value
|
|
884
|
-
) {
|
|
885
|
-
props.defaultChecked = true;
|
|
886
|
-
}
|
|
887
|
-
} else if (typeof defaultVal === "boolean") {
|
|
888
|
-
// Standalone checkbox bound to a boolean signal.
|
|
889
|
-
props.defaultChecked = defaultVal;
|
|
890
|
-
} else {
|
|
891
|
-
// Checkbox is a group member: defaultVal is the array of
|
|
892
|
-
// selected item values.
|
|
893
|
-
const checkboxValue = props.value;
|
|
894
|
-
props.defaultChecked =
|
|
895
|
-
Array.isArray(defaultVal) && defaultVal.includes(checkboxValue);
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
return;
|
|
899
|
-
}
|
|
900
|
-
|
|
901
|
-
seedDefaultValueFromSignal(props);
|
|
902
|
-
}
|
|
903
|
-
|
|
904
|
-
const currentType = props.type;
|
|
905
|
-
// Apply min/max/step formatters before anything else — this must run even for
|
|
906
|
-
// standard HTML types (date, time, etc.) that have no NAVI_TYPE_DEFAULTS entry.
|
|
907
|
-
const currentTypeMinMaxFormatter = MIN_MAX_FORMATTER_BY_TYPE[currentType];
|
|
908
|
-
const currentTypeStepFormatter = STEP_FORMATTER_BY_TYPE[currentType];
|
|
909
|
-
if (currentTypeMinMaxFormatter) {
|
|
910
|
-
props.min = currentTypeMinMaxFormatter(props.min);
|
|
911
|
-
props.max = currentTypeMinMaxFormatter(props.max);
|
|
912
|
-
}
|
|
913
|
-
if (currentTypeStepFormatter) {
|
|
914
|
-
props.step = currentTypeStepFormatter(props.step);
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
// For navi_number: choose inputMode based on whether step/min/max suggest decimals.
|
|
918
|
-
// inputMode="numeric" (integer keyboard) vs "decimal" (keyboard with decimal separator).
|
|
919
|
-
if (currentType === "navi_number") {
|
|
920
|
-
if (props.inputMode === undefined) {
|
|
921
|
-
props.inputMode =
|
|
922
|
-
hasDecimalPlaces(props.step) ||
|
|
923
|
-
hasDecimalPlaces(props.min) ||
|
|
924
|
-
hasDecimalPlaces(props.max)
|
|
925
|
-
? "decimal"
|
|
926
|
-
: "numeric";
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
|
|
930
|
-
const { charGuard } = props;
|
|
931
|
-
if (charGuard) {
|
|
932
|
-
if (charGuard === true || charGuard === "auto") {
|
|
933
|
-
// Auto-resolve charGuard from context.
|
|
934
|
-
let charGuardResolved;
|
|
935
|
-
const inputMode = props.inputMode;
|
|
936
|
-
if (inputMode === "numeric") {
|
|
937
|
-
charGuardResolved = "numeric";
|
|
938
|
-
} else if (inputMode === "decimal") {
|
|
939
|
-
charGuardResolved = "decimal";
|
|
940
|
-
} else if (currentType === "tel") {
|
|
941
|
-
charGuardResolved = "tel";
|
|
942
|
-
} else if (currentType === "email") {
|
|
943
|
-
charGuardResolved = "email";
|
|
944
|
-
}
|
|
945
|
-
if (charGuardResolved !== undefined) {
|
|
946
|
-
props.charGuard = charGuardResolved;
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
// charGuard is now resolved: derive inputMode from it if not already set.
|
|
950
|
-
if (props.inputMode === undefined && props.charGuard) {
|
|
951
|
-
const autoMode = INPUT_MODE_FROM_CHAR_GUARD[props.charGuard];
|
|
952
|
-
if (autoMode) {
|
|
953
|
-
props.inputMode = autoMode;
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
// Build pattern from the resolved charGuard (preset name → class, or raw class passthrough).
|
|
957
|
-
if (props.pattern === undefined && props.charGuard) {
|
|
958
|
-
const charClass = CHAR_CLASS_PRESETS[props.charGuard] ?? props.charGuard;
|
|
959
|
-
props.pattern = `${charClass}*`;
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
// Compute maxLength from max when inputMode is numeric/decimal.
|
|
964
|
-
// Done here (after inputMode is set) so controller.props has the resolved value.
|
|
965
|
-
if (props.maxLength === undefined) {
|
|
966
|
-
if (props.inputMode === "numeric") {
|
|
967
|
-
const { min, max } = props;
|
|
968
|
-
if (max === undefined) ; else {
|
|
969
|
-
const canBeNegative = min === undefined ? max < 0 : min < 0;
|
|
970
|
-
const signCharCount = canBeNegative ? 1 : 0;
|
|
971
|
-
const integerDigitCount = String(Math.floor(Math.abs(max))).length;
|
|
972
|
-
props.maxLength = signCharCount + integerDigitCount;
|
|
973
|
-
}
|
|
974
|
-
} else if (props.inputMode === "decimal") {
|
|
975
|
-
const { min, max, step } = props;
|
|
976
|
-
if (max === undefined) ; else if (step === undefined) ; else {
|
|
977
|
-
const canBeNegative = min === undefined ? max < 0 : min < 0;
|
|
978
|
-
const signCharCount = canBeNegative ? 1 : 0;
|
|
979
|
-
const integerDigitCount = String(Math.floor(Math.abs(max))).length;
|
|
980
|
-
const stepStr = String(step);
|
|
981
|
-
const dotIndex = stepStr.indexOf(".");
|
|
982
|
-
// integer step + decimal inputMode is an unusual combo, but we stay consistent:
|
|
983
|
-
// no decimal part in maxLength since valid values are whole numbers anyway
|
|
984
|
-
const isIntegerStep = dotIndex === -1;
|
|
985
|
-
const decimalSignCharCount = isIntegerStep ? 0 : 1;
|
|
986
|
-
const decimalDigitCount = isIntegerStep
|
|
987
|
-
? 0
|
|
988
|
-
: stepStr.length - dotIndex - 1;
|
|
989
|
-
props.maxLength =
|
|
990
|
-
signCharCount +
|
|
991
|
-
integerDigitCount +
|
|
992
|
-
decimalSignCharCount +
|
|
993
|
-
decimalDigitCount;
|
|
994
|
-
}
|
|
995
|
-
}
|
|
996
|
-
}
|
|
997
|
-
|
|
998
|
-
// Resolve maxLengthGuard boolean/auto → the computed maxLength number.
|
|
999
|
-
if (props.maxLengthGuard === true || props.maxLengthGuard === "auto") {
|
|
1000
|
-
props.maxLengthGuard =
|
|
1001
|
-
typeof props.maxLength === "number" ? props.maxLength : undefined;
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
const currentTypeDefaults = NAVI_TYPE_DEFAULTS[currentType];
|
|
1005
|
-
if (!currentTypeDefaults) {
|
|
1006
|
-
return;
|
|
1007
|
-
}
|
|
1008
|
-
|
|
1009
|
-
for (const key of Object.keys(currentTypeDefaults)) {
|
|
1010
|
-
if (props[key] === undefined) {
|
|
1011
|
-
props[key] = currentTypeDefaults[key];
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
const targetType = currentTypeDefaults.type;
|
|
1015
|
-
props.type = targetType;
|
|
1016
|
-
resolveInputProps(props);
|
|
1017
|
-
};
|
|
1018
|
-
|
|
1019
|
-
// Presets that imply a specific mobile keyboard inputMode.
|
|
1020
|
-
const INPUT_MODE_FROM_CHAR_GUARD = {
|
|
1021
|
-
numeric: "numeric",
|
|
1022
|
-
pin: "numeric",
|
|
1023
|
-
card: "numeric",
|
|
1024
|
-
tel: "tel",
|
|
1025
|
-
decimal: "decimal",
|
|
1026
|
-
};
|
|
1027
|
-
|
|
1028
|
-
const normalizeToDate = (value) => {
|
|
1029
|
-
if (value === undefined || value === null) {
|
|
1030
|
-
return null;
|
|
1031
|
-
}
|
|
1032
|
-
if (typeof value === "number") {
|
|
1033
|
-
return new Date(value);
|
|
1034
|
-
}
|
|
1035
|
-
if (value instanceof Date) {
|
|
1036
|
-
return value;
|
|
1037
|
-
}
|
|
1038
|
-
return null;
|
|
1039
|
-
};
|
|
1040
|
-
|
|
1041
|
-
const toInputDate = (value) => {
|
|
1042
|
-
const date = normalizeToDate(value);
|
|
1043
|
-
if (!date) {
|
|
1044
|
-
return value;
|
|
1045
|
-
}
|
|
1046
|
-
const yyyy = date.getFullYear();
|
|
1047
|
-
const mm = String(date.getMonth() + 1).padStart(2, "0");
|
|
1048
|
-
const dd = String(date.getDate()).padStart(2, "0");
|
|
1049
|
-
return `${yyyy}-${mm}-${dd}`;
|
|
1050
|
-
};
|
|
1051
|
-
const toInputMonth = (value) => {
|
|
1052
|
-
const date = normalizeToDate(value);
|
|
1053
|
-
if (!date) {
|
|
1054
|
-
return value;
|
|
1055
|
-
}
|
|
1056
|
-
const yyyy = date.getFullYear();
|
|
1057
|
-
const mm = String(date.getMonth() + 1).padStart(2, "0");
|
|
1058
|
-
return `${yyyy}-${mm}`;
|
|
1059
|
-
};
|
|
1060
|
-
const toInputWeek = (value) => {
|
|
1061
|
-
const date = normalizeToDate(value);
|
|
1062
|
-
if (!date) {
|
|
1063
|
-
return value;
|
|
1064
|
-
}
|
|
1065
|
-
// ISO week number
|
|
1066
|
-
const d = new Date(date);
|
|
1067
|
-
d.setHours(0, 0, 0, 0);
|
|
1068
|
-
d.setDate(d.getDate() + 3 - ((d.getDay() + 6) % 7));
|
|
1069
|
-
const yearStart = new Date(d.getFullYear(), 0, 4);
|
|
1070
|
-
const week =
|
|
1071
|
-
Math.round(
|
|
1072
|
-
((d - yearStart) / 86400000 - 3 + ((yearStart.getDay() + 6) % 7)) / 7,
|
|
1073
|
-
) + 1;
|
|
1074
|
-
return `${d.getFullYear()}-W${String(week).padStart(2, "0")}`;
|
|
1075
|
-
};
|
|
1076
|
-
const toInputTime = (value) => {
|
|
1077
|
-
const date = normalizeToDate(value);
|
|
1078
|
-
if (!date) {
|
|
1079
|
-
return value;
|
|
1080
|
-
}
|
|
1081
|
-
const hh = String(date.getHours()).padStart(2, "0");
|
|
1082
|
-
const mm = String(date.getMinutes()).padStart(2, "0");
|
|
1083
|
-
return `${hh}:${mm}`;
|
|
1084
|
-
};
|
|
1085
|
-
const toInputDatetime = (value) => {
|
|
1086
|
-
const date = normalizeToDate(value);
|
|
1087
|
-
if (!date) {
|
|
1088
|
-
return value;
|
|
1089
|
-
}
|
|
1090
|
-
const yyyy = date.getFullYear();
|
|
1091
|
-
const mm = String(date.getMonth() + 1).padStart(2, "0");
|
|
1092
|
-
const dd = String(date.getDate()).padStart(2, "0");
|
|
1093
|
-
const hh = String(date.getHours()).padStart(2, "0");
|
|
1094
|
-
const min = String(date.getMinutes()).padStart(2, "0");
|
|
1095
|
-
return `${yyyy}-${mm}-${dd}T${hh}:${min}`;
|
|
1096
|
-
};
|
|
1097
|
-
|
|
1098
|
-
const MIN_MAX_FORMATTER_BY_TYPE = {
|
|
1099
|
-
"date": toInputDate,
|
|
1100
|
-
"month": toInputMonth,
|
|
1101
|
-
"week": toInputWeek,
|
|
1102
|
-
"time": toInputTime,
|
|
1103
|
-
"datetime-local": toInputDatetime,
|
|
1104
|
-
"datetime": toInputDatetime,
|
|
1105
|
-
};
|
|
1106
|
-
const STEP_FORMATTER_BY_TYPE = {
|
|
1107
|
-
"time": timeStringToSeconds,
|
|
1108
|
-
"datetime-local": timeStringToSeconds,
|
|
1109
|
-
"datetime": timeStringToSeconds,
|
|
1110
|
-
};
|
|
1111
|
-
|
|
1112
|
-
const hasDecimalPlaces = (value) => {
|
|
1113
|
-
if (value === undefined || value === null) {
|
|
1114
|
-
return false;
|
|
1115
|
-
}
|
|
1116
|
-
const num = Number(value);
|
|
1117
|
-
return !isNaN(num) && !Number.isInteger(num);
|
|
1118
|
-
};
|
|
1119
|
-
|
|
1120
|
-
const dispatchRequestSetUIState = (element, value, detail) => {
|
|
1121
|
-
const controlHost = findControlHost(element) || element;
|
|
1122
|
-
return dispatchInternalCustomEvent(controlHost, "navi_set_ui_state", {
|
|
1123
|
-
...detail,
|
|
1124
|
-
value,
|
|
1125
|
-
});
|
|
1126
|
-
};
|
|
1127
|
-
const dispatchRequestClearUIState = (element, e) => {
|
|
1128
|
-
const controlHost = findControlHost(element) || element;
|
|
1129
|
-
return dispatchInternalCustomEvent(controlHost, "navi_clear_ui_state", {
|
|
1130
|
-
event: e,
|
|
1131
|
-
});
|
|
1132
|
-
};
|
|
1133
|
-
const dispatchRequestResetUIState = (element, e) => {
|
|
1134
|
-
const controlHost = findControlHost(element) || element;
|
|
1135
|
-
return dispatchInternalCustomEvent(controlHost, "navi_reset_ui_state", {
|
|
1136
|
-
event: e,
|
|
1137
|
-
});
|
|
1138
|
-
};
|
|
1139
|
-
/**
|
|
1140
|
-
* @param {Element} el
|
|
1141
|
-
* @param {{ own?: boolean }} [options] `own`: what the element holds BY ITSELF.
|
|
1142
|
-
* Only a button ever answers differently — one with no value of its own
|
|
1143
|
-
* inherits the value of the control around it, which is what makes
|
|
1144
|
-
* `--navi-send` on a form's button be about that form. Something asking what
|
|
1145
|
-
* THIS element says (a travel command reading what the travel is about) wants
|
|
1146
|
-
* the own value and would otherwise be handed the surrounding control's.
|
|
1147
|
-
*/
|
|
1148
|
-
const getUIStateFromElement = (el, { own } = {}) => {
|
|
1149
|
-
let uiState;
|
|
1150
|
-
dispatchInternalCustomEvent(el, "navi_get_ui_state", {
|
|
1151
|
-
own,
|
|
1152
|
-
respondWith: (v) => {
|
|
1153
|
-
uiState = v;
|
|
1154
|
-
},
|
|
1155
|
-
});
|
|
1156
|
-
return uiState;
|
|
1157
|
-
};
|
|
1158
|
-
|
|
1159
|
-
/**
|
|
1160
|
-
* Converts a JS value into the form expected by the browser DOM property for a
|
|
1161
|
-
* given control type/input type combination.
|
|
1162
|
-
*
|
|
1163
|
-
* For example:
|
|
1164
|
-
* - `datetime-local` inputs expect a local datetime string without timezone
|
|
1165
|
-
* - `number`/`range` inputs expect a numeric string or number
|
|
1166
|
-
* - `color` inputs require a non-empty hex string (falls back to `#000000`)
|
|
1167
|
-
* - All other inputs receive the value as-is (undefined → "")
|
|
1168
|
-
*
|
|
1169
|
-
* Returns either the converted value directly, or a converter function when the
|
|
1170
|
-
* conversion depends on the runtime value (e.g. plain inputs return `asInputValue`).
|
|
1171
|
-
*
|
|
1172
|
-
* @param {any} value - The JS value to convert.
|
|
1173
|
-
* @param {{ controlType: string, type: string }} options
|
|
1174
|
-
* @returns {any} The DOM-compatible value or a converter function.
|
|
1175
|
-
*/
|
|
1176
|
-
const asControlHostValue = (
|
|
1177
|
-
jsValue,
|
|
1178
|
-
{ controlType, type, naviInputType, pad },
|
|
1179
|
-
) => {
|
|
1180
|
-
if (controlType === "select") {
|
|
1181
|
-
// A select holds one of its options, always a string; holding nothing is
|
|
1182
|
-
// the empty option, which the element spells "".
|
|
1183
|
-
return asInputValue(jsValue);
|
|
1184
|
-
}
|
|
1185
|
-
if (controlType === "input" || controlType === "picker") {
|
|
1186
|
-
if (type === "datetime-local") {
|
|
1187
|
-
return asDatetimeLocalString(jsValue);
|
|
1188
|
-
}
|
|
1189
|
-
if (type === "range" || isNumberInput(type, naviInputType)) {
|
|
1190
|
-
return asNumberString(jsValue, pad);
|
|
1191
|
-
}
|
|
1192
|
-
if (type === "color") {
|
|
1193
|
-
return asColorString(jsValue);
|
|
1194
|
-
}
|
|
1195
|
-
return asInputValue(jsValue);
|
|
1196
|
-
}
|
|
1197
|
-
return jsValue;
|
|
1198
|
-
};
|
|
1199
|
-
// As explained in https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input/datetime-local#setting_timezones
|
|
1200
|
-
// datetime-local does not support timezones
|
|
1201
|
-
const asDatetimeLocalString = (dateTimeString) => {
|
|
1202
|
-
const date = new Date(dateTimeString);
|
|
1203
|
-
if (isNaN(date.getTime())) {
|
|
1204
|
-
return dateTimeString;
|
|
1205
|
-
}
|
|
1206
|
-
const year = date.getFullYear();
|
|
1207
|
-
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
1208
|
-
const day = String(date.getDate()).padStart(2, "0");
|
|
1209
|
-
const hours = String(date.getHours()).padStart(2, "0");
|
|
1210
|
-
const minutes = String(date.getMinutes()).padStart(2, "0");
|
|
1211
|
-
const seconds = String(date.getSeconds()).padStart(2, "0");
|
|
1212
|
-
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;
|
|
1213
|
-
};
|
|
1214
|
-
// `pad` is how many digits the number is WRITTEN on — an hour is held as 7 and
|
|
1215
|
-
// shown as "07". Held and shown are two things here, the way they are for a
|
|
1216
|
-
// datetime-local above: what the field says is derived from what the control
|
|
1217
|
-
// holds, and reading it back (readNumberFromInput) gives the number again.
|
|
1218
|
-
const asNumberString = (jsValue, pad) => {
|
|
1219
|
-
if (jsValue === undefined) {
|
|
1220
|
-
return "";
|
|
1221
|
-
}
|
|
1222
|
-
if (!pad || jsValue === "" || jsValue === null) {
|
|
1223
|
-
return jsValue;
|
|
1224
|
-
}
|
|
1225
|
-
const number = Number(jsValue);
|
|
1226
|
-
if (Number.isNaN(number)) {
|
|
1227
|
-
return jsValue;
|
|
1228
|
-
}
|
|
1229
|
-
const negative = number < 0;
|
|
1230
|
-
const digits = String(negative ? -number : number).padStart(Number(pad), "0");
|
|
1231
|
-
return negative ? `-${digits}` : digits;
|
|
1232
|
-
};
|
|
1233
|
-
// Browser requires a non-empty value for <input type="color">.
|
|
1234
|
-
// When our logical value is empty we give it #000000 so it doesn't choke.
|
|
1235
|
-
// The UI uses the original (possibly empty) value to show the checkerboard.
|
|
1236
|
-
const asColorString = (jsValue) => {
|
|
1237
|
-
return jsValue || "#000000";
|
|
1238
|
-
};
|
|
1239
|
-
const asInputValue = (jsValue) => {
|
|
1240
|
-
if (jsValue === undefined) {
|
|
1241
|
-
return "";
|
|
1242
|
-
}
|
|
1243
|
-
return jsValue;
|
|
1244
|
-
};
|
|
1245
|
-
|
|
1246
|
-
/**
|
|
1247
|
-
* Reads the current logical JS value from a control host DOM element.
|
|
1248
|
-
*
|
|
1249
|
-
* Handles all navi control host element types:
|
|
1250
|
-
* - `<button>` — reads via `navi_get_value` custom event, falls back to `button.value`
|
|
1251
|
-
* - `<input type="number|range">` — parses as a number, returns `undefined` when empty
|
|
1252
|
-
* - `<input type="checkbox|radio">` — returns `undefined` when unchecked, otherwise reads
|
|
1253
|
-
* via `navi_get_value` custom event (to preserve the original JS type of the value prop)
|
|
1254
|
-
* - `<input type="datetime-local">` — converts the local datetime string to an ISO 8601 string
|
|
1255
|
-
* - `<input type="navi_picker">` — delegates to the controller via `navi_get_ui_state`
|
|
1256
|
-
* - All other inputs — returns `input.value` as a string
|
|
1257
|
-
*
|
|
1258
|
-
* @param {HTMLElement} controlHost - The control host DOM element to read from.
|
|
1259
|
-
* @returns {any} The current logical value of the control.
|
|
1260
|
-
*/
|
|
1261
|
-
const readControlValue = (controlHost) => {
|
|
1262
|
-
if (
|
|
1263
|
-
controlHost.tagName === "BUTTON" ||
|
|
1264
|
-
controlHost.getAttribute("role") === "button"
|
|
1265
|
-
) {
|
|
1266
|
-
return readValueFromButton(controlHost);
|
|
1267
|
-
}
|
|
1268
|
-
if (controlHost.tagName === "INPUT") {
|
|
1269
|
-
// important: input.type = "navi_js"; followed by input.type; returns "text"
|
|
1270
|
-
// so use getAttribute
|
|
1271
|
-
const type = controlHost.getAttribute("type");
|
|
1272
|
-
|
|
1273
|
-
if (
|
|
1274
|
-
type === "range" ||
|
|
1275
|
-
isNumberInput(type, controlHost.getAttribute("navi-input-type"))
|
|
1276
|
-
) {
|
|
1277
|
-
return readNumberFromInput(controlHost);
|
|
1278
|
-
}
|
|
1279
|
-
if (type === "color") {
|
|
1280
|
-
return readValueFromControlHost(controlHost);
|
|
1281
|
-
}
|
|
1282
|
-
if (type === "checkbox" || type === "radio") {
|
|
1283
|
-
return readValueFromCheckableInput(controlHost);
|
|
1284
|
-
}
|
|
1285
|
-
if (type === "datetime-local") {
|
|
1286
|
-
return readDatetimeLocalFromInput(controlHost);
|
|
1287
|
-
}
|
|
1288
|
-
if (type === "navi_js") {
|
|
1289
|
-
return getUIStateFromElement(controlHost);
|
|
1290
|
-
}
|
|
1291
|
-
return readValueFromInput(controlHost);
|
|
1292
|
-
}
|
|
1293
|
-
if (controlHost.hasAttribute("navi-control-host")) {
|
|
1294
|
-
// Non-button, non-input navi controls (e.g. Badge.Button rendered as span)
|
|
1295
|
-
return readValueFromControlHost(controlHost);
|
|
1296
|
-
}
|
|
1297
|
-
return readValueFromElement(controlHost);
|
|
1298
|
-
};
|
|
1299
|
-
const readValueFromControlHost = (controlHost) => {
|
|
1300
|
-
return readValueFromNaviCustomEvent(controlHost, controlHost.value);
|
|
1301
|
-
};
|
|
1302
|
-
const readValueFromButton = (button) => {
|
|
1303
|
-
return readValueFromControlHost(button);
|
|
1304
|
-
};
|
|
1305
|
-
const readDatetimeLocalFromInput = (input) => {
|
|
1306
|
-
const localDateTimeString = input.value;
|
|
1307
|
-
if (localDateTimeString === "") {
|
|
1308
|
-
return "";
|
|
1309
|
-
}
|
|
1310
|
-
const localDate = new Date(localDateTimeString);
|
|
1311
|
-
if (isNaN(localDate.getTime())) {
|
|
1312
|
-
return localDateTimeString;
|
|
1313
|
-
}
|
|
1314
|
-
return localDate.toISOString();
|
|
1315
|
-
};
|
|
1316
|
-
const readNumberFromInput = (input) => {
|
|
1317
|
-
const numberString = input.value;
|
|
1318
|
-
if (numberString === "") {
|
|
1319
|
-
return "";
|
|
1320
|
-
}
|
|
1321
|
-
const asNumber = Number(numberString);
|
|
1322
|
-
if (isNaN(asNumber)) {
|
|
1323
|
-
return numberString;
|
|
1324
|
-
}
|
|
1325
|
-
return asNumber;
|
|
1326
|
-
};
|
|
1327
|
-
const readValueFromCheckableInput = (input) => {
|
|
1328
|
-
const checked = input.checked;
|
|
1329
|
-
if (!checked) {
|
|
1330
|
-
return undefined;
|
|
1331
|
-
}
|
|
1332
|
-
return readValueFromControlHost(input);
|
|
1333
|
-
};
|
|
1334
|
-
const readValueFromInput = (input) => {
|
|
1335
|
-
const value = input.value;
|
|
1336
|
-
return value;
|
|
1337
|
-
};
|
|
1338
|
-
const readValueFromElement = (element) => {
|
|
1339
|
-
const value = element.value;
|
|
1340
|
-
return value;
|
|
1341
|
-
};
|
|
1342
|
-
const readValueFromNaviCustomEvent = (field, fallback) => {
|
|
1343
|
-
// prefer the value given as prop (respect original type, browser would convert to string)
|
|
1344
|
-
let responded;
|
|
1345
|
-
let value;
|
|
1346
|
-
dispatchCustomEvent(field, "navi_get_value", {
|
|
1347
|
-
respondWith: (jsValue) => {
|
|
1348
|
-
responded = true;
|
|
1349
|
-
value = jsValue;
|
|
1350
|
-
},
|
|
1351
|
-
});
|
|
1352
|
-
if (responded) {
|
|
1353
|
-
return value;
|
|
1354
|
-
}
|
|
1355
|
-
return fallback;
|
|
1356
|
-
};
|
|
1357
|
-
|
|
1358
|
-
/**
|
|
1359
|
-
* A control is bound to EITHER a signal (two-way) or a value/checked prop,
|
|
1360
|
-
* never both — the two would fight over the state. The signal is the one that
|
|
1361
|
-
* wins; this says so in dev, wherever the question is decided (a leaf control
|
|
1362
|
-
* in control_hooks, a group in ui_state_controller).
|
|
1363
|
-
*
|
|
1364
|
-
* Only the controlled prop (value/checked) collides with signal — the default
|
|
1365
|
-
* prop (defaultValue/defaultChecked) is legitimately seeded from the signal by
|
|
1366
|
-
* resolveInputProps, so it must not warn here.
|
|
1367
|
-
*/
|
|
1368
|
-
const warnSignalCollision = (props, controlType, stateProp) => {
|
|
1369
|
-
if (Object.hasOwn(props, stateProp)) {
|
|
1370
|
-
console.warn(
|
|
1371
|
-
`[navi] "${controlType}" got both "signal" and "${stateProp}". ` +
|
|
1372
|
-
`"signal" is the source of truth; "${stateProp}" is ignored. ` +
|
|
1373
|
-
`Pass only "signal".`,
|
|
1374
|
-
);
|
|
1375
|
-
}
|
|
1376
|
-
};
|
|
1377
|
-
|
|
1378
|
-
/**
|
|
1379
|
-
* A signal handed to a prop that holds a plain value is the `signal` binding
|
|
1380
|
-
* written in the wrong place. Nothing throws: the signal object becomes the
|
|
1381
|
-
* value, and a value that cannot be written to the DOM is replaced by a
|
|
1382
|
-
* `window.__navi_js('…')` reference string — which is then what the field
|
|
1383
|
-
* shows on screen (see toDomValue in controller_registry.js).
|
|
1384
|
-
*/
|
|
1385
|
-
const warnSignalAsState = (props, controlType, stateProps) => {
|
|
1386
|
-
for (const stateProp of stateProps) {
|
|
1387
|
-
if (isSignal(props[stateProp])) {
|
|
1388
|
-
console.warn(
|
|
1389
|
-
`[navi] "${controlType}" got a signal as "${stateProp}". ` +
|
|
1390
|
-
`A signal binds a control through "signal" — did you mean signal={...}? ` +
|
|
1391
|
-
`As "${stateProp}" it is taken for the value itself.`,
|
|
1392
|
-
);
|
|
1393
|
-
}
|
|
1394
|
-
}
|
|
1395
|
-
};
|
|
1396
|
-
|
|
1397
|
-
// In-memory registry of all mounted ui state controllers keyed by their id.
|
|
1398
|
-
// Allows direct controller access without dispatching DOM events — used by external
|
|
1399
|
-
// callers (e.g. selectable_list) to call setUIState by id instead of via the DOM.
|
|
1400
|
-
const controllersById = new Map();
|
|
1401
|
-
|
|
1402
|
-
// In-memory registry for radio controllers, keyed by input name.
|
|
1403
|
-
// Allows radio sibling unchecking without querying the DOM — necessary when
|
|
1404
|
-
// items are virtualized and their DOM element may not exist at the time.
|
|
1405
|
-
// Form scoping is reproduced by comparing parentUIStateController references.
|
|
1406
|
-
const radioControllersByName = new Map();
|
|
1407
|
-
|
|
1408
|
-
// Registry for non-serializable JS values that cannot be written to DOM attributes as-is.
|
|
1409
|
-
// When a value is an object/array, we store it here and write a reference string to the DOM
|
|
1410
|
-
// instead of "[object Object]". Console-inspectable via window.__navi_js('id').
|
|
1411
|
-
// The controller id is used as key — if the controller has no id, the value is not registered.
|
|
1412
|
-
const naviJsRegistry = new Map();
|
|
1413
|
-
|
|
1414
|
-
// Controllers already reported for sharing their id with another one. An id is
|
|
1415
|
-
// stated on every render of its control, so without this the warning below
|
|
1416
|
-
// would repeat for as long as both controls are on screen.
|
|
1417
|
-
const controllersWarned = new WeakSet();
|
|
1418
|
-
|
|
1419
|
-
const getUIStateControllerById = (id) => controllersById.get(id);
|
|
1420
|
-
const getRadioSiblings = (radioUIStateController) => {
|
|
1421
|
-
const siblings = radioControllersByName.get(radioUIStateController.name);
|
|
1422
|
-
return siblings;
|
|
1423
|
-
};
|
|
1424
|
-
|
|
1425
|
-
const toDomValue = (
|
|
1426
|
-
jsValue,
|
|
1427
|
-
{ controlType, id, type, naviInputType, pad },
|
|
1428
|
-
) => {
|
|
1429
|
-
const domValue = asControlHostValue(jsValue, {
|
|
1430
|
-
controlType,
|
|
1431
|
-
type,
|
|
1432
|
-
naviInputType,
|
|
1433
|
-
pad,
|
|
1434
|
-
});
|
|
1435
|
-
if (isSerializableAsDomValue(domValue)) {
|
|
1436
|
-
return domValue;
|
|
1437
|
-
}
|
|
1438
|
-
naviJsRegistry.set(id, domValue);
|
|
1439
|
-
return `window.__navi_js('${id}')`;
|
|
1440
|
-
};
|
|
1441
|
-
|
|
1442
|
-
window.__navi_js = (id) => naviJsRegistry.get(id);
|
|
1443
|
-
const isSerializableAsDomValue = (value) => {
|
|
1444
|
-
if (value === null || value === undefined) {
|
|
1445
|
-
return true;
|
|
1446
|
-
}
|
|
1447
|
-
const type = typeof value;
|
|
1448
|
-
return type === "string" || type === "number" || type === "boolean";
|
|
1449
|
-
};
|
|
1450
|
-
|
|
1451
|
-
const onUIStateControllerCreated = (uiStateController) => {
|
|
1452
|
-
const { id, name, controlType } = uiStateController;
|
|
1453
|
-
if (id) {
|
|
1454
|
-
{
|
|
1455
|
-
const existing = controllersById.get(id);
|
|
1456
|
-
if (
|
|
1457
|
-
existing &&
|
|
1458
|
-
existing !== uiStateController &&
|
|
1459
|
-
!controllersWarned.has(uiStateController)
|
|
1460
|
-
) {
|
|
1461
|
-
controllersWarned.add(uiStateController);
|
|
1462
|
-
console.warn(
|
|
1463
|
-
`[navi] Two controls share the same id "${id}" ("${existing.controlType}" and "${controlType}"). ` +
|
|
1464
|
-
`The id resolves to whichever of them rendered last, so the other one is unreachable through it (undefined uiState, dead picker buttons). ` +
|
|
1465
|
-
`Common causes: several controls inside a single <Field> (the Field provides one id via context and every control consumes it — put each control in its own <Field>, or give one an explicit id), ` +
|
|
1466
|
-
`or the same content mounted twice (e.g. two popups sharing an id prefix while both hold their content).`,
|
|
1467
|
-
);
|
|
1468
|
-
}
|
|
1469
|
-
}
|
|
1470
|
-
controllersById.set(id, uiStateController);
|
|
1471
|
-
}
|
|
1472
|
-
const proxyFor = uiStateController.props["navi-control-proxy-for"];
|
|
1473
|
-
if (proxyFor) {
|
|
1474
|
-
let proxySet = proxyControllersByRealInputId.get(proxyFor);
|
|
1475
|
-
if (!proxySet) {
|
|
1476
|
-
proxySet = new Set();
|
|
1477
|
-
proxyControllersByRealInputId.set(proxyFor, proxySet);
|
|
1478
|
-
}
|
|
1479
|
-
proxySet.add(uiStateController);
|
|
1480
|
-
}
|
|
1481
|
-
if (
|
|
1482
|
-
controlType === "input" &&
|
|
1483
|
-
uiStateController.props.type === "radio" &&
|
|
1484
|
-
name
|
|
1485
|
-
) {
|
|
1486
|
-
let set = radioControllersByName.get(name);
|
|
1487
|
-
if (!set) {
|
|
1488
|
-
set = new Set();
|
|
1489
|
-
radioControllersByName.set(name, set);
|
|
1490
|
-
}
|
|
1491
|
-
set.add(uiStateController);
|
|
1492
|
-
}
|
|
1493
|
-
};
|
|
1494
|
-
const onUIStateControllerDestroyed = (uiStateController) => {
|
|
1495
|
-
const { id, name, controlType } = uiStateController;
|
|
1496
|
-
if (id) {
|
|
1497
|
-
// Only the controller the id currently points at may take the entry away:
|
|
1498
|
-
// when two controls share an id, the one leaving would otherwise unregister
|
|
1499
|
-
// the one staying (see the warning in onUIStateControllerCreated), and the
|
|
1500
|
-
// same holds while a control is being replaced by its successor, which
|
|
1501
|
-
// registers during its render, before this cleanup runs.
|
|
1502
|
-
if (controllersById.get(id) === uiStateController) {
|
|
1503
|
-
controllersById.delete(id);
|
|
1504
|
-
naviJsRegistry.delete(id);
|
|
1505
|
-
}
|
|
1506
|
-
}
|
|
1507
|
-
const proxyFor = uiStateController.props["navi-control-proxy-for"];
|
|
1508
|
-
if (proxyFor) {
|
|
1509
|
-
const proxySet = proxyControllersByRealInputId.get(proxyFor);
|
|
1510
|
-
if (proxySet) {
|
|
1511
|
-
proxySet.delete(uiStateController);
|
|
1512
|
-
if (proxySet.size === 0) {
|
|
1513
|
-
proxyControllersByRealInputId.delete(proxyFor);
|
|
1514
|
-
}
|
|
1515
|
-
}
|
|
1516
|
-
}
|
|
1517
|
-
if (
|
|
1518
|
-
controlType === "input" &&
|
|
1519
|
-
uiStateController.controlHostProps.type === "radio" &&
|
|
1520
|
-
name
|
|
1521
|
-
) {
|
|
1522
|
-
const set = radioControllersByName.get(name);
|
|
1523
|
-
if (set) {
|
|
1524
|
-
set.delete(uiStateController);
|
|
1525
|
-
if (set.size === 0) {
|
|
1526
|
-
radioControllersByName.delete(name);
|
|
1527
|
-
}
|
|
1528
|
-
}
|
|
1529
|
-
}
|
|
1530
|
-
};
|
|
1531
|
-
|
|
1532
|
-
/**
|
|
1533
|
-
* Controller-based equivalent of findControlProxyTarget.
|
|
1534
|
-
* Given a proxy controller, returns the real control's controller.
|
|
1535
|
-
* Finds the target by walking the parent controller's children — no DOM queries.
|
|
1536
|
-
* Returns `null` when the controller is not a proxy or the target is not found.
|
|
1537
|
-
*/
|
|
1538
|
-
const findControlProxyTargetController = (controller) => {
|
|
1539
|
-
const proxyFor = controller.controlHostProps["navi-control-proxy-for"];
|
|
1540
|
-
if (!proxyFor) {
|
|
1541
|
-
return null;
|
|
1542
|
-
}
|
|
1543
|
-
return getUIStateControllerById(proxyFor) ?? null;
|
|
1544
|
-
};
|
|
1545
|
-
|
|
1546
|
-
// Reverse-lookup map: real-input id → the proxy controllers that reference it
|
|
1547
|
-
// via `navi-control-proxy-for`. A single control can be represented by several
|
|
1548
|
-
// proxies (an "enable"/"disable" button pair for one radio, for instance), so
|
|
1549
|
-
// each id holds a set. Maintained on create/destroy so lookup is O(1).
|
|
1550
|
-
const proxyControllersByRealInputId = new Map();
|
|
1551
|
-
const findProxyControllers = (realInputId) => {
|
|
1552
|
-
if (!realInputId) {
|
|
1553
|
-
return null;
|
|
1554
|
-
}
|
|
1555
|
-
return proxyControllersByRealInputId.get(realInputId) ?? null;
|
|
1556
|
-
};
|
|
1557
|
-
|
|
1558
|
-
/**
|
|
1559
|
-
* DOM utilities for the proxy control pattern.
|
|
1560
|
-
*
|
|
1561
|
-
* Some components need a native `<input>` internally — for form submission,
|
|
1562
|
-
* constraint validation, or browser autofill — but the user may not want to
|
|
1563
|
-
* display that input at all. In those cases the input is hidden and a separate
|
|
1564
|
-
* visible element (the proxy) takes over the visual and interactive role.
|
|
1565
|
-
*
|
|
1566
|
-
* The typical use case is `SelectableList`: each list item acts as a styled
|
|
1567
|
-
* radio button, but an actual `<input type="radio">` lives hidden in the DOM
|
|
1568
|
-
* so form submission and validation work natively. When users DO want to
|
|
1569
|
-
* display the input they want full control over its appearance, so they render
|
|
1570
|
-
* their own element and link it to the real input via `navi-control-proxy-for`:
|
|
1571
|
-
*
|
|
1572
|
-
* ```html
|
|
1573
|
-
* <div>
|
|
1574
|
-
* <input id="color_red" type="radio" name="color" value="red" /> ← real control (hidden, drives form/validation)
|
|
1575
|
-
* <input type="radio" name="proxy" value="red" /> ← proxy (visible, delegates interactions to real input)
|
|
1576
|
-
* </div>
|
|
1577
|
-
* ```
|
|
1578
|
-
*
|
|
1579
|
-
* A press on a checkable proxy is dealt to the real control, and so is the
|
|
1580
|
-
* focus: state management, validation and form submission then all happen
|
|
1581
|
-
* where the value lives. A proxy that says what its press does — a button
|
|
1582
|
-
* carrying `command="--navi-check"` — answers the press itself instead.
|
|
1583
|
-
*
|
|
1584
|
-
* Note: an alternative design would be to require users to always instantiate
|
|
1585
|
-
* the input explicitly — e.g. `<Selectable.Input headless />` when they don't
|
|
1586
|
-
* want to display it. That would remove the need for the proxy mechanism
|
|
1587
|
-
* entirely. For now we keep the proxy pattern.
|
|
1588
|
-
*/
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
/**
|
|
1592
|
-
* Given a proxy element, returns the real control it represents.
|
|
1593
|
-
* Returns `null` when `el` is not a proxy.
|
|
1594
|
-
*/
|
|
1595
|
-
const findControlProxyTarget = (el) => {
|
|
1596
|
-
const proxyFor = el.getAttribute("navi-control-proxy-for");
|
|
1597
|
-
if (!proxyFor) {
|
|
1598
|
-
return null;
|
|
1599
|
-
}
|
|
1600
|
-
return document.getElementById(proxyFor);
|
|
1601
|
-
};
|
|
1602
|
-
|
|
1603
|
-
// Shared: asked on every pseudo state change of every box, and almost always
|
|
1604
|
-
// empty.
|
|
1605
|
-
const NO_PROXIES = Object.freeze([]);
|
|
1606
|
-
/**
|
|
1607
|
-
* Given a real control element, returns every proxy that visually represents
|
|
1608
|
-
* it — a control can have more than one (an "enable"/"disable" button pair for
|
|
1609
|
-
* one radio, for instance).
|
|
1610
|
-
*
|
|
1611
|
-
* Answered from the controller registry rather than the document: every proxy
|
|
1612
|
-
* declares itself through the `navi-control-proxy-for` prop, so the registry
|
|
1613
|
-
* knows them all, while asking the document means walking it in full for each
|
|
1614
|
-
* of the (overwhelmingly many) controls that have no proxy at all.
|
|
1615
|
-
*
|
|
1616
|
-
* Returns an empty array when no proxy exists for `el`.
|
|
1617
|
-
*/
|
|
1618
|
-
const findControlProxies = (el) => {
|
|
1619
|
-
if (!el.id) {
|
|
1620
|
-
return NO_PROXIES;
|
|
1621
|
-
}
|
|
1622
|
-
const proxyControllerSet = findProxyControllers(el.id);
|
|
1623
|
-
if (!proxyControllerSet) {
|
|
1624
|
-
return NO_PROXIES;
|
|
1625
|
-
}
|
|
1626
|
-
const proxyElements = [];
|
|
1627
|
-
for (const proxyController of proxyControllerSet) {
|
|
1628
|
-
const proxyElement = proxyController.ref.current;
|
|
1629
|
-
if (proxyElement) {
|
|
1630
|
-
proxyElements.push(proxyElement);
|
|
1631
|
-
}
|
|
1632
|
-
}
|
|
1633
|
-
return proxyElements;
|
|
1634
|
-
};
|
|
1635
|
-
|
|
1636
|
-
/**
|
|
1637
|
-
* Given a real control element, returns the proxy a gesture on the control
|
|
1638
|
-
* lands on — the one thing said about the control is drawn on.
|
|
1639
|
-
*
|
|
1640
|
-
* Use when you need a single visible stand-in for the real control — anchoring
|
|
1641
|
-
* a callout, for instance. Anything notifying proxies of a state change wants
|
|
1642
|
-
* `findControlProxies` instead, so a control represented by several of them
|
|
1643
|
-
* updates all of them.
|
|
1644
|
-
*
|
|
1645
|
-
* A proxy the pointer cannot reach is drawn for the control without standing
|
|
1646
|
-
* for it: the surface around it takes the presses for everything it holds, so
|
|
1647
|
-
* that surface is where a refusal about the control belongs — not a box inside
|
|
1648
|
-
* it the finger never has to find. A selectable list row whose whole area is
|
|
1649
|
-
* the click target is exactly that arrangement (see `selectableArea`), and it
|
|
1650
|
-
* is read from the arrangement itself rather than from a flag repeating it:
|
|
1651
|
-
* where a gesture can land IS pointer-events.
|
|
1652
|
-
*
|
|
1653
|
-
* Returns `null` when no proxy stands for `el`.
|
|
1654
|
-
*/
|
|
1655
|
-
const findControlProxy = (el) => {
|
|
1656
|
-
for (const proxyElement of findControlProxies(el)) {
|
|
1657
|
-
if (getComputedStyle(proxyElement).pointerEvents === "none") {
|
|
1658
|
-
continue;
|
|
1659
|
-
}
|
|
1660
|
-
return proxyElement;
|
|
1661
|
-
}
|
|
1662
|
-
return null;
|
|
1663
|
-
};
|
|
1664
|
-
|
|
1665
|
-
const addInputEffect = (
|
|
1666
|
-
input,
|
|
1667
|
-
callback,
|
|
1668
|
-
{ waitForChange = false, debounce = 0, debugInteraction = () => {} } = {},
|
|
1669
|
-
) => {
|
|
1670
|
-
const getState =
|
|
1671
|
-
input.type === "checkbox" || input.type === "radio"
|
|
1672
|
-
? () => input.checked
|
|
1673
|
-
: () => input.value;
|
|
1674
|
-
|
|
1675
|
-
if (waitForChange) {
|
|
1676
|
-
return listenInputStateChange(input, callback, { getState });
|
|
1677
|
-
}
|
|
1678
|
-
|
|
1679
|
-
const [teardown, addTeardown] = createPubSub();
|
|
1680
|
-
let currentState = getState();
|
|
1681
|
-
let timeout;
|
|
1682
|
-
let debounceTimeout;
|
|
1683
|
-
let onEvent;
|
|
1684
|
-
if (debounce) {
|
|
1685
|
-
onEvent = (e, { skipDebounce } = {}) => {
|
|
1686
|
-
clearTimeout(timeout);
|
|
1687
|
-
clearTimeout(debounceTimeout);
|
|
1688
|
-
const state = getState();
|
|
1689
|
-
if (state === currentState) {
|
|
1690
|
-
debugInteraction(
|
|
1691
|
-
e,
|
|
1692
|
-
`interaction ignored (state unchanged: "${state}")`,
|
|
1693
|
-
);
|
|
1694
|
-
return;
|
|
1695
|
-
}
|
|
1696
|
-
|
|
1697
|
-
if (skipDebounce) {
|
|
1698
|
-
debugInteraction(e, `skip debounce, callback called with "${state}"`);
|
|
1699
|
-
if (callback(e) !== false) {
|
|
1700
|
-
currentState = state;
|
|
1701
|
-
}
|
|
1702
|
-
} else {
|
|
1703
|
-
debugInteraction(
|
|
1704
|
-
e,
|
|
1705
|
-
`debounced ${debounce}ms, pending callback with "${state}"`,
|
|
1706
|
-
);
|
|
1707
|
-
debounceTimeout = setTimeout(() => {
|
|
1708
|
-
debugInteraction(
|
|
1709
|
-
e,
|
|
1710
|
-
`debounce elapsed, callback called with "${state}"`,
|
|
1711
|
-
);
|
|
1712
|
-
if (callback(e) !== false) {
|
|
1713
|
-
currentState = state;
|
|
1714
|
-
}
|
|
1715
|
-
}, debounce);
|
|
1716
|
-
}
|
|
1717
|
-
};
|
|
1718
|
-
// no need to wait for change events (blur, enter key)
|
|
1719
|
-
// we consider this as strong interactions requesting an immediate response
|
|
1720
|
-
const stop = listenInputStateChange(
|
|
1721
|
-
input,
|
|
1722
|
-
(e) => {
|
|
1723
|
-
onEvent(e, { skipDebounce: true });
|
|
1724
|
-
},
|
|
1725
|
-
{ getState, changeOnEnter: true },
|
|
1726
|
-
);
|
|
1727
|
-
addTeardown(() => {
|
|
1728
|
-
stop();
|
|
1729
|
-
});
|
|
1730
|
-
} else {
|
|
1731
|
-
onEvent = (e) => {
|
|
1732
|
-
clearTimeout(timeout);
|
|
1733
|
-
const state = getState();
|
|
1734
|
-
if (state === currentState) {
|
|
1735
|
-
debugInteraction(
|
|
1736
|
-
e,
|
|
1737
|
-
`interaction ignored (state unchanged: "${state}")`,
|
|
1738
|
-
);
|
|
1739
|
-
return;
|
|
1740
|
-
}
|
|
1741
|
-
debugInteraction(e, `callback called with "${state}"`);
|
|
1742
|
-
if (callback(e) !== false) {
|
|
1743
|
-
currentState = state;
|
|
1744
|
-
}
|
|
1745
|
-
};
|
|
1746
|
-
// Autocomplete, programmatic changes, form restoration
|
|
1747
|
-
input.addEventListener("change", onEvent);
|
|
1748
|
-
addTeardown(() => {
|
|
1749
|
-
input.removeEventListener("change", onEvent);
|
|
1750
|
-
});
|
|
1751
|
-
}
|
|
1752
|
-
|
|
1753
|
-
const onAsyncEvent = (e, options) => {
|
|
1754
|
-
debugInteraction(e, `async event, scheduling callback`);
|
|
1755
|
-
timeout = setTimeout(() => {
|
|
1756
|
-
onEvent(e, options);
|
|
1757
|
-
}, 0);
|
|
1758
|
-
};
|
|
1759
|
-
|
|
1760
|
-
// Standard user input (typing)
|
|
1761
|
-
const onInput = (e) => {
|
|
1762
|
-
onEvent(e);
|
|
1763
|
-
};
|
|
1764
|
-
input.addEventListener("input", onInput);
|
|
1765
|
-
addTeardown(() => {
|
|
1766
|
-
input.removeEventListener("input", onInput);
|
|
1767
|
-
});
|
|
1768
|
-
|
|
1769
|
-
// Form reset - need to check the form
|
|
1770
|
-
const form = input.form;
|
|
1771
|
-
if (form) {
|
|
1772
|
-
// Form reset happens asynchronously, check value after reset completes
|
|
1773
|
-
form.addEventListener("reset", onAsyncEvent);
|
|
1774
|
-
addTeardown(() => {
|
|
1775
|
-
form.removeEventListener("reset", onAsyncEvent);
|
|
1776
|
-
});
|
|
1777
|
-
}
|
|
1778
|
-
|
|
1779
|
-
// Paste events (some browsers need special handling)
|
|
1780
|
-
input.addEventListener("paste", onEvent);
|
|
1781
|
-
addTeardown(() => {
|
|
1782
|
-
input.removeEventListener("paste", onEvent);
|
|
1783
|
-
});
|
|
1784
|
-
|
|
1785
|
-
input.addEventListener("navi_ui_state_change", (e) => {
|
|
1786
|
-
// radios are unchecked by an internal setUIState call when another radio is checked.
|
|
1787
|
-
// navi_ui_state_change is dispatched whenever setUIState changes state, so we
|
|
1788
|
-
// listen here to keep currentState in sync — otherwise input_effect thinks the
|
|
1789
|
-
// radio is still checked and ignores the next user click as "state unchanged".
|
|
1790
|
-
if (input.type === "radio") {
|
|
1791
|
-
currentState = e.detail.value;
|
|
1792
|
-
}
|
|
1793
|
-
const clearEvent = findEvent(
|
|
1794
|
-
e,
|
|
1795
|
-
(eInChain) =>
|
|
1796
|
-
eInChain.type === "navi_set_ui_state" && eInChain.detail.isClear,
|
|
1797
|
-
);
|
|
1798
|
-
const isClear = Boolean(clearEvent);
|
|
1799
|
-
if (isClear) {
|
|
1800
|
-
// "navi_clear" behaves like an async event
|
|
1801
|
-
// a bit like form reset because
|
|
1802
|
-
// our action will be updated async after the component re-renders
|
|
1803
|
-
// and we need to wait that to happen to properly call action with the right value
|
|
1804
|
-
debugInteraction(e, `navi_clear received, scheduling callback`);
|
|
1805
|
-
onAsyncEvent(e, { skipDebounce: true });
|
|
1806
|
-
}
|
|
1807
|
-
});
|
|
1808
|
-
|
|
1809
|
-
return teardown;
|
|
1810
|
-
};
|
|
1811
|
-
|
|
1812
|
-
const listenInputStateChange = (
|
|
1813
|
-
input,
|
|
1814
|
-
callback,
|
|
1815
|
-
{ getState, changeOnEnter },
|
|
1816
|
-
) => {
|
|
1817
|
-
const [teardown, addTeardown] = createPubSub();
|
|
1818
|
-
|
|
1819
|
-
let stateAtInteraction;
|
|
1820
|
-
const oninput = () => {
|
|
1821
|
-
stateAtInteraction = undefined;
|
|
1822
|
-
};
|
|
1823
|
-
const onkeydown = (e) => {
|
|
1824
|
-
if (e.key === "Enter") {
|
|
1825
|
-
/**
|
|
1826
|
-
* Browser trigger a "change" event right after the enter is pressed
|
|
1827
|
-
* if the input value has changed.
|
|
1828
|
-
* We need to prevent the next change event otherwise we would request action twice
|
|
1829
|
-
*/
|
|
1830
|
-
stateAtInteraction = getState();
|
|
1831
|
-
if (changeOnEnter) {
|
|
1832
|
-
onchange(e);
|
|
1833
|
-
}
|
|
1834
|
-
}
|
|
1835
|
-
if (e.key === "Escape") {
|
|
1836
|
-
/**
|
|
1837
|
-
* Browser trigger a "change" event right after the escape is pressed
|
|
1838
|
-
* if the input value has changed.
|
|
1839
|
-
* We need to prevent the next change event otherwise we would request action when
|
|
1840
|
-
* we actually want to cancel
|
|
1841
|
-
*/
|
|
1842
|
-
stateAtInteraction = getState();
|
|
1843
|
-
}
|
|
1844
|
-
};
|
|
1845
|
-
const onchange = (e) => {
|
|
1846
|
-
if (stateAtInteraction !== undefined && getState() === stateAtInteraction) {
|
|
1847
|
-
stateAtInteraction = undefined;
|
|
1848
|
-
return;
|
|
1849
|
-
}
|
|
1850
|
-
callback(e);
|
|
1851
|
-
};
|
|
1852
|
-
input.addEventListener("input", oninput);
|
|
1853
|
-
input.addEventListener("keydown", onkeydown);
|
|
1854
|
-
input.addEventListener("change", onchange);
|
|
1855
|
-
addTeardown(() => {
|
|
1856
|
-
input.removeEventListener("input", oninput);
|
|
1857
|
-
input.removeEventListener("keydown", onkeydown);
|
|
1858
|
-
input.removeEventListener("change", onchange);
|
|
1859
|
-
});
|
|
1860
|
-
|
|
1861
|
-
{
|
|
1862
|
-
// Handle programmatic value changes that don't trigger browser change events
|
|
1863
|
-
//
|
|
1864
|
-
// Problem: When input values are set programmatically (not by user typing),
|
|
1865
|
-
// browsers don't fire the 'change' event. However, our application logic
|
|
1866
|
-
// still needs to detect these changes.
|
|
1867
|
-
//
|
|
1868
|
-
// Example scenario:
|
|
1869
|
-
// 1. User starts editing (letter key pressed, value set programmatically)
|
|
1870
|
-
// 2. User doesn't type anything additional (this is the key part)
|
|
1871
|
-
// 3. User clicks outside to finish editing
|
|
1872
|
-
// 4. Without this code, no change event would fire despite the fact that the input value did change from its original state
|
|
1873
|
-
//
|
|
1874
|
-
// This distinction is crucial because:
|
|
1875
|
-
//
|
|
1876
|
-
// - If the user typed additional text after the initial programmatic value,
|
|
1877
|
-
// the browser would fire change events normally
|
|
1878
|
-
// - But when they don't type anything else, the browser considers it as "no user interaction"
|
|
1879
|
-
// even though the programmatic initial value represents a meaningful change
|
|
1880
|
-
//
|
|
1881
|
-
// We achieve this by checking if the input value has changed between focus and blur without any user interaction
|
|
1882
|
-
// if yes we fire the callback because input value did change
|
|
1883
|
-
let stateAtStart = getState();
|
|
1884
|
-
let interacted = false;
|
|
1885
|
-
|
|
1886
|
-
const onfocus = () => {
|
|
1887
|
-
interacted = false;
|
|
1888
|
-
stateAtStart = getState();
|
|
1889
|
-
};
|
|
1890
|
-
const oninput = (e) => {
|
|
1891
|
-
if (!e.isTrusted) {
|
|
1892
|
-
// non trusted "input" events will be ignored by the browser when deciding to fire "change" event
|
|
1893
|
-
// we ignore them too
|
|
1894
|
-
return;
|
|
1895
|
-
}
|
|
1896
|
-
interacted = true;
|
|
1897
|
-
};
|
|
1898
|
-
const onblur = (e) => {
|
|
1899
|
-
if (interacted) {
|
|
1900
|
-
return;
|
|
1901
|
-
}
|
|
1902
|
-
if (stateAtStart === getState()) {
|
|
1903
|
-
return;
|
|
1904
|
-
}
|
|
1905
|
-
callback(e);
|
|
1906
|
-
};
|
|
1907
|
-
|
|
1908
|
-
input.addEventListener("focus", onfocus);
|
|
1909
|
-
input.addEventListener("input", oninput);
|
|
1910
|
-
input.addEventListener("blur", onblur);
|
|
1911
|
-
addTeardown(() => {
|
|
1912
|
-
input.removeEventListener("focus", onfocus);
|
|
1913
|
-
input.removeEventListener("input", oninput);
|
|
1914
|
-
input.removeEventListener("blur", onblur);
|
|
1915
|
-
});
|
|
1916
|
-
}
|
|
1917
|
-
|
|
1918
|
-
return teardown;
|
|
1919
|
-
};
|
|
1920
|
-
|
|
1921
|
-
const requestPseudoStateCheck = (element, detail) => {
|
|
1922
|
-
dispatchInternalCustomEvent(
|
|
1923
|
-
element,
|
|
1924
|
-
"navi_pseudo_state_request_check",
|
|
1925
|
-
detail,
|
|
1926
|
-
);
|
|
1927
|
-
// When a control has visible proxies mirroring its state (e.g. selectable
|
|
1928
|
-
// radio with `navi-control-proxy-for`), re-check them too so they stay in
|
|
1929
|
-
// sync with the real control.
|
|
1930
|
-
for (const proxy of findControlProxies(element)) {
|
|
1931
|
-
dispatchInternalCustomEvent(
|
|
1932
|
-
proxy,
|
|
1933
|
-
"navi_pseudo_state_request_check",
|
|
1934
|
-
detail,
|
|
1935
|
-
);
|
|
1936
|
-
}
|
|
1937
|
-
};
|
|
1938
|
-
/**
|
|
1939
|
-
* Called back whenever `element`'s pseudo state changes.
|
|
1940
|
-
*
|
|
1941
|
-
* Kept beside the element rather than announced to the DOM: an element that
|
|
1942
|
-
* changes state has at most one or two interested parties, each known by name
|
|
1943
|
-
* (the box drawing it, the accent color reading its computed style), and a
|
|
1944
|
-
* CustomEvent costs its allocation and its capture/bubble walk whether anyone
|
|
1945
|
-
* listens or not — paid on every element, on every state change. A lookup that
|
|
1946
|
-
* finds nothing costs nothing.
|
|
1947
|
-
*
|
|
1948
|
-
* @param {Element} element
|
|
1949
|
-
* @param {(pseudoState: object, oldPseudoState: object) => void} callback
|
|
1950
|
-
* @returns {() => void} teardown
|
|
1951
|
-
*/
|
|
1952
|
-
const subscribeToPseudoState = (element, callback) => {
|
|
1953
|
-
let subscriberSet = pseudoStateSubscriberSetWeakMap.get(element);
|
|
1954
|
-
if (!subscriberSet) {
|
|
1955
|
-
subscriberSet = new Set();
|
|
1956
|
-
pseudoStateSubscriberSetWeakMap.set(element, subscriberSet);
|
|
1957
|
-
}
|
|
1958
|
-
subscriberSet.add(callback);
|
|
1959
|
-
return () => {
|
|
1960
|
-
subscriberSet.delete(callback);
|
|
1961
|
-
};
|
|
1962
|
-
};
|
|
1963
|
-
const pseudoStateSubscriberSetWeakMap = new WeakMap();
|
|
1964
|
-
const notifyPseudoStateSubscribers = (element, value, oldValue) => {
|
|
1965
|
-
const subscriberSet = pseudoStateSubscriberSetWeakMap.get(element);
|
|
1966
|
-
if (!subscriberSet) {
|
|
1967
|
-
return;
|
|
1968
|
-
}
|
|
1969
|
-
for (const subscriber of subscriberSet) {
|
|
1970
|
-
subscriber(value, oldValue);
|
|
1971
|
-
}
|
|
1972
|
-
};
|
|
1973
|
-
|
|
1974
|
-
const PSEUDO_CLASSES = {};
|
|
1975
|
-
Object.assign(PSEUDO_CLASSES, {
|
|
1976
|
-
":valid": {
|
|
1977
|
-
attribute: "data-valid",
|
|
1978
|
-
test: (el) => el.matches(":valid"),
|
|
1979
|
-
},
|
|
1980
|
-
":invalid": {
|
|
1981
|
-
attribute: "data-invalid",
|
|
1982
|
-
test: (el) => el.matches(":invalid"),
|
|
1983
|
-
},
|
|
1984
|
-
":visited": {
|
|
1985
|
-
attribute: "data-visited",
|
|
1986
|
-
},
|
|
1987
|
-
// Written by whoever knows the current url — a Link from its href, a Button
|
|
1988
|
-
// from its route — so it lives here rather than with one of them.
|
|
1989
|
-
":-navi-href-current": {
|
|
1990
|
-
attribute: "data-href-current",
|
|
1991
|
-
},
|
|
1992
|
-
});
|
|
1993
|
-
const definePseudoClass = (pseudoClass, definition) => {
|
|
1994
|
-
PSEUDO_CLASSES[pseudoClass] = definition;
|
|
1995
|
-
};
|
|
1996
|
-
|
|
1997
|
-
// On touch devices (hover: none), browsers synthesize mouseenter/mouseleave
|
|
1998
|
-
// from touch events but never fire mouseleave when the finger lifts, leaving
|
|
1999
|
-
// el.matches(":hover") stuck at true. This causes hover styles (e.g. input
|
|
2000
|
-
// background highlight) to remain visible long after the user has stopped
|
|
2001
|
-
// touching the element. Checking (hover: hover) lets us skip hover tracking
|
|
2002
|
-
// entirely on touch-only devices where persistent hover makes no sense.
|
|
2003
|
-
const hoverSupported = window.matchMedia("(hover: hover)").matches;
|
|
2004
|
-
definePseudoClass(":hover", {
|
|
2005
|
-
attribute: "data-hover",
|
|
2006
|
-
setup: (el, callback) => {
|
|
2007
|
-
if (!hoverSupported) {
|
|
2008
|
-
return () => {};
|
|
2009
|
-
}
|
|
2010
|
-
const recheckProxy = (e) => {
|
|
2011
|
-
for (const proxy of findControlProxies(el)) {
|
|
2012
|
-
requestPseudoStateCheck(proxy, { event: e });
|
|
2013
|
-
}
|
|
2014
|
-
};
|
|
2015
|
-
const recheckProxyTarget = (e) => {
|
|
2016
|
-
const proxyTarget = findControlProxyTarget(el);
|
|
2017
|
-
if (proxyTarget) {
|
|
2018
|
-
requestPseudoStateCheck(proxyTarget, { event: e });
|
|
2019
|
-
}
|
|
2020
|
-
};
|
|
2021
|
-
let onmouseenter = (e) => {
|
|
2022
|
-
callback();
|
|
2023
|
-
recheckProxy(e);
|
|
2024
|
-
recheckProxyTarget(e);
|
|
2025
|
-
};
|
|
2026
|
-
let onmouseleave = (e) => {
|
|
2027
|
-
callback();
|
|
2028
|
-
recheckProxy(e);
|
|
2029
|
-
recheckProxyTarget(e);
|
|
2030
|
-
};
|
|
2031
|
-
|
|
2032
|
-
if (el.tagName === "LABEL") {
|
|
2033
|
-
// input.matches(":hover") is true when hovering the label
|
|
2034
|
-
// so when label is hovered/not hovered we need to recheck the input too
|
|
2035
|
-
const recheckInput = (e) => {
|
|
2036
|
-
if (el.htmlFor) {
|
|
2037
|
-
const input = document.getElementById(el.htmlFor);
|
|
2038
|
-
if (!input) {
|
|
2039
|
-
// cannot find the input for this label in the DOM
|
|
2040
|
-
return;
|
|
2041
|
-
}
|
|
2042
|
-
requestPseudoStateCheck(input, { event: e });
|
|
2043
|
-
return;
|
|
2044
|
-
}
|
|
2045
|
-
const input = el.querySelector("input, textarea, select");
|
|
2046
|
-
if (!input) {
|
|
2047
|
-
// label does not contain an input
|
|
2048
|
-
return;
|
|
2049
|
-
}
|
|
2050
|
-
requestPseudoStateCheck(input, { event: e });
|
|
2051
|
-
};
|
|
2052
|
-
const _onmouseenter = onmouseenter;
|
|
2053
|
-
onmouseenter = (e) => {
|
|
2054
|
-
recheckInput(e);
|
|
2055
|
-
_onmouseenter(e);
|
|
2056
|
-
};
|
|
2057
|
-
const _onmouseleave = onmouseleave;
|
|
2058
|
-
onmouseleave = (e) => {
|
|
2059
|
-
recheckInput(e);
|
|
2060
|
-
_onmouseleave(e);
|
|
2061
|
-
};
|
|
2062
|
-
}
|
|
2063
|
-
|
|
2064
|
-
el.addEventListener("mouseenter", onmouseenter);
|
|
2065
|
-
el.addEventListener("mouseleave", onmouseleave);
|
|
2066
|
-
return () => {
|
|
2067
|
-
el.removeEventListener("mouseenter", onmouseenter);
|
|
2068
|
-
el.removeEventListener("mouseleave", onmouseleave);
|
|
2069
|
-
};
|
|
2070
|
-
},
|
|
2071
|
-
test: (el) => {
|
|
2072
|
-
if (!hoverSupported) {
|
|
2073
|
-
return false;
|
|
2074
|
-
}
|
|
2075
|
-
if (el.matches(":hover")) {
|
|
2076
|
-
return true;
|
|
2077
|
-
}
|
|
2078
|
-
for (const proxy of findControlProxies(el)) {
|
|
2079
|
-
if (proxy.matches(":hover")) {
|
|
2080
|
-
return true;
|
|
2081
|
-
}
|
|
2082
|
-
}
|
|
2083
|
-
return false;
|
|
2084
|
-
},
|
|
2085
|
-
});
|
|
2086
|
-
definePseudoClass(":disabled", {
|
|
2087
|
-
attribute: "data-disabled",
|
|
2088
|
-
add: (el) => {
|
|
2089
|
-
if (
|
|
2090
|
-
el.tagName === "BUTTON" ||
|
|
2091
|
-
el.tagName === "INPUT" ||
|
|
2092
|
-
el.tagName === "SELECT" ||
|
|
2093
|
-
el.tagName === "TEXTAREA"
|
|
2094
|
-
) {
|
|
2095
|
-
el.disabled = true;
|
|
2096
|
-
}
|
|
2097
|
-
},
|
|
2098
|
-
remove: (el) => {
|
|
2099
|
-
if (
|
|
2100
|
-
el.tagName === "BUTTON" ||
|
|
2101
|
-
el.tagName === "INPUT" ||
|
|
2102
|
-
el.tagName === "SELECT" ||
|
|
2103
|
-
el.tagName === "TEXTAREA"
|
|
2104
|
-
) {
|
|
2105
|
-
el.disabled = false;
|
|
2106
|
-
}
|
|
2107
|
-
},
|
|
2108
|
-
});
|
|
2109
|
-
definePseudoClass(":read-only", {
|
|
2110
|
-
attribute: "data-readonly",
|
|
2111
|
-
add: (el) => {
|
|
2112
|
-
if (
|
|
2113
|
-
el.tagName === "INPUT" ||
|
|
2114
|
-
el.tagName === "SELECT" ||
|
|
2115
|
-
el.tagName === "TEXTAREA"
|
|
2116
|
-
) {
|
|
2117
|
-
if (el.type === "checkbox" || el.type === "radio") {
|
|
2118
|
-
// there is no readOnly for checkboxes/radios
|
|
2119
|
-
return;
|
|
2120
|
-
}
|
|
2121
|
-
// el.readOnly = true;
|
|
2122
|
-
}
|
|
2123
|
-
},
|
|
2124
|
-
remove: (el) => {
|
|
2125
|
-
if (
|
|
2126
|
-
el.tagName === "INPUT" ||
|
|
2127
|
-
el.tagName === "SELECT" ||
|
|
2128
|
-
el.tagName === "TEXTAREA"
|
|
2129
|
-
) {
|
|
2130
|
-
if (el.type === "checkbox" || el.type === "radio") {
|
|
2131
|
-
// there is no readOnly for checkboxes/radios
|
|
2132
|
-
return;
|
|
2133
|
-
}
|
|
2134
|
-
// el.readOnly = false;
|
|
2135
|
-
}
|
|
2136
|
-
},
|
|
2137
|
-
});
|
|
2138
|
-
definePseudoClass(":checked", {
|
|
2139
|
-
attribute: "data-checked",
|
|
2140
|
-
setup: (el, callback) => {
|
|
2141
|
-
if (el.type === "checkbox") {
|
|
2142
|
-
// Listen to user interactions
|
|
2143
|
-
el.addEventListener("input", callback);
|
|
2144
|
-
// Intercept programmatic changes to .checked property
|
|
2145
|
-
const originalDescriptor = Object.getOwnPropertyDescriptor(
|
|
2146
|
-
HTMLInputElement.prototype,
|
|
2147
|
-
"checked",
|
|
2148
|
-
);
|
|
2149
|
-
Object.defineProperty(el, "checked", {
|
|
2150
|
-
get: originalDescriptor.get,
|
|
2151
|
-
set(value) {
|
|
2152
|
-
originalDescriptor.set.call(this, value);
|
|
2153
|
-
callback();
|
|
2154
|
-
},
|
|
2155
|
-
configurable: true,
|
|
2156
|
-
});
|
|
2157
|
-
return () => {
|
|
2158
|
-
// Restore original property descriptor
|
|
2159
|
-
Object.defineProperty(el, "checked", originalDescriptor);
|
|
2160
|
-
el.removeEventListener("input", callback);
|
|
2161
|
-
};
|
|
2162
|
-
}
|
|
2163
|
-
if (el.type === "radio") {
|
|
2164
|
-
// Listen to changes on the radio
|
|
2165
|
-
el.addEventListener("input", callback);
|
|
2166
|
-
// Intercept programmatic changes to .checked property
|
|
2167
|
-
const originalDescriptor = Object.getOwnPropertyDescriptor(
|
|
2168
|
-
HTMLInputElement.prototype,
|
|
2169
|
-
"checked",
|
|
2170
|
-
);
|
|
2171
|
-
Object.defineProperty(el, "checked", {
|
|
2172
|
-
get: originalDescriptor.get,
|
|
2173
|
-
set(value) {
|
|
2174
|
-
originalDescriptor.set.call(this, value);
|
|
2175
|
-
callback();
|
|
2176
|
-
},
|
|
2177
|
-
configurable: true,
|
|
2178
|
-
});
|
|
2179
|
-
return () => {
|
|
2180
|
-
el.removeEventListener("input", callback);
|
|
2181
|
-
// Restore original property descriptor
|
|
2182
|
-
Object.defineProperty(el, "checked", originalDescriptor);
|
|
2183
|
-
};
|
|
2184
|
-
}
|
|
2185
|
-
if (el.tagName === "INPUT") {
|
|
2186
|
-
el.addEventListener("input", callback);
|
|
2187
|
-
return () => {
|
|
2188
|
-
el.removeEventListener("input", callback);
|
|
2189
|
-
};
|
|
2190
|
-
}
|
|
2191
|
-
return () => {};
|
|
2192
|
-
},
|
|
2193
|
-
test: (el) => el.matches(":checked"),
|
|
2194
|
-
});
|
|
2195
|
-
definePseudoClass(":active", {
|
|
2196
|
-
attribute: "data-active",
|
|
2197
|
-
setup: (el, callback) => {
|
|
2198
|
-
// I'ts recommended to use :-navi-pressed over :active for interactive elements.
|
|
2199
|
-
const onPointerDown = () => {
|
|
2200
|
-
const onRelease = () => {
|
|
2201
|
-
document.removeEventListener("pointercancel", onRelease, true);
|
|
2202
|
-
document.removeEventListener("pointerup", onRelease, true);
|
|
2203
|
-
callback();
|
|
2204
|
-
};
|
|
2205
|
-
document.addEventListener("pointercancel", onRelease, true);
|
|
2206
|
-
document.addEventListener("pointerup", onRelease, true);
|
|
2207
|
-
callback();
|
|
2208
|
-
};
|
|
2209
|
-
el.addEventListener("pointerdown", onPointerDown);
|
|
2210
|
-
return () => {
|
|
2211
|
-
el.removeEventListener("pointerdown", onPointerDown);
|
|
2212
|
-
};
|
|
2213
|
-
},
|
|
2214
|
-
test: (el) => el.matches(":active"),
|
|
2215
|
-
});
|
|
2216
|
-
|
|
2217
|
-
// The current input modality: true after a keyboard navigation key (arrow keys,
|
|
2218
|
-
// Escape, Enter, Ctrl, Alt, Shift, Space — Space ignored on editable fields),
|
|
2219
|
-
// false after a pointer interaction. Updated by the listeners in the
|
|
2220
|
-
// focus_classes block below. At module scope so isMatchingFocusVisible (used
|
|
2221
|
-
// here and in control_hooks.jsx) can read it.
|
|
2222
|
-
let keyboardNavigationUsed = false;
|
|
2223
|
-
|
|
2224
|
-
// HOW FOCUS-VISIBLE IS DECIDED (the details behind isMatchingFocusVisible)
|
|
2225
|
-
//
|
|
2226
|
-
// Two rules, depending on what `el` is:
|
|
2227
|
-
//
|
|
2228
|
-
// 1. An EDITABLE target (text-ish input, textarea, contenteditable — anything
|
|
2229
|
-
// whose whole point is keyboard input, see isEditableTarget) shows its ring
|
|
2230
|
-
// whenever it holds the focus, however the focus arrived — mouse,
|
|
2231
|
-
// programmatic, even a focus({ focusVisible: false }). Being focused, for
|
|
2232
|
-
// such a field, means being about to type, and that is what the ring
|
|
2233
|
-
// announces. Its check is on :focus rather than :focus-visible on purpose:
|
|
2234
|
-
// the native :focus-visible obeys the focusVisible option, which callers
|
|
2235
|
-
// set from the modality without knowing what they are focusing.
|
|
2236
|
-
//
|
|
2237
|
-
// 2. Anything else needs BOTH the native :focus-visible match AND the current
|
|
2238
|
-
// modality being keyboard (`keyboardNavigationUsed`).
|
|
2239
|
-
//
|
|
2240
|
-
// The native match alone cannot be trusted, because we routinely take the
|
|
2241
|
-
// pointer out of the browser's hands: a picker opens on mousedown and calls
|
|
2242
|
-
// preventDefault() so the browser does not move focus itself (see
|
|
2243
|
-
// picker_custom.jsx). The browser therefore never registers that a pointer
|
|
2244
|
-
// was what moved focus, and whatever focus-visible state the previous
|
|
2245
|
-
// keyboard interaction left behind stays true — even across the
|
|
2246
|
-
// programmatic focus({ focusVisible: false }) that follows. Close a picker
|
|
2247
|
-
// with Escape (ring on the trigger, rightly) and click it open again: the
|
|
2248
|
-
// popup's own focusable would come up ringed, from a mouse press.
|
|
2249
|
-
//
|
|
2250
|
-
// `keyboardNavigationUsed` has no such blind spot — it is set by navigation
|
|
2251
|
-
// keydowns and cleared by pointerdown, whatever anyone prevents afterwards
|
|
2252
|
-
// — so it is the authority for everything rule 1 does not cover.
|
|
2253
|
-
//
|
|
2254
|
-
// Ring INHERITANCE (a controlled element ringing because its aria-controls
|
|
2255
|
-
// controller is focused) stays gated on the keyboard modality even for an
|
|
2256
|
-
// editable controller — see hasIndirectFocus: propagating a ring promises
|
|
2257
|
-
// keyboard shortcuts will drive the controlled element, a promise that only
|
|
2258
|
-
// holds once a physical keyboard has actually been used.
|
|
2259
|
-
/**
|
|
2260
|
-
* Whether `el` should currently show a focus ring — the enriched
|
|
2261
|
-
* :focus-visible behind every [data-focus-visible] navi renders.
|
|
2262
|
-
* Use this instead of a bare el.matches(":focus-visible") wherever
|
|
2263
|
-
* focus-visible is evaluated; the comment above details the rules.
|
|
2264
|
-
* @param {Element} el
|
|
2265
|
-
* @returns {boolean}
|
|
2266
|
-
*/
|
|
2267
|
-
const isMatchingFocusVisible = (el) => {
|
|
2268
|
-
if (isEditableTarget(el)) {
|
|
2269
|
-
return el.matches(":focus");
|
|
2270
|
-
}
|
|
2271
|
-
if (!el.matches(":focus-visible")) {
|
|
2272
|
-
return false;
|
|
2273
|
-
}
|
|
2274
|
-
if (!keyboardNavigationUsed) {
|
|
2275
|
-
return false;
|
|
2276
|
-
}
|
|
2277
|
-
return true;
|
|
2278
|
-
};
|
|
2279
|
-
|
|
2280
|
-
// Elements that invite keyboard input: focusing one — even with the mouse —
|
|
2281
|
-
// means the user is about to type, so they always warrant a visible focus.
|
|
2282
|
-
// Also used to ignore the Space key as a navigation key while typing, and by
|
|
2283
|
-
// programmatic focus (focus_transfer.js) to pass focusVisible: true so the
|
|
2284
|
-
// native :focus-visible agrees with the ring rule 1 above draws.
|
|
2285
|
-
const EDITABLE_INPUT_TYPE_SET = new Set([
|
|
2286
|
-
"text",
|
|
2287
|
-
"search",
|
|
2288
|
-
"url",
|
|
2289
|
-
"email",
|
|
2290
|
-
"password",
|
|
2291
|
-
"tel",
|
|
2292
|
-
"number",
|
|
2293
|
-
"date",
|
|
2294
|
-
"time",
|
|
2295
|
-
"datetime-local",
|
|
2296
|
-
"month",
|
|
2297
|
-
"week",
|
|
2298
|
-
]);
|
|
2299
|
-
const isEditableTarget = (target) => {
|
|
2300
|
-
if (!target) {
|
|
2301
|
-
return false;
|
|
2302
|
-
}
|
|
2303
|
-
const tag = target.tagName;
|
|
2304
|
-
if (tag === "TEXTAREA") {
|
|
2305
|
-
return !target.readOnly;
|
|
2306
|
-
}
|
|
2307
|
-
if (tag === "INPUT") {
|
|
2308
|
-
if (!target.type || EDITABLE_INPUT_TYPE_SET.has(target.type)) {
|
|
2309
|
-
return !target.readOnly;
|
|
2310
|
-
}
|
|
2311
|
-
}
|
|
2312
|
-
if (target.isContentEditable) {
|
|
2313
|
-
return true;
|
|
2314
|
-
}
|
|
2315
|
-
return false;
|
|
2316
|
-
};
|
|
2317
|
-
|
|
2318
|
-
// The current modality, for code deciding whether something it is about to
|
|
2319
|
-
// focus should show a ring — a slide arriving, a popup opening. The question is
|
|
2320
|
-
// "was the user on the keyboard when this was asked for", which is what this
|
|
2321
|
-
// flag says; the element that happens to hold the focus right now says nothing
|
|
2322
|
-
// about it (it may have been focused programmatically, ring or no ring, by the
|
|
2323
|
-
// travel before this one).
|
|
2324
|
-
const isKeyboardModality = () => keyboardNavigationUsed;
|
|
2325
|
-
|
|
2326
|
-
{
|
|
2327
|
-
// We implement :focus and :focus-visible with enriched semantics:
|
|
2328
|
-
// an element is considered focused not only when it natively has focus, but also
|
|
2329
|
-
// when a "focus proxy" element has focus (e.g. a read-only range input delegates
|
|
2330
|
-
// focus to a sibling span) or when a controlling element has focus (e.g. a combobox
|
|
2331
|
-
// input with aria-controls pointing to a listbox — the listbox should appear focused
|
|
2332
|
-
// while the input is focused).
|
|
2333
|
-
//
|
|
2334
|
-
// We intentionally reuse the native :focus / :focus-visible names rather than
|
|
2335
|
-
// introducing new navi-specific pseudo-classes (e.g. :-navi-focus). This is a
|
|
2336
|
-
// deliberate exception: all existing CSS and code written as [data-focus] or
|
|
2337
|
-
// [data-focus-visible] automatically benefits from the enriched behavior without
|
|
2338
|
-
// any changes. A separate navi-specific class would require updating every
|
|
2339
|
-
// component.
|
|
2340
|
-
//
|
|
2341
|
-
// When a controller element (e.g. combobox input) gains or loses focus,
|
|
2342
|
-
// notify the elements it controls via aria-controls so they re-check their
|
|
2343
|
-
// focus state. `requestPseudoStateCheck` also re-checks the controlled
|
|
2344
|
-
// element's proxy (if any), so the visible proxy mirrors the hidden real
|
|
2345
|
-
// input's inherited focus.
|
|
2346
|
-
const notifyAriaControlled = (el, e) => {
|
|
2347
|
-
const controlledIds = el.getAttribute("aria-controls");
|
|
2348
|
-
if (!controlledIds) {
|
|
2349
|
-
return;
|
|
2350
|
-
}
|
|
2351
|
-
for (const id of controlledIds.split(" ")) {
|
|
2352
|
-
const controlled = document.getElementById(id);
|
|
2353
|
-
if (controlled) {
|
|
2354
|
-
requestPseudoStateCheck(controlled, { event: e });
|
|
2355
|
-
}
|
|
2356
|
-
}
|
|
2357
|
-
};
|
|
2358
|
-
// Tracks whether the user has pressed a keyboard navigation key (arrow keys,
|
|
2359
|
-
// Escape, Enter, Ctrl, Alt, Shift, Space) since the last pointer interaction.
|
|
2360
|
-
// Space is ignored when the target is an editable field.
|
|
2361
|
-
// This flag is used to gate focus-visible inheritance via aria-controls:
|
|
2362
|
-
// on mobile (or when the user hasn't used keyboard nav yet) an input that
|
|
2363
|
-
// controls a radio should not cause the radio to show a focus ring.
|
|
2364
|
-
// (Declared at module scope — see keyboardNavigationUsed above.)
|
|
2365
|
-
const NAVIGATION_KEY_SET = new Set([
|
|
2366
|
-
"ArrowUp",
|
|
2367
|
-
"ArrowDown",
|
|
2368
|
-
"ArrowLeft",
|
|
2369
|
-
"ArrowRight",
|
|
2370
|
-
"Escape",
|
|
2371
|
-
"Enter",
|
|
2372
|
-
"Control",
|
|
2373
|
-
"Alt",
|
|
2374
|
-
"Shift",
|
|
2375
|
-
" ",
|
|
2376
|
-
"Tab",
|
|
2377
|
-
]);
|
|
2378
|
-
document.addEventListener(
|
|
2379
|
-
"keydown",
|
|
2380
|
-
(e) => {
|
|
2381
|
-
if (!NAVIGATION_KEY_SET.has(e.key)) {
|
|
2382
|
-
return;
|
|
2383
|
-
}
|
|
2384
|
-
if (e.key === " " && isEditableTarget(e.target)) {
|
|
2385
|
-
return;
|
|
2386
|
-
}
|
|
2387
|
-
keyboardNavigationUsed = true;
|
|
2388
|
-
},
|
|
2389
|
-
{ capture: true },
|
|
2390
|
-
);
|
|
2391
|
-
document.addEventListener(
|
|
2392
|
-
"pointerdown",
|
|
2393
|
-
() => {
|
|
2394
|
-
keyboardNavigationUsed = false;
|
|
2395
|
-
},
|
|
2396
|
-
{ capture: true },
|
|
2397
|
-
);
|
|
2398
|
-
|
|
2399
|
-
// A keystroke flips keyboardNavigationUsed, which can turn :focus-visible on —
|
|
2400
|
-
// but only for the element that holds focus, directly or through aria-controls
|
|
2401
|
-
// / a proxy. Pressing a key cannot reveal a focus ring on an unfocused element.
|
|
2402
|
-
// So a single shared handler re-checks just that focus chain (the active
|
|
2403
|
-
// element, what it controls, and its proxy — the last two via
|
|
2404
|
-
// requestPseudoStateCheck / notifyAriaControlled). This runs in the bubble
|
|
2405
|
-
// phase, after the capture-phase listener above has updated the flag.
|
|
2406
|
-
//
|
|
2407
|
-
// The alternative — each registered :focus-visible element adding its own
|
|
2408
|
-
// document keydown listener that re-tests itself — makes one keypress cost
|
|
2409
|
-
// O(number-of-boxes) full-document [aria-controls] / proxy queries, since every
|
|
2410
|
-
// unfocused element falls through matches(":focus-visible") into hasIndirectFocus.
|
|
2411
|
-
const recheckFocusChainOnKey = (e) => {
|
|
2412
|
-
const active = document.activeElement;
|
|
2413
|
-
if (!active || active === document.body) {
|
|
2414
|
-
return;
|
|
2415
|
-
}
|
|
2416
|
-
requestPseudoStateCheck(active, { event: e });
|
|
2417
|
-
notifyAriaControlled(active, e);
|
|
2418
|
-
};
|
|
2419
|
-
document.addEventListener("keydown", recheckFocusChainOnKey);
|
|
2420
|
-
document.addEventListener("keyup", recheckFocusChainOnKey);
|
|
2421
|
-
|
|
2422
|
-
// Returns true when el holds focus indirectly — either because a controlling
|
|
2423
|
-
// element (aria-controls) has focus, or because el is a proxy whose target
|
|
2424
|
-
// is itself controlled by a focused element.
|
|
2425
|
-
const hasIndirectFocus = (el, { requireFocusVisible = false } = {}) => {
|
|
2426
|
-
// No ring inheritance without a keyboard: an editable target draws its own
|
|
2427
|
-
// ring on any focus (see isMatchingFocusVisible), but propagating that ring
|
|
2428
|
-
// to a controlled element (aria-controls) is a promise that keyboard
|
|
2429
|
-
// shortcuts will drive it — a promise that holds only once a physical
|
|
2430
|
-
// keyboard has actually been used. On touch devices the flag stays false
|
|
2431
|
-
// and a focused search input keeps its ring to itself.
|
|
2432
|
-
if (requireFocusVisible && !keyboardNavigationUsed) {
|
|
2433
|
-
return false;
|
|
2434
|
-
}
|
|
2435
|
-
// A controller/proxy counts as focused for inheritance via the same rule
|
|
2436
|
-
// used everywhere: :focus for plain inheritance, isMatchingFocusVisible for
|
|
2437
|
-
// the focus-visible variant (so a mouse-focused controller doesn't propagate
|
|
2438
|
-
// a ring).
|
|
2439
|
-
const isFocusedTarget = (target) =>
|
|
2440
|
-
requireFocusVisible
|
|
2441
|
-
? isMatchingFocusVisible(target)
|
|
2442
|
-
: target.matches(":focus");
|
|
2443
|
-
// Both branches of isFocusedTarget rest on :focus / :focus-visible, and only
|
|
2444
|
-
// one element in the document can match those: document.activeElement. So
|
|
2445
|
-
// the single controller worth testing is known upfront — asking the document
|
|
2446
|
-
// for every [aria-controls] would collect candidates that cannot qualify,
|
|
2447
|
-
// once per element and again on every re-check, on a document each new
|
|
2448
|
-
// element makes bigger.
|
|
2449
|
-
const isControlledBy = (target) => {
|
|
2450
|
-
const id = target.id;
|
|
2451
|
-
if (!id) {
|
|
2452
|
-
return false;
|
|
2453
|
-
}
|
|
2454
|
-
const activeElement = document.activeElement;
|
|
2455
|
-
if (!activeElement || activeElement === document.body) {
|
|
2456
|
-
return false;
|
|
2457
|
-
}
|
|
2458
|
-
if (!activeElement.matches(`[aria-controls~="${id}"]`)) {
|
|
2459
|
-
return false;
|
|
2460
|
-
}
|
|
2461
|
-
// A controller inside the element it controls means focus is already
|
|
2462
|
-
// native (:focus-within) — nothing to inherit.
|
|
2463
|
-
if (target.contains(activeElement)) {
|
|
2464
|
-
return false;
|
|
2465
|
-
}
|
|
2466
|
-
return isFocusedTarget(activeElement);
|
|
2467
|
-
};
|
|
2468
|
-
if (isControlledBy(el)) {
|
|
2469
|
-
return true;
|
|
2470
|
-
}
|
|
2471
|
-
const proxyTarget = findControlProxyTarget(el);
|
|
2472
|
-
if (proxyTarget) {
|
|
2473
|
-
if (isFocusedTarget(proxyTarget)) {
|
|
2474
|
-
return true;
|
|
2475
|
-
}
|
|
2476
|
-
if (isControlledBy(proxyTarget)) {
|
|
2477
|
-
return true;
|
|
2478
|
-
}
|
|
2479
|
-
}
|
|
2480
|
-
return false;
|
|
2481
|
-
};
|
|
2482
|
-
|
|
2483
|
-
// One registration per element for everything focus-related, shared by
|
|
2484
|
-
// :focus, :focus-visible and :focus-within: they all react to the same
|
|
2485
|
-
// focusin/focusout, and they all re-check through the same callback (see
|
|
2486
|
-
// initPseudoStyles), which the Set turns into one call.
|
|
2487
|
-
const focusTrackingWeakMap = new WeakMap();
|
|
2488
|
-
const trackFocus = (el, callback) => {
|
|
2489
|
-
let tracking = focusTrackingWeakMap.get(el);
|
|
2490
|
-
if (!tracking) {
|
|
2491
|
-
const callbackSet = new Set();
|
|
2492
|
-
const onFocusChange = (e) => {
|
|
2493
|
-
for (const trackedCallback of callbackSet) {
|
|
2494
|
-
trackedCallback();
|
|
2495
|
-
}
|
|
2496
|
-
notifyAriaControlled(el, e);
|
|
2497
|
-
};
|
|
2498
|
-
el.addEventListener("focusin", onFocusChange);
|
|
2499
|
-
el.addEventListener("focusout", onFocusChange);
|
|
2500
|
-
tracking = {
|
|
2501
|
-
callbackSet,
|
|
2502
|
-
teardown: () => {
|
|
2503
|
-
el.removeEventListener("focusin", onFocusChange);
|
|
2504
|
-
el.removeEventListener("focusout", onFocusChange);
|
|
2505
|
-
focusTrackingWeakMap.delete(el);
|
|
2506
|
-
},
|
|
2507
|
-
};
|
|
2508
|
-
focusTrackingWeakMap.set(el, tracking);
|
|
2509
|
-
observeAriaControls();
|
|
2510
|
-
}
|
|
2511
|
-
tracking.callbackSet.add(callback);
|
|
2512
|
-
return () => {
|
|
2513
|
-
tracking.callbackSet.delete(callback);
|
|
2514
|
-
if (tracking.callbackSet.size === 0) {
|
|
2515
|
-
tracking.teardown();
|
|
2516
|
-
}
|
|
2517
|
-
};
|
|
2518
|
-
};
|
|
2519
|
-
// When aria-controls changes on a focused element, what it used to control
|
|
2520
|
-
// and what it controls now both re-check their inherited focus. One observer
|
|
2521
|
-
// on the document rather than one per tracked element: the attribute changes
|
|
2522
|
-
// rarely, on few elements, while thousands are tracked — and it can be set
|
|
2523
|
-
// after the element was, so "has it at setup" would miss it.
|
|
2524
|
-
let ariaControlsObserver = null;
|
|
2525
|
-
const observeAriaControls = () => {
|
|
2526
|
-
if (ariaControlsObserver) {
|
|
2527
|
-
return;
|
|
2528
|
-
}
|
|
2529
|
-
ariaControlsObserver = new MutationObserver((mutations) => {
|
|
2530
|
-
for (const mutation of mutations) {
|
|
2531
|
-
const el = mutation.target;
|
|
2532
|
-
if (!focusTrackingWeakMap.has(el)) {
|
|
2533
|
-
continue;
|
|
2534
|
-
}
|
|
2535
|
-
if (!el.matches(":focus-within")) {
|
|
2536
|
-
continue;
|
|
2537
|
-
}
|
|
2538
|
-
const oldIds = (mutation.oldValue || "").split(" ").filter(Boolean);
|
|
2539
|
-
for (const id of oldIds) {
|
|
2540
|
-
const controlled = document.getElementById(id);
|
|
2541
|
-
if (controlled) {
|
|
2542
|
-
requestPseudoStateCheck(controlled, {});
|
|
2543
|
-
}
|
|
2544
|
-
}
|
|
2545
|
-
notifyAriaControlled(el, {});
|
|
2546
|
-
}
|
|
2547
|
-
});
|
|
2548
|
-
ariaControlsObserver.observe(document.documentElement, {
|
|
2549
|
-
subtree: true,
|
|
2550
|
-
attributes: true,
|
|
2551
|
-
attributeFilter: ["aria-controls"],
|
|
2552
|
-
attributeOldValue: true,
|
|
2553
|
-
});
|
|
2554
|
-
};
|
|
2555
|
-
|
|
2556
|
-
definePseudoClass(":focus", {
|
|
2557
|
-
attribute: "data-focus",
|
|
2558
|
-
setup: trackFocus,
|
|
2559
|
-
test: (el) => {
|
|
2560
|
-
if (el.matches(":focus")) {
|
|
2561
|
-
return true;
|
|
2562
|
-
}
|
|
2563
|
-
if (hasIndirectFocus(el)) {
|
|
2564
|
-
return true;
|
|
2565
|
-
}
|
|
2566
|
-
return false;
|
|
2567
|
-
},
|
|
2568
|
-
});
|
|
2569
|
-
definePseudoClass(":focus-visible", {
|
|
2570
|
-
attribute: "data-focus-visible",
|
|
2571
|
-
// No per-element keydown/keyup listener: the shared recheckFocusChainOnKey
|
|
2572
|
-
// handler re-checks the focused element (the only one a keystroke can turn
|
|
2573
|
-
// focus-visible) so a keypress stays O(1), not O(number-of-boxes).
|
|
2574
|
-
setup: trackFocus,
|
|
2575
|
-
test: (el) => {
|
|
2576
|
-
if (isMatchingFocusVisible(el)) {
|
|
2577
|
-
return true;
|
|
2578
|
-
}
|
|
2579
|
-
if (hasIndirectFocus(el, { requireFocusVisible: true })) {
|
|
2580
|
-
return true;
|
|
2581
|
-
}
|
|
2582
|
-
return false;
|
|
2583
|
-
},
|
|
2584
|
-
});
|
|
2585
|
-
definePseudoClass(":focus-within", {
|
|
2586
|
-
attribute: "data-focus-within",
|
|
2587
|
-
setup: trackFocus,
|
|
2588
|
-
test: (el) => {
|
|
2589
|
-
if (el.matches(":focus-within")) {
|
|
2590
|
-
return true;
|
|
2591
|
-
}
|
|
2592
|
-
if (hasIndirectFocus(el)) {
|
|
2593
|
-
return true;
|
|
2594
|
-
}
|
|
2595
|
-
if (el.contains(document.activeElement)) {
|
|
2596
|
-
// for some reason :focus-within sometimes is false while focus is within...
|
|
2597
|
-
// (popover with chrome for some reason)
|
|
2598
|
-
return true;
|
|
2599
|
-
}
|
|
2600
|
-
return false;
|
|
2601
|
-
},
|
|
2602
|
-
});
|
|
2603
|
-
}
|
|
2604
|
-
|
|
2605
|
-
Object.assign(PSEUDO_CLASSES, {
|
|
2606
|
-
":-navi-pointed": {
|
|
2607
|
-
attribute: "data-pointed",
|
|
2608
|
-
},
|
|
2609
|
-
":-navi-pointed-by-mouse": {
|
|
2610
|
-
attribute: "data-pointed-by-mouse",
|
|
2611
|
-
},
|
|
2612
|
-
":-navi-pointed-by-keyboard": {
|
|
2613
|
-
attribute: "data-pointed-by-keyboard",
|
|
2614
|
-
},
|
|
2615
|
-
":-navi-pointed-by-proxy": {
|
|
2616
|
-
attribute: "data-pointed-by-proxy",
|
|
2617
|
-
},
|
|
2618
|
-
":-navi-selected": {
|
|
2619
|
-
attribute: "data-selected",
|
|
2620
|
-
},
|
|
2621
|
-
":-navi-loading": {
|
|
2622
|
-
attribute: "data-loading",
|
|
2623
|
-
},
|
|
2624
|
-
":-navi-status-info": {
|
|
2625
|
-
attribute: "data-status-info",
|
|
2626
|
-
},
|
|
2627
|
-
":-navi-status-success": {
|
|
2628
|
-
attribute: "data-status-success",
|
|
2629
|
-
},
|
|
2630
|
-
":-navi-status-warning": {
|
|
2631
|
-
attribute: "data-status-warning",
|
|
2632
|
-
},
|
|
2633
|
-
":-navi-status-error": {
|
|
2634
|
-
attribute: "data-status-error",
|
|
2635
|
-
},
|
|
2636
|
-
":-navi-expanded": {
|
|
2637
|
-
attribute: "data-expanded",
|
|
2638
|
-
},
|
|
2639
|
-
":-navi-void": {
|
|
2640
|
-
attribute: "data-void",
|
|
2641
|
-
},
|
|
2642
|
-
"::highlight": {},
|
|
2643
|
-
});
|
|
2644
|
-
definePseudoClass(":-navi-has-value", {
|
|
2645
|
-
attribute: "data-has-value",
|
|
2646
|
-
setup: (el, callback) => {
|
|
2647
|
-
const controlHost = findControlHost(el) || el;
|
|
2648
|
-
return addInputEffect(controlHost, callback);
|
|
2649
|
-
},
|
|
2650
|
-
test: (el) => {
|
|
2651
|
-
if (isControlHost(el)) {
|
|
2652
|
-
const uiState = getUIStateFromElement(el);
|
|
2653
|
-
if (uiState === undefined || uiState === "") {
|
|
2654
|
-
return false;
|
|
2655
|
-
}
|
|
2656
|
-
return true;
|
|
2657
|
-
}
|
|
2658
|
-
if (el.value === "") {
|
|
2659
|
-
return false;
|
|
2660
|
-
}
|
|
2661
|
-
return true;
|
|
2662
|
-
},
|
|
2663
|
-
});
|
|
2664
|
-
{
|
|
2665
|
-
const pressedElements = new WeakSet();
|
|
2666
|
-
definePseudoClass(":-navi-pressed", {
|
|
2667
|
-
attribute: "data-pressed",
|
|
2668
|
-
setup: (el, callback) => {
|
|
2669
|
-
// Prefer :-navi-pressed over :active for interactive elements because:
|
|
2670
|
-
// - :active only tracks the primary (left) button; right-click and touch
|
|
2671
|
-
// long-press do not trigger :active reliably across browsers.
|
|
2672
|
-
// - :-navi-pressed explicitly ignores non-primary buttons (e.g. right-click)
|
|
2673
|
-
// and correctly clears pressed state when a context menu opens on long-press,
|
|
2674
|
-
// which would otherwise leave the element stuck in a pressed appearance.
|
|
2675
|
-
|
|
2676
|
-
// Note: it might be tempting to use el.setPointerCapture() here so that pointerup
|
|
2677
|
-
// always fires on el regardless of where the pointer is released. However,
|
|
2678
|
-
// pointer capture routes all subsequent pointer events to the capturing element,
|
|
2679
|
-
// which means any other element in the tree that expects to receive pointerup,
|
|
2680
|
-
// mouseup, click, etc. after a pointerdown will silently not get them.
|
|
2681
|
-
// For example a <label> that reacts to mousedown + click, or a third-party
|
|
2682
|
-
// library that attaches its own listeners, would break because an ancestor
|
|
2683
|
-
// grabbed the pointer out from under them.
|
|
2684
|
-
// To avoid forcing every such element to declare an opt-out attribute
|
|
2685
|
-
// (e.g. navi-own-pointer-capture) we simply listen on document instead,
|
|
2686
|
-
// which is safe and does not interfere with anyone else's event flow.
|
|
2687
|
-
const onPointerDown = (e) => {
|
|
2688
|
-
if (e.button !== 0) {
|
|
2689
|
-
// only left pointer (mouse left click, touch, pen)
|
|
2690
|
-
return;
|
|
2691
|
-
}
|
|
2692
|
-
pressedElements.add(el);
|
|
2693
|
-
const onRelease = () => {
|
|
2694
|
-
pressedElements.delete(el);
|
|
2695
|
-
document.removeEventListener("pointercancel", onRelease, true);
|
|
2696
|
-
document.removeEventListener("pointerup", onRelease, true);
|
|
2697
|
-
document.removeEventListener("contextmenu", onContextMenu, true);
|
|
2698
|
-
callback();
|
|
2699
|
-
};
|
|
2700
|
-
const onContextMenu = (e) => {
|
|
2701
|
-
// On touch devices, a long-press triggers the context menu.
|
|
2702
|
-
// If the context menu is not prevented, it means it will open and the
|
|
2703
|
-
// pointer events (pointerup, lostpointercapture) won't fire normally,
|
|
2704
|
-
// leaving the element stuck in pressed state. We clear it manually.
|
|
2705
|
-
// e.button === -1 means the event was synthesized from a long-press (not a real mouse click).
|
|
2706
|
-
if (e.button === -1 && !e.defaultPrevented) {
|
|
2707
|
-
pressedElements.delete(el);
|
|
2708
|
-
document.removeEventListener("pointercancel", onRelease, true);
|
|
2709
|
-
document.removeEventListener("pointerup", onRelease, true);
|
|
2710
|
-
document.removeEventListener("contextmenu", onContextMenu, true);
|
|
2711
|
-
callback();
|
|
2712
|
-
}
|
|
2713
|
-
};
|
|
2714
|
-
document.addEventListener("pointercancel", onRelease, true);
|
|
2715
|
-
document.addEventListener("pointerup", onRelease, true);
|
|
2716
|
-
document.addEventListener("contextmenu", onContextMenu, true);
|
|
2717
|
-
callback();
|
|
2718
|
-
};
|
|
2719
|
-
el.addEventListener("pointerdown", onPointerDown);
|
|
2720
|
-
return () => {
|
|
2721
|
-
el.removeEventListener("pointerdown", onPointerDown);
|
|
2722
|
-
pressedElements.delete(el);
|
|
2723
|
-
};
|
|
2724
|
-
},
|
|
2725
|
-
test: (el) => pressedElements.has(el),
|
|
2726
|
-
});
|
|
2727
|
-
}
|
|
2728
|
-
|
|
2729
|
-
{
|
|
2730
|
-
definePseudoClass(":-navi-drag-grabbed", {
|
|
2731
|
-
attribute: "navi-drag-grabbed",
|
|
2732
|
-
setup: (el, callback) => {
|
|
2733
|
-
const onGrab = () => {
|
|
2734
|
-
callback();
|
|
2735
|
-
const onRelease = () => {
|
|
2736
|
-
el.removeEventListener("navi_drag_release", onRelease);
|
|
2737
|
-
callback();
|
|
2738
|
-
};
|
|
2739
|
-
el.addEventListener("navi_drag_release", onRelease);
|
|
2740
|
-
};
|
|
2741
|
-
el.addEventListener("navi_drag_grab", onGrab);
|
|
2742
|
-
return () => {
|
|
2743
|
-
el.removeEventListener("navi_drag_grab", onGrab);
|
|
2744
|
-
};
|
|
2745
|
-
},
|
|
2746
|
-
test: (el) => el.hasAttribute("data-drag-grabbed"),
|
|
2747
|
-
});
|
|
2748
|
-
definePseudoClass(":-navi-dragging", {
|
|
2749
|
-
attribute: "navi-dragging",
|
|
2750
|
-
setup: (el, callback) => {
|
|
2751
|
-
const onStart = () => {
|
|
2752
|
-
callback();
|
|
2753
|
-
const onRelease = () => {
|
|
2754
|
-
el.removeEventListener("navi_drag_release", onRelease);
|
|
2755
|
-
callback();
|
|
2756
|
-
};
|
|
2757
|
-
el.addEventListener("navi_drag_release", onRelease);
|
|
2758
|
-
};
|
|
2759
|
-
el.addEventListener("navi_drag_start", onStart);
|
|
2760
|
-
return () => {
|
|
2761
|
-
el.removeEventListener("navi_drag_start", onStart);
|
|
2762
|
-
};
|
|
2763
|
-
},
|
|
2764
|
-
test: (el) => el.hasAttribute("data-dragging"),
|
|
2765
|
-
});
|
|
2766
|
-
}
|
|
2767
|
-
|
|
2768
|
-
const EMPTY_STATE = {};
|
|
2769
|
-
const elementToImpactWeakMap = new WeakMap();
|
|
2770
|
-
const initPseudoStyles = (
|
|
2771
|
-
element,
|
|
2772
|
-
{
|
|
2773
|
-
pseudoClasses,
|
|
2774
|
-
pseudoState, // ":disabled", ":read-only", ":-navi-loading", etc...
|
|
2775
|
-
effect,
|
|
2776
|
-
elementToImpact = element,
|
|
2777
|
-
elementListeningPseudoState,
|
|
2778
|
-
},
|
|
2779
|
-
) => {
|
|
2780
|
-
elementToImpactWeakMap.set(element, elementToImpact);
|
|
2781
|
-
if (elementListeningPseudoState === element) {
|
|
2782
|
-
console.warn(
|
|
2783
|
-
`elementListeningPseudoState should not be the same as element to avoid infinite loop`,
|
|
2784
|
-
);
|
|
2785
|
-
elementListeningPseudoState = null;
|
|
2786
|
-
}
|
|
2787
|
-
|
|
2788
|
-
const proxyTarget = findControlProxyTarget(element);
|
|
2789
|
-
|
|
2790
|
-
const onStateChange = (value, oldValue) => {
|
|
2791
|
-
effect?.(value, oldValue);
|
|
2792
|
-
if (elementListeningPseudoState) {
|
|
2793
|
-
notifyPseudoStateSubscribers(
|
|
2794
|
-
elementListeningPseudoState,
|
|
2795
|
-
value,
|
|
2796
|
-
oldValue,
|
|
2797
|
-
);
|
|
2798
|
-
}
|
|
2799
|
-
// When this element's state changes, notify any proxy element that mirrors it
|
|
2800
|
-
// so it can re-check and visually reflect the new state.
|
|
2801
|
-
for (const proxy of findControlProxies(element)) {
|
|
2802
|
-
requestPseudoStateCheck(proxy, {});
|
|
2803
|
-
}
|
|
2804
|
-
};
|
|
2805
|
-
|
|
2806
|
-
if (!pseudoClasses || pseudoClasses.length === 0) {
|
|
2807
|
-
onStateChange(EMPTY_STATE);
|
|
2808
|
-
return () => {};
|
|
2809
|
-
}
|
|
2810
|
-
|
|
2811
|
-
const [teardown, addTeardown] = createPubSub();
|
|
2812
|
-
|
|
2813
|
-
let state;
|
|
2814
|
-
const checkPseudoClasses = () => {
|
|
2815
|
-
let someChange = false;
|
|
2816
|
-
const currentState = {};
|
|
2817
|
-
for (const pseudoClass of pseudoClasses) {
|
|
2818
|
-
const pseudoClassDefinition = PSEUDO_CLASSES[pseudoClass];
|
|
2819
|
-
if (!pseudoClassDefinition) {
|
|
2820
|
-
console.warn(`Unknown pseudo class: ${pseudoClass}`);
|
|
2821
|
-
continue;
|
|
2822
|
-
}
|
|
2823
|
-
let currentValue;
|
|
2824
|
-
if (
|
|
2825
|
-
pseudoState &&
|
|
2826
|
-
Object.hasOwn(pseudoState, pseudoClass) &&
|
|
2827
|
-
pseudoState[pseudoClass] !== undefined
|
|
2828
|
-
) {
|
|
2829
|
-
currentValue = pseudoState[pseudoClass];
|
|
2830
|
-
} else {
|
|
2831
|
-
const { test } = pseudoClassDefinition;
|
|
2832
|
-
if (test) {
|
|
2833
|
-
currentValue = test(element, pseudoState);
|
|
2834
|
-
}
|
|
2835
|
-
}
|
|
2836
|
-
// If this element is a proxy for another (navi-control-proxy-for="targetId"),
|
|
2837
|
-
// inherit the target's active pseudo-state when the element itself isn't in that state.
|
|
2838
|
-
// We check the target's elementToImpact (not the target itself) because the
|
|
2839
|
-
// data-* attribute may be set on a different element (e.g. pseudoStateSelector).
|
|
2840
|
-
if (!currentValue && proxyTarget) {
|
|
2841
|
-
const { attribute } = pseudoClassDefinition;
|
|
2842
|
-
if (attribute) {
|
|
2843
|
-
const targetElementToImpact =
|
|
2844
|
-
elementToImpactWeakMap.get(proxyTarget) || proxyTarget;
|
|
2845
|
-
if (targetElementToImpact.hasAttribute(attribute)) {
|
|
2846
|
-
currentValue = true;
|
|
2847
|
-
}
|
|
2848
|
-
}
|
|
2849
|
-
}
|
|
2850
|
-
currentState[pseudoClass] = currentValue;
|
|
2851
|
-
const oldValue = state ? state[pseudoClass] : undefined;
|
|
2852
|
-
if (oldValue !== currentValue || !state) {
|
|
2853
|
-
someChange = true;
|
|
2854
|
-
const { attribute, add, remove } = pseudoClassDefinition;
|
|
2855
|
-
if (currentValue) {
|
|
2856
|
-
if (attribute) {
|
|
2857
|
-
elementToImpact.setAttribute(attribute, "");
|
|
2858
|
-
}
|
|
2859
|
-
add?.(element);
|
|
2860
|
-
} else {
|
|
2861
|
-
if (attribute) {
|
|
2862
|
-
elementToImpact.removeAttribute(attribute);
|
|
2863
|
-
}
|
|
2864
|
-
remove?.(element);
|
|
2865
|
-
}
|
|
2866
|
-
}
|
|
2867
|
-
}
|
|
2868
|
-
if (!someChange) {
|
|
2869
|
-
return;
|
|
2870
|
-
}
|
|
2871
|
-
const oldState = state;
|
|
2872
|
-
state = currentState;
|
|
2873
|
-
onStateChange(state, oldState);
|
|
2874
|
-
};
|
|
2875
|
-
|
|
2876
|
-
addTeardown(
|
|
2877
|
-
subscribeToPseudoState(element, (pseudoState, oldPseudoState) => {
|
|
2878
|
-
state = pseudoState;
|
|
2879
|
-
onStateChange(state, oldPseudoState);
|
|
2880
|
-
}),
|
|
2881
|
-
);
|
|
2882
|
-
// One function for every way a re-check can be asked, so that setups
|
|
2883
|
-
// registering it side by side (the focus tracking, for one) hold the same
|
|
2884
|
-
// callback and call it once.
|
|
2885
|
-
const requestCheck = () => {
|
|
2886
|
-
checkPseudoClasses();
|
|
2887
|
-
};
|
|
2888
|
-
element.addEventListener("navi_pseudo_state_request_check", requestCheck);
|
|
2889
|
-
addTeardown(() => {
|
|
2890
|
-
element.removeEventListener(
|
|
2891
|
-
"navi_pseudo_state_request_check",
|
|
2892
|
-
requestCheck,
|
|
2893
|
-
);
|
|
2894
|
-
});
|
|
2895
|
-
|
|
2896
|
-
for (const pseudoClass of pseudoClasses) {
|
|
2897
|
-
const pseudoClassDefinition = PSEUDO_CLASSES[pseudoClass];
|
|
2898
|
-
if (!pseudoClassDefinition) {
|
|
2899
|
-
console.warn(`Unknown pseudo class: ${pseudoClass}`);
|
|
2900
|
-
continue;
|
|
2901
|
-
}
|
|
2902
|
-
const { setup } = pseudoClassDefinition;
|
|
2903
|
-
if (setup) {
|
|
2904
|
-
const cleanup = setup(element, requestCheck);
|
|
2905
|
-
addTeardown(cleanup);
|
|
2906
|
-
}
|
|
2907
|
-
}
|
|
2908
|
-
checkPseudoClasses();
|
|
2909
|
-
{
|
|
2910
|
-
// just in case + catch use forcing them in chrome devtools
|
|
2911
|
-
addTeardown(pollInDev(requestCheck));
|
|
2912
|
-
}
|
|
2913
|
-
|
|
2914
|
-
return teardown;
|
|
2915
|
-
};
|
|
2916
|
-
|
|
2917
|
-
const applyStyle = (
|
|
2918
|
-
element,
|
|
2919
|
-
style,
|
|
2920
|
-
pseudoState,
|
|
2921
|
-
pseudoNamedStyles,
|
|
2922
|
-
preventInitialTransition,
|
|
2923
|
-
) => {
|
|
2924
|
-
if (!element) {
|
|
2925
|
-
return;
|
|
2926
|
-
}
|
|
2927
|
-
const styleToApply = getStyleToApply(style, pseudoState, pseudoNamedStyles);
|
|
2928
|
-
// The same object comes back for every state change of a box whose inline
|
|
2929
|
-
// style has no pseudo entry: nothing to write.
|
|
2930
|
-
if (appliedStyleWeakMap.get(element) === styleToApply) {
|
|
2931
|
-
return;
|
|
2932
|
-
}
|
|
2933
|
-
updateStyle(element, styleToApply, preventInitialTransition);
|
|
2934
|
-
};
|
|
2935
|
-
|
|
2936
|
-
const PSEUDO_STATE_DEFAULT = {};
|
|
2937
|
-
const PSEUDO_NAMED_STYLES_DEFAULT = {};
|
|
2938
|
-
const getStyleToApply = (styles, pseudoState, pseudoNamedStyles) => {
|
|
2939
|
-
if (
|
|
2940
|
-
!pseudoState ||
|
|
2941
|
-
pseudoState === PSEUDO_STATE_DEFAULT ||
|
|
2942
|
-
!pseudoNamedStyles ||
|
|
2943
|
-
pseudoNamedStyles === PSEUDO_NAMED_STYLES_DEFAULT
|
|
2944
|
-
) {
|
|
2945
|
-
return styles;
|
|
2946
|
-
}
|
|
2947
|
-
let style = styles;
|
|
2948
|
-
for (const pseudoKey of Object.keys(pseudoNamedStyles)) {
|
|
2949
|
-
if (!hasEveryState(pseudoState, getPseudoKeyRequiredStates(pseudoKey))) {
|
|
2950
|
-
continue;
|
|
2951
|
-
}
|
|
2952
|
-
if (style === styles) {
|
|
2953
|
-
style = { ...styles };
|
|
2954
|
-
}
|
|
2955
|
-
// Both sides are already normalized for CSS by the box; only the
|
|
2956
|
-
// properties that compose (a press scale on top of a translate) go through
|
|
2957
|
-
// a merge, the rest is a plain override.
|
|
2958
|
-
const styleToAdd = pseudoNamedStyles[pseudoKey];
|
|
2959
|
-
for (const key of Object.keys(styleToAdd)) {
|
|
2960
|
-
const value = styleToAdd[key];
|
|
2961
|
-
if (value === undefined) {
|
|
2962
|
-
continue;
|
|
2963
|
-
}
|
|
2964
|
-
if (key === "transform" || key === "willChange") {
|
|
2965
|
-
style[key] = mergeOneStyle(style[key], value, key, "css");
|
|
2966
|
-
} else {
|
|
2967
|
-
style[key] = value;
|
|
2968
|
-
}
|
|
2969
|
-
}
|
|
2970
|
-
}
|
|
2971
|
-
return style;
|
|
2972
|
-
};
|
|
2973
|
-
|
|
2974
|
-
// The state names a pseudo key asks for, parsed once: the same few keys come
|
|
2975
|
-
// back on every state change of every box that has them. "::x" alone always
|
|
2976
|
-
// matches; "::x:a:b" and ":a:b" ask for ":a" and ":b" — the state keys as
|
|
2977
|
-
// checkPseudoClasses writes them, colon included.
|
|
2978
|
-
const pseudoKeyRequiredStatesMap = new Map();
|
|
2979
|
-
const getPseudoKeyRequiredStates = (pseudoKey) => {
|
|
2980
|
-
const cached = pseudoKeyRequiredStatesMap.get(pseudoKey);
|
|
2981
|
-
if (cached) {
|
|
2982
|
-
return cached;
|
|
2983
|
-
}
|
|
2984
|
-
let requiredStates;
|
|
2985
|
-
if (pseudoKey.startsWith("::")) {
|
|
2986
|
-
const nextColonIndex = pseudoKey.indexOf(":", 2);
|
|
2987
|
-
requiredStates =
|
|
2988
|
-
nextColonIndex === -1
|
|
2989
|
-
? []
|
|
2990
|
-
: getPseudoKeyRequiredStates(pseudoKey.slice(nextColonIndex));
|
|
2991
|
-
} else {
|
|
2992
|
-
const nextColonIndex = pseudoKey.indexOf(":", 1);
|
|
2993
|
-
requiredStates =
|
|
2994
|
-
nextColonIndex === -1
|
|
2995
|
-
? [pseudoKey]
|
|
2996
|
-
: pseudoKey
|
|
2997
|
-
.slice(1)
|
|
2998
|
-
.split(":")
|
|
2999
|
-
.map((state) => `:${state}`);
|
|
3000
|
-
}
|
|
3001
|
-
pseudoKeyRequiredStatesMap.set(pseudoKey, requiredStates);
|
|
3002
|
-
return requiredStates;
|
|
3003
|
-
};
|
|
3004
|
-
const hasEveryState = (pseudoState, states) => {
|
|
3005
|
-
for (const state of states) {
|
|
3006
|
-
if (!pseudoState[state]) {
|
|
3007
|
-
return false;
|
|
3008
|
-
}
|
|
3009
|
-
}
|
|
3010
|
-
return true;
|
|
3011
|
-
};
|
|
3012
|
-
|
|
3013
|
-
// element → the style object last written to it, so the next one is written
|
|
3014
|
-
// as a difference: the values that changed, the keys it no longer has.
|
|
3015
|
-
const appliedStyleWeakMap = new WeakMap();
|
|
3016
|
-
const elementTransitionWeakMap = new WeakMap();
|
|
3017
|
-
const elementRenderedWeakSet = new WeakSet();
|
|
3018
|
-
const NO_STYLE = {};
|
|
3019
|
-
const updateStyle = (element, style, preventInitialTransition) => {
|
|
3020
|
-
const styleToApply = style || NO_STYLE;
|
|
3021
|
-
const styleApplied = appliedStyleWeakMap.get(element) || NO_STYLE;
|
|
3022
|
-
// TRANSITION ANTI-FLICKER STRATEGY:
|
|
3023
|
-
// Problem: When setting both transition and styled properties simultaneously
|
|
3024
|
-
// (e.g., el.style.transition = "border-radius 0.3s ease"; el.style.borderRadius = "20px"),
|
|
3025
|
-
// the browser will immediately perform a transition even if no transition existed before.
|
|
3026
|
-
//
|
|
3027
|
-
// Solution: Temporarily disable transitions during initial style application by setting
|
|
3028
|
-
// transition to "none", then restore the intended transition after the frame completes.
|
|
3029
|
-
// We handle multiple updateStyle calls in the same frame gracefully - only one
|
|
3030
|
-
// requestAnimationFrame is scheduled per element, and the final transition value wins.
|
|
3031
|
-
let skipTransition = false;
|
|
3032
|
-
if (!elementRenderedWeakSet.has(element)) {
|
|
3033
|
-
const hasTransition = Object.hasOwn(styleToApply, "transition");
|
|
3034
|
-
if (hasTransition || preventInitialTransition) {
|
|
3035
|
-
if (!elementTransitionWeakMap.has(element)) {
|
|
3036
|
-
element.style.transition = "none";
|
|
3037
|
-
}
|
|
3038
|
-
elementTransitionWeakMap.set(element, styleToApply.transition);
|
|
3039
|
-
// Stays "none" until the first frame puts the intended value back
|
|
3040
|
-
skipTransition = true;
|
|
3041
|
-
}
|
|
3042
|
-
afterFirstFrame(element);
|
|
3043
|
-
}
|
|
3044
|
-
|
|
3045
|
-
for (const key of Object.keys(styleToApply)) {
|
|
3046
|
-
const value = styleToApply[key];
|
|
3047
|
-
if (value === undefined || value === null) {
|
|
3048
|
-
// a removal: handled below with the keys this style no longer has
|
|
3049
|
-
continue;
|
|
3050
|
-
}
|
|
3051
|
-
if (skipTransition && key === "transition") {
|
|
3052
|
-
continue;
|
|
3053
|
-
}
|
|
3054
|
-
if (styleApplied[key] === value) {
|
|
3055
|
-
continue;
|
|
3056
|
-
}
|
|
3057
|
-
if (key.startsWith("--")) {
|
|
3058
|
-
element.style.setProperty(key, value);
|
|
3059
|
-
} else {
|
|
3060
|
-
element.style[key] = value;
|
|
3061
|
-
}
|
|
3062
|
-
}
|
|
3063
|
-
|
|
3064
|
-
for (const key of Object.keys(styleApplied)) {
|
|
3065
|
-
const previousValue = styleApplied[key];
|
|
3066
|
-
if (previousValue === undefined || previousValue === null) {
|
|
3067
|
-
continue;
|
|
3068
|
-
}
|
|
3069
|
-
const value = styleToApply[key];
|
|
3070
|
-
if (value !== undefined && value !== null) {
|
|
3071
|
-
continue;
|
|
3072
|
-
}
|
|
3073
|
-
if (key.startsWith("--")) {
|
|
3074
|
-
element.style.removeProperty(key);
|
|
3075
|
-
} else {
|
|
3076
|
-
element.style[key] = "";
|
|
3077
|
-
}
|
|
3078
|
-
}
|
|
3079
|
-
|
|
3080
|
-
appliedStyleWeakMap.set(element, styleToApply);
|
|
3081
|
-
};
|
|
3082
|
-
|
|
3083
|
-
// One frame for every element waiting for its first one, not one frame each.
|
|
3084
|
-
// What this owes each element — put back the transition suppressed above, and
|
|
3085
|
-
// remember it has now been painted once — is a few microseconds, while asking
|
|
3086
|
-
// the browser for a frame costs more than that, and a page mounting thousands of
|
|
3087
|
-
// boxes asks thousands of times in the same tick. A Set, so an element styled
|
|
3088
|
-
// twice before the frame arrives is still one entry.
|
|
3089
|
-
let elementSetWaitingFirstFrame = new Set();
|
|
3090
|
-
let firstFrameScheduled = false;
|
|
3091
|
-
const afterFirstFrame = (element) => {
|
|
3092
|
-
elementSetWaitingFirstFrame.add(element);
|
|
3093
|
-
if (firstFrameScheduled) {
|
|
3094
|
-
return;
|
|
3095
|
-
}
|
|
3096
|
-
firstFrameScheduled = true;
|
|
3097
|
-
requestAnimationFrame(() => {
|
|
3098
|
-
firstFrameScheduled = false;
|
|
3099
|
-
const elements = elementSetWaitingFirstFrame;
|
|
3100
|
-
elementSetWaitingFirstFrame = new Set();
|
|
3101
|
-
for (const element of elements) {
|
|
3102
|
-
if (elementTransitionWeakMap.has(element)) {
|
|
3103
|
-
const transitionToRestore = elementTransitionWeakMap.get(element);
|
|
3104
|
-
if (transitionToRestore === undefined) {
|
|
3105
|
-
element.style.transition = "";
|
|
3106
|
-
} else {
|
|
3107
|
-
element.style.transition = transitionToRestore;
|
|
3108
|
-
}
|
|
3109
|
-
elementTransitionWeakMap.delete(element);
|
|
3110
|
-
}
|
|
3111
|
-
elementRenderedWeakSet.add(element);
|
|
3112
|
-
}
|
|
3113
|
-
});
|
|
3114
|
-
};
|
|
3115
|
-
|
|
3116
|
-
// Dev only: a periodic re-check for what nothing announces — a state forced
|
|
3117
|
-
// from the devtools. One interval walking every registered check rather than
|
|
3118
|
-
// one per element: a page of a thousand boxes would otherwise run a thousand
|
|
3119
|
-
// timers a second.
|
|
3120
|
-
const devPollCheckSet = new Set();
|
|
3121
|
-
let devPollInterval = null;
|
|
3122
|
-
const pollInDev = (check) => {
|
|
3123
|
-
devPollCheckSet.add(check);
|
|
3124
|
-
if (devPollInterval === null) {
|
|
3125
|
-
devPollInterval = setInterval(() => {
|
|
3126
|
-
for (const registeredCheck of devPollCheckSet) {
|
|
3127
|
-
registeredCheck();
|
|
3128
|
-
}
|
|
3129
|
-
}, 1_000);
|
|
3130
|
-
}
|
|
3131
|
-
return () => {
|
|
3132
|
-
devPollCheckSet.delete(check);
|
|
3133
|
-
if (devPollCheckSet.size === 0) {
|
|
3134
|
-
clearInterval(devPollInterval);
|
|
3135
|
-
devPollInterval = null;
|
|
3136
|
-
}
|
|
3137
|
-
};
|
|
3138
|
-
};
|
|
3139
|
-
|
|
3140
|
-
/**
|
|
3141
|
-
* navi's stance on the on-screen keyboard: it overlays the app rather than
|
|
3142
|
-
* resizing the viewport, wherever the browser can be told so (the
|
|
3143
|
-
* VirtualKeyboard API — Chromium only). See virtual_keyboard.js in @jsenv/dom
|
|
3144
|
-
* for what that trades away and what it gives back.
|
|
3145
|
-
*
|
|
3146
|
-
* Turned on rather than offered, because navi already sizes everything that
|
|
3147
|
-
* escapes normal flow against the app's own rectangle rather than against the
|
|
3148
|
-
* window (--navi-app-width/height, see navi_css_vars.js), and
|
|
3149
|
-
* --navi-keyboard-inset-bottom (safe_area.js) puts the keyboard into exactly
|
|
3150
|
-
* that rectangle. So the two mechanisms reach the same numbers here, and the
|
|
3151
|
-
* overlay reaches them without reflowing the page underneath — a resizing
|
|
3152
|
-
* viewport is a resize of everything, fired transiently every time focus goes
|
|
3153
|
-
* from one input to the next.
|
|
3154
|
-
*
|
|
3155
|
-
* An app that built its own layout around the viewport shrinking can say so
|
|
3156
|
-
* with disableVirtualKeyboardOverlay(), and gets the behavior Firefox and
|
|
3157
|
-
* Safari give it anyway.
|
|
3158
|
-
*
|
|
3159
|
-
* Taking the deal means taking over what the browser stops doing under it:
|
|
3160
|
-
* bringing the focused field out from under the keyboard. A viewport that
|
|
3161
|
-
* shrinks makes the browser scroll to the field; a keyboard that merely paints
|
|
3162
|
-
* over the page leaves whatever is under it under it. So navi does that
|
|
3163
|
-
* scroll here, and it needs two things safe_area.js provides: a
|
|
3164
|
-
* scroll-padding-bottom that counts the keyboard in, so the scroll stops right
|
|
3165
|
-
* above it, and room at the end of the scroller, so a field near the end of
|
|
3166
|
-
* the page has somewhere to go.
|
|
3167
|
-
*/
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
setVirtualKeyboardOverlaysContent(true);
|
|
3171
|
-
|
|
3172
|
-
const disableVirtualKeyboardOverlay = () => {
|
|
3173
|
-
setVirtualKeyboardOverlaysContent(false);
|
|
3174
|
-
};
|
|
3175
|
-
|
|
3176
|
-
const revealFocusedField = () => {
|
|
3177
|
-
// 0 as well once the overlay is disabled: the viewport shrinks and the
|
|
3178
|
-
// browser reveals the field itself.
|
|
3179
|
-
if (getVirtualKeyboardOverlayHeight() === 0) {
|
|
3180
|
-
return;
|
|
3181
|
-
}
|
|
3182
|
-
const field = document.activeElement;
|
|
3183
|
-
if (!isEditableTarget(field)) {
|
|
3184
|
-
return;
|
|
3185
|
-
}
|
|
3186
|
-
// "nearest" against a scroll-padding-bottom that counts the keyboard in
|
|
3187
|
-
// (:root and [data-navi-safe-area], see safe_area.js): a field already clear
|
|
3188
|
-
// of it does not move, one under it stops right above it.
|
|
3189
|
-
scrollIntoViewThroughScrollables(field, {
|
|
3190
|
-
block: "nearest",
|
|
3191
|
-
behavior: "instant",
|
|
3192
|
-
});
|
|
3193
|
-
};
|
|
3194
|
-
// The keyboard rising, or changing height (suggestion strip, emoji panel).
|
|
3195
|
-
subscribeVirtualKeyboardGeometryChange(revealFocusedField);
|
|
3196
|
-
// From one field to the next with the keyboard up: when both want the same
|
|
3197
|
-
// keyboard it does not move, and no geometrychange fires.
|
|
3198
|
-
document.addEventListener("focusin", revealFocusedField, { capture: true });
|
|
3199
|
-
|
|
3200
369
|
installImportMetaCssBuild(import.meta);
|
|
3201
370
|
|
|
3202
371
|
const button = document.createElement("button");
|
|
@@ -3368,10 +537,10 @@ effect(() => {
|
|
|
3368
537
|
// that positions popups (pickPositionRelativeTo in @jsenv/dom) computes
|
|
3369
538
|
// against the visual viewport narrowed by the app's own bands, so a dialog
|
|
3370
539
|
// centers on the app column and a side panel docks flush against the app's
|
|
3371
|
-
// edge rather than the window's. Here rather than in each component
|
|
3372
|
-
//
|
|
3373
|
-
//
|
|
540
|
+
// edge rather than the window's. Here rather than in each component: it is
|
|
541
|
+
// one decision about the whole window, and this is the module that makes
|
|
542
|
+
// those.
|
|
3374
543
|
setPlacementViewportInsets(getAppInsets);
|
|
3375
544
|
|
|
3376
|
-
export {
|
|
545
|
+
export { coarsePointerSignal, getAppHeight, getAppWidth, installImportMetaCssBuild, smallTouchScreenSignal, visualViewportHeightSignal, visualViewportWidthSignal, windowHeightSignal, windowWidthSignal };
|
|
3377
546
|
//# sourceMappingURL=jsenv_navi_side_effects.js.map
|