@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
176 lines (172 loc) • 8.29 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
*/
import { proxyCustomElement, HTMLElement, h } from '@stencil/core/internal/client';
import { T as TOOLTIP_REFERENCE, a as TOOLTIP_DELAY_MS } from './resources2.js';
import { q as queryElementRoots } from './dom.js';
const tooltipManagerCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 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;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{position:relative;display:block}";
const TooltipManager = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.hoverTimeouts = new WeakMap();
// --------------------------------------------------------------------------
//
// Properties
//
// --------------------------------------------------------------------------
/**
* CSS Selector to match reference elements for tooltips. Reference elements will be identified by this selector in order to open their associated tooltip.
* @default `[data-calcite-tooltip-reference]`
*/
this.selector = `[${TOOLTIP_REFERENCE}]`;
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
this.queryTooltip = (composedPath) => {
const { el } = this;
if (!composedPath.includes(el)) {
return null;
}
const referenceElement = composedPath.find((pathEl) => { var _a; return (_a = pathEl === null || pathEl === void 0 ? void 0 : pathEl.hasAttribute) === null || _a === void 0 ? void 0 : _a.call(pathEl, TOOLTIP_REFERENCE); });
if (!referenceElement) {
return null;
}
const id = referenceElement.getAttribute(TOOLTIP_REFERENCE);
return queryElementRoots(el, { id });
};
this.clearHoverTimeout = (tooltip) => {
const { hoverTimeouts } = this;
if (hoverTimeouts.has(tooltip)) {
window.clearTimeout(hoverTimeouts.get(tooltip));
hoverTimeouts.delete(tooltip);
}
};
this.closeExistingTooltip = () => {
const { tooltipEl } = this;
if (tooltipEl) {
this.toggleTooltip(tooltipEl, false);
}
};
this.focusTooltip = ({ tooltip, value }) => {
this.closeExistingTooltip();
if (value) {
this.clearHoverTimeout(tooltip);
}
this.toggleTooltip(tooltip, value);
};
this.toggleTooltip = (tooltip, value) => {
tooltip.open = value;
if (value) {
this.tooltipEl = tooltip;
}
};
this.hoverToggle = ({ tooltip, value }) => {
const { hoverTimeouts } = this;
hoverTimeouts.delete(tooltip);
if (value) {
this.closeExistingTooltip();
}
this.toggleTooltip(tooltip, value);
};
this.hoverTooltip = ({ tooltip, value }) => {
this.clearHoverTimeout(tooltip);
const { hoverTimeouts } = this;
const timeoutId = window.setTimeout(() => this.hoverToggle({ tooltip, value }), TOOLTIP_DELAY_MS );
hoverTimeouts.set(tooltip, timeoutId);
};
this.activeTooltipHover = (event) => {
const { tooltipEl, hoverTimeouts } = this;
const { type } = event;
if (!tooltipEl) {
return;
}
if (type === "mouseover" && event.composedPath().includes(tooltipEl)) {
this.clearHoverTimeout(tooltipEl);
}
else if (type === "mouseout" && !hoverTimeouts.has(tooltipEl)) {
this.hoverTooltip({ tooltip: tooltipEl, value: false });
}
};
this.hoverEvent = (event, value) => {
const tooltip = this.queryTooltip(event.composedPath());
this.activeTooltipHover(event);
if (!tooltip) {
return;
}
this.hoverTooltip({ tooltip, value });
};
this.focusEvent = (event, value) => {
const tooltip = this.queryTooltip(event.composedPath());
if (!tooltip || tooltip === this.clickedTooltip) {
this.clickedTooltip = null;
return;
}
this.focusTooltip({ tooltip, value });
};
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
render() {
return h("slot", null);
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
keyUpHandler(event) {
if (event.key === "Escape") {
const { tooltipEl } = this;
if (tooltipEl) {
this.clearHoverTimeout(tooltipEl);
this.toggleTooltip(tooltipEl, false);
}
}
}
mouseEnterShow(event) {
this.hoverEvent(event, true);
}
mouseLeaveHide(event) {
this.hoverEvent(event, false);
}
clickHandler(event) {
const clickedTooltip = this.queryTooltip(event.composedPath());
this.clickedTooltip = clickedTooltip;
if (clickedTooltip) {
this.toggleTooltip(clickedTooltip, false);
}
}
focusShow(event) {
this.focusEvent(event, true);
}
blurHide(event) {
this.focusEvent(event, false);
}
get el() { return this; }
static get style() { return tooltipManagerCss; }
}, [1, "calcite-tooltip-manager", {
"selector": [1]
}, [[4, "keyup", "keyUpHandler"], [3, "mouseover", "mouseEnterShow"], [3, "mouseout", "mouseLeaveHide"], [2, "click", "clickHandler"], [2, "focus", "focusShow"], [2, "blur", "blurHide"]]]);
function defineCustomElement() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-tooltip-manager"];
components.forEach(tagName => { switch (tagName) {
case "calcite-tooltip-manager":
if (!customElements.get(tagName)) {
customElements.define(tagName, TooltipManager);
}
break;
} });
}
defineCustomElement();
export { TooltipManager as T, defineCustomElement as d };