@base-ui/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
144 lines (140 loc) • 5.85 kB
JavaScript
'use client';
import * as React from 'react';
import { fastComponent } from '@base-ui/utils/fastHooks';
import { EMPTY_OBJECT } from '@base-ui/utils/empty';
import { useIsoLayoutEffect } from '@base-ui/utils/useIsoLayoutEffect';
import { TooltipRootContext } from "./TooltipRootContext.mjs";
import { useClientPoint, useDismiss } from "../../floating-ui-react/index.mjs";
import { createChangeEventDetails } from "../../internals/createBaseUIEventDetails.mjs";
import { PopupHandleAttachment, useImplicitActiveTrigger, usePopupRootStore, useOpenStateTransitions, usePopupInteractionProps } from "../../utils/popups/index.mjs";
import { mergeProps } from "../../merge-props/index.mjs";
import { TooltipStore } from "../store/TooltipStore.mjs";
import { REASONS } from "../../internals/reasons.mjs";
/**
* Groups all parts of the tooltip.
* Doesn't render its own HTML element.
*
* Documentation: [Base UI Tooltip](https://base-ui.com/react/components/tooltip)
*/
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export const TooltipRoot = fastComponent(function TooltipRoot(props) {
const {
disabled = false,
defaultOpen = false,
open: openProp,
disableHoverablePopup = false,
trackCursorAxis = 'none',
actionsRef,
onOpenChange,
onOpenChangeComplete,
handle,
triggerId: triggerIdProp,
defaultTriggerId: defaultTriggerIdProp = null,
children
} = props;
const store = usePopupRootStore((floatingId, nested) => new TooltipStore({
open: defaultOpen,
openProp,
activeTriggerId: defaultTriggerIdProp,
triggerIdProp
}, floatingId, nested));
store.useControlledProp('openProp', openProp);
store.useControlledProp('triggerIdProp', triggerIdProp);
store.useContextCallback('onOpenChange', onOpenChange);
store.useContextCallback('onOpenChangeComplete', onOpenChangeComplete);
const openState = store.useState('open');
const open = !disabled && openState;
const activeTriggerId = store.useState('activeTriggerId');
const mounted = store.useState('mounted');
const payload = store.useState('payload');
store.useSyncedValues({
trackCursorAxis,
disableHoverablePopup,
disabled
});
useImplicitActiveTrigger(store, {
closeOnActiveTriggerUnmount: true
});
const {
forceUnmount,
transitionStatus
} = useOpenStateTransitions(open, store);
const isInstantPhase = store.useState('isInstantPhase');
const instantType = store.useState('instantType');
const lastOpenChangeReason = store.useState('lastOpenChangeReason');
// Animations should be instant in two cases:
// 1) Opening during the provider's instant phase (adjacent tooltip opens instantly)
// 2) Closing because another tooltip opened (reason === 'none')
// Otherwise, allow the animation to play. In particular, do not disable animations
// during the 'ending' phase unless it's due to a sibling opening.
const previousInstantTypeRef = React.useRef(null);
useIsoLayoutEffect(() => {
if (openState && disabled) {
store.setOpen(false, createChangeEventDetails(REASONS.disabled));
}
}, [openState, disabled, store]);
useIsoLayoutEffect(() => {
if (transitionStatus === 'ending' && lastOpenChangeReason === REASONS.none || transitionStatus !== 'ending' && isInstantPhase) {
// Capture the current instant type so we can restore it later
// and set to 'delay' to disable animations while moving from one trigger to another
// within a delay group.
if (instantType !== 'delay') {
previousInstantTypeRef.current = instantType;
}
store.set('instantType', 'delay');
} else if (previousInstantTypeRef.current !== null) {
store.set('instantType', previousInstantTypeRef.current);
previousInstantTypeRef.current = null;
}
}, [transitionStatus, isInstantPhase, lastOpenChangeReason, instantType, store]);
useIsoLayoutEffect(() => {
if (open) {
if (activeTriggerId == null) {
store.set('payload', undefined);
}
}
}, [store, activeTriggerId, open]);
React.useImperativeHandle(actionsRef, () => ({
unmount: forceUnmount,
close: () => store.setOpen(false, createChangeEventDetails(REASONS.imperativeAction))
}), [forceUnmount, store]);
const shouldRenderInteractions = open || mounted || !disabled && trackCursorAxis !== 'none';
return /*#__PURE__*/_jsxs(TooltipRootContext.Provider, {
value: store,
children: [handle && /*#__PURE__*/_jsx(PopupHandleAttachment, {
handle: handle,
store: store
}), shouldRenderInteractions && /*#__PURE__*/_jsx(TooltipInteractions, {
store: store,
disabled: disabled,
trackCursorAxis: trackCursorAxis
}), typeof children === 'function' ? children({
payload
}) : children]
});
});
if (process.env.NODE_ENV !== "production") TooltipRoot.displayName = "TooltipRoot";
function TooltipInteractions({
store,
disabled,
trackCursorAxis
}) {
const floatingRootContext = store.useState('floatingRootContext');
const dismiss = useDismiss(floatingRootContext, {
enabled: !disabled,
referencePress: () => store.select('closeOnClick')
});
const clientPoint = useClientPoint(floatingRootContext, {
enabled: !disabled && trackCursorAxis !== 'none',
axis: trackCursorAxis === 'none' ? undefined : trackCursorAxis
});
// Both hooks return `trigger: reference` (same object identity), so the active and
// inactive trigger props can never differ. `useClientPoint` has no floating-side props.
const triggerProps = React.useMemo(() => mergeProps(clientPoint.reference, dismiss.reference), [clientPoint.reference, dismiss.reference]);
usePopupInteractionProps(store, {
activeTriggerProps: triggerProps,
inactiveTriggerProps: triggerProps,
popupProps: dismiss.floating ?? EMPTY_OBJECT
});
return null;
}