@dxos/react-ui-gameboard 0.8.4-main.ae835ea → 0.8.4-main.bc674ce
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/dist/lib/browser/index.mjs +536 -661
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +536 -661
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/Chessboard/Chessboard.d.ts +1 -1
- package/dist/types/src/components/Chessboard/Chessboard.d.ts.map +1 -1
- package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts +1 -1
- package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts.map +1 -1
- package/dist/types/src/components/Chessboard/chess.d.ts +5 -4
- package/dist/types/src/components/Chessboard/chess.d.ts.map +1 -1
- package/dist/types/src/components/Gameboard/Gameboard.d.ts +5 -5
- package/dist/types/src/components/Gameboard/Gameboard.d.ts.map +1 -1
- package/dist/types/src/components/Gameboard/types.d.ts +2 -3
- package/dist/types/src/components/Gameboard/types.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +25 -22
- package/src/components/Chessboard/Chessboard.stories.tsx +9 -5
- package/src/components/Chessboard/Chessboard.tsx +10 -4
- package/src/components/Chessboard/chess.ts +13 -10
- package/src/components/Gameboard/Gameboard.tsx +11 -6
- package/src/components/Gameboard/Piece.tsx +1 -1
- package/src/components/Gameboard/Square.tsx +1 -1
- package/src/components/Gameboard/types.ts +2 -3
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-gameboard",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.bc674ce",
|
|
4
4
|
"description": "Game board.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
13
|
"type": "module",
|
|
@@ -24,37 +28,36 @@
|
|
|
24
28
|
"src"
|
|
25
29
|
],
|
|
26
30
|
"dependencies": {
|
|
27
|
-
"@atlaskit/pragmatic-drag-and-drop": "
|
|
28
|
-
"@atlaskit/pragmatic-drag-and-drop-hitbox": "
|
|
29
|
-
"@
|
|
30
|
-
"@preact/signals-core": "^1.12.1",
|
|
31
|
+
"@atlaskit/pragmatic-drag-and-drop": "1.7.7",
|
|
32
|
+
"@atlaskit/pragmatic-drag-and-drop-hitbox": "1.1.0",
|
|
33
|
+
"@effect-atom/atom-react": "^0.4.6",
|
|
31
34
|
"@radix-ui/react-context": "1.1.1",
|
|
32
|
-
"chess.js": "^1.
|
|
35
|
+
"chess.js": "^1.0.0",
|
|
33
36
|
"react-resize-detector": "^11.0.1",
|
|
34
|
-
"@dxos/debug": "0.8.4-main.
|
|
35
|
-
"@dxos/invariant": "0.8.4-main.
|
|
36
|
-
"@dxos/node-std": "0.8.4-main.
|
|
37
|
-
"@dxos/
|
|
38
|
-
"@dxos/
|
|
37
|
+
"@dxos/debug": "0.8.4-main.bc674ce",
|
|
38
|
+
"@dxos/invariant": "0.8.4-main.bc674ce",
|
|
39
|
+
"@dxos/node-std": "0.8.4-main.bc674ce",
|
|
40
|
+
"@dxos/log": "0.8.4-main.bc674ce",
|
|
41
|
+
"@dxos/util": "0.8.4-main.bc674ce"
|
|
39
42
|
},
|
|
40
43
|
"devDependencies": {
|
|
41
44
|
"@svgr/cli": "^8.1.0",
|
|
42
45
|
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
43
|
-
"@types/react": "~19.2.
|
|
44
|
-
"@types/react-dom": "~19.2.
|
|
46
|
+
"@types/react": "~19.2.7",
|
|
47
|
+
"@types/react-dom": "~19.2.3",
|
|
45
48
|
"lodash.defaultsdeep": "^4.6.1",
|
|
46
|
-
"react": "~19.2.
|
|
47
|
-
"react-dom": "~19.2.
|
|
49
|
+
"react": "~19.2.3",
|
|
50
|
+
"react-dom": "~19.2.3",
|
|
48
51
|
"vite": "7.1.9",
|
|
49
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
50
|
-
"@dxos/
|
|
51
|
-
"@dxos/
|
|
52
|
+
"@dxos/react-ui": "0.8.4-main.bc674ce",
|
|
53
|
+
"@dxos/storybook-utils": "0.8.4-main.bc674ce",
|
|
54
|
+
"@dxos/ui-theme": "0.8.4-main.bc674ce"
|
|
52
55
|
},
|
|
53
56
|
"peerDependencies": {
|
|
54
|
-
"react": "
|
|
55
|
-
"react-dom": "
|
|
56
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
57
|
-
"@dxos/
|
|
57
|
+
"react": "~19.2.3",
|
|
58
|
+
"react-dom": "~19.2.3",
|
|
59
|
+
"@dxos/react-ui": "0.8.4-main.bc674ce",
|
|
60
|
+
"@dxos/ui-theme": "0.8.4-main.bc674ce"
|
|
58
61
|
},
|
|
59
62
|
"publishConfig": {
|
|
60
63
|
"access": "public"
|
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { RegistryContext } from '@effect-atom/atom-react';
|
|
5
6
|
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
6
|
-
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
|
7
|
+
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
|
7
8
|
|
|
8
9
|
import { Button, Toolbar } from '@dxos/react-ui';
|
|
9
10
|
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
11
|
+
import { withRegistry } from '@dxos/storybook-utils';
|
|
10
12
|
|
|
11
13
|
import { Gameboard, type GameboardRootProps, type Move, type Player } from '../Gameboard';
|
|
12
14
|
|
|
@@ -18,7 +20,8 @@ type DefaultStoryProps = Pick<ChessboardProps, 'orientation' | 'showLabels' | 'd
|
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
const DefaultStory = ({ orientation: _orientation, pgn, ...props }: DefaultStoryProps) => {
|
|
21
|
-
const
|
|
23
|
+
const registry = useContext(RegistryContext);
|
|
24
|
+
const model = useMemo(() => new ChessModel(registry, pgn), [registry, pgn]);
|
|
22
25
|
const [orientation, setOrientation] = useState<Player | undefined>(_orientation);
|
|
23
26
|
|
|
24
27
|
const handleDrop = useCallback<NonNullable<GameboardRootProps<ChessModel>['onDrop']>>(
|
|
@@ -52,7 +55,8 @@ const DefaultStory = ({ orientation: _orientation, pgn, ...props }: DefaultStory
|
|
|
52
55
|
};
|
|
53
56
|
|
|
54
57
|
const GridStory = () => {
|
|
55
|
-
const
|
|
58
|
+
const registry = useContext(RegistryContext);
|
|
59
|
+
const models = useMemo(() => Array.from({ length: 9 }).map(() => new ChessModel(registry)), [registry]);
|
|
56
60
|
useEffect(() => {
|
|
57
61
|
const i = setInterval(() => {
|
|
58
62
|
const model = models[Math.floor(Math.random() * models.length)];
|
|
@@ -62,7 +66,7 @@ const GridStory = () => {
|
|
|
62
66
|
}, []);
|
|
63
67
|
|
|
64
68
|
return (
|
|
65
|
-
<div className='
|
|
69
|
+
<div className='bs-full aspect-square mx-auto'>
|
|
66
70
|
<div className='grid grid-cols-3 gap-2'>
|
|
67
71
|
{models.map((model, i) => (
|
|
68
72
|
<div key={i} className='aspect-square'>
|
|
@@ -80,7 +84,7 @@ const meta = {
|
|
|
80
84
|
title: 'ui/react-ui-gameboard/Chessboard',
|
|
81
85
|
component: Chessboard,
|
|
82
86
|
render: DefaultStory,
|
|
83
|
-
decorators: [withTheme, withLayout({
|
|
87
|
+
decorators: [withRegistry, withTheme, withLayout({ layout: 'column' })],
|
|
84
88
|
} satisfies Meta<typeof Chessboard>;
|
|
85
89
|
|
|
86
90
|
export default meta;
|
|
@@ -2,17 +2,19 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { Atom, useAtomValue } from '@effect-atom/atom-react';
|
|
5
6
|
import React, { type PropsWithChildren, forwardRef, memo, useEffect, useMemo, useRef, useState } from 'react';
|
|
6
7
|
import { useResizeDetector } from 'react-resize-detector';
|
|
7
8
|
|
|
8
9
|
import { type ThemedClassName, useForwardedRef } from '@dxos/react-ui';
|
|
9
|
-
import { mx } from '@dxos/
|
|
10
|
+
import { mx } from '@dxos/ui-theme';
|
|
10
11
|
import { isNonNullable } from '@dxos/util';
|
|
11
12
|
|
|
12
13
|
import {
|
|
13
14
|
type DOMRectBounds,
|
|
14
15
|
Gameboard,
|
|
15
16
|
type Location,
|
|
17
|
+
type PieceMap,
|
|
16
18
|
type PieceRecord,
|
|
17
19
|
type Player,
|
|
18
20
|
getRelativeBounds,
|
|
@@ -22,6 +24,9 @@ import {
|
|
|
22
24
|
|
|
23
25
|
import { type ChessModel, type ChessPiece, ChessPieces, boardStyles, getSquareColor, locationToPos } from './chess';
|
|
24
26
|
|
|
27
|
+
/** Fallback atom for when model is undefined. */
|
|
28
|
+
const EMPTY_PIECES_ATOM = Atom.make<PieceMap<ChessPiece>>({});
|
|
29
|
+
|
|
25
30
|
export type ChessboardProps = ThemedClassName<
|
|
26
31
|
PropsWithChildren<{
|
|
27
32
|
orientation?: Player;
|
|
@@ -40,6 +45,7 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
|
|
|
40
45
|
const targetRef = useForwardedRef(forwardedRef);
|
|
41
46
|
const { width, height } = useResizeDetector({ targetRef, refreshRate: 200 });
|
|
42
47
|
const { model, promoting, onPromotion } = useGameboardContext<ChessModel>(Chessboard.displayName!);
|
|
48
|
+
const pieces = useAtomValue(model?.pieces ?? EMPTY_PIECES_ATOM);
|
|
43
49
|
|
|
44
50
|
// Board squares.
|
|
45
51
|
const squares = useMemo<Location[]>(() => {
|
|
@@ -84,7 +90,7 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
|
|
|
84
90
|
return [];
|
|
85
91
|
}
|
|
86
92
|
|
|
87
|
-
return Object.values(
|
|
93
|
+
return Object.values(pieces)
|
|
88
94
|
.map((piece) => {
|
|
89
95
|
if (piece.id === promoting?.id) {
|
|
90
96
|
return null;
|
|
@@ -94,7 +100,7 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
|
|
|
94
100
|
return { piece, bounds };
|
|
95
101
|
})
|
|
96
102
|
.filter(isNonNullable);
|
|
97
|
-
}, [grid,
|
|
103
|
+
}, [grid, pieces, promoting]);
|
|
98
104
|
|
|
99
105
|
return (
|
|
100
106
|
<div ref={targetRef} tabIndex={0} className={mx('relative outline-none', classNames)}>
|
|
@@ -134,7 +140,7 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
|
|
|
134
140
|
piece={promoting}
|
|
135
141
|
onSelect={(piece) => {
|
|
136
142
|
onPromotion({
|
|
137
|
-
from: Object.values(
|
|
143
|
+
from: Object.values(pieces).find((p) => p.id === promoting.id)!.location,
|
|
138
144
|
to: piece.location,
|
|
139
145
|
piece: promoting.type,
|
|
140
146
|
promotion: piece.type,
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type
|
|
5
|
+
import { Atom, type Registry } from '@effect-atom/atom-react';
|
|
6
6
|
import { Chess as ChessJS } from 'chess.js';
|
|
7
7
|
import { type FC, type SVGProps } from 'react';
|
|
8
8
|
|
|
@@ -85,15 +85,18 @@ export const createChess = (pgn?: string): ChessJS => {
|
|
|
85
85
|
*/
|
|
86
86
|
export class ChessModel implements GameboardModel<ChessPiece> {
|
|
87
87
|
private readonly _chess = new ChessJS();
|
|
88
|
-
private readonly _pieces =
|
|
89
|
-
private readonly _moveIndex =
|
|
88
|
+
private readonly _pieces = Atom.make<PieceMap<ChessPiece>>({});
|
|
89
|
+
private readonly _moveIndex = Atom.make(0);
|
|
90
90
|
|
|
91
|
-
constructor(
|
|
91
|
+
constructor(
|
|
92
|
+
private readonly _registry: Registry.Registry,
|
|
93
|
+
pgn?: string,
|
|
94
|
+
) {
|
|
92
95
|
this.update(pgn);
|
|
93
96
|
}
|
|
94
97
|
|
|
95
98
|
get readonly(): boolean {
|
|
96
|
-
return this._moveIndex
|
|
99
|
+
return this._registry.get(this._moveIndex) !== this._chess.history().length;
|
|
97
100
|
}
|
|
98
101
|
|
|
99
102
|
get turn(): Player {
|
|
@@ -104,11 +107,11 @@ export class ChessModel implements GameboardModel<ChessPiece> {
|
|
|
104
107
|
return this._chess;
|
|
105
108
|
}
|
|
106
109
|
|
|
107
|
-
get pieces():
|
|
110
|
+
get pieces(): Atom.Atom<PieceMap<ChessPiece>> {
|
|
108
111
|
return this._pieces;
|
|
109
112
|
}
|
|
110
113
|
|
|
111
|
-
get moveIndex():
|
|
114
|
+
get moveIndex(): Atom.Atom<number> {
|
|
112
115
|
return this._moveIndex;
|
|
113
116
|
}
|
|
114
117
|
|
|
@@ -158,7 +161,7 @@ export class ChessModel implements GameboardModel<ChessPiece> {
|
|
|
158
161
|
|
|
159
162
|
const current = this._chess.history();
|
|
160
163
|
if (!isValidNextMove(previous, current)) {
|
|
161
|
-
this._pieces
|
|
164
|
+
this._registry.set(this._pieces, {});
|
|
162
165
|
}
|
|
163
166
|
|
|
164
167
|
this._updateBoard(this._chess);
|
|
@@ -201,8 +204,8 @@ export class ChessModel implements GameboardModel<ChessPiece> {
|
|
|
201
204
|
* Update pieces preserving identity.
|
|
202
205
|
*/
|
|
203
206
|
private _updateBoard(chess: ChessJS): void {
|
|
204
|
-
this._pieces
|
|
205
|
-
this._moveIndex
|
|
207
|
+
this._registry.set(this._pieces, createPieceMap(chess));
|
|
208
|
+
this._registry.set(this._moveIndex, chess.history().length);
|
|
206
209
|
}
|
|
207
210
|
}
|
|
208
211
|
|
|
@@ -8,13 +8,13 @@ import React, { type PropsWithChildren, forwardRef, useCallback, useEffect, useS
|
|
|
8
8
|
|
|
9
9
|
import { log } from '@dxos/log';
|
|
10
10
|
import { type ThemedClassName } from '@dxos/react-ui';
|
|
11
|
-
import { mx } from '@dxos/
|
|
11
|
+
import { mx } from '@dxos/ui-theme';
|
|
12
12
|
|
|
13
13
|
import { Piece, type PieceProps } from './Piece';
|
|
14
14
|
import { Square, type SquareProps } from './Square';
|
|
15
15
|
import { type GameboardModel, type Move, type PieceRecord, isLocation, isPiece } from './types';
|
|
16
16
|
|
|
17
|
-
export type GameboardContextValue<M extends GameboardModel
|
|
17
|
+
export type GameboardContextValue<M extends GameboardModel<any>> = {
|
|
18
18
|
model: M;
|
|
19
19
|
dragging?: boolean; // TODO(burdon): Change to PieceRecord.
|
|
20
20
|
promoting?: PieceRecord;
|
|
@@ -23,7 +23,7 @@ export type GameboardContextValue<M extends GameboardModel> = {
|
|
|
23
23
|
|
|
24
24
|
const [GameboardContextProvider, useRadixGameboardContext] = createContext<GameboardContextValue<any>>('Gameboard');
|
|
25
25
|
|
|
26
|
-
const useGameboardContext = <M extends GameboardModel
|
|
26
|
+
const useGameboardContext = <M extends GameboardModel<any>>(consumerName: string): GameboardContextValue<M> => {
|
|
27
27
|
return useRadixGameboardContext(consumerName);
|
|
28
28
|
};
|
|
29
29
|
|
|
@@ -31,7 +31,7 @@ const useGameboardContext = <M extends GameboardModel>(consumerName: string): Ga
|
|
|
31
31
|
// Root
|
|
32
32
|
//
|
|
33
33
|
|
|
34
|
-
type GameboardRootProps<M extends GameboardModel
|
|
34
|
+
type GameboardRootProps<M extends GameboardModel<any>> = PropsWithChildren<{
|
|
35
35
|
model?: M;
|
|
36
36
|
moveNumber?: number;
|
|
37
37
|
onDrop?: (move: Move) => boolean;
|
|
@@ -40,11 +40,16 @@ type GameboardRootProps<M extends GameboardModel> = PropsWithChildren<{
|
|
|
40
40
|
/**
|
|
41
41
|
* Generic board container.
|
|
42
42
|
*/
|
|
43
|
-
const GameboardRoot = <M extends GameboardModel
|
|
43
|
+
const GameboardRoot = <M extends GameboardModel<any>>({
|
|
44
|
+
children,
|
|
45
|
+
model,
|
|
46
|
+
moveNumber,
|
|
47
|
+
onDrop,
|
|
48
|
+
}: GameboardRootProps<M>) => {
|
|
44
49
|
const [dragging, setDragging] = useState(false);
|
|
45
50
|
const [promoting, setPromoting] = useState<PieceRecord | undefined>();
|
|
46
51
|
|
|
47
|
-
const handlePromotion = useCallback<GameboardContextValue<
|
|
52
|
+
const handlePromotion = useCallback<GameboardContextValue<GameboardModel<any>>['onPromotion']>((move) => {
|
|
48
53
|
log('onPromotion', { move });
|
|
49
54
|
setPromoting(undefined);
|
|
50
55
|
onDrop?.(move);
|
|
@@ -11,7 +11,7 @@ import { createPortal } from 'react-dom';
|
|
|
11
11
|
import { invariant } from '@dxos/invariant';
|
|
12
12
|
import { log } from '@dxos/log';
|
|
13
13
|
import { type ThemedClassName, useDynamicRef } from '@dxos/react-ui';
|
|
14
|
-
import { mx } from '@dxos/
|
|
14
|
+
import { mx } from '@dxos/ui-theme';
|
|
15
15
|
|
|
16
16
|
import { useGameboardContext } from './Gameboard';
|
|
17
17
|
import { type Location, type PieceRecord, type Player, isEqualLocation, isLocation } from './types';
|
|
@@ -8,7 +8,7 @@ import React, { memo, useEffect, useRef, useState } from 'react';
|
|
|
8
8
|
import { invariant } from '@dxos/invariant';
|
|
9
9
|
import { log } from '@dxos/log';
|
|
10
10
|
import { type ThemedClassName } from '@dxos/react-ui';
|
|
11
|
-
import { mx } from '@dxos/
|
|
11
|
+
import { mx } from '@dxos/ui-theme';
|
|
12
12
|
|
|
13
13
|
import { useGameboardContext } from './Gameboard';
|
|
14
14
|
import { type Location, isPiece } from './types';
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type
|
|
5
|
+
import { type Atom } from '@effect-atom/atom-react';
|
|
6
6
|
|
|
7
7
|
// TODO(burdon): Don't make this assumption.
|
|
8
8
|
export type Player = 'black' | 'white';
|
|
@@ -54,8 +54,7 @@ export const isEqualLocation = (l1: Location, l2: Location): boolean => l1[0] ==
|
|
|
54
54
|
export interface GameboardModel<T extends PieceType = PieceType> {
|
|
55
55
|
readonly: boolean;
|
|
56
56
|
turn: Player;
|
|
57
|
-
|
|
58
|
-
pieces: ReadonlySignal<PieceMap<T>>;
|
|
57
|
+
pieces: Atom.Atom<PieceMap<T>>;
|
|
59
58
|
isValidMove: (move: Move) => boolean;
|
|
60
59
|
canPromote?: (move: Move) => boolean;
|
|
61
60
|
}
|