Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vue3-chessboard

qwerty084347GPL-3.01.3.3TypeScript support: included

vue3-chessboard is a component library for creating chess web apps with vue3

vue, chessboard, chess, vue3, lichess, chessground, chess.js, typescript

readme

vue3-chessboard

Unit Tests CodeQL npm

vue3-chesssboard is a component library for vue 3 built with lichess chessground & chess.js.

Chessboard

You can find a demo and the full library documentation here.

Table of contents

Features:

  • Customizable chessboard
  • Custom Events for check, checkmate, draw, move etc.
  • Undo Moves, reset game, get opening name, get current fen...
  • Promotion dialog window
  • Fully typed API/Custom Events

Installation

npm i vue3-chessboard

Usage

Basic Example (Composition API)

<script setup>
import { TheChessboard } from 'vue3-chessboard';
import 'vue3-chessboard/style.css';
</script>

<template>
  <TheChessboard />
</template>

Basic Example (Options API)

<template>
  <TheChessboard />
</template>

<script>
import { TheChessboard } from 'vue3-chessboard';
import 'vue3-chessboard/style.css';

export default {
  components: {
    TheChessboard,
  },
};
</script>

Example Typescript Component

<script setup lang="ts">
import { TheChessboard } from 'vue3-chessboard';
import 'vue3-chessboard/style.css';
import type { BoardApi, BoardConfig } from 'vue3-chessboard';

let boardAPI: BoardApi;
const boardConfig: BoardConfig = {
  coordinates: false,
};

function handleCheckmate(isMated: string) {
  if (isMated === 'w') {
    alert('Black wins!');
  } else {
    alert('White wins!');
  }
}
</script>

<template>
  <section>
    <div>
      <button @click="boardAPI?.toggleOrientation()">
        Toggle orientation
      </button>
      <button @click="boardAPI?.resetBoard()">Reset</button>
      <button @click="boardAPI?.undoLastMove()">Undo</button>
      <button @click="boardAPI?.toggleMoves()">Threats</button>
    </div>
    <TheChessboard
      :board-config="boardConfig"
      @board-created="(api) => (boardAPI = api)"
      @checkmate="handleCheckmate"
    />
  </section>
</template>

Example Javascript Component

<script setup>
import { TheChessboard } from 'vue3-chessboard';
import 'vue3-chessboard/style.css';

let boardAPI;
const boardConfig = {
  coordinates: false,
};

function handleCheckmate(isMated) {
  if (isMated === 'w') {
    alert('Black wins!');
  } else {
    alert('White wins!');
  }
}
</script>

<template>
  <section>
    <div>
      <button @click="boardAPI?.toggleOrientation()">
        Toggle orientation
      </button>
      <button @click="boardAPI?.resetBoard()">Reset</button>
      <button @click="boardAPI?.undoLastMove()">Undo</button>
      <button @click="boardAPI?.toggleMoves()">Threats</button>
    </div>
    <TheChessboard
      :board-config="boardConfig"
      @board-created="(api) => (boardAPI = api)"
      @checkmate="handleCheckmate"
    />
  </section>
</template>

Docs

You can find the full documentation here.

Setup:

  • Just install the package and import the component in your project, like shown above.
  • Don't forget to include the stylesheet:
    import 'vue3-chessboard/style.css';

Chessboard config

You can pass a config object to the chessboard component to modify the board to your needs, as a prop (:board-config). The config object is optional and will be merged with the default config. The default config is based on the lichess board config. Additionally custom callback functions can be passed to the component. For example a custom function can be run on piece selection or after each move.

API

The chessboard component provides an API to interact with the chessboard. The API is accessible via the board-created event. The event will be emitted when the chessboard is created and ready to use.

Available methods:

For a full list of available methods please visit the documentation.

Custom Events

You can listen for events on the chessboard component. The following events are available:

  • boardCreated - Emitted when the chessboard is created and the API is ready to use
  • checkmate - Emitted when a player is checkmated
  • stalemate - Emitted when a player is stalemated
  • draw - Emitted when the game is drawn
  • check - Emitted when a player is checked
  • promotion - Emitted when a player promotes
  • move - Emitted when a player makes a move
const emit = defineEmits<{
  (e: 'boardCreated', boardApi: BoardApi): void;
  (e: 'checkmate', isMated: PieceColor): void;
  (e: 'stalemate'): void;
  (e: 'draw'): void;
  (e: 'check', isInCheck: PieceColor): void;
  (e: 'promotion', promotion: PromotionEvent): void;
  (e: 'move', move: MoveEvent): void;
}>();

changelog

v1.3.2 (2023-12-10)

  • revert style export changes

v1.3.1 (2023-12-10)

