UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

241 lines (240 loc) • 10.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 { Fragment, h, Host } from "@stencil/core"; import { slotChangeHasAssignedElement } from "../../utils/dom"; import { CSS, ICONS, SLOTS } from "./resources"; import { componentLoaded, setComponentLoaded, setUpLoadableComponent } from "../../utils/loadable"; /** * @slot logo - A slot for adding a `calcite-logo` component to the primary navigation level. * @slot user - A slot for adding a `calcite-user` component to the primary navigation level. * @slot progress - A slot for adding a `calcite-progress` component to the primary navigation level. * @slot navigation-action - A slot for adding a `calcite-action` component to the primary navigation level. * @slot content-start - A slot for adding a `calcite-menu`, `calcite-action`, or other interactive elements in the start position of any navigation level. * @slot content-center - A slot for adding a `calcite-menu`, `calcite-action`, or other interactive elements in the center position of the primary navigation level. * @slot content-end - A slot for adding a `calcite-menu`, `calcite-action`, or other interactive elements in the end position of any navigation level. * @slot navigation-secondary - A slot for adding a `calcite-navigation` component in the secondary navigation level. Components rendered here will not display `calcite-navigation-logo` or `calcite-navigation-user` components. * @slot navigation-tertiary - A slot for adding a `calcite-navigation` component in the tertiary navigation level. Components rendered here will not display `calcite-navigation-logo` or `calcite-navigation-user` components. */ export class CalciteNavigation { constructor() { //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- this.actionClickHandler = () => { this.calciteNavigationActionSelect.emit(); }; this.handleUserSlotChange = (event) => { if (this.isPrimaryLevel()) { this.userSlotHasElements = slotChangeHasAssignedElement(event); } }; this.handleLogoSlotChange = (event) => { if (this.isPrimaryLevel()) { this.logoSlotHasElements = slotChangeHasAssignedElement(event); } }; this.handleContentStartSlotChange = (event) => { if (this.isPrimaryLevel()) { this.primaryContentStartSlotHasElements = slotChangeHasAssignedElement(event); } }; this.handleContentEndSlotChange = (event) => { if (this.isPrimaryLevel()) { this.primaryContentEndSlotHasElements = slotChangeHasAssignedElement(event); } }; this.handleContentCenterSlotChange = (event) => { if (this.isPrimaryLevel()) { this.primaryContentCenterSlotHasElements = slotChangeHasAssignedElement(event); } }; this.handleSecondarySlotChange = (event) => { this.secondarySlotHasElements = slotChangeHasAssignedElement(event); }; this.handleTertiarySlotChange = (event) => { this.tertiarySlotHasElements = slotChangeHasAssignedElement(event); }; this.handleMenuActionSlotChange = (event) => { if (this.isPrimaryLevel()) { this.navigationActionSlotHasElements = slotChangeHasAssignedElement(event); if (this.navigationActionSlotHasElements) { this.navigationAction = false; } } }; this.handleProgressSlotChange = (event) => { if (this.isPrimaryLevel()) { this.progressSlotHasElement = slotChangeHasAssignedElement(event); } }; this.label = undefined; this.navigationAction = false; this.logoSlotHasElements = undefined; this.navigationActionSlotHasElements = undefined; this.primaryContentCenterSlotHasElements = undefined; this.primaryContentEndSlotHasElements = undefined; this.primaryContentStartSlotHasElements = undefined; this.progressSlotHasElement = undefined; this.secondarySlotHasElements = undefined; this.tertiarySlotHasElements = undefined; this.userSlotHasElements = undefined; } //-------------------------------------------------------------------------- // // Public Methods // //-------------------------------------------------------------------------- /** When `navigation-action` is `true`, sets focus on the component's action element. */ async setFocus() { await componentLoaded(this); await this.navigationActionEl?.setFocus(); } //-------------------------------------------------------------------------- // // Lifecycle // //-------------------------------------------------------------------------- componentWillLoad() { setUpLoadableComponent(this); } componentDidLoad() { setComponentLoaded(this); } isPrimaryLevel() { return this.el.slot !== SLOTS.navSecondary && this.el.slot !== SLOTS.navTertiary; } //-------------------------------------------------------------------------- // // Render Methods // //-------------------------------------------------------------------------- renderMenuAction() { return (h("slot", { name: SLOTS.navigationAction, onSlotchange: this.handleMenuActionSlotChange }, this.navigationAction && (h("calcite-action", { icon: ICONS.hamburger, onClick: this.actionClickHandler, text: this.label, // eslint-disable-next-line react/jsx-sort-props ref: (el) => (this.navigationActionEl = el) })))); } render() { const primaryLevelHasElements = this.logoSlotHasElements || this.userSlotHasElements || this.navigationActionSlotHasElements || this.primaryContentCenterSlotHasElements || this.primaryContentEndSlotHasElements || this.primaryContentStartSlotHasElements || this.navigationAction; const slotName = this.el.slot; return (h(Host, null, h("div", { class: { [CSS.container]: true, [CSS.secondary]: slotName === SLOTS.navSecondary, [CSS.tertiary]: slotName === SLOTS.navTertiary, [CSS.primary]: primaryLevelHasElements } }, h("div", { class: { [CSS.hide]: !this.progressSlotHasElement, [SLOTS.progress]: true } }, h("slot", { name: SLOTS.progress, onSlotchange: this.handleProgressSlotChange })), h("div", { class: { [CSS.containerContent]: true, [CSS.hasProgress]: this.progressSlotHasElement } }, this.renderMenuAction(), h("div", { class: { [CSS.hide]: !this.logoSlotHasElements, [SLOTS.logo]: true } }, h("slot", { name: SLOTS.logo, onSlotchange: this.handleLogoSlotChange })), h("slot", { name: SLOTS.contentStart, onSlotchange: this.handleContentStartSlotChange }), h("slot", { name: SLOTS.contentCenter, onSlotchange: this.handleContentCenterSlotChange }), h("slot", { name: SLOTS.contentEnd, onSlotchange: this.handleContentEndSlotChange }), h("div", { class: { [CSS.hide]: !this.userSlotHasElements, [SLOTS.user]: true } }, h("slot", { name: SLOTS.user, onSlotchange: this.handleUserSlotChange })))), h(Fragment, null, h("slot", { name: SLOTS.navSecondary, onSlotchange: this.handleSecondarySlotChange }), h("slot", { name: SLOTS.navTertiary, onSlotchange: this.handleTertiarySlotChange })))); } static get is() { return "calcite-navigation"; } static get encapsulation() { return "shadow"; } static get originalStyleUrls() { return { "$": ["navigation.scss"] }; } static get styleUrls() { return { "$": ["navigation.css"] }; } static get properties() { return { "label": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "When `navigationAction` is `true`, specifies the label of the `calcite-action`." }, "attribute": "label", "reflect": false }, "navigationAction": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "When `true`, displays a `calcite-action` and emits a `calciteNavActionSelect` event on selection change." }, "attribute": "navigation-action", "reflect": true, "defaultValue": "false" } }; } static get states() { return { "logoSlotHasElements": {}, "navigationActionSlotHasElements": {}, "primaryContentCenterSlotHasElements": {}, "primaryContentEndSlotHasElements": {}, "primaryContentStartSlotHasElements": {}, "progressSlotHasElement": {}, "secondarySlotHasElements": {}, "tertiarySlotHasElements": {}, "userSlotHasElements": {} }; } static get events() { return [{ "method": "calciteNavigationActionSelect", "name": "calciteNavigationActionSelect", "bubbles": true, "cancelable": false, "composed": true, "docs": { "tags": [], "text": "When `navigationAction` is true, emits when the displayed action selection changes." }, "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": "When `navigation-action` is `true`, sets focus on the component's action element.", "tags": [] } } }; } static get elementRef() { return "el"; } }