//
// Copyright 2026 DXOS.org
//
// @import-as-namespace
import { Portal } from '@ark-ui/react/portal';
import { Tooltip as TooltipPrimitive, useTooltipContext } from '@ark-ui/react/tooltip';
import React, {
type ComponentPropsWithoutRef,
type ReactNode,
type RefObject,
createContext,
forwardRef,
useContext,
useEffect,
useRef,
useState,
} from 'react';
import { useComposedRefs } from '@dxos/react-hooks';
import { mx } from '@dxos/ui-theme';
import { type ThemedClassName } from '@dxos/ui-types';
import { recipes } from '../../recipes.ts';
import { type Size } from '../../sizes.ts';
import { usePopupSize } from '../ScrollArea/PopupScroll.tsx';
/** Gap between trigger and popup, in px (positioning takes a number, not a CSS variable). */
const POPUP_GUTTER = 2;
/** Short enough to feel responsive, long enough that sweeping the pointer across a toolbar shows nothing. */
const OPEN_DELAY = 600;
type TooltipSide = 'top' | 'right' | 'bottom' | 'left';
/** The Root's `openDelay`, read by the Trigger, which runs the hover delay itself (DESIGN.md follow-up 33). */
const OpenDelayContext = createContext(OPEN_DELAY);
//
// Root
//
type TooltipRootProps = TooltipPrimitive.RootProps;
/** Ark tooltip; content mounts on open and unmounts on close unless the caller opts out. */
const TooltipRoot = ({
openDelay = OPEN_DELAY,
lazyMount = true,
unmountOnExit = true,
positioning,
...props
}: TooltipRootProps) => (
);
TooltipRoot.displayName = 'Tooltip.Root';
//
// Trigger
//
type TooltipTriggerProps = Omit & {
/** Shorthand, as on the current `Tooltip.Trigger`: the trigger brings its own Root and a Content showing this (any node, so it replaces the HTML `content` attribute). */
content?: ReactNode;
/** With `content`, the side the tooltip opens on; below by default. */
side?: TooltipSide;
};
/**
* Use `asChild` to describe a `Button`. Opens on hover after the Root's delay and on keyboard
* focus only; the delay runs here because zag skips it while any tooltip is marked open, so a click would flash one
* (DESIGN.md follow-up 33). With `content` it needs no Root or Content around it.
*/
const TooltipTrigger = forwardRef(
({ content, side, ...props }, forwardedRef) =>
content === undefined ? (
) : (
{content}
),
);
TooltipTrigger.displayName = 'Tooltip.Trigger';
const TooltipTriggerImpl = forwardRef(
(
{
onBlur,
onFocus,
onKeyDown,
onPointerMove,
onPointerOver,
onPointerEnter,
onPointerLeave,
onPointerDown,
...props
},
forwardedRef,
) => {
const tooltip = useTooltipContext();
const openDelay = useContext(OpenDelayContext);
const timer = useRef>(undefined);
const closeTimer = useRef>(undefined);
const pressed = useRef(false);
const cancel = () => clearTimeout(timer.current);
useEffect(
() => () => {
cancel();
clearTimeout(closeTimer.current);
},
[],
);
return (
{
onPointerMove?.(event);
event.preventDefault();
}}
onPointerOver={(event) => {
onPointerOver?.(event);
event.preventDefault();
}}
onPointerEnter={(event) => {
onPointerEnter?.(event);
// An enter is a fresh hover: a press can only be stale here (its leave was never seen).
pressed.current = false;
if (event.defaultPrevented || event.pointerType === 'touch') {
return;
}
cancel();
timer.current = setTimeout(() => {
if (!pressed.current) {
tooltip.setOpen(true);
}
}, openDelay);
}}
onPointerLeave={(event) => {
onPointerLeave?.(event);
pressed.current = false;
cancel();
}}
onPointerDown={(event) => {
onPointerDown?.(event);
pressed.current = true;
cancel();
tooltip.setOpen(false);
}}
onKeyDown={(event) => {
onKeyDown?.(event);
// Keyboard use ends the press, so the next keyboard focus shows the label again.
pressed.current = false;
}}
onFocus={(event) => {
onFocus?.(event);
if (pressed.current || !event.currentTarget.matches(':focus-visible')) {
event.preventDefault();
}
}}
onBlur={(event) => {
onBlur?.(event);
// The press that focused the trigger is over once focus leaves; a later focus is judged on its own.
pressed.current = false;
if (event.defaultPrevented) {
return;
}
// Deferred so a tooltip opened by the same focus move claims zag's shared store first (DESIGN.md follow-up 20).
event.preventDefault();
// A pending hover would otherwise reopen the tooltip on a trigger that no longer has focus.
cancel();
closeTimer.current = setTimeout(() => tooltip.setOpen(false));
}}
ref={forwardedRef}
/>
);
},
);
//
// Content
//
type TooltipContentProps = ThemedClassName & {
/** Overrides the size inherited from the trigger's nearest sized ancestor (Phase 4 decision 2); `sm` without one. */
size?: Size;
/** Point at the trigger with an arrow in the tooltip's inverted surface colour. */
arrow?: boolean;
/** Portals into this element instead of the body (e.g. a sized scope, AUDIT 2.2). */
container?: RefObject;
};
/**
* Portalled text on the inverted surface (as the current Tooltip; no `data-surface`, whose popup fill it would fight),
* capped at 20rem wide, with an arrow unless `arrow={false}`.
*/
const TooltipContent = forwardRef(
({ classNames, size, arrow = true, container, children, ...props }, forwardedRef) => {
const tooltip = useTooltipContext();
const popupSize = usePopupSize(size, tooltip.open, [tooltip.getTriggerProps().id], 'sm');
return (
{children}
{arrow && (
)}
);
},
);
TooltipContent.displayName = 'Tooltip.Content';
//
// TextTooltip
//
type TextTooltipProps = ThemedClassName, 'children'>> & {
/** Rendered truncated to one line; the tooltip shows it in full, and only while it is cut off. */
text: string;
side?: TooltipSide;
};
/** One line of text that ellipsizes, with a tooltip of the full text on hover only when it is truncated. */
export const TextTooltip = forwardRef(
({ classNames, text, side, ...props }, forwardedRef) => {
const [open, setOpen] = useState(false);
const localRef = useRef(null);
const ref = useComposedRefs(forwardedRef, localRef);
return (
{
const element = localRef.current;
setOpen(open && !!element && element.scrollWidth > element.clientWidth);
}}
positioning={side ? { placement: side } : undefined}
>
{text}
{text}
);
},
);
TextTooltip.displayName = 'TextTooltip';
export type {
TooltipContentProps as ContentProps,
TooltipRootProps as RootProps,
TextTooltipProps,
TooltipTriggerProps as TriggerProps,
};
export { TooltipContent as Content, TooltipRoot as Root, TooltipTrigger as Trigger };
export type { TooltipSide as Side };