@telperion/elements 3.0.1 → 4.0.1

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.
Files changed (74) hide show
  1. package/.build-manifest.json +12 -1
  2. package/README.md +45 -0
  3. package/_chunks/signal-watcher-lit-element-B9xodkLt.d.cts +29 -0
  4. package/_chunks/signal-watcher-lit-element-B9xodkLt.d.ts +29 -0
  5. package/drag-drop/drag-handle-element.cjs +1 -1
  6. package/drag-drop/drag-handle-element.js +1 -1
  7. package/fesm/elements.mjs +548 -31
  8. package/fesm/elements.mjs.map +1 -1
  9. package/iife/elements.js +187 -21
  10. package/iife/elements.js.map +1 -1
  11. package/iife/popover/index.js +44 -6
  12. package/iife/popover/index.js.map +1 -1
  13. package/iife/router/index.js.map +1 -1
  14. package/iife/select/index.js +273 -0
  15. package/iife/select/index.js.map +1 -0
  16. package/index.cjs +15 -0
  17. package/index.cjs.map +1 -1
  18. package/index.d.cts +2 -0
  19. package/index.d.ts +2 -0
  20. package/index.js +3 -0
  21. package/index.js.map +1 -1
  22. package/package.json +32 -5
  23. package/popover/index.d.cts +2 -2
  24. package/popover/index.d.ts +2 -2
  25. package/popover/popover-element.cjs +29 -19
  26. package/popover/popover-element.cjs.map +1 -1
  27. package/popover/popover-element.js +30 -20
  28. package/popover/popover-element.js.map +1 -1
  29. package/router/index.d.cts +2 -26
  30. package/router/index.d.ts +2 -26
  31. package/router/tha-router-config.cjs +2 -2
  32. package/router/tha-router-config.cjs.map +1 -1
  33. package/router/tha-router-config.js +2 -2
  34. package/router/tha-router-config.js.map +1 -1
  35. package/router/tha-router-link.cjs +1 -1
  36. package/router/tha-router-link.js +1 -1
  37. package/router/tha-router-outlet.cjs +1 -1
  38. package/router/tha-router-outlet.js +1 -1
  39. package/router/tha-router-param.cjs +1 -1
  40. package/router/tha-router-param.js +1 -1
  41. package/router/tha-router.cjs +1 -1
  42. package/router/tha-router.js +1 -1
  43. package/select/index.cjs +21 -0
  44. package/select/index.cjs.map +1 -0
  45. package/select/index.d.cts +219 -0
  46. package/select/index.d.ts +219 -0
  47. package/select/index.js +4 -0
  48. package/select/index.js.map +1 -0
  49. package/select/internal-props.cjs +10 -0
  50. package/select/internal-props.cjs.map +1 -0
  51. package/select/internal-props.js +6 -0
  52. package/select/internal-props.js.map +1 -0
  53. package/select/option.element.cjs +88 -0
  54. package/select/option.element.cjs.map +1 -0
  55. package/select/option.element.js +88 -0
  56. package/select/option.element.js.map +1 -0
  57. package/select/select-context.cjs +14 -0
  58. package/select/select-context.cjs.map +1 -0
  59. package/select/select-context.js +12 -0
  60. package/select/select-context.js.map +1 -0
  61. package/select/select.element.cjs +376 -0
  62. package/select/select.element.cjs.map +1 -0
  63. package/select/select.element.js +376 -0
  64. package/select/select.element.js.map +1 -0
  65. package/select/selected-content.element.cjs +65 -0
  66. package/select/selected-content.element.cjs.map +1 -0
  67. package/select/selected-content.element.js +65 -0
  68. package/select/selected-content.element.js.map +1 -0
  69. package/utils/signal-watcher-lit-element.cjs.map +1 -0
  70. package/utils/signal-watcher-lit-element.js.map +1 -0
  71. package/router/signal-watcher-lit-element.cjs.map +0 -1
  72. package/router/signal-watcher-lit-element.js.map +0 -1
  73. /package/{router → utils}/signal-watcher-lit-element.cjs +0 -0
  74. /package/{router → utils}/signal-watcher-lit-element.js +0 -0
package/fesm/elements.mjs CHANGED
@@ -6,10 +6,13 @@ import { noop } from '@telperion/js-utils/function/noop';
6
6
  import { createMemoryHistory, createHashHistory, createBrowserHistory } from 'history';
7
7
  import { defer } from '@telperion/js-utils/function/defer';
