/*! * All material copyright ESRI, All Rights Reserved, unless otherwise specified. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details. * v1.0.0-beta.82 */ import { Component, Element, Event, Host, Prop, Watch, h, Method } from "@stencil/core"; import { ExpandToggle, toggleChildActionText } from "../functional/ExpandToggle"; import { CSS, SLOTS, TEXT } from "./resources"; import { getSlotted, focusElement } from "../../utils/dom"; import { getOverflowCount, overflowActions, queryActions, overflowActionsDebounceInMs } from "./utils"; import { createObserver } from "../../utils/observers"; import { debounce } from "lodash-es"; import { connectConditionalSlotComponent, disconnectConditionalSlotComponent } from "../../utils/conditionalSlot"; /** * @slot - A slot for adding `calcite-action`s that will appear at the top of the action bar. * @slot bottom-actions - A slot for adding `calcite-action`s that will appear at the bottom of the action bar, above the collapse/expand button. * @slot expand-tooltip - Used to set the tooltip for the expand toggle. */ export class ActionBar { constructor() { // -------------------------------------------------------------------------- // // Properties // // -------------------------------------------------------------------------- /** * When set to true, the expand-toggling behavior will be disabled. */ this.expandDisabled = false; /** * Indicates whether widget is expanded. */ this.expanded = false; /** * Disables automatically overflowing actions that won't fit into menus. */ this.overflowActionsDisabled = false; this.mutationObserver = createObserver("mutation", () => { const { el, expanded } = this; toggleChildActionText({ parent: el, expanded }); this.conditionallyOverflowActions(); }); this.resizeObserver = createObserver("resize", (entries) => this.resizeHandlerEntries(entries)); // -------------------------------------------------------------------------- // // Private Methods // // -------------------------------------------------------------------------- this.actionMenuOpenChangeHandler = (event) => { if (event.detail) { const composedPath = event.composedPath(); Array.from(this.el.querySelectorAll("calcite-action-group")).forEach((group) => { if (!composedPath.includes(group)) { group.menuOpen = false; } }); } }; this.resizeHandlerEntries = (entries) => { entries.forEach(this.resizeHandler); }; this.resizeHandler = (entry) => { const { height } = entry.contentRect; this.resize(height); }; this.resize = debounce((height) => { var _a; const { el, expanded, expandDisabled } = this; if (!height) { return; } const actions = queryActions(el); const actionCount = expandDisabled ? actions.length : actions.length + 1; const actionGroups = Array.from(el.querySelectorAll("calcite-action-group")); const groupCount = getSlotted(el, SLOTS.bottomActions) || !expandDisabled ? actionGroups.length + 1 : actionGroups.length; const overflowCount = getOverflowCount({ actionCount, actionHeight: (_a = actions[0]) === null || _a === void 0 ? void 0 : _a.clientHeight, height, groupCount }); overflowActions({ actionGroups, expanded, overflowCount }); }, overflowActionsDebounceInMs); this.conditionallyOverflowActions = () => { if (!this.overflowActionsDisabled) { this.overflowActions(); } }; this.toggleExpand = () => { this.expanded = !this.expanded; this.calciteActionBarToggle.emit(); }; this.setExpandToggleRef = (el) => { this.expandToggleEl = el; }; } expandHandler() { this.conditionallyOverflowActions(); } expandedHandler(expanded) { toggleChildActionText({ parent: this.el, expanded }); } overflowDisabledHandler(overflowActionsDisabled) { overflowActionsDisabled ? this.resizeObserver.disconnect() : this.resizeObserver.observe(this.el); } // -------------------------------------------------------------------------- // // Lifecycle // // -------------------------------------------------------------------------- componentDidLoad() { this.conditionallyOverflowActions(); } connectedCallback() { var _a, _b; const { el, expanded } = this; toggleChildActionText({ parent: el, expanded }); (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(el, { childList: true, subtree: true }); if (!this.overflowActionsDisabled) { (_b = this.resizeObserver) === null || _b === void 0 ? void 0 : _b.observe(el); } this.conditionallyOverflowActions(); connectConditionalSlotComponent(this); } disconnectedCallback() { var _a, _b; (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect(); (_b = this.resizeObserver) === null || _b === void 0 ? void 0 : _b.disconnect(); disconnectConditionalSlotComponent(this); } // -------------------------------------------------------------------------- // // Methods // // -------------------------------------------------------------------------- /** * Overflows actions that won't fit into menus. * @internal */ async overflowActions() { this.resize(this.el.clientHeight); } /** Sets focus on the component. */ async setFocus(focusId) { if (focusId === "expand-toggle") { await focusElement(this.expandToggleEl); return; } this.el.focus(); } // -------------------------------------------------------------------------- // // Render Methods // // -------------------------------------------------------------------------- renderBottomActionGroup() { const { expanded, expandDisabled, intlExpand, intlCollapse, el, position, toggleExpand, scale } = this; const tooltip = getSlotted(el, SLOTS.expandTooltip); const expandLabel = intlExpand || TEXT.expand; const collapseLabel = intlCollapse || TEXT.collapse; 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; return getSlotted(el, SLOTS.bottomActions) || expandToggleNode ? (h("calcite-action-group", { class: CSS.actionGroupBottom, scale: scale }, h("slot", { name: SLOTS.bottomActions }), h("slot", { name: SLOTS.expandTooltip }), expandToggleNode)) : null; } render() { return (h(Host, { onCalciteActionMenuOpenChange: this.actionMenuOpenChangeHandler }, h("slot", null), this.renderBottomActionGroup())); } static get is() { return "calcite-action-bar"; } static get encapsulation() { return "shadow"; } static get originalStyleUrls() { return { "$": ["action-bar.scss"] }; } static get styleUrls() { return { "$": ["action-bar.css"] }; } static get properties() { return { "expandDisabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "When set to true, the expand-toggling behavior will be disabled." }, "attribute": "expand-disabled", "reflect": true, "defaultValue": "false" }, "expanded": { "type": "boolean", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Indicates whether widget is expanded." }, "attribute": "expanded", "reflect": true, "defaultValue": "false" }, "intlExpand": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Updates the label of the expand icon when the component is not expanded." }, "attribute": "intl-expand", "reflect": false }, "intlCollapse": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Updates the label of the collapse icon when the component is expanded." }, "attribute": "intl-collapse", "reflect": false }, "overflowActionsDisabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Disables automatically overflowing actions that won't fit into menus." }, "attribute": "overflow-actions-disabled", "reflect": false, "defaultValue": "false" }, "position": { "type": "string", "mutable": false, "complexType": { "original": "Position", "resolved": "\"end\" | \"start\"", "references": { "Position": { "location": "import", "path": "../interfaces" } } }, "required": false, "optional": false, "docs": { "tags": [], "text": "Arranges the component depending on the elements 'dir' property." }, "attribute": "position", "reflect": true }, "scale": { "type": "string", "mutable": false, "complexType": { "original": "Scale", "resolved": "\"l\" | \"m\" | \"s\"", "references": { "Scale": { "location": "import", "path": "../interfaces" } } }, "required": false, "optional": false, "docs": { "tags": [], "text": "Specifies the size of the expand action." }, "attribute": "scale", "reflect": true } }; } static get events() { return [{ "method": "calciteActionBarToggle", "name": "calciteActionBarToggle", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Emitted when expanded has been toggled." }, "complexType": { "original": "any", "resolved": "any", "references": {} } }]; } static get methods() { return { "overflowActions": { "complexType": { "signature": "() => Promise", "parameters": [], "references": { "Promise": { "location": "global" } }, "return": "Promise" }, "docs": { "text": "Overflows actions that won't fit into menus.", "tags": [{ "name": "internal", "text": undefined }] } }, "setFocus": { "complexType": { "signature": "(focusId?: \"expand-toggle\") => Promise", "parameters": [{ "tags": [], "text": "" }], "references": { "Promise": { "location": "global" } }, "return": "Promise" }, "docs": { "text": "Sets focus on the component.", "tags": [] } } }; } static get elementRef() { return "el"; } static get watchers() { return [{ "propName": "expandDisabled", "methodName": "expandHandler" }, { "propName": "expanded", "methodName": "expandedHandler" }, { "propName": "overflowActionsDisabled", "methodName": "overflowDisabledHandler" }]; } }