@telperion/elements 2.0.1 → 3.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.
@@ -66,7 +66,18 @@
66
66
  "dts": "details-set/index.d.ts",
67
67
  "dcts": "details-set/index.d.cts"
68
68
  }
69
+ },
70
+ {
71
+ "name": "popover/index",
72
+ "sourcePath": "src/popover/index.ts",
73
+ "outputs": {
74
+ "esm": "popover/index.js",
75
+ "cjs": "popover/index.cjs",
76
+ "iife": "iife/popover/index.js",
77
+ "dts": "popover/index.d.ts",
78
+ "dcts": "popover/index.d.cts"
79
+ }
69
80
  }
70
81
  ],
71
- "generatedAt": "2026-04-30T11:34:29.707Z"
82
+ "generatedAt": "2026-05-01T13:46:36.220Z"
72
83
  }
package/README.md CHANGED
@@ -59,6 +59,9 @@ Use the **explicit `/iife/...` path** so the CDN serves the standalone bundle (s
59
59
 
60
60
  <!-- Or just the details-set module (TelperionElements.DetailsSet) -->
61
61
  <script src="https://unpkg.com/@telperion/elements/iife/details-set/index.js"></script>
62
+
63
+ <!-- Or just the popover module (TelperionElements.Popover) -->
64
+ <script src="https://unpkg.com/@telperion/elements/iife/popover/index.js"></script>
62
65
  ```
63
66
 
64
67
  Each per-module bundle is fully self-contained: side-effect imports register the custom elements as soon as the script is evaluated. jsDelivr (`https://cdn.jsdelivr.net/npm/@telperion/elements/iife/...`) and other npm CDNs work the same way.
@@ -160,6 +163,48 @@ import "@telperion/elements/details-set";
160
163
 
161
164
  ---
162
165
 
166
+ ### Popover
167
+
168
+ Framework-agnostic popover element built on the native [Popover API](https://developer.mozilla.org/docs/Web/API/Popover_API) and [CSS Anchor Positioning](https://developer.mozilla.org/docs/Web/CSS/CSS_anchor_positioning). Placement is **pure CSS** — JavaScript only parses attributes, resolves the anchor, and (optionally) wires triggers.
169
+
170
+ **Includes:**
171
+ - `<tp-popover>` &mdash; auto-applies the native `popover` attribute and anchors itself via CSS
172
+
173
+ **Features:**
174
+ - Auto `popover` attribute (`auto` by default; switch with `mode="manual"`)
175
+ - Anchor resolved from a `target` querySelector string, falling back to `parentElement`
176
+ - Declarative `position` syntax with three forms:
177
+ - Full: `<pop-inline> to <target-inline> / <pop-block> to <target-block>`
178
+ - Two-keyword: `<inline> / <block>` (e.g. `center / top`)
179
+ - Single axis or single keyword (e.g. `top to bottom`, `start`, `bottom`)
180
+ - Inline keywords: `start | center | end`; block keywords: `top | middle | bottom`
181
+ - Optional `trigger="click" | "hover"` (hover also opens on focus)
182
+ - Use plain CSS `margin` on the popover for a gap from its anchor
183
+ - Automatic edge-flipping via the default `position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline` (override with `position-try-fallbacks: none`)
184
+ - Light-DOM rendering; no JS positioning loop
185
+
186
+ #### JS/TS
187
+ ```ts
188
+ import "@telperion/elements/popover";
189
+ ```
190
+
191
+ #### HTML
192
+ ```html
193
+ <div>
194
+ <button>Anchor</button>
195
+ <tp-popover position="top">Above the parent.</tp-popover>
196
+ </div>
197
+
198
+ <button id="trigger">Click me</button>
199
+ <tp-popover target="#trigger" position="start / bottom" trigger="click">
200
+ Anchored via target="#trigger".
201
+ </tp-popover>
202
+ ```
203
+
204
+ [Popover Documentation →](./src/popover/README.md)
205
+
206
+ ---
207
+
163
208
  This library is actively being developed. More components will be added over time.
164
209
 
165
210
  ## 🤝 Contributing
@@ -168,4 +213,4 @@ Contributions are welcome! Please feel free to submit a Pull Request.
168
213
 
169
214
  ## 📄 License
170
215
 
171
- MIT © [Thales RC](https://github.com/thalesrc)
216
+ MIT © [Telperion Technology](https://github.com/telperiontech)
package/fesm/elements.mjs CHANGED
@@ -7,6 +7,7 @@ import { createMemoryHistory, createHashHistory, createBrowserHistory } from 'hi
7
7
  import { defer } from '@telperion/js-utils/function/defer';
8
8
  import { compact } from '@telperion/js-utils/array/compact';
9
9
  import { getParents } from '@thalesrc/dom-utils/get-parents';
10
+ import { uniqueId } from '@telperion/js-utils/unique-id';
10
11
 
11
12
  var _a$2, _b;
12
13
  /**
@@ -2323,5 +2324,396 @@ DetailsSetElement = __decorate([
2323
2324
  customElement("tp-details-set")
2324
2325
  ], DetailsSetElement);
2325
2326
 
2326
- export { DetailsSetElement, DragElement, DragHandleElement, DropZoneElement, ThaDragEvent, ThaRoute, ThaRouter, ThaRouterConfig, ThaRouterLink, ThaRouterOutlet, ThaRouterParam };
2327
+ /**
2328
+ * Positioning utilities for `tp-popover`.
2329
+ *
2330
+ * The element's `position` attribute is a small DSL describing where the
2331
+ * popover edge attaches to the anchor edge on each axis. This module owns
2332
+ * the parsing of that DSL into a normalised {@link ParsedPosition}.
2333
+ */
2334
+ const DEFAULT_POSITION = {
2335
+ popInline: "center",
2336
+ targetInline: "center",
2337
+ popBlock: "bottom",
2338
+ targetBlock: "top",
2339
+ };
2340
+ const INLINE_KEYWORDS = new Set(["start", "center", "end"]);
2341
+ const BLOCK_KEYWORDS = new Set(["top", "middle", "bottom"]);
2342
+ /** Inline shorthand → `<pop-inline> to <target-inline>`. */
2343
+ const INLINE_SHORTHAND = {
2344
+ start: ["end", "start"],
2345
+ center: ["center", "center"],
2346
+ end: ["start", "end"],
2347
+ };
2348
+ /** Block shorthand → `<pop-block> to <target-block>`. */
2349
+ const BLOCK_SHORTHAND = {
2350
+ top: ["bottom", "top"],
2351
+ middle: ["middle", "middle"],
2352
+ bottom: ["top", "bottom"],
2353
+ };
2354
+ function parseInlineHalf(raw) {
2355
+ const parts = raw.trim().split(/\s+to\s+/);
2356
+ if (parts.length === 1) {
2357
+ const k = parts[0];
2358
+ return INLINE_KEYWORDS.has(k) ? INLINE_SHORTHAND[k] : null;
2359
+ }
2360
+ if (parts.length === 2 && INLINE_KEYWORDS.has(parts[0]) && INLINE_KEYWORDS.has(parts[1])) {
2361
+ return [parts[0], parts[1]];
2362
+ }
2363
+ return null;
2364
+ }
2365
+ function parseBlockHalf(raw) {
2366
+ const parts = raw.trim().split(/\s+to\s+/);
2367
+ if (parts.length === 1) {
2368
+ const k = parts[0];
2369
+ return BLOCK_KEYWORDS.has(k) ? BLOCK_SHORTHAND[k] : null;
2370
+ }
2371
+ if (parts.length === 2 && BLOCK_KEYWORDS.has(parts[0]) && BLOCK_KEYWORDS.has(parts[1])) {
2372
+ return [parts[0], parts[1]];
2373
+ }
2374
+ return null;
2375
+ }
2376
+ /**
2377
+ * Parse a `position` attribute value. Supported forms:
2378
+ *
2379
+ * - Full: `"<pop-inline> to <target-inline> / <pop-block> to <target-block>"`
2380
+ * - Two-keyword: `"<inline> / <block>"` (each half may also be the full
2381
+ * `"a to b"` form). E.g. `"center / top"` → `"center to center / bottom to top"`.
2382
+ * - Single axis: one half only (no `/`) — either `"<a> to <b>"` or a single
2383
+ * keyword. The other axis defaults to its centered/middle form. E.g.
2384
+ * `"top to bottom"` → `"center to center / top to bottom"`,
2385
+ * `"start to center"` → `"start to center / middle to middle"`,
2386
+ * `"start"` → `"end to start / middle to middle"`.
2387
+ *
2388
+ * Returns `null` for invalid input so callers can warn and fall back.
2389
+ */
2390
+ function parsePosition(raw) {
2391
+ if (!raw)
2392
+ return null;
2393
+ const trimmed = raw.trim().toLowerCase();
2394
+ if (!trimmed)
2395
+ return null;
2396
+ // Single axis (no `/`): either a single keyword or `"<a> to <b>"`.
2397
+ if (!trimmed.includes("/")) {
2398
+ const inline = parseInlineHalf(trimmed);
2399
+ if (inline) {
2400
+ const [popBlock, targetBlock] = BLOCK_SHORTHAND.middle;
2401
+ return { popInline: inline[0], targetInline: inline[1], popBlock, targetBlock };
2402
+ }
2403
+ const block = parseBlockHalf(trimmed);
2404
+ if (block) {
2405
+ const [popInline, targetInline] = INLINE_SHORTHAND.center;
2406
+ return { popInline, targetInline, popBlock: block[0], targetBlock: block[1] };
2407
+ }
2408
+ return null;
2409
+ }
2410
+ const halves = trimmed.split("/");
2411
+ if (halves.length !== 2)
2412
+ return null;
2413
+ const inline = parseInlineHalf(halves[0]);
2414
+ const block = parseBlockHalf(halves[1]);
2415
+ if (!inline || !block)
2416
+ return null;
2417
+ return {
2418
+ popInline: inline[0],
2419
+ targetInline: inline[1],
2420
+ popBlock: block[0],
2421
+ targetBlock: block[1],
2422
+ };
2423
+ }
2424
+
2425
+ let PopoverElement = class PopoverElement extends LitElement {
2426
+ constructor() {
2427
+ super(...arguments);
2428
+ /** Mirrored to the native `popover` attribute. */
2429
+ this.mode = "auto";
2430
+ /** CSS query selector resolved against `document`. */
2431
+ this.target = null;
2432
+ /** Placement spec — see class docs. */
2433
+ this.position = null;
2434
+ /** How the resolved target opens the popover. */
2435
+ this.trigger = "manual";
2436
+ /** Per-instance anchor id (without the leading `--`). */
2437
+ this.#anchorId = uniqueId("tp-popover");
2438
+ /** Currently resolved anchor element (if any). */
2439
+ this.#anchorEl = null;
2440
+ /** Whether we set `anchor-name` on the current anchor (so we clean up). */
2441
+ this.#anchorNameSet = false;
2442
+ /** Bound trigger listener cleanups for the current anchor. */
2443
+ this.#triggerCleanups = [];
2444
+ }
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 = `
2459
+ tp-popover {
2460
+ --tp-popover-inline-position: attr(data-ti type(*));
2461
+ --tp-popover-block-position: attr(data-tb type(*));
2462
+ inset: auto;
2463
+ translate: var(--tp-popover-translate-x, 0) var(--tp-popover-translate-y, 0);
2464
+ position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
2465
+
2466
+ &[data-tb="middle"] {
2467
+ --tp-popover-block-position: center;
2468
+ }
2469
+
2470
+ &[data-pi="start"],
2471
+ &[data-pi="center"] {
2472
+ inset-inline-start: anchor(var(--tp-popover-inline-position));
2473
+ }
2474
+
2475
+ &[data-pi="center"] {
2476
+ --tp-popover-translate-x: -50%;
2477
+ }
2478
+
2479
+ &[data-pi="end"] {
2480
+ inset-inline-end: anchor(var(--tp-popover-inline-position));
2481
+ }
2482
+
2483
+ &[data-pb="top"],
2484
+ &[data-pb="middle"] {
2485
+ inset-block-start: anchor(var(--tp-popover-block-position));
2486
+ }
2487
+
2488
+ &[data-pb="middle"] {
2489
+ --tp-popover-translate-y: -50%;
2490
+ }
2491
+
2492
+ &[data-pb="bottom"] {
2493
+ inset-block-end: anchor(var(--tp-popover-block-position));
2494
+ }
2495
+ }
2496
+ `;
2497
+ document.head.appendChild(style);
2498
+ }
2499
+ /** Per-instance anchor id (without the leading `--`). */
2500
+ #anchorId;
2501
+ /** Currently resolved anchor element (if any). */
2502
+ #anchorEl;
2503
+ /** The currently-resolved anchor element. */
2504
+ get anchorElement() {
2505
+ return this.#anchorEl;
2506
+ }
2507
+ /** Custom-property name (with leading `--`) bound on the anchor. */
2508
+ get anchorName() {
2509
+ return `--${this.#anchorId}`;
2510
+ }
2511
+ /** Whether we set `anchor-name` on the current anchor (so we clean up). */
2512
+ #anchorNameSet;
2513
+ /** Bound trigger listener cleanups for the current anchor. */
2514
+ #triggerCleanups;
2515
+ createRenderRoot() {
2516
+ return this;
2517
+ }
2518
+ connectedCallback() {
2519
+ super.connectedCallback();
2520
+ this.#syncMode();
2521
+ this.#syncAnchor();
2522
+ this.#syncPosition();
2523
+ this.#syncTrigger();
2524
+ }
2525
+ disconnectedCallback() {
2526
+ super.disconnectedCallback();
2527
+ this.#clearTrigger();
2528
+ this.#releaseAnchor();
2529
+ }
2530
+ updated(changed) {
2531
+ if (changed.has("mode"))
2532
+ this.#syncMode();
2533
+ if (changed.has("target")) {
2534
+ this.#syncAnchor();
2535
+ this.#syncTrigger();
2536
+ }
2537
+ if (changed.has("position"))
2538
+ this.#syncPosition();
2539
+ if (changed.has("trigger"))
2540
+ this.#syncTrigger();
2541
+ }
2542
+ render() {
2543
+ return html$1 ``;
2544
+ }
2545
+ // -- Mode / popover attr --------------------------------------------------
2546
+ #syncMode() {
2547
+ const value = this.mode === "manual" ? "manual" : "auto";
2548
+ if (this.getAttribute("popover") !== value) {
2549
+ this.setAttribute("popover", value);
2550
+ }
2551
+ }
2552
+ // -- Anchor resolution ----------------------------------------------------
2553
+ #resolveTarget() {
2554
+ const selector = this.target;
2555
+ if (selector) {
2556
+ try {
2557
+ const el = document.querySelector(selector);
2558
+ if (el instanceof HTMLElement)
2559
+ return el;
2560
+ console.warn(`<tp-popover>: target "${selector}" did not resolve to an HTMLElement; using parentElement.`);
2561
+ }
2562
+ catch {
2563
+ console.warn(`<tp-popover>: invalid target selector "${selector}"; using parentElement.`);
2564
+ }
2565
+ }
2566
+ const parent = this.parentElement;
2567
+ return parent instanceof HTMLElement ? parent : null;
2568
+ }
2569
+ #syncAnchor() {
2570
+ const next = this.#resolveTarget();
2571
+ if (next === this.#anchorEl) {
2572
+ // Re-apply in case the target lost its anchor-name in between.
2573
+ if (next)
2574
+ this.#applyAnchorTo(next);
2575
+ return;
2576
+ }
2577
+ this.#releaseAnchor();
2578
+ this.#anchorEl = next;
2579
+ if (next)
2580
+ this.#applyAnchorTo(next);
2581
+ }
2582
+ #applyAnchorTo(el) {
2583
+ const cssName = `--${this.#anchorId}`;
2584
+ const existing = el.style.getPropertyValue("anchor-name").trim();
2585
+ if (existing && existing !== cssName) {
2586
+ // Don't overwrite a pre-existing anchor-name; just bind to it.
2587
+ this.style.setProperty("position-anchor", existing);
2588
+ this.#anchorNameSet = false;
2589
+ return;
2590
+ }
2591
+ el.style.setProperty("anchor-name", cssName);
2592
+ this.style.setProperty("position-anchor", cssName);
2593
+ this.#anchorNameSet = true;
2594
+ }
2595
+ #releaseAnchor() {
2596
+ if (this.#anchorEl && this.#anchorNameSet) {
2597
+ const cssName = `--${this.#anchorId}`;
2598
+ if (this.#anchorEl.style.getPropertyValue("anchor-name").trim() === cssName) {
2599
+ this.#anchorEl.style.removeProperty("anchor-name");
2600
+ }
2601
+ }
2602
+ this.#anchorEl = null;
2603
+ this.#anchorNameSet = false;
2604
+ this.style.removeProperty("position-anchor");
2605
+ }
2606
+ // -- Position parsing -----------------------------------------------------
2607
+ #syncPosition() {
2608
+ let parsed = parsePosition(this.position);
2609
+ if (this.position && !parsed) {
2610
+ console.warn(`<tp-popover>: invalid position "${this.position}"; using default.`);
2611
+ }
2612
+ parsed ??= DEFAULT_POSITION;
2613
+ this.dataset["pi"] = parsed.popInline;
2614
+ this.dataset["ti"] = parsed.targetInline;
2615
+ this.dataset["pb"] = parsed.popBlock;
2616
+ this.dataset["tb"] = parsed.targetBlock;
2617
+ }
2618
+ // -- Trigger handling -----------------------------------------------------
2619
+ #clearTrigger() {
2620
+ for (const fn of this.#triggerCleanups)
2621
+ fn();
2622
+ this.#triggerCleanups = [];
2623
+ }
2624
+ #syncTrigger() {
2625
+ this.#clearTrigger();
2626
+ const anchor = this.#anchorEl;
2627
+ if (!anchor)
2628
+ return;
2629
+ if (this.trigger === "click") {
2630
+ const onClick = (event) => {
2631
+ // If the click bubbled up from inside the popover itself (which
2632
+ // happens when the anchor is an ancestor of the popover), ignore
2633
+ // it — otherwise the popover would close immediately on every
2634
+ // interaction with its own content.
2635
+ if (event.composedPath().includes(this))
2636
+ return;
2637
+ try {
2638
+ this.togglePopover();
2639
+ }
2640
+ catch {
2641
+ /* not connected / unsupported */
2642
+ }
2643
+ };
2644
+ anchor.addEventListener("click", onClick);
2645
+ this.#triggerCleanups.push(() => anchor.removeEventListener("click", onClick));
2646
+ return;
2647
+ }
2648
+ if (this.trigger === "hover") {
2649
+ let hideTimer = null;
2650
+ const cancelHide = () => {
2651
+ if (hideTimer !== null) {
2652
+ clearTimeout(hideTimer);
2653
+ hideTimer = null;
2654
+ }
2655
+ };
2656
+ const show = () => {
2657
+ cancelHide();
2658
+ try {
2659
+ this.showPopover();
2660
+ }
2661
+ catch {
2662
+ /* already open / unsupported */
2663
+ }
2664
+ };
2665
+ const scheduleHide = () => {
2666
+ cancelHide();
2667
+ hideTimer = setTimeout(() => {
2668
+ try {
2669
+ this.hidePopover();
2670
+ }
2671
+ catch {
2672
+ /* already closed / unsupported */
2673
+ }
2674
+ }, 120);
2675
+ };
2676
+ anchor.addEventListener("pointerenter", show);
2677
+ anchor.addEventListener("pointerleave", scheduleHide);
2678
+ anchor.addEventListener("focusin", show);
2679
+ anchor.addEventListener("focusout", scheduleHide);
2680
+ this.addEventListener("pointerenter", cancelHide);
2681
+ this.addEventListener("pointerleave", scheduleHide);
2682
+ this.addEventListener("focusin", cancelHide);
2683
+ this.addEventListener("focusout", scheduleHide);
2684
+ this.#triggerCleanups.push(() => {
2685
+ cancelHide();
2686
+ anchor.removeEventListener("pointerenter", show);
2687
+ anchor.removeEventListener("pointerleave", scheduleHide);
2688
+ anchor.removeEventListener("focusin", show);
2689
+ anchor.removeEventListener("focusout", scheduleHide);
2690
+ this.removeEventListener("pointerenter", cancelHide);
2691
+ this.removeEventListener("pointerleave", scheduleHide);
2692
+ this.removeEventListener("focusin", cancelHide);
2693
+ this.removeEventListener("focusout", scheduleHide);
2694
+ });
2695
+ }
2696
+ }
2697
+ };
2698
+ __decorate([
2699
+ property({ reflect: true }),
2700
+ __metadata("design:type", String)
2701
+ ], PopoverElement.prototype, "mode", void 0);
2702
+ __decorate([
2703
+ property({ reflect: true }),
2704
+ __metadata("design:type", Object)
2705
+ ], PopoverElement.prototype, "target", void 0);
2706
+ __decorate([
2707
+ property({ reflect: true }),
2708
+ __metadata("design:type", Object)
2709
+ ], PopoverElement.prototype, "position", void 0);
2710
+ __decorate([
2711
+ property({ reflect: true }),
2712
+ __metadata("design:type", String)
2713
+ ], PopoverElement.prototype, "trigger", void 0);
2714
+ PopoverElement = __decorate([
2715
+ customElement("tp-popover")
2716
+ ], PopoverElement);
2717
+
2718
+ export { DetailsSetElement, DragElement, DragHandleElement, DropZoneElement, PopoverElement, ThaDragEvent, ThaRoute, ThaRouter, ThaRouterConfig, ThaRouterLink, ThaRouterOutlet, ThaRouterParam };
2327
2719
  //# sourceMappingURL=elements.mjs.map