@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
159 lines (158 loc) • 4.93 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 { createObserver } from "../../utils/observers";
import { CSS } from "./resources";
/**
* @slot - A slot for adding `calcite-flow-item` elements to the component.
*/
export class Flow {
constructor() {
this.itemMutationObserver = createObserver("mutation", () => this.updateFlowProps());
this.getFlowDirection = (oldFlowItemCount, newFlowItemCount) => {
const allowRetreatingDirection = oldFlowItemCount > 1;
const allowAdvancingDirection = oldFlowItemCount && newFlowItemCount > 1;
if (!allowAdvancingDirection && !allowRetreatingDirection) {
return null;
}
return newFlowItemCount < oldFlowItemCount ? "retreating" : "advancing";
};
this.updateFlowProps = () => {
const { el, items } = this;
const newItems = Array.from(el.querySelectorAll("calcite-flow-item")).filter((flowItem) => flowItem.closest("calcite-flow") === el);
const oldItemCount = items.length;
const newItemCount = newItems.length;
const activeItem = newItems[newItemCount - 1];
const previousItem = newItems[newItemCount - 2];
if (newItemCount && activeItem) {
newItems.forEach((itemNode) => {
itemNode.showBackButton = itemNode === activeItem && newItemCount > 1;
itemNode.hidden = itemNode !== activeItem;
});
}
if (previousItem) {
previousItem.menuOpen = false;
}
this.items = newItems;
if (oldItemCount !== newItemCount) {
const flowDirection = this.getFlowDirection(oldItemCount, newItemCount);
this.itemCount = newItemCount;
this.flowDirection = flowDirection;
}
};
this.flowDirection = null;
this.itemCount = 0;
this.items = [];
}
// --------------------------------------------------------------------------
//
// Public Methods
//
// --------------------------------------------------------------------------
/**
* Removes the currently active `calcite-flow-item`.
*/
async back() {
const { items } = this;
const lastItem = items[items.length - 1];
if (!lastItem) {
return;
}
const beforeBack = lastItem.beforeBack
? lastItem.beforeBack
: () => Promise.resolve();
return beforeBack.call(lastItem).then(() => {
lastItem.remove();
return lastItem;
});
}
// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------
connectedCallback() {
this.itemMutationObserver?.observe(this.el, { childList: true, subtree: true });
this.updateFlowProps();
}
disconnectedCallback() {
this.itemMutationObserver?.disconnect();
}
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
handleItemBackClick() {
this.back();
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
render() {
const { flowDirection } = this;
const frameDirectionClasses = {
[CSS.frame]: true,
[CSS.frameAdvancing]: flowDirection === "advancing",
[CSS.frameRetreating]: flowDirection === "retreating"
};
return (h("div", { class: frameDirectionClasses }, h("slot", null)));
}
static get is() { return "calcite-flow"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() {
return {
"$": ["flow.scss"]
};
}
static get styleUrls() {
return {
"$": ["flow.css"]
};
}
static get states() {
return {
"flowDirection": {},
"itemCount": {},
"items": {}
};
}
static get methods() {
return {
"back": {
"complexType": {
"signature": "() => Promise<HTMLCalciteFlowItemElement>",
"parameters": [],
"references": {
"Promise": {
"location": "global"
},
"HTMLCalciteFlowItemElement": {
"location": "global"
}
},
"return": "Promise<HTMLCalciteFlowItemElement>"
},
"docs": {
"text": "Removes the currently active `calcite-flow-item`.",
"tags": []
}
}
};
}
static get elementRef() { return "el"; }
static get listeners() {
return [{
"name": "calciteFlowItemBack",
"method": "handleItemBackClick",
"target": undefined,
"capture": false,
"passive": false
}];
}
}