@wordpress/components
Version:
UI components for WordPress.
132 lines (125 loc) • 5.42 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.Tooltip = exports.TOOLTIP_DELAY = void 0;
var Ariakit = _interopRequireWildcard(require("@ariakit/react"));
var _clsx = _interopRequireDefault(require("clsx"));
var _compose = require("@wordpress/compose");
var _element = require("@wordpress/element");
var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
var _shortcut = _interopRequireDefault(require("../shortcut"));
var _utils = require("../popover/utils");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const TooltipInternalContext = (0, _element.createContext)({
isNestedInTooltip: false
});
/**
* Time over anchor to wait before showing tooltip
*/
const TOOLTIP_DELAY = exports.TOOLTIP_DELAY = 700;
const CONTEXT_VALUE = {
isNestedInTooltip: true
};
function UnforwardedTooltip(props, ref) {
const {
children,
className,
delay = TOOLTIP_DELAY,
hideOnClick = true,
placement,
position,
shortcut,
text,
...restProps
} = props;
const {
isNestedInTooltip
} = (0, _element.useContext)(TooltipInternalContext);
const baseId = (0, _compose.useInstanceId)(Tooltip, 'tooltip');
const describedById = text || shortcut ? baseId : undefined;
const isOnlyChild = _element.Children.count(children) === 1;
// console error if more than one child element is added
if (!isOnlyChild) {
if ('development' === process.env.NODE_ENV) {
// eslint-disable-next-line no-console
console.error('wp-components.Tooltip should be called with only a single child element.');
}
}
// Compute tooltip's placement:
// - give priority to `placement` prop, if defined
// - otherwise, compute it from the legacy `position` prop (if defined)
// - finally, fallback to the default placement: 'bottom'
let computedPlacement;
if (placement !== undefined) {
computedPlacement = placement;
} else if (position !== undefined) {
computedPlacement = (0, _utils.positionToPlacement)(position);
(0, _deprecated.default)('`position` prop in wp.components.tooltip', {
since: '6.4',
alternative: '`placement` prop'
});
}
computedPlacement = computedPlacement || 'bottom';
const tooltipStore = Ariakit.useTooltipStore({
placement: computedPlacement,
showTimeout: delay
});
const mounted = Ariakit.useStoreState(tooltipStore, 'mounted');
if (isNestedInTooltip) {
return isOnlyChild ? /*#__PURE__*/(0, _jsxRuntime.jsx)(Ariakit.Role, {
...restProps,
render: children
}) : children;
}
// TODO: this is a temporary workaround to minimize the effects of the
// Ariakit upgrade. Ariakit doesn't pass the `aria-describedby` prop to
// the tooltip anchor anymore since 0.4.0, so we need to add it manually.
// The `aria-describedby` attribute is added only if the anchor doesn't have
// one already, and if the tooltip text is not the same as the anchor's
// `aria-label`
// See: https://github.com/WordPress/gutenberg/pull/64066
// See: https://github.com/WordPress/gutenberg/pull/65989
function addDescribedById(element) {
return describedById && mounted && element.props['aria-describedby'] === undefined && element.props['aria-label'] !== text ? (0, _element.cloneElement)(element, {
'aria-describedby': describedById
}) : element;
}
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(TooltipInternalContext.Provider, {
value: CONTEXT_VALUE,
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Ariakit.TooltipAnchor, {
onClick: hideOnClick ? tooltipStore.hide : undefined,
store: tooltipStore,
render: isOnlyChild ? addDescribedById(children) : undefined,
ref: ref,
children: isOnlyChild ? undefined : children
}), isOnlyChild && (text || shortcut) && /*#__PURE__*/(0, _jsxRuntime.jsxs)(Ariakit.Tooltip, {
...restProps,
className: (0, _clsx.default)('components-tooltip', className),
unmountOnHide: true,
gutter: 4,
id: describedById,
overflowPadding: 0.5,
store: tooltipStore,
children: [text, shortcut && /*#__PURE__*/(0, _jsxRuntime.jsx)(_shortcut.default, {
className: text ? 'components-tooltip__shortcut' : '',
shortcut: shortcut
})]
})]
});
}
const Tooltip = exports.Tooltip = (0, _element.forwardRef)(UnforwardedTooltip);
var _default = exports.default = Tooltip;
//# sourceMappingURL=index.js.map