@react-chess-tools/react-chess-game 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +569 -0
- package/dist/index.cjs +265 -184
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +48 -4
- package/dist/index.d.ts +48 -4
- package/dist/index.js +271 -185
- package/dist/index.js.map +1 -1
- package/package.json +3 -1
- package/src/components/ChessGame/ChessGame.stories.helpers.tsx +181 -0
- package/src/components/ChessGame/ChessGame.stories.tsx +574 -35
- package/src/components/ChessGame/Clock/index.tsx +174 -0
- package/src/components/ChessGame/Theme.stories.tsx +2 -2
- package/src/components/ChessGame/index.ts +2 -0
- package/src/components/ChessGame/parts/Board.tsx +214 -205
- package/src/components/ChessGame/parts/KeyboardControls.tsx +9 -0
- package/src/components/ChessGame/parts/Root.tsx +15 -1
- package/src/components/ChessGame/parts/Sounds.tsx +9 -0
- package/src/components/ChessGame/parts/__tests__/Board.test.tsx +122 -0
- package/src/components/ChessGame/parts/__tests__/KeyboardControls.test.tsx +34 -0
- package/src/components/ChessGame/parts/__tests__/Root.test.tsx +50 -0
- package/src/components/ChessGame/parts/__tests__/Sounds.test.tsx +22 -0
- package/src/hooks/useChessGame.ts +50 -12
- package/src/index.ts +10 -0
- package/src/theme/__tests__/context.test.tsx +0 -15
- package/README.MD +0 -190
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @react-chess-tools/react-chess-game
|
|
2
2
|
|
|
3
|
+
## 1.0.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 93e1029: feat: add chess clock
|
|
8
|
+
- Updated dependencies [93e1029]
|
|
9
|
+
- @react-chess-tools/react-chess-clock@1.0.1
|
|
10
|
+
|
|
11
|
+
## 1.0.1
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- f9b7665: feat: add ref forwarding, HTML attributes, and improved asChild pattern
|
|
16
|
+
|
|
3
17
|
## 1.0.0
|
|
4
18
|
|
|
5
19
|
### Major Changes
|
package/README.md
ADDED
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h1>@react-chess-tools/react-chess-game</h1>
|
|
3
|
+
<p>An easy-customizable, ready-to-use chess game component for React</p>
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@react-chess-tools/react-chess-game)
|
|
6
|
+
[](https://www.npmjs.com/package/@react-chess-tools/react-chess-game)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
[](https://www.typescriptlang.org/)
|
|
9
|
+
|
|
10
|
+
<p>
|
|
11
|
+
<a href="https://github.com/Clariity/react-chessboard">react-chessboard</a> +
|
|
12
|
+
<a href="https://github.com/jhlywa/chess.js">chess.js</a> + nice defaults
|
|
13
|
+
</p>
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
## Table of Contents
|
|
17
|
+
|
|
18
|
+
- [Overview](#overview)
|
|
19
|
+
- [Features](#features)
|
|
20
|
+
- [Installation](#installation)
|
|
21
|
+
- [Quick Start](#quick-start)
|
|
22
|
+
- [Demo](#demo)
|
|
23
|
+
- [API Reference](#api-reference)
|
|
24
|
+
- [ChessGame.Root](#chessgameroot)
|
|
25
|
+
- [ChessGame.Board](#chessgameboard)
|
|
26
|
+
- [ChessGame.Sounds](#chessgamesounds)
|
|
27
|
+
- [ChessGame.KeyboardControls](#chessgamekeyboardcontrols)
|
|
28
|
+
- [ChessGame.Clock](#chessgameclock)
|
|
29
|
+
- [Hooks](#hooks)
|
|
30
|
+
- [useChessGameContext](#usechessgamecontext)
|
|
31
|
+
- [Examples](#examples)
|
|
32
|
+
- [License](#license)
|
|
33
|
+
|
|
34
|
+
## Overview
|
|
35
|
+
|
|
36
|
+
`@react-chess-tools/react-chess-game` is a React component that bridges [chess.js](https://github.com/jhlywa/chess.js) with [react-chessboard](https://github.com/Clariity/react-chessboard) to offer a full-featured, ready-to-integrate chess board experience.
|
|
37
|
+
|
|
38
|
+
Built using a compound component pattern (similar to [Radix UI](https://www.radix-ui.com/)), it provides a `ChessGameContext` that you can use to customize and enhance the game while maintaining sensible defaults.
|
|
39
|
+
|
|
40
|
+
The package now includes integrated chess clock functionality powered by [`@react-chess-tools/react-chess-clock`](https://www.npmjs.com/package/@react-chess-tools/react-chess-clock).
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
- **Move-by-click** - Click to select and move pieces
|
|
45
|
+
- **Sound effects** - Built-in sounds for moves, captures, check, and game over
|
|
46
|
+
- **Square highlighting** - Visual feedback for valid moves and last move
|
|
47
|
+
- **Keyboard controls** - Navigate through game history with arrow keys
|
|
48
|
+
- **Integrated Chess Clock** - Built-in clock support with multiple timing methods
|
|
49
|
+
- **Full game state** - Access to check, checkmate, stalemate, draw detection
|
|
50
|
+
- **TypeScript** - Full TypeScript support with comprehensive type definitions
|
|
51
|
+
- **Customizable** - Override any default with your own implementation
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
npm install @react-chess-tools/react-chess-game
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
yarn add @react-chess-tools/react-chess-game
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pnpm add @react-chess-tools/react-chess-game
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Quick Start
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
import { ChessGame } from "@react-chess-tools/react-chess-game";
|
|
71
|
+
|
|
72
|
+
function App() {
|
|
73
|
+
return (
|
|
74
|
+
<ChessGame.Root>
|
|
75
|
+
<ChessGame.Board />
|
|
76
|
+
<ChessGame.Sounds />
|
|
77
|
+
<ChessGame.KeyboardControls />
|
|
78
|
+
</ChessGame.Root>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
With a chess clock:
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
import { ChessGame } from "@react-chess-tools/react-chess-game";
|
|
87
|
+
|
|
88
|
+
function App() {
|
|
89
|
+
return (
|
|
90
|
+
<ChessGame.Root timeControl={{ time: "5+3" }}>
|
|
91
|
+
<ChessGame.Clock.Display color="white" />
|
|
92
|
+
<ChessGame.Board />
|
|
93
|
+
<ChessGame.Clock.Display color="black" />
|
|
94
|
+
<ChessGame.Clock.PlayPause />
|
|
95
|
+
</ChessGame.Root>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Demo
|
|
101
|
+
|
|
102
|
+
Visit the [live demo](https://react-chess-tools.vercel.app/) to see the component in action.
|
|
103
|
+
|
|
104
|
+
## API Reference
|
|
105
|
+
|
|
106
|
+
### ChessGame.Root
|
|
107
|
+
|
|
108
|
+
The root component that provides `ChessGameContext` to all child components. It instantiates a `Chess` instance using the `fen` prop and optionally sets up a chess clock.
|
|
109
|
+
|
|
110
|
+
**Note:** This is a logic-only component (Context Provider). It does not render any DOM elements.
|
|
111
|
+
|
|
112
|
+
#### Props
|
|
113
|
+
|
|
114
|
+
| Name | Type | Default | Description |
|
|
115
|
+
| ------------------ | ----------------------- | ----------------- | -------------------------------------------------------- |
|
|
116
|
+
| `children` | `ReactNode` | - | Child components |
|
|
117
|
+
| `fen` | `string` | Starting position | Initial FEN string for the chess game |
|
|
118
|
+
| `orientation` | `"w" \| "b"` | `"w"` | Board orientation (white or black at bottom) |
|
|
119
|
+
| `theme` | `PartialChessGameTheme` | - | Optional theme configuration |
|
|
120
|
+
| `timeControl` | `TimeControlConfig` | - | Optional clock configuration to enable chess clock |
|
|
121
|
+
| `autoSwitchOnMove` | `boolean` | `true` | Auto-switch clock on move (when timeControl is provided) |
|
|
122
|
+
|
|
123
|
+
#### Example
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
<ChessGame.Root
|
|
127
|
+
fen="rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"
|
|
128
|
+
orientation="b"
|
|
129
|
+
timeControl={{ time: "10+5" }}
|
|
130
|
+
>
|
|
131
|
+
<ChessGame.Board />
|
|
132
|
+
</ChessGame.Root>
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### ChessGame.Board
|
|
136
|
+
|
|
137
|
+
The main chess board component. Renders the board and pieces using `react-chessboard` v5.
|
|
138
|
+
|
|
139
|
+
Supports **ref forwarding** and all standard **HTML div attributes** (className, style, id, data-_, aria-_, etc.).
|
|
140
|
+
|
|
141
|
+
#### Props
|
|
142
|
+
|
|
143
|
+
| Name | Type | Description |
|
|
144
|
+
| ----------- | -------------------------------- | ------------------------------------------------------------------------- |
|
|
145
|
+
| `options` | `ChessboardOptions` | Options forwarded to `react-chessboard`. Your values merge with defaults. |
|
|
146
|
+
| `ref` | `Ref<HTMLDivElement>` | Forwarded ref to the wrapper div element |
|
|
147
|
+
| `className` | `string` | Custom CSS class names |
|
|
148
|
+
| `style` | `CSSProperties` | Custom inline styles |
|
|
149
|
+
| `...` | `HTMLAttributes<HTMLDivElement>` | All standard HTML div attributes |
|
|
150
|
+
|
|
151
|
+
#### Example
|
|
152
|
+
|
|
153
|
+
```tsx
|
|
154
|
+
<ChessGame.Root>
|
|
155
|
+
<ChessGame.Board
|
|
156
|
+
options={{
|
|
157
|
+
squareStyles: { e4: { boxShadow: "inset 0 0 0 2px #4f46e5" } },
|
|
158
|
+
onPieceDrop: ({ sourceSquare, targetSquare }) => {
|
|
159
|
+
console.log(`Move: ${sourceSquare} -> ${targetSquare}`);
|
|
160
|
+
return true;
|
|
161
|
+
},
|
|
162
|
+
showNotation: true,
|
|
163
|
+
animationDurationInMs: 300,
|
|
164
|
+
}}
|
|
165
|
+
className="my-custom-board"
|
|
166
|
+
style={{ borderRadius: "8px" }}
|
|
167
|
+
id="game-board"
|
|
168
|
+
data-testid="chess-board"
|
|
169
|
+
/>
|
|
170
|
+
</ChessGame.Root>
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### ChessGame.Sounds
|
|
174
|
+
|
|
175
|
+
Provides sound effects for the chess game. Uses built-in sounds by default, but custom sounds can be provided as base64-encoded strings.
|
|
176
|
+
|
|
177
|
+
**Note:** This is a logic-only component that returns `null`. It sets up audio functionality via hooks.
|
|
178
|
+
|
|
179
|
+
#### Props
|
|
180
|
+
|
|
181
|
+
| Name | Type | Default | Description |
|
|
182
|
+
| -------- | -------------------------------- | ------- | ------------------------------------------------------------------------- |
|
|
183
|
+
| `sounds` | `Partial<Record<Sound, string>>` | - | Custom sounds configuration. Keys: `move`, `capture`, `check`, `gameOver` |
|
|
184
|
+
|
|
185
|
+
#### Example
|
|
186
|
+
|
|
187
|
+
```tsx
|
|
188
|
+
<ChessGame.Root>
|
|
189
|
+
<ChessGame.Sounds
|
|
190
|
+
sounds={{
|
|
191
|
+
move: customMoveSound,
|
|
192
|
+
capture: customCaptureSound,
|
|
193
|
+
}}
|
|
194
|
+
/>
|
|
195
|
+
<ChessGame.Board />
|
|
196
|
+
</ChessGame.Root>
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### ChessGame.KeyboardControls
|
|
200
|
+
|
|
201
|
+
Enables keyboard navigation through the game history.
|
|
202
|
+
|
|
203
|
+
**Note:** This is a logic-only component that returns `null`. It sets up keyboard event listeners via hooks.
|
|
204
|
+
|
|
205
|
+
#### Props
|
|
206
|
+
|
|
207
|
+
| Name | Type | Default | Description |
|
|
208
|
+
| ---------- | ------------------ | ------------------------- | --------------------------------------------- |
|
|
209
|
+
| `controls` | `KeyboardControls` | `defaultKeyboardControls` | Object mapping key names to handler functions |
|
|
210
|
+
|
|
211
|
+
**Default Controls:**
|
|
212
|
+
|
|
213
|
+
- `ArrowLeft` - Go to previous move
|
|
214
|
+
- `ArrowRight` - Go to next move
|
|
215
|
+
- `ArrowUp` - Go to starting position
|
|
216
|
+
- `ArrowDown` - Go to latest move
|
|
217
|
+
|
|
218
|
+
#### Example
|
|
219
|
+
|
|
220
|
+
```tsx
|
|
221
|
+
<ChessGame.Root>
|
|
222
|
+
<ChessGame.KeyboardControls
|
|
223
|
+
controls={{
|
|
224
|
+
ArrowLeft: (ctx) => ctx.methods.goToPreviousMove(),
|
|
225
|
+
ArrowRight: (ctx) => ctx.methods.goToNextMove(),
|
|
226
|
+
Home: (ctx) => ctx.methods.goToStart(),
|
|
227
|
+
End: (ctx) => ctx.methods.goToEnd(),
|
|
228
|
+
}}
|
|
229
|
+
/>
|
|
230
|
+
<ChessGame.Board />
|
|
231
|
+
</ChessGame.Root>
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### ChessGame.Clock
|
|
235
|
+
|
|
236
|
+
Integrated chess clock components. When `timeControl` is provided to `ChessGame.Root`, these components become available to display and control the clock.
|
|
237
|
+
|
|
238
|
+
The clock automatically switches when moves are made on the board (can be disabled with `autoSwitchOnMove={false}`).
|
|
239
|
+
|
|
240
|
+
**Note:** These are wrapper components that use the clock state from `ChessGame.Root`. No need for a separate `ChessClock.Root` provider.
|
|
241
|
+
|
|
242
|
+
#### Available Components
|
|
243
|
+
|
|
244
|
+
| Component | Description |
|
|
245
|
+
| --------------------------- | -------------------------------------------- |
|
|
246
|
+
| `ChessGame.Clock.Display` | Displays the current time for a player |
|
|
247
|
+
| `ChessGame.Clock.Switch` | Button to manually switch the active clock |
|
|
248
|
+
| `ChessGame.Clock.PlayPause` | Button to start, pause, and resume the clock |
|
|
249
|
+
| `ChessGame.Clock.Reset` | Button to reset the clock |
|
|
250
|
+
|
|
251
|
+
#### ChessGame.Clock.Display
|
|
252
|
+
|
|
253
|
+
Displays the current time for a player.
|
|
254
|
+
|
|
255
|
+
```tsx
|
|
256
|
+
<ChessGame.Clock.Display
|
|
257
|
+
color="white"
|
|
258
|
+
format="auto"
|
|
259
|
+
style={{ fontFamily: "monospace", fontSize: "24px" }}
|
|
260
|
+
/>
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
**Props:**
|
|
264
|
+
|
|
265
|
+
| Name | Type | Default | Description |
|
|
266
|
+
| ------------ | ------------------------------------------- | -------- | ---------------------------------- |
|
|
267
|
+
| `color` | `"white" \| "black"` | - | Player color to display (required) |
|
|
268
|
+
| `format` | `"auto" \| "mm:ss" \| "ss.d" \| "hh:mm:ss"` | `"auto"` | Time format |
|
|
269
|
+
| `formatTime` | `(milliseconds: number) => string` | - | Custom time formatting function |
|
|
270
|
+
| `...` | `HTMLAttributes<HTMLDivElement>` | - | All standard HTML div attributes |
|
|
271
|
+
|
|
272
|
+
#### ChessGame.Clock.Switch
|
|
273
|
+
|
|
274
|
+
A button that manually switches the active player's clock.
|
|
275
|
+
|
|
276
|
+
```tsx
|
|
277
|
+
<ChessGame.Clock.Switch>Switch Turn</ChessGame.Clock.Switch>
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
**Props:**
|
|
281
|
+
|
|
282
|
+
| Name | Type | Default | Description |
|
|
283
|
+
| --------- | ----------------------------------------- | ------- | -------------------------------------- |
|
|
284
|
+
| `asChild` | `boolean` | `false` | Render as child element (slot pattern) |
|
|
285
|
+
| `...` | `ButtonHTMLAttributes<HTMLButtonElement>` | - | All standard HTML button attributes |
|
|
286
|
+
|
|
287
|
+
#### ChessGame.Clock.PlayPause
|
|
288
|
+
|
|
289
|
+
A button to start, pause, and resume the clock.
|
|
290
|
+
|
|
291
|
+
```tsx
|
|
292
|
+
<ChessGame.Clock.PlayPause
|
|
293
|
+
startContent="Start Game"
|
|
294
|
+
pauseContent="Pause"
|
|
295
|
+
resumeContent="Resume"
|
|
296
|
+
/>
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
**Props:**
|
|
300
|
+
|
|
301
|
+
| Name | Type | Default | Description |
|
|
302
|
+
| ----------------- | ----------------------------------------- | ------------- | -------------------------------------- |
|
|
303
|
+
| `startContent` | `ReactNode` | `"Start"` | Content shown when clock is idle |
|
|
304
|
+
| `pauseContent` | `ReactNode` | `"Pause"` | Content shown when clock is running |
|
|
305
|
+
| `resumeContent` | `ReactNode` | `"Resume"` | Content shown when clock is paused |
|
|
306
|
+
| `delayedContent` | `ReactNode` | `"Start"` | Content shown when clock is delayed |
|
|
307
|
+
| `finishedContent` | `ReactNode` | `"Game Over"` | Content shown when clock is finished |
|
|
308
|
+
| `asChild` | `boolean` | `false` | Render as child element (slot pattern) |
|
|
309
|
+
| `...` | `ButtonHTMLAttributes<HTMLButtonElement>` | - | All standard HTML button attributes |
|
|
310
|
+
|
|
311
|
+
#### ChessGame.Clock.Reset
|
|
312
|
+
|
|
313
|
+
A button that resets the clock.
|
|
314
|
+
|
|
315
|
+
```tsx
|
|
316
|
+
<ChessGame.Clock.Reset>Reset</ChessGame.Clock.Reset>;
|
|
317
|
+
|
|
318
|
+
{
|
|
319
|
+
/* Reset with new time control */
|
|
320
|
+
}
|
|
321
|
+
<ChessGame.Clock.Reset timeControl="10+5">
|
|
322
|
+
Change to 10+5
|
|
323
|
+
</ChessGame.Clock.Reset>;
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
**Props:**
|
|
327
|
+
|
|
328
|
+
| Name | Type | Description |
|
|
329
|
+
| ------------- | ----------------------------------------- | --------------------------------------------- |
|
|
330
|
+
| `timeControl` | `TimeControlInput` | New time control to apply on reset (optional) |
|
|
331
|
+
| `asChild` | `boolean` | Render as child element (slot pattern) |
|
|
332
|
+
| `...` | `ButtonHTMLAttributes<HTMLButtonElement>` | All standard HTML button attributes |
|
|
333
|
+
|
|
334
|
+
For more details on time control formats, timing methods, and clock start modes, see the [`@react-chess-tools/react-chess-clock` documentation](https://www.npmjs.com/package/@react-chess-tools/react-chess-clock).
|
|
335
|
+
|
|
336
|
+
## Hooks
|
|
337
|
+
|
|
338
|
+
### useChessGameContext
|
|
339
|
+
|
|
340
|
+
Access the chess game context from any child component.
|
|
341
|
+
|
|
342
|
+
```tsx
|
|
343
|
+
import { useChessGameContext } from "@react-chess-tools/react-chess-game";
|
|
344
|
+
|
|
345
|
+
function GameStatus() {
|
|
346
|
+
const { currentFen, info, methods, clock } = useChessGameContext();
|
|
347
|
+
|
|
348
|
+
return (
|
|
349
|
+
<div>
|
|
350
|
+
<p>Turn: {info.turn === "w" ? "White" : "Black"}</p>
|
|
351
|
+
{info.isCheck && <p>Check!</p>}
|
|
352
|
+
{info.isCheckmate && <p>Checkmate!</p>}
|
|
353
|
+
{clock && <p>White: {clock.times.white}ms</p>}
|
|
354
|
+
<button onClick={() => methods.flipBoard()}>Flip Board</button>
|
|
355
|
+
</div>
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
#### Return Values
|
|
361
|
+
|
|
362
|
+
| Name | Type | Description |
|
|
363
|
+
| ------------------ | ----------------------------- | ------------------------------------- |
|
|
364
|
+
| `game` | `Chess` | The underlying chess.js instance |
|
|
365
|
+
| `orientation` | `"w" \| "b"` | Current board orientation |
|
|
366
|
+
| `currentFen` | `string` | Current FEN string |
|
|
367
|
+
| `currentPosition` | `string` | Current position in game history |
|
|
368
|
+
| `currentMoveIndex` | `number` | Index of current move in history |
|
|
369
|
+
| `isLatestMove` | `boolean` | Whether viewing the latest position |
|
|
370
|
+
| `methods` | `Methods` | Methods to interact with the game |
|
|
371
|
+
| `info` | `Info` | Game state information |
|
|
372
|
+
| `clock` | `UseChessClockReturn \| null` | Clock state (if timeControl provided) |
|
|
373
|
+
|
|
374
|
+
#### Methods
|
|
375
|
+
|
|
376
|
+
| Method | Type | Description |
|
|
377
|
+
| ------------------ | ------------------------------------------------ | --------------------------------------- |
|
|
378
|
+
| `makeMove` | `(move: string \| MoveObject) => boolean` | Make a move, returns true if successful |
|
|
379
|
+
| `setPosition` | `(fen: string, orientation: "w" \| "b") => void` | Set a new position |
|
|
380
|
+
| `flipBoard` | `() => void` | Flip the board orientation |
|
|
381
|
+
| `goToMove` | `(moveIndex: number) => void` | Jump to specific move (-1 = start) |
|
|
382
|
+
| `goToStart` | `() => void` | Go to starting position |
|
|
383
|
+
| `goToEnd` | `() => void` | Go to latest move |
|
|
384
|
+
| `goToPreviousMove` | `() => void` | Go to previous move |
|
|
385
|
+
| `goToNextMove` | `() => void` | Go to next move |
|
|
386
|
+
|
|
387
|
+
#### Info Object
|
|
388
|
+
|
|
389
|
+
| Property | Type | Description |
|
|
390
|
+
| ------------------------ | ------------ | ------------------------------------- |
|
|
391
|
+
| `turn` | `"w" \| "b"` | Current turn |
|
|
392
|
+
| `isPlayerTurn` | `boolean` | Whether it's the player's turn |
|
|
393
|
+
| `isOpponentTurn` | `boolean` | Whether it's the opponent's turn |
|
|
394
|
+
| `moveNumber` | `number` | Current move number |
|
|
395
|
+
| `lastMove` | `Move` | Last move made |
|
|
396
|
+
| `isCheck` | `boolean` | Whether current player is in check |
|
|
397
|
+
| `isCheckmate` | `boolean` | Whether it's checkmate |
|
|
398
|
+
| `isDraw` | `boolean` | Whether the game is a draw |
|
|
399
|
+
| `isDrawn` | `boolean` | Alias for `isDraw` |
|
|
400
|
+
| `isStalemate` | `boolean` | Whether it's stalemate |
|
|
401
|
+
| `isThreefoldRepetition` | `boolean` | Whether threefold repetition occurred |
|
|
402
|
+
| `isInsufficientMaterial` | `boolean` | Whether there's insufficient material |
|
|
403
|
+
| `isGameOver` | `boolean` | Whether the game has ended |
|
|
404
|
+
| `hasPlayerWon` | `boolean` | Whether the player has won |
|
|
405
|
+
| `hasPlayerLost` | `boolean` | Whether the player has lost |
|
|
406
|
+
|
|
407
|
+
## Examples
|
|
408
|
+
|
|
409
|
+
### Basic Game with All Features
|
|
410
|
+
|
|
411
|
+
```tsx
|
|
412
|
+
import { ChessGame } from "@react-chess-tools/react-chess-game";
|
|
413
|
+
|
|
414
|
+
function FullFeaturedGame() {
|
|
415
|
+
return (
|
|
416
|
+
<ChessGame.Root>
|
|
417
|
+
<ChessGame.Sounds />
|
|
418
|
+
<ChessGame.KeyboardControls />
|
|
419
|
+
<ChessGame.Board />
|
|
420
|
+
</ChessGame.Root>
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
### Game with Chess Clock
|
|
426
|
+
|
|
427
|
+
```tsx
|
|
428
|
+
import { ChessGame } from "@react-chess-tools/react-chess-game";
|
|
429
|
+
|
|
430
|
+
function GameWithClock() {
|
|
431
|
+
return (
|
|
432
|
+
<ChessGame.Root timeControl={{ time: "5+3" }}>
|
|
433
|
+
<ChessGame.Clock.Display color="white" />
|
|
434
|
+
<ChessGame.Board />
|
|
435
|
+
<ChessGame.Clock.Display color="black" />
|
|
436
|
+
<ChessGame.Clock.PlayPause />
|
|
437
|
+
<ChessGame.Clock.Reset>Reset</ChessGame.Clock.Reset>
|
|
438
|
+
</ChessGame.Root>
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
### Custom Starting Position
|
|
444
|
+
|
|
445
|
+
```tsx
|
|
446
|
+
import { ChessGame } from "@react-chess-tools/react-chess-game";
|
|
447
|
+
|
|
448
|
+
function CustomPosition() {
|
|
449
|
+
// Sicilian Defense starting position
|
|
450
|
+
const sicilianFen =
|
|
451
|
+
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2";
|
|
452
|
+
|
|
453
|
+
return (
|
|
454
|
+
<ChessGame.Root fen={sicilianFen}>
|
|
455
|
+
<ChessGame.Board />
|
|
456
|
+
</ChessGame.Root>
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
### Game with Move History Display
|
|
462
|
+
|
|
463
|
+
```tsx
|
|
464
|
+
import {
|
|
465
|
+
ChessGame,
|
|
466
|
+
useChessGameContext,
|
|
467
|
+
} from "@react-chess-tools/react-chess-game";
|
|
468
|
+
|
|
469
|
+
function MoveHistory() {
|
|
470
|
+
const { game, currentMoveIndex, methods } = useChessGameContext();
|
|
471
|
+
const history = game.history();
|
|
472
|
+
|
|
473
|
+
return (
|
|
474
|
+
<div className="move-history">
|
|
475
|
+
{history.map((move, index) => (
|
|
476
|
+
<button
|
|
477
|
+
key={index}
|
|
478
|
+
onClick={() => methods.goToMove(index)}
|
|
479
|
+
className={index === currentMoveIndex ? "active" : ""}
|
|
480
|
+
>
|
|
481
|
+
{move}
|
|
482
|
+
</button>
|
|
483
|
+
))}
|
|
484
|
+
</div>
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function GameWithHistory() {
|
|
489
|
+
return (
|
|
490
|
+
<ChessGame.Root>
|
|
491
|
+
<ChessGame.Board />
|
|
492
|
+
<MoveHistory />
|
|
493
|
+
<ChessGame.KeyboardControls />
|
|
494
|
+
</ChessGame.Root>
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
### Game with Status Display
|
|
500
|
+
|
|
501
|
+
```tsx
|
|
502
|
+
import {
|
|
503
|
+
ChessGame,
|
|
504
|
+
useChessGameContext,
|
|
505
|
+
} from "@react-chess-tools/react-chess-game";
|
|
506
|
+
|
|
507
|
+
function GameStatus() {
|
|
508
|
+
const { info } = useChessGameContext();
|
|
509
|
+
|
|
510
|
+
if (info.isCheckmate) {
|
|
511
|
+
return (
|
|
512
|
+
<div className="status">
|
|
513
|
+
Checkmate! {info.hasPlayerWon ? "You win!" : "You lose!"}
|
|
514
|
+
</div>
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
if (info.isDraw) {
|
|
518
|
+
return <div className="status">Draw!</div>;
|
|
519
|
+
}
|
|
520
|
+
if (info.isCheck) {
|
|
521
|
+
return <div className="status">Check!</div>;
|
|
522
|
+
}
|
|
523
|
+
return (
|
|
524
|
+
<div className="status">Turn: {info.turn === "w" ? "White" : "Black"}</div>
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function GameWithStatus() {
|
|
529
|
+
return (
|
|
530
|
+
<ChessGame.Root>
|
|
531
|
+
<GameStatus />
|
|
532
|
+
<ChessGame.Board />
|
|
533
|
+
<ChessGame.Sounds />
|
|
534
|
+
</ChessGame.Root>
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
### Using ChessClock Directly
|
|
540
|
+
|
|
541
|
+
You can also use `ChessClock` components directly alongside `ChessGame`:
|
|
542
|
+
|
|
543
|
+
```tsx
|
|
544
|
+
import { ChessGame, ChessClock } from "@react-chess-tools/react-chess-game";
|
|
545
|
+
|
|
546
|
+
function GameWithSeparateClock() {
|
|
547
|
+
return (
|
|
548
|
+
<div>
|
|
549
|
+
<ChessClock.Root timeControl={{ time: "10+0" }}>
|
|
550
|
+
<ChessClock.Display color="white" />
|
|
551
|
+
<ChessClock.Display color="black" />
|
|
552
|
+
<ChessClock.PlayPause />
|
|
553
|
+
</ChessClock.Root>
|
|
554
|
+
|
|
555
|
+
<ChessGame.Root>
|
|
556
|
+
<ChessGame.Board />
|
|
557
|
+
</ChessGame.Root>
|
|
558
|
+
</div>
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
## License
|
|
564
|
+
|
|
565
|
+
This project is [MIT](https://opensource.org/licenses/MIT) licensed.
|
|
566
|
+
|
|
567
|
+
## Show Your Support
|
|
568
|
+
|
|
569
|
+
Give a star if this project helped you!
|