261 lines
8.1 KiB
JavaScript
261 lines
8.1 KiB
JavaScript
'use client'
|
|
"use strict";
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
|
|
// src/use-tooltip.ts
|
|
var use_tooltip_exports = {};
|
|
__export(use_tooltip_exports, {
|
|
useTooltip: () => useTooltip
|
|
});
|
|
module.exports = __toCommonJS(use_tooltip_exports);
|
|
var import_dom_utils = require("@chakra-ui/dom-utils");
|
|
var import_popper = require("@chakra-ui/popper");
|
|
var import_react_use_disclosure = require("@chakra-ui/react-use-disclosure");
|
|
var import_react_use_event_listener = require("@chakra-ui/react-use-event-listener");
|
|
var import_react_use_merge_refs = require("@chakra-ui/react-use-merge-refs");
|
|
var import_shared_utils = require("@chakra-ui/shared-utils");
|
|
var import_react = require("react");
|
|
var getDoc = (ref) => {
|
|
var _a;
|
|
return ((_a = ref.current) == null ? void 0 : _a.ownerDocument) || document;
|
|
};
|
|
var getWin = (ref) => {
|
|
var _a, _b;
|
|
return ((_b = (_a = ref.current) == null ? void 0 : _a.ownerDocument) == null ? void 0 : _b.defaultView) || window;
|
|
};
|
|
function useTooltip(props = {}) {
|
|
const {
|
|
openDelay = 0,
|
|
closeDelay = 0,
|
|
closeOnClick = true,
|
|
closeOnMouseDown,
|
|
closeOnScroll,
|
|
closeOnPointerDown = closeOnMouseDown,
|
|
closeOnEsc = true,
|
|
onOpen: onOpenProp,
|
|
onClose: onCloseProp,
|
|
placement,
|
|
id,
|
|
isOpen: isOpenProp,
|
|
defaultIsOpen,
|
|
arrowSize = 10,
|
|
arrowShadowColor,
|
|
arrowPadding,
|
|
modifiers,
|
|
isDisabled,
|
|
gutter,
|
|
offset,
|
|
direction,
|
|
...htmlProps
|
|
} = props;
|
|
const { isOpen, onOpen, onClose } = (0, import_react_use_disclosure.useDisclosure)({
|
|
isOpen: isOpenProp,
|
|
defaultIsOpen,
|
|
onOpen: onOpenProp,
|
|
onClose: onCloseProp
|
|
});
|
|
const { referenceRef, getPopperProps, getArrowInnerProps, getArrowProps } = (0, import_popper.usePopper)({
|
|
enabled: isOpen,
|
|
placement,
|
|
arrowPadding,
|
|
modifiers,
|
|
gutter,
|
|
offset,
|
|
direction
|
|
});
|
|
const uuid = (0, import_react.useId)();
|
|
const uid = id != null ? id : uuid;
|
|
const tooltipId = `tooltip-${uid}`;
|
|
const ref = (0, import_react.useRef)(null);
|
|
const enterTimeout = (0, import_react.useRef)();
|
|
const clearEnterTimeout = (0, import_react.useCallback)(() => {
|
|
if (enterTimeout.current) {
|
|
clearTimeout(enterTimeout.current);
|
|
enterTimeout.current = void 0;
|
|
}
|
|
}, []);
|
|
const exitTimeout = (0, import_react.useRef)();
|
|
const clearExitTimeout = (0, import_react.useCallback)(() => {
|
|
if (exitTimeout.current) {
|
|
clearTimeout(exitTimeout.current);
|
|
exitTimeout.current = void 0;
|
|
}
|
|
}, []);
|
|
const closeNow = (0, import_react.useCallback)(() => {
|
|
clearExitTimeout();
|
|
onClose();
|
|
}, [onClose, clearExitTimeout]);
|
|
const dispatchCloseEvent = useCloseEvent(ref, closeNow);
|
|
const openWithDelay = (0, import_react.useCallback)(() => {
|
|
if (!isDisabled && !enterTimeout.current) {
|
|
if (isOpen)
|
|
dispatchCloseEvent();
|
|
const win = getWin(ref);
|
|
enterTimeout.current = win.setTimeout(onOpen, openDelay);
|
|
}
|
|
}, [dispatchCloseEvent, isDisabled, isOpen, onOpen, openDelay]);
|
|
const closeWithDelay = (0, import_react.useCallback)(() => {
|
|
clearEnterTimeout();
|
|
const win = getWin(ref);
|
|
exitTimeout.current = win.setTimeout(closeNow, closeDelay);
|
|
}, [closeDelay, closeNow, clearEnterTimeout]);
|
|
const onClick = (0, import_react.useCallback)(() => {
|
|
if (isOpen && closeOnClick) {
|
|
closeWithDelay();
|
|
}
|
|
}, [closeOnClick, closeWithDelay, isOpen]);
|
|
const onPointerDown = (0, import_react.useCallback)(() => {
|
|
if (isOpen && closeOnPointerDown) {
|
|
closeWithDelay();
|
|
}
|
|
}, [closeOnPointerDown, closeWithDelay, isOpen]);
|
|
const onKeyDown = (0, import_react.useCallback)(
|
|
(event) => {
|
|
if (isOpen && event.key === "Escape") {
|
|
closeWithDelay();
|
|
}
|
|
},
|
|
[isOpen, closeWithDelay]
|
|
);
|
|
(0, import_react_use_event_listener.useEventListener)(
|
|
() => getDoc(ref),
|
|
"keydown",
|
|
closeOnEsc ? onKeyDown : void 0
|
|
);
|
|
(0, import_react_use_event_listener.useEventListener)(
|
|
() => {
|
|
if (!closeOnScroll)
|
|
return null;
|
|
const node = ref.current;
|
|
if (!node)
|
|
return null;
|
|
const scrollParent = (0, import_dom_utils.getScrollParent)(node);
|
|
return scrollParent.localName === "body" ? getWin(ref) : scrollParent;
|
|
},
|
|
"scroll",
|
|
() => {
|
|
if (isOpen && closeOnScroll) {
|
|
closeNow();
|
|
}
|
|
},
|
|
{ passive: true, capture: true }
|
|
);
|
|
(0, import_react.useEffect)(() => {
|
|
if (!isDisabled)
|
|
return;
|
|
clearEnterTimeout();
|
|
if (isOpen)
|
|
onClose();
|
|
}, [isDisabled, isOpen, onClose, clearEnterTimeout]);
|
|
(0, import_react.useEffect)(() => {
|
|
return () => {
|
|
clearEnterTimeout();
|
|
clearExitTimeout();
|
|
};
|
|
}, [clearEnterTimeout, clearExitTimeout]);
|
|
(0, import_react_use_event_listener.useEventListener)(() => ref.current, "pointerleave", closeWithDelay);
|
|
const getTriggerProps = (0, import_react.useCallback)(
|
|
(props2 = {}, _ref = null) => {
|
|
const triggerProps = {
|
|
...props2,
|
|
ref: (0, import_react_use_merge_refs.mergeRefs)(ref, _ref, referenceRef),
|
|
onPointerEnter: (0, import_shared_utils.callAllHandlers)(props2.onPointerEnter, (e) => {
|
|
if (e.pointerType === "touch")
|
|
return;
|
|
openWithDelay();
|
|
}),
|
|
onClick: (0, import_shared_utils.callAllHandlers)(props2.onClick, onClick),
|
|
onPointerDown: (0, import_shared_utils.callAllHandlers)(props2.onPointerDown, onPointerDown),
|
|
onFocus: (0, import_shared_utils.callAllHandlers)(props2.onFocus, openWithDelay),
|
|
onBlur: (0, import_shared_utils.callAllHandlers)(props2.onBlur, closeWithDelay),
|
|
"aria-describedby": isOpen ? tooltipId : void 0
|
|
};
|
|
return triggerProps;
|
|
},
|
|
[
|
|
openWithDelay,
|
|
closeWithDelay,
|
|
onPointerDown,
|
|
isOpen,
|
|
tooltipId,
|
|
onClick,
|
|
referenceRef
|
|
]
|
|
);
|
|
const getTooltipPositionerProps = (0, import_react.useCallback)(
|
|
(props2 = {}, forwardedRef = null) => getPopperProps(
|
|
{
|
|
...props2,
|
|
style: {
|
|
...props2.style,
|
|
[import_popper.popperCSSVars.arrowSize.var]: arrowSize ? `${arrowSize}px` : void 0,
|
|
[import_popper.popperCSSVars.arrowShadowColor.var]: arrowShadowColor
|
|
}
|
|
},
|
|
forwardedRef
|
|
),
|
|
[getPopperProps, arrowSize, arrowShadowColor]
|
|
);
|
|
const getTooltipProps = (0, import_react.useCallback)(
|
|
(props2 = {}, ref2 = null) => {
|
|
const styles = {
|
|
...props2.style,
|
|
position: "relative",
|
|
transformOrigin: import_popper.popperCSSVars.transformOrigin.varRef
|
|
};
|
|
return {
|
|
ref: ref2,
|
|
...htmlProps,
|
|
...props2,
|
|
id: tooltipId,
|
|
role: "tooltip",
|
|
style: styles
|
|
};
|
|
},
|
|
[htmlProps, tooltipId]
|
|
);
|
|
return {
|
|
isOpen,
|
|
show: openWithDelay,
|
|
hide: closeWithDelay,
|
|
getTriggerProps,
|
|
getTooltipProps,
|
|
getTooltipPositionerProps,
|
|
getArrowProps,
|
|
getArrowInnerProps
|
|
};
|
|
}
|
|
var closeEventName = "chakra-ui:close-tooltip";
|
|
function useCloseEvent(ref, close) {
|
|
(0, import_react.useEffect)(() => {
|
|
const doc = getDoc(ref);
|
|
doc.addEventListener(closeEventName, close);
|
|
return () => doc.removeEventListener(closeEventName, close);
|
|
}, [close, ref]);
|
|
return () => {
|
|
const doc = getDoc(ref);
|
|
const win = getWin(ref);
|
|
doc.dispatchEvent(new win.CustomEvent(closeEventName));
|
|
};
|
|
}
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
useTooltip
|
|
});
|
|
//# sourceMappingURL=use-tooltip.js.map
|