UNPKG

@base-ui-components/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.

141 lines (138 loc) 5.5 kB
'use client'; import * as React from 'react'; import { useIsoLayoutEffect } from '@base-ui-components/utils/useIsoLayoutEffect'; import { TooltipRootContext } from "./TooltipRootContext.js"; import { useClientPoint, useDismiss, useFocus, useInteractions, useSyncedFloatingRootContext } from "../../floating-ui-react/index.js"; import { createChangeEventDetails } from "../../utils/createBaseUIEventDetails.js"; import { useImplicitActiveTrigger, useOpenStateTransitions } from "../../utils/popups/index.js"; import { TooltipStore } from "../store/TooltipStore.js"; import { REASONS } from "../../utils/reasons.js"; /** * 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 } from "react/jsx-runtime"; export 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 = TooltipStore.useStore(handle?.store, { open: openProp ?? defaultOpen, activeTriggerId: triggerIdProp !== undefined ? triggerIdProp : defaultTriggerIdProp }); store.useControlledProp('open', openProp, defaultOpen); store.useControlledProp('activeTriggerId', triggerIdProp, defaultTriggerIdProp); store.useContextCallback('onOpenChange', onOpenChange); store.useContextCallback('onOpenChangeComplete', onOpenChangeComplete); const openState = store.useState('open'); const activeTriggerId = store.useState('activeTriggerId'); const payload = store.useState('payload'); store.useSyncedValues({ trackCursorAxis, disableHoverablePopup }); const open = !disabled && openState; useIsoLayoutEffect(() => { if (openState && disabled) { store.setOpen(false, createChangeEventDetails(REASONS.disabled)); } }, [openState, disabled, store]); store.useSyncedValue('disabled', disabled); useImplicitActiveTrigger(store); 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 (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]); const handleImperativeClose = React.useCallback(() => { store.setOpen(false, createTooltipEventDetails(store, REASONS.imperativeAction)); }, [store]); React.useImperativeHandle(actionsRef, () => ({ unmount: forceUnmount, close: handleImperativeClose }), [forceUnmount, handleImperativeClose]); const floatingRootContext = useSyncedFloatingRootContext({ popupStore: store, onOpenChange: store.setOpen }); const focus = useFocus(floatingRootContext, { enabled: !disabled }); const dismiss = useDismiss(floatingRootContext, { enabled: !disabled, referencePress: true }); const clientPoint = useClientPoint(floatingRootContext, { enabled: !disabled && trackCursorAxis !== 'none', axis: trackCursorAxis === 'none' ? undefined : trackCursorAxis }); const { getReferenceProps, getFloatingProps, getTriggerProps } = useInteractions([focus, dismiss, clientPoint]); const activeTriggerProps = React.useMemo(() => getReferenceProps(), [getReferenceProps]); const inactiveTriggerProps = React.useMemo(() => getTriggerProps(), [getTriggerProps]); const popupProps = React.useMemo(() => getFloatingProps(), [getFloatingProps]); store.useSyncedValues({ floatingRootContext, activeTriggerProps, inactiveTriggerProps, popupProps }); return /*#__PURE__*/_jsx(TooltipRootContext.Provider, { value: store, children: typeof children === 'function' ? children({ payload }) : children }); } function createTooltipEventDetails(store, reason) { const details = createChangeEventDetails(reason); details.preventUnmountOnClose = () => { store.set('preventUnmountingOnClose', true); }; return details; }