UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

384 lines (383 loc) • 12.7 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, toAriaBoolean } from "../../utils/dom"; import { guid } from "../../utils/guid"; import { connectInteractive, disconnectInteractive, updateHostInteraction } from "../../utils/interactive"; import { connectLocalized, disconnectLocalized } from "../../utils/locale"; import { connectMessages, disconnectMessages, setUpMessages, updateMessages } from "../../utils/t9n"; import { Heading } from "../functional/Heading"; import { CSS, ICONS, SLOTS } from "./resources"; /** * @slot - A slot for adding custom content. * @slot icon - A slot for adding a leading header icon with `calcite-icon`. * @slot control - A slot for adding a single HTML input element in a header. * @slot header-menu-actions - A slot for adding an overflow menu with `calcite-action`s inside a dropdown menu. */ export class Block { constructor() { this.guid = guid(); // -------------------------------------------------------------------------- // // Private Methods // // -------------------------------------------------------------------------- this.onHeaderClick = () => { this.open = !this.open; this.calciteBlockToggle.emit(); }; this.collapsible = false; this.disabled = false; this.dragHandle = false; this.heading = undefined; this.headingLevel = undefined; this.loading = false; this.open = false; this.status = undefined; this.description = undefined; this.messages = undefined; this.messageOverrides = undefined; this.effectiveLocale = undefined; this.defaultMessages = undefined; } onMessagesChange() { /* wired up by t9n util */ } effectiveLocaleChange() { updateMessages(this, this.effectiveLocale); } // -------------------------------------------------------------------------- // // Lifecycle // // -------------------------------------------------------------------------- connectedCallback() { connectConditionalSlotComponent(this); connectInteractive(this); connectLocalized(this); connectMessages(this); } disconnectedCallback() { disconnectInteractive(this); disconnectLocalized(this); disconnectMessages(this); disconnectConditionalSlotComponent(this); } componentDidRender() { updateHostInteraction(this); } async componentWillLoad() { await setUpMessages(this); } // -------------------------------------------------------------------------- // // Render Methods // // -------------------------------------------------------------------------- renderScrim() { const { loading } = this; const defaultSlot = h("slot", null); return [loading ? h("calcite-scrim", { loading: loading }) : null, defaultSlot]; } renderIcon() { const { el, status } = this; const showingLoadingStatus = this.loading && !this.open; const statusIcon = showingLoadingStatus ? ICONS.refresh : ICONS[status]; const hasIcon = getSlotted(el, SLOTS.icon) || statusIcon; const iconEl = !statusIcon ? (h("slot", { key: "icon-slot", name: SLOTS.icon })) : (h("calcite-icon", { class: { [CSS.statusIcon]: true, [CSS.valid]: status == "valid", [CSS.invalid]: status == "invalid", [CSS.loading]: showingLoadingStatus }, icon: statusIcon, scale: "m" })); return hasIcon ? h("div", { class: CSS.icon }, iconEl) : null; } renderTitle() { const { heading, headingLevel, description } = this; return heading || description ? (h("div", { class: CSS.title }, h(Heading, { class: CSS.heading, level: headingLevel }, heading), description ? h("div", { class: CSS.description }, description) : null)) : null; } render() { const { collapsible, el, loading, open, messages } = this; const toggleLabel = open ? messages.collapse : messages.expand; const headerContent = (h("header", { class: CSS.header }, this.renderIcon(), this.renderTitle())); const hasControl = !!getSlotted(el, SLOTS.control); const hasMenuActions = !!getSlotted(el, SLOTS.headerMenuActions); const collapseIcon = open ? ICONS.opened : ICONS.closed; const { guid } = this; const regionId = `${guid}-region`; const buttonId = `${guid}-button`; const headerNode = (h("div", { class: CSS.headerContainer }, this.dragHandle ? h("calcite-handle", null) : null, collapsible ? (h("button", { "aria-controls": regionId, "aria-expanded": collapsible ? toAriaBoolean(open) : null, "aria-label": toggleLabel, class: CSS.toggle, id: buttonId, onClick: this.onHeaderClick, title: toggleLabel }, headerContent, !hasControl && !hasMenuActions ? (h("calcite-icon", { "aria-hidden": "true", class: CSS.toggleIcon, icon: collapseIcon, scale: "s" })) : null)) : (headerContent), loading ? (h("calcite-loader", { inline: true, label: messages.loading })) : hasControl ? (h("div", { class: CSS.controlContainer }, h("slot", { name: SLOTS.control }))) : null, hasMenuActions ? (h("calcite-action-menu", { label: messages.options }, h("slot", { name: SLOTS.headerMenuActions }))) : null)); return (h(Host, null, h("article", { "aria-busy": toAriaBoolean(loading), class: { [CSS.container]: true } }, headerNode, h("section", { "aria-expanded": toAriaBoolean(open), "aria-labelledby": buttonId, class: CSS.content, hidden: !open, id: regionId }, this.renderScrim())))); } static get is() { return "calcite-block"; } static get encapsulation() { return "shadow"; } static get originalStyleUrls() { return { "$": ["block.scss"] }; } static get styleUrls() { return { "$": ["block.css"] }; } static get assetsDirs() { return ["assets"]; } static get properties() { return { "collapsible": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "When `true`, the component is collapsible." }, "attribute": "collapsible", "reflect": true, "defaultValue": "false" }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "When `true`, interaction is prevented and the component is displayed with lower opacity." }, "attribute": "disabled", "reflect": true, "defaultValue": "false" }, "dragHandle": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "When `true`, displays a drag handle in the header." }, "attribute": "drag-handle", "reflect": true, "defaultValue": "false" }, "heading": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": true, "optional": false, "docs": { "tags": [], "text": "The component header text." }, "attribute": "heading", "reflect": false }, "headingLevel": { "type": "number", "mutable": false, "complexType": { "original": "HeadingLevel", "resolved": "1 | 2 | 3 | 4 | 5 | 6", "references": { "HeadingLevel": { "location": "import", "path": "../functional/Heading" } } }, "required": false, "optional": false, "docs": { "tags": [], "text": "Specifies the number at which section headings should start." }, "attribute": "heading-level", "reflect": true }, "loading": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "When `true`, a busy indicator is displayed." }, "attribute": "loading", "reflect": true, "defaultValue": "false" }, "open": { "type": "boolean", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "When `true`, expands the component and its contents." }, "attribute": "open", "reflect": true, "defaultValue": "false" }, "status": { "type": "string", "mutable": false, "complexType": { "original": "Status", "resolved": "\"idle\" | \"invalid\" | \"valid\"", "references": { "Status": { "location": "import", "path": "../interfaces" } } }, "required": false, "optional": false, "docs": { "tags": [], "text": "Displays a status-related indicator icon." }, "attribute": "status", "reflect": true }, "description": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "A description for the component, which displays below the heading." }, "attribute": "description", "reflect": false }, "messages": { "type": "unknown", "mutable": true, "complexType": { "original": "BlockMessages", "resolved": "{ collapse: string; expand: string; loading: string; options: string; }", "references": { "BlockMessages": { "location": "import", "path": "./assets/block/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<BlockMessages>", "resolved": "{ collapse?: string; expand?: string; loading?: string; options?: string; }", "references": { "Partial": { "location": "global" }, "BlockMessages": { "location": "import", "path": "./assets/block/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": "calciteBlockToggle", "name": "calciteBlockToggle", "bubbles": true, "cancelable": false, "composed": true, "docs": { "tags": [], "text": "Emits when the component's header is clicked." }, "complexType": { "original": "void", "resolved": "void", "references": {} } }]; } static get elementRef() { return "el"; } static get watchers() { return [{ "propName": "messageOverrides", "methodName": "onMessagesChange" }, { "propName": "effectiveLocale", "methodName": "effectiveLocaleChange" }]; } }