ControlHintTooltip.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import {
  2. cloneElement,
  3. isValidElement,
  4. useCallback,
  5. type ComponentProps,
  6. type ReactNode,
  7. type Ref,
  8. } from "react";
  9. import { cn } from "@/components/lib/utils.js";
  10. import {
  11. Tooltip,
  12. TooltipContent,
  13. TooltipProvider,
  14. TooltipTrigger,
  15. } from "@/components/ui/tooltip.js";
  16. import { isAppleKeyboardPlatform } from "@/lib/keyboardShortcuts.js";
  17. interface ControlHintTooltipProps {
  18. children: ReactNode;
  19. title: ReactNode;
  20. description?: string;
  21. shortcut?: string;
  22. standalone?: boolean;
  23. open?: boolean;
  24. onOpenChange?: (open: boolean) => void;
  25. side?: ComponentProps<typeof TooltipContent>["side"];
  26. align?: ComponentProps<typeof TooltipContent>["align"];
  27. sideOffset?: ComponentProps<typeof TooltipContent>["sideOffset"];
  28. className?: string;
  29. triggerClassName?: string;
  30. triggerRef?: Ref<HTMLElement>;
  31. }
  32. type TriggerChildProps = {
  33. className?: string;
  34. ref?: Ref<HTMLElement>;
  35. };
  36. const shortcutKbdBaseClassName =
  37. "rounded-md h-4 inline-flex items-center bg-tooltip-tag px-1.5 text-ui-xs font-medium text-tooltip-tag-foreground";
  38. function setRef<T>(ref: Ref<T> | undefined, value: T | null) {
  39. if (!ref) {
  40. return;
  41. }
  42. if (typeof ref === "function") {
  43. ref(value);
  44. return;
  45. }
  46. ref.current = value;
  47. }
  48. export function ControlHintTooltip({
  49. children,
  50. title,
  51. description,
  52. shortcut,
  53. standalone = false,
  54. open,
  55. onOpenChange,
  56. side = "top",
  57. align = "center",
  58. sideOffset = 2,
  59. className,
  60. triggerClassName,
  61. triggerRef,
  62. }: ControlHintTooltipProps) {
  63. const useAppleShortcutFont = isAppleKeyboardPlatform();
  64. const shortcutFontClassName = useAppleShortcutFont ? "tracking-normal" : "font-mono";
  65. const shortcutFontStyle = useAppleShortcutFont
  66. ? {
  67. fontFamily:
  68. '-apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Helvetica Neue", sans-serif',
  69. }
  70. : undefined;
  71. const isTriggerElement = isValidElement<TriggerChildProps>(children);
  72. const childRef = isTriggerElement ? children.props.ref : undefined;
  73. // Radix Tooltip/Dropdown/Popper 的 asChild trigger 会在 ref 变化时同步 setState。
  74. // 这里必须稳定 callback ref 身份,否则每次 render 都会触发 ref detach/attach 并形成更新循环。
  75. const composedTriggerRef = useCallback(
  76. (value: HTMLElement | null) => {
  77. setRef(childRef, value);
  78. setRef(triggerRef, value);
  79. },
  80. [childRef, triggerRef],
  81. );
  82. const trigger = isTriggerElement ? (
  83. cloneElement(children, {
  84. // 以前额外包一层 span,Radix Tooltip/Select/Popover 多层 asChild 组合时,
  85. // 事件和 ref 会落到不同 DOM 上,触发 SlotClone 渲染栈错误。这里直接合到真实触发器。
  86. className: cn("shrink-0", children.props.className, triggerClassName),
  87. ref: composedTriggerRef,
  88. })
  89. ) : (
  90. <span
  91. ref={triggerRef as Ref<HTMLSpanElement>}
  92. className={cn("inline-flex shrink-0", triggerClassName)}
  93. >
  94. {children}
  95. </span>
  96. );
  97. // 大会话会为每条消息动作渲染大量 ControlHintTooltip,逐个创建 Provider
  98. // 会把 Radix 上下文树放大到消息数量级;共享 Provider 统一放在 Root。
  99. const tooltip = (
  100. <Tooltip open={open} onOpenChange={onOpenChange}>
  101. <TooltipTrigger asChild>{trigger}</TooltipTrigger>
  102. <TooltipContent
  103. align={align}
  104. side={side}
  105. sideOffset={sideOffset}
  106. className={cn(
  107. description
  108. ? "max-w-72 flex-col items-start gap-1.5 px-3 py-2 text-left"
  109. : "max-w-[min(28rem,calc(100vw-1rem))] items-center gap-2 px-2.5 py-1 text-left has-data-[slot=kbd]:pr-1",
  110. className,
  111. )}
  112. >
  113. {description ? (
  114. <div className="flex w-full items-start justify-between gap-3">
  115. <span className="text-ui-sm font-medium leading-4 text-tooltip-foreground">
  116. {title}
  117. </span>
  118. {shortcut ? (
  119. <kbd
  120. data-slot="kbd"
  121. className={cn(shortcutKbdBaseClassName, shortcutFontClassName, "shrink-0")}
  122. style={shortcutFontStyle}
  123. >
  124. {shortcut}
  125. </kbd>
  126. ) : null}
  127. </div>
  128. ) : (
  129. // 无描述的提示过去同时受 max-w-xs 和 nowrap 约束,长中英文文案会越界裁剪。
  130. // 短提示继续按内容宽度展示,超过视口安全宽度时允许自然换行;显式换行用于结构化提示。
  131. <span className="text-ui-sm font-medium whitespace-pre-line break-words text-tooltip-foreground">
  132. {title}
  133. </span>
  134. )}
  135. {description ? (
  136. <span className="max-w-64 text-ui-sm/relaxed text-tooltip-foreground/80">
  137. {description}
  138. </span>
  139. ) : null}
  140. {!description && shortcut ? (
  141. <kbd
  142. data-slot="kbd"
  143. className={cn(shortcutKbdBaseClassName, shortcutFontClassName, "shrink-0")}
  144. style={shortcutFontStyle}
  145. >
  146. {shortcut}
  147. </kbd>
  148. ) : null}
  149. </TooltipContent>
  150. </Tooltip>
  151. );
  152. // 少数组件需要支持脱离 Root 的 SSR/单测渲染;由封装按需补 Provider,
  153. // 避免业务层重新组合 Tooltip primitives,同时不让列表中的常规提示重复创建上下文。
  154. return standalone ? <TooltipProvider>{tooltip}</TooltipProvider> : tooltip;
  155. }