Skip to content

feat: manually resizable board #125

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion src/chessboard/components/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDivElement>(null);

const {
Expand Down Expand Up @@ -66,6 +67,11 @@ export function Board() {
<PromotionDialog />
</>
)}
<ResizableButton
initialBoardWidth={boardWidth}
boardContainer={boardContainer}
setBoardWidth={setBoardWidth}
/>
</div>
</div>
) : (
Expand Down
77 changes: 77 additions & 0 deletions src/chessboard/components/ResizeButton.tsx
Original file line number Diff line number Diff line change
@@ -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<CSSProperties>({
position: "absolute",
zIndex: "24",
border: "none",
outline: "none",
padding: "0",
borderRadius: "50%",
cursor: "nwse-resize",
background: "transparent",
});

const resizeButtonRef = useRef<HTMLButtonElement>(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 (
<button
ref={resizeButtonRef}
style={{
...buttonStyle,
right: `${-squareWidth / 4}px`,
bottom: `${-squareWidth / 4}px`,
width: `${squareWidth / 2}px`,
height: `${squareWidth / 2}px`,
}}
onClick={() => console.log("Clik")}
onMouseDown={onResizeBoard}
onMouseOver={() => {
setButtonStyle({ ...buttonStyle, background: "#ff980091" });
}}
onMouseOut={() => {
setButtonStyle({ ...buttonStyle, background: "transparent" });
}}
>
<svg
width="100%"
height="100%"
viewBox="0 0 0.96 0.96"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M.4.8h.4V.4" stroke="#33363F" strokeWidth=".08" />
<path d="M.48.68h.2v-.2" stroke="#33363F" strokeWidth=".08" />
</svg>
</button>
);
};

export default ResizableButton;
6 changes: 5 additions & 1 deletion src/chessboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const Chessboard = forwardRef<ClearPremoves, ChessboardProps>(
style={{
display: "flex",
flexDirection: "column",
alignItems: "center",
width: "100%",
}}
>
Expand All @@ -98,7 +99,10 @@ export const Chessboard = forwardRef<ClearPremoves, ChessboardProps>(
ref={ref}
>
<CustomDragLayer boardContainer={boardContainerPos} />
<Board />
<Board
boardContainer={boardContainerPos}
setBoardWidth={setBoardWidth}
/>
</ChessboardProvider>
)}
</DndProvider>
Expand Down
42 changes: 42 additions & 0 deletions stories/Chessboard.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1063,3 +1063,45 @@ export const BoardWithCustomArrows = () => {
</div>
);
};

///////////////////////////////////
////////// 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 (
<>
<div style={{ display: "flex" }}>
<div style={emptyPanelStyle}>Left Panel</div>
<div
style={{
flex: "0 0 360px",
marginBottom: "24px",
}}
>
<Chessboard
id="ResizableBoard"
customBoardStyle={{
borderRadius: "4px",
boxShadow: "0 2px 10px rgba(0, 0, 0, 0.5)",
}}
/>
<button style={buttonStyle}>Button 1</button>
<button style={buttonStyle}>Button 2</button>
<button style={buttonStyle}>Button 3</button>
</div>
<div style={emptyPanelStyle}>Right Panel</div>
</div>
<div style={{ ...emptyPanelStyle, minHeight: "240px" }}>Bottom Panel</div>
</>
);
};