MCPcopy
hub / github.com/coder/mux / calculatePosition

Function calculatePosition

src/browser/components/TutorialTooltip/TutorialTooltip.tsx:46–128  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

44 }
45
46 const calculatePosition = () => {
47 const target = targetEl.getBoundingClientRect();
48 const tooltip = tooltipRef.current!.getBoundingClientRect();
49 const viewportWidth = window.innerWidth;
50 const viewportHeight = window.innerHeight;
51 const gap = 12;
52
53 const preferredPosition = step.position ?? "bottom";
54 let actualPosition = preferredPosition;
55 let top: number;
56 let left: number;
57
58 // Try preferred position, flip if it doesn't fit
59 if (preferredPosition === "bottom" || preferredPosition === "top") {
60 if (preferredPosition === "bottom") {
61 top = target.bottom + gap;
62 if (top + tooltip.height > viewportHeight) {
63 actualPosition = "top";
64 top = target.top - tooltip.height - gap;
65 }
66 } else {
67 top = target.top - tooltip.height - gap;
68 if (top < 0) {
69 actualPosition = "bottom";
70 top = target.bottom + gap;
71 }
72 }
73 // Center horizontally relative to target
74 left = target.left + target.width / 2 - tooltip.width / 2;
75 } else {
76 // left or right
77 if (preferredPosition === "right") {
78 left = target.right + gap;
79 if (left + tooltip.width > viewportWidth) {
80 actualPosition = "left";
81 left = target.left - tooltip.width - gap;
82 }
83 } else {
84 left = target.left - tooltip.width - gap;
85 if (left < 0) {
86 actualPosition = "right";
87 left = target.right + gap;
88 }
89 }
90 // Center vertically relative to target
91 top = target.top + target.height / 2 - tooltip.height / 2;
92 }
93
94 // Clamp to viewport bounds
95 const minMargin = 8;
96 left = Math.max(minMargin, Math.min(viewportWidth - tooltip.width - minMargin, left));
97 top = Math.max(minMargin, Math.min(viewportHeight - tooltip.height - minMargin, top));
98
99 // Calculate arrow position
100 const arrowStyle: React.CSSProperties = {};
101 if (actualPosition === "bottom" || actualPosition === "top") {
102 const arrowLeft = target.left + target.width / 2 - left;
103 arrowStyle.left = `${Math.max(12, Math.min(tooltip.width - 12, arrowLeft))}px`;

Callers 1

TutorialTooltipFunction · 0.85

Calls 1

getBoundingClientRectMethod · 0.65

Tested by

no test coverage detected