ISSUE-017 - Implemented drag bounds that consider the dimensions of dragged element.

This is bugged as the resize observer I'm using doesn't update itself when I reveal the OverlayItem from hiding.
This commit is contained in:
Kyle Geib committed 2022-01-13 15:00:21 -05:00
1 parent 861d702a27
commit aba5cc20be
5 files changed
+51 -25

No files matched your search

@@ -23,15 +23,6 @@ export function OverlayControl() {
/* For prototyping use only */
const [idNum, setIdNum] = useState(0);
const getRandomArbitrary = (min: number, max: number) =>
Math.random() * (max - min) + min;
const posRandomizer = () => {
dispatch(setPosition({
x: Math.floor(getRandomArbitrary(-50, 850)),
y: Math.floor(getRandomArbitrary(-50, 650)),
}));
};
const nextIdNum = () => {
const nextIdNum = (idNum + 1) % overlayIds.length;
setIdNum(nextIdNum);
@@ -45,7 +36,7 @@ export function OverlayControl() {
if (currentOverlayItem == null) {
dispatch(initNewOverlayItem({
overlayId: overlayIds[idNum],
initialPosition: { x: 0, y: 300 },
initialPosition: { x: 400, y: 300 },
setVisible: true,
}));
} else {
@@ -63,12 +54,6 @@ export function OverlayControl() {
{/* For prototyping use only */}
<br />
<a
onClick={posRandomizer}
>
Random Location
</a>
<br />
<a
onClick={nextIdNum}
>
@@ -2,24 +2,47 @@ import { useSelector, useDispatch } from "react-redux";
import { useTransition, animated } from "@react-spring/web";
import { useDrag } from "@use-gesture/react";
import { useDimensions } from "../../../utils/hooks";
import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector";
import { setPosition } from "../../../features/overlayTool/overlayToolSlice";
import { OverlayItemContainer } from "./styles";
export function OverlayDisplay() {
type OverlayDisplayProps = {
containerDimensions: {
width: number,
height: number,
},
bleedover?: number,
};
export function OverlayDisplay({
containerDimensions: { width: containerWidth, height: containerHeight },
bleedover = 0,
}: OverlayDisplayProps) {
const dispatch = useDispatch();
const {
currentOverlayId,
currentOverlayItem,
visible,
} = useSelector(overlayToolSelector);
const dispatch = useDispatch();
// Monitor element dimensions to calculate min/max drag coordinates.
const [overlayItemRef, {
width: overlayItemWidth,
height: overlayItemHeight,
}] = useDimensions<HTMLDivElement>();
const
minX = -bleedover,
minY = -bleedover,
maxX = containerWidth - overlayItemWidth + bleedover,
maxY = containerHeight - overlayItemHeight + bleedover;
const bindOverlayItem = useDrag(({ args, delta }) => {
const newX = args[0].xPos + delta[0];
const newY = args[0].yPos + delta[1];
dispatch(setPosition({
x: args[0].xPos + delta[0],
y: args[0].yPos + delta[1],
x: newX >= minX && newX <= maxX ? newX : args[0].xPos,
y: newY >= minY && newY <= maxY ? newY : args[0].yPos,
}));
});
@@ -54,7 +77,11 @@ export function OverlayDisplay() {
}}
>
<animated.div style={{ ...style, position: "absolute" }}>
<OverlayItemContainer visible={visible} tempColor={currentOverlayId}>
<OverlayItemContainer
ref={overlayItemRef}
visible={visible}
tempColor={currentOverlayId}
>
{currentOverlayId}
<br />
(x:{Math.floor(position.x)}px, y:{Math.floor(position.y)}px)
@@ -12,6 +12,7 @@ export const OverlayItemContainer = styled.div.attrs<OverlayItemContainerProps>(
},
}))<OverlayItemContainerProps>`
/* Temporary. Overlay will be transparent and based on an image. */
/* width: ${({ tempColor }) => tempColor == "red" ? "150px" : "100px"}; */
width: 150px;
height: 150px;
background-color: ${({ tempColor }) => tempColor};
@@ -16,11 +16,14 @@ import { OverlayDisplay } from "../../OverlayTool";
import { workspaceObject } from "../workspaces";
const PREVIEW_WINDOW_HEIGHT = 600;
const PREVIEW_WINDOW_WIDTH = 800;
export const Workspace = () => {
const dispatch = useDispatch();
const { workspaceId } = useParams();
const { animatedBG } = useSelector(workspaceSelector);
const { currentWorkspaceId: overlayWorkspaceId } = useSelector(overlayToolSelector);
const dispatch = useDispatch();
useEffect(() => {
if (workspaceId == null) return;
@@ -45,13 +48,23 @@ export const Workspace = () => {
centerChildren
>
<H2>{workspaceId ? workspaceObject[workspaceId].name : "Unknown"}</H2>
<PreviewWindow height={600} width={800} animatedBG={animatedBG}>
<PreviewWindow
height={PREVIEW_WINDOW_HEIGHT}
width={PREVIEW_WINDOW_WIDTH}
animatedBG={animatedBG}
>
<MockupBlock
color="#415bad"
height="300px"
width="400px"
>Preview Item</MockupBlock>
<OverlayDisplay />
<OverlayDisplay
containerDimensions={{
height: PREVIEW_WINDOW_HEIGHT,
width: PREVIEW_WINDOW_WIDTH,
}}
bleedover={50}
/>
</PreviewWindow>
<ControlsContainer>
<WorkspaceControls />
@@ -15,7 +15,7 @@ export const WorkspaceList = ({ headerOffset }: WorkspaceListProps) => {
return (
<WorkspaceListBox headerOffset={headerOffset}>
<Stack as="ul" gutter="md">
{workspaceList.map((id, i) => (
{workspaceList.map((id) => (
<WorkspaceListNavLink
key={id}
to={id}