@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
249 lines (244 loc) • 10.5 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 { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
const stepperCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 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;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{position:relative;display:-ms-flexbox;display:flex;width:100%;min-width:100%;-ms-flex-direction:row;flex-direction:row;-ms-flex-wrap:wrap;flex-wrap:wrap;-ms-flex-align:stretch;align-items:stretch;-ms-flex-pack:justify;justify-content:space-between}:host([layout=vertical]){-ms-flex:1 1 auto;flex:1 1 auto;-ms-flex-direction:column;flex-direction:column}:host ::slotted(.calcite-stepper-content){display:-ms-flexbox;display:flex;width:100%;min-width:100%;-ms-flex-direction:column;flex-direction:column;-ms-flex-wrap:wrap;flex-wrap:wrap}";
const Stepper = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.calciteStepperItemChange = createEvent(this, "calciteStepperItemChange", 7);
//--------------------------------------------------------------------------
//
// Public Properties
//
//--------------------------------------------------------------------------
/** optionally display a status icon next to the step title */
this.icon = false;
/** specify the layout of stepper, defaults to horizontal */
this.layout = "horizontal";
/** optionally display the number next to the step title */
this.numbered = false;
/** specify the scale of stepper, defaults to m */
this.scale = "m";
//--------------------------------------------------------------------------
//
// Private State/Props
//
//--------------------------------------------------------------------------
/** created list of Stepper items */
this.items = [];
/** sorted list of Stepper items */
this.sortedItems = [];
}
// watch for removal of disabled to register step
contentWatcher() {
if (this.layout === "horizontal") {
if (!this.stepperContentContainer && this.requestedContent) {
this.addHorizontalContentContainer();
}
this.updateContent(this.requestedContent);
}
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
componentDidLoad() {
// if no stepper items are set as active, default to the first one
if (!this.currentPosition) {
this.calciteStepperItemChange.emit({
position: 0
});
}
}
componentWillLoad() {
if (this.layout === "horizontal" && !this.stepperContentContainer) {
this.addHorizontalContentContainer();
}
}
render() {
return h("slot", null);
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
calciteStepperItemKeyEvent(e) {
const item = e.detail.item;
const itemToFocus = e.target;
const isFirstItem = this.itemIndex(itemToFocus) === 0;
const isLastItem = this.itemIndex(itemToFocus) === this.sortedItems.length - 1;
switch (item.key) {
case "ArrowDown":
case "ArrowRight":
if (isLastItem) {
this.focusFirstItem();
}
else {
this.focusNextItem(itemToFocus);
}
break;
case "ArrowUp":
case "ArrowLeft":
if (isFirstItem) {
this.focusLastItem();
}
else {
this.focusPrevItem(itemToFocus);
}
break;
case "Home":
this.focusFirstItem();
break;
case "End":
this.focusLastItem();
break;
}
}
registerItem(event) {
const item = {
item: event.target,
position: event.detail.position,
content: event.detail.content
};
if (item.content && item.item.active) {
this.requestedContent = item.content;
}
if (!this.items.includes(item)) {
this.items.push(item);
}
this.sortedItems = this.sortItems();
}
updateItem(event) {
if (event.detail.content) {
this.requestedContent = event.detail.content;
}
this.currentPosition = event.detail.position;
this.calciteStepperItemChange.emit({
position: this.currentPosition
});
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** set the next step as active */
async nextStep() {
this.currentPosition =
this.currentPosition + 1 < this.items.length
? this.currentPosition + 1
: this.currentPosition;
this.emitChangedItem();
}
/** set the previous step as active */
async prevStep() {
this.currentPosition =
this.currentPosition - 1 >= 0 ? this.currentPosition - 1 : this.currentPosition;
this.emitChangedItem();
}
/** set the requested step as active */
async goToStep(num) {
this.currentPosition = num - 1;
this.emitChangedItem();
}
/** set the first step as active */
async startStep() {
this.currentPosition = 0;
this.emitChangedItem();
}
/** set the last step as active */
async endStep() {
this.currentPosition = this.items.length - 1;
this.emitChangedItem();
}
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
addHorizontalContentContainer() {
this.stepperContentContainer = document.createElement("div");
this.stepperContentContainer.classList.add("calcite-stepper-content");
this.el.insertAdjacentElement("beforeend", this.stepperContentContainer);
}
emitChangedItem() {
this.calciteStepperItemChange.emit({
position: this.currentPosition
});
}
focusFirstItem() {
const firstItem = this.sortedItems[0];
this.focusElement(firstItem);
}
focusLastItem() {
const lastItem = this.sortedItems[this.sortedItems.length - 1];
this.focusElement(lastItem);
}
focusNextItem(e) {
const index = this.itemIndex(e);
const nextItem = this.sortedItems[index + 1] || this.sortedItems[0];
this.focusElement(nextItem);
}
focusPrevItem(e) {
const index = this.itemIndex(e);
const prevItem = this.sortedItems[index - 1] || this.sortedItems[this.sortedItems.length - 1];
this.focusElement(prevItem);
}
itemIndex(e) {
return this.sortedItems.indexOf(e);
}
focusElement(item) {
const target = item;
target.focus();
}
sortItems() {
const items = Array.from(this.items)
.filter((a) => !a.item.disabled)
.sort((a, b) => a.position - b.position)
.map((a) => a.item);
return [...Array.from(new Set(items))];
}
updateContent(content) {
this.stepperContentContainer.innerHTML = "";
this.stepperContentContainer.append(...content);
}
get el() { return this; }
static get watchers() { return {
"requestedContent": ["contentWatcher"]
}; }
static get style() { return stepperCss; }
}, [1, "calcite-stepper", {
"icon": [516],
"layout": [513],
"numbered": [516],
"scale": [513],
"requestedContent": [1040],
"nextStep": [64],
"prevStep": [64],
"goToStep": [64],
"startStep": [64],
"endStep": [64]
}, [[0, "calciteStepperItemKeyEvent", "calciteStepperItemKeyEvent"], [0, "calciteStepperItemRegister", "registerItem"], [0, "calciteStepperItemSelect", "updateItem"]]]);
function defineCustomElement$1() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-stepper"];
components.forEach(tagName => { switch (tagName) {
case "calcite-stepper":
if (!customElements.get(tagName)) {
customElements.define(tagName, Stepper);
}
break;
} });
}
defineCustomElement$1();
const CalciteStepper = Stepper;
const defineCustomElement = defineCustomElement$1;
export { CalciteStepper, defineCustomElement };