@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
363 lines (362 loc) • 11.6 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 } from "@stencil/core";
import { getElementDir, toAriaBoolean } from "../../utils/dom";
import { connectLocalized, disconnectLocalized } from "../../utils/locale";
import { createObserver } from "../../utils/observers";
import { connectMessages, disconnectMessages, setUpMessages, updateMessages } from "../../utils/t9n";
import { Heading } from "../functional/Heading";
import { CSS, ICONS } from "./resources";
/**
* @slot - A slot for adding `calcite-tip`s.
*/
export class TipManager {
constructor() {
this.mutationObserver = createObserver("mutation", () => this.setUpTips());
this.hideTipManager = () => {
this.closed = true;
this.calciteTipManagerClose.emit();
};
this.previousClicked = () => {
this.previousTip();
};
this.nextClicked = () => {
this.nextTip();
};
this.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;
}
};
this.storeContainerRef = (el) => {
this.container = el;
};
this.closed = false;
this.headingLevel = undefined;
this.messages = undefined;
this.messageOverrides = undefined;
this.selectedIndex = undefined;
this.tips = undefined;
this.total = undefined;
this.direction = undefined;
this.groupTitle = undefined;
this.defaultMessages = undefined;
this.effectiveLocale = "";
}
closedChangeHandler() {
this.direction = null;
}
onMessagesChange() {
/* wired up by t9n util */
}
selectedChangeHandler() {
this.showSelectedTip();
this.updateGroupTitle();
}
async effectiveLocaleChange() {
await updateMessages(this, this.effectiveLocale);
this.updateGroupTitle();
}
// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------
connectedCallback() {
connectLocalized(this);
connectMessages(this);
this.setUpTips();
this.mutationObserver?.observe(this.el, { childList: true, subtree: true });
}
async componentWillLoad() {
await setUpMessages(this);
this.updateGroupTitle();
}
disconnectedCallback() {
this.mutationObserver?.disconnect();
disconnectLocalized(this);
disconnectMessages(this);
}
// --------------------------------------------------------------------------
//
// Public Methods
//
// --------------------------------------------------------------------------
/** Selects the next `calcite-tip` to display. */
async nextTip() {
this.direction = "advancing";
const nextIndex = this.selectedIndex + 1;
this.selectedIndex = (nextIndex + this.total) % this.total;
}
/** Selects the previous `calcite-tip` to display. */
async previousTip() {
this.direction = "retreating";
const previousIndex = this.selectedIndex - 1;
this.selectedIndex = (previousIndex + this.total) % this.total;
}
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
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();
}
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;
}
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
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 ? (h("footer", { class: CSS.pagination }, h("calcite-action", { class: CSS.pagePrevious, icon: dir === "ltr" ? ICONS.chevronLeft : ICONS.chevronRight, onClick: this.previousClicked, scale: "m", text: previousLabel }), h("span", { class: CSS.pagePosition }, `${paginationLabel} ${selectedIndex + 1}/${total}`), h("calcite-action", { class: CSS.pageNext, icon: dir === "ltr" ? ICONS.chevronRight : ICONS.chevronLeft, onClick: this.nextClicked, scale: "m", text: nextLabel }))) : null;
}
render() {
const { closed, direction, headingLevel, groupTitle, selectedIndex, messages, total } = this;
const closeLabel = messages.close;
if (total === 0) {
return null;
}
return (h("section", { "aria-hidden": toAriaBoolean(closed), class: CSS.container, hidden: closed, onKeyDown: this.tipManagerKeyDownHandler, tabIndex: 0,
// eslint-disable-next-line react/jsx-sort-props
ref: this.storeContainerRef }, h("header", { class: CSS.header }, h(Heading, { class: CSS.heading, level: headingLevel }, groupTitle), h("calcite-action", { class: CSS.close, onClick: this.hideTipManager, scale: "m", text: closeLabel }, h("calcite-icon", { icon: ICONS.close, scale: "m" }))), h("div", { class: {
[CSS.tipContainer]: true,
[CSS.tipContainerAdvancing]: !closed && direction === "advancing",
[CSS.tipContainerRetreating]: !closed && direction === "retreating"
}, key: selectedIndex, tabIndex: 0 }, h("slot", null)), this.renderPagination()));
}
static get is() { return "calcite-tip-manager"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() {
return {
"$": ["tip-manager.scss"]
};
}
static get styleUrls() {
return {
"$": ["tip-manager.css"]
};
}
static get assetsDirs() { return ["assets"]; }
static get properties() {
return {
"closed": {
"type": "boolean",
"mutable": true,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When `true`, does not display or position the component."
},
"attribute": "closed",
"reflect": true,
"defaultValue": "false"
},
"headingLevel": {
"type": "number",
"mutable": false,
"complexType": {
"original": "HeadingLevel",
"resolved": "1 | 2 | 3 | 4 | 5 | 6",
"references": {
"HeadingLevel": {
"location": "import",
"path": "../functional/Heading"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Specifies the number at which section headings should start."
},
"attribute": "heading-level",
"reflect": true
},
"messages": {
"type": "unknown",
"mutable": true,
"complexType": {
"original": "TipManagerMessages",
"resolved": "{ defaultGroupTitle: string; defaultPaginationLabel: string; close: string; previous: string; next: string; }",
"references": {
"TipManagerMessages": {
"location": "import",
"path": "./assets/tip-manager/t9n"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": "Made into a prop for testing purposes only"
}
},
"messageOverrides": {
"type": "unknown",
"mutable": true,
"complexType": {
"original": "Partial<TipManagerMessages>",
"resolved": "{ defaultGroupTitle?: string; defaultPaginationLabel?: string; close?: string; previous?: string; next?: string; }",
"references": {
"Partial": {
"location": "global"
},
"TipManagerMessages": {
"location": "import",
"path": "./assets/tip-manager/t9n"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Use this property to override individual strings used by the component."
}
}
};
}
static get states() {
return {
"selectedIndex": {},
"tips": {},
"total": {},
"direction": {},
"groupTitle": {},
"defaultMessages": {},
"effectiveLocale": {}
};
}
static get events() {
return [{
"method": "calciteTipManagerClose",
"name": "calciteTipManagerClose",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [],
"text": "Emits when the component has been closed."
},
"complexType": {
"original": "void",
"resolved": "void",
"references": {}
}
}];
}
static get methods() {
return {
"nextTip": {
"complexType": {
"signature": "() => Promise<void>",
"parameters": [],
"references": {
"Promise": {
"location": "global"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "Selects the next `calcite-tip` to display.",
"tags": []
}
},
"previousTip": {
"complexType": {
"signature": "() => Promise<void>",
"parameters": [],
"references": {
"Promise": {
"location": "global"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "Selects the previous `calcite-tip` to display.",
"tags": []
}
}
};
}
static get elementRef() { return "el"; }
static get watchers() {
return [{
"propName": "closed",
"methodName": "closedChangeHandler"
}, {
"propName": "messageOverrides",
"methodName": "onMessagesChange"
}, {
"propName": "selectedIndex",
"methodName": "selectedChangeHandler"
}, {
"propName": "effectiveLocale",
"methodName": "effectiveLocaleChange"
}];
}
}