UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

123 lines (119 loc) 6.3 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 { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client'; import { o as nodeListToArray } from './dom.js'; import { g as guid } from './guid.js'; const tabCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-right{0%{opacity:0;transform:translate3D(-5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-left{0%{opacity:0;transform:translate3D(5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-right{animation-name:in-right}.calcite-animate__in-left{animation-name:in-left}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing);--calcite-floating-ui-z-index:var(--calcite-app-z-index-dropdown)}:host([hidden]){display:none}:host([selected]) section,:host([selected]) .container{display:block}:host{display:none;block-size:100%;inline-size:100%}:host([selected]){display:block;block-size:100%;inline-size:100%;overflow:auto}section,.container{display:none;block-size:100%;inline-size:100%}:host([scale=s]){padding-block:0.25rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=m]){padding-block:0.5rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=l]){font-size:var(--calcite-font-size-0);line-height:1.25rem;padding-block:13px}"; const Tab = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement { constructor() { super(); this.__registerHost(); this.__attachShadow(); this.calciteInternalTabRegister = createEvent(this, "calciteInternalTabRegister", 6); this.guid = `calcite-tab-title-${guid()}`; this.tab = undefined; this.selected = false; this.scale = "m"; this.labeledBy = undefined; } //-------------------------------------------------------------------------- // // Lifecycle // //-------------------------------------------------------------------------- render() { const id = this.el.id || this.guid; return (h(Host, { "aria-labelledby": this.labeledBy, id: id }, h("div", { class: "container", role: "tabpanel", tabIndex: this.selected ? 0 : -1 }, h("section", null, h("slot", null))))); } connectedCallback() { this.parentTabsEl = this.el.closest("calcite-tabs"); } componentDidLoad() { this.calciteInternalTabRegister.emit(); } componentWillRender() { this.scale = this.parentTabsEl?.scale; } disconnectedCallback() { // Dispatching to body in order to be listened by other elements that are still connected to the DOM. document.body?.dispatchEvent(new CustomEvent("calciteTabUnregister", { detail: this.el })); } //-------------------------------------------------------------------------- // // Event Listeners // //-------------------------------------------------------------------------- internalTabChangeHandler(event) { const targetTabsEl = event .composedPath() .find((el) => el.tagName === "CALCITE-TABS"); // to allow `<calcite-tabs>` to be nested we need to make sure this // `calciteTabChange` event was actually fired from a within the same // `<calcite-tabs>` that is the a parent of this tab. if (targetTabsEl !== this.parentTabsEl) { return; } if (this.tab) { this.selected = this.tab === event.detail.tab; } else { this.getTabIndex().then((index) => { this.selected = index === event.detail.tab; }); } event.stopPropagation(); } //-------------------------------------------------------------------------- // // Public Methods // //-------------------------------------------------------------------------- /** * Returns the index of the component item within the tab array. */ async getTabIndex() { return Array.prototype.indexOf.call(nodeListToArray(this.el.parentElement.children).filter((el) => el.matches("calcite-tab")), this.el); } //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- /** * @param tabIds * @param titleIds * @internal */ async updateAriaInfo(tabIds = [], titleIds = []) { this.labeledBy = titleIds[tabIds.indexOf(this.el.id)] || null; } get el() { return this; } static get style() { return tabCss; } }, [1, "calcite-tab", { "tab": [513], "selected": [1540], "scale": [1537], "labeledBy": [32], "getTabIndex": [64], "updateAriaInfo": [64] }, [[16, "calciteInternalTabChange", "internalTabChangeHandler"]]]); function defineCustomElement() { if (typeof customElements === "undefined") { return; } const components = ["calcite-tab"]; components.forEach(tagName => { switch (tagName) { case "calcite-tab": if (!customElements.get(tagName)) { customElements.define(tagName, Tab); } break; } }); } defineCustomElement(); export { Tab as T, defineCustomElement as d };