1.0.19 • Published 9 months ago

chessboard-rn v1.0.19

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

chessboard-rn

Basic, but flexible Chessboard component for React Native apps, suited to mobile devices.

The package is essentially react-chessboard refactored to work with React Native, albeit with fewer features. If your chess application is browser-based, consider using react-chessboard.

Installation

npm install chessboard-rn

Usage

chessboard-rn integrates with the brilliant chess.js; leverage chess.js functions via the onSquarePress prop to create unique (or bog-standard) chess applications. For example:

import React, { Component } from 'react';
import { View } from 'react-native';

import { ChessBoard } from 'chessboard-rn';
import { Chess } from 'chess.js'


class BasicExample extends Component {

  state = {
    fen: 'start',
    pieceSquare: '',
    square: '',
    history: [],
  };

  componentDidMount() {
    this.game = new Chess();
    this.setState({ history: this.game.history({ verbose: true }) });
  }

  onSquarePress = (square) => {
    let move = this.game.move({
      from: this.state.pieceSquare,
      to: square,
    });

    if (move === null) return;

    this.setState({
      fen: this.game.fen(),
      history: this.game.history({ verbose: true }),
      pieceSquare: '',
    });
  };

  render() {
    const { fen } = this.state;
    return (
      <View style={container}>
        <Chessboard
          position={fen}
          onSquarePress={this.onSquarePress}
        />
      </View>
    );
  }
}

const container = {
  display: 'flex',
  justifyContent: 'space-around',
  alignItems: 'center',
  flexWrap: 'wrap',
  width: '100%',
  aspectRatio: 1,
};


export default BasicExample;

Props

PropDefault ValueOptionsDescription
animationDurationnumber: 300Time in milliseconds for piece to slide to target square. Only used when the position is programmatically changed. If a new position is set before the animation is complete, the board will cancel the current animation and snap to the new position.
arePremovesAllowedboolean: falsetrue, falseWhether or not premoves are allowed.
boardOrientationstring: 'white''white', 'black'The orientation of the board, the chosen colour will be at the bottom of the board.
boardWidthnumber: 375The width of the board in pixels. Default value won't match device; pass Dimensions.get('screen').width
customBoardStyleobject: {}inline CSS stylingCustom board style object e.g. { borderRadius: 5, shadowColor: "#000", shadowOffset: { width: 5, height: 5 } }
customDarkSquareStyleobject: { backgroundColor: '#B58863' }inline CSS stylingCustom dark square style object.
customLightSquareStyleobject: { backgroundColor: '#F0D9B5' }inline CSS stylingCustom light square style object.
customPiecesobject: {}Custom pieces object where each key must match a corresponding chess piece (wP, wB, wN, wR, wQ, wK, bP, bB, bN, bR, bQ, bK). The value of each piece is a function that takes in some optional arguments to use and must return JSX to render. e.g. { wK: ({ squareWidth: number, targetSquare: string, sourceSquare: string }) => jsx }.
customPremoveDarkSquareStyleobject: { backgroundColor: '#A42323' }inline CSS stylingCustom premove dark square style object.
customPremoveLightSquareStyleobject: { backgroundColor: '#BD2828' }inline CSS stylingCustom premove light square style object.
customSquareStylesobject: {}inline CSS stylingCustom styles for all squares.
idnumber: 0string, numberBoard identifier, necessary if more than one board is mounted.
getPositionObjectfunction: (currentPosition) => {}User function that receives current position object when position changes.
moveHighlightColoursarray: []Custom colours for circular movehighlight markers. Multiple array elements display in a linear gradient (bottom to top).
moveHighlightSizenumber: 0Defines the diameter of move highlight makers, as a fraction of square width.
moveHighlightSquaresarray: []The specific squares (e.g., 'a4', 'a5') subject to move highlight styling.
onSquarePressfunction: (square) => {}Function that performs user-specified actions related to the square. NOTE: chess.js can ascertain the piece on the square.
positionstring: 'start''start', FEN string, { e5: 'wK', e4: 'wP', ... }FEN string or position object notating where the chess pieces are on the board. Start position can also be notated with the string: 'start'.
showBoardNotationboolean: truetrue, falseWhether or not to show the file and rank co-ordinates (a..h, 1..8).

Contributing

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

License

MIT


1.0.19

9 months ago

1.0.2

10 months ago

1.0.18

9 months ago

1.0.1

10 months ago

1.0.17

9 months ago

1.0.16

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

10 months ago

1.0.11

9 months ago

1.0.10

9 months ago

1.0.15

9 months ago

1.0.14

9 months ago

1.0.13

9 months ago

1.0.12

9 months ago

1.0.0

10 months ago

0.1.0

10 months ago