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
Changes from 1 commit
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
32 changes: 25 additions & 7 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,23 +53,32 @@ export function PromotionDialog() {
const dialogCoords = getRelativeCoords(
boardOrientation,
boardWidth,
promoteToSquare || "a8"
promoteToSquare || "a8",
);

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) => (
<PromotionOption key={option} option={option} />
))}
{
// Reversing the order in which piece icons appear for vertical dialog if promotion occurs on the bottom rank
isBottomRank && promotionDialogVariant === "vertical"
? promotionOptions
.reverse()
.map((option) => <PromotionOption key={option} option={option} />)
: promotionOptions.map((option) => (
<PromotionOption key={option} option={option} />
))
}
Copy link
Owner

Choose a reason for hiding this comment

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

the logic for this can sit outside the JSX to make it look a bit tidier

const orderedPromotionOptions = isBottomRank && promotionDialogVariant === "vertical"
  ? promotionOptions.reverse() 
  : promotionOptions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

</div>
);
}