2.0.6 • Published 9 months ago

react-fen-chess-board v2.0.6

Weekly downloads
4
License
MIT
Repository
github
Last release
9 months ago

react-fen-chess-board

react-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.

Installation

npm install react-fen-chess-board

Rendering a static chess board

To render a static chess board you simply provide the FEN of the position you want to display.

import React from "react";
import { render } from "react-dom";
import { ChessBoard } from "react-fen-chess-board";

render(
  <ChessBoard
    // FEN for the starting position in chess
    fen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
  />,
  document.getElementById("your-react-root")
);

static board

Props

rotated (default: false)

Controls if the board should be rotated or not.

render(
  <ChessBoard
    fen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
    rotated
  />,
  document.getElementById("your-react-root")
);

rotated board

onMove (default: undefined)

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.

import React from "react";
import { render } from "react-dom";
import { ChessBoardDndProvider } from "react-fen-chess-board";
import MyChessBoard from "./MyChessBoard";

render(
  <ChessBoardDndProvider>
    <MyChessBoard />
  </ChessBoardDndProvider>,
  document.getElementById("your-react-root")
);

Theming

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

export const brownBoardTheme = {
  darkSquare: "#b58863",
  lightSquare: "#f0d9b5"
};

render(
  <ChessBoard
    fen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
    boardTheme={brownBoardTheme}
  />,
  document.getElementById("your-react-root")
);

boardTheme

pieceTheme

import {
  BlackPawn, BlackKnight, BlackBishop, BlackRook, BlackQueen, BlackKing,
  WhitePawn, WhiteKnight, WhiteBishop, WhiteRook, WhiteQueen, WhiteKing
} from "./merida";

const meridaPieceTheme = {
  "p": ({ board, position, dragSource }) => <BlackPawn/>,
  "n": () => <BlackKnight/>,
  "b": () => <BlackBishop/>,
  "q": () => <BlackQueen/>,
  "r": () => <BlackRook/>,
  "k": () => <BlackKing/>,
  "P": () => <WhitePawn/>,
  "N": () => <WhiteKnight/>,
  "B": () => <WhiteBishop/>,
  "Q": () => <WhiteQueen/>,
  "R": () => <WhiteRook/>,
  "K": () => <WhiteKing/>,
  " ": () => <div/>
};

render(
  <ChessBoard
    fen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
    pieceTheme={meridaPieceTheme}
  />,
  document.getElementById("your-react-root")
);

alt text

Render props

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.

2.0.3

9 months ago

2.0.2

10 months ago

2.0.5

9 months ago

2.0.4

9 months ago

2.0.6

9 months ago

2.0.1

10 months ago

2.0.0

10 months ago

1.0.1

4 years ago

1.0.0

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago