import type { TooltipContentProps, TooltipPortalProps, TooltipProps as RootProps, } from '@radix-ui/react-tooltip'; import * as TooltipPrimitive from '@radix-ui/react-tooltip'; import { cssVar } from '@toeverything/theme'; import { cssVarV2 } from '@toeverything/theme/v2'; import clsx from 'clsx'; import { type ReactElement, type ReactNode } from 'react'; import { getCommand } from '../../utils/keyboard-mapping'; import * as styles from './styles.css'; export interface TooltipProps { // `children` can not be string, number or even undefined children: ReactElement; content?: ReactNode; /** * When shortcut is provided, will use a single line layout * * ```tsx * // [T] * // [⌘ + K] * // [⌘] [K] * // [⌘] [K] or [Ctrl] [K] * ``` * * Mapping: * | Shortcut | macOS | Windows | * |----------|-------|---------| * | `$mod` | `⌘` | `Ctrl` | * | `$alt` | `⌥` | `Alt` | * | `$shift` | `⇧` | `Shift` | */ shortcut?: string | string[]; side?: TooltipContentProps['side']; align?: TooltipContentProps['align']; rootOptions?: Omit; portalOptions?: TooltipPortalProps; options?: Omit; } const TooltipShortcut = ({ shortcut }: { shortcut: string | string[] }) => { const commands = (Array.isArray(shortcut) ? shortcut : [shortcut]) .map(cmd => cmd.trim()) .map(cmd => getCommand(cmd)); return (
{commands.map((cmd, index) => (
{cmd}
))}
); }; export const Tooltip = ({ children, content, side = 'top', align = 'center', shortcut, options, rootOptions, portalOptions, }: TooltipProps) => { if (!content) { return children; } const { className, ...contentOptions } = options || {}; const { style: contentStyle, ...restContentOptions } = contentOptions; return ( {children} {shortcut ? (
{content}
) : ( content )}
); };