8
8
  import { compact } from '@telperion/js-utils/array/compact';
9
- import { getParents } from '@thalesrc/dom-utils/get-parents';
9
+ import { getParents } from '@telperion/dom-utils/get-parents';
10
10
  import { uniqueId } from '@telperion/js-utils/unique-id';
11
+ import { createContext, consume, provide } from '@lit/context';
12
+ import { sliceBefore } from '@telperion/js-utils/array/slice-before';
13
+ import { DomClone } from '@telperion/dom-utils';
11
14
 
12
- var _a$2, _b;
15
+ var _a$3, _b;
13
16
  /**
14
17
  * Symbol used to access the route's template fragment signal.
15
18
  * @internal
@@ -53,7 +56,7 @@ let ThaRoute = class ThaRoute extends LitElement {
53
56
  * Automatically updates when the template content changes.
54
57
  * @internal
55
58
  */
56
- this[_a$2] = signal(this.#getTemplateFragment());
59
+ this[_a$3] = signal(this.#getTemplateFragment());
57
60
  /**
58
61
  * URLPattern instance for matching the current URL against this route's path.
59
62
  * @internal
@@ -73,7 +76,7 @@ let ThaRoute = class ThaRoute extends LitElement {
73
76
  this[FRAGMENT].set(this.#getTemplateFragment());
74
77
  });
75
78
  }
76
- static { _a$2 = FRAGMENT, _b = PATH_PATTERN; }
79
+ static { _a$3 = FRAGMENT, _b = PATH_PATTERN; }
77
80
  /** Observes changes to child elements to detect template updates */
78
81
  #templateObserver;
79
82
  /**
@@ -284,7 +287,7 @@ const HistoryManaged = (superClass) => {
284
287
  return HistoryManagedLitElement;
285
288
  };
286
289
 
287
- var _a$1;
290
+ var _a$2;
288
291
  /**
289
292
  * Symbol used by the router to call the internal render method on the outlet.
290
293
  * @internal
@@ -333,7 +336,7 @@ let ThaRouterOutlet = class ThaRouterOutlet extends SignalWatcherLitElement {
333
336
  const { location: { pathname } } = router[HISTORY].get();
334
337
  return pattern.exec(pathname, 'http://dummy.base')?.pathname.groups ?? null;
335
338
  });
336
- this[_a$1] = computed(() => {
339
+ this[_a$2] = computed(() => {
337
340
  const ownParams = this.#ownParams.get() ?? {};
338
341
  const parent = this.parentElement?.closest('tha-router-outlet');
339
342
  const parentParams = parent?.[PARAMS].get() ?? {};
@@ -441,7 +444,7 @@ let ThaRouterOutlet = class ThaRouterOutlet extends SignalWatcherLitElement {
441
444
  * @param route - The route to render, or null to clear
442
445
  * @internal
443
446
  */
444
- [(_a$1 = PARAMS, RENDER_ROUTE)](route) {
447
+ [(_a$2 = PARAMS, RENDER_ROUTE)](route) {
445
448
  this.#routeToRender.set(route ? new WeakRef(route) : null);
446
449
  }
447
450
  /**
@@ -1342,15 +1345,15 @@ const ACCEPT_DROP_PROP = Symbol('Accept Drop');
1342
1345
  const REGISTER_HANDLE_PROP = Symbol('Register Handle');
1343
1346
  const UNREGISTER_HANDLE_PROP = Symbol('Unregister Handle');
1344
1347
 
1345
- var _a;
1348
+ var _a$1;
1346
1349
  class ThaDragEvent extends DragEvent {
1347
1350
  static #DragElementEvents = ['thadrag', 'thadragstart', 'thadragend', 'thadropzoneenter', 'thadropzoneleave', 'thadropzoneover', 'thadropped'];
1348
1351
  static #DropElementEvents = ['thadrop', 'thadropend', 'thadragenter', 'thadragleave', 'thadragover'];
1349
1352
  get #target() {
1350
- if (_a.#DropElementEvents.includes(this.type)) {
1353
+ if (_a$1.#DropElementEvents.includes(this.type)) {
1351
1354
  return this.dropzoneTarget;
1352
1355
  }
1353
- else if (_a.#DragElementEvents.includes(this.type)) {
1356
+ else if (_a$1.#DragElementEvents.includes(this.type)) {
1354
1357
  return this.dragTarget;
1355
1358
  }
1356
1359
  return new WeakRef(this.target);
@@ -1370,7 +1373,7 @@ class ThaDragEvent extends DragEvent {
1370
1373
  this.dragTarget.deref()?.[ACCEPT_DROP_PROP](this.dropzoneTarget, rect);
1371
1374
  }
1372
1375
  }
1373
- _a = ThaDragEvent;
1376
+ _a$1 = ThaDragEvent;
1374
1377
 
1375
1378
  var DragElement_1;
1376
1379
  let DragElement = class DragElement extends LitElement {
@@ -2432,7 +2435,7 @@ let PopoverElement = class PopoverElement extends LitElement {
2432
2435
  /** Placement spec — see class docs. */
2433
2436
  this.position = null;
2434
2437
  /** How the resolved target opens the popover. */
2435
- this.trigger = "manual";
2438
+ this.trigger = "click";
2436
2439
  /** Per-instance anchor id (without the leading `--`). */
2437
2440
  this.#anchorId = uniqueId("tp-popover");
2438
2441
  /** Currently resolved anchor element (if any). */
@@ -2442,20 +2445,19 @@ let PopoverElement = class PopoverElement extends LitElement {
2442
2445
  /** Bound trigger listener cleanups for the current anchor. */
2443
2446
  this.#triggerCleanups = [];
2444
2447
  }
2445
- static {
2446
- const style = (this.GLOBAL_STYLE = document.createElement("style"));
2447
- // Pre-baked rules. Each instance gets:
2448
- // - data-pi/data-ti/data-pb/data-tb attributes set by JS,
2449
- // - inline `position-anchor: --tp-popover-N`,
2450
- // - inline `anchor-name: --tp-popover-N` on the resolved target.
2451
- //
2452
- // Use `margin` on the popover to add gap between the popover and its
2453
- // anchor target. The default `position-try-fallbacks` flips the popover
2454
- // off-axis when there is no room — the engine swaps `inset-*-start` with
2455
- // `inset-*-end` and the `start`/`end` keywords inside `anchor()` for us,
2456
- // and our translate-vars are symmetric so they remain correct after a
2457
- // flip.
2458
- style.textContent = `
2448
+ // Pre-baked rules. Each instance gets:
2449
+ // - data-pi/data-ti/data-pb/data-tb attributes set by JS,
2450
+ // - inline `position-anchor: --tp-popover-N`,
2451
+ // - inline `anchor-name: --tp-popover-N` on the resolved target.
2452
+ //
2453
+ // Use `margin` on the popover to add gap between the popover and its
2454
+ // anchor target. The default `position-try-fallbacks` flips the popover
2455
+ // off-axis when there is no room — the engine swaps `inset-*-start` with
2456
+ // `inset-*-end` and the `start`/`end` keywords inside `anchor()` for us,
2457
+ // and our translate-vars are symmetric so they remain correct after a
2458
+ // flip.
2459
+ static { this.styles = (() => {
2460
+ const style = css `
2459
2461
  tp-popover {
2460
2462
  --tp-popover-inline-position: attr(data-ti type(*));
2461
2463
  --tp-popover-block-position: attr(data-tb type(*));
@@ -2494,8 +2496,9 @@ let PopoverElement = class PopoverElement extends LitElement {
2494
2496
  }
2495
2497
  }
2496
2498
  `;
2497
- document.head.appendChild(style);
2498
- }
2499
+ document.adoptedStyleSheets = [...document.adoptedStyleSheets, style.styleSheet];
2500
+ return style;
2501
+ })(); }
2499
2502
  /** Per-instance anchor id (without the leading `--`). */
2500
2503
  #anchorId;
2501
2504
  /** Currently resolved anchor element (if any). */
@@ -2554,7 +2557,8 @@ let PopoverElement = class PopoverElement extends LitElement {
2554
2557
  const selector = this.target;
2555
2558
  if (selector) {
2556
2559
  try {
2557
- const el = document.querySelector(selector);
2560
+ const root = this.getRootNode();
2561
+ const el = root.querySelector(selector);
2558
2562
  if (el instanceof HTMLElement)
2559
2563
  return el;
2560
2564
  console.warn(`<tp-popover>: target "${selector}" did not resolve to an HTMLElement; using parentElement.`);
@@ -2564,7 +2568,16 @@ let PopoverElement = class PopoverElement extends LitElement {
2564
2568
  }
2565
2569
  }
2566
2570
  const parent = this.parentElement;
2567
- return parent instanceof HTMLElement ? parent : null;
2571
+ if (parent instanceof HTMLElement)
2572
+ return parent;
2573
+ // No parent element — likely placed directly inside a ShadowRoot.
2574
+ // Climb out to the shadow host so the popover anchors to the component
2575
+ // that contains it (e.g. a Lit element using shadow DOM).
2576
+ const root = this.getRootNode();
2577
+ if (root instanceof ShadowRoot && root.host instanceof HTMLElement) {
2578
+ return root.host;
2579
+ }
2580
+ return null;
2568
2581
  }
2569
2582
  #syncAnchor() {
2570
2583
  const next = this.#resolveTarget();
@@ -2715,5 +2728,509 @@ PopoverElement = __decorate([
2715
2728
  customElement("tp-popover")
2716
2729
  ], PopoverElement);
2717
2730
 
2718
- export { DetailsSetElement, DragElement, DragHandleElement, DropZoneElement, PopoverElement, ThaDragEvent, ThaRoute, ThaRouter, ThaRouterConfig, ThaRouterLink, ThaRouterOutlet, ThaRouterParam };
2731
+ /**
2732
+ * Context key carrying the host `<tp-select>` element down through the
2733
+ * composed tree. Using `@lit/context` means consumers can be nested inside
2734
+ * any number of shadow DOM boundaries and still find their `<tp-select>`
2735
+ * via the bubbling, composed `context-request` event.
2736
+ */
2737
+ const selectContext = createContext(Symbol("tp-select"));
2738
+
2739
+ const REGISTER_OPTION = Symbol("select:register-option");
2740
+ const UNREGISTER_OPTION = Symbol("select:unregister-option");
2741
+ const SHOULD_REGISTER_TO_SELECT = Symbol("option:should-register-to-select");
2742
+
2743
+ let SelectedContentElement = class SelectedContentElement extends SignalWatcherLitElement {
2744
+ static { this.styles = (() => {
2745
+ const style = css `
2746
+ tp-selected-content {
2747
+ display: inline-block;
2748
+
2749
+ tp-option {
2750
+ display: inline-block;
2751
+
2752
+ &:not(:last-of-type)::after {
2753
+ content: ",";
2754
+ white-space: pre;
2755
+ margin-right: 0.25em;
2756
+ }
2757
+ }
2758
+ }
2759
+ `;
2760
+ document.adoptedStyleSheets = [...document.adoptedStyleSheets, style.styleSheet];
2761
+ return style;
2762
+ })(); }
2763
+ #cloneCache = new WeakMap();
2764
+ createRenderRoot() {
2765
+ return this;
2766
+ }
2767
+ render() {
2768
+ const placeholder = this.select?.placeholder ?? "";
2769
+ const selectedOptions = this.select?.selectedOptions.get().map(ref => ref.deref()).filter(Boolean) ?? [];
2770
+ const clones = this.#getOptionClones(selectedOptions);
2771
+ return !selectedOptions.length
2772
+ ? html$1 `<span part="placeholder">${placeholder}</span>`
2773
+ : clones.map(clone => html$1 `${clone}`);
2774
+ }
2775
+ #getOptionClones(options) {
2776
+ return options.map(option => {
2777
+ let mirror = this.#cloneCache.get(option);
2778
+ if (!mirror) {
2779
+ mirror = new DomClone(option);
2780
+ this.#cloneCache.set(option, mirror);
2781
+ mirror.clone.setAttribute("part", "selected-content-option");
2782
+ mirror.clone[SHOULD_REGISTER_TO_SELECT] = false;
2783
+ }
2784
+ return mirror.clone;
2785
+ });
2786
+ }
2787
+ };
2788
+ __decorate([
2789
+ consume({ context: selectContext, subscribe: true }),
2790
+ state(),
2791
+ __metadata("design:type", Function)
2792
+ ], SelectedContentElement.prototype, "select", void 0);
2793
+ SelectedContentElement = __decorate([
2794
+ customElement("tp-selected-content")
2795
+ ], SelectedContentElement);
2796
+
2797
+ var _a;
2798
+ let OptionElement = class OptionElement extends SignalWatcherLitElement {
2799
+ constructor() {
2800
+ super(...arguments);
2801
+ this[_a] = true;
2802
+ this.value = "";
2803
+ }
2804
+ static { _a = SHOULD_REGISTER_TO_SELECT; }
2805
+ static { this.styles = (() => {
2806
+ const style = css `
2807
+ tp-option:not([part="selected-content-option"]) {
2808
+ display: block;
2809
+ cursor: pointer;
2810
+ background: var(--tp-select-option-color, white);
2811
+
2812
+ &:hover {
2813
+ background: color-mix(in xyz, var(--tp-select-option-color, white) calc(100% - var(--tp-select-highlight-percent, 20%)), black var(--tp-select-highlight-percent, 20%));
2814
+ }
2815
+
2816
+ &[selected] {
2817
+ background: var(--tp-select-selection-color, #0078d4);
2818
+ color: var(--tp-select-selection-contrast-color, white);
2819
+
2820
+ &:hover {
2821
+ background: color-mix(in xyz, var(--tp-select-selection-color, #0078d4) calc(100% - var(--tp-select-highlight-percent, 20%)), black var(--tp-select-highlight-percent, 20%));
2822
+ }
2823
+ }
2824
+ }
2825
+ `;
2826
+ document.adoptedStyleSheets = [...document.adoptedStyleSheets, style.styleSheet];
2827
+ return style;
2828
+ })(); }
2829
+ connectedCallback() {
2830
+ super.connectedCallback();
2831
+ if (!this[SHOULD_REGISTER_TO_SELECT])
2832
+ return;
2833
+ this.select?.[REGISTER_OPTION](this);
2834
+ }
2835
+ disconnectedCallback() {
2836
+ super.disconnectedCallback();
2837
+ if (!this[SHOULD_REGISTER_TO_SELECT])
2838
+ return;
2839
+ this.select?.[UNREGISTER_OPTION](this);
2840
+ }
2841
+ /**
2842
+ * Reflect `selected` state by reading the parent's signal during the
2843
+ * normal Lit update. `SignalWatcher` auto-tracks signal access here, so
2844
+ * the option will re-render whenever the selection changes — without
2845
+ * the disposal-race that `updateEffect` exposes.
2846
+ */
2847
+ willUpdate(_changed) {
2848
+ if (!this[SHOULD_REGISTER_TO_SELECT])
2849
+ return;
2850
+ const next = this.select?.selectedOptions
2851
+ .get()
2852
+ .some((ref) => ref.deref() === this) ?? false;
2853
+ this.toggleAttribute("selected", next);
2854
+ }
2855
+ createRenderRoot() {
2856
+ return this;
2857
+ }
2858
+ render() {
2859
+ return html$1 ``;
2860
+ }
2861
+ };
2862
+ __decorate([
2863
+ consume({ context: selectContext, subscribe: true }),
2864
+ state(),
2865
+ __metadata("design:type", Function)
2866
+ ], OptionElement.prototype, "select", void 0);
2867
+ __decorate([
2868
+ property({ type: String, reflect: true }),
2869
+ __metadata("design:type", Object)
2870
+ ], OptionElement.prototype, "value", void 0);
2871
+ OptionElement = __decorate([
2872
+ customElement("tp-option")
2873
+ ], OptionElement);
2874
+
2875
+ var SelectElement_1;
2876
+ let SelectElement = class SelectElement extends SignalWatcherLitElement {
2877
+ static { SelectElement_1 = this; }
2878
+ static { this.formAssociated = true; }
2879
+ static { this.ALLOWED_OPTION_ELEMENTS = [OptionElement]; }
2880
+ static { this.styles = css `
2881
+ :host {
2882
+ position: relative;
2883
+ display: inline-flex;
2884
+ height: fit-content;
2885
+
2886
+ --tp-select-option-color: white;
2887
+ --tp-select-selection-color: #0078d4;
2888
+ --tp-select-highlight-percent: 20%;
2889
+ --tp-select-selection-contrast-color: white;
2890
+ }
2891
+
2892
+ :host([disabled]) {
2893
+ pointer-events: none;
2894
+ opacity: 0.6;
2895
+ }
2896
+
2897
+ :host([open]) {
2898
+ [pseudo="indicator"] {
2899
+ transform: rotateX(180deg);
2900
+ }
2901
+ }
2902
+
2903
+ [pseudo="button"] {
2904
+ background: white;
2905
+ cursor: pointer;
2906
+ display: inline-flex;
2907
+ align-items: center;
2908
+ }
2909
+
2910
+ [pseudo="indicator"] {
2911
+ margin-inline-start: auto;
2912
+ }
2913
+
2914
+ tp-popover {
2915
+ border: none;
2916
+ padding: 0;
2917
+ }
2918
+ `; }
2919
+ #options;
2920
+ #value;
2921
+ get value() {
2922
+ return this.#value.get();
2923
+ }
2924
+ set value(next) {
2925
+ defer(() => {
2926
+ const values = next.split(",").map((s) => s.trim());
2927
+ const options = Array.from(this.#options);
2928
+ this.selectedOptions.set(values
2929
+ .map((value) => options.find((option) => option.value === value))
2930
+ .filter(Boolean)
2931
+ .map((option) => new WeakRef(option)));
2932
+ });
2933
+ }
2934
+ #placeholder;
2935
+ get placeholder() {
2936
+ return this.#placeholder.get();
2937
+ }
2938
+ set placeholder(next) {
2939
+ this.#signalSetter(this.#placeholder, "placeholder", next);
2940
+ }
2941
+ #internals;
2942
+ /** Initial `value` captured at construction so `formResetCallback` can restore it. */
2943
+ #initialValue;
2944
+ constructor() {
2945
+ super();
2946
+ this.#options = new Set();
2947
+ /**
2948
+ * Provides this `<tp-select>` instance to descendants via `@lit/context`,
2949
+ * so consumers (e.g. `<tp-selected-content>`) can find it across any
2950
+ * number of shadow DOM boundaries — not just light-DOM ancestors.
2951
+ */
2952
+ this.self = this;
2953
+ this.selectedOptions = signal([]);
2954
+ this.#value = computed(() => this.selectedOptions
2955
+ .get()
2956
+ .map((ref) => ref.deref()?.value ?? "")
2957
+ .filter(Boolean)
2958
+ .join(","));
2959
+ this.#placeholder = signal("Select an option");
2960
+ /**
2961
+ * Maximum number of options that can be selected at once.
2962
+ *
2963
+ * Defaults to `1` (single-select). Any positive integer enables
2964
+ * multi-select up to that many entries. Use the literal string
2965
+ * `"infinite"` in HTML to allow an unbounded
2966
+ * selection — the converter normalises it to `Number.POSITIVE_INFINITY`.
2967
+ *
2968
+ * When the limit is reached and a new option is selected, the oldest
2969
+ * entry in `selectedOptions` is dropped (FIFO) so the new selection can
2970
+ * fit. Setting `max` to a smaller number trims the current selection
2971
+ * down to size from the end.
2972
+ */
2973
+ this.max = 1;
2974
+ /** Form field name. Submitted with the owning `<form>`. */
2975
+ this.name = null;
2976
+ /** When true, the element is non-interactive and excluded from form submission. */
2977
+ this.disabled = false;
2978
+ /** When true, an empty selection makes the element invalid (`valueMissing`). */
2979
+ this.required = false;
2980
+ this.open = false;
2981
+ /** Initial `value` captured at construction so `formResetCallback` can restore it. */
2982
+ this.#initialValue = "";
2983
+ this.#internals = this.attachInternals();
2984
+ }
2985
+ /** Live `ElementInternals` (validity, form, labels, etc.). */
2986
+ get internals() {
2987
+ return this.#internals;
2988
+ }
2989
+ /** Owning `<form>` element, or `null` when not associated. */
2990
+ get form() {
2991
+ return this.#internals.form;
2992
+ }
2993
+ /** Native validity state of this control. */
2994
+ get validity() {
2995
+ return this.#internals.validity;
2996
+ }
2997
+ get validationMessage() {
2998
+ return this.#internals.validationMessage;
2999
+ }
3000
+ get willValidate() {
3001
+ return this.#internals.willValidate;
3002
+ }
3003
+ checkValidity() {
3004
+ return this.#internals.checkValidity();
3005
+ }
3006
+ reportValidity() {
3007
+ return this.#internals.reportValidity();
3008
+ }
3009
+ createRenderRoot() {
3010
+ const root = super.createRenderRoot();
3011
+ root.adoptedStyleSheets = [
3012
+ ...root.adoptedStyleSheets,
3013
+ PopoverElement.styles.styleSheet,
3014
+ SelectedContentElement.styles.styleSheet,
3015
+ OptionElement.styles.styleSheet,
3016
+ ];
3017
+ return root;
3018
+ }
3019
+ connectedCallback() {
3020
+ super.connectedCallback();
3021
+ this.#initialValue = this.value;
3022
+ }
3023
+ disconnectedCallback() {
3024
+ super.disconnectedCallback();
3025
+ }
3026
+ render() {
3027
+ return html$1 `
3028
+ <div pseudo="button" part="button">
3029
+ <slot name="button">
3030
+ <tp-selected-content part="selected-content"></tp-selected-content>
3031
+ <slot name="indicator">
3032
+ <span part="indicator" pseudo="indicator">▾</span>
3033
+ </slot>
3034
+ </slot>
3035
+ </div>
3036
+ <tp-popover
3037
+ position="start to start / bottom"
3038
+ target="[pseudo=button]"
3039
+ @click=${this.#handlePopoverClick}
3040
+ @toggle=${this.#handlePopoverToggle}
3041
+ part="popover">
3042
+ <slot name="popover">
3043
+ <slot></slot>
3044
+ </slot>
3045
+ </tp-popover>
3046
+ `;
3047
+ }
3048
+ updated(changed) {
3049
+ super.updated(changed);
3050
+ if (changed.has("max") && Number.isFinite(this.max)) {
3051
+ const current = this.selectedOptions.get();
3052
+ if (current.length > this.max) {
3053
+ this.selectedOptions.set(current.slice(current.length - this.max));
3054
+ }
3055
+ }
3056
+ }
3057
+ /**
3058
+ * Keep `ElementInternals` in sync with the current value and validity.
3059
+ * Reading `this.value` here transitively reads `selectedOptions`, so the
3060
+ * `SignalWatcher` base auto-tracks it and re-runs `willUpdate` whenever
3061
+ * the selection changes — no `updateEffect` (and no microtask race) needed.
3062
+ */
3063
+ willUpdate(_changed) {
3064
+ this.#syncForm();
3065
+ }
3066
+ // -- Form-associated callbacks ------------------------------------------
3067
+ formResetCallback() {
3068
+ this.value = this.#initialValue;
3069
+ }
3070
+ formDisabledCallback(disabled) {
3071
+ this.disabled = disabled;
3072
+ }
3073
+ formStateRestoreCallback(state) {
3074
+ if (state == null) {
3075
+ this.value = "";
3076
+ return;
3077
+ }
3078
+ if (typeof state === "string") {
3079
+ this.value = state;
3080
+ return;
3081
+ }
3082
+ if (state instanceof FormData) {
3083
+ const values = [];
3084
+ state.forEach((v) => {
3085
+ if (typeof v === "string")
3086
+ values.push(v);
3087
+ });
3088
+ this.value = values.join(",");
3089
+ }
3090
+ }
3091
+ /**
3092
+ * Push the current selection into `ElementInternals`. Single-select forms
3093
+ * report a string; multi-select forms report a `FormData` with one entry
3094
+ * per selected value (so the form serialises the same way as a native
3095
+ * `<select multiple>`). Also updates validity for `required`.
3096
+ */
3097
+ #syncForm() {
3098
+ const values = this.value === "" ? [] : this.value.split(",");
3099
+ if (this.max === 1) {
3100
+ this.#internals.setFormValue(values[0] ?? "");
3101
+ }
3102
+ else {
3103
+ const fd = new FormData();
3104
+ const fieldName = this.name ?? "";
3105
+ for (const v of values)
3106
+ fd.append(fieldName, v);
3107
+ this.#internals.setFormValue(fd);
3108
+ }
3109
+ if (this.required && values.length === 0) {
3110
+ this.#internals.setValidity({ valueMissing: true }, "Please select an option.");
3111
+ }
3112
+ else {
3113
+ this.#internals.setValidity({});
3114
+ }
3115
+ }
3116
+ selectOption(option) {
3117
+ const current = this.selectedOptions.get();
3118
+ if (current.find(ref => ref.deref() === option))
3119
+ return;
3120
+ const next = [...current, new WeakRef(option)];
3121
+ const trimmed = next.length > this.max
3122
+ ? next.slice(next.length - this.max)
3123
+ : next;
3124
+ this.selectedOptions.set(trimmed);
3125
+ }
3126
+ deselectOption(option) {
3127
+ const optionRef = this.selectedOptions.get().find(ref => ref.deref() === option);
3128
+ if (!optionRef)
3129
+ return;
3130
+ this.selectedOptions.set(this.selectedOptions.get().filter(ref => ref !== optionRef));
3131
+ }
3132
+ toggleOption(option) {
3133
+ const ref = this.selectedOptions.get().find(ref => ref.deref() === option);
3134
+ if (ref) {
3135
+ this.deselectOption(option);
3136
+ }
3137
+ else {
3138
+ this.selectOption(option);
3139
+ }
3140
+ }
3141
+ [REGISTER_OPTION](option) {
3142
+ this.#options.add(option);
3143
+ }
3144
+ [UNREGISTER_OPTION](option) {
3145
+ this.#options.delete(option);
3146
+ const selectedOptions = this.selectedOptions.get();
3147
+ const optionRef = selectedOptions.find(ref => ref.deref() === option);
3148
+ if (!optionRef)
3149
+ return;
3150
+ this.selectedOptions.set(selectedOptions.filter(ref => ref !== optionRef));
3151
+ }
3152
+ #handlePopoverClick(e) {
3153
+ if (this.disabled)
3154
+ return;
3155
+ const option = sliceBefore(e.composedPath(), this)
3156
+ .find(el => SelectElement_1.ALLOWED_OPTION_ELEMENTS.some(allowed => el instanceof allowed));
3157
+ if (!option)
3158
+ return;
3159
+ this.toggleOption(option);
3160
+ const count = this.selectedOptions.get().length;
3161
+ // Close the popover when a new selection just filled the quota.
3162
+ if (count >= this.max) {
3163
+ const popover = this.shadowRoot?.querySelector("tp-popover");
3164
+ popover?.hidePopover();
3165
+ }
3166
+ }
3167
+ #handlePopoverToggle(e) {
3168
+ this.open = e.newState === "open";
3169
+ }
3170
+ #signalSetter(signal, propName, newValue) {
3171
+ const previous = signal.get();
3172
+ if (previous === newValue)
3173
+ return;
3174
+ signal.set(newValue);
3175
+ this.requestUpdate(propName, previous);
3176
+ }
3177
+ };
3178
+ __decorate([
3179
+ provide({ context: selectContext }),
3180
+ __metadata("design:type", SelectElement)
3181
+ ], SelectElement.prototype, "self", void 0);
3182
+ __decorate([
3183
+ property({ type: String, reflect: true, noAccessor: true }),
3184
+ __metadata("design:type", String),
3185
+ __metadata("design:paramtypes", [String])
3186
+ ], SelectElement.prototype, "value", null);
3187
+ __decorate([
3188
+ property({ type: String, reflect: true, noAccessor: true }),
3189
+ __metadata("design:type", String),
3190
+ __metadata("design:paramtypes", [String])
3191
+ ], SelectElement.prototype, "placeholder", null);
3192
+ __decorate([
3193
+ property({
3194
+ reflect: true,
3195
+ converter: {
3196
+ fromAttribute: (value) => {
3197
+ if (value === null || value === undefined || value === "")
3198
+ return 1;
3199
+ const trimmed = String(value).trim().toLowerCase();
3200
+ if (trimmed === "infinite")
3201
+ return Number.POSITIVE_INFINITY;
3202
+ const parsed = Number(trimmed);
3203
+ return Number.isFinite(parsed) && parsed >= 1 ? Math.floor(parsed) : 1;
3204
+ },
3205
+ toAttribute: (value) => {
3206
+ if (!Number.isFinite(value))
3207
+ return "infinite";
3208
+ return String(value);
3209
+ },
3210
+ },
3211
+ }),
3212
+ __metadata("design:type", Number)
3213
+ ], SelectElement.prototype, "max", void 0);
3214
+ __decorate([
3215
+ property({ type: String, reflect: true }),
3216
+ __metadata("design:type", Object)
3217
+ ], SelectElement.prototype, "name", void 0);
3218
+ __decorate([
3219
+ property({ type: Boolean, reflect: true }),
3220
+ __metadata("design:type", Object)
3221
+ ], SelectElement.prototype, "disabled", void 0);
3222
+ __decorate([
3223
+ property({ type: Boolean, reflect: true }),
3224
+ __metadata("design:type", Object)
3225
+ ], SelectElement.prototype, "required", void 0);
3226
+ __decorate([
3227
+ property({ type: Boolean, reflect: true }),
3228
+ __metadata("design:type", Object)
3229
+ ], SelectElement.prototype, "open", void 0);
3230
+ SelectElement = SelectElement_1 = __decorate([
3231
+ customElement("tp-select"),
3232
+ __metadata("design:paramtypes", [])
3233
+ ], SelectElement);
3234
+
3235
+ export { DetailsSetElement, DragElement, DragHandleElement, DropZoneElement, OptionElement, PopoverElement, SelectElement, SelectedContentElement, ThaDragEvent, ThaRoute, ThaRouter, ThaRouterConfig, ThaRouterLink, ThaRouterOutlet, ThaRouterParam };
2719
3236
  //# sourceMappingURL=elements.mjs.map