@mirasen/react-chessboard 1.0.0
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/LICENSE +21 -0
- package/README.md +409 -0
- package/dist/Chessboard.d.ts +2 -0
- package/dist/Chessboard.js +70 -0
- package/dist/adapters/chessjs.d.ts +2 -0
- package/dist/adapters/chessjs.js +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.js +1 -0
- package/package.json +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 [Contributors](https://github.com/mirasen-io/react-chessboard/graphs/contributors)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/@mirasen/react-chessboard)
|
|
2
|
+
[](https://github.com/mirasen-io/react-chessboard/actions/workflows/ci.yml)
|
|
3
|
+
[](https://sonarcloud.io/summary/new_code?id=mirasen-io_react-chessboard)
|
|
4
|
+
[](https://sonarcloud.io/summary/new_code?id=mirasen-io_react-chessboard)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
|
|
7
|
+
# @mirasen/react-chessboard
|
|
8
|
+
|
|
9
|
+
A React chessboard component with built-in interaction, promotion, animation, and chess.js integration.
|
|
10
|
+
|
|
11
|
+
Start with a working chessboard, not a board primitive.
|
|
12
|
+
|
|
13
|
+
## Try it live
|
|
14
|
+
|
|
15
|
+
These examples use the same Mirasen chessboard runtime that powers the React component.
|
|
16
|
+
|
|
17
|
+
- [Play a chess.js game](https://mirasen.io/chessboard/examples/chessjs.html)
|
|
18
|
+
- [Try the promotion flow](https://mirasen.io/chessboard/examples/promotion.html)
|
|
19
|
+
- [Try the minimal example](https://mirasen.io/chessboard/examples/minimal.html)
|
|
20
|
+
|
|
21
|
+
## Why this exists
|
|
22
|
+
|
|
23
|
+
Most React chessboard components give you a board primitive and leave the real chess UX to your app:
|
|
24
|
+
|
|
25
|
+
- click and drag behavior
|
|
26
|
+
- legal target feedback
|
|
27
|
+
- promotion flow
|
|
28
|
+
- special move glue
|
|
29
|
+
- animation
|
|
30
|
+
- synchronization with a rules engine
|
|
31
|
+
|
|
32
|
+
`@mirasen/react-chessboard` wraps `@mirasen/chessboard` in a small React component so React apps can start from a working chessboard instead.
|
|
33
|
+
|
|
34
|
+
React owns lifecycle and props. Mirasen owns board rendering, input, interaction, animation, promotion, and extension behavior.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm install @mirasen/react-chessboard
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
For chess.js integration:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npm install @mirasen/react-chessboard chess.js
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
React and React DOM are peer dependencies. The package supports React 18 and newer.
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
### Minimal React component
|
|
53
|
+
|
|
54
|
+
```tsx
|
|
55
|
+
import { Chessboard } from '@mirasen/react-chessboard';
|
|
56
|
+
|
|
57
|
+
export function App() {
|
|
58
|
+
return (
|
|
59
|
+
<div style={{ width: 420, height: 420 }}>
|
|
60
|
+
<Chessboard position={{ id: 'game-1', position: 'start' }} />
|
|
61
|
+
</div>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The outer container needs a visible size. The board fills that container.
|
|
67
|
+
|
|
68
|
+
User moves are disabled unless you provide `movability`. For a playable chess game, connect the component to a game/rules layer such as `chess.js`.
|
|
69
|
+
|
|
70
|
+
### Connect to chess.js
|
|
71
|
+
|
|
72
|
+
`@mirasen/react-chessboard` owns board interaction and UI move output.
|
|
73
|
+
`chess.js` owns legality and game state.
|
|
74
|
+
|
|
75
|
+
The package re-exports the Mirasen chess.js adapter helpers:
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
import {
|
|
79
|
+
Chessboard,
|
|
80
|
+
type BoardOrientation,
|
|
81
|
+
type MoveOutput,
|
|
82
|
+
type MovabilityInput
|
|
83
|
+
} from '@mirasen/react-chessboard';
|
|
84
|
+
import { toBoardMoveDestinations, toGameMove } from '@mirasen/react-chessboard/adapters/chessjs';
|
|
85
|
+
import { Chess } from 'chess.js';
|
|
86
|
+
import { useCallback, useMemo, useRef, useState } from 'react';
|
|
87
|
+
|
|
88
|
+
type PositionRequest = {
|
|
89
|
+
id: number;
|
|
90
|
+
position: string;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export function App() {
|
|
94
|
+
const chessRef = useRef(new Chess());
|
|
95
|
+
|
|
96
|
+
const [position, setPosition] = useState<PositionRequest>({
|
|
97
|
+
id: 0,
|
|
98
|
+
position: chessRef.current.fen()
|
|
99
|
+
});
|
|
100
|
+
const [orientation, setOrientation] = useState<BoardOrientation>('white');
|
|
101
|
+
const [autoPromoteToQueen, setAutoPromoteToQueen] = useState(false);
|
|
102
|
+
const [lastMove, setLastMove] = useState<string | null>(null);
|
|
103
|
+
const [error, setError] = useState<string | null>(null);
|
|
104
|
+
|
|
105
|
+
const syncPosition = useCallback(() => {
|
|
106
|
+
setPosition((current) => ({
|
|
107
|
+
id: current.id + 1,
|
|
108
|
+
position: chessRef.current.fen()
|
|
109
|
+
}));
|
|
110
|
+
}, []);
|
|
111
|
+
|
|
112
|
+
const movability = useMemo<MovabilityInput>(
|
|
113
|
+
() => ({
|
|
114
|
+
mode: 'strict',
|
|
115
|
+
destinations: (source) => {
|
|
116
|
+
const moves = chessRef.current.moves({
|
|
117
|
+
square: source,
|
|
118
|
+
verbose: true
|
|
119
|
+
});
|
|
120
|
+
const destinations = toBoardMoveDestinations(moves);
|
|
121
|
+
|
|
122
|
+
return destinations.length > 0 ? destinations : undefined;
|
|
123
|
+
}
|
|
124
|
+
}),
|
|
125
|
+
[position.position]
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const onUIMove = useCallback(
|
|
129
|
+
(move: MoveOutput) => {
|
|
130
|
+
try {
|
|
131
|
+
chessRef.current.move(toGameMove(move));
|
|
132
|
+
setLastMove(`${move.from}-${move.to}`);
|
|
133
|
+
setError(null);
|
|
134
|
+
syncPosition();
|
|
135
|
+
} catch (cause) {
|
|
136
|
+
setError(cause instanceof Error ? cause.message : 'Illegal move');
|
|
137
|
+
syncPosition();
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
[syncPosition]
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
function resetGame() {
|
|
144
|
+
chessRef.current.reset();
|
|
145
|
+
setLastMove(null);
|
|
146
|
+
setError(null);
|
|
147
|
+
syncPosition();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function flipBoard() {
|
|
151
|
+
setOrientation((current) => (current === 'white' ? 'black' : 'white'));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return (
|
|
155
|
+
<main>
|
|
156
|
+
<div style={{ width: 420, height: 420 }}>
|
|
157
|
+
<Chessboard
|
|
158
|
+
position={position}
|
|
159
|
+
orientation={orientation}
|
|
160
|
+
movability={movability}
|
|
161
|
+
onUIMove={onUIMove}
|
|
162
|
+
autoPromoteToQueen={autoPromoteToQueen}
|
|
163
|
+
/>
|
|
164
|
+
</div>
|
|
165
|
+
|
|
166
|
+
<button type="button" onClick={resetGame}>
|
|
167
|
+
Reset
|
|
168
|
+
</button>
|
|
169
|
+
<button type="button" onClick={flipBoard}>
|
|
170
|
+
Flip board
|
|
171
|
+
</button>
|
|
172
|
+
<label>
|
|
173
|
+
<input
|
|
174
|
+
type="checkbox"
|
|
175
|
+
checked={autoPromoteToQueen}
|
|
176
|
+
onChange={(event) => setAutoPromoteToQueen(event.currentTarget.checked)}
|
|
177
|
+
/>
|
|
178
|
+
Auto-promote to queen
|
|
179
|
+
</label>
|
|
180
|
+
|
|
181
|
+
<p>FEN: {position.position}</p>
|
|
182
|
+
<p>Last move: {lastMove ?? 'none'}</p>
|
|
183
|
+
{error ? <p role="alert">{error}</p> : null}
|
|
184
|
+
</main>
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
For a complete local smoke example, see [`examples/app`](./examples/app).
|
|
190
|
+
|
|
191
|
+
## Request-based props
|
|
192
|
+
|
|
193
|
+
The component intentionally uses request objects for board-changing commands.
|
|
194
|
+
|
|
195
|
+
```tsx
|
|
196
|
+
<Chessboard position={{ id: gameId, position: fen }} />
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
`position` is not a controlled prop that reapplies on every render. It is an id-based set-position request:
|
|
200
|
+
|
|
201
|
+
- same `position.id` → ignored
|
|
202
|
+
- new `position.id` → `board.setPosition(...)` is applied once
|
|
203
|
+
|
|
204
|
+
This makes React rerenders safe and prevents repeated prop/effect synchronization from resetting board state or clearing visual feedback.
|
|
205
|
+
|
|
206
|
+
### External moves
|
|
207
|
+
|
|
208
|
+
Use `externalMove` for computer, remote, replay, or engine moves that should be applied to the board as moves rather than full-position resets.
|
|
209
|
+
|
|
210
|
+
```tsx
|
|
211
|
+
import { toBoardMove } from '@mirasen/react-chessboard/adapters/chessjs';
|
|
212
|
+
|
|
213
|
+
const appliedMove = chess.move(randomMove);
|
|
214
|
+
|
|
215
|
+
setExternalMove((current) => ({
|
|
216
|
+
id: (current?.id ?? 0) + 1,
|
|
217
|
+
move: toBoardMove(appliedMove)
|
|
218
|
+
}));
|
|
219
|
+
|
|
220
|
+
<Chessboard position={{ id: gameId, position: chess.fen() }} externalMove={externalMove} />;
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
`externalMove` is also id-based:
|
|
224
|
+
|
|
225
|
+
- same `externalMove.id` → ignored
|
|
226
|
+
- new `externalMove.id` → `board.move(...)` is applied once
|
|
227
|
+
|
|
228
|
+
This prevents duplicate React renders from replaying the same move.
|
|
229
|
+
|
|
230
|
+
## Props
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
import type { CSSProperties } from 'react';
|
|
234
|
+
import type {
|
|
235
|
+
BoardOrientation,
|
|
236
|
+
MoveOutput,
|
|
237
|
+
MoveRequestInput,
|
|
238
|
+
MovabilityInput,
|
|
239
|
+
PositionInput
|
|
240
|
+
} from '@mirasen/react-chessboard';
|
|
241
|
+
|
|
242
|
+
type PositionRequest = {
|
|
243
|
+
id: string | number;
|
|
244
|
+
position: PositionInput;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
type ExternalMoveRequest = {
|
|
248
|
+
id: string | number;
|
|
249
|
+
move: MoveRequestInput;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
type ChessboardProps = {
|
|
253
|
+
position: PositionRequest;
|
|
254
|
+
externalMove?: ExternalMoveRequest;
|
|
255
|
+
orientation?: BoardOrientation;
|
|
256
|
+
movability?: MovabilityInput;
|
|
257
|
+
onUIMove?: (move: MoveOutput) => void;
|
|
258
|
+
autoPromoteToQueen?: boolean;
|
|
259
|
+
className?: string;
|
|
260
|
+
style?: CSSProperties;
|
|
261
|
+
};
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### `position`
|
|
265
|
+
|
|
266
|
+
Id-based set-position request. Use a new `id` when you want the board to apply a new position.
|
|
267
|
+
|
|
268
|
+
### `externalMove`
|
|
269
|
+
|
|
270
|
+
Id-based move request for moves coming from outside the user interaction flow.
|
|
271
|
+
|
|
272
|
+
### `orientation`
|
|
273
|
+
|
|
274
|
+
Board orientation. Accepts the same color input as `@mirasen/chessboard`, such as `'white'` or `'black'`.
|
|
275
|
+
|
|
276
|
+
### `movability`
|
|
277
|
+
|
|
278
|
+
Controls user-initiated moves. Use strict movability with legal destinations when integrating with a rules engine.
|
|
279
|
+
|
|
280
|
+
### `onUIMove`
|
|
281
|
+
|
|
282
|
+
Called when the user completes a board move. Use this to update your game/rules layer.
|
|
283
|
+
|
|
284
|
+
### `autoPromoteToQueen`
|
|
285
|
+
|
|
286
|
+
When `true`, promotion automatically selects a queen. When omitted or `false`, the normal built-in promotion flow is used.
|
|
287
|
+
|
|
288
|
+
### `className` and `style`
|
|
289
|
+
|
|
290
|
+
Applied to the outer container only. They are for layout, not board theming.
|
|
291
|
+
|
|
292
|
+
## chess.js adapter helpers
|
|
293
|
+
|
|
294
|
+
The React package re-exports the core chess.js adapter helpers:
|
|
295
|
+
|
|
296
|
+
```ts
|
|
297
|
+
import {
|
|
298
|
+
toBoardMove,
|
|
299
|
+
toBoardMoveDestinations,
|
|
300
|
+
toGameMove,
|
|
301
|
+
type ChessJsMoveInput
|
|
302
|
+
} from '@mirasen/react-chessboard/adapters/chessjs';
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
- `toGameMove` converts a board UI move into a move accepted by `chess.move(...)`.
|
|
306
|
+
- `toBoardMove` converts a `chess.js` move result into a board move request.
|
|
307
|
+
- `toBoardMoveDestinations` converts verbose legal `chess.js` moves into strict board destinations.
|
|
308
|
+
|
|
309
|
+
This keeps the integration boundary explicit:
|
|
310
|
+
|
|
311
|
+
- `chess.js` decides which moves are legal.
|
|
312
|
+
- The React component displays and collects user moves.
|
|
313
|
+
- Mirasen handles board interaction, target feedback, promotion UI, special-move board updates, and animation.
|
|
314
|
+
|
|
315
|
+
## What you get out of the box
|
|
316
|
+
|
|
317
|
+
The underlying Mirasen board provides the default first-party chessboard baseline:
|
|
318
|
+
|
|
319
|
+
- SVG rendering
|
|
320
|
+
- pointer-based click and drag interaction
|
|
321
|
+
- selected-square feedback
|
|
322
|
+
- active-target feedback
|
|
323
|
+
- legal move hints
|
|
324
|
+
- last-move feedback
|
|
325
|
+
- promotion UI
|
|
326
|
+
- optional auto-promotion
|
|
327
|
+
- animation for board-state changes
|
|
328
|
+
- mobile-friendly pointer behavior
|
|
329
|
+
|
|
330
|
+
## Styling and customization
|
|
331
|
+
|
|
332
|
+
The React component intentionally exposes a small high-level API and uses the default Mirasen board appearance.
|
|
333
|
+
|
|
334
|
+
It does not expose color, square, piece, renderer, or extension customization props in v1.
|
|
335
|
+
|
|
336
|
+
This is deliberate: the React package is the simple path for users who want a working chessboard quickly.
|
|
337
|
+
|
|
338
|
+
For custom visuals, custom extensions, custom piece sets, or low-level runtime control, use `@mirasen/chessboard` directly from React.
|
|
339
|
+
|
|
340
|
+
## Advanced: use the core package directly from React
|
|
341
|
+
|
|
342
|
+
Power users can use the framework-agnostic core package directly:
|
|
343
|
+
|
|
344
|
+
```tsx
|
|
345
|
+
import { createBoard, type Chessboard as CoreChessboard } from '@mirasen/chessboard';
|
|
346
|
+
import { useEffect, useRef } from 'react';
|
|
347
|
+
|
|
348
|
+
export function CustomBoard() {
|
|
349
|
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
350
|
+
const boardRef = useRef<CoreChessboard | null>(null);
|
|
351
|
+
|
|
352
|
+
useEffect(() => {
|
|
353
|
+
const element = containerRef.current;
|
|
354
|
+
|
|
355
|
+
if (!element) {
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const board = createBoard({
|
|
360
|
+
element
|
|
361
|
+
// extensions/config/custom visuals
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
boardRef.current = board;
|
|
365
|
+
|
|
366
|
+
return () => {
|
|
367
|
+
board.destroy();
|
|
368
|
+
boardRef.current = null;
|
|
369
|
+
};
|
|
370
|
+
}, []);
|
|
371
|
+
|
|
372
|
+
return <div ref={containerRef} style={{ width: 420, height: 420 }} />;
|
|
373
|
+
}
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
Use this route when you need lower-level runtime access than the React component intentionally provides.
|
|
377
|
+
|
|
378
|
+
## Local example app
|
|
379
|
+
|
|
380
|
+
```bash
|
|
381
|
+
cd examples/app
|
|
382
|
+
npm install
|
|
383
|
+
npm run dev
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
Production build:
|
|
387
|
+
|
|
388
|
+
```bash
|
|
389
|
+
cd examples/app
|
|
390
|
+
npm run build
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
## Relationship to @mirasen/chessboard
|
|
394
|
+
|
|
395
|
+
`@mirasen/react-chessboard` is a thin wrapper over `@mirasen/chessboard`.
|
|
396
|
+
|
|
397
|
+
Use this package when you want a React component with a small high-level API.
|
|
398
|
+
|
|
399
|
+
Use `@mirasen/chessboard` directly when you want the full framework-agnostic runtime and extension system.
|
|
400
|
+
|
|
401
|
+
## Artwork
|
|
402
|
+
|
|
403
|
+
The default board appearance uses the same Chessnut piece set as `@mirasen/chessboard`.
|
|
404
|
+
|
|
405
|
+
- Author: Alexis Luengas — https://github.com/LexLuengas
|
|
406
|
+
- Source: https://github.com/LexLuengas/chessnut-pieces
|
|
407
|
+
- License: Apache License 2.0 — https://github.com/LexLuengas/chessnut-pieces/blob/master/LICENSE.txt
|
|
408
|
+
|
|
409
|
+
For full attribution, see the core package artwork documentation in `@mirasen/chessboard`.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createBoard } from '@mirasen/chessboard';
|
|
3
|
+
import { useEffect, useRef } from 'react';
|
|
4
|
+
export function Chessboard({ position, externalMove, orientation, movability, onUIMove, autoPromoteToQueen, className, style }) {
|
|
5
|
+
const containerRef = useRef(null);
|
|
6
|
+
const boardRef = useRef(null);
|
|
7
|
+
const onUIMoveRef = useRef(onUIMove);
|
|
8
|
+
const lastPositionIdRef = useRef(null);
|
|
9
|
+
const lastExternalMoveIdRef = useRef(null);
|
|
10
|
+
// Keep latest callback in ref
|
|
11
|
+
onUIMoveRef.current = onUIMove;
|
|
12
|
+
// Board creation and destruction
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (!containerRef.current)
|
|
15
|
+
return;
|
|
16
|
+
const board = createBoard({ element: containerRef.current });
|
|
17
|
+
boardRef.current = board;
|
|
18
|
+
board.extensions.events.setOnUIMove((move) => {
|
|
19
|
+
onUIMoveRef.current?.(move);
|
|
20
|
+
});
|
|
21
|
+
return () => {
|
|
22
|
+
board.extensions.events.setOnUIMove(null);
|
|
23
|
+
board.destroy();
|
|
24
|
+
boardRef.current = null;
|
|
25
|
+
};
|
|
26
|
+
}, []);
|
|
27
|
+
// Position sync
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!boardRef.current)
|
|
30
|
+
return;
|
|
31
|
+
if (lastPositionIdRef.current === position.id)
|
|
32
|
+
return;
|
|
33
|
+
boardRef.current.setPosition(position.position);
|
|
34
|
+
lastPositionIdRef.current = position.id;
|
|
35
|
+
}, [position.id, position.position]);
|
|
36
|
+
// External move sync
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (!boardRef.current)
|
|
39
|
+
return;
|
|
40
|
+
if (!externalMove)
|
|
41
|
+
return;
|
|
42
|
+
if (lastExternalMoveIdRef.current === externalMove.id)
|
|
43
|
+
return;
|
|
44
|
+
boardRef.current.move(externalMove.move);
|
|
45
|
+
lastExternalMoveIdRef.current = externalMove.id;
|
|
46
|
+
}, [externalMove?.id, externalMove?.move]);
|
|
47
|
+
// Orientation sync
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (!boardRef.current)
|
|
50
|
+
return;
|
|
51
|
+
if (orientation === undefined)
|
|
52
|
+
return;
|
|
53
|
+
boardRef.current.setOrientation(orientation);
|
|
54
|
+
}, [orientation]);
|
|
55
|
+
// Movability sync
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
if (!boardRef.current)
|
|
58
|
+
return;
|
|
59
|
+
if (movability === undefined)
|
|
60
|
+
return;
|
|
61
|
+
boardRef.current.setMovability(movability);
|
|
62
|
+
}, [movability]);
|
|
63
|
+
// autoPromoteToQueen sync
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
if (!boardRef.current)
|
|
66
|
+
return;
|
|
67
|
+
boardRef.current.extensions.autoPromote.toQueen = autoPromoteToQueen ?? false;
|
|
68
|
+
}, [autoPromoteToQueen]);
|
|
69
|
+
return _jsx("div", { ref: containerRef, className: className, style: style });
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { toBoardMove, toBoardMoveDestinations, toGameMove } from '@mirasen/chessboard/adapters/chessjs';
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Chessboard } from './Chessboard.js';
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ColorInput, MovabilityInput, MoveOutput, MoveRequestInput, PositionInput } from '@mirasen/chessboard';
|
|
2
|
+
import type { CSSProperties } from 'react';
|
|
3
|
+
export type BoardOrientation = ColorInput;
|
|
4
|
+
export type PositionRequest = {
|
|
5
|
+
id: string | number;
|
|
6
|
+
position: PositionInput;
|
|
7
|
+
};
|
|
8
|
+
export type ExternalMoveRequest = {
|
|
9
|
+
id: string | number;
|
|
10
|
+
move: MoveRequestInput;
|
|
11
|
+
};
|
|
12
|
+
export type ChessboardProps = {
|
|
13
|
+
position: PositionRequest;
|
|
14
|
+
externalMove?: ExternalMoveRequest;
|
|
15
|
+
orientation?: BoardOrientation;
|
|
16
|
+
movability?: MovabilityInput;
|
|
17
|
+
onUIMove?: (move: MoveOutput) => void;
|
|
18
|
+
autoPromoteToQueen?: boolean;
|
|
19
|
+
className?: string;
|
|
20
|
+
style?: CSSProperties;
|
|
21
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mirasen/react-chessboard",
|
|
3
|
+
"description": "A React chessboard component with built-in interaction, promotion, animation, and chess.js integration.",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"mirasen",
|
|
8
|
+
"react",
|
|
9
|
+
"chess",
|
|
10
|
+
"chessboard",
|
|
11
|
+
"react-chessboard",
|
|
12
|
+
"chess-ui",
|
|
13
|
+
"chess.js",
|
|
14
|
+
"chessjs",
|
|
15
|
+
"typescript",
|
|
16
|
+
"svg"
|
|
17
|
+
],
|
|
18
|
+
"homepage": "https://mirasen.io/chessboard/",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/mirasen-io/react-chessboard.git"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/mirasen-io/react-chessboard/issues"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./adapters/chessjs": {
|
|
34
|
+
"types": "./dist/adapters/chessjs.d.ts",
|
|
35
|
+
"import": "./dist/adapters/chessjs.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "rimraf ./dist && tsc --project tsconfig.json && npm run prepack",
|
|
40
|
+
"build:release": "rimraf ./dist && tsc --project tsconfig-release.json && npm run prepack",
|
|
41
|
+
"prepack": "publint",
|
|
42
|
+
"format": "prettier --write .",
|
|
43
|
+
"lint": "prettier --check . && eslint .",
|
|
44
|
+
"check": "tsc --skipLibCheck --noEmit",
|
|
45
|
+
"check:test": "tsc --project tsconfig-test.json --skipLibCheck --noEmit",
|
|
46
|
+
"test": "npm run check:test && vitest run --run",
|
|
47
|
+
"coverage": "npm run check:test && vitest run --coverage",
|
|
48
|
+
"changeset:version": "changeset version && npm install && git add --all",
|
|
49
|
+
"changeset:publish": "changeset publish"
|
|
50
|
+
},
|
|
51
|
+
"files": [
|
|
52
|
+
"dist",
|
|
53
|
+
"!dist/**/*.test.*",
|
|
54
|
+
"!dist/**/*.spec.*",
|
|
55
|
+
"README.md",
|
|
56
|
+
"LICENSE"
|
|
57
|
+
],
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@mirasen/chessboard": "^1.1.3"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"react": ">=18",
|
|
63
|
+
"react-dom": ">=18",
|
|
64
|
+
"chess.js": "^1.4.0"
|
|
65
|
+
},
|
|
66
|
+
"peerDependenciesMeta": {
|
|
67
|
+
"chess.js": {
|
|
68
|
+
"optional": true
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"devDependencies": {
|
|
72
|
+
"@changesets/cli": "^2.28.1",
|
|
73
|
+
"@eslint/compat": "^2.0.0",
|
|
74
|
+
"@eslint/js": "^10.0.1",
|
|
75
|
+
"@testing-library/react": "^16.3.2",
|
|
76
|
+
"@types/node": "^25.0.2",
|
|
77
|
+
"@types/react": "^18.3.0",
|
|
78
|
+
"@types/react-dom": "^18.3.0",
|
|
79
|
+
"@vitest/coverage-istanbul": "^4.0.3",
|
|
80
|
+
"eslint": "^10.0.2",
|
|
81
|
+
"eslint-config-prettier": "^10.1.2",
|
|
82
|
+
"globals": "^17.4.0",
|
|
83
|
+
"jsdom": "^29.1.1",
|
|
84
|
+
"prettier": "^3.5.3",
|
|
85
|
+
"publint": "^0.3.11",
|
|
86
|
+
"react": "^18.3.1",
|
|
87
|
+
"react-dom": "^18.3.1",
|
|
88
|
+
"rimraf": "^6.0.1",
|
|
89
|
+
"typescript": "^5.8.2",
|
|
90
|
+
"typescript-eslint": "^8.57.2",
|
|
91
|
+
"vitest": "^4.0.3"
|
|
92
|
+
},
|
|
93
|
+
"engines": {
|
|
94
|
+
"node": ">=20"
|
|
95
|
+
}
|
|
96
|
+
}
|