calcite-flow.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. import { proxyCustomElement, HTMLElement, h } from '@stencil/core/internal/client/index.js';
  7. import { c as createObserver } from './observers.js';
  8. const CSS = {
  9. frame: "frame",
  10. frameAdvancing: "frame--advancing",
  11. frameRetreating: "frame--retreating"
  12. };
  13. const flowCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity: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;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:host{box-sizing:border-box;background-color:var(--calcite-ui-foreground-1);color:var(--calcite-ui-text-2);font-size:var(--calcite-font-size--1)}:host *{box-sizing:border-box}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{position:relative;display:flex;inline-size:100%;flex:1 1 auto;align-items:stretch;overflow:hidden;background-color:transparent}:host .frame{position:relative;margin:0px;display:flex;inline-size:100%;flex:1 1 auto;flex-direction:column;align-items:stretch;padding:0px}:host ::slotted(calcite-flow-item),:host ::slotted(calcite-panel){block-size:100%}:host ::slotted(.calcite-match-height:last-child){display:flex;flex:1 1 auto;overflow:hidden}:host .frame--advancing{animation:calcite-frame-advance var(--calcite-animation-timing)}:host .frame--retreating{animation:calcite-frame-retreat var(--calcite-animation-timing)}@keyframes calcite-frame-advance{0%{--tw-bg-opacity:0.5;transform:translate3d(50px, 0, 0)}100%{--tw-bg-opacity:1;transform:translate3d(0, 0, 0)}}@keyframes calcite-frame-retreat{0%{--tw-bg-opacity:0.5;transform:translate3d(-50px, 0, 0)}100%{--tw-bg-opacity:1;transform:translate3d(0, 0, 0)}}";
  14. const Flow = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  15. constructor() {
  16. super();
  17. this.__registerHost();
  18. this.__attachShadow();
  19. this.flowDirection = null;
  20. this.itemCount = 0;
  21. this.items = [];
  22. this.itemMutationObserver = createObserver("mutation", () => this.updateFlowProps());
  23. this.getFlowDirection = (oldFlowItemCount, newFlowItemCount) => {
  24. const allowRetreatingDirection = oldFlowItemCount > 1;
  25. const allowAdvancingDirection = oldFlowItemCount && newFlowItemCount > 1;
  26. if (!allowAdvancingDirection && !allowRetreatingDirection) {
  27. return null;
  28. }
  29. return newFlowItemCount < oldFlowItemCount ? "retreating" : "advancing";
  30. };
  31. this.updateFlowProps = () => {
  32. const { el, items } = this;
  33. const newItems = Array.from(el.querySelectorAll("calcite-flow-item, calcite-panel")).filter((flowItem) => !flowItem.matches("calcite-flow-item calcite-flow-item, calcite-panel calcite-panel"));
  34. const oldItemCount = items.length;
  35. const newItemCount = newItems.length;
  36. const activeItem = newItems[newItemCount - 1];
  37. const previousItem = newItems[newItemCount - 2];
  38. if (newItemCount && activeItem) {
  39. newItems.forEach((itemNode) => {
  40. itemNode.showBackButton = itemNode === activeItem && newItemCount > 1;
  41. itemNode.hidden = itemNode !== activeItem;
  42. });
  43. }
  44. if (previousItem) {
  45. previousItem.menuOpen = false;
  46. }
  47. this.items = newItems;
  48. if (oldItemCount !== newItemCount) {
  49. const flowDirection = this.getFlowDirection(oldItemCount, newItemCount);
  50. this.itemCount = newItemCount;
  51. this.flowDirection = flowDirection;
  52. }
  53. };
  54. }
  55. // --------------------------------------------------------------------------
  56. //
  57. // Public Methods
  58. //
  59. // --------------------------------------------------------------------------
  60. /**
  61. * Removes the currently active `calcite-flow-item` or `calcite-panel`.
  62. */
  63. async back() {
  64. const { items } = this;
  65. const lastItem = items[items.length - 1];
  66. if (!lastItem) {
  67. return;
  68. }
  69. const beforeBack = lastItem.beforeBack
  70. ? lastItem.beforeBack
  71. : () => Promise.resolve();
  72. return beforeBack.call(lastItem).then(() => {
  73. lastItem.remove();
  74. return lastItem;
  75. });
  76. }
  77. // --------------------------------------------------------------------------
  78. //
  79. // Lifecycle
  80. //
  81. // --------------------------------------------------------------------------
  82. connectedCallback() {
  83. var _a;
  84. (_a = this.itemMutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  85. this.updateFlowProps();
  86. }
  87. disconnectedCallback() {
  88. var _a;
  89. (_a = this.itemMutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  90. }
  91. // --------------------------------------------------------------------------
  92. //
  93. // Private Methods
  94. //
  95. // --------------------------------------------------------------------------
  96. handleItemBackClick() {
  97. this.back();
  98. }
  99. // --------------------------------------------------------------------------
  100. //
  101. // Render Methods
  102. //
  103. // --------------------------------------------------------------------------
  104. render() {
  105. const { flowDirection } = this;
  106. const frameDirectionClasses = {
  107. [CSS.frame]: true,
  108. [CSS.frameAdvancing]: flowDirection === "advancing",
  109. [CSS.frameRetreating]: flowDirection === "retreating"
  110. };
  111. return (h("div", { class: frameDirectionClasses }, h("slot", null)));
  112. }
  113. get el() { return this; }
  114. static get style() { return flowCss; }
  115. }, [1, "calcite-flow", {
  116. "flowDirection": [32],
  117. "itemCount": [32],
  118. "items": [32],
  119. "back": [64]
  120. }, [[0, "calciteFlowItemBackClick", "handleItemBackClick"], [0, "calcitePanelBackClick", "handleItemBackClick"]]]);
  121. function defineCustomElement$1() {
  122. if (typeof customElements === "undefined") {
  123. return;
  124. }
  125. const components = ["calcite-flow"];
  126. components.forEach(tagName => { switch (tagName) {
  127. case "calcite-flow":
  128. if (!customElements.get(tagName)) {
  129. customElements.define(tagName, Flow);
  130. }
  131. break;
  132. } });
  133. }
  134. defineCustomElement$1();
  135. const CalciteFlow = Flow;
  136. const defineCustomElement = defineCustomElement$1;
  137. export { CalciteFlow, defineCustomElement };