mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-12-01 21:05:42 +09:00
React 19 simplified ref object story, moved JSX from global into react package. Here prepared our code to not refactor too much in packages bump.
33 lines
879 B
TypeScript
33 lines
879 B
TypeScript
import { useEffect, useRef } from "react";
|
|
import type { Point } from "@webstudio-is/design-system";
|
|
|
|
// Draw a point where we think the pointer is to visualize if calculations are based on the right position
|
|
// Only needed for debugging.
|
|
export const usePointerOutline = () => {
|
|
const ref = useRef<undefined | HTMLDivElement>(undefined);
|
|
|
|
useEffect(() => {
|
|
const div = document.createElement("div");
|
|
div.style.cssText = `
|
|
position: absolute;
|
|
width: 5px;
|
|
height: 5px;
|
|
background: red;
|
|
`;
|
|
document.body.appendChild(div);
|
|
ref.current = div;
|
|
return () => {
|
|
document.body.removeChild(div);
|
|
ref.current = undefined;
|
|
};
|
|
}, []);
|
|
|
|
return (offset: Point) => {
|
|
if (ref.current === undefined) {
|
|
return;
|
|
}
|
|
ref.current.style.left = `${offset.x}px`;
|
|
ref.current.style.top = `${offset.y}px`;
|
|
};
|
|
};
|