chunk.UOG2BNX2.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import {
  2. createPopper
  3. } from "./chunk.GADG7AUG.js";
  4. import {
  5. getNearestTabbableElement
  6. } from "./chunk.DVN52LS5.js";
  7. import {
  8. scrollIntoView
  9. } from "./chunk.XAZN5AQ5.js";
  10. import {
  11. animateTo,
  12. stopAnimations,
  13. waitForEvent
  14. } from "./chunk.CTCDC263.js";
  15. import {
  16. getAnimation,
  17. setDefaultAnimation
  18. } from "./chunk.NC36RJW4.js";
  19. import {
  20. event,
  21. watch
  22. } from "./chunk.XX234VRK.js";
  23. import {
  24. e as e2
  25. } from "./chunk.YXKHB4AC.js";
  26. import {
  27. T,
  28. e,
  29. h,
  30. n,
  31. o,
  32. r
  33. } from "./chunk.5PIDMFOE.js";
  34. import {
  35. __decorateClass
  36. } from "./chunk.IHGPZX35.js";
  37. // _uyihdawu1:/Users/claviska/Projects/shoelace/src/components/dropdown/dropdown.scss
  38. var dropdown_default = ":host {\n position: relative;\n box-sizing: border-box;\n}\n:host *, :host *:before, :host *:after {\n box-sizing: inherit;\n}\n\n[hidden] {\n display: none !important;\n}\n\n:host {\n display: inline-block;\n}\n\n.dropdown {\n position: relative;\n}\n\n.dropdown__trigger {\n display: block;\n}\n\n.dropdown__positioner {\n position: absolute;\n z-index: var(--sl-z-index-dropdown);\n}\n\n.dropdown__panel {\n max-height: 75vh;\n font-family: var(--sl-font-sans);\n font-size: var(--sl-font-size-medium);\n font-weight: var(--sl-font-weight-normal);\n color: var(--color);\n background-color: var(--sl-panel-background-color);\n border: solid 1px var(--sl-panel-border-color);\n border-radius: var(--sl-border-radius-medium);\n box-shadow: var(--sl-shadow-large);\n overflow: auto;\n overscroll-behavior: none;\n}\n\n.dropdown__positioner[data-popper-placement^=top] .dropdown__panel {\n transform-origin: bottom;\n}\n.dropdown__positioner[data-popper-placement^=bottom] .dropdown__panel {\n transform-origin: top;\n}\n.dropdown__positioner[data-popper-placement^=left] .dropdown__panel {\n transform-origin: right;\n}\n.dropdown__positioner[data-popper-placement^=right] .dropdown__panel {\n transform-origin: left;\n}";
  39. // src/components/dropdown/dropdown.ts
  40. var id = 0;
  41. var SlDropdown = class extends h {
  42. constructor() {
  43. super(...arguments);
  44. this.componentId = `dropdown-${++id}`;
  45. this.hasInitialized = false;
  46. this.open = false;
  47. this.placement = "bottom-start";
  48. this.disabled = false;
  49. this.closeOnSelect = true;
  50. this.distance = 2;
  51. this.skidding = 0;
  52. this.hoist = false;
  53. }
  54. connectedCallback() {
  55. super.connectedCallback();
  56. this.handleMenuItemActivate = this.handleMenuItemActivate.bind(this);
  57. this.handlePanelSelect = this.handlePanelSelect.bind(this);
  58. this.handleDocumentKeyDown = this.handleDocumentKeyDown.bind(this);
  59. this.handleDocumentMouseDown = this.handleDocumentMouseDown.bind(this);
  60. if (!this.containingElement) {
  61. this.containingElement = this;
  62. }
  63. this.updateComplete.then(() => {
  64. this.popover = createPopper(this.trigger, this.positioner, {
  65. placement: this.placement,
  66. strategy: this.hoist ? "fixed" : "absolute",
  67. modifiers: [
  68. {
  69. name: "flip",
  70. options: {
  71. boundary: "viewport"
  72. }
  73. },
  74. {
  75. name: "offset",
  76. options: {
  77. offset: [this.skidding, this.distance]
  78. }
  79. }
  80. ]
  81. });
  82. });
  83. }
  84. firstUpdated() {
  85. this.panel.hidden = !this.open;
  86. this.updateComplete.then(() => this.hasInitialized = true);
  87. }
  88. disconnectedCallback() {
  89. super.disconnectedCallback();
  90. this.hide();
  91. this.popover.destroy();
  92. }
  93. focusOnTrigger() {
  94. const slot = this.trigger.querySelector("slot");
  95. const trigger = slot.assignedElements({ flatten: true })[0];
  96. if (trigger && typeof trigger.focus === "function") {
  97. trigger.focus();
  98. }
  99. }
  100. getMenu() {
  101. const slot = this.panel.querySelector("slot");
  102. return slot.assignedElements({ flatten: true }).filter((el) => el.tagName.toLowerCase() === "sl-menu")[0];
  103. }
  104. handleDocumentKeyDown(event2) {
  105. var _a;
  106. if (event2.key === "Escape") {
  107. this.hide();
  108. this.focusOnTrigger();
  109. return;
  110. }
  111. if (event2.key === "Tab") {
  112. if (this.open && ((_a = document.activeElement) == null ? void 0 : _a.tagName.toLowerCase()) === "sl-menu-item") {
  113. event2.preventDefault();
  114. this.hide();
  115. this.focusOnTrigger();
  116. return;
  117. }
  118. setTimeout(() => {
  119. var _a2, _b;
  120. const activeElement = this.containingElement.getRootNode() instanceof ShadowRoot ? (_b = (_a2 = document.activeElement) == null ? void 0 : _a2.shadowRoot) == null ? void 0 : _b.activeElement : document.activeElement;
  121. if ((activeElement == null ? void 0 : activeElement.closest(this.containingElement.tagName.toLowerCase())) !== this.containingElement) {
  122. this.hide();
  123. return;
  124. }
  125. });
  126. }
  127. }
  128. handleDocumentMouseDown(event2) {
  129. const path = event2.composedPath();
  130. if (!path.includes(this.containingElement)) {
  131. this.hide();
  132. return;
  133. }
  134. }
  135. handleMenuItemActivate(event2) {
  136. const item = event2.target;
  137. scrollIntoView(item, this.panel);
  138. }
  139. handlePanelSelect(event2) {
  140. const target = event2.target;
  141. if (this.closeOnSelect && target.tagName.toLowerCase() === "sl-menu") {
  142. this.hide();
  143. this.focusOnTrigger();
  144. }
  145. }
  146. handlePopoverOptionsChange() {
  147. if (this.popover) {
  148. this.popover.setOptions({
  149. placement: this.placement,
  150. strategy: this.hoist ? "fixed" : "absolute",
  151. modifiers: [
  152. {
  153. name: "flip",
  154. options: {
  155. boundary: "viewport"
  156. }
  157. },
  158. {
  159. name: "offset",
  160. options: {
  161. offset: [this.skidding, this.distance]
  162. }
  163. }
  164. ]
  165. });
  166. }
  167. }
  168. handleTriggerClick() {
  169. this.open ? this.hide() : this.show();
  170. }
  171. handleTriggerKeyDown(event2) {
  172. const menu = this.getMenu();
  173. const menuItems = menu ? [...menu.querySelectorAll("sl-menu-item")] : [];
  174. const firstMenuItem = menuItems[0];
  175. const lastMenuItem = menuItems[menuItems.length - 1];
  176. if (event2.key === "Escape") {
  177. this.focusOnTrigger();
  178. this.hide();
  179. return;
  180. }
  181. if ([" ", "Enter"].includes(event2.key)) {
  182. event2.preventDefault();
  183. this.open ? this.hide() : this.show();
  184. return;
  185. }
  186. if (["ArrowDown", "ArrowUp"].includes(event2.key)) {
  187. event2.preventDefault();
  188. if (!this.open) {
  189. this.show();
  190. }
  191. if (event2.key === "ArrowDown" && firstMenuItem) {
  192. firstMenuItem.focus();
  193. return;
  194. }
  195. if (event2.key === "ArrowUp" && lastMenuItem) {
  196. lastMenuItem.focus();
  197. return;
  198. }
  199. }
  200. const ignoredKeys = ["Tab", "Shift", "Meta", "Ctrl", "Alt"];
  201. if (this.open && menu && !ignoredKeys.includes(event2.key)) {
  202. menu.typeToSelect(event2.key);
  203. return;
  204. }
  205. }
  206. handleTriggerKeyUp(event2) {
  207. if (event2.key === " ") {
  208. event2.preventDefault();
  209. }
  210. }
  211. handleTriggerSlotChange() {
  212. this.updateAccessibleTrigger();
  213. }
  214. updateAccessibleTrigger() {
  215. if (this.trigger) {
  216. const slot = this.trigger.querySelector("slot");
  217. const assignedElements = slot.assignedElements({ flatten: true });
  218. const accessibleTrigger = assignedElements.map(getNearestTabbableElement)[0];
  219. if (accessibleTrigger) {
  220. accessibleTrigger.setAttribute("aria-haspopup", "true");
  221. accessibleTrigger.setAttribute("aria-expanded", this.open ? "true" : "false");
  222. }
  223. }
  224. }
  225. async show() {
  226. if (this.open) {
  227. return;
  228. }
  229. this.open = true;
  230. return waitForEvent(this, "sl-after-show");
  231. }
  232. async hide() {
  233. if (!this.open) {
  234. return;
  235. }
  236. this.open = false;
  237. return waitForEvent(this, "sl-after-hide");
  238. }
  239. reposition() {
  240. if (!this.open) {
  241. return;
  242. }
  243. this.popover.update();
  244. }
  245. async handleOpenChange() {
  246. if (!this.hasInitialized || this.disabled) {
  247. return;
  248. }
  249. this.updateAccessibleTrigger();
  250. if (this.open) {
  251. this.slShow.emit();
  252. this.panel.addEventListener("sl-activate", this.handleMenuItemActivate);
  253. this.panel.addEventListener("sl-select", this.handlePanelSelect);
  254. document.addEventListener("keydown", this.handleDocumentKeyDown);
  255. document.addEventListener("mousedown", this.handleDocumentMouseDown);
  256. await stopAnimations(this);
  257. this.popover.update();
  258. this.panel.hidden = false;
  259. const { keyframes, options } = getAnimation(this, "dropdown.show");
  260. await animateTo(this.panel, keyframes, options);
  261. this.slAfterShow.emit();
  262. } else {
  263. this.slHide.emit();
  264. this.panel.removeEventListener("sl-activate", this.handleMenuItemActivate);
  265. this.panel.removeEventListener("sl-select", this.handlePanelSelect);
  266. document.addEventListener("keydown", this.handleDocumentKeyDown);
  267. document.removeEventListener("mousedown", this.handleDocumentMouseDown);
  268. await stopAnimations(this);
  269. const { keyframes, options } = getAnimation(this, "dropdown.hide");
  270. await animateTo(this.panel, keyframes, options);
  271. this.panel.hidden = true;
  272. this.slAfterHide.emit();
  273. }
  274. }
  275. render() {
  276. return T`
  277. <div
  278. part="base"
  279. id=${this.componentId}
  280. class=${e2({
  281. dropdown: true,
  282. "dropdown--open": this.open
  283. })}
  284. >
  285. <span
  286. part="trigger"
  287. class="dropdown__trigger"
  288. @click=${this.handleTriggerClick}
  289. @keydown=${this.handleTriggerKeyDown}
  290. @keyup=${this.handleTriggerKeyUp}
  291. >
  292. <slot name="trigger" @slotchange=${this.handleTriggerSlotChange}></slot>
  293. </span>
  294. <!-- Position the panel with a wrapper since the popover makes use of translate. This let's us add animations
  295. on the panel without interfering with the position. -->
  296. <div class="dropdown__positioner">
  297. <div
  298. part="panel"
  299. class="dropdown__panel"
  300. role="menu"
  301. aria-hidden=${this.open ? "false" : "true"}
  302. aria-labelledby=${this.componentId}
  303. >
  304. <slot></slot>
  305. </div>
  306. </div>
  307. </div>
  308. `;
  309. }
  310. };
  311. SlDropdown.styles = r(dropdown_default);
  312. __decorateClass([
  313. o(".dropdown__trigger")
  314. ], SlDropdown.prototype, "trigger", 2);
  315. __decorateClass([
  316. o(".dropdown__panel")
  317. ], SlDropdown.prototype, "panel", 2);
  318. __decorateClass([
  319. o(".dropdown__positioner")
  320. ], SlDropdown.prototype, "positioner", 2);
  321. __decorateClass([
  322. e({ type: Boolean, reflect: true })
  323. ], SlDropdown.prototype, "open", 2);
  324. __decorateClass([
  325. e()
  326. ], SlDropdown.prototype, "placement", 2);
  327. __decorateClass([
  328. e({ type: Boolean })
  329. ], SlDropdown.prototype, "disabled", 2);
  330. __decorateClass([
  331. e({ attribute: "close-on-select", type: Boolean, reflect: true })
  332. ], SlDropdown.prototype, "closeOnSelect", 2);
  333. __decorateClass([
  334. e({ attribute: false })
  335. ], SlDropdown.prototype, "containingElement", 2);
  336. __decorateClass([
  337. e({ type: Number })
  338. ], SlDropdown.prototype, "distance", 2);
  339. __decorateClass([
  340. e({ type: Number })
  341. ], SlDropdown.prototype, "skidding", 2);
  342. __decorateClass([
  343. e({ type: Boolean })
  344. ], SlDropdown.prototype, "hoist", 2);
  345. __decorateClass([
  346. event("sl-show")
  347. ], SlDropdown.prototype, "slShow", 2);
  348. __decorateClass([
  349. event("sl-after-show")
  350. ], SlDropdown.prototype, "slAfterShow", 2);
  351. __decorateClass([
  352. event("sl-hide")
  353. ], SlDropdown.prototype, "slHide", 2);
  354. __decorateClass([
  355. event("sl-after-hide")
  356. ], SlDropdown.prototype, "slAfterHide", 2);
  357. __decorateClass([
  358. watch("distance"),
  359. watch("hoist"),
  360. watch("placement"),
  361. watch("skidding")
  362. ], SlDropdown.prototype, "handlePopoverOptionsChange", 1);
  363. __decorateClass([
  364. watch("open")
  365. ], SlDropdown.prototype, "handleOpenChange", 1);
  366. SlDropdown = __decorateClass([
  367. n("sl-dropdown")
  368. ], SlDropdown);
  369. var dropdown_default2 = SlDropdown;
  370. setDefaultAnimation("dropdown.show", {
  371. keyframes: [
  372. { opacity: 0, transform: "scale(0.9)" },
  373. { opacity: 1, transform: "scale(1)" }
  374. ],
  375. options: { duration: 150, easing: "ease" }
  376. });
  377. setDefaultAnimation("dropdown.hide", {
  378. keyframes: [
  379. { opacity: 1, transform: "scale(1)" },
  380. { opacity: 0, transform: "scale(0.9)" }
  381. ],
  382. options: { duration: 150, easing: "ease" }
  383. });
  384. export {
  385. dropdown_default2 as dropdown_default
  386. };