UNPKG

@wordpress/components

Version:
74 lines (73 loc) • 2.99 kB
import * as Ariakit from "@ariakit/react"; import clsx from "clsx"; import { useInstanceId } from "@wordpress/compose"; import { Children, cloneElement, forwardRef, useContext } from "@wordpress/element"; import deprecated from "@wordpress/deprecated"; import Shortcut from "../shortcut/index.mjs"; import { positionToPlacement } from "../popover/utils.mjs"; import { TooltipInternalContext } from "./context.mjs"; import { jsx, jsxs } from "react/jsx-runtime"; // packages/components/src/tooltip/index.tsx /** * Time over anchor to wait before showing tooltip */ const TOOLTIP_DELAY = 700; const CONTEXT_VALUE = { isNestedInTooltip: true }; function UnforwardedTooltip(props, ref) { const { children, className, delay = 700, hideOnClick = true, placement, position, shortcut, text, ...restProps } = props; const { isNestedInTooltip } = useContext(TooltipInternalContext); const baseId = useInstanceId(Tooltip, "tooltip"); const describedById = text || shortcut ? baseId : void 0; const isOnlyChild = Children.count(children) === 1; if (!isOnlyChild) { if ("development" === process.env.NODE_ENV) console.error("wp-components.Tooltip should be called with only a single child element."); } let computedPlacement; if (placement !== void 0) computedPlacement = placement; else if (position !== void 0) { computedPlacement = positionToPlacement(position); deprecated("`position` prop in wp.components.tooltip", { since: "6.4", alternative: "`placement` prop" }); } computedPlacement = computedPlacement || "top"; const tooltipStore = Ariakit.useTooltipStore({ placement: computedPlacement, showTimeout: delay }); const mounted = Ariakit.useStoreState(tooltipStore, "mounted"); if (isNestedInTooltip) return isOnlyChild ? /* @__PURE__ */ jsx(Ariakit.Role, { ...restProps, render: children }) : children; function addDescribedById(el) { const element = el; return describedById && mounted && element.props["aria-describedby"] === void 0 && element.props["aria-label"] !== text ? cloneElement(element, { "aria-describedby": describedById }) : element; } return /* @__PURE__ */ jsxs(TooltipInternalContext.Provider, { value: CONTEXT_VALUE, children: [/* @__PURE__ */ jsx(Ariakit.TooltipAnchor, { onClick: hideOnClick ? tooltipStore.hide : void 0, store: tooltipStore, render: isOnlyChild ? addDescribedById(children) : void 0, ref, children: isOnlyChild ? void 0 : children }), isOnlyChild && (text || shortcut) && /* @__PURE__ */ jsxs(Ariakit.Tooltip, { ...restProps, className: clsx("components-tooltip", className), unmountOnHide: true, gutter: 4, id: describedById, overflowPadding: .5, store: tooltipStore, children: [text, shortcut && /* @__PURE__ */ jsx(Shortcut, { className: text ? "components-tooltip__shortcut" : "", shortcut })] })] }); } const Tooltip = forwardRef(UnforwardedTooltip); export { TOOLTIP_DELAY, Tooltip, Tooltip as default }; //# sourceMappingURL=index.mjs.map