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

v0.1.2

Published

A lightweight, simple, and high-performing chessboard for React Native

Downloads

149

Readme

A lightweight, simple, and high-performing chessboard for React Native.

Deeply inspired by the Chess Youtube Episode from the series "Can it be done in React Native?" made by William Candillon.

Disclaimer

If you want this package in production, use it with caution.

Installation

You need to have already installed the following packages:

Open a Terminal in your project's folder and install the library using yarn:

yarn add react-native-chessboard

or with npm:

npm install react-native-chessboard

Usage

import Chessboard from 'react-native-chessboard';

const App = () => (
  <View
    style={{
        flex: 1, 
        alignItems: 'center',
        justifyContent: 'center'
    }}
  >
    <Chessboard/>
  </View>
);

Properties

gestureEnabled?: boolean

Enables gestures for chess pieces.

Default: true


fen?: string

Indicates the initial fen position of the chessboard.


withLetters?: boolean

Decides whether or not to show the letters on the bottom horizontal axis of the chessboard.

Default: true


withNumbers?: boolean

Decides whether or not to show the numbers on the left vertical axis of the chessboard.

Default: true


boardSize?: number

Indicates the chessboard width and height.

Default: Math.floor(SCREEN_WIDTH / 8) * 8


renderPiece?: (piece: PieceType) => React.ReactElement | null

It gives the possibility to customise the chessboard pieces.

In detail, it takes a PieceType as input, which is constructed as follows:

type Player = 'b' | 'w';
type Type = 'q' | 'r' | 'n' | 'b' | 'k' | 'p';
type PieceType = `${Player}${Type}`;

onMove?: (info: ChessMoveInfo) => void;

It's a particularly useful callback if you want to execute an instruction after a move.

import Chessboard from 'react-native-chessboard';

const App = () => (
  <View
    style={{
        flex: 1, 
        alignItems: 'center',
        justifyContent: 'center'
    }}
  >
    <Chessboard
        onMove={({ state }) => {
          if (state.in_checkmate) {
            console.log('Life goes on.');
          }
        }}
      />
  </View>
);

In detail, you can access these parameters:

  • in_check: boolean
  • in_checkmate: boolean
  • in_draw: boolean
  • in_stalemate: boolean
  • in_threefold_repetition: boolean
  • insufficient_material: boolean
  • game_over: boolean
  • fen: boolean

P.S: These parameters are the outputs given by the respective functions used by chess.js (on which the package is built).


colors?: ChessboardColorsType

Useful if you want to customise the default colors used in the chessboard.

Default:

  • black: '#62B1A8'
  • white: '#D9FDF8'
  • lastMoveHighlight: 'rgba(255,255,0, 0.5)'
  • checkmateHighlight: '#E84855'
  • promotionPieceButton: '#FF9B71'

durations?: { move?: number }

Useful if you want to customise the default durations used in the chessboard (in milliseconds).

Default:

  • move: 150

What if I want to move pieces around without gestures?

Fortunately, the package provides the possibility of passing a React Ref to the component.

The operations granted are:

move: ({ from: Square; to: Square; }) => Promise<Move | undefined> | undefined;

Very useful if you want to move the pieces on the chessboard programmatically.

import Chessboard, { ChessboardRef } from 'react-native-chessboard';

const App = () => {
  const chessboardRef = useRef<ChessboardRef>(null);

  useEffect(() => {
    (async () => {
      await chessboardRef.current?.move({ from: 'e2', to: 'e4' });
      await chessboardRef.current?.move({ from: 'e7', to: 'e5' });
      await chessboardRef.current?.move({ from: 'd1', to: 'f3' });
      await chessboardRef.current?.move({ from: 'a7', to: 'a6' });
      await chessboardRef.current?.move({ from: 'f1', to: 'c4' });
      await chessboardRef.current?.move({ from: 'a6', to: 'a5' });
      await chessboardRef.current?.move({ from: 'f3', to: 'f7' });
    })();
  }, []);

  return (
      <View
        style={{
            flex: 1, 
            alignItems: 'center',
            justifyContent: 'center'
        }}
    >
        <Chessboard
            ref={chessboardRef}
            durations={{ move: 1000 }}
        />
    </View>
  )
};

highlight: (_: { square: Square; color?: string }) => void;

Highlight a square on the chessboard. The default color is 'rgba(255,255,0, 0.5)'.


resetAllHighlightedSquares: () => void


resetBoard: (fen?: string) => void;

Resets the chessboard from a fen position.


getState: () => ChessboardState;

Returns the current state of the chessboard (which can also be retrieved using the onMove callback).


Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT