@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
181 lines (180 loc) • 9.15 kB
JavaScript
/*! All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://github.com/Esri/calcite-design-system/blob/dev/LICENSE.md for details.
v3.2.1 */
import { c as customElement } from "../../chunks/runtime.js";
import { ref } from "lit-html/directives/ref.js";
import { keyed } from "lit-html/directives/keyed.js";
import { html } from "lit";
import { LitElement, createEvent, safeClassMap } from "@arcgis/lumina";
import { g as getElementDir } from "../../chunks/dom.js";
import { c as createObserver } from "../../chunks/observers.js";
import { H as Heading } from "../../chunks/Heading.js";
import { l as logger } from "../../chunks/logger.js";
import { u as useT9n } from "../../chunks/useT9n.js";
import { css } from "@lit/reactive-element/css-tag.js";
const CSS = {
header: "header",
heading: "heading",
close: "close",
container: "container",
tipContainer: "tip-container",
tipContainerAdvancing: "tip-container--advancing",
tipContainerRetreating: "tip-container--retreating",
pagination: "pagination",
pagePosition: "page-position",
pageNext: "page-next",
pagePrevious: "page-previous"
};
const ICONS = {
chevronLeft: "chevron-left",
chevronRight: "chevron-right",
close: "x"
};
const styles = css`:host{box-sizing:border-box;display:block;background-color:var(--calcite-color-foreground-1);font-size:var(--calcite-font-size--1);line-height:1rem;color:var(--calcite-color-text-2);--calcite-tip-manager-height: 19vh}:host *{box-sizing:border-box}:host([closed]){display:none}.header{margin:0;display:flex;align-content:space-between;align-items:center;fill:var(--calcite-color-text-2);color:var(--calcite-color-text-2)}.heading{margin:0;padding:0;font-weight:var(--calcite-font-weight-medium)}.header .heading{flex:1 1 auto;padding:.5rem}.header{border-width:0px;border-block-end-width:1px;border-style:solid;border-color:var(--calcite-color-border-3);padding-block:0px;padding-inline-end:0px;padding-inline-start:1rem}.header .heading{padding:0;font-size:var(--calcite-font-size-1);line-height:1.5rem;font-weight:var(--calcite-font-weight-bold);color:var(--calcite-color-text-1)}.container{position:relative;overflow:hidden;outline-color:transparent;min-block-size:150px}.container:focus{outline:2px solid var(--calcite-color-focus, var(--calcite-ui-focus-color, var(--calcite-color-brand)));outline-offset:calc(2px*(1 - (2*clamp(0,var(--calcite-offset-invert-focus),1))))}.tip-container{margin-block-start:1px;display:flex;align-items:flex-start;justify-content:center;overflow:auto;padding:1rem;outline-color:transparent;animation-name:none;animation-duration:var(--calcite-animation-timing);block-size:var(--calcite-tip-manager-height)}.tip-container:focus{outline:2px solid var(--calcite-color-focus, var(--calcite-ui-focus-color, var(--calcite-color-brand)));outline-offset:calc(2px*(1 - (2*clamp(0,var(--calcite-offset-invert-focus),1))))}::slotted(calcite-tip){margin:0;border-style:none;max-inline-size:var(--calcite-tip-max-width)}.tip-container--advancing{animation-name:tip-advance}.tip-container--retreating{animation-name:tip-retreat}.pagination{display:flex;align-items:center;justify-content:center;padding-inline:0px;padding-block:.75rem .5rem}.page-position{margin-block:0px;margin-inline:.5rem;font-size:var(--calcite-font-size--2);line-height:1rem}@keyframes tip-advance{0%{opacity:0;transform:translate3d(50px,0,0) scale(.99)}to{opacity:1;transform:translateZ(0) scale(1)}}@keyframes tip-retreat{0%{opacity:0;transform:translate3d(-50px,0,0) scale(.99)}to{opacity:1;transform:translateZ(0) scale(1)}}:host([hidden]){display:none}[hidden]{display:none}`;
class TipManager extends LitElement {
constructor() {
super(...arguments);
this.messages = useT9n();
this.mutationObserver = createObserver("mutation", () => this.setUpTips());
this.closed = false;
this.calciteTipManagerClose = createEvent({ cancelable: false });
}
static {
this.properties = { direction: [16, {}, { state: true }], groupTitle: [16, {}, { state: true }], selectedIndex: [16, {}, { state: true }], tips: [16, {}, { state: true }], total: [16, {}, { state: true }], closed: [7, {}, { reflect: true, type: Boolean }], headingLevel: [11, {}, { type: Number, reflect: true }], messageOverrides: [0, {}, { attribute: false }] };
}
static {
this.styles = styles;
}
async nextTip() {
this.direction = "advancing";
const nextIndex = this.selectedIndex + 1;
this.selectedIndex = (nextIndex + this.total) % this.total;
}
async previousTip() {
this.direction = "retreating";
const previousIndex = this.selectedIndex - 1;
this.selectedIndex = (previousIndex + this.total) % this.total;
}
connectedCallback() {
super.connectedCallback();
this.setUpTips();
this.mutationObserver?.observe(this.el, { childList: true, subtree: true });
}
async load() {
logger.deprecated("component", {
name: "tip-manager",
removalVersion: 4,
suggested: "carousel"
});
}
willUpdate(changes) {
if (changes.has("closed") && (this.hasUpdated || this.closed !== false)) {
this.direction = null;
}
if (changes.has("selectedIndex")) {
this.selectedChangeHandler();
}
if (changes.has("messages")) {
this.updateGroupTitle();
}
}
loaded() {
this.updateGroupTitle();
}
disconnectedCallback() {
super.disconnectedCallback();
this.mutationObserver?.disconnect();
}
selectedChangeHandler() {
this.showSelectedTip();
this.updateGroupTitle();
}
setUpTips() {
const tips = Array.from(this.el.querySelectorAll("calcite-tip"));
this.total = tips.length;
if (this.total === 0) {
return;
}
const selectedTip = this.el.querySelector("calcite-tip[selected]");
this.tips = tips;
this.selectedIndex = selectedTip ? tips.indexOf(selectedTip) : 0;
tips.forEach((tip) => {
tip.closeDisabled = true;
});
this.showSelectedTip();
}
hideTipManager() {
this.closed = true;
this.calciteTipManagerClose.emit();
}
showSelectedTip() {
this.tips.forEach((tip, index) => {
const isSelected = this.selectedIndex === index;
tip.selected = isSelected;
tip.hidden = !isSelected;
});
}
updateGroupTitle() {
if (this.tips) {
const selectedTip = this.tips[this.selectedIndex];
const tipParent = selectedTip.closest("calcite-tip-group");
this.groupTitle = tipParent?.groupTitle || this.messages?.defaultGroupTitle;
}
}
previousClicked() {
this.previousTip();
}
nextClicked() {
this.nextTip();
}
tipManagerKeyDownHandler(event) {
if (event.target !== this.container) {
return;
}
switch (event.key) {
case "ArrowRight":
event.preventDefault();
this.nextTip();
break;
case "ArrowLeft":
event.preventDefault();
this.previousTip();
break;
case "Home":
event.preventDefault();
this.selectedIndex = 0;
break;
case "End":
event.preventDefault();
this.selectedIndex = this.total - 1;
break;
}
}
storeContainerRef(el) {
this.container = el;
}
renderPagination() {
const dir = getElementDir(this.el);
const { selectedIndex, tips, total, messages } = this;
const nextLabel = messages.next;
const previousLabel = messages.previous;
const paginationLabel = messages.defaultPaginationLabel;
return tips.length > 1 ? html`<footer class=${safeClassMap(CSS.pagination)}><calcite-action class=${safeClassMap(CSS.pagePrevious)} .icon=${dir === "ltr" ? ICONS.chevronLeft : ICONS.chevronRight} @click=${this.previousClicked} scale=m .text=${previousLabel}></calcite-action><span class=${safeClassMap(CSS.pagePosition)}>${`${paginationLabel} ${selectedIndex + 1}/${total}`}</span><calcite-action class=${safeClassMap(CSS.pageNext)} .icon=${dir === "ltr" ? ICONS.chevronRight : ICONS.chevronLeft} @click=${this.nextClicked} scale=m .text=${nextLabel}></calcite-action></footer>` : null;
}
render() {
const { closed, direction, headingLevel, groupTitle, selectedIndex, messages, total } = this;
const closeLabel = messages.close;
if (total === 0) {
return null;
}
return html`<section .ariaHidden=${closed} class=${safeClassMap(CSS.container)} .hidden=${closed} @keydown=${this.tipManagerKeyDownHandler} tabindex=0 ${ref(this.storeContainerRef)}><header class=${safeClassMap(CSS.header)}>${Heading({ class: CSS.heading, level: headingLevel, children: groupTitle })}<calcite-action class=${safeClassMap(CSS.close)} @click=${this.hideTipManager} scale=m .text=${closeLabel}><calcite-icon .icon=${ICONS.close} scale=m></calcite-icon></calcite-action></header>${keyed(selectedIndex, html`<div class=${safeClassMap({
[CSS.tipContainer]: true,
[CSS.tipContainerAdvancing]: !closed && direction === "advancing",
[CSS.tipContainerRetreating]: !closed && direction === "retreating"
})} tabindex=0><slot></slot></div>`)}${this.renderPagination()}</section>`;
}
}
customElement("calcite-tip-manager", TipManager);
export {
TipManager
};