calcite-tooltip_2.cjs.entry.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. const index = require('./index-a0010f96.js');
  9. const guid = require('./guid-f4f03a7a.js');
  10. const floatingUi = require('./floating-ui-b48c8256.js');
  11. const dom = require('./dom-2ec8c9ed.js');
  12. require('./debounce-2c8b61fb.js');
  13. require('./resources-b5a5f8a7.js');
  14. const CSS = {
  15. container: "container",
  16. arrow: "arrow"
  17. };
  18. const TOOLTIP_DELAY_MS = 500;
  19. const ARIA_DESCRIBED_BY = "aria-describedby";
  20. class TooltipManager$1 {
  21. constructor() {
  22. // --------------------------------------------------------------------------
  23. //
  24. // Private Properties
  25. //
  26. // --------------------------------------------------------------------------
  27. this.registeredElements = new WeakMap();
  28. this.hoverTimeouts = new WeakMap();
  29. this.registeredElementCount = 0;
  30. // --------------------------------------------------------------------------
  31. //
  32. // Private Methods
  33. //
  34. // --------------------------------------------------------------------------
  35. this.queryTooltip = (composedPath) => {
  36. const { registeredElements } = this;
  37. const registeredElement = composedPath.find((pathEl) => registeredElements.has(pathEl));
  38. return registeredElements.get(registeredElement);
  39. };
  40. this.keyDownHandler = (event) => {
  41. if (event.key === "Escape") {
  42. const { activeTooltipEl } = this;
  43. if (activeTooltipEl) {
  44. this.clearHoverTimeout(activeTooltipEl);
  45. this.toggleTooltip(activeTooltipEl, false);
  46. }
  47. }
  48. };
  49. this.mouseEnterShow = (event) => {
  50. this.hoverEvent(event, true);
  51. };
  52. this.mouseLeaveHide = (event) => {
  53. this.hoverEvent(event, false);
  54. };
  55. this.clickHandler = (event) => {
  56. if (!dom.isPrimaryPointerButton(event)) {
  57. return;
  58. }
  59. const clickedTooltip = this.queryTooltip(event.composedPath());
  60. this.clickedTooltip = clickedTooltip;
  61. if (clickedTooltip === null || clickedTooltip === void 0 ? void 0 : clickedTooltip.closeOnClick) {
  62. this.toggleTooltip(clickedTooltip, false);
  63. this.clearHoverTimeout(clickedTooltip);
  64. }
  65. };
  66. this.focusShow = (event) => {
  67. this.focusEvent(event, true);
  68. };
  69. this.blurHide = (event) => {
  70. this.focusEvent(event, false);
  71. };
  72. this.hoverToggle = (tooltip, value) => {
  73. const { hoverTimeouts } = this;
  74. hoverTimeouts.delete(tooltip);
  75. if (value) {
  76. this.closeExistingTooltip();
  77. }
  78. this.toggleTooltip(tooltip, value);
  79. };
  80. }
  81. // --------------------------------------------------------------------------
  82. //
  83. // Public Methods
  84. //
  85. // --------------------------------------------------------------------------
  86. registerElement(referenceEl, tooltip) {
  87. this.registeredElementCount++;
  88. this.registeredElements.set(referenceEl, tooltip);
  89. if (this.registeredElementCount === 1) {
  90. this.addListeners();
  91. }
  92. }
  93. unregisterElement(referenceEl) {
  94. if (this.registeredElements.delete(referenceEl)) {
  95. this.registeredElementCount--;
  96. }
  97. if (this.registeredElementCount === 0) {
  98. this.removeListeners();
  99. }
  100. }
  101. addListeners() {
  102. document.addEventListener("keydown", this.keyDownHandler);
  103. document.addEventListener("pointerover", this.mouseEnterShow, { capture: true });
  104. document.addEventListener("pointerout", this.mouseLeaveHide, { capture: true });
  105. document.addEventListener("pointerdown", this.clickHandler, { capture: true });
  106. document.addEventListener("focusin", this.focusShow, { capture: true });
  107. document.addEventListener("focusout", this.blurHide, { capture: true });
  108. }
  109. removeListeners() {
  110. document.removeEventListener("keydown", this.keyDownHandler);
  111. document.removeEventListener("pointerover", this.mouseEnterShow, { capture: true });
  112. document.removeEventListener("pointerout", this.mouseLeaveHide, { capture: true });
  113. document.removeEventListener("pointerdown", this.clickHandler, { capture: true });
  114. document.removeEventListener("focusin", this.focusShow, { capture: true });
  115. document.removeEventListener("focusout", this.blurHide, { capture: true });
  116. }
  117. clearHoverTimeout(tooltip) {
  118. const { hoverTimeouts } = this;
  119. if (hoverTimeouts.has(tooltip)) {
  120. window.clearTimeout(hoverTimeouts.get(tooltip));
  121. hoverTimeouts.delete(tooltip);
  122. }
  123. }
  124. closeExistingTooltip() {
  125. const { activeTooltipEl } = this;
  126. if (activeTooltipEl) {
  127. this.toggleTooltip(activeTooltipEl, false);
  128. }
  129. }
  130. focusTooltip(tooltip, value) {
  131. this.closeExistingTooltip();
  132. if (value) {
  133. this.clearHoverTimeout(tooltip);
  134. }
  135. this.toggleTooltip(tooltip, value);
  136. }
  137. toggleTooltip(tooltip, value) {
  138. tooltip.open = value;
  139. if (value) {
  140. this.activeTooltipEl = tooltip;
  141. }
  142. }
  143. hoverTooltip(tooltip, value) {
  144. this.clearHoverTimeout(tooltip);
  145. const { hoverTimeouts } = this;
  146. const timeoutId = window.setTimeout(() => this.hoverToggle(tooltip, value), TOOLTIP_DELAY_MS );
  147. hoverTimeouts.set(tooltip, timeoutId);
  148. }
  149. activeTooltipHover(event) {
  150. const { activeTooltipEl, hoverTimeouts } = this;
  151. const { type } = event;
  152. if (!activeTooltipEl) {
  153. return;
  154. }
  155. if (type === "pointerover" && event.composedPath().includes(activeTooltipEl)) {
  156. this.clearHoverTimeout(activeTooltipEl);
  157. }
  158. else if (type === "pointerout" && !hoverTimeouts.has(activeTooltipEl)) {
  159. this.hoverTooltip(activeTooltipEl, false);
  160. }
  161. }
  162. hoverEvent(event, value) {
  163. const tooltip = this.queryTooltip(event.composedPath());
  164. this.activeTooltipHover(event);
  165. if (!tooltip) {
  166. return;
  167. }
  168. this.hoverTooltip(tooltip, value);
  169. }
  170. focusEvent(event, value) {
  171. const tooltip = this.queryTooltip(event.composedPath());
  172. if (!tooltip || tooltip === this.clickedTooltip) {
  173. this.clickedTooltip = null;
  174. return;
  175. }
  176. this.focusTooltip(tooltip, value);
  177. }
  178. }
  179. const tooltipCss = "@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{display:block;position:absolute;z-index:999}.calcite-floating-ui-anim{position:relative;transition:var(--calcite-floating-ui-transition);visibility:hidden;transition-property:transform, visibility, opacity;opacity:0;box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);z-index:1;border-radius:0.25rem}:host([data-placement^=bottom]) .calcite-floating-ui-anim{transform:translateY(-5px)}:host([data-placement^=top]) .calcite-floating-ui-anim{transform:translateY(5px)}:host([data-placement^=left]) .calcite-floating-ui-anim{transform:translateX(5px)}:host([data-placement^=right]) .calcite-floating-ui-anim{transform:translateX(-5px)}:host([data-placement]) .calcite-floating-ui-anim--active{opacity:1;visibility:visible;transform:translate(0)}:host([calcite-hydrated-hidden]){visibility:hidden !important;pointer-events:none}.arrow,.arrow::before{position:absolute;inline-size:8px;block-size:8px;z-index:-1}.arrow::before{content:\"\";--tw-shadow:0 4px 8px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -1px rgba(0, 0, 0, 0.04);--tw-shadow-colored:0 4px 8px -1px var(--tw-shadow-color), 0 2px 4px -1px var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);transform:rotate(45deg);background:var(--calcite-ui-foreground-1)}:host([data-placement^=top]) .arrow{inset-block-end:-4px}:host([data-placement^=bottom]) .arrow{inset-block-start:-4px}:host([data-placement^=left]) .arrow{direction:ltr;inset-inline-end:-4px}:host([data-placement^=right]) .arrow{direction:ltr;inset-inline-start:-4px}.container{position:relative;overflow:hidden;border-radius:0.25rem;background-color:var(--calcite-ui-foreground-1);padding-block:0.75rem;padding-inline:1rem;font-size:var(--calcite-font-size--2);line-height:1.375;font-weight:var(--calcite-font-weight-medium);color:var(--calcite-ui-text-1);max-inline-size:20rem;max-block-size:20rem;text-align:start}.calcite-floating-ui-anim{border-radius:0.25rem;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-3);background-color:var(--calcite-ui-foreground-1)}.arrow::before{outline:1px solid var(--calcite-ui-border-3)}";
  180. const manager = new TooltipManager$1();
  181. const Tooltip = class {
  182. constructor(hostRef) {
  183. index.registerInstance(this, hostRef);
  184. // --------------------------------------------------------------------------
  185. //
  186. // Properties
  187. //
  188. // --------------------------------------------------------------------------
  189. /** Closes the component when the `referenceElement` is clicked. */
  190. this.closeOnClick = false;
  191. /**
  192. * Offset the position of the component away from the `referenceElement`.
  193. *
  194. * @default 6
  195. */
  196. this.offsetDistance = floatingUi.defaultOffsetDistance;
  197. /**
  198. * Offset the position of the component along the `referenceElement`.
  199. */
  200. this.offsetSkidding = 0;
  201. /**
  202. * When `true`, the component is open.
  203. */
  204. this.open = false;
  205. /**
  206. * Determines the type of positioning to use for the overlaid content.
  207. *
  208. * Using `"absolute"` will work for most cases. The component will be positioned inside of overflowing parent containers and will affect the container's layout.
  209. *
  210. * The `"fixed"` value should be used to escape an overflowing parent container, or when the reference element's `position` CSS property is `"fixed"`.
  211. *
  212. */
  213. this.overlayPositioning = "absolute";
  214. /**
  215. * Determines where the component will be positioned relative to the `referenceElement`.
  216. *
  217. * @see [LogicalPlacement](https://github.com/Esri/calcite-components/blob/master/src/utils/floating-ui.ts#L25)
  218. */
  219. this.placement = "auto";
  220. this.guid = `calcite-tooltip-${guid.guid()}`;
  221. this.hasLoaded = false;
  222. // --------------------------------------------------------------------------
  223. //
  224. // Private Methods
  225. //
  226. // --------------------------------------------------------------------------
  227. this.setUpReferenceElement = (warn = true) => {
  228. this.removeReferences();
  229. this.effectiveReferenceElement = this.getReferenceElement();
  230. floatingUi.connectFloatingUI(this, this.effectiveReferenceElement, this.el);
  231. const { el, referenceElement, effectiveReferenceElement } = this;
  232. if (warn && referenceElement && !effectiveReferenceElement) {
  233. console.warn(`${el.tagName}: reference-element id "${referenceElement}" was not found.`, {
  234. el
  235. });
  236. }
  237. this.addReferences();
  238. };
  239. this.getId = () => {
  240. return this.el.id || this.guid;
  241. };
  242. this.addReferences = () => {
  243. const { effectiveReferenceElement } = this;
  244. if (!effectiveReferenceElement) {
  245. return;
  246. }
  247. const id = this.getId();
  248. if ("setAttribute" in effectiveReferenceElement) {
  249. effectiveReferenceElement.setAttribute(ARIA_DESCRIBED_BY, id);
  250. }
  251. manager.registerElement(effectiveReferenceElement, this.el);
  252. };
  253. this.removeReferences = () => {
  254. const { effectiveReferenceElement } = this;
  255. if (!effectiveReferenceElement) {
  256. return;
  257. }
  258. if ("removeAttribute" in effectiveReferenceElement) {
  259. effectiveReferenceElement.removeAttribute(ARIA_DESCRIBED_BY);
  260. }
  261. manager.unregisterElement(effectiveReferenceElement);
  262. };
  263. }
  264. offsetDistanceOffsetHandler() {
  265. this.reposition(true);
  266. }
  267. offsetSkiddingHandler() {
  268. this.reposition(true);
  269. }
  270. openHandler(value) {
  271. if (value) {
  272. this.reposition(true);
  273. }
  274. else {
  275. floatingUi.updateAfterClose(this.el);
  276. }
  277. }
  278. overlayPositioningHandler() {
  279. this.reposition(true);
  280. }
  281. placementHandler() {
  282. this.reposition(true);
  283. }
  284. referenceElementHandler() {
  285. this.setUpReferenceElement();
  286. }
  287. // --------------------------------------------------------------------------
  288. //
  289. // Lifecycle
  290. //
  291. // --------------------------------------------------------------------------
  292. connectedCallback() {
  293. this.setUpReferenceElement(this.hasLoaded);
  294. }
  295. componentDidLoad() {
  296. if (this.referenceElement && !this.effectiveReferenceElement) {
  297. this.setUpReferenceElement();
  298. }
  299. this.reposition(true);
  300. this.hasLoaded = true;
  301. }
  302. disconnectedCallback() {
  303. this.removeReferences();
  304. floatingUi.disconnectFloatingUI(this, this.effectiveReferenceElement, this.el);
  305. }
  306. // --------------------------------------------------------------------------
  307. //
  308. // Public Methods
  309. //
  310. // --------------------------------------------------------------------------
  311. /**
  312. * Updates the position of the component.
  313. *
  314. * @param delayed
  315. */
  316. async reposition(delayed = false) {
  317. const { el, effectiveReferenceElement, placement, overlayPositioning, offsetDistance, offsetSkidding, arrowEl } = this;
  318. return floatingUi.reposition(this, {
  319. floatingEl: el,
  320. referenceEl: effectiveReferenceElement,
  321. overlayPositioning,
  322. placement,
  323. offsetDistance,
  324. offsetSkidding,
  325. includeArrow: true,
  326. arrowEl,
  327. type: "tooltip"
  328. }, delayed);
  329. }
  330. getReferenceElement() {
  331. const { referenceElement, el } = this;
  332. return ((typeof referenceElement === "string"
  333. ? dom.queryElementRoots(el, { id: referenceElement })
  334. : referenceElement) || null);
  335. }
  336. // --------------------------------------------------------------------------
  337. //
  338. // Render Methods
  339. //
  340. // --------------------------------------------------------------------------
  341. render() {
  342. const { effectiveReferenceElement, label, open } = this;
  343. const displayed = effectiveReferenceElement && open;
  344. const hidden = !displayed;
  345. return (index.h(index.Host, { "aria-hidden": dom.toAriaBoolean(hidden), "aria-label": label, "aria-live": "polite", "calcite-hydrated-hidden": hidden, id: this.getId(), role: "tooltip" }, index.h("div", { class: {
  346. [floatingUi.FloatingCSS.animation]: true,
  347. [floatingUi.FloatingCSS.animationActive]: displayed
  348. } }, index.h("div", { class: CSS.arrow, ref: (arrowEl) => (this.arrowEl = arrowEl) }), index.h("div", { class: CSS.container }, index.h("slot", null)))));
  349. }
  350. get el() { return index.getElement(this); }
  351. static get watchers() { return {
  352. "offsetDistance": ["offsetDistanceOffsetHandler"],
  353. "offsetSkidding": ["offsetSkiddingHandler"],
  354. "open": ["openHandler"],
  355. "overlayPositioning": ["overlayPositioningHandler"],
  356. "placement": ["placementHandler"],
  357. "referenceElement": ["referenceElementHandler"]
  358. }; }
  359. };
  360. Tooltip.style = tooltipCss;
  361. const tooltipManagerCss = "@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{position:relative;display:block}";
  362. const TooltipManager = class {
  363. constructor(hostRef) {
  364. index.registerInstance(this, hostRef);
  365. // --------------------------------------------------------------------------
  366. //
  367. // Properties
  368. //
  369. // --------------------------------------------------------------------------
  370. /**
  371. * CSS Selector to match reference elements for tooltips. Reference elements will be identified by this selector in order to open their associated tooltip.
  372. *
  373. * @default `[data-calcite-tooltip-reference]`
  374. */
  375. this.selector = "[data-calcite-tooltip-reference]";
  376. }
  377. // --------------------------------------------------------------------------
  378. //
  379. // Render Methods
  380. //
  381. // --------------------------------------------------------------------------
  382. render() {
  383. return index.h("slot", null);
  384. }
  385. };
  386. TooltipManager.style = tooltipManagerCss;
  387. exports.calcite_tooltip = Tooltip;
  388. exports.calcite_tooltip_manager = TooltipManager;