@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
264 lines (263 loc) • 7.37 kB
JavaScript
/*!
* 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 { nodeListToArray } from "../../utils/dom";
import { guid } from "../../utils/guid";
/**
* @slot - A slot for adding custom content.
*/
export class Tab {
constructor() {
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;
}
static get is() { return "calcite-tab"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() {
return {
"$": ["tab.scss"]
};
}
static get styleUrls() {
return {
"$": ["tab.css"]
};
}
static get properties() {
return {
"tab": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Specifies a unique name for the component.\n\nWhen specified, use the same value on the `calcite-tab-title`."
},
"attribute": "tab",
"reflect": true
},
"selected": {
"type": "boolean",
"mutable": true,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When `true`, the component's contents are selected.\n\nOnly one tab can be selected within the `calcite-tabs` parent."
},
"attribute": "selected",
"reflect": true,
"defaultValue": "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": undefined
}],
"text": ""
},
"attribute": "scale",
"reflect": true,
"defaultValue": "\"m\""
}
};
}
static get states() {
return {
"labeledBy": {}
};
}
static get events() {
return [{
"method": "calciteInternalTabRegister",
"name": "calciteInternalTabRegister",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"complexType": {
"original": "void",
"resolved": "void",
"references": {}
}
}];
}
static get methods() {
return {
"getTabIndex": {
"complexType": {
"signature": "() => Promise<number>",
"parameters": [],
"references": {
"Promise": {
"location": "global"
}
},
"return": "Promise<number>"
},
"docs": {
"text": "Returns the index of the component item within the tab array.",
"tags": []
}
},
"updateAriaInfo": {
"complexType": {
"signature": "(tabIds?: string[], titleIds?: string[]) => Promise<void>",
"parameters": [{
"tags": [{
"name": "param",
"text": "tabIds"
}],
"text": ""
}, {
"tags": [{
"name": "param",
"text": "titleIds"
}],
"text": ""
}],
"references": {
"Promise": {
"location": "global"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "",
"tags": [{
"name": "param",
"text": "tabIds"
}, {
"name": "param",
"text": "titleIds"
}, {
"name": "internal",
"text": undefined
}]
}
}
};
}
static get elementRef() { return "el"; }
static get listeners() {
return [{
"name": "calciteInternalTabChange",
"method": "internalTabChangeHandler",
"target": "body",
"capture": false,
"passive": false
}];
}
}