123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- /*!
- * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
- * v1.0.0-beta.82
- */
- import { Component, Element, Event, h, Host, Listen, Prop, State, Watch } from "@stencil/core";
- import { getElementDir, filterDirectChildren } from "../../utils/dom";
- import { createObserver } from "../../utils/observers";
- /**
- * @slot - A slot for adding `calcite-tab-title`s.
- */
- export class TabNav {
- constructor() {
- /** @internal Parent tabs component scale value */
- this.scale = "m";
- /** @internal Parent tabs component layout value */
- this.layout = "inline";
- /** @internal Parent tabs component position value */
- this.position = "below";
- /** @internal Parent tabs component bordered value when layout is "inline" */
- this.bordered = false;
- this.animationActiveDuration = 0.3;
- this.resizeObserver = createObserver("resize", () => {
- // remove active indicator transition duration during resize to prevent wobble
- this.activeIndicatorEl.style.transitionDuration = "0s";
- this.updateActiveWidth();
- this.updateOffsetPosition();
- });
- //--------------------------------------------------------------------------
- //
- // Private Methods
- //
- //--------------------------------------------------------------------------
- this.handleContainerScroll = () => {
- // remove active indicator transition duration while container is scrolling to prevent wobble
- this.activeIndicatorEl.style.transitionDuration = "0s";
- this.updateOffsetPosition();
- };
- }
- async selectedTabChanged() {
- if (localStorage &&
- this.storageId &&
- this.selectedTab !== undefined &&
- this.selectedTab !== null) {
- localStorage.setItem(`calcite-tab-nav-${this.storageId}`, JSON.stringify(this.selectedTab));
- }
- this.calciteInternalTabChange.emit({
- tab: this.selectedTab
- });
- this.selectedTabEl = await this.getTabTitleById(this.selectedTab);
- }
- selectedTabElChanged() {
- this.updateOffsetPosition();
- this.updateActiveWidth();
- // reset the animation time on tab selection
- this.activeIndicatorEl.style.transitionDuration = `${this.animationActiveDuration}s`;
- }
- //--------------------------------------------------------------------------
- //
- // Lifecycle
- //
- //--------------------------------------------------------------------------
- connectedCallback() {
- var _a;
- this.parentTabsEl = this.el.closest("calcite-tabs");
- (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el);
- }
- disconnectedCallback() {
- var _a;
- (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
- }
- componentWillLoad() {
- const storageKey = `calcite-tab-nav-${this.storageId}`;
- if (localStorage && this.storageId && localStorage.getItem(storageKey)) {
- const storedTab = JSON.parse(localStorage.getItem(storageKey));
- this.selectedTab = storedTab;
- }
- }
- componentWillRender() {
- const { parentTabsEl } = this;
- this.layout = parentTabsEl === null || parentTabsEl === void 0 ? void 0 : parentTabsEl.layout;
- this.position = parentTabsEl === null || parentTabsEl === void 0 ? void 0 : parentTabsEl.position;
- this.scale = parentTabsEl === null || parentTabsEl === void 0 ? void 0 : parentTabsEl.scale;
- this.bordered = parentTabsEl === null || parentTabsEl === void 0 ? void 0 : parentTabsEl.bordered;
- // fix issue with active tab-title not lining up with blue indicator
- if (this.selectedTabEl) {
- this.updateOffsetPosition();
- }
- }
- componentDidRender() {
- // if every tab title is active select the first tab.
- if (this.tabTitles.length &&
- this.tabTitles.every((title) => !title.active) &&
- !this.selectedTab) {
- this.tabTitles[0].getTabIdentifier().then((tab) => {
- this.calciteInternalTabChange.emit({
- tab
- });
- });
- }
- }
- render() {
- const dir = getElementDir(this.el);
- const width = `${this.indicatorWidth}px`;
- const offset = `${this.indicatorOffset}px`;
- const indicatorStyle = dir !== "rtl" ? { width, left: offset } : { width, right: offset };
- return (h(Host, { role: "tablist" },
- h("div", { class: "tab-nav", onScroll: this.handleContainerScroll, ref: (el) => (this.tabNavEl = el) },
- h("div", { class: "tab-nav-active-indicator-container", ref: (el) => (this.activeIndicatorContainerEl = el) },
- h("div", { class: "tab-nav-active-indicator", ref: (el) => (this.activeIndicatorEl = el), style: indicatorStyle })),
- h("slot", null))));
- }
- //--------------------------------------------------------------------------
- //
- // Event Listeners
- //
- //--------------------------------------------------------------------------
- focusPreviousTabHandler(e) {
- const currentIndex = this.getIndexOfTabTitle(e.target, this.enabledTabTitles);
- const previousTab = this.enabledTabTitles[currentIndex - 1] ||
- this.enabledTabTitles[this.enabledTabTitles.length - 1];
- previousTab.focus();
- e.stopPropagation();
- e.preventDefault();
- }
- focusNextTabHandler(e) {
- const currentIndex = this.getIndexOfTabTitle(e.target, this.enabledTabTitles);
- const nextTab = this.enabledTabTitles[currentIndex + 1] || this.enabledTabTitles[0];
- nextTab.focus();
- e.stopPropagation();
- e.preventDefault();
- }
- internalActivateTabHandler(e) {
- this.selectedTab = e.detail.tab
- ? e.detail.tab
- : this.getIndexOfTabTitle(e.target);
- e.stopPropagation();
- e.preventDefault();
- }
- activateTabHandler(e) {
- this.calciteTabChange.emit({
- tab: this.selectedTab
- });
- e.stopPropagation();
- e.preventDefault();
- }
- /**
- * Check for active tabs on register and update selected
- */
- updateTabTitles(e) {
- if (e.target.active) {
- this.selectedTab = e.detail;
- }
- }
- globalInternalTabChangeHandler(e) {
- if (this.syncId &&
- e.target !== this.el &&
- e.target.syncId === this.syncId &&
- this.selectedTab !== e.detail.tab) {
- this.selectedTab = e.detail.tab;
- e.stopPropagation();
- }
- }
- updateOffsetPosition() {
- var _a, _b, _c, _d, _e;
- const dir = getElementDir(this.el);
- const navWidth = (_a = this.activeIndicatorContainerEl) === null || _a === void 0 ? void 0 : _a.offsetWidth;
- const tabLeft = (_b = this.selectedTabEl) === null || _b === void 0 ? void 0 : _b.offsetLeft;
- const tabWidth = (_c = this.selectedTabEl) === null || _c === void 0 ? void 0 : _c.offsetWidth;
- const offsetRight = navWidth - (tabLeft + tabWidth);
- this.indicatorOffset =
- dir !== "rtl" ? tabLeft - ((_d = this.tabNavEl) === null || _d === void 0 ? void 0 : _d.scrollLeft) : offsetRight + ((_e = this.tabNavEl) === null || _e === void 0 ? void 0 : _e.scrollLeft);
- }
- updateActiveWidth() {
- var _a;
- this.indicatorWidth = (_a = this.selectedTabEl) === null || _a === void 0 ? void 0 : _a.offsetWidth;
- }
- getIndexOfTabTitle(el, tabTitles = this.tabTitles) {
- // In most cases, since these indexes correlate with tab contents, we want to consider all tab titles.
- // However, when doing relative index operations, it makes sense to pass in this.enabledTabTitles as the 2nd arg.
- return tabTitles.indexOf(el);
- }
- async getTabTitleById(id) {
- return Promise.all(this.tabTitles.map((el) => el.getTabIdentifier())).then((ids) => {
- return this.tabTitles[ids.indexOf(id)];
- });
- }
- get tabTitles() {
- return filterDirectChildren(this.el, "calcite-tab-title");
- }
- get enabledTabTitles() {
- return filterDirectChildren(this.el, "calcite-tab-title:not([disabled])");
- }
- static get is() { return "calcite-tab-nav"; }
- static get encapsulation() { return "shadow"; }
- static get originalStyleUrls() { return {
- "$": ["tab-nav.scss"]
- }; }
- static get styleUrls() { return {
- "$": ["tab-nav.css"]
- }; }
- static get properties() { return {
- "storageId": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Name to use when saving selected tab data to localStorage"
- },
- "attribute": "storage-id",
- "reflect": false
- },
- "syncId": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Pass the same string to multiple tab navs to keep them all in sync if one changes"
- },
- "attribute": "sync-id",
- "reflect": false
- },
- "scale": {
- "type": "string",
- "mutable": true,
- "complexType": {
- "original": "Scale",
- "resolved": "\"l\" | \"m\" | \"s\"",
- "references": {
- "Scale": {
- "location": "import",
- "path": "../interfaces"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": "Parent tabs component scale value"
- }],
- "text": ""
- },
- "attribute": "scale",
- "reflect": true,
- "defaultValue": "\"m\""
- },
- "layout": {
- "type": "string",
- "mutable": true,
- "complexType": {
- "original": "TabLayout",
- "resolved": "\"center\" | \"inline\"",
- "references": {
- "TabLayout": {
- "location": "import",
- "path": "../tabs/interfaces"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": "Parent tabs component layout value"
- }],
- "text": ""
- },
- "attribute": "layout",
- "reflect": true,
- "defaultValue": "\"inline\""
- },
- "position": {
- "type": "string",
- "mutable": true,
- "complexType": {
- "original": "TabPosition",
- "resolved": "\"above\" | \"below\"",
- "references": {
- "TabPosition": {
- "location": "import",
- "path": "../tabs/interfaces"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": "Parent tabs component position value"
- }],
- "text": ""
- },
- "attribute": "position",
- "reflect": true,
- "defaultValue": "\"below\""
- },
- "bordered": {
- "type": "boolean",
- "mutable": true,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": "Parent tabs component bordered value when layout is \"inline\""
- }],
- "text": ""
- },
- "attribute": "bordered",
- "reflect": true,
- "defaultValue": "false"
- },
- "indicatorOffset": {
- "type": "number",
- "mutable": true,
- "complexType": {
- "original": "number",
- "resolved": "number",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": ""
- },
- "attribute": "indicator-offset",
- "reflect": false
- },
- "indicatorWidth": {
- "type": "number",
- "mutable": true,
- "complexType": {
- "original": "number",
- "resolved": "number",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": ""
- },
- "attribute": "indicator-width",
- "reflect": false
- }
- }; }
- static get states() { return {
- "selectedTab": {},
- "selectedTabEl": {}
- }; }
- static get events() { return [{
- "method": "calciteTabChange",
- "name": "calciteTabChange",
- "bubbles": true,
- "cancelable": true,
- "composed": true,
- "docs": {
- "tags": [{
- "name": "see",
- "text": "[TabChangeEventDetail](https://github.com/Esri/calcite-components/blob/master/src/components/tab/interfaces.ts#L1)"
- }],
- "text": "Emitted when the active tab changes"
- },
- "complexType": {
- "original": "TabChangeEventDetail",
- "resolved": "TabChangeEventDetail",
- "references": {
- "TabChangeEventDetail": {
- "location": "import",
- "path": "../tab/interfaces"
- }
- }
- }
- }, {
- "method": "calciteInternalTabChange",
- "name": "calciteInternalTabChange",
- "bubbles": true,
- "cancelable": true,
- "composed": true,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": ""
- },
- "complexType": {
- "original": "TabChangeEventDetail",
- "resolved": "TabChangeEventDetail",
- "references": {
- "TabChangeEventDetail": {
- "location": "import",
- "path": "../tab/interfaces"
- }
- }
- }
- }]; }
- static get elementRef() { return "el"; }
- static get watchers() { return [{
- "propName": "selectedTab",
- "methodName": "selectedTabChanged"
- }, {
- "propName": "selectedTabEl",
- "methodName": "selectedTabElChanged"
- }]; }
- static get listeners() { return [{
- "name": "calciteTabsFocusPrevious",
- "method": "focusPreviousTabHandler",
- "target": undefined,
- "capture": false,
- "passive": false
- }, {
- "name": "calciteTabsFocusNext",
- "method": "focusNextTabHandler",
- "target": undefined,
- "capture": false,
- "passive": false
- }, {
- "name": "calciteInternalTabsActivate",
- "method": "internalActivateTabHandler",
- "target": undefined,
- "capture": false,
- "passive": false
- }, {
- "name": "calciteTabsActivate",
- "method": "activateTabHandler",
- "target": undefined,
- "capture": false,
- "passive": false
- }, {
- "name": "calciteTabTitleRegister",
- "method": "updateTabTitles",
- "target": undefined,
- "capture": false,
- "passive": false
- }, {
- "name": "calciteInternalTabChange",
- "method": "globalInternalTabChangeHandler",
- "target": "body",
- "capture": false,
- "passive": false
- }]; }
- }
|