418 lines
13 KiB
JavaScript
418 lines
13 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/index.ts
|
|
var src_exports = {};
|
|
__export(src_exports, {
|
|
Tooltip: () => Tooltip,
|
|
useTooltip: () => useTooltip
|
|
});
|
|
module.exports = __toCommonJS(src_exports);
|
|
|
|
// src/tooltip.tsx
|
|
var import_popper2 = require("@chakra-ui/popper");
|
|
var import_portal = require("@chakra-ui/portal");
|
|
var import_system = require("@chakra-ui/system");
|
|
|
|
// ../../utilities/object-utils/src/omit.ts
|
|
function omit(object, keysToOmit = []) {
|
|
const clone = Object.assign({}, object);
|
|
for (const key of keysToOmit) {
|
|
if (key in clone) {
|
|
delete clone[key];
|
|
}
|
|
}
|
|
return clone;
|
|
}
|
|
|
|
// ../../utilities/object-utils/src/pick.ts
|
|
function pick(object, keysToPick) {
|
|
const result = {};
|
|
for (const key of keysToPick) {
|
|
if (key in object) {
|
|
result[key] = object[key];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// src/tooltip.tsx
|
|
var import_framer_motion = require("framer-motion");
|
|
var import_react2 = require("react");
|
|
|
|
// src/tooltip.transition.tsx
|
|
var scale = {
|
|
exit: {
|
|
scale: 0.85,
|
|
opacity: 0,
|
|
transition: {
|
|
opacity: { duration: 0.15, easings: "easeInOut" },
|
|
scale: { duration: 0.2, easings: "easeInOut" }
|
|
}
|
|
},
|
|
enter: {
|
|
scale: 1,
|
|
opacity: 1,
|
|
transition: {
|
|
opacity: { easings: "easeOut", duration: 0.2 },
|
|
scale: { duration: 0.2, ease: [0.175, 0.885, 0.4, 1.1] }
|
|
}
|
|
}
|
|
};
|
|
|
|
// src/use-tooltip.ts
|
|
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));
|
|
};
|
|
}
|
|
|
|
// src/tooltip.tsx
|
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
var MotionDiv = (0, import_system.chakra)(import_framer_motion.motion.div);
|
|
var Tooltip = (0, import_system.forwardRef)((props, ref) => {
|
|
var _a, _b;
|
|
const styles = (0, import_system.useStyleConfig)("Tooltip", props);
|
|
const ownProps = (0, import_system.omitThemingProps)(props);
|
|
const theme = (0, import_system.useTheme)();
|
|
const {
|
|
children,
|
|
label,
|
|
shouldWrapChildren,
|
|
"aria-label": ariaLabel,
|
|
hasArrow,
|
|
bg,
|
|
portalProps,
|
|
background,
|
|
backgroundColor,
|
|
bgColor,
|
|
motionProps,
|
|
...rest
|
|
} = ownProps;
|
|
const userDefinedBg = (_b = (_a = background != null ? background : backgroundColor) != null ? _a : bg) != null ? _b : bgColor;
|
|
if (userDefinedBg) {
|
|
styles.bg = userDefinedBg;
|
|
const bgVar = (0, import_system.getCSSVar)(theme, "colors", userDefinedBg);
|
|
styles[import_popper2.popperCSSVars.arrowBg.var] = bgVar;
|
|
}
|
|
const tooltip = useTooltip({ ...rest, direction: theme.direction });
|
|
const shouldWrap = typeof children === "string" || shouldWrapChildren;
|
|
let trigger;
|
|
if (shouldWrap) {
|
|
trigger = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
import_system.chakra.span,
|
|
{
|
|
display: "inline-block",
|
|
tabIndex: 0,
|
|
...tooltip.getTriggerProps(),
|
|
children
|
|
}
|
|
);
|
|
} else {
|
|
const child = import_react2.Children.only(children);
|
|
trigger = (0, import_react2.cloneElement)(
|
|
child,
|
|
tooltip.getTriggerProps(child.props, child.ref)
|
|
);
|
|
}
|
|
const hasAriaLabel = !!ariaLabel;
|
|
const _tooltipProps = tooltip.getTooltipProps({}, ref);
|
|
const tooltipProps = hasAriaLabel ? omit(_tooltipProps, ["role", "id"]) : _tooltipProps;
|
|
const srOnlyProps = pick(_tooltipProps, ["role", "id"]);
|
|
if (!label) {
|
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
|
|
}
|
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
trigger,
|
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_framer_motion.AnimatePresence, { children: tooltip.isOpen && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_portal.Portal, { ...portalProps, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
import_system.chakra.div,
|
|
{
|
|
...tooltip.getTooltipPositionerProps(),
|
|
__css: {
|
|
zIndex: styles.zIndex,
|
|
pointerEvents: "none"
|
|
},
|
|
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
MotionDiv,
|
|
{
|
|
variants: scale,
|
|
initial: "exit",
|
|
animate: "enter",
|
|
exit: "exit",
|
|
...motionProps,
|
|
...tooltipProps,
|
|
__css: styles,
|
|
children: [
|
|
label,
|
|
hasAriaLabel && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_system.chakra.span, { srOnly: true, ...srOnlyProps, children: ariaLabel }),
|
|
hasArrow && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
import_system.chakra.div,
|
|
{
|
|
"data-popper-arrow": true,
|
|
className: "chakra-tooltip__arrow-wrapper",
|
|
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
import_system.chakra.div,
|
|
{
|
|
"data-popper-arrow-inner": true,
|
|
className: "chakra-tooltip__arrow",
|
|
__css: { bg: styles.bg }
|
|
}
|
|
)
|
|
}
|
|
)
|
|
]
|
|
}
|
|
)
|
|
}
|
|
) }) })
|
|
] });
|
|
});
|
|
Tooltip.displayName = "Tooltip";
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
Tooltip,
|
|
useTooltip
|
|
});
|
|
//# sourceMappingURL=index.js.map
|