A react chess board controlled using FEN notation.
npm install react-fen-chess-boardreact-fen-chess-board is as the name suggests a React chess board which is controlled using FEN
notation.
You can find a working demo here.
`npm install react-fen-chess-board
To render a static chess board you simply provide the FEN of the position you want to display.
``typescript jsx
import React from "react";
import { render } from "react-dom";
import { ChessBoard } from "react-fen-chess-board";
render(
fen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
/>,
document.getElementById("your-react-root")
);
``
Controls if the board should be rotated or not.
``typescript jsx``
render(
rotated
/>,
document.getElementById("your-react-root")
);
A function which enables drag-and-drop and is called whenever the user drags and drops a piece. The
demo uses this prop
alongside chess.js to create
a useChessBoard
hook that enables a fully functional chess board.
See main.tsx for an example
of how to use it.
Since react-fen-chess-board uses react-dnd behind the
scenes your application needs to be wrapped in
a DndProvider in order to use onMove.
Out of the box react-fen-chess-board includes a ChessBoardDndProvider component you can use for
this purpose.
``typescript jsx
import React from "react";
import { render } from "react-dom";
import { ChessBoardDndProvider } from "react-fen-chess-board";
import MyChessBoard from "./MyChessBoard";
render(
document.getElementById("your-react-root")
);
``
Besides the ability to pass render props or using CSS there is also an option to pass
a pieceTheme or boardTheme to change the appearance of the ChessBoard with less hassle.
#### boardTheme
``javascript
export const brownBoardTheme = {
darkSquare: "#b58863",
lightSquare: "#f0d9b5"
};
render(
boardTheme={brownBoardTheme}
/>,
document.getElementById("your-react-root")
);
``
#### pieceTheme
``javascript
import {
BlackPawn, BlackKnight, BlackBishop, BlackRook, BlackQueen, BlackKing,
WhitePawn, WhiteKnight, WhiteBishop, WhiteRook, WhiteQueen, WhiteKing
} from "./merida";
const meridaPieceTheme = {
"p": ({ board, position, dragSource }) =>
"n": () =>
"b": () =>
"q": () =>
"r": () =>
"k": () =>
"P": () =>
"N": () =>
"B": () =>
"Q": () =>
"R": () =>
"K": () =>
" ": () =>
render(
pieceTheme={meridaPieceTheme}
/>,
document.getElementById("your-react-root")
);
``
To allow for powerful customization for the appearance and behaviour of the ChessBoard there are
different render functions which can be overwritten.
#### renderBoard
Determines how the board wrapper is rendered.
#### renderSquare
Determines how the squares are rendered based on the boardTheme. This function will be called
once for each square on the board, starting with a8 and ending on h1. The result of renderPiece
and renderCoordinate is passed as parameters.
#### renderPiece
Determines how the pieces are rendered using the defined pieceTheme.
#### renderCoordinate
Determines how the coordinates are rendered.
#### renderPreviewPiece
Determines how a piece currently being dragged is rendered.
#### renderDroppableSquare
Determines how the drop-target wrapper around a Square should be rendered. This function is only
called if onMove is defined. The result of renderDraggablePiece is passed as a parameter.
#### renderDraggablePiece
Determines how the drag-source wrapper around a Piece should be rendered. This function is only
called if onMove` is defined.