Skip to content

Repository files navigation

@echecs/react-board

React chessboard component with drag & drop, animation, and theming. Bundled cburnett piece set, zero external dependencies beyond React.

Live demo

Installation

npm install @echecs/react-board
# or
pnpm add @echecs/react-board

Peer dependencies: React ≥18, @echecs/position ≥3.

Quick start

Minimal

import{Board}from'@echecs/react-board';exportfunctionApp(){return<Board/>;}

Full featured

import{useState}from'react';import{Board}from'@echecs/react-board';importtype{MoveEvent}from'@echecs/react-board';constSTARTING_FEN='rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';exportfunctionChessGame(){const[position,setPosition]=useState(STARTING_FEN);const[orientation,setOrientation]=useState<'white'|'black'>('white');functionhandleMove(event: MoveEvent): boolean{// validate and apply move, then update position// return false to reject the movereturntrue;}return(<divstyle={{width: 480}}><Boardanimatecoordinatesorientation={orientation}position={position}onMove={handleMove}/><buttononClick={()=>setOrientation((o)=>(o==='white' ? 'black' : 'white'))}>
Flip board
</button></div>);}

Props

PropTypeDefaultDescription
animatebooleantrueEnable CSS transition animation on piece moves
arrowsArrow[]Arrows to render on the board
childrenReact.ReactNodeContent rendered inside the board grid
coordinatesbooleantrueShow rank/file coordinate labels
highlightSquare[][]Squares to highlight with an overlay
interactivebooleantrueEnable drag & drop and click-to-move
legalMovesMap<Square, Square[]>Legal move map — restricts user interaction
onMove(move: MoveEvent) => booleanCalled on every attempted move; return false to reject
onSquareClick(square: Square) => voidCalled when a square is clicked
orientation'white' | 'black''white'Which side is at the bottom
piecesPieceSetDEFAULT_PIECESCustom piece component record
positionstring | Map<Square, Piece>starting positionFEN string or position map
turn'white' | 'black'Restrict interaction to one colour's pieces

Theming

All visual styling is controlled via CSS custom properties. Set them on a parent element or directly on the board container to override defaults.

.my-board {
--board-dark-square:#b58863;
--board-light-square:#f0d9b5;
--board-highlight:rgba(20,85,30,0.5);
--board-legal-dot:rgba(0,0,0,0.3);
}
VariableDefaultDescription
--board-dark-square#779952Dark square colour
--board-light-square#edeed1Light square colour
--board-highlightrgba(255, 255, 0, 0.4)Highlight overlay colour
--board-legal-dotrgba(0, 0, 0, 0.2)Legal move dot colour
--board-coordinate-on-light#779952Coordinate text on light squares
--board-coordinate-on-dark#edeed1Coordinate text on dark squares
--board-coordinate-weight600Coordinate font weight
--board-promotion-backgroundrgba(0, 0, 0, 0.6)Promotion dialog background
--board-drag-shadowdrop-shadow(0 4px 8px rgba(0,0,0,0.4))Drag ghost filter
--board-piece-transitiontransform 200ms easePiece move animation transition

Custom pieces

Supply a PieceSet record mapping piece keys to image URLs (any format the browser can render as a CSS background-image — data URIs, SVG files, PNGs):

import{DEFAULT_PIECES}from'@echecs/react-board';importtype{PieceSet}from'@echecs/react-board';constmyPieces: PieceSet={
...DEFAULT_PIECES,wP: '/assets/pieces/white-pawn.svg',};<Boardpieces={myPieces}/>;

Piece key format: 'b' | 'w' + 'B' | 'K' | 'N' | 'P' | 'Q' | 'R' (e.g. 'wK' = white king, 'bP' = black pawn).

Sound assets

Bundled move sounds (Lichess standard set, MIT licensed). Import via subpath:

importmoveSoundfrom'@echecs/react-board/sounds/move.mp3';importcaptureSoundfrom'@echecs/react-board/sounds/capture.mp3';importcastleSoundfrom'@echecs/react-board/sounds/castle.mp3';importcheckSoundfrom'@echecs/react-board/sounds/check.mp3';importgameEndSoundfrom'@echecs/react-board/sounds/game-end.mp3';
Subpath exportDescription
@echecs/react-board/sounds/move.mp3Standard piece move
@echecs/react-board/sounds/capture.mp3Piece capture
@echecs/react-board/sounds/castle.mp3Castling move
@echecs/react-board/sounds/check.mp3Check
@echecs/react-board/sounds/game-end.mp3Game over

Promotion dialog

PromotionDialog is exported separately for cases where you manage promotion state yourself:

import{PromotionDialog}from'@echecs/react-board';<PromotionDialogcolor="white"squareSize={60}onSelect={(piece)=>console.log(piece)}// 'queen' | 'rook' | 'bishop' | 'knight'onCancel={()=>console.log('cancelled')}/>;

PromotionDialog props

PropTypeDefaultDescription
color'white' | 'black'Which colour is promoting
onCancel() => voidOptional. Called when dismissed
onSelect(piece: PromotionPiece) => voidCalled when a piece is clicked
piecesPieceSetDEFAULT_PIECESPiece component set
squareSizenumberSquare size in pixels

API reference

squareCoords(square, orientation)

Returns { col, row } — 1-based CSS grid coordinates for square given the board orientation.

import{squareCoords}from'@echecs/react-board';squareCoords('e4','white');// { col: 5, row: 5 }squareCoords('e4','black');// { col: 4, row: 4 }

Exported types

TypeDescription
Annotations{ arrows: Arrow[]; circles: Circle[] } — drawable annotation state
Arrow{ from: Square; to: Square; kind: ArrowKind } — arrow descriptor
ArrowKind'alternative' | 'capture' | 'danger' | 'move' — annotation colour
BoardPropsAll props accepted by <Board />
Circle{ square: Square; kind: ArrowKind } — circle annotation descriptor
Color'black' | 'white' — piece colour (from @echecs/position)
File'a' | 'b' | … | 'h' — board file (from @echecs/position)
MoveEvent{ from, to, capture, promotion? } — passed to onMove
Piece{ color: Color; type: PieceType } — piece on a square
PieceKeyUnion of all 12 piece keys ('wK', 'bP', …)
PieceSetRecord<PieceKey, string> — maps piece keys to image URLs
PieceType'bishop' | 'king' | … | 'rook' — piece type (from @echecs/position)
PromotionDialogPropsAll props accepted by <PromotionDialog />
PromotionPiece'bishop' | 'knight' | 'queen' | 'rook' — promotable piece
Rank'1' | '2' | … | '8' — board rank (from @echecs/position)
Square`${File}${Rank}` — board square (from @echecs/position)
SquareCoords{ col: number; row: number } — return type of squareCoords

License

MIT

About

React chessboard component with drag & drop, animation, and theming. Bundled cburnett piece set, zero external dependencies.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages