tooltip.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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, Host } from '@stencil/core/internal/client/index.js';
  7. import { g as guid } from './guid.js';
  8. import { b as defaultOffsetDistance, c as connectFloatingUI, u as updateAfterClose, a as disconnectFloatingUI, r as reposition, F as FloatingCSS } from './floating-ui.js';
  9. import { i as isPrimaryPointerButton, q as queryElementRoots, t as toAriaBoolean } from './dom.js';
  10. const CSS = {
  11. container: "container",
  12. arrow: "arrow"
  13. };
  14. const TOOLTIP_DELAY_MS = 500;
  15. const ARIA_DESCRIBED_BY = "aria-describedby";
  16. class TooltipManager {
  17. constructor() {
  18. // --------------------------------------------------------------------------
  19. //
  20. // Private Properties
  21. //
  22. // --------------------------------------------------------------------------
  23. this.registeredElements = new WeakMap();
  24. this.hoverTimeouts = new WeakMap();
  25. this.registeredElementCount = 0;
  26. // --------------------------------------------------------------------------
  27. //
  28. // Private Methods
  29. //
  30. // --------------------------------------------------------------------------
  31. this.queryTooltip = (composedPath) => {
  32. const { registeredElements } = this;
  33. const registeredElement = composedPath.find((pathEl) => registeredElements.has(pathEl));
  34. return registeredElements.get(registeredElement);
  35. };
  36. this.keyDownHandler = (event) => {
  37. if (event.key === "Escape") {
  38. const { activeTooltipEl } = this;
  39. if (activeTooltipEl) {
  40. this.clearHoverTimeout(activeTooltipEl);
  41. this.toggleTooltip(activeTooltipEl, false);
  42. }
  43. }
  44. };
  45. this.mouseEnterShow = (event) => {
  46. this.hoverEvent(event, true);
  47. };
  48. this.mouseLeaveHide = (event) => {
  49. this.hoverEvent(event, false);
  50. };
  51. this.clickHandler = (event) => {
  52. if (!isPrimaryPointerButton(event)) {
  53. return;
  54. }
  55. const clickedTooltip = this.queryTooltip(event.composedPath());
  56. this.clickedTooltip = clickedTooltip;
  57. if (clickedTooltip === null || clickedTooltip === void 0 ? void 0 : clickedTooltip.closeOnClick) {
  58. this.toggleTooltip(clickedTooltip, false);
  59. this.clearHoverTimeout(clickedTooltip);
  60. }
  61. };
  62. this.focusShow = (event) => {
  63. this.focusEvent(event, true);
  64. };
  65. this.blurHide = (event) => {
  66. this.focusEvent(event, false);
  67. };
  68. this.hoverToggle = (tooltip, value) => {
  69. const { hoverTimeouts } = this;
  70. hoverTimeouts.delete(tooltip);
  71. if (value) {
  72. this.closeExistingTooltip();
  73. }
  74. this.toggleTooltip(tooltip, value);
  75. };
  76. }
  77. // --------------------------------------------------------------------------
  78. //
  79. // Public Methods
  80. //
  81. // --------------------------------------------------------------------------
  82. registerElement(referenceEl, tooltip) {
  83. this.registeredElementCount++;
  84. this.registeredElements.set(referenceEl, tooltip);
  85. if (this.registeredElementCount === 1) {
  86. this.addListeners();
  87. }
  88. }
  89. unregisterElement(referenceEl) {
  90. if (this.registeredElements.delete(referenceEl)) {
  91. this.registeredElementCount--;
  92. }
  93. if (this.registeredElementCount === 0) {
  94. this.removeListeners();
  95. }
  96. }
  97. addListeners() {
  98. document.addEventListener("keydown", this.keyDownHandler);
  99. document.addEventListener("pointerover", this.mouseEnterShow, { capture: true });
  100. document.addEventListener("pointerout", this.mouseLeaveHide, { capture: true });
  101. document.addEventListener("pointerdown", this.clickHandler, { capture: true });
  102. document.addEventListener("focusin", this.focusShow, { capture: true });
  103. document.addEventListener("focusout", this.blurHide, { capture: true });
  104. }
  105. removeListeners() {
  106. document.removeEventListener("keydown", this.keyDownHandler);
  107. document.removeEventListener("pointerover", this.mouseEnterShow, { capture: true });
  108. document.removeEventListener("pointerout", this.mouseLeaveHide, { capture: true });
  109. document.removeEventListener("pointerdown", this.clickHandler, { capture: true });
  110. document.removeEventListener("focusin", this.focusShow, { capture: true });
  111. document.removeEventListener("focusout", this.blurHide, { capture: true });
  112. }
  113. clearHoverTimeout(tooltip) {
  114. const { hoverTimeouts } = this;
  115. if (hoverTimeouts.has(tooltip)) {
  116. window.clearTimeout(hoverTimeouts.get(tooltip));
  117. hoverTimeouts.delete(tooltip);
  118. }
  119. }
  120. closeExistingTooltip() {
  121. const { activeTooltipEl } = this;
  122. if (activeTooltipEl) {
  123. this.toggleTooltip(activeTooltipEl, false);
  124. }
  125. }
  126. focusTooltip(tooltip, value) {
  127. this.closeExistingTooltip();
  128. if (value) {
  129. this.clearHoverTimeout(tooltip);
  130. }
  131. this.toggleTooltip(tooltip, value);
  132. }
  133. toggleTooltip(tooltip, value) {
  134. tooltip.open = value;
  135. if (value) {
  136. this.activeTooltipEl = tooltip;
  137. }
  138. }
  139. hoverTooltip(tooltip, value) {
  140. this.clearHoverTimeout(tooltip);
  141. const { hoverTimeouts } = this;
  142. const timeoutId = window.setTimeout(() => this.hoverToggle(tooltip, value), TOOLTIP_DELAY_MS );
  143. hoverTimeouts.set(tooltip, timeoutId);
  144. }
  145. activeTooltipHover(event) {
  146. const { activeTooltipEl, hoverTimeouts } = this;
  147. const { type } = event;
  148. if (!activeTooltipEl) {
  149. return;
  150. }
  151. if (type === "pointerover" && event.composedPath().includes(activeTooltipEl)) {
  152. this.clearHoverTimeout(activeTooltipEl);
  153. }
  154. else if (type === "pointerout" && !hoverTimeouts.has(activeTooltipEl)) {
  155. this.hoverTooltip(activeTooltipEl, false);
  156. }
  157. }
  158. hoverEvent(event, value) {
  159. const tooltip = this.queryTooltip(event.composedPath());
  160. this.activeTooltipHover(event);
  161. if (!tooltip) {
  162. return;
  163. }
  164. this.hoverTooltip(tooltip, value);
  165. }
  166. focusEvent(event, value) {
  167. const tooltip = this.queryTooltip(event.composedPath());
  168. if (!tooltip || tooltip === this.clickedTooltip) {
  169. this.clickedTooltip = null;
  170. return;
  171. }
  172. this.focusTooltip(tooltip, value);
  173. }
  174. }
  175. 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)}";
  176. const manager = new TooltipManager();
  177. const Tooltip = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  178. constructor() {
  179. super();
  180. this.__registerHost();
  181. this.__attachShadow();
  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 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. static get style() { return tooltipCss; }
  358. }, [1, "calcite-tooltip", {
  359. "closeOnClick": [516, "close-on-click"],
  360. "label": [1],
  361. "offsetDistance": [514, "offset-distance"],
  362. "offsetSkidding": [514, "offset-skidding"],
  363. "open": [516],
  364. "overlayPositioning": [513, "overlay-positioning"],
  365. "placement": [513],
  366. "referenceElement": [1, "reference-element"],
  367. "effectiveReferenceElement": [32],
  368. "reposition": [64]
  369. }]);
  370. function defineCustomElement() {
  371. if (typeof customElements === "undefined") {
  372. return;
  373. }
  374. const components = ["calcite-tooltip"];
  375. components.forEach(tagName => { switch (tagName) {
  376. case "calcite-tooltip":
  377. if (!customElements.get(tagName)) {
  378. customElements.define(tagName, Tooltip);
  379. }
  380. break;
  381. } });
  382. }
  383. defineCustomElement();
  384. export { Tooltip as T, defineCustomElement as d };