@stainless-api/ui-primitives 0.1.0-beta.11 → 0.1.0-beta.12

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.
@@ -1,2 +1,35 @@
1
- export * from './dropdown-button';
2
- //# sourceMappingURL=index.js.map
1
+ //#region src/scripts/dropdown-button.ts
2
+ function initDropdownButton({ dropdownId, onSelect, onPrimaryAction }) {
3
+ const dropdown = document.getElementById(dropdownId);
4
+ if (!dropdown) return;
5
+ const trigger = dropdown.querySelector("[data-part=\"trigger\"]");
6
+ const menu = dropdown.querySelector("[data-part=\"menu\"]");
7
+ const primaryAction = dropdown.querySelector("[data-part=\"primary-action\"]");
8
+ if (!trigger || !menu || !primaryAction) return;
9
+ let isOpen = false;
10
+ function toggleDropdown() {
11
+ if (!trigger || !menu) return;
12
+ isOpen = !isOpen;
13
+ menu.dataset.state = isOpen ? "open" : "closed";
14
+ trigger.setAttribute("aria-expanded", String(isOpen));
15
+ }
16
+ trigger.addEventListener("click", toggleDropdown);
17
+ document.addEventListener("click", (event) => {
18
+ if (!isOpen) return;
19
+ if (!dropdown.contains(event.target)) toggleDropdown();
20
+ if (primaryAction && primaryAction.contains(event.target)) toggleDropdown();
21
+ });
22
+ dropdown.querySelectorAll("[data-part=\"item\"]").forEach((item) => {
23
+ item.addEventListener("click", () => {
24
+ const value = item.getAttribute("data-value");
25
+ if (value) onSelect(value);
26
+ toggleDropdown();
27
+ });
28
+ });
29
+ primaryAction.addEventListener("click", () => {
30
+ onPrimaryAction(primaryAction);
31
+ });
32
+ }
33
+
34
+ //#endregion
35
+ export { initDropdownButton };