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

Function TutorialTooltip

src/browser/components/TutorialTooltip/TutorialTooltip.tsx:28–224  ·  view source on GitHub ↗
({
  step,
  currentStep,
  totalSteps,
  onNext,
  onDismiss,
  onDisableTutorial,
})

Source from the content-addressed store, hash-verified

26}
27
28export const TutorialTooltip: React.FC<TutorialTooltipProps> = ({
29 step,
30 currentStep,
31 totalSteps,
32 onNext,
33 onDismiss,
34 onDisableTutorial,
35}) => {
36 const tooltipRef = useRef<HTMLDivElement>(null);
37 const [position, setPosition] = useState<TooltipPosition | null>(null);
38 const [showDisableOption, setShowDisableOption] = useState(false);
39
40 useLayoutEffect(() => {
41 const targetEl = document.querySelector(`[data-tutorial="${step.target}"]`);
42 if (!targetEl || !tooltipRef.current) {
43 return;
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) {

Callers

nothing calls this directly

Calls 6

cnFunction · 0.90
calculatePositionFunction · 0.85
removeEventListenerMethod · 0.80
addMethod · 0.80
removeMethod · 0.65
addEventListenerMethod · 0.45

Tested by

no test coverage detected