@dxos/react-ui-gameboard 0.8.4-main.ae835ea → 0.8.4-main.bc2380dfbc
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 +102 -5
- package/README.md +2 -0
- package/dist/lib/browser/index.mjs +497 -679
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +497 -679
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/Chessboard/Chessboard.d.ts +2 -2
- package/dist/types/src/components/Chessboard/Chessboard.d.ts.map +1 -1
- package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts +2 -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 +10 -7
- package/dist/types/src/components/Gameboard/Gameboard.d.ts.map +1 -1
- package/dist/types/src/components/Gameboard/Piece.d.ts.map +1 -1
- package/dist/types/src/components/Gameboard/Square.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/src/components/Gameboard/util.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/alpha/bB.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/alpha/bK.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/alpha/bN.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/alpha/bP.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/alpha/bQ.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/alpha/bR.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/alpha/wB.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/alpha/wK.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/alpha/wN.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/alpha/wP.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/alpha/wQ.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/alpha/wR.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/cburnett/bB.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/cburnett/bK.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/cburnett/bN.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/cburnett/bP.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/cburnett/bQ.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/cburnett/bR.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/cburnett/wB.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/cburnett/wK.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/cburnett/wN.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/cburnett/wP.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/cburnett/wQ.d.ts.map +1 -1
- package/dist/types/src/gen/pieces/chess/cburnett/wR.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +27 -27
- package/src/components/Chessboard/Chessboard.stories.tsx +9 -5
- package/src/components/Chessboard/Chessboard.tsx +19 -12
- package/src/components/Chessboard/chess.ts +13 -10
- package/src/components/Gameboard/Gameboard.tsx +8 -10
- package/src/components/Gameboard/Piece.tsx +5 -3
- package/src/components/Gameboard/Square.tsx +5 -3
- package/src/components/Gameboard/types.ts +2 -3
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-gameboard",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.bc2380dfbc",
|
|
4
4
|
"description": "Game board.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
-
"
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
11
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
13
|
"type": "module",
|
|
10
14
|
"exports": {
|
|
@@ -16,45 +20,41 @@
|
|
|
16
20
|
}
|
|
17
21
|
},
|
|
18
22
|
"types": "dist/types/src/index.d.ts",
|
|
19
|
-
"typesVersions": {
|
|
20
|
-
"*": {}
|
|
21
|
-
},
|
|
22
23
|
"files": [
|
|
23
24
|
"dist",
|
|
24
25
|
"src"
|
|
25
26
|
],
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"@atlaskit/pragmatic-drag-and-drop": "
|
|
28
|
-
"@atlaskit/pragmatic-drag-and-drop-hitbox": "
|
|
29
|
-
"@
|
|
30
|
-
"@preact/signals-core": "^1.12.1",
|
|
28
|
+
"@atlaskit/pragmatic-drag-and-drop": "1.7.7",
|
|
29
|
+
"@atlaskit/pragmatic-drag-and-drop-hitbox": "1.1.0",
|
|
30
|
+
"@effect-atom/atom-react": "^0.5.0",
|
|
31
31
|
"@radix-ui/react-context": "1.1.1",
|
|
32
|
-
"chess.js": "^1.
|
|
32
|
+
"chess.js": "^1.0.0",
|
|
33
33
|
"react-resize-detector": "^11.0.1",
|
|
34
|
-
"@dxos/debug": "0.8.4-main.
|
|
35
|
-
"@dxos/invariant": "0.8.4-main.
|
|
36
|
-
"@dxos/
|
|
37
|
-
"@dxos/util": "0.8.4-main.
|
|
38
|
-
"@dxos/
|
|
34
|
+
"@dxos/debug": "0.8.4-main.bc2380dfbc",
|
|
35
|
+
"@dxos/invariant": "0.8.4-main.bc2380dfbc",
|
|
36
|
+
"@dxos/log": "0.8.4-main.bc2380dfbc",
|
|
37
|
+
"@dxos/util": "0.8.4-main.bc2380dfbc",
|
|
38
|
+
"@dxos/node-std": "0.8.4-main.bc2380dfbc"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@svgr/cli": "^8.1.0",
|
|
42
42
|
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
43
|
-
"@types/react": "~19.2.
|
|
44
|
-
"@types/react-dom": "~19.2.
|
|
43
|
+
"@types/react": "~19.2.7",
|
|
44
|
+
"@types/react-dom": "~19.2.3",
|
|
45
45
|
"lodash.defaultsdeep": "^4.6.1",
|
|
46
|
-
"react": "~19.2.
|
|
47
|
-
"react-dom": "~19.2.
|
|
48
|
-
"vite": "
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/
|
|
51
|
-
"@dxos/
|
|
46
|
+
"react": "~19.2.3",
|
|
47
|
+
"react-dom": "~19.2.3",
|
|
48
|
+
"vite": "^8.0.13",
|
|
49
|
+
"@dxos/storybook-utils": "0.8.4-main.bc2380dfbc",
|
|
50
|
+
"@dxos/ui-theme": "0.8.4-main.bc2380dfbc",
|
|
51
|
+
"@dxos/react-ui": "0.8.4-main.bc2380dfbc"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
|
-
"react": "
|
|
55
|
-
"react-dom": "
|
|
56
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
57
|
-
"@dxos/
|
|
54
|
+
"react": "~19.2.3",
|
|
55
|
+
"react-dom": "~19.2.3",
|
|
56
|
+
"@dxos/react-ui": "0.8.4-main.bc2380dfbc",
|
|
57
|
+
"@dxos/ui-theme": "0.8.4-main.bc2380dfbc"
|
|
58
58
|
},
|
|
59
59
|
"publishConfig": {
|
|
60
60
|
"access": "public"
|
|
@@ -2,14 +2,15 @@
|
|
|
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
|
-
|
|
13
14
|
import { ChessModel } from './chess';
|
|
14
15
|
import { Chessboard, type ChessboardProps } from './Chessboard';
|
|
15
16
|
|
|
@@ -18,7 +19,8 @@ type DefaultStoryProps = Pick<ChessboardProps, 'orientation' | 'showLabels' | 'd
|
|
|
18
19
|
};
|
|
19
20
|
|
|
20
21
|
const DefaultStory = ({ orientation: _orientation, pgn, ...props }: DefaultStoryProps) => {
|
|
21
|
-
const
|
|
22
|
+
const registry = useContext(RegistryContext);
|
|
23
|
+
const model = useMemo(() => new ChessModel(registry, pgn), [registry, pgn]);
|
|
22
24
|
const [orientation, setOrientation] = useState<Player | undefined>(_orientation);
|
|
23
25
|
|
|
24
26
|
const handleDrop = useCallback<NonNullable<GameboardRootProps<ChessModel>['onDrop']>>(
|
|
@@ -52,7 +54,8 @@ const DefaultStory = ({ orientation: _orientation, pgn, ...props }: DefaultStory
|
|
|
52
54
|
};
|
|
53
55
|
|
|
54
56
|
const GridStory = () => {
|
|
55
|
-
const
|
|
57
|
+
const registry = useContext(RegistryContext);
|
|
58
|
+
const models = useMemo(() => Array.from({ length: 9 }).map(() => new ChessModel(registry)), [registry]);
|
|
56
59
|
useEffect(() => {
|
|
57
60
|
const i = setInterval(() => {
|
|
58
61
|
const model = models[Math.floor(Math.random() * models.length)];
|
|
@@ -80,7 +83,7 @@ const meta = {
|
|
|
80
83
|
title: 'ui/react-ui-gameboard/Chessboard',
|
|
81
84
|
component: Chessboard,
|
|
82
85
|
render: DefaultStory,
|
|
83
|
-
decorators: [withTheme, withLayout({
|
|
86
|
+
decorators: [withRegistry, withTheme(), withLayout({ layout: 'column' })],
|
|
84
87
|
} satisfies Meta<typeof Chessboard>;
|
|
85
88
|
|
|
86
89
|
export default meta;
|
|
@@ -105,5 +108,6 @@ export const Debug: Story = {
|
|
|
105
108
|
};
|
|
106
109
|
|
|
107
110
|
export const Grid = {
|
|
111
|
+
decorators: [withRegistry, withTheme(), withLayout({ layout: 'fullscreen' })],
|
|
108
112
|
render: GridStory,
|
|
109
113
|
};
|
|
@@ -2,26 +2,35 @@
|
|
|
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,
|
|
19
21
|
locationToString,
|
|
20
22
|
useGameboardContext,
|
|
21
23
|
} from '../Gameboard';
|
|
22
|
-
|
|
23
24
|
import { type ChessModel, type ChessPiece, ChessPieces, boardStyles, getSquareColor, locationToPos } from './chess';
|
|
24
25
|
|
|
26
|
+
/** Fallback atom for when model is undefined. */
|
|
27
|
+
const EMPTY_PIECES_ATOM = Atom.make<PieceMap<ChessPiece>>({});
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Chessboard layout.
|
|
31
|
+
*/
|
|
32
|
+
const CHESSBOARD_NAME = 'Chessboard';
|
|
33
|
+
|
|
25
34
|
export type ChessboardProps = ThemedClassName<
|
|
26
35
|
PropsWithChildren<{
|
|
27
36
|
orientation?: Player;
|
|
@@ -32,14 +41,12 @@ export type ChessboardProps = ThemedClassName<
|
|
|
32
41
|
}>
|
|
33
42
|
>;
|
|
34
43
|
|
|
35
|
-
/**
|
|
36
|
-
* Chessboard layout.
|
|
37
|
-
*/
|
|
38
44
|
const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
|
|
39
45
|
({ classNames, orientation, showLabels, debug, rows = 8, cols = 8 }, forwardedRef) => {
|
|
40
46
|
const targetRef = useForwardedRef(forwardedRef);
|
|
41
47
|
const { width, height } = useResizeDetector({ targetRef, refreshRate: 200 });
|
|
42
|
-
const { model, promoting, onPromotion } = useGameboardContext<ChessModel>(
|
|
48
|
+
const { model, promoting, onPromotion } = useGameboardContext<ChessModel>(CHESSBOARD_NAME);
|
|
49
|
+
const pieces = useAtomValue(model?.pieces ?? EMPTY_PIECES_ATOM);
|
|
43
50
|
|
|
44
51
|
// Board squares.
|
|
45
52
|
const squares = useMemo<Location[]>(() => {
|
|
@@ -55,7 +62,7 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
|
|
|
55
62
|
<div
|
|
56
63
|
key={locationToString(location)}
|
|
57
64
|
{...{
|
|
58
|
-
|
|
65
|
+
'data-location': locationToString(location),
|
|
59
66
|
}}
|
|
60
67
|
/>
|
|
61
68
|
);
|
|
@@ -84,7 +91,7 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
|
|
|
84
91
|
return [];
|
|
85
92
|
}
|
|
86
93
|
|
|
87
|
-
return Object.values(
|
|
94
|
+
return Object.values(pieces)
|
|
88
95
|
.map((piece) => {
|
|
89
96
|
if (piece.id === promoting?.id) {
|
|
90
97
|
return null;
|
|
@@ -94,10 +101,10 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
|
|
|
94
101
|
return { piece, bounds };
|
|
95
102
|
})
|
|
96
103
|
.filter(isNonNullable);
|
|
97
|
-
}, [grid,
|
|
104
|
+
}, [grid, pieces, promoting]);
|
|
98
105
|
|
|
99
106
|
return (
|
|
100
|
-
<div ref={targetRef} tabIndex={0} className={mx('relative outline-
|
|
107
|
+
<div ref={targetRef} tabIndex={0} className={mx('dx-expander relative outline-hidden', classNames)}>
|
|
101
108
|
{/* DOM Layout. */}
|
|
102
109
|
<div ref={gridRef} className='grid grid-rows-8 grid-cols-8 aspect-square select-none'>
|
|
103
110
|
{layout}
|
|
@@ -134,7 +141,7 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
|
|
|
134
141
|
piece={promoting}
|
|
135
142
|
onSelect={(piece) => {
|
|
136
143
|
onPromotion({
|
|
137
|
-
from: Object.values(
|
|
144
|
+
from: Object.values(pieces).find((p) => p.id === promoting.id)!.location,
|
|
138
145
|
to: piece.location,
|
|
139
146
|
piece: promoting.type,
|
|
140
147
|
promotion: piece.type,
|
|
@@ -147,7 +154,7 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
|
|
|
147
154
|
},
|
|
148
155
|
);
|
|
149
156
|
|
|
150
|
-
ChessboardComponent.displayName =
|
|
157
|
+
ChessboardComponent.displayName = CHESSBOARD_NAME;
|
|
151
158
|
|
|
152
159
|
export const Chessboard = memo(ChessboardComponent);
|
|
153
160
|
|
|
@@ -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,20 +31,19 @@ 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
|
-
moveNumber?: number;
|
|
37
36
|
onDrop?: (move: Move) => boolean;
|
|
38
37
|
}>;
|
|
39
38
|
|
|
40
39
|
/**
|
|
41
40
|
* Generic board container.
|
|
42
41
|
*/
|
|
43
|
-
const GameboardRoot = <M extends GameboardModel
|
|
42
|
+
const GameboardRoot = <M extends GameboardModel<any>>({ children, model, onDrop }: GameboardRootProps<M>) => {
|
|
44
43
|
const [dragging, setDragging] = useState(false);
|
|
45
44
|
const [promoting, setPromoting] = useState<PieceRecord | undefined>();
|
|
46
45
|
|
|
47
|
-
const handlePromotion = useCallback<GameboardContextValue<
|
|
46
|
+
const handlePromotion = useCallback<GameboardContextValue<GameboardModel<any>>['onPromotion']>((move) => {
|
|
48
47
|
log('onPromotion', { move });
|
|
49
48
|
setPromoting(undefined);
|
|
50
49
|
onDrop?.(move);
|
|
@@ -107,11 +106,10 @@ const GameboardContent = forwardRef<HTMLDivElement, GameboardContentProps>(
|
|
|
107
106
|
({ children, classNames, grow, contain }, forwardedRef) => {
|
|
108
107
|
return (
|
|
109
108
|
<div
|
|
110
|
-
|
|
111
|
-
className={mx(grow && 'grid is-full bs-full size-container place-content-center', classNames)}
|
|
109
|
+
className={mx(grow && 'dx-size-container dx-expander grid place-content-center', classNames)}
|
|
112
110
|
ref={forwardedRef}
|
|
113
111
|
>
|
|
114
|
-
{contain ? <div className='
|
|
112
|
+
{contain ? <div className='w-[min(100cqw,100cqh)] h-[min(100cqw,100cqh)]'>{children}</div> : children}
|
|
115
113
|
</div>
|
|
116
114
|
);
|
|
117
115
|
},
|
|
@@ -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';
|
|
@@ -26,8 +26,10 @@ export type PieceProps = ThemedClassName<{
|
|
|
26
26
|
onClick?: () => void;
|
|
27
27
|
}>;
|
|
28
28
|
|
|
29
|
+
const PIECE_NAME = 'Piece';
|
|
30
|
+
|
|
29
31
|
export const Piece = memo(({ classNames, Component, piece, bounds, label, onClick }: PieceProps) => {
|
|
30
|
-
const { model, dragging: isDragging, promoting } = useGameboardContext(
|
|
32
|
+
const { model, dragging: isDragging, promoting } = useGameboardContext(PIECE_NAME);
|
|
31
33
|
const promotingRef = useDynamicRef(promoting);
|
|
32
34
|
const [dragging, setDragging] = useState(false);
|
|
33
35
|
const [preview, setPreview] = useState<HTMLElement>();
|
|
@@ -134,4 +136,4 @@ export const Piece = memo(({ classNames, Component, piece, bounds, label, onClic
|
|
|
134
136
|
);
|
|
135
137
|
});
|
|
136
138
|
|
|
137
|
-
Piece.displayName =
|
|
139
|
+
Piece.displayName = PIECE_NAME;
|
|
@@ -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';
|
|
@@ -22,10 +22,12 @@ export type SquareProps = ThemedClassName<{
|
|
|
22
22
|
label?: string;
|
|
23
23
|
}>;
|
|
24
24
|
|
|
25
|
+
const SQUARE_NAME = 'Square';
|
|
26
|
+
|
|
25
27
|
export const Square = memo(({ location, bounds, label, classNames }: SquareProps) => {
|
|
26
28
|
const ref = useRef<HTMLDivElement>(null);
|
|
27
29
|
const [state, setState] = useState<HoveredState>('idle');
|
|
28
|
-
const { model } = useGameboardContext(
|
|
30
|
+
const { model } = useGameboardContext(SQUARE_NAME);
|
|
29
31
|
|
|
30
32
|
useEffect(() => {
|
|
31
33
|
const el = ref.current;
|
|
@@ -71,4 +73,4 @@ export const Square = memo(({ location, bounds, label, classNames }: SquareProps
|
|
|
71
73
|
);
|
|
72
74
|
});
|
|
73
75
|
|
|
74
|
-
Square.displayName =
|
|
76
|
+
Square.displayName = SQUARE_NAME;
|
|
@@ -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
|
}
|