calcite-action-menu.entry.js 13 KB

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