@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
357 lines (350 loc) • 19 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 { r as registerInstance, h, H as Host, g as getElement } from './index-bd94948c.js';
import { g as guid } from './guid-196bd5ea.js';
import { a as defaultOffsetDistance, u as updatePopper, c as createPopper, C as CSS$1 } from './popper-da45ec93.js';
import { q as queryElementRoots } from './dom-e7fa77b9.js';
const CSS = {
container: "container",
arrow: "arrow"
};
const TOOLTIP_DELAY_MS = 500;
const TOOLTIP_REFERENCE = "data-calcite-tooltip-reference";
const ARIA_DESCRIBED_BY = "aria-describedby";
const tooltipCss = "@-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{display:block;position:absolute;z-index:999;-webkit-transform:scale(0);transform:scale(0)}.calcite-popper-anim{position:relative;z-index:1;-webkit-transition:var(--calcite-popper-transition);transition:var(--calcite-popper-transition);visibility:hidden;-webkit-transition-property:visibility, opacity, -webkit-transform;transition-property:visibility, opacity, -webkit-transform;transition-property:transform, visibility, opacity;transition-property:transform, visibility, opacity, -webkit-transform;opacity:0;-webkit-box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);border-radius:0.25rem}:host([data-popper-placement^=bottom]) .calcite-popper-anim{-webkit-transform:translateY(-5px);transform:translateY(-5px)}:host([data-popper-placement^=top]) .calcite-popper-anim{-webkit-transform:translateY(5px);transform:translateY(5px)}:host([data-popper-placement^=left]) .calcite-popper-anim{-webkit-transform:translateX(5px);transform:translateX(5px)}:host([data-popper-placement^=right]) .calcite-popper-anim{-webkit-transform:translateX(-5px);transform:translateX(-5px)}:host([data-popper-placement]) .calcite-popper-anim--active{opacity:1;visibility:visible;-webkit-transform:translate(0);transform:translate(0)}:host([data-popper-placement][data-popper-escaped]),:host([data-popper-placement][data-popper-reference-hidden]){pointer-events:none;opacity:0}.arrow,.arrow::before{position:absolute;width:8px;height: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);-webkit-box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);-webkit-transform:rotate(45deg);transform:rotate(45deg);background:var(--calcite-ui-foreground-1)}:host([data-popper-placement^=top]) .arrow{bottom:-4px}:host([data-popper-placement^=bottom]) .arrow{top:-4px}:host([data-popper-placement^=left]) .arrow{right:-4px}:host([data-popper-placement^=right]) .arrow{left:-4px}.container{position:relative;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;-ms-flex-pack:start;justify-content:flex-start;border-radius:0.25rem;background-color:var(--calcite-ui-foreground-1);padding-top:0.75rem;padding-bottom:0.75rem;padding-left:1rem;padding-right: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);overflow:hidden;max-width:20rem;max-height:20rem}.calcite-popper-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)}";
const Tooltip = class {
constructor(hostRef) {
registerInstance(this, hostRef);
/**
* Offset the position of the tooltip away from the reference element.
* @default 6
*/
this.offsetDistance = defaultOffsetDistance;
/**
* Offset the position of the tooltip along the reference element.
*/
this.offsetSkidding = 0;
/**
* Display and position the component.
*/
this.open = false;
/** Describes the type of positioning to use for the overlaid content. If your element is in a fixed container, use the 'fixed' value. */
this.overlayPositioning = "absolute";
/**
* Determines where the component will be positioned relative to the referenceElement.
* @see [PopperPlacement](https://github.com/Esri/calcite-components/blob/master/src/utils/popper.ts#L25)
*/
this.placement = "auto";
this.guid = `calcite-tooltip-${guid()}`;
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
this.setUpReferenceElement = () => {
this.removeReferences();
this.effectiveReferenceElement = this.getReferenceElement();
const { el, referenceElement, effectiveReferenceElement } = this;
if (referenceElement && !effectiveReferenceElement) {
console.warn(`${el.tagName}: reference-element id "${referenceElement}" was not found.`, {
el
});
}
this.addReferences();
this.createPopper();
};
this.getId = () => {
return this.el.id || this.guid;
};
this.addReferences = () => {
const { effectiveReferenceElement } = this;
if (!effectiveReferenceElement) {
return;
}
const id = this.getId();
effectiveReferenceElement.setAttribute(TOOLTIP_REFERENCE, id);
effectiveReferenceElement.setAttribute(ARIA_DESCRIBED_BY, id);
};
this.removeReferences = () => {
const { effectiveReferenceElement } = this;
if (!effectiveReferenceElement) {
return;
}
effectiveReferenceElement.removeAttribute(TOOLTIP_REFERENCE);
effectiveReferenceElement.removeAttribute(ARIA_DESCRIBED_BY);
};
this.show = () => {
this.open = true;
};
this.hide = () => {
this.open = false;
};
}
offsetDistanceOffsetHandler() {
this.reposition();
}
offsetSkiddingHandler() {
this.reposition();
}
openHandler() {
this.reposition();
}
placementHandler() {
this.reposition();
}
referenceElementHandler() {
this.setUpReferenceElement();
}
// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------
componentWillLoad() {
this.setUpReferenceElement();
}
componentDidLoad() {
this.reposition();
}
disconnectedCallback() {
this.removeReferences();
this.destroyPopper();
}
// --------------------------------------------------------------------------
//
// Public Methods
//
// --------------------------------------------------------------------------
/** Updates the position of the component. */
async reposition() {
const { popper, el, placement } = this;
const modifiers = this.getModifiers();
popper
? await updatePopper({
el,
modifiers,
placement,
popper
})
: this.createPopper();
}
getReferenceElement() {
const { referenceElement, el } = this;
return ((typeof referenceElement === "string"
? queryElementRoots(el, { id: referenceElement })
: referenceElement) || null);
}
getModifiers() {
const { arrowEl, offsetDistance, offsetSkidding } = this;
const arrowModifier = {
name: "arrow",
enabled: true,
options: {
element: arrowEl
}
};
const offsetModifier = {
name: "offset",
enabled: true,
options: {
offset: [offsetSkidding, offsetDistance]
}
};
const eventListenerModifier = {
name: "eventListeners",
enabled: this.open
};
return [arrowModifier, offsetModifier, eventListenerModifier];
}
createPopper() {
this.destroyPopper();
const { el, placement, effectiveReferenceElement: referenceEl, overlayPositioning } = this;
const modifiers = this.getModifiers();
this.popper = createPopper({
el,
modifiers,
placement,
overlayPositioning,
referenceEl
});
}
destroyPopper() {
const { popper } = this;
if (popper) {
popper.destroy();
}
this.popper = null;
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
render() {
const { effectiveReferenceElement, label, open } = this;
const displayed = effectiveReferenceElement && open;
const hidden = !displayed;
return (h(Host, { "aria-hidden": hidden.toString(), "aria-label": label, "calcite-hydrated-hidden": hidden, id: this.getId(), role: "tooltip" }, h("div", { class: {
[CSS$1.animation]: true,
[CSS$1.animationActive]: displayed
} }, h("div", { class: CSS.arrow, ref: (arrowEl) => (this.arrowEl = arrowEl) }), h("div", { class: CSS.container }, h("slot", null)))));
}
get el() { return getElement(this); }
static get watchers() { return {
"offsetDistance": ["offsetDistanceOffsetHandler"],
"offsetSkidding": ["offsetSkiddingHandler"],
"open": ["openHandler"],
"placement": ["placementHandler"],
"referenceElement": ["referenceElementHandler"]
}; }
};
Tooltip.style = tooltipCss;
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 = class {
constructor(hostRef) {
registerInstance(this, hostRef);
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 getElement(this); }
};
TooltipManager.style = tooltipManagerCss;
export { Tooltip as calcite_tooltip, TooltipManager as calcite_tooltip_manager };