GithubHelp home page GithubHelp logo

lostlivio / vue3-chessboard Goto Github PK

View Code? Open in Web Editor NEW

This project forked from qwerty084/vue3-chessboard

0.0 0.0 0.0 1.14 MB

Vue 3 chessboard component, built with lichess chessground and chess.js

Home Page: https://qwerty084.github.io/vue3-chessboard/

License: GNU General Public License v3.0

TypeScript 95.19% Vue 4.81%

vue3-chessboard's Introduction

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 a class based 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 the API is 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;
}>();

vue3-chessboard's People

Contributors

qwerty084 avatar dependabot[bot] avatar gavin-lb avatar ddbogdanov avatar chew avatar mateusocb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.