@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
369 lines (368 loc) • 11.4 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.
*/
import { Component, Element, Event, Method, Prop, State, Watch, h } from "@stencil/core";
import { CSS, ICONS, TEXT, HEADING_LEVEL } from "./resources";
import { getElementDir } from "../../utils/dom";
import { Heading } from "../functional/Heading";
import { createObserver } from "../../utils/observers";
/**
* @slot - A slot for adding `calcite-tip`s.
*/
export class TipManager {
constructor() {
// --------------------------------------------------------------------------
//
// Properties
//
// --------------------------------------------------------------------------
/**
* Alternate text for closing the `calcite-tip-manager`.
*/
this.closed = false;
this.mutationObserver = createObserver("mutation", () => this.setUpTips());
this.hideTipManager = () => {
this.closed = true;
this.calciteTipManagerToggle.emit();
};
this.previousClicked = () => {
this.previousTip();
};
this.nextClicked = () => {
this.nextTip();
};
this.tipManagerKeyUpHandler = (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;
};
}
closedChangeHandler() {
this.direction = null;
this.calciteTipManagerToggle.emit();
}
selectedChangeHandler() {
this.showSelectedTip();
this.updateGroupTitle();
}
// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------
connectedCallback() {
var _a;
this.setUpTips();
(_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
}
disconnectedCallback() {
var _a;
(_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
}
// --------------------------------------------------------------------------
//
// Public Methods
//
// --------------------------------------------------------------------------
/** Selects the next tip to display */
async nextTip() {
this.direction = "advancing";
const nextIndex = this.selectedIndex + 1;
this.selectedIndex = (nextIndex + this.total) % this.total;
}
/** Selects the previous 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.nonDismissible = true;
});
this.showSelectedTip();
this.updateGroupTitle();
}
showSelectedTip() {
this.tips.forEach((tip, index) => {
const isSelected = this.selectedIndex === index;
tip.selected = isSelected;
tip.hidden = !isSelected;
});
}
updateGroupTitle() {
const selectedTip = this.tips[this.selectedIndex];
const tipParent = selectedTip.closest("calcite-tip-group");
this.groupTitle = (tipParent === null || tipParent === void 0 ? void 0 : tipParent.groupTitle) || this.intlDefaultTitle || TEXT.defaultGroupTitle;
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
renderPagination() {
const dir = getElementDir(this.el);
const { selectedIndex, tips, total, intlNext, intlPrevious, intlPaginationLabel } = this;
const nextLabel = intlNext || TEXT.next;
const previousLabel = intlPrevious || TEXT.previous;
const paginationLabel = intlPaginationLabel || TEXT.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, intlClose, total } = this;
const closeLabel = intlClose || TEXT.close;
if (total === 0) {
return null;
}
return (h("section", { "aria-hidden": closed.toString(), class: CSS.container, hidden: closed, onKeyUp: this.tipManagerKeyUpHandler, ref: this.storeContainerRef, tabIndex: 0 },
h("header", { class: CSS.header },
h(Heading, { class: CSS.heading, level: headingLevel || HEADING_LEVEL }, 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 properties() { return {
"closed": {
"type": "boolean",
"mutable": true,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Alternate text for closing the `calcite-tip-manager`."
},
"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": "Number at which section headings should start for this component."
},
"attribute": "heading-level",
"reflect": false
},
"intlClose": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Alternate text for closing the tip."
},
"attribute": "intl-close",
"reflect": false
},
"intlDefaultTitle": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "The default group title for the `calcite-tip-manager`."
},
"attribute": "intl-default-title",
"reflect": false
},
"intlNext": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Alternate text for navigating to the next tip."
},
"attribute": "intl-next",
"reflect": false
},
"intlPaginationLabel": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Label that appears on hover of pagination icon."
},
"attribute": "intl-pagination-label",
"reflect": false
},
"intlPrevious": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Alternate text for navigating to the previous tip."
},
"attribute": "intl-previous",
"reflect": false
}
}; }
static get states() { return {
"selectedIndex": {},
"tips": {},
"total": {},
"direction": {},
"groupTitle": {}
}; }
static get events() { return [{
"method": "calciteTipManagerToggle",
"name": "calciteTipManagerToggle",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "Emitted when the `calcite-tip-manager` has been toggled open or closed."
},
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
}
}]; }
static get methods() { return {
"nextTip": {
"complexType": {
"signature": "() => Promise<void>",
"parameters": [],
"references": {
"Promise": {
"location": "global"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "Selects the next tip to display",
"tags": []
}
},
"previousTip": {
"complexType": {
"signature": "() => Promise<void>",
"parameters": [],
"references": {
"Promise": {
"location": "global"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "Selects the previous tip to display",
"tags": []
}
}
}; }
static get elementRef() { return "el"; }
static get watchers() { return [{
"propName": "closed",
"methodName": "closedChangeHandler"
}, {
"propName": "selectedIndex",
"methodName": "selectedChangeHandler"
}]; }
}