npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-fen-chess-board

v2.0.6

Published

A react chess board controlled using FEN notation.

Downloads

51

Readme

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.