diff --git a/app/routes/DragableBox.tsx b/app/routes/DragableBox.tsx index 0e64a1a..aabf442 100644 --- a/app/routes/DragableBox.tsx +++ b/app/routes/DragableBox.tsx @@ -42,12 +42,15 @@ const DraggableBox = ({ const maxX = parentRect.width - boxRect.width; const maxY = parentRect.height - boxRect.height; - setXPosition(Math.max(0, Math.min(relativeX, maxX))); - setYPosition(Math.max(0, Math.min(relativeY, maxY))); + let newX = (Math.max(0, Math.min(relativeX, maxX))); + let newY = (Math.max(0, Math.min(relativeY, maxY))); + + setXPosition(newX); + setYPosition(newY); if (setter) { setter({ - x: relativeX, - y: relativeY + x: newX, + y: newY }) } }; @@ -78,7 +81,7 @@ const DraggableBox = ({ return (
) { + return (); +} diff --git a/app/routes/_index.tsx b/app/routes/_index.tsx index 043d3ae..c28915a 100644 --- a/app/routes/_index.tsx +++ b/app/routes/_index.tsx @@ -1,7 +1,7 @@ -import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/cloudflare"; +import type { MetaFunction } from "@remix-run/cloudflare"; import DragableBox from "./DragableBox"; -import { useEffect, useState } from "react"; -import { useLoaderData, useSearchParams } from "@remix-run/react"; +import { useEffect, useRef, useState } from "react"; +import { PhAngle } from "./PhAngle"; export const meta: MetaFunction = () => { return [ @@ -11,7 +11,6 @@ export const meta: MetaFunction = () => { }; export default function Index() { - const [searchParams] = useSearchParams(); const [selectorHeight, setSelectorHeight] = useState(0); const [selectorWidth, setSelectorWidth] = useState(0); const [selectorTop, setSelectorTop] = useState(0); @@ -20,44 +19,59 @@ export default function Index() { x: 0, y: 0 }) - + const [containerWidth, setContainerWidth] = useState(null) + const [containerHeight, setContainerHeight] = useState(null) const [secondDragable, setSecondDragable] = useState({ x: 0, y: 0 }) + const containerRef = useRef(null) + + useEffect(() => { + const updateContainerSize = () => { + if (containerRef.current) { + setContainerWidth(containerRef.current.offsetWidth); + setContainerHeight(containerRef.current.offsetHeight); + } + } + window.addEventListener('resize', updateContainerSize); + return () => window.removeEventListener('resize', updateContainerSize); + }, [containerRef]) + - // Use useEffect to calculate dimensions and position useEffect(() => { - // Calculate width const width = Math.abs(secondDragable.x - firstDragable.x); setSelectorWidth(width); - - // Calculate height const height = Math.abs(secondDragable.y - firstDragable.y); setSelectorHeight(height); - - // Calculate top position (minimum of y coordinates) setSelectorTop(Math.min(firstDragable.y, secondDragable.y)); - - // Calculate left position (minimum of x coordinates) setSelectorLeft(Math.min(firstDragable.x, secondDragable.x)); }, [firstDragable, secondDragable]); return (
-
-
- - +
+ {containerHeight !== null && containerWidth !== null && ( + <> +
+ + + + + + + + )}
+ {containerWidth} - {containerHeight}
); }