The React Chessboard Library used at ChessOpenings.co.uk
Inspired and adapted from the unmaintained chessboardjsx
react-chessboard is a React component that provides chessboard functionality to your application. The Chess game logic that controls the board should be independent to the board, using a library such as Chess.js. An example of these two working together is shown in the example below.
ChessOpenings.co.uk was originally built utilising the chessboardjsx library. With chessboardjsx being unmaintained, it made it difficult to add functionality or optimise performance, so react-chessboard was made.
npm i react-chessboard
- Board Orientation Choice
- Custom Actions
- getPositionObject
- onDragOverSquare
- onMouseOutSquare
- onMouseOverSquare
- onPieceClick
- onPieceDrop
- onSquareClick
- onSquareRightClick
- Customisable Board Styles
- Customisable Pieces
- Customisable Square Styles
- Drag and Drop
- Moving Piece Animations
- Optional Square Coordinates Notation
- Position Control
- Responsive Board Width
- Draw Arrows
- Promotion Piece Select
- Spare Pieces
- If more than one board is rendered and draggable on a low end device, performance will struggle due to performance issues with react-dnd.
- In the rare case that react-chessboard component is hot swapped out for another in its place, this will cause an issue with the CustomDragLayer. To prevent this, the react-chessboard component needs to be completely unmounted before being replaced. An example of how this can be achieved is shown in
example/src/index.js.
import Chessboard from 'react-chessboard';
export default function App() {
return (
<div>
<Chessboard id="BasicBoard" />
</div>
);
}import { useState } from 'react';
import Chess from 'chess.js';
import Chessboard from 'react-chessboard';
export default function PlayRandomMoveEngine() {
const [game, setGame] = useState(new Chess());
function safeGameMutate(modify) {
setGame((g) => {
const update = { ...g };
modify(update);
return update;
});
}
function makeRandomMove() {
const possibleMoves = game.moves();
if (game.game_over() || game.in_draw() || possibleMoves.length === 0) return; // exit if the game is over
const randomIndex = Math.floor(Math.random() * possibleMoves.length);
safeGameMutate((game) => {
game.move(possibleMoves[randomIndex]);
});
}
function onDrop(sourceSquare, targetSquare) {
let move = null;
safeGameMutate((game) => {
move = game.move({
from: sourceSquare,
to: targetSquare,
promotion: 'q' // always promote to a queen for example simplicity
});
});
if (move === null) return; // illegal move
setTimeout(makeRandomMove, 200);
}
return <Chessboard position={game.fen()} onPieceDrop={onDrop} />;
}For more advanced examples, please see example boards show in example/src/boards.
| Prop | Default Value | Options | Description |
|---|---|---|---|
| animationDuration | number: 300 | Time in milliseconds for piece to slide to target square. Only used when the position is programmatically changed. If a new position is set before the animation is complete, the board will cancel the current animation and snap to the new position. | |
| arePiecesDraggable | boolean: true, | [true, false] | Whether or not all pieces are draggable. |
| boardOrientation | string: 'white', | ['white', 'black'] | The orientation of the board, the chosen colour will be at the bottom of the board. |
| boardWidth | number: 560, | The width of the board in pixels. | |
| customBoardStyle | object: {}, | inline CSS styling | Custom board style object e.g. { borderRadius: '5px', boxShadow: '0 5px 15px rgba(0, 0, 0, 0.5 '}. |
| customDarkSquareStyle | object: { backgroundColor: '#B58863' }, | inline CSS styling | Custom dark square style object. |
| customDropSquareStyle | object: { boxShadow: 'inset 0 0 1px 6px rgba(255,255,255,0.75)' }, | inline CSS styling | Custom drop square style object (Square being hovered over with dragged piece). |
| customLightSquareStyle | object: { backgroundColor: '#F0D9B5' }, | inline CSS styling | Custom light square style object. |
| customPieces | object: {}, | Custom pieces object where each key must match a corresponding chess piece (wP, wB, wN, wR, wQ, wK, bP, bB, bN, bR, bQ, bK). The value of each piece is a function that takes in some optional arguments to use and must return JSX to render. e.g. { wK: ({ isDragging: boolean, squareWidth: number, droppedPiece: string, targetSquare: string, sourceSquare: string }) => jsx } | |
| customSquareStyles | object: {}, | inline CSS styling | Custom styles for all squares. |
| id | number: 0 | [string, number] | Board identifier, necessary if more than one board is mounted for drag and drop. |
| isDraggablePiece | function: ({ piece, sourceSquare }) => true, | returns [true, false] | Function called when a piece drag is attempted. Returns if piece is draggable. |
| getPositionObject | function: (currentPosition) => {}, | User function that receives current position object when position changes. | |
| onDragOverSquare | function: (square) => {}, | User function that is run when piece is dragged over a square. | |
| onMouseOutSquare | function: (square) => {}, | User function that is run when mouse leaves a square. | |
| onMouseOverSquare | function: (square) => {}, | User function that is run when mouse is over a square. | |
| onPieceClick | function: (piece) => {}, | User function that is run when piece is clicked. | |
| onPieceDrop | function: ({ sourceSquare, targetSquare, piece }) => {}, | User function that is run when piece is dropped on a square. | |
| onSquareClick | function: (square) => {}, | User function that is run when a square is clicked. | |
| onSquareRightClick | function: (square) => {}, | User function that is run when a square is right clicked. | |
| position | string: 'start', | ['start', FEN string, { e5: 'wK', e4: 'wP', ... }] | FEN string or position object notating where the chess pieces are on the board. Start position can also be notated with the string: 'start'. |
| showBoardNotation | boolean: true | [true, false] | Whether or not to show the file and rank co-ordinates (a..h, 1..8) |
- Fork this repository
- Clone your forked repository onto your development machine
git clone https://github.com/yourUsernameHere/react-chessboard.gitcd react-chessboard - Create a branch for your PR
git checkout -b your-branch-name - Set upstream remote
git remote add upstream https://github.com/Clariity/react-chessboard.git - Make your changes
- Test your changes using the examples folder
npm run buildcd examplesnpm start - Push your changes
git add .git commit -m "feature/cool-new-feature"git push --set-upstream origin your-branch-name - Create pull request on GitHub
- Contribute again
git checkout maingit pull upstream maingit checkout -b your-new-branch-name
MIT