Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix drag boundary not updating when board is moved on screen #149

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/chessboard/components/CustomDragLayer.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ReactNode, useCallback } from "react";
import { ReactNode, RefObject, useCallback, useEffect, useState } from "react";
import { useDragLayer, XYCoord } from "react-dnd";

import { useChessboard } from "../context/chessboard-context";
import { CustomPieceFn, Piece, Square } from "../types";

export type CustomDragLayerProps = {
boardContainer: { left: number; top: number };
boardRef: RefObject<HTMLObjectElement>;
};

export function CustomDragLayer({ boardContainer }: CustomDragLayerProps) {
export function CustomDragLayer({ boardRef }: CustomDragLayerProps) {
const { boardWidth, chessPieces, id, snapToCursor, allowDragOutsideBoard } =
useChessboard();

Expand All @@ -31,6 +31,12 @@ export function CustomDragLayer({ boardContainer }: CustomDragLayerProps) {
isDragging: boolean;
} = collectedProps;

const [boardContainer, setBoardContainer] = useState({ left: 0, top: 0 })
useEffect(() => {
const rect = boardRef.current?.getBoundingClientRect()
setBoardContainer({ left: rect?.left || 0, top: rect?.top || 0 })
}, [boardRef.current, isDragging])

const getItemStyle = useCallback(
(clientOffset: XYCoord | null, sourceClientOffset: XYCoord | null) => {
if (!clientOffset || !sourceClientOffset) return { display: "none" };
Expand Down
24 changes: 2 additions & 22 deletions src/chessboard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { forwardRef, useEffect, useMemo, useRef, useState } from "react";
import { forwardRef, useEffect, useRef, useState } from "react";
import { DndProvider } from "react-dnd";
import { HTML5Backend } from "react-dnd-html5-backend";
import { TouchBackend } from "react-dnd-touch-backend";
Expand Down Expand Up @@ -32,26 +32,7 @@ export const Chessboard = forwardRef<ClearPremoves, ChessboardProps>(
const [backendSet, setBackendSet] = useState(false);
const [isMobile, setIsMobile] = useState(false);
const [boardWidth, setBoardWidth] = useState(props.boardWidth);

const boardRef = useRef<HTMLObjectElement>(null);
const boardContainerRef = useRef<HTMLDivElement>(null);

const [boardContainerPos, setBoardContainerPos] = useState({
left: 0,
top: 0,
});

const metrics = useMemo(
() => boardRef.current?.getBoundingClientRect(),
[boardRef.current]
);

useEffect(() => {
setBoardContainerPos({
left: metrics?.left ? metrics?.left : 0,
top: metrics?.top ? metrics?.top : 0,
});
}, [metrics]);

useEffect(() => {
setIsMobile("ontouchstart" in window);
Expand All @@ -78,7 +59,6 @@ export const Chessboard = forwardRef<ClearPremoves, ChessboardProps>(
return backendSet && clientWindow ? (
<ErrorBoundary>
<div
ref={boardContainerRef}
style={{
display: "flex",
flexDirection: "column",
Expand All @@ -97,7 +77,7 @@ export const Chessboard = forwardRef<ClearPremoves, ChessboardProps>(
{...otherProps}
ref={ref}
>
<CustomDragLayer boardContainer={boardContainerPos} />
<CustomDragLayer boardRef={boardRef} />
<Board />
</ChessboardProvider>
)}
Expand Down