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: vertical promotion dialog bottom overflow #180

Open
wants to merge 2 commits 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
27 changes: 22 additions & 5 deletions src/chessboard/components/PromotionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ export function PromotionDialog() {
`${promotePieceColor ?? "w"}B`,
];

// Determines if promotion is happening on the bottom rank
const isBottomRank =
(boardOrientation === "white" && promoteToSquare?.[1] === "1") ||
(boardOrientation === "black" && promoteToSquare?.[1] === "8");
Comment on lines +24 to +25
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if the board is more than 8x8? (see #174)

I think we may need to add a new property/properties to allow users to set the promotion rank for each colour, and default it to normal values

Copy link
Contributor Author

@Epitomaniac Epitomaniac Jan 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as we have access to the number of ranks, we can set its value for black and 1 for white. I don't think a new property is required unless there's a variant that allows promotion on a rank that's not the first or the last one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the calculation for translate effect assumes the board to be 8 by 8, so this also needs to change for custom boards based on the board dimentions.
transform: translate(${-boardWidth / 16}px, ${-boardWidth / 16}px)


const dialogStyles = {
default: {
display: "grid",
gridTemplateColumns: "1fr 1fr",
transform: `translate(${-boardWidth / 8}px, ${-boardWidth / 8}px)`,
transform: isBottomRank
? `translate(${-boardWidth / 8}px, ${+boardWidth / 8}px)`
: `translate(${-boardWidth / 8}px, ${-boardWidth / 8}px)`,
},
vertical: {
transform: `translate(${-boardWidth / 16}px, ${-boardWidth / 16}px)`,
transform: isBottomRank
? `translate(${-boardWidth / 16}px, ${+boardWidth / 16}px)`
: `translate(${-boardWidth / 16}px, ${-boardWidth / 16}px)`,
},
modal: {
display: "flex",
Expand All @@ -44,21 +53,29 @@ export function PromotionDialog() {
const dialogCoords = getRelativeCoords(
boardOrientation,
boardWidth,
promoteToSquare || "a8"
promoteToSquare || "a8",
);

// Reversing the order in which piece icons appear for vertical dialog if promotion occurs on the bottom rank
const orderedPromotionOptions =
isBottomRank && promotionDialogVariant === "vertical"
? promotionOptions.reverse()
: promotionOptions;

return (
<div
style={{
position: "absolute",
top: `${dialogCoords?.y}px`,
// Bottom rank promotion forces the dialog to start from the bottom edge
top: isBottomRank ? undefined : `${dialogCoords?.y}px`,
bottom: isBottomRank ? `${boardWidth - dialogCoords?.y}px` : undefined,
left: `${dialogCoords?.x}px`,
zIndex: 1000,
...dialogStyles[promotionDialogVariant],
}}
title="Choose promotion piece"
>
{promotionOptions.map((option) => (
{orderedPromotionOptions.map((option) => (
<PromotionOption key={option} option={option} />
))}
</div>
Expand Down
15 changes: 8 additions & 7 deletions stories/Chessboard.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export const ClickToMove = () => {
verbose: true,
});
const foundMove = moves.find(
(m) => m.from === moveFrom && m.to === square
(m) => m.from === moveFrom && m.to === square,
);
// not a valid move
if (!foundMove) {
Expand Down Expand Up @@ -833,7 +833,7 @@ export const CustomSquare = () => {
</div>
</div>
);
}
},
);

return (
Expand Down Expand Up @@ -865,7 +865,7 @@ export const AnalysisBoard = () => {

positionEvaluation &&
setPositionEvaluation(
((game.turn() === "w" ? 1 : -1) * Number(positionEvaluation)) / 100
((game.turn() === "w" ? 1 : -1) * Number(positionEvaluation)) / 100,
);
possibleMate && setPossibleMate(possibleMate);
depth && setDepth(depth);
Expand Down Expand Up @@ -1036,8 +1036,9 @@ export const BoardWithCustomArrows = () => {
///////////////////////////////////
export const ManualBoardEditor = () => {
const game = useMemo(() => new Chess("8/8/8/8/8/8/8/8 w - - 0 1"), []); // empty board
const [boardOrientation, setBoardOrientation] =
useState<"white" | "black">("white");
const [boardOrientation, setBoardOrientation] = useState<"white" | "black">(
"white",
);
const [boardWidth, setBoardWidth] = useState(360);
const [fenPosition, setFenPosition] = useState(game.fen());

Expand All @@ -1051,7 +1052,7 @@ export const ManualBoardEditor = () => {
setFenPosition(game.fen());
} else {
alert(
`The board already contains ${color === "w" ? "WHITE" : "BLACK"} KING`
`The board already contains ${color === "w" ? "WHITE" : "BLACK"} KING`,
);
}

Expand Down Expand Up @@ -1180,7 +1181,7 @@ export const ManualBoardEditor = () => {
style={buttonStyle}
onClick={() => {
setBoardOrientation(
boardOrientation === "white" ? "black" : "white"
boardOrientation === "white" ? "black" : "white",
);
}}
>
Expand Down