@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
331 lines (326 loc) • 19.9 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 { proxyCustomElement, HTMLElement, createEvent, Build, h, Host } from '@stencil/core/internal/client';
import { g as getElementProp, t as toAriaBoolean, c as getElementDir, o as nodeListToArray } from './dom.js';
import { g as guid } from './guid.js';
import { c as connectInteractive, d as disconnectInteractive, u as updateHostInteraction } from './interactive.js';
import { c as createObserver } from './observers.js';
import { c as connectLocalized, d as disconnectLocalized } from './locale.js';
import { c as connectMessages, d as disconnectMessages, s as setUpMessages, u as updateMessages } from './t9n.js';
import { d as defineCustomElement$1 } from './icon.js';
const CSS = {
closeButton: "close-button",
container: "container",
content: "content",
contentHasText: "content--has-text",
iconEnd: "icon-end",
iconStart: "icon-start",
iconPresent: "icon-present",
titleIcon: "calcite-tab-title--icon"
};
const ICONS = {
close: "x"
};
const tabTitleCss = "@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([disabled]){cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) *,:host([disabled]) ::slotted(*){pointer-events:none}:host{display:block;flex:0 1 auto;outline:2px solid transparent;outline-offset:2px;margin-inline-start:0px;margin-inline-end:1.25rem}:host([layout=center][scale=s]),:host([layout=center][scale=m]),:host([layout=center][scale=l]){margin-block:0px;text-align:center;margin-inline-end:0px;flex-basis:12rem}:host([layout=center][scale=s]) .content,:host([layout=center][scale=m]) .content,:host([layout=center][scale=l]) .content{margin:auto}:host([layout=center][closable]) .content{padding-inline-start:32px}:host([layout=center][bordered][closable][scale=s]) .content{padding-inline-start:36px}:host([layout=center][bordered][closable][scale=m]) .content{padding-inline-start:40px}:host([layout=center][closable][scale=l]) .content{padding-inline-start:40px}:host([layout=center][closable][bordered][scale=l]) .content{padding-inline-start:52px}:host([position=bottom]) .container{border-block-end-width:0px;border-block-start-width:2px;border-block-start-color:transparent;border-block-start-style:solid}:host([closed]){display:none}.container{outline-color:transparent}:host(:focus) .container{outline:2px solid var(--calcite-ui-focus-color, var(--calcite-ui-brand));outline-offset:calc(\n -2px *\n calc(\n 1 -\n 2 * clamp(\n 0,\n var(--calcite-ui-focus-offset-invert),\n 1\n )\n )\n )}:host(:focus) .container:focus-within{outline-color:transparent}:host(:active) a,:host(:focus) a,:host(:hover) a{border-color:var(--calcite-ui-border-2);color:var(--calcite-ui-text-1);text-decoration-line:none}:host([selected]) .container{border-color:transparent;color:var(--calcite-ui-text-1)}:host([disabled]) .container{pointer-events:none;opacity:0.5}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([scale=s]){margin-inline-end:1rem}:host([scale=s]) .content{padding-block:0.25rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=m]) .content{padding-block:0.5rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=l]){margin-inline-end:1.5rem}:host([scale=l]) .content{padding-block:0.625rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}.container{box-sizing:border-box;display:flex;block-size:100%;inline-size:100%;cursor:pointer;align-content:center;justify-content:space-between;border-block-end-width:2px;padding-inline:0px;font-size:var(--calcite-font-size--1);line-height:1rem;color:var(--calcite-ui-text-3);transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;border-block-end-color:transparent;border-block-end-style:solid}.content{display:flex;align-items:center;justify-content:center}.calcite-tab-title--icon{position:relative;margin:0px;display:inline-flex;align-self:center}.calcite-tab-title--icon svg{transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.content--has-text{padding:0.25rem}.content--has-text .calcite-tab-title--icon.icon-start{margin-inline-end:0.5rem}.content--has-text .calcite-tab-title--icon.icon-end{margin-inline-start:0.5rem}.close-button{display:flex;cursor:pointer;-webkit-appearance:none;appearance:none;align-content:center;align-items:center;justify-content:center;align-self:center;border-style:none;background-color:var(--calcite-ui-foreground-1);color:var(--calcite-ui-text-3);outline-color:transparent;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;block-size:calc(100% - 2px);background-color:var(--calcite-button-transparent-1);margin-inline-start:auto}.close-button:focus{outline:2px solid var(--calcite-ui-focus-color, var(--calcite-ui-brand));outline-offset:calc(\n -2px *\n calc(\n 1 -\n 2 * clamp(\n 0,\n var(--calcite-ui-focus-offset-invert),\n 1\n )\n )\n );outline-offset:-1px}.close-button:focus,.close-button:hover{color:var(--calcite-ui-text-1);background-color:var(--calcite-ui-foreground-2)}.close-button:active{color:var(--calcite-ui-text-1);background-color:var(--calcite-ui-foreground-3)}.close-button calcite-icon{color:inherit}:host([icon-start][icon-end]) .calcite-tab-title--icon:first-child{margin-inline-end:0.5rem}:host([bordered]){margin-inline-end:0}:host([bordered][selected]){box-shadow:inset 0px -2px var(--calcite-ui-foreground-1)}:host([bordered][selected][position=bottom]){box-shadow:inset 0 2px 0 var(--calcite-ui-foreground-1)}:host([bordered]:hover) .container,:host([bordered]:focus) .container,:host([bordered]:active) .container{position:relative}:host([bordered]:hover) .container{background-color:var(--calcite-button-transparent-hover)}:host([closable]) .container,:host([bordered]) .container{border-inline-start:1px solid transparent;border-inline-end:1px solid transparent}:host([closable]) .container .close-button,:host([bordered]) .container .close-button{margin-inline:0}:host([closable]) .content{box-sizing:border-box;block-size:100%;border-block-end-color:transparent}:host([closable][position=bottom]) .container,:host([bordered][position=bottom]) .container{border-block-start-style:unset}:host([selected][bordered]) .container{border-inline-start-color:var(--calcite-ui-border-1);border-inline-end-color:var(--calcite-ui-border-1)}:host([bordered]) .content{padding-inline:0.75rem}:host([bordered][scale=s]) .content{padding-inline:0.5rem}:host([bordered][scale=l]) .content{padding-inline:1rem}@media (forced-colors: active){:host{outline-width:0;outline-offset:0}:host(:focus) .container{outline-color:highlight}:host([bordered]) .container{border-block-end-style:solid}:host([bordered][position=bottom]) .container{border-block-start-style:solid}:host([bordered][selected]) .container{border-block-end-style:none}:host([bordered][position=bottom][selected]) .container{border-block-start-style:none}}";
const TabTitle = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.calciteTabsActivate = createEvent(this, "calciteTabsActivate", 6);
this.calciteInternalTabsActivate = createEvent(this, "calciteInternalTabsActivate", 6);
this.calciteTabsClose = createEvent(this, "calciteTabsClose", 6);
this.calciteInternalTabsClose = createEvent(this, "calciteInternalTabsClose", 6);
this.calciteInternalTabsFocusNext = createEvent(this, "calciteInternalTabsFocusNext", 6);
this.calciteInternalTabsFocusPrevious = createEvent(this, "calciteInternalTabsFocusPrevious", 6);
this.calciteInternalTabsFocusFirst = createEvent(this, "calciteInternalTabsFocusFirst", 6);
this.calciteInternalTabsFocusLast = createEvent(this, "calciteInternalTabsFocusLast", 6);
this.calciteInternalTabTitleRegister = createEvent(this, "calciteInternalTabTitleRegister", 6);
this.calciteInternalTabIconChanged = createEvent(this, "calciteInternalTabIconChanged", 6);
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
this.closeClickHandler = () => {
this.closeTabTitleAndNotify();
};
//--------------------------------------------------------------------------
//
// Private State/Props
//
//--------------------------------------------------------------------------
/** watches for changing text content */
this.mutationObserver = createObserver("mutation", () => this.updateHasText());
this.resizeObserver = createObserver("resize", () => {
this.calciteInternalTabIconChanged.emit();
});
this.guid = `calcite-tab-title-${guid()}`;
this.selected = false;
this.closable = false;
this.closed = false;
this.disabled = false;
this.iconEnd = undefined;
this.iconFlipRtl = undefined;
this.iconStart = undefined;
this.layout = undefined;
this.position = undefined;
this.scale = undefined;
this.bordered = false;
this.tab = undefined;
this.messages = undefined;
this.messageOverrides = undefined;
this.controls = undefined;
this.defaultMessages = undefined;
this.effectiveLocale = undefined;
this.hasText = false;
}
selectedHandler() {
if (this.selected) {
this.emitActiveTab(false);
}
}
onMessagesChange() {
/* wired up by t9n util */
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
connectInteractive(this);
connectLocalized(this);
connectMessages(this);
this.setupTextContentObserver();
this.parentTabNavEl = this.el.closest("calcite-tab-nav");
this.parentTabsEl = this.el.closest("calcite-tabs");
}
disconnectedCallback() {
this.mutationObserver?.disconnect();
// Dispatching to body in order to be listened by other elements that are still connected to the DOM.
document.body?.dispatchEvent(new CustomEvent("calciteTabTitleUnregister", {
detail: this.el
}));
this.resizeObserver?.disconnect();
disconnectInteractive(this);
disconnectLocalized(this);
disconnectMessages(this);
}
async componentWillLoad() {
await setUpMessages(this);
if (Build.isBrowser) {
this.updateHasText();
}
if (this.tab && this.selected) {
this.emitActiveTab(false);
}
}
componentWillRender() {
if (this.parentTabsEl) {
this.layout = this.parentTabsEl.layout;
this.position = this.parentTabsEl.position;
this.scale = this.parentTabsEl.scale;
this.bordered = this.parentTabsEl.bordered;
}
// handle case when tab-nav is only parent
if (!this.parentTabsEl && this.parentTabNavEl) {
this.position = getElementProp(this.parentTabNavEl, "position", this.position);
this.scale = getElementProp(this.parentTabNavEl, "scale", this.scale);
}
}
render() {
const { el, closed } = this;
const id = el.id || this.guid;
const iconStartEl = (h("calcite-icon", { class: { [CSS.titleIcon]: true, [CSS.iconStart]: true }, flipRtl: this.iconFlipRtl === "start" || this.iconFlipRtl === "both", icon: this.iconStart, scale: this.scale === "l" ? "m" : "s" }));
const iconEndEl = (h("calcite-icon", { class: { [CSS.titleIcon]: true, [CSS.iconEnd]: true }, flipRtl: this.iconFlipRtl === "end" || this.iconFlipRtl === "both", icon: this.iconEnd, scale: this.scale === "l" ? "m" : "s" }));
return (h(Host, { "aria-controls": this.controls, "aria-selected": toAriaBoolean(this.selected), id: id, role: "tab", tabIndex: this.selected ? 0 : -1 }, h("div", { class: {
container: true,
[CSS.iconPresent]: !!this.iconStart || !!this.iconEnd
}, hidden: closed,
// eslint-disable-next-line react/jsx-sort-props
ref: (el) => this.resizeObserver?.observe(el) }, h("div", { class: { [CSS.content]: true, [CSS.contentHasText]: this.hasText } }, this.iconStart ? iconStartEl : null, h("slot", null), this.iconEnd ? iconEndEl : null), this.renderCloseButton())));
}
renderCloseButton() {
const { closable, messages } = this;
return closable ? (h("button", { "aria-label": messages.close, class: CSS.closeButton, disabled: false, key: CSS.closeButton, onClick: this.closeClickHandler, title: messages.close, type: "button",
// eslint-disable-next-line react/jsx-sort-props
ref: (el) => (this.closeButtonEl = el) }, h("calcite-icon", { icon: ICONS.close, scale: this.scale === "l" ? "m" : "s" }))) : null;
}
async componentDidLoad() {
this.calciteInternalTabTitleRegister.emit(await this.getTabIdentifier());
}
componentDidRender() {
updateHostInteraction(this, () => {
return this.selected;
});
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
internalTabChangeHandler(event) {
const targetTabsEl = event
.composedPath()
.find((el) => el.tagName === "CALCITE-TABS");
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();
}
onClick() {
if (this.disabled) {
return;
}
this.emitActiveTab();
}
keyDownHandler(event) {
switch (event.key) {
case " ":
case "Enter":
if (!event.composedPath().includes(this.closeButtonEl)) {
this.emitActiveTab();
event.preventDefault();
}
break;
case "ArrowRight":
event.preventDefault();
if (getElementDir(this.el) === "ltr") {
this.calciteInternalTabsFocusNext.emit();
}
else {
this.calciteInternalTabsFocusPrevious.emit();
}
break;
case "ArrowLeft":
event.preventDefault();
if (getElementDir(this.el) === "ltr") {
this.calciteInternalTabsFocusPrevious.emit();
}
else {
this.calciteInternalTabsFocusNext.emit();
}
break;
case "Home":
event.preventDefault();
this.calciteInternalTabsFocusFirst.emit();
break;
case "End":
event.preventDefault();
this.calciteInternalTabsFocusLast.emit();
break;
}
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/**
* Returns the index of the title within the `calcite-tab-nav`.
*/
async getTabIndex() {
return Array.prototype.indexOf.call(nodeListToArray(this.el.parentElement.children).filter((el) => el.matches("calcite-tab-title")), this.el);
}
/**
* @internal
*/
async getTabIdentifier() {
return this.tab ? this.tab : this.getTabIndex();
}
/**
* @param tabIds
* @param titleIds
* @internal
*/
async updateAriaInfo(tabIds = [], titleIds = []) {
this.controls = tabIds[titleIds.indexOf(this.el.id)] || null;
}
effectiveLocaleChange() {
updateMessages(this, this.effectiveLocale);
}
updateHasText() {
this.hasText = this.el.textContent.trim().length > 0;
}
setupTextContentObserver() {
this.mutationObserver?.observe(this.el, { childList: true, subtree: true });
}
emitActiveTab(userTriggered = true) {
if (this.disabled || this.closed) {
return;
}
const payload = { tab: this.tab };
this.calciteInternalTabsActivate.emit(payload);
if (userTriggered) {
// emit in the next frame to let internal events sync up
requestAnimationFrame(() => this.calciteTabsActivate.emit());
}
}
closeTabTitleAndNotify() {
this.closed = true;
this.calciteInternalTabsClose.emit({ tab: this.tab });
this.calciteTabsClose.emit();
}
static get assetsDirs() { return ["assets"]; }
get el() { return this; }
static get watchers() { return {
"selected": ["selectedHandler"],
"messageOverrides": ["onMessagesChange"],
"effectiveLocale": ["effectiveLocaleChange"]
}; }
static get style() { return tabTitleCss; }
}, [1, "calcite-tab-title", {
"selected": [1540],
"closable": [516],
"closed": [1540],
"disabled": [516],
"iconEnd": [513, "icon-end"],
"iconFlipRtl": [513, "icon-flip-rtl"],
"iconStart": [513, "icon-start"],
"layout": [1537],
"position": [1537],
"scale": [1537],
"bordered": [1540],
"tab": [513],
"messages": [1040],
"messageOverrides": [1040],
"controls": [32],
"defaultMessages": [32],
"effectiveLocale": [32],
"hasText": [32],
"getTabIndex": [64],
"getTabIdentifier": [64],
"updateAriaInfo": [64]
}, [[16, "calciteInternalTabChange", "internalTabChangeHandler"], [0, "click", "onClick"], [0, "keydown", "keyDownHandler"]]]);
function defineCustomElement() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-tab-title", "calcite-icon"];
components.forEach(tagName => { switch (tagName) {
case "calcite-tab-title":
if (!customElements.get(tagName)) {
customElements.define(tagName, TabTitle);
}
break;
case "calcite-icon":
if (!customElements.get(tagName)) {
defineCustomElement$1();
}
break;
} });
}
defineCustomElement();
export { TabTitle as T, defineCustomElement as d };