@proximus/lavender-dropdown 1.0.0-alpha.2

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 (2) hide show
  1. package/dist/index.js +139 -0
  2. package/package.json +46 -0
package/dist/index.js ADDED
@@ -0,0 +1,139 @@
1
+ import { WithExtraAttributes as g } from "@proximus/lavender-common";
2
+ const m = ':host{position:relative}@media screen and (max-width: 767px){{display:block;width:100%}}:host([grow]) ::slotted([slot="trigger"]):after{right:0;padding-right:var(--px-padding-xs-mobile)}::slotted([slot="popover"]){position:absolute;border-radius:var(--px-radius-main, 8px);background:var(--px-background-color-action-neutral-bare-default, #FFF);box-shadow:0 20px 25px -5px #25252514;margin:0;padding:0;border:0;padding-block:var(--px-padding-s-mobile);right:var(--px-padding-s-mobile);width:auto}@media screen and (min-width: 768px){{padding-block:var(--px-padding-s-desktop);right:auto;width:auto}}:host([anchoralignment="top-left"]) ::slotted([slot="popover"]){top:auto}:host([anchoralignment="top-right"]) ::slotted([slot="popover"]){top:auto}@media screen and (max-width: 768px){{left:var(--px-padding-s-mobile)}}:host([anchoralignment="bottom-right"]) ::slotted([slot="popover"]){left:auto}', u = [
3
+ "top-left",
4
+ "top-right",
5
+ "bottom-left",
6
+ "bottom-right"
7
+ ];
8
+ function b(a, t, e = "bottom-left", r) {
9
+ "anchorName" in document.documentElement.style || t.addEventListener("toggle", () => {
10
+ var h, l;
11
+ if (!a || !t)
12
+ return;
13
+ const o = a.getBoundingClientRect(), c = ((h = window.visualViewport) == null ? void 0 : h.height) ?? window.innerHeight, i = ((l = window.visualViewport) == null ? void 0 : l.width) ?? window.innerWidth, s = r.getPropertyValue("--px-padding-s-mobile") || "16px", d = parseInt(
14
+ (r.getPropertyValue("--px-icon-size-xs-desktop") || "24").replace("px", "")
15
+ );
16
+ ["top-left", "top-right"].includes(e) ? (t.style.bottom = `calc( ${s} + ${c - o.top - window.scrollY}px)`, t.style.top = "auto", e === "top-left" ? t.style.left = i < 768 ? `${s}px` : `${o.left}px` : t.style.right = i < 768 ? `${s}px` : `${i - o.right}px`) : (t.style.top = `${o.bottom + window.scrollY}px`, e === "bottom-left" ? t.style.left = i < 768 ? `${s}px` : `${o.left}px` : t.style.right = i < 768 ? `${s}px` : `${i - o.right - d}px`);
17
+ });
18
+ }
19
+ const p = new CSSStyleSheet();
20
+ p.replaceSync(m);
21
+ const n = "bottom-left";
22
+ class f extends g {
23
+ constructor() {
24
+ super(p), this.template = () => `
25
+ <style>${this.css}</style>
26
+ <slot name="trigger"></slot>
27
+ <slot name="popover"></slot>`, this.isAboutToClose = !1, this.shadowRoot.innerHTML = this.template();
28
+ }
29
+ /**
30
+ * Instance level styling for the dropdown component.
31
+ * This is used to set the anchor name and position of the popover.
32
+ * @private
33
+ */
34
+ get css() {
35
+ return `::slotted([slot='trigger']) {
36
+ anchor-name: ${this.anchorName};
37
+ }
38
+ :host([anchoralignment="bottom-left"]) {
39
+ ::slotted([slot="popover"]) {
40
+ position-anchor: ${this.anchorName};
41
+ top: anchor(${this.anchorName} bottom);
42
+ left: anchor(${this.anchorName} left);
43
+ }
44
+ }
45
+ :host([anchoralignment="bottom-right"]) {
46
+ ::slotted([slot="popover"]) {
47
+ position-anchor: ${this.anchorName};
48
+ top: anchor(${this.anchorName} bottom);
49
+ right: calc(anchor(${this.anchorName} right) - var(--px-icon-size-xs-desktop));
50
+ }
51
+ }
52
+ :host([anchoralignment="top-left"]) {
53
+ ::slotted([slot="popover"]) {
54
+ position-anchor: ${this.anchorName};
55
+ bottom: calc( var(--px-padding-s-mobile) + anchor(${this.anchorName} top));
56
+ left: anchor(${this.anchorName} left);
57
+ }
58
+ }
59
+ :host([anchoralignment="top-right"]) {
60
+ ::slotted([slot="popover"]) {
61
+ position-anchor: ${this.anchorName};
62
+ bottom: calc( var(--px-padding-s-mobile) + anchor(${this.anchorName} top));
63
+ right: anchor(${this.anchorName} right);
64
+ }
65
+ }`;
66
+ }
67
+ static get observedAttributes() {
68
+ return [...super.observedAttributes, "id", "anchoralignment"];
69
+ }
70
+ connectedCallback() {
71
+ this.getAttribute("id") || this.setAttribute(
72
+ "id",
73
+ `px-dropdown-${Math.random().toString(36).substring(2, 15)}`
74
+ ), this.getAttribute("anchoralignment") || this.setAttribute("anchoralignment", n), b(
75
+ this.$trigger,
76
+ this.$popoverElement,
77
+ this.anchorAlignment,
78
+ getComputedStyle(this)
79
+ );
80
+ }
81
+ /**
82
+ * Handles the manual display of the popover when the trigger is clicked.
83
+ * This is necessary for cases where the trigger is not a button or input element.
84
+ * This method adds event listeners to the trigger element to toggle the popover.
85
+ * It also manages the state of whether the popover is about to close or not to avoid
86
+ * race conditions when the popover is toggled and the trigger click event is fired.
87
+ */
88
+ handleManualPopoverDisplay() {
89
+ this.$trigger && !["button", "input"].includes(this.$trigger.localName) && (this.$trigger.addEventListener("click", () => {
90
+ this.isAboutToClose || this.$popoverElement.togglePopover({ source: this.$trigger });
91
+ }), this.$popoverElement.addEventListener("beforetoggle", () => {
92
+ this.$popoverElement.matches(":popover-open") && (this.isAboutToClose = !0);
93
+ }), this.$popoverElement.addEventListener("toggle", () => {
94
+ this.$popoverElement.matches(":popover-open") || setTimeout(() => {
95
+ this.isAboutToClose = !1;
96
+ });
97
+ }));
98
+ }
99
+ attributeChangedCallback(t, e, r) {
100
+ switch (t) {
101
+ case "id":
102
+ e !== r && this.initPopover();
103
+ break;
104
+ default:
105
+ super.attributeChangedCallback(t, e, r);
106
+ break;
107
+ }
108
+ }
109
+ initPopover() {
110
+ this.$trigger && (this.$trigger.setAttribute("popovertarget", this.popoverId), this.$popoverElement.setAttribute("id", this.popoverId), this.$popoverElement.setAttribute("popover", ""), this.$style.innerHTML = this.css, this.handleManualPopoverDisplay());
111
+ }
112
+ get popoverId() {
113
+ return `${this.getAttribute("id")}-popover`;
114
+ }
115
+ get anchorName() {
116
+ return `--${this.getAttribute("id")}-anchor`;
117
+ }
118
+ get $style() {
119
+ return this.shadowRoot.querySelector("style");
120
+ }
121
+ get $trigger() {
122
+ return this.querySelector('[slot="trigger"]');
123
+ }
124
+ get $popoverElement() {
125
+ return this.querySelector('[slot="popover"]');
126
+ }
127
+ get anchorAlignment() {
128
+ return this.getAttribute("anchoralignment") || n;
129
+ }
130
+ set anchorAlignment(t) {
131
+ u.includes(t) ? this.setAttribute("anchoralignment", t) : (console.warn(
132
+ `Invalid anchor alignment value: ${t}. Using default ${n}.`
133
+ ), this.setAttribute("anchoralignment", n));
134
+ }
135
+ }
136
+ customElements.get("px-dropdown") || customElements.define("px-dropdown", f);
137
+ export {
138
+ f as Dropdown
139
+ };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@proximus/lavender-dropdown",
3
+ "version": "1.0.0-alpha.2",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./src/index.ts",
9
+ "development": "./src/index.ts",
10
+ "default": "./dist/index.js"
11
+ },
12
+ "./src/*.css": {
13
+ "development": "src/*.css"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "type": "module",
20
+ "scripts": {
21
+ "build": "rm -rf dist;tsc; vite build",
22
+ "test": "vitest run --coverage"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "lerna": {
28
+ "command": {
29
+ "publish": {
30
+ "assets": [
31
+ "CHANGELOG.md",
32
+ "package.json",
33
+ "dist/*.js",
34
+ "dist/*.cjs",
35
+ "dist/css/**/*.css",
36
+ "dist/js/**/*.js",
37
+ "dist/*.svg",
38
+ "dist/*.ttf",
39
+ "dist/glyphmap.json",
40
+ "dist/*.d.ts"
41
+ ]
42
+ }
43
+ }
44
+ },
45
+ "gitHead": "127dc25543d5b6545c1611a6d91a6ebeb7e98fb1"
46
+ }