action-menu.js 14 KB

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