calcite-tip-manager.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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, createEvent, h } from '@stencil/core/internal/client/index.js';
  7. import { T as TEXT, C as CSS, I as ICONS, H as HEADING_LEVEL } from './resources3.js';
  8. import { c as getElementDir, t as toAriaBoolean } from './dom.js';
  9. import { H as Heading } from './Heading.js';
  10. import { c as createObserver } from './observers.js';
  11. import { d as defineCustomElement$4 } from './action.js';
  12. import { d as defineCustomElement$3 } from './icon.js';
  13. import { d as defineCustomElement$2 } from './loader.js';
  14. const tipManagerCss = "@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}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{box-sizing:border-box;display:block;background-color:var(--calcite-ui-foreground-1);font-size:var(--calcite-font-size--1);line-height:1rem;color:var(--calcite-ui-text-2);--calcite-tip-manager-height:19vh}:host *{box-sizing:border-box}:host([closed]){display:none}.header{margin:0px;display:flex;align-content:space-between;align-items:center;fill:var(--calcite-ui-text-2);color:var(--calcite-ui-text-2)}.heading{margin:0px;padding:0px;font-weight:var(--calcite-font-weight-medium)}.header .heading{flex:1 1 auto;padding:0.5rem}.header{border-width:0px;border-block-end-width:1px;border-style:solid;border-color:var(--calcite-ui-border-3);padding-block:0px;padding-inline-end:0px;padding-inline-start:1rem}.header .heading{padding:0px;font-size:var(--calcite-font-size-1);line-height:1.5rem;font-weight:var(--calcite-font-weight-bold);color:var(--calcite-ui-text-1)}.container{position:relative;overflow:hidden;outline-color:transparent;min-block-size:150px}.container:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:2px}.tip-container{margin-block-start:1px;display:flex;align-items:flex-start;justify-content:center;overflow:auto;outline-color:transparent;animation-name:none;animation-duration:var(--calcite-animation-timing);block-size:var(--calcite-tip-manager-height)}.tip-container:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:2px}::slotted(calcite-tip){border-style:none;padding:0.75rem;max-inline-size:var(--calcite-tip-max-width)}.tip-container--advancing{animation-name:tip-advance}.tip-container--retreating{animation-name:tip-retreat}.pagination{display:flex;align-items:center;justify-content:center;padding-inline:0px;padding-block:0.75rem 0.5rem}.page-position{margin-block:0px;margin-inline:0.5rem;font-size:var(--calcite-font-size--2);line-height:1rem}@keyframes tip-advance{0%{opacity:0;transform:translate3d(50px, 0, 0) scale(0.99)}100%{opacity:1;transform:translate3d(0, 0, 0) scale(1)}}@keyframes tip-retreat{0%{opacity:0;transform:translate3d(-50px, 0, 0) scale(0.99)}100%{opacity:1;transform:translate3d(0, 0, 0) scale(1)}}";
  15. const TipManager = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  16. constructor() {
  17. super();
  18. this.__registerHost();
  19. this.__attachShadow();
  20. this.calciteTipManagerToggle = createEvent(this, "calciteTipManagerToggle", 6);
  21. this.calciteTipManagerClose = createEvent(this, "calciteTipManagerClose", 6);
  22. // --------------------------------------------------------------------------
  23. //
  24. // Properties
  25. //
  26. // --------------------------------------------------------------------------
  27. /**
  28. * When `true`, does not display or position the component.
  29. */
  30. this.closed = false;
  31. this.mutationObserver = createObserver("mutation", () => this.setUpTips());
  32. this.hideTipManager = () => {
  33. this.closed = true;
  34. this.calciteTipManagerToggle.emit();
  35. this.calciteTipManagerClose.emit();
  36. };
  37. this.previousClicked = () => {
  38. this.previousTip();
  39. };
  40. this.nextClicked = () => {
  41. this.nextTip();
  42. };
  43. this.tipManagerKeyDownHandler = (event) => {
  44. if (event.target !== this.container) {
  45. return;
  46. }
  47. switch (event.key) {
  48. case "ArrowRight":
  49. event.preventDefault();
  50. this.nextTip();
  51. break;
  52. case "ArrowLeft":
  53. event.preventDefault();
  54. this.previousTip();
  55. break;
  56. case "Home":
  57. event.preventDefault();
  58. this.selectedIndex = 0;
  59. break;
  60. case "End":
  61. event.preventDefault();
  62. this.selectedIndex = this.total - 1;
  63. break;
  64. }
  65. };
  66. this.storeContainerRef = (el) => {
  67. this.container = el;
  68. };
  69. }
  70. closedChangeHandler() {
  71. this.direction = null;
  72. this.calciteTipManagerToggle.emit();
  73. }
  74. selectedChangeHandler() {
  75. this.showSelectedTip();
  76. this.updateGroupTitle();
  77. }
  78. // --------------------------------------------------------------------------
  79. //
  80. // Lifecycle
  81. //
  82. // --------------------------------------------------------------------------
  83. connectedCallback() {
  84. var _a;
  85. this.setUpTips();
  86. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  87. }
  88. disconnectedCallback() {
  89. var _a;
  90. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  91. }
  92. // --------------------------------------------------------------------------
  93. //
  94. // Public Methods
  95. //
  96. // --------------------------------------------------------------------------
  97. /** Selects the next `calcite-tip` to display. */
  98. async nextTip() {
  99. this.direction = "advancing";
  100. const nextIndex = this.selectedIndex + 1;
  101. this.selectedIndex = (nextIndex + this.total) % this.total;
  102. }
  103. /** Selects the previous `calcite-tip` to display. */
  104. async previousTip() {
  105. this.direction = "retreating";
  106. const previousIndex = this.selectedIndex - 1;
  107. this.selectedIndex = (previousIndex + this.total) % this.total;
  108. }
  109. // --------------------------------------------------------------------------
  110. //
  111. // Private Methods
  112. //
  113. // --------------------------------------------------------------------------
  114. setUpTips() {
  115. const tips = Array.from(this.el.querySelectorAll("calcite-tip"));
  116. this.total = tips.length;
  117. if (this.total === 0) {
  118. return;
  119. }
  120. const selectedTip = this.el.querySelector("calcite-tip[selected]");
  121. this.tips = tips;
  122. this.selectedIndex = selectedTip ? tips.indexOf(selectedTip) : 0;
  123. tips.forEach((tip) => {
  124. tip.nonDismissible = true;
  125. });
  126. this.showSelectedTip();
  127. this.updateGroupTitle();
  128. }
  129. showSelectedTip() {
  130. this.tips.forEach((tip, index) => {
  131. const isSelected = this.selectedIndex === index;
  132. tip.selected = isSelected;
  133. tip.hidden = !isSelected;
  134. });
  135. }
  136. updateGroupTitle() {
  137. const selectedTip = this.tips[this.selectedIndex];
  138. const tipParent = selectedTip.closest("calcite-tip-group");
  139. this.groupTitle = (tipParent === null || tipParent === void 0 ? void 0 : tipParent.groupTitle) || this.intlDefaultTitle || TEXT.defaultGroupTitle;
  140. }
  141. // --------------------------------------------------------------------------
  142. //
  143. // Render Methods
  144. //
  145. // --------------------------------------------------------------------------
  146. renderPagination() {
  147. const dir = getElementDir(this.el);
  148. const { selectedIndex, tips, total, intlNext, intlPrevious, intlPaginationLabel } = this;
  149. const nextLabel = intlNext || TEXT.next;
  150. const previousLabel = intlPrevious || TEXT.previous;
  151. const paginationLabel = intlPaginationLabel || TEXT.defaultPaginationLabel;
  152. 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;
  153. }
  154. render() {
  155. const { closed, direction, headingLevel, groupTitle, selectedIndex, intlClose, total } = this;
  156. const closeLabel = intlClose || TEXT.close;
  157. if (total === 0) {
  158. return null;
  159. }
  160. return (h("section", { "aria-hidden": toAriaBoolean(closed), class: CSS.container, hidden: closed, onKeyDown: this.tipManagerKeyDownHandler, 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: {
  161. [CSS.tipContainer]: true,
  162. [CSS.tipContainerAdvancing]: !closed && direction === "advancing",
  163. [CSS.tipContainerRetreating]: !closed && direction === "retreating"
  164. }, key: selectedIndex, tabIndex: 0 }, h("slot", null)), this.renderPagination()));
  165. }
  166. get el() { return this; }
  167. static get watchers() { return {
  168. "closed": ["closedChangeHandler"],
  169. "selectedIndex": ["selectedChangeHandler"]
  170. }; }
  171. static get style() { return tipManagerCss; }
  172. }, [1, "calcite-tip-manager", {
  173. "closed": [1540],
  174. "headingLevel": [514, "heading-level"],
  175. "intlClose": [1, "intl-close"],
  176. "intlDefaultTitle": [1, "intl-default-title"],
  177. "intlNext": [1, "intl-next"],
  178. "intlPaginationLabel": [1, "intl-pagination-label"],
  179. "intlPrevious": [1, "intl-previous"],
  180. "selectedIndex": [32],
  181. "tips": [32],
  182. "total": [32],
  183. "direction": [32],
  184. "groupTitle": [32],
  185. "nextTip": [64],
  186. "previousTip": [64]
  187. }]);
  188. function defineCustomElement$1() {
  189. if (typeof customElements === "undefined") {
  190. return;
  191. }
  192. const components = ["calcite-tip-manager", "calcite-action", "calcite-icon", "calcite-loader"];
  193. components.forEach(tagName => { switch (tagName) {
  194. case "calcite-tip-manager":
  195. if (!customElements.get(tagName)) {
  196. customElements.define(tagName, TipManager);
  197. }
  198. break;
  199. case "calcite-action":
  200. if (!customElements.get(tagName)) {
  201. defineCustomElement$4();
  202. }
  203. break;
  204. case "calcite-icon":
  205. if (!customElements.get(tagName)) {
  206. defineCustomElement$3();
  207. }
  208. break;
  209. case "calcite-loader":
  210. if (!customElements.get(tagName)) {
  211. defineCustomElement$2();
  212. }
  213. break;
  214. } });
  215. }
  216. defineCustomElement$1();
  217. const CalciteTipManager = TipManager;
  218. const defineCustomElement = defineCustomElement$1;
  219. export { CalciteTipManager, defineCustomElement };