UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

356 lines (355 loc) • 11.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 { 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 { connectMessages, disconnectMessages, setUpMessages, updateMessages } from "../../utils/t9n"; import { ExpandToggle, toggleChildActionText } from "../functional/ExpandToggle"; import { CSS, SLOTS } from "./resources"; import { createObserver } from "../../utils/observers"; /** * @slot - A slot for adding `calcite-action`s to the component. * @slot expand-tooltip - A slot to set the `calcite-tooltip` for the expand toggle. */ export class ActionPad { constructor() { this.mutationObserver = createObserver("mutation", () => this.setGroupLayout(Array.from(this.el.querySelectorAll("calcite-action-group")))); // -------------------------------------------------------------------------- // // 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.toggleExpand = () => { this.expanded = !this.expanded; this.calciteActionPadToggle.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.position = undefined; this.scale = undefined; this.messages = undefined; this.messageOverrides = undefined; this.effectiveLocale = ""; this.defaultMessages = undefined; } expandedHandler(expanded) { toggleChildActionText({ el: this.el, expanded }); } layoutHandler() { this.updateGroups(); } onMessagesChange() { /* wired up by t9n util */ } effectiveLocaleChange() { updateMessages(this, this.effectiveLocale); } // -------------------------------------------------------------------------- // // Lifecycle // // -------------------------------------------------------------------------- connectedCallback() { connectConditionalSlotComponent(this); connectLocalized(this); connectMessages(this); } disconnectedCallback() { disconnectLocalized(this); disconnectMessages(this); disconnectConditionalSlotComponent(this); } async componentWillLoad() { setUpLoadableComponent(this); const { el, expanded } = this; toggleChildActionText({ el, expanded }); await setUpMessages(this); } componentDidLoad() { setComponentLoaded(this); } // -------------------------------------------------------------------------- // // Methods // // -------------------------------------------------------------------------- /** * 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)); } // -------------------------------------------------------------------------- // // Component Methods // // -------------------------------------------------------------------------- renderBottomActionGroup() { const { expanded, expandDisabled, messages, el, position, toggleExpand, scale, layout } = 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 expandToggleNode ? (h("calcite-action-group", { class: CSS.actionGroupBottom, layout: layout, scale: scale }, h("slot", { name: SLOTS.expandTooltip }), expandToggleNode)) : null; } render() { return (h(Host, { onCalciteActionMenuOpen: this.actionMenuOpenHandler }, h("div", { class: CSS.container }, h("slot", { onSlotchange: this.handleDefaultSlotChange }), this.renderBottomActionGroup()))); } static get is() { return "calcite-action-pad"; } static get encapsulation() { return "shadow"; } static get delegatesFocus() { return true; } static get originalStyleUrls() { return { "$": ["action-pad.scss"] }; } static get styleUrls() { return { "$": ["action-pad.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": "Layout", "resolved": "\"grid\" | \"horizontal\" | \"vertical\"", "references": { "Layout": { "location": "import", "path": "../interfaces" } } }, "required": false, "optional": false, "docs": { "tags": [], "text": "Indicates the layout of the component." }, "attribute": "layout", "reflect": true, "defaultValue": "\"vertical\"" }, "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": "ActionPadMessages", "resolved": "{ expand: string; collapse: string; }", "references": { "ActionPadMessages": { "location": "import", "path": "./assets/action-pad/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<ActionPadMessages>", "resolved": "{ expand?: string; collapse?: string; }", "references": { "Partial": { "location": "global" }, "ActionPadMessages": { "location": "import", "path": "./assets/action-pad/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": "calciteActionPadToggle", "name": "calciteActionPadToggle", "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 { "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": "expanded", "methodName": "expandedHandler" }, { "propName": "layout", "methodName": "layoutHandler" }, { "propName": "messageOverrides", "methodName": "onMessagesChange" }, { "propName": "effectiveLocale", "methodName": "effectiveLocaleChange" }]; } }