CAUTION: this release is broken, use >= v1.3.2 instead

  • fix style export in package.json (#239)

v1.3.0 (2023-12-10)

CAUTION: this release is broken, use >= v1.3.2 instead

Bug fixes

  • fix putPiece with movable set to free from jumping back to previous position (#236)
  • fixed promotion dialog with multiple board instances (#233)

Other

v1.2.4 (2023-10-16)

Features

  • added a new method setPgnInfo for setting a header in the pgn (#224) @chew

v1.2.3 (2023-08-27)

Bug fixes

  • fix resetBoard() method when a piece is selected(#221)

v1.2.2 (2023-08-27)

Dependencies

  • update chessground to latest version (#219)

v1.2.1 (2023-08-02)

Features

Bug fixes

  • fixed config option movable.free = true, not being able to make moves after the first one (promotions are still buggy in this mode)

v1.2.0 (2023-07-03)

Features

  • reactive-config - the board config object can now be made reactive (#190) @gavin-lb. Please visit the documentation for more information.
  • history-viewer - the board component now contains a history viewer (#200) @gavin-lb. Please visit the documentation for more information.

Other

  • boardAPI - the board api should not be used with a ref, as shown in earlier versions in the documentation. Please refer to the new documentation. Since using a ref can cause performance issues.

1.1.8 (2023-06-04)

Features

  • new getIsCheck() method (9b9a4ac)

Bug fixes

  • fix legal move overwrite after setting the player color (28448be) @ddbogdanov
  • emit move event after check and checkmate (b437455)
  • fix config merge when player color is set (9b9a4ac)
  • fix captured pawn visible after en passant with move method (cd64742)
  • fix clearBord() method not working (9b9a4ac)

Other

  • updated dependencies
  • added more test cases (9b9a4ac)

1.1.7 (2023-05-20)

Features

  • multiplayer support through setting the player color as a prop to the board component (841dfac) @ddbogdanov

1.1.6 (2023-05-13)

Bug fixes

1.1.5 (2023-03-25)

  • update all dependencies

1.1.4 (2023-03-25)

Bug Fixes

  • emit check event on pgn load when in check (1c9cebf)

Features

  • improve promotion animation (c22b6a3)
  • added new turn number method (ce6fdce)

1.1.3 (2023-02-25)

  • remove event data from draw and stalemate events
  • update foreach to for of loop
  • improve coordinates

1.1.2 (2023-02-19)

Breaking

  • if you are using TypeScript you need to change the type import "ChessboardAPI" to "BoardApi" (fbcd3c9)

Bug Fixes

  • export all available types from the library (b4d1315)

Other




1.1.1 (2023-02-18)

Bug Fixes




1.1.0 (2023-02-17)

Features

Bug Fixes

  • fixed bug in inital fen (b272ec7)

Other

  • dependency version updates



1.0.0 (2023-02-05)

v1.0.0. includes some breaking changes. Only breaking changes will be listed here. Refer to the new documentation to update your app to v1.0.0

  • callback functions as props have been removed. Take a look at the documentation to see how to register callbacks in v1.0.0.
  • board and game from the boardAPI have been made private. You need to use the corresponding method to achieve the same result. Visit the docs to get an overview of all available methods.
  • If you encounter any issues visit the docs or open an issue.

0.0.9 (2022-11-13)

Bug Fixes

  • board: fix coordinates when board orientation is toggled (85ec6d5)
  • board: remove duplicate styles (85ec6d5)

Dev

  • implement new tsconfig structure & update testing suite/ci (6a4c2be)
  • add html and App.vue for local testing (34fb7f0)



0.0.8 (2022-10-26)

Bug Fixes

  • board: load promotion dialog synchronously (374ed67)
  • boardAPI: fix undo method (40bb425)



0.0.7 (2022-10-24)

Bug Fixes

  • board: fix undo while promoting (90c36de)
  • board: fix reselect of pieces (9dde375)
  • board: fix undo not being animated (09c888c)

Features

  • board: add callback functions for piece select and after move as props to board component (d6dc946)
  • boardAPI: add method getOpeningName() to retrieve opening name for the current position from lichess api (d8457c5)
  • boardAPI: add method getOpeningDescription() to retrieve opening description for the current position from wikibooks.org api (d8457c5)
  • boardAPI: add makeMove() method to make a move programmatically on the board (8215b15), closes #18

Dev

  • setup vitest and added some tests
  • setup github actions for pull requests (runs type check, build, eslint and tests)
  • code base cleaned up

IMPORTANT CHANGES

  • pinia is no longer required, it wasn't used extensivly and added unnecessary bloat (9a177c2)


0.0.6 (2022-10-18)

Bug Fixes

  • boardAPI: reset selected square when resetBoard() is being called (d8457c5)
  • board: fix coordinates when board orientation is toggled (d8457c5)

Features

  • boardAPI: added method to get the opening name for the current position from the lichess public api (d8457c5)
  • board: added lichess svgs for pieces (266ebbe)