calcite-tooltip_2.entry.js 18 KB

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