From 21c2d712271767ff636470781692a62822ad6670 Mon Sep 17 00:00:00 2001 From: Manukyanq Date: Mon, 22 Jan 2024 00:37:40 +0300 Subject: [PATCH] feat: manually resizable board --- src/chessboard/components/Board.tsx | 8 ++- src/chessboard/components/ResizeButton.tsx | 77 ++++++++++++++++++++++ src/chessboard/index.tsx | 6 +- stories/Chessboard.stories.tsx | 42 ++++++++++++ 4 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 src/chessboard/components/ResizeButton.tsx diff --git a/src/chessboard/components/Board.tsx b/src/chessboard/components/Board.tsx index adee1ec..6029882 100644 --- a/src/chessboard/components/Board.tsx +++ b/src/chessboard/components/Board.tsx @@ -4,8 +4,9 @@ import { Arrows } from "./Arrows"; import { useChessboard } from "../context/chessboard-context"; import { PromotionDialog } from "./PromotionDialog"; import { WhiteKing } from "./ErrorBoundary"; +import { ResizableButton } from "./ResizeButton"; -export function Board() { +export function Board({ boardContainer, setBoardWidth }: any) { const boardRef = useRef(null); const { @@ -66,6 +67,11 @@ export function Board() { )} + ) : ( diff --git a/src/chessboard/components/ResizeButton.tsx b/src/chessboard/components/ResizeButton.tsx new file mode 100644 index 0000000..fa52935 --- /dev/null +++ b/src/chessboard/components/ResizeButton.tsx @@ -0,0 +1,77 @@ +import { useState, useRef, CSSProperties } from "react"; + +export const ResizableButton = ({ + initialBoardWidth, + boardContainer, + setBoardWidth, +}: any) => { + const squareWidth = initialBoardWidth / 8; + const [buttonStyle, setButtonStyle] = useState({ + position: "absolute", + zIndex: "24", + border: "none", + outline: "none", + padding: "0", + borderRadius: "50%", + cursor: "nwse-resize", + background: "transparent", + }); + + const resizeButtonRef = useRef(null); + + function calculateResizedDivHeight(e: MouseEvent) { + setButtonStyle({ ...buttonStyle, background: "#ff980091" }); + const mouseYCoord = e.clientY; + const mouseXCoord = e.clientX; + setBoardWidth( + Math.max( + mouseYCoord - boardContainer.top, + mouseXCoord - boardContainer.left + ) + ); + } + + function stopResize() { + setButtonStyle({ ...buttonStyle, background: "transparent" }); + window.removeEventListener("mousemove", calculateResizedDivHeight, false); + window.removeEventListener("mouseup", stopResize, false); + } + + const onResizeBoard = () => { + window.addEventListener("mousemove", calculateResizedDivHeight, false); + window.addEventListener("mouseup", stopResize, false); + }; + return ( + + ); +}; + +export default ResizableButton; diff --git a/src/chessboard/index.tsx b/src/chessboard/index.tsx index 79eb0b3..61665c3 100644 --- a/src/chessboard/index.tsx +++ b/src/chessboard/index.tsx @@ -82,6 +82,7 @@ export const Chessboard = forwardRef( style={{ display: "flex", flexDirection: "column", + alignItems: "center", width: "100%", }} > @@ -98,7 +99,10 @@ export const Chessboard = forwardRef( ref={ref} > - + )} diff --git a/stories/Chessboard.stories.tsx b/stories/Chessboard.stories.tsx index c268435..946c5c3 100644 --- a/stories/Chessboard.stories.tsx +++ b/stories/Chessboard.stories.tsx @@ -1063,3 +1063,45 @@ export const BoardWithCustomArrows = () => { ); }; + +/////////////////////////////////// +////////// ResizableBoard /////////// +/////////////////////////////////// +export const ResizableBoard = () => { + const emptyPanelStyle = { + background: "#f0d9b5", + borderRadius: "16px", + border: "2px solid #b58863", + display: "flex", + justifyContent: "center", + alignItems: "center", + flex: "1 1 auto", + margin: " 0px 24px 24px", + }; + return ( + <> +
+
Left Panel
+
+ + + + +
+
Right Panel
+
+
Bottom Panel
+ + ); +};