Skip to content

Commit 9f3751c

Browse files
authored
feat: use viewbox for arrow svg (#207)
1 parent 2cbe4a4 commit 9f3751c

File tree

2 files changed

+10
-37
lines changed

2 files changed

+10
-37
lines changed

src/Arrows.tsx

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import { Fragment } from 'react';
33
import { useChessboardContext } from './ChessboardProvider';
44
import { getRelativeCoords } from './utils';
55

6-
type Props = {
7-
boardWidth: number | undefined;
8-
boardHeight: number | undefined;
9-
};
10-
11-
export function Arrows({ boardWidth, boardHeight }: Props) {
6+
export function Arrows() {
127
const {
138
id,
149
arrows,
@@ -21,9 +16,8 @@ export function Arrows({ boardWidth, boardHeight }: Props) {
2116
newArrowOverSquare,
2217
} = useChessboardContext();
2318

24-
if (!boardWidth) {
25-
return null;
26-
}
19+
const viewBoxWidth = 2048;
20+
const viewBoxHeight = viewBoxWidth * (chessboardRows / chessboardColumns);
2721

2822
const currentlyDrawingArrow =
2923
newArrowStartSquare &&
@@ -42,11 +36,12 @@ export function Arrows({ boardWidth, boardHeight }: Props) {
4236

4337
return (
4438
<svg
45-
width={boardWidth}
46-
height={boardHeight}
39+
viewBox={`0 0 ${viewBoxWidth} ${viewBoxHeight}`}
4740
style={{
4841
position: 'absolute',
4942
top: '0',
43+
right: '0',
44+
bottom: '0',
5045
left: '0',
5146
pointerEvents: 'none',
5247
zIndex: '20', // place above pieces
@@ -55,21 +50,21 @@ export function Arrows({ boardWidth, boardHeight }: Props) {
5550
{arrowsToDraw.map((arrow, i) => {
5651
const from = getRelativeCoords(
5752
boardOrientation,
58-
boardWidth,
53+
viewBoxWidth,
5954
chessboardColumns,
6055
chessboardRows,
6156
arrow.startSquare,
6257
);
6358
const to = getRelativeCoords(
6459
boardOrientation,
65-
boardWidth,
60+
viewBoxWidth,
6661
chessboardColumns,
6762
chessboardRows,
6863
arrow.endSquare,
6964
);
7065

7166
// we want to shorten the arrow length so the tip of the arrow is more central to the target square instead of running over the center
72-
const squareWidth = boardWidth / chessboardColumns;
67+
const squareWidth = viewBoxWidth / chessboardColumns;
7368
let ARROW_LENGTH_REDUCER =
7469
squareWidth / arrowOptions.arrowLengthReducerDenominator;
7570

src/Board.tsx

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { DragOverlay } from '@dnd-kit/core';
22
import { snapCenterToCursor } from '@dnd-kit/modifiers';
3-
import { useEffect, useRef, useState } from 'react';
43

54
import { Arrows } from './Arrows';
65
import { Draggable } from './Draggable';
@@ -21,32 +20,11 @@ export function Board() {
2120
draggingPiece,
2221
id,
2322
} = useChessboardContext();
24-
const boardRef = useRef<HTMLDivElement>(null);
25-
const [boardWidth, setBoardWidth] = useState(boardRef.current?.clientWidth);
26-
const [boardHeight, setBoardHeight] = useState(
27-
boardRef.current?.clientHeight,
28-
);
29-
30-
// if the board dimensions change, update the board width and height
31-
useEffect(() => {
32-
if (boardRef.current) {
33-
const resizeObserver = new ResizeObserver(() => {
34-
setBoardWidth(boardRef.current?.clientWidth as number);
35-
setBoardHeight(boardRef.current?.clientHeight as number);
36-
});
37-
resizeObserver.observe(boardRef.current);
38-
39-
return () => {
40-
resizeObserver.disconnect();
41-
};
42-
}
43-
}, [boardRef.current]);
4423

4524
return (
4625
<>
4726
<div
4827
id={`${id}-board`}
49-
ref={boardRef}
5028
style={{ ...defaultBoardStyle(chessboardColumns), ...boardStyle }}
5129
>
5230
{board.map((row) =>
@@ -73,7 +51,7 @@ export function Board() {
7351
}),
7452
)}
7553

76-
<Arrows boardWidth={boardWidth} boardHeight={boardHeight} />
54+
<Arrows />
7755
</div>
7856

7957
<DragOverlay

0 commit comments

Comments
 (0)