@cosxai/ui 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/primitives/Tooltip.tsx +26 -5
package/package.json
CHANGED
|
@@ -29,23 +29,35 @@ export interface TooltipProps {
|
|
|
29
29
|
}>;
|
|
30
30
|
// ms before showing. Default 120.
|
|
31
31
|
delay?: number;
|
|
32
|
-
// "top" hovers above the trigger, "bottom" below
|
|
33
|
-
|
|
32
|
+
// "top" hovers above the trigger, "bottom" below, "auto" prefers
|
|
33
|
+
// top but flips to bottom when the trigger sits near the viewport
|
|
34
|
+
// top edge (would otherwise clip against the browser chrome).
|
|
35
|
+
// Default "auto".
|
|
36
|
+
placement?: "top" | "bottom" | "auto";
|
|
34
37
|
// Render even if `content` is falsy. Default false — callers can
|
|
35
38
|
// wrap unconditionally without branching JSX.
|
|
36
39
|
alwaysRender?: boolean;
|
|
37
40
|
}
|
|
38
41
|
|
|
42
|
+
// AUTO_FLIP_THRESHOLD_PX = the vertical space "top" placement needs
|
|
43
|
+
// above the trigger (tooltip height + gap + a bit of breathing room).
|
|
44
|
+
// Trigger positioned closer than this to the viewport top gets a
|
|
45
|
+
// bottom-flip.
|
|
46
|
+
const AUTO_FLIP_THRESHOLD_PX = 48;
|
|
47
|
+
|
|
39
48
|
export function Tooltip({
|
|
40
49
|
content,
|
|
41
50
|
children,
|
|
42
51
|
delay = 120,
|
|
43
|
-
placement = "
|
|
52
|
+
placement = "auto",
|
|
44
53
|
alwaysRender = false,
|
|
45
54
|
}: TooltipProps) {
|
|
46
55
|
const triggerRef = useRef<HTMLElement | null>(null);
|
|
47
56
|
const [open, setOpen] = useState(false);
|
|
48
57
|
const [pos, setPos] = useState<{ x: number; y: number } | null>(null);
|
|
58
|
+
const [resolvedPlacement, setResolvedPlacement] = useState<"top" | "bottom">(
|
|
59
|
+
placement === "bottom" ? "bottom" : "top",
|
|
60
|
+
);
|
|
49
61
|
const timerRef = useRef<number | null>(null);
|
|
50
62
|
|
|
51
63
|
const cancelTimer = useCallback(() => {
|
|
@@ -59,9 +71,18 @@ export function Tooltip({
|
|
|
59
71
|
const el = triggerRef.current;
|
|
60
72
|
if (!el) return;
|
|
61
73
|
const r = el.getBoundingClientRect();
|
|
74
|
+
// "auto" resolves to top when there's enough room above, else
|
|
75
|
+
// bottom. Explicit "top" / "bottom" are honoured verbatim.
|
|
76
|
+
let effective: "top" | "bottom";
|
|
77
|
+
if (placement === "auto") {
|
|
78
|
+
effective = r.top < AUTO_FLIP_THRESHOLD_PX ? "bottom" : "top";
|
|
79
|
+
} else {
|
|
80
|
+
effective = placement;
|
|
81
|
+
}
|
|
82
|
+
setResolvedPlacement(effective);
|
|
62
83
|
setPos({
|
|
63
84
|
x: r.left + r.width / 2,
|
|
64
|
-
y:
|
|
85
|
+
y: effective === "top" ? r.top : r.bottom,
|
|
65
86
|
});
|
|
66
87
|
}, [placement]);
|
|
67
88
|
|
|
@@ -137,7 +158,7 @@ export function Tooltip({
|
|
|
137
158
|
left: pos?.x ?? -9999,
|
|
138
159
|
top: pos?.y ?? -9999,
|
|
139
160
|
transform:
|
|
140
|
-
|
|
161
|
+
resolvedPlacement === "top"
|
|
141
162
|
? "translate(-50%, calc(-100% - 8px))"
|
|
142
163
|
: "translate(-50%, 8px)",
|
|
143
164
|
background: "var(--ck-bg-surface)",
|