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-simple-chessboard

v1.1.0

Published

A minimal React chessboard component based on chessboard.js

Downloads

8

Readme

react-simple-chessboard

A minimal React chessboard component that uses the popular chessboard.js library.

react-simple-chessboard is "just a board," meaning that it doesn't provide move validation, PGN parsing, AI, etc. All it does is show a chessboard with the pieces in the given position, and optionally let you move pieces around with drag and drop.

Table of Contents

Requirements

  • jQuery 1.8.3+
  • React 16.8.0+

These are peer dependencies, so you need to install them yourself; they will not be installed automatically when you install react-simple-chessboard.

Installation

npm install --save react-simple-chessboard

Importing

import Chessboard from 'react-simple-chessboard'; // ES module
const Chessboard = require('react-simple-chessboard'); // CommonJS

CDN

If you don't want to import Chessboard into your application, you can load it from the CDN and use it globally via window.Chessboard:

<script src="https://unpkg.com/react-simple-chessboard"></script>

If you use the CDN, you must load jQuery and React first, e.g.

<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>

Usage

There are two ways to use the Chessboard component:

  1. As a controlled component, where the state is managed by its parent
  2. As an uncontrolled component, where it manages its own state

Controlled component

To use Chessboard as a controlled component, set the position property:

import React, { useState } from 'react';
import Chessboard from 'react-simple-chessboard';

const positions = [
    // 1. e4
    'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1',
    // undo
    'start',
    // 1. e4
    'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1',
    // 1. ... c5
    'rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2',
    // 2. Nf3
    'rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2'
];

const App = props => {
    const [position, setPosition] = useState('start');

    return (
        <div className="app">
            <Chessboard position={position} />
            <button onClick={() => setPosition(positions.shift())}>
                Move
            </button>
            <p>Position: {position}</p>
        </div>
    );
};

position should be a string in Forsyth-Edwards Notation (FEN) or the string 'start' (the starting position). When the position prop changes, the board updates to show the new position.

If drag-and-drop is enabled by setting the draggable property to true, you can use the onChange handler to keep your state variable in sync with the board position:

import React, { useState } from 'react';
import Chessboard from 'react-simple-chessboard';

const App = props => {
    const [position, setPosition] = useState('start');

    return (
        <div className="app">
            <Chessboard
                draggable={true}
                position={position}
                onChange={newPos => setPosition(newPos)}
            />
            <p>Position: {position}</p>
        </div>
    );
};

Uncontrolled component

To use Chessboard as an uncontrolled component, pass an initial position with the defaultPosition property:

import React from 'react';
import Chessboard from 'react-simple-chessboard';

const App = props => {
    return (
        <div className="app">
            <Chessboard draggable={true} defaultPosition="start" />
        </div>
    );
};

This simply draws a chessboard in the given position. If drag-and-drop is enabled by setting the draggable property to true, you can move pieces around on the board, but you can't access the board's state.

Integration with react-chess.js

You can integrate react-simple-chessboard with react-chess.js to get move validation and other features:

import React, { useState } from 'react';
import Chessboard from 'react-simple-chessboard';
import useChess from 'react-chess.js';

const moves = ['e4', 'e5', 'Ba8', 'Nf3'];

const App = (props) => {
    const { move, fen, reset, undo } = useChess();

    return (
        <div className="app">
            <Chessboard position={fen} />

            <button onClick={() => move(moves.shift())}>Move</button>
            <button onClick={undo}>Undo</button>
            <button onClick={reset}>Reset</button>
        </div>
    );
};

The third move in this example (Ba8) is illegal, so the board position will not change the third time you click the "Move" button. See the react-chess.js documentation for details.

See also

  • react-chess.js: React hook for integrating with the chess.js library, giving move validation, piece movement, and callbacks for legal moves, illegal moves, check, checkmate, etc.

  • Chessboard.jsx: Another "just a board" chessboard component for React, but with much more functionality than react-simple-chessboard, including multiple boards and customizable pieces. Unfortunately, it is currently unmaintained.

  • react-chess: Another React chessboard component with more features than react-simple-chessboard, but less than Chessboard.jsx. Also appears unmaintained.

License

react-simple-chessboard is released under the GPLv3 license.