calcite-action-bar.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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, Host } from '@stencil/core/internal/client/index.js';
  7. import { t as toggleChildActionText, o as overflowActionsDebounceInMs, q as queryActions, g as geActionDimensions, a as overflowActions, E as ExpandToggle, b as getOverflowCount } from './ExpandToggle.js';
  8. import { b as getSlotted, f as focusElement } from './dom.js';
  9. import { c as createObserver } from './observers.js';
  10. import { c as connectConditionalSlotComponent, d as disconnectConditionalSlotComponent } from './conditionalSlot.js';
  11. import { d as defineCustomElement$7 } from './action.js';
  12. import { d as defineCustomElement$6 } from './action-group.js';
  13. import { d as defineCustomElement$5 } from './action-menu.js';
  14. import { d as defineCustomElement$4 } from './icon.js';
  15. import { d as defineCustomElement$3 } from './loader.js';
  16. import { d as defineCustomElement$2 } from './popover.js';
  17. import { d as debounce } from './debounce.js';
  18. const CSS = {
  19. actionGroupBottom: "action-group--bottom"
  20. };
  21. const SLOTS = {
  22. bottomActions: "bottom-actions",
  23. expandTooltip: "expand-tooltip"
  24. };
  25. const TEXT = {
  26. expand: "Expand",
  27. collapse: "Collapse"
  28. };
  29. const actionBarCss = "@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}}:host{box-sizing:border-box;background-color:var(--calcite-ui-foreground-1);color:var(--calcite-ui-text-2);font-size:var(--calcite-font-size--1)}:host *{box-sizing:border-box}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{pointer-events:auto;display:inline-flex;align-self:stretch;background:transparent;--calcite-action-bar-expanded-max-width:auto}:host([layout=vertical]){flex-direction:column}:host([layout=horizontal]){flex-direction:row}:host([layout=vertical][overflow-actions-disabled]){overflow-y:auto}:host([layout=horizontal][overflow-actions-disabled]){overflow-x:auto}:host([layout=vertical][expanded]){max-inline-size:var(--calcite-action-bar-expanded-max-width)}::slotted(calcite-action-group){border-width:0px;border-block-end-width:1px;border-style:solid;border-color:var(--calcite-ui-border-3)}:host([layout=horizontal]) ::slotted(calcite-action-group){border-width:0px;border-inline-end-width:1px;border-style:solid}::slotted(calcite-action-group:last-child){border-block-end-width:0px;border-inline-end-width:0px}.action-group--bottom{flex-grow:1;justify-content:flex-end;padding-block-end:0px;padding-inline-end:0px}";
  30. const ActionBar = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  31. constructor() {
  32. super();
  33. this.__registerHost();
  34. this.__attachShadow();
  35. this.calciteActionBarToggle = createEvent(this, "calciteActionBarToggle", 6);
  36. // --------------------------------------------------------------------------
  37. //
  38. // Properties
  39. //
  40. // --------------------------------------------------------------------------
  41. /**
  42. * When `true`, the expand-toggling behavior is disabled.
  43. */
  44. this.expandDisabled = false;
  45. /**
  46. * When `true`, the component is expanded.
  47. */
  48. this.expanded = false;
  49. /**
  50. * The layout direction of the actions.
  51. */
  52. this.layout = "vertical";
  53. /**
  54. * Disables automatically overflowing `calcite-action`s that won't fit into menus.
  55. */
  56. this.overflowActionsDisabled = false;
  57. this.mutationObserver = createObserver("mutation", () => {
  58. const { el, expanded } = this;
  59. toggleChildActionText({ parent: el, expanded });
  60. this.conditionallyOverflowActions();
  61. });
  62. this.resizeObserver = createObserver("resize", (entries) => this.resizeHandlerEntries(entries));
  63. // --------------------------------------------------------------------------
  64. //
  65. // Private Methods
  66. //
  67. // --------------------------------------------------------------------------
  68. this.actionMenuOpenChangeHandler = (event) => {
  69. if (event.detail) {
  70. const composedPath = event.composedPath();
  71. Array.from(this.el.querySelectorAll("calcite-action-group")).forEach((group) => {
  72. if (!composedPath.includes(group)) {
  73. group.menuOpen = false;
  74. }
  75. });
  76. }
  77. };
  78. this.resizeHandlerEntries = (entries) => {
  79. entries.forEach(this.resizeHandler);
  80. };
  81. this.resizeHandler = (entry) => {
  82. const { width, height } = entry.contentRect;
  83. this.resize({ width, height });
  84. };
  85. this.resize = debounce(({ width, height }) => {
  86. const { el, expanded, expandDisabled, layout } = this;
  87. if ((layout === "vertical" && !height) || (layout === "horizontal" && !width)) {
  88. return;
  89. }
  90. const actions = queryActions(el);
  91. const actionCount = expandDisabled ? actions.length : actions.length + 1;
  92. const actionGroups = Array.from(el.querySelectorAll("calcite-action-group"));
  93. const groupCount = getSlotted(el, SLOTS.bottomActions) || !expandDisabled
  94. ? actionGroups.length + 1
  95. : actionGroups.length;
  96. const { actionHeight, actionWidth } = geActionDimensions(actions);
  97. const overflowCount = getOverflowCount({
  98. layout,
  99. actionCount,
  100. actionHeight,
  101. actionWidth,
  102. height,
  103. width,
  104. groupCount
  105. });
  106. overflowActions({
  107. actionGroups,
  108. expanded,
  109. overflowCount
  110. });
  111. }, overflowActionsDebounceInMs);
  112. this.conditionallyOverflowActions = () => {
  113. if (!this.overflowActionsDisabled) {
  114. this.overflowActions();
  115. }
  116. };
  117. this.toggleExpand = () => {
  118. this.expanded = !this.expanded;
  119. this.calciteActionBarToggle.emit();
  120. };
  121. this.setExpandToggleRef = (el) => {
  122. this.expandToggleEl = el;
  123. };
  124. }
  125. expandHandler() {
  126. this.conditionallyOverflowActions();
  127. }
  128. expandedHandler(expanded) {
  129. toggleChildActionText({ parent: this.el, expanded });
  130. this.conditionallyOverflowActions();
  131. }
  132. overflowDisabledHandler(overflowActionsDisabled) {
  133. overflowActionsDisabled
  134. ? this.resizeObserver.disconnect()
  135. : this.resizeObserver.observe(this.el);
  136. }
  137. // --------------------------------------------------------------------------
  138. //
  139. // Lifecycle
  140. //
  141. // --------------------------------------------------------------------------
  142. componentDidLoad() {
  143. this.conditionallyOverflowActions();
  144. }
  145. connectedCallback() {
  146. var _a, _b;
  147. const { el, expanded } = this;
  148. toggleChildActionText({ parent: el, expanded });
  149. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(el, { childList: true, subtree: true });
  150. if (!this.overflowActionsDisabled) {
  151. (_b = this.resizeObserver) === null || _b === void 0 ? void 0 : _b.observe(el);
  152. }
  153. this.conditionallyOverflowActions();
  154. connectConditionalSlotComponent(this);
  155. }
  156. disconnectedCallback() {
  157. var _a, _b;
  158. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  159. (_b = this.resizeObserver) === null || _b === void 0 ? void 0 : _b.disconnect();
  160. disconnectConditionalSlotComponent(this);
  161. }
  162. // --------------------------------------------------------------------------
  163. //
  164. // Methods
  165. //
  166. // --------------------------------------------------------------------------
  167. /**
  168. * Overflows actions that won't fit into menus.
  169. *
  170. * @internal
  171. */
  172. async overflowActions() {
  173. this.resize({ width: this.el.clientWidth, height: this.el.clientHeight });
  174. }
  175. /**
  176. * Sets focus on the component.
  177. *
  178. * @param focusId
  179. */
  180. async setFocus(focusId) {
  181. var _a;
  182. if (focusId === "expand-toggle") {
  183. await focusElement(this.expandToggleEl);
  184. return;
  185. }
  186. (_a = this.el) === null || _a === void 0 ? void 0 : _a.focus();
  187. }
  188. // --------------------------------------------------------------------------
  189. //
  190. // Render Methods
  191. //
  192. // --------------------------------------------------------------------------
  193. renderBottomActionGroup() {
  194. const { expanded, expandDisabled, intlExpand, intlCollapse, el, position, toggleExpand, scale, layout } = this;
  195. const tooltip = getSlotted(el, SLOTS.expandTooltip);
  196. const expandLabel = intlExpand || TEXT.expand;
  197. const collapseLabel = intlCollapse || TEXT.collapse;
  198. const expandToggleNode = !expandDisabled ? (h(ExpandToggle, { el: el, expanded: expanded, intlCollapse: collapseLabel, intlExpand: expandLabel, position: position, ref: this.setExpandToggleRef, scale: scale, toggle: toggleExpand, tooltip: tooltip })) : null;
  199. return getSlotted(el, SLOTS.bottomActions) || expandToggleNode ? (h("calcite-action-group", { class: CSS.actionGroupBottom, layout: layout, scale: scale }, h("slot", { name: SLOTS.bottomActions }), h("slot", { name: SLOTS.expandTooltip }), expandToggleNode)) : null;
  200. }
  201. render() {
  202. return (h(Host, { onCalciteActionMenuOpenChange: this.actionMenuOpenChangeHandler }, h("slot", null), this.renderBottomActionGroup()));
  203. }
  204. get el() { return this; }
  205. static get watchers() { return {
  206. "expandDisabled": ["expandHandler"],
  207. "expanded": ["expandedHandler"],
  208. "overflowActionsDisabled": ["overflowDisabledHandler"]
  209. }; }
  210. static get style() { return actionBarCss; }
  211. }, [1, "calcite-action-bar", {
  212. "expandDisabled": [516, "expand-disabled"],
  213. "expanded": [1540],
  214. "intlExpand": [1, "intl-expand"],
  215. "intlCollapse": [1, "intl-collapse"],
  216. "layout": [513],
  217. "overflowActionsDisabled": [516, "overflow-actions-disabled"],
  218. "position": [513],
  219. "scale": [513],
  220. "overflowActions": [64],
  221. "setFocus": [64]
  222. }]);
  223. function defineCustomElement$1() {
  224. if (typeof customElements === "undefined") {
  225. return;
  226. }
  227. const components = ["calcite-action-bar", "calcite-action", "calcite-action-group", "calcite-action-menu", "calcite-icon", "calcite-loader", "calcite-popover"];
  228. components.forEach(tagName => { switch (tagName) {
  229. case "calcite-action-bar":
  230. if (!customElements.get(tagName)) {
  231. customElements.define(tagName, ActionBar);
  232. }
  233. break;
  234. case "calcite-action":
  235. if (!customElements.get(tagName)) {
  236. defineCustomElement$7();
  237. }
  238. break;
  239. case "calcite-action-group":
  240. if (!customElements.get(tagName)) {
  241. defineCustomElement$6();
  242. }
  243. break;
  244. case "calcite-action-menu":
  245. if (!customElements.get(tagName)) {
  246. defineCustomElement$5();
  247. }
  248. break;
  249. case "calcite-icon":
  250. if (!customElements.get(tagName)) {
  251. defineCustomElement$4();
  252. }
  253. break;
  254. case "calcite-loader":
  255. if (!customElements.get(tagName)) {
  256. defineCustomElement$3();
  257. }
  258. break;
  259. case "calcite-popover":
  260. if (!customElements.get(tagName)) {
  261. defineCustomElement$2();
  262. }
  263. break;
  264. } });
  265. }
  266. defineCustomElement$1();
  267. const CalciteActionBar = ActionBar;
  268. const defineCustomElement = defineCustomElement$1;
  269. export { CalciteActionBar, defineCustomElement };