action-menu.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.82
  5. */
  6. import { proxyCustomElement, HTMLElement, createEvent, h, Fragment } from '@stencil/core/internal/client';
  7. import { b as getSlotted, t as toAriaBoolean, f as focusElement } from './dom.js';
  8. import { g as getRoundRobinIndex } from './array.js';
  9. import { g as guid } from './guid.js';
  10. import { c as createObserver } from './observers.js';
  11. import { c as connectConditionalSlotComponent, d as disconnectConditionalSlotComponent } from './conditionalSlot.js';
  12. import { d as defineCustomElement$4 } from './action.js';
  13. import { d as defineCustomElement$3 } from './icon.js';
  14. import { d as defineCustomElement$2 } from './loader.js';
  15. import { d as defineCustomElement$1 } from './popover.js';
  16. const CSS = {
  17. menu: "menu",
  18. defaultTrigger: "default-trigger"
  19. };
  20. const SLOTS = {
  21. tooltip: "tooltip",
  22. trigger: "trigger"
  23. };
  24. const ICONS = {
  25. menu: "ellipsis"
  26. };
  27. const actionMenuCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;background-color:var(--calcite-ui-foreground-1);font-size:var(--calcite-font-size-1);color:var(--calcite-ui-text-2)}.menu ::slotted(calcite-action){margin:0.125rem;display:-ms-flexbox;display:flex;outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out}::slotted(calcite-action[active]){outline:2px solid var(--calcite-ui-brand);outline-offset:0px}.default-trigger{position:relative;height:100%;-ms-flex:0 1 auto;flex:0 1 auto;-ms-flex-item-align:stretch;align-self:stretch}slot[name=trigger]::slotted(calcite-action),calcite-action::slotted([slot=trigger]){position:relative;height:100%;-ms-flex:0 1 auto;flex:0 1 auto;-ms-flex-item-align:stretch;align-self:stretch}.menu{-ms-flex-direction:column;flex-direction:column;-ms-flex-wrap:nowrap;flex-wrap:nowrap;outline:2px solid transparent;outline-offset:2px}";
  28. const SUPPORTED_BUTTON_NAV_KEYS = ["ArrowUp", "ArrowDown"];
  29. const SUPPORTED_MENU_NAV_KEYS = ["ArrowUp", "ArrowDown", "End", "Home"];
  30. const ActionMenu = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  31. constructor() {
  32. super();
  33. this.__registerHost();
  34. this.__attachShadow();
  35. this.calciteActionMenuOpenChange = createEvent(this, "calciteActionMenuOpenChange", 7);
  36. // --------------------------------------------------------------------------
  37. //
  38. // Properties
  39. //
  40. // --------------------------------------------------------------------------
  41. /**
  42. * Indicates whether widget is expanded.
  43. */
  44. this.expanded = false;
  45. /**
  46. * Opens the action menu.
  47. */
  48. this.open = false;
  49. /** Describes the type of positioning to use for the overlaid content. If your element is in a fixed container, use the 'fixed' value. */
  50. this.overlayPositioning = "absolute";
  51. /**
  52. * Determines where the component will be positioned relative to the referenceElement.
  53. * @see [PopperPlacement](https://github.com/Esri/calcite-components/blob/master/src/utils/popper.ts#L25)
  54. */
  55. this.placement = "auto";
  56. this.actionElements = [];
  57. this.mutationObserver = createObserver("mutation", () => this.getActions());
  58. this.guid = `calcite-action-menu-${guid()}`;
  59. this.menuId = `${this.guid}-menu`;
  60. this.menuButtonId = `${this.guid}-menu-button`;
  61. this.activeMenuItemIndex = -1;
  62. // --------------------------------------------------------------------------
  63. //
  64. // Component Methods
  65. //
  66. // --------------------------------------------------------------------------
  67. this.connectMenuButtonEl = () => {
  68. const { el, menuButtonId, menuId, open, label } = this;
  69. const menuButtonEl = getSlotted(el, SLOTS.trigger) || this.defaultMenuButtonEl;
  70. if (this.menuButtonEl === menuButtonEl) {
  71. return;
  72. }
  73. this.disconnectMenuButtonEl();
  74. this.menuButtonEl = menuButtonEl;
  75. this.setTooltipReferenceElement();
  76. if (!menuButtonEl) {
  77. return;
  78. }
  79. menuButtonEl.active = open;
  80. menuButtonEl.setAttribute("aria-controls", menuId);
  81. menuButtonEl.setAttribute("aria-expanded", toAriaBoolean(open));
  82. menuButtonEl.setAttribute("aria-haspopup", "true");
  83. if (!menuButtonEl.id) {
  84. menuButtonEl.id = menuButtonId;
  85. }
  86. if (!menuButtonEl.label) {
  87. menuButtonEl.label = label;
  88. }
  89. if (!menuButtonEl.text) {
  90. menuButtonEl.text = label;
  91. }
  92. menuButtonEl.addEventListener("click", this.menuButtonClick);
  93. menuButtonEl.addEventListener("keydown", this.menuButtonKeyDown);
  94. menuButtonEl.addEventListener("keyup", this.menuButtonKeyUp);
  95. };
  96. this.disconnectMenuButtonEl = () => {
  97. const { menuButtonEl } = this;
  98. if (!menuButtonEl) {
  99. return;
  100. }
  101. menuButtonEl.removeEventListener("click", this.menuButtonClick);
  102. menuButtonEl.removeEventListener("keydown", this.menuButtonKeyDown);
  103. menuButtonEl.removeEventListener("keyup", this.menuButtonKeyUp);
  104. };
  105. this.setDefaultMenuButtonEl = (el) => {
  106. this.defaultMenuButtonEl = el;
  107. this.connectMenuButtonEl();
  108. };
  109. // --------------------------------------------------------------------------
  110. //
  111. // Private Methods
  112. //
  113. // --------------------------------------------------------------------------
  114. this.handleCalciteActionClick = () => {
  115. this.open = false;
  116. this.setFocus();
  117. };
  118. this.menuButtonClick = () => {
  119. this.toggleOpen();
  120. };
  121. this.updateTooltip = (event) => {
  122. const tooltips = event.target
  123. .assignedElements({
  124. flatten: true
  125. })
  126. .filter((el) => el === null || el === void 0 ? void 0 : el.matches("calcite-tooltip"));
  127. this.tooltipEl = tooltips[0];
  128. this.setTooltipReferenceElement();
  129. };
  130. this.setTooltipReferenceElement = () => {
  131. const { tooltipEl, expanded, menuButtonEl } = this;
  132. if (tooltipEl) {
  133. tooltipEl.referenceElement = !expanded ? menuButtonEl : null;
  134. }
  135. };
  136. this.updateAction = (action, index) => {
  137. const { guid, activeMenuItemIndex } = this;
  138. const id = `${guid}-action-${index}`;
  139. action.tabIndex = -1;
  140. action.setAttribute("role", "menuitem");
  141. if (!action.id) {
  142. action.id = id;
  143. }
  144. action.active = index === activeMenuItemIndex;
  145. };
  146. this.updateActions = (actions) => {
  147. actions === null || actions === void 0 ? void 0 : actions.forEach(this.updateAction);
  148. };
  149. this.getActions = () => {
  150. const { el } = this;
  151. const actionElements = getSlotted(el, { all: true, matches: "calcite-action" });
  152. this.updateActions(actionElements);
  153. this.actionElements = actionElements;
  154. this.connectMenuButtonEl();
  155. };
  156. this.menuButtonKeyUp = (event) => {
  157. const { key } = event;
  158. const { actionElements } = this;
  159. if (!this.isValidKey(key, SUPPORTED_BUTTON_NAV_KEYS)) {
  160. return;
  161. }
  162. event.preventDefault();
  163. if (!actionElements.length) {
  164. return;
  165. }
  166. this.toggleOpen(true);
  167. this.handleActionNavigation(key, actionElements);
  168. };
  169. this.menuButtonKeyDown = (event) => {
  170. const { key } = event;
  171. if (!this.isValidKey(key, SUPPORTED_BUTTON_NAV_KEYS)) {
  172. return;
  173. }
  174. event.preventDefault();
  175. };
  176. this.menuActionsContainerKeyDown = (event) => {
  177. const { key } = event;
  178. const { actionElements, activeMenuItemIndex } = this;
  179. if (key === "Tab") {
  180. this.open = false;
  181. return;
  182. }
  183. if (key === " " || key === "Enter") {
  184. event.preventDefault();
  185. const action = actionElements[activeMenuItemIndex];
  186. action ? action.click() : this.toggleOpen(false);
  187. return;
  188. }
  189. if (this.isValidKey(key, SUPPORTED_MENU_NAV_KEYS)) {
  190. event.preventDefault();
  191. }
  192. };
  193. this.menuActionsContainerKeyUp = (event) => {
  194. const { key } = event;
  195. const { actionElements } = this;
  196. if (key === "Escape") {
  197. this.toggleOpen(false);
  198. return;
  199. }
  200. if (!this.isValidKey(key, SUPPORTED_MENU_NAV_KEYS)) {
  201. return;
  202. }
  203. event.preventDefault();
  204. if (!actionElements.length) {
  205. return;
  206. }
  207. this.handleActionNavigation(key, actionElements);
  208. };
  209. this.handleActionNavigation = (key, actions) => {
  210. const currentIndex = this.activeMenuItemIndex;
  211. if (key === "Home") {
  212. this.activeMenuItemIndex = 0;
  213. }
  214. if (key === "End") {
  215. this.activeMenuItemIndex = actions.length - 1;
  216. }
  217. if (key === "ArrowUp") {
  218. this.activeMenuItemIndex = getRoundRobinIndex(Math.max(currentIndex - 1, -1), actions.length);
  219. }
  220. if (key === "ArrowDown") {
  221. this.activeMenuItemIndex = getRoundRobinIndex(currentIndex + 1, actions.length);
  222. }
  223. };
  224. this.toggleOpenEnd = () => {
  225. this.setFocus();
  226. this.el.removeEventListener("calcitePopoverOpen", this.toggleOpenEnd);
  227. };
  228. this.toggleOpen = (value = !this.open) => {
  229. this.el.addEventListener("calcitePopoverOpen", this.toggleOpenEnd);
  230. this.open = value;
  231. };
  232. }
  233. // --------------------------------------------------------------------------
  234. //
  235. // Lifecycle
  236. //
  237. // --------------------------------------------------------------------------
  238. connectedCallback() {
  239. var _a;
  240. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  241. this.getActions();
  242. connectConditionalSlotComponent(this);
  243. }
  244. disconnectedCallback() {
  245. var _a;
  246. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  247. this.disconnectMenuButtonEl();
  248. disconnectConditionalSlotComponent(this);
  249. }
  250. expandedHandler() {
  251. this.open = false;
  252. this.setTooltipReferenceElement();
  253. }
  254. openHandler(open) {
  255. this.activeMenuItemIndex = this.open ? 0 : -1;
  256. if (this.menuButtonEl) {
  257. this.menuButtonEl.active = open;
  258. }
  259. this.calciteActionMenuOpenChange.emit(open);
  260. }
  261. closeCalciteActionMenuOnClick(event) {
  262. const composedPath = event.composedPath();
  263. if (composedPath.includes(this.el)) {
  264. return;
  265. }
  266. this.open = false;
  267. }
  268. activeMenuItemIndexHandler() {
  269. this.updateActions(this.actionElements);
  270. }
  271. // --------------------------------------------------------------------------
  272. //
  273. // Methods
  274. //
  275. // --------------------------------------------------------------------------
  276. /** Sets focus on the component. */
  277. async setFocus() {
  278. focusElement(this.open ? this.menuEl : this.menuButtonEl);
  279. }
  280. renderMenuButton() {
  281. const { label, scale } = this;
  282. const menuButtonSlot = (h("slot", { name: SLOTS.trigger }, h("calcite-action", { class: CSS.defaultTrigger, icon: ICONS.menu, ref: this.setDefaultMenuButtonEl, scale: scale, text: label })));
  283. return menuButtonSlot;
  284. }
  285. renderMenuItems() {
  286. const { actionElements, activeMenuItemIndex, open, menuId, menuButtonEl, label, placement, overlayPositioning, flipPlacements } = this;
  287. const activeAction = actionElements[activeMenuItemIndex];
  288. const activeDescendantId = (activeAction === null || activeAction === void 0 ? void 0 : activeAction.id) || null;
  289. return (h("calcite-popover", { disablePointer: true, flipPlacements: flipPlacements, label: label, offsetDistance: 0, open: open, overlayPositioning: overlayPositioning, placement: placement, referenceElement: menuButtonEl }, h("div", { "aria-activedescendant": activeDescendantId, "aria-labelledby": menuButtonEl === null || menuButtonEl === void 0 ? void 0 : menuButtonEl.id, class: CSS.menu, id: menuId, onClick: this.handleCalciteActionClick, onKeyDown: this.menuActionsContainerKeyDown, onKeyUp: this.menuActionsContainerKeyUp, ref: (el) => (this.menuEl = el), role: "menu", tabIndex: -1 }, h("slot", null))));
  290. }
  291. render() {
  292. return (h(Fragment, null, this.renderMenuButton(), this.renderMenuItems(), h("slot", { name: SLOTS.tooltip, onSlotchange: this.updateTooltip })));
  293. }
  294. isValidKey(key, supportedKeys) {
  295. return !!supportedKeys.find((k) => k === key);
  296. }
  297. get el() { return this; }
  298. static get watchers() { return {
  299. "expanded": ["expandedHandler"],
  300. "open": ["openHandler"],
  301. "activeMenuItemIndex": ["activeMenuItemIndexHandler"]
  302. }; }
  303. static get style() { return actionMenuCss; }
  304. }, [1, "calcite-action-menu", {
  305. "expanded": [516],
  306. "flipPlacements": [16],
  307. "label": [1],
  308. "open": [1540],
  309. "overlayPositioning": [1, "overlay-positioning"],
  310. "placement": [513],
  311. "scale": [513],
  312. "activeMenuItemIndex": [32],
  313. "setFocus": [64]
  314. }, [[8, "click", "closeCalciteActionMenuOnClick"]]]);
  315. function defineCustomElement() {
  316. if (typeof customElements === "undefined") {
  317. return;
  318. }
  319. const components = ["calcite-action-menu", "calcite-action", "calcite-icon", "calcite-loader", "calcite-popover"];
  320. components.forEach(tagName => { switch (tagName) {
  321. case "calcite-action-menu":
  322. if (!customElements.get(tagName)) {
  323. customElements.define(tagName, ActionMenu);
  324. }
  325. break;
  326. case "calcite-action":
  327. if (!customElements.get(tagName)) {
  328. defineCustomElement$4();
  329. }
  330. break;
  331. case "calcite-icon":
  332. if (!customElements.get(tagName)) {
  333. defineCustomElement$3();
  334. }
  335. break;
  336. case "calcite-loader":
  337. if (!customElements.get(tagName)) {
  338. defineCustomElement$2();
  339. }
  340. break;
  341. case "calcite-popover":
  342. if (!customElements.get(tagName)) {
  343. defineCustomElement$1();
  344. }
  345. break;
  346. } });
  347. }
  348. defineCustomElement();
  349. export { ActionMenu as A, SLOTS as S, defineCustomElement as d };