UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

476 lines (475 loc) • 15.1 kB
/*! * All material copyright ESRI, All Rights Reserved, unless otherwise specified. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details. * v1.5.0-next.4 */ import { h, Host } from "@stencil/core"; import { debounce } from "lodash-es"; import { connectConditionalSlotComponent, disconnectConditionalSlotComponent } from "../../utils/conditionalSlot"; import { getSlotted, slotChangeGetAssignedElements } from "../../utils/dom"; import { componentLoaded, setComponentLoaded, setUpLoadableComponent } from "../../utils/loadable"; import { connectLocalized, disconnectLocalized } from "../../utils/locale"; import { createObserver } from "../../utils/observers"; import { connectMessages, disconnectMessages, setUpMessages, updateMessages } from "../../utils/t9n"; import { ExpandToggle, toggleChildActionText } from "../functional/ExpandToggle"; import { CSS, SLOTS } from "./resources"; import { geActionDimensions, getOverflowCount, overflowActions, overflowActionsDebounceInMs, queryActions } from "./utils"; /** * @slot - A slot for adding `calcite-action`s that will appear at the top of the component. * @slot bottom-actions - A slot for adding `calcite-action`s that will appear at the bottom of the component, above the collapse/expand button. * @slot expand-tooltip - A slot to set the `calcite-tooltip` for the expand toggle. */ export class ActionBar { constructor() { this.mutationObserver = createObserver("mutation", () => { const { el, expanded } = this; toggleChildActionText({ el, expanded }); this.overflowActions(); }); this.resizeObserver = createObserver("resize", (entries) => this.resizeHandlerEntries(entries)); // -------------------------------------------------------------------------- // // Private Methods // // -------------------------------------------------------------------------- this.actionMenuOpenHandler = (event) => { if (event.target.menuOpen) { 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 { width, height } = entry.contentRect; this.resize({ width, height }); }; this.resize = debounce(({ width, height }) => { const { el, expanded, expandDisabled, layout, overflowActionsDisabled } = this; if (overflowActionsDisabled || (layout === "vertical" && !height) || (layout === "horizontal" && !width)) { return; } const actions = queryActions(el); const actionCount = expandDisabled ? actions.length : actions.length + 1; const actionGroups = Array.from(el.querySelectorAll("calcite-action-group")); this.setGroupLayout(actionGroups); const groupCount = getSlotted(el, SLOTS.bottomActions) || !expandDisabled ? actionGroups.length + 1 : actionGroups.length; const { actionHeight, actionWidth } = geActionDimensions(actions); const overflowCount = getOverflowCount({ layout, actionCount, actionHeight, actionWidth, height, width, groupCount }); overflowActions({ actionGroups, expanded, overflowCount }); }, overflowActionsDebounceInMs); this.toggleExpand = () => { this.expanded = !this.expanded; this.calciteActionBarToggle.emit(); }; this.setExpandToggleRef = (el) => { this.expandToggleEl = el; }; this.handleDefaultSlotChange = (event) => { const groups = slotChangeGetAssignedElements(event).filter((el) => el?.matches("calcite-action-group")); this.setGroupLayout(groups); }; this.expandDisabled = false; this.expanded = false; this.layout = "vertical"; this.overflowActionsDisabled = false; this.position = undefined; this.scale = undefined; this.messages = undefined; this.messageOverrides = undefined; this.effectiveLocale = undefined; this.defaultMessages = undefined; } expandHandler() { this.overflowActions(); } expandedHandler() { const { el, expanded } = this; toggleChildActionText({ el, expanded }); this.overflowActions(); } layoutHandler() { this.updateGroups(); } overflowDisabledHandler(overflowActionsDisabled) { overflowActionsDisabled ? this.resizeObserver.disconnect() : this.resizeObserver.observe(this.el); } onMessagesChange() { /* wired up by t9n util */ } effectiveLocaleChange() { updateMessages(this, this.effectiveLocale); } // -------------------------------------------------------------------------- // // Lifecycle // // -------------------------------------------------------------------------- componentDidLoad() { const { el, expanded } = this; setComponentLoaded(this); toggleChildActionText({ el, expanded }); this.overflowActions(); } connectedCallback() { const { el, expanded } = this; connectLocalized(this); connectMessages(this); toggleChildActionText({ el, expanded }); this.mutationObserver?.observe(el, { childList: true, subtree: true }); if (!this.overflowActionsDisabled) { this.resizeObserver?.observe(el); } this.overflowActions(); connectConditionalSlotComponent(this); } async componentWillLoad() { setUpLoadableComponent(this); await setUpMessages(this); } disconnectedCallback() { this.mutationObserver?.disconnect(); this.resizeObserver?.disconnect(); disconnectConditionalSlotComponent(this); disconnectLocalized(this); disconnectMessages(this); } // -------------------------------------------------------------------------- // // Methods // // -------------------------------------------------------------------------- /** * Overflows actions that won't fit into menus. * * @internal */ async overflowActions() { this.resize({ width: this.el.clientWidth, height: this.el.clientHeight }); } /** * Sets focus on the component's first focusable element. */ async setFocus() { await componentLoaded(this); this.el?.focus(); } updateGroups() { this.setGroupLayout(Array.from(this.el.querySelectorAll("calcite-action-group"))); } setGroupLayout(groups) { groups.forEach((group) => (group.layout = this.layout)); } // -------------------------------------------------------------------------- // // Render Methods // // -------------------------------------------------------------------------- renderBottomActionGroup() { const { expanded, expandDisabled, el, position, toggleExpand, scale, layout, messages } = this; const tooltip = getSlotted(el, SLOTS.expandTooltip); const expandToggleNode = !expandDisabled ? (h(ExpandToggle, { el: el, expanded: expanded, intlCollapse: messages.collapse, intlExpand: messages.expand, position: position, scale: scale, toggle: toggleExpand, tooltip: tooltip, // eslint-disable-next-line react/jsx-sort-props ref: this.setExpandToggleRef })) : null; 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; } render() { return (h(Host, { onCalciteActionMenuOpen: this.actionMenuOpenHandler }, h("slot", { onSlotchange: this.handleDefaultSlotChange }), this.renderBottomActionGroup())); } static get is() { return "calcite-action-bar"; } static get encapsulation() { return "shadow"; } static get delegatesFocus() { return true; } static get originalStyleUrls() { return { "$": ["action-bar.scss"] }; } static get styleUrls() { return { "$": ["action-bar.css"] }; } static get assetsDirs() { return ["assets"]; } static get properties() { return { "expandDisabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "When `true`, the expand-toggling behavior is 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": "When `true`, the component is expanded." }, "attribute": "expanded", "reflect": true, "defaultValue": "false" }, "layout": { "type": "string", "mutable": false, "complexType": { "original": "Extract<\"horizontal\" | \"vertical\", Layout>", "resolved": "\"horizontal\" | \"vertical\"", "references": { "Extract": { "location": "global" }, "Layout": { "location": "import", "path": "../interfaces" } } }, "required": false, "optional": false, "docs": { "tags": [], "text": "The layout direction of the actions." }, "attribute": "layout", "reflect": true, "defaultValue": "\"vertical\"" }, "overflowActionsDisabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Disables automatically overflowing `calcite-action`s that won't fit into menus." }, "attribute": "overflow-actions-disabled", "reflect": true, "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 element's `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 `calcite-action`." }, "attribute": "scale", "reflect": true }, "messages": { "type": "unknown", "mutable": true, "complexType": { "original": "ActionBarMessages", "resolved": "{ expand: string; collapse: string; }", "references": { "ActionBarMessages": { "location": "import", "path": "./assets/action-bar/t9n" } } }, "required": false, "optional": false, "docs": { "tags": [{ "name": "internal", "text": undefined }], "text": "Made into a prop for testing purposes only" } }, "messageOverrides": { "type": "unknown", "mutable": true, "complexType": { "original": "Partial<ActionBarMessages>", "resolved": "{ expand?: string; collapse?: string; }", "references": { "Partial": { "location": "global" }, "ActionBarMessages": { "location": "import", "path": "./assets/action-bar/t9n" } } }, "required": false, "optional": false, "docs": { "tags": [], "text": "Use this property to override individual strings used by the component." } } }; } static get states() { return { "effectiveLocale": {}, "defaultMessages": {} }; } static get events() { return [{ "method": "calciteActionBarToggle", "name": "calciteActionBarToggle", "bubbles": true, "cancelable": false, "composed": true, "docs": { "tags": [], "text": "Emits when the `expanded` property is toggled." }, "complexType": { "original": "void", "resolved": "void", "references": {} } }]; } static get methods() { return { "overflowActions": { "complexType": { "signature": "() => Promise<void>", "parameters": [], "references": { "Promise": { "location": "global" } }, "return": "Promise<void>" }, "docs": { "text": "Overflows actions that won't fit into menus.", "tags": [{ "name": "internal", "text": undefined }] } }, "setFocus": { "complexType": { "signature": "() => Promise<void>", "parameters": [], "references": { "Promise": { "location": "global" } }, "return": "Promise<void>" }, "docs": { "text": "Sets focus on the component's first focusable element.", "tags": [] } } }; } static get elementRef() { return "el"; } static get watchers() { return [{ "propName": "expandDisabled", "methodName": "expandHandler" }, { "propName": "expanded", "methodName": "expandedHandler" }, { "propName": "layout", "methodName": "layoutHandler" }, { "propName": "overflowActionsDisabled", "methodName": "overflowDisabledHandler" }, { "propName": "messageOverrides", "methodName": "onMessagesChange" }, { "propName": "effectiveLocale", "methodName": "effectiveLocaleChange" }]; } }