@dxos/react-ui-gameboard 0.8.4-main.e8ec1fe → 0.8.4-main.ef1bc66f44

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.
Files changed (27) hide show
  1. package/dist/lib/browser/index.mjs +542 -664
  2. package/dist/lib/browser/index.mjs.map +3 -3
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +542 -664
  5. package/dist/lib/node-esm/index.mjs.map +3 -3
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/components/Chessboard/Chessboard.d.ts +1 -1
  8. package/dist/types/src/components/Chessboard/Chessboard.d.ts.map +1 -1
  9. package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts +1 -1
  10. package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts.map +1 -1
  11. package/dist/types/src/components/Chessboard/chess.d.ts +5 -4
  12. package/dist/types/src/components/Chessboard/chess.d.ts.map +1 -1
  13. package/dist/types/src/components/Gameboard/Gameboard.d.ts +5 -5
  14. package/dist/types/src/components/Gameboard/Gameboard.d.ts.map +1 -1
  15. package/dist/types/src/components/Gameboard/Piece.d.ts.map +1 -1
  16. package/dist/types/src/components/Gameboard/Square.d.ts.map +1 -1
  17. package/dist/types/src/components/Gameboard/types.d.ts +2 -3
  18. package/dist/types/src/components/Gameboard/types.d.ts.map +1 -1
  19. package/dist/types/tsconfig.tsbuildinfo +1 -1
  20. package/package.json +25 -22
  21. package/src/components/Chessboard/Chessboard.stories.tsx +8 -4
  22. package/src/components/Chessboard/Chessboard.tsx +14 -6
  23. package/src/components/Chessboard/chess.ts +13 -10
  24. package/src/components/Gameboard/Gameboard.tsx +11 -6
  25. package/src/components/Gameboard/Piece.tsx +5 -3
  26. package/src/components/Gameboard/Square.tsx +5 -3
  27. 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.e8ec1fe",
3
+ "version": "0.8.4-main.ef1bc66f44",
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": "^1.4.0",
28
- "@atlaskit/pragmatic-drag-and-drop-hitbox": "^1.0.3",
29
- "@preact-signals/safe-react": "^0.9.0",
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.5.0",
31
34
  "@radix-ui/react-context": "1.1.1",
32
- "chess.js": "^1.4.0",
35
+ "chess.js": "^1.0.0",
33
36
  "react-resize-detector": "^11.0.1",
34
- "@dxos/debug": "0.8.4-main.e8ec1fe",
35
- "@dxos/invariant": "0.8.4-main.e8ec1fe",
36
- "@dxos/log": "0.8.4-main.e8ec1fe",
37
- "@dxos/util": "0.8.4-main.e8ec1fe",
38
- "@dxos/node-std": "0.8.4-main.e8ec1fe"
37
+ "@dxos/debug": "0.8.4-main.ef1bc66f44",
38
+ "@dxos/invariant": "0.8.4-main.ef1bc66f44",
39
+ "@dxos/node-std": "0.8.4-main.ef1bc66f44",
40
+ "@dxos/log": "0.8.4-main.ef1bc66f44",
41
+ "@dxos/util": "0.8.4-main.ef1bc66f44"
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.2",
44
- "@types/react-dom": "~19.2.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.0",
47
- "react-dom": "~19.2.0",
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.e8ec1fe",
50
- "@dxos/react-ui-theme": "0.8.4-main.e8ec1fe",
51
- "@dxos/storybook-utils": "0.8.4-main.e8ec1fe"
52
+ "@dxos/react-ui": "0.8.4-main.ef1bc66f44",
53
+ "@dxos/ui-theme": "0.8.4-main.ef1bc66f44",
54
+ "@dxos/storybook-utils": "0.8.4-main.ef1bc66f44"
52
55
  },
53
56
  "peerDependencies": {
54
- "react": "^19.0.0",
55
- "react-dom": "^19.0.0",
56
- "@dxos/react-ui": "0.8.4-main.e8ec1fe",
57
- "@dxos/react-ui-theme": "0.8.4-main.e8ec1fe"
57
+ "react": "~19.2.3",
58
+ "react-dom": "~19.2.3",
59
+ "@dxos/react-ui": "0.8.4-main.ef1bc66f44",
60
+ "@dxos/ui-theme": "0.8.4-main.ef1bc66f44"
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 model = useMemo(() => new ChessModel(pgn), [pgn]);
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 models = useMemo(() => Array.from({ length: 9 }).map(() => new ChessModel()), []);
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)];
@@ -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({ container: 'column' })],
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/react-ui-theme';
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;
@@ -35,11 +40,14 @@ export type ChessboardProps = ThemedClassName<
35
40
  /**
36
41
  * Chessboard layout.
37
42
  */
43
+ const CHESSBOARD_NAME = 'Chessboard';
44
+
38
45
  const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
39
46
  ({ classNames, orientation, showLabels, debug, rows = 8, cols = 8 }, forwardedRef) => {
40
47
  const targetRef = useForwardedRef(forwardedRef);
41
48
  const { width, height } = useResizeDetector({ targetRef, refreshRate: 200 });
42
- const { model, promoting, onPromotion } = useGameboardContext<ChessModel>(Chessboard.displayName!);
49
+ const { model, promoting, onPromotion } = useGameboardContext<ChessModel>(CHESSBOARD_NAME);
50
+ const pieces = useAtomValue(model?.pieces ?? EMPTY_PIECES_ATOM);
43
51
 
44
52
  // Board squares.
45
53
  const squares = useMemo<Location[]>(() => {
@@ -84,7 +92,7 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
84
92
  return [];
85
93
  }
86
94
 
87
- return Object.values(model?.pieces.value ?? {})
95
+ return Object.values(pieces)
88
96
  .map((piece) => {
89
97
  if (piece.id === promoting?.id) {
90
98
  return null;
@@ -94,7 +102,7 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
94
102
  return { piece, bounds };
95
103
  })
96
104
  .filter(isNonNullable);
97
- }, [grid, model?.pieces.value, promoting]);
105
+ }, [grid, pieces, promoting]);
98
106
 
99
107
  return (
100
108
  <div ref={targetRef} tabIndex={0} className={mx('relative outline-none', classNames)}>
@@ -134,7 +142,7 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
134
142
  piece={promoting}
135
143
  onSelect={(piece) => {
136
144
  onPromotion({
137
- from: Object.values(model.pieces.value).find((p) => p.id === promoting.id)!.location,
145
+ from: Object.values(pieces).find((p) => p.id === promoting.id)!.location,
138
146
  to: piece.location,
139
147
  piece: promoting.type,
140
148
  promotion: piece.type,
@@ -147,7 +155,7 @@ const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
147
155
  },
148
156
  );
149
157
 
150
- ChessboardComponent.displayName = 'Chessboard';
158
+ ChessboardComponent.displayName = CHESSBOARD_NAME;
151
159
 
152
160
  export const Chessboard = memo(ChessboardComponent);
153
161
 
@@ -2,7 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { type ReadonlySignal, signal } from '@preact/signals-core';
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 = signal<PieceMap<ChessPiece>>({});
89
- private readonly _moveIndex = signal(0);
88
+ private readonly _pieces = Atom.make<PieceMap<ChessPiece>>({});
89
+ private readonly _moveIndex = Atom.make(0);
90
90
 
91
- constructor(pgn?: string) {
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.value !== this._chess.history().length;
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(): ReadonlySignal<PieceMap<ChessPiece>> {
110
+ get pieces(): Atom.Atom<PieceMap<ChessPiece>> {
108
111
  return this._pieces;
109
112
  }
110
113
 
111
- get moveIndex(): ReadonlySignal<number> {
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.value = {};
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.value = createPieceMap(chess);
205
- this._moveIndex.value = chess.history().length;
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/react-ui-theme';
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>(consumerName: string): GameboardContextValue<M> => {
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> = PropsWithChildren<{
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>({ children, model, moveNumber, onDrop }: GameboardRootProps<M>) => {
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<M>['onPromotion']>((move) => {
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/react-ui-theme';
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(Piece.displayName!);
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 = 'Piece';
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/react-ui-theme';
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(Square.displayName!);
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 = 'Square';
76
+ Square.displayName = SQUARE_NAME;
@@ -2,7 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { type ReadonlySignal } from '@preact/signals-core';
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
- /** @reactive */
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
  }