calcite-action-menu.entry.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 { r as registerInstance, c as createEvent, h, F as Fragment, g as getElement } from './index-8ece2564.js';
  7. import { C as CSS, I as ICONS, S as SLOTS } from './resources-3c30f589.js';
  8. import { g as getSlotted, t as toAriaBoolean, f as focusElement } from './dom-da697a3f.js';
  9. import { g as getRoundRobinIndex } from './array-1655bbf7.js';
  10. import { g as guid } from './guid-b4461004.js';
  11. import { c as createObserver } from './observers-b198f831.js';
  12. import { c as connectConditionalSlotComponent, d as disconnectConditionalSlotComponent } from './conditionalSlot-203be745.js';
  13. 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}";
  14. const SUPPORTED_BUTTON_NAV_KEYS = ["ArrowUp", "ArrowDown"];
  15. const SUPPORTED_MENU_NAV_KEYS = ["ArrowUp", "ArrowDown", "End", "Home"];
  16. const ActionMenu = class {
  17. constructor(hostRef) {
  18. registerInstance(this, hostRef);
  19. this.calciteActionMenuOpenChange = createEvent(this, "calciteActionMenuOpenChange", 7);
  20. // --------------------------------------------------------------------------
  21. //
  22. // Properties
  23. //
  24. // --------------------------------------------------------------------------
  25. /**
  26. * Indicates whether widget is expanded.
  27. */
  28. this.expanded = false;
  29. /**
  30. * Opens the action menu.
  31. */
  32. this.open = false;
  33. /** Describes the type of positioning to use for the overlaid content. If your element is in a fixed container, use the 'fixed' value. */
  34. this.overlayPositioning = "absolute";
  35. /**
  36. * Determines where the component will be positioned relative to the referenceElement.
  37. * @see [PopperPlacement](https://github.com/Esri/calcite-components/blob/master/src/utils/popper.ts#L25)
  38. */
  39. this.placement = "auto";
  40. this.actionElements = [];
  41. this.mutationObserver = createObserver("mutation", () => this.getActions());
  42. this.guid = `calcite-action-menu-${guid()}`;
  43. this.menuId = `${this.guid}-menu`;
  44. this.menuButtonId = `${this.guid}-menu-button`;
  45. this.activeMenuItemIndex = -1;
  46. // --------------------------------------------------------------------------
  47. //
  48. // Component Methods
  49. //
  50. // --------------------------------------------------------------------------
  51. this.connectMenuButtonEl = () => {
  52. const { el, menuButtonId, menuId, open, label } = this;
  53. const menuButtonEl = getSlotted(el, SLOTS.trigger) || this.defaultMenuButtonEl;
  54. if (this.menuButtonEl === menuButtonEl) {
  55. return;
  56. }
  57. this.disconnectMenuButtonEl();
  58. this.menuButtonEl = menuButtonEl;
  59. this.setTooltipReferenceElement();
  60. if (!menuButtonEl) {
  61. return;
  62. }
  63. menuButtonEl.active = open;
  64. menuButtonEl.setAttribute("aria-controls", menuId);
  65. menuButtonEl.setAttribute("aria-expanded", toAriaBoolean(open));
  66. menuButtonEl.setAttribute("aria-haspopup", "true");
  67. if (!menuButtonEl.id) {
  68. menuButtonEl.id = menuButtonId;
  69. }
  70. if (!menuButtonEl.label) {
  71. menuButtonEl.label = label;
  72. }
  73. if (!menuButtonEl.text) {
  74. menuButtonEl.text = label;
  75. }
  76. menuButtonEl.addEventListener("click", this.menuButtonClick);
  77. menuButtonEl.addEventListener("keydown", this.menuButtonKeyDown);
  78. menuButtonEl.addEventListener("keyup", this.menuButtonKeyUp);
  79. };
  80. this.disconnectMenuButtonEl = () => {
  81. const { menuButtonEl } = this;
  82. if (!menuButtonEl) {
  83. return;
  84. }
  85. menuButtonEl.removeEventListener("click", this.menuButtonClick);
  86. menuButtonEl.removeEventListener("keydown", this.menuButtonKeyDown);
  87. menuButtonEl.removeEventListener("keyup", this.menuButtonKeyUp);
  88. };
  89. this.setDefaultMenuButtonEl = (el) => {
  90. this.defaultMenuButtonEl = el;
  91. this.connectMenuButtonEl();
  92. };
  93. // --------------------------------------------------------------------------
  94. //
  95. // Private Methods
  96. //
  97. // --------------------------------------------------------------------------
  98. this.handleCalciteActionClick = () => {
  99. this.open = false;
  100. this.setFocus();
  101. };
  102. this.menuButtonClick = () => {
  103. this.toggleOpen();
  104. };
  105. this.updateTooltip = (event) => {
  106. const tooltips = event.target
  107. .assignedElements({
  108. flatten: true
  109. })
  110. .filter((el) => el === null || el === void 0 ? void 0 : el.matches("calcite-tooltip"));
  111. this.tooltipEl = tooltips[0];
  112. this.setTooltipReferenceElement();
  113. };
  114. this.setTooltipReferenceElement = () => {
  115. const { tooltipEl, expanded, menuButtonEl } = this;
  116. if (tooltipEl) {
  117. tooltipEl.referenceElement = !expanded ? menuButtonEl : null;
  118. }
  119. };
  120. this.updateAction = (action, index) => {
  121. const { guid, activeMenuItemIndex } = this;
  122. const id = `${guid}-action-${index}`;
  123. action.tabIndex = -1;
  124. action.setAttribute("role", "menuitem");
  125. if (!action.id) {
  126. action.id = id;
  127. }
  128. action.active = index === activeMenuItemIndex;
  129. };
  130. this.updateActions = (actions) => {
  131. actions === null || actions === void 0 ? void 0 : actions.forEach(this.updateAction);
  132. };
  133. this.getActions = () => {
  134. const { el } = this;
  135. const actionElements = getSlotted(el, { all: true, matches: "calcite-action" });
  136. this.updateActions(actionElements);
  137. this.actionElements = actionElements;
  138. this.connectMenuButtonEl();
  139. };
  140. this.menuButtonKeyUp = (event) => {
  141. const { key } = event;
  142. const { actionElements } = this;
  143. if (!this.isValidKey(key, SUPPORTED_BUTTON_NAV_KEYS)) {
  144. return;
  145. }
  146. event.preventDefault();
  147. if (!actionElements.length) {
  148. return;
  149. }
  150. this.toggleOpen(true);
  151. this.handleActionNavigation(key, actionElements);
  152. };
  153. this.menuButtonKeyDown = (event) => {
  154. const { key } = event;
  155. if (!this.isValidKey(key, SUPPORTED_BUTTON_NAV_KEYS)) {
  156. return;
  157. }
  158. event.preventDefault();
  159. };
  160. this.menuActionsContainerKeyDown = (event) => {
  161. const { key } = event;
  162. const { actionElements, activeMenuItemIndex } = this;
  163. if (key === "Tab") {
  164. this.open = false;
  165. return;
  166. }
  167. if (key === " " || key === "Enter") {
  168. event.preventDefault();
  169. const action = actionElements[activeMenuItemIndex];
  170. action ? action.click() : this.toggleOpen(false);
  171. return;
  172. }
  173. if (this.isValidKey(key, SUPPORTED_MENU_NAV_KEYS)) {
  174. event.preventDefault();
  175. }
  176. };
  177. this.menuActionsContainerKeyUp = (event) => {
  178. const { key } = event;
  179. const { actionElements } = this;
  180. if (key === "Escape") {
  181. this.toggleOpen(false);
  182. return;
  183. }
  184. if (!this.isValidKey(key, SUPPORTED_MENU_NAV_KEYS)) {
  185. return;
  186. }
  187. event.preventDefault();
  188. if (!actionElements.length) {
  189. return;
  190. }
  191. this.handleActionNavigation(key, actionElements);
  192. };
  193. this.handleActionNavigation = (key, actions) => {
  194. const currentIndex = this.activeMenuItemIndex;
  195. if (key === "Home") {
  196. this.activeMenuItemIndex = 0;
  197. }
  198. if (key === "End") {
  199. this.activeMenuItemIndex = actions.length - 1;
  200. }
  201. if (key === "ArrowUp") {
  202. this.activeMenuItemIndex = getRoundRobinIndex(Math.max(currentIndex - 1, -1), actions.length);
  203. }
  204. if (key === "ArrowDown") {
  205. this.activeMenuItemIndex = getRoundRobinIndex(currentIndex + 1, actions.length);
  206. }
  207. };
  208. this.toggleOpenEnd = () => {
  209. this.setFocus();
  210. this.el.removeEventListener("calcitePopoverOpen", this.toggleOpenEnd);
  211. };
  212. this.toggleOpen = (value = !this.open) => {
  213. this.el.addEventListener("calcitePopoverOpen", this.toggleOpenEnd);
  214. this.open = value;
  215. };
  216. }
  217. // --------------------------------------------------------------------------
  218. //
  219. // Lifecycle
  220. //
  221. // --------------------------------------------------------------------------
  222. connectedCallback() {
  223. var _a;
  224. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  225. this.getActions();
  226. connectConditionalSlotComponent(this);
  227. }
  228. disconnectedCallback() {
  229. var _a;
  230. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  231. this.disconnectMenuButtonEl();
  232. disconnectConditionalSlotComponent(this);
  233. }
  234. expandedHandler() {
  235. this.open = false;
  236. this.setTooltipReferenceElement();
  237. }
  238. openHandler(open) {
  239. this.activeMenuItemIndex = this.open ? 0 : -1;
  240. if (this.menuButtonEl) {
  241. this.menuButtonEl.active = open;
  242. }
  243. this.calciteActionMenuOpenChange.emit(open);
  244. }
  245. closeCalciteActionMenuOnClick(event) {
  246. const composedPath = event.composedPath();
  247. if (composedPath.includes(this.el)) {
  248. return;
  249. }
  250. this.open = false;
  251. }
  252. activeMenuItemIndexHandler() {
  253. this.updateActions(this.actionElements);
  254. }
  255. // --------------------------------------------------------------------------
  256. //
  257. // Methods
  258. //
  259. // --------------------------------------------------------------------------
  260. /** Sets focus on the component. */
  261. async setFocus() {
  262. focusElement(this.open ? this.menuEl : this.menuButtonEl);
  263. }
  264. renderMenuButton() {
  265. const { label, scale } = this;
  266. const menuButtonSlot = (h("slot", { name: SLOTS.trigger }, h("calcite-action", { class: CSS.defaultTrigger, icon: ICONS.menu, ref: this.setDefaultMenuButtonEl, scale: scale, text: label })));
  267. return menuButtonSlot;
  268. }
  269. renderMenuItems() {
  270. const { actionElements, activeMenuItemIndex, open, menuId, menuButtonEl, label, placement, overlayPositioning, flipPlacements } = this;
  271. const activeAction = actionElements[activeMenuItemIndex];
  272. const activeDescendantId = (activeAction === null || activeAction === void 0 ? void 0 : activeAction.id) || null;
  273. 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))));
  274. }
  275. render() {
  276. return (h(Fragment, null, this.renderMenuButton(), this.renderMenuItems(), h("slot", { name: SLOTS.tooltip, onSlotchange: this.updateTooltip })));
  277. }
  278. isValidKey(key, supportedKeys) {
  279. return !!supportedKeys.find((k) => k === key);
  280. }
  281. get el() { return getElement(this); }
  282. static get watchers() { return {
  283. "expanded": ["expandedHandler"],
  284. "open": ["openHandler"],
  285. "activeMenuItemIndex": ["activeMenuItemIndexHandler"]
  286. }; }
  287. };
  288. ActionMenu.style = actionMenuCss;
  289. export { ActionMenu as calcite_action_menu };