@dxos/react-ui-gameboard 0.8.4-main.fd6878d → 0.8.4-staging.60fe92afc8

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 (58) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +2 -0
  3. package/dist/lib/browser/index.mjs +588 -735
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/node-esm/index.mjs +588 -735
  7. package/dist/lib/node-esm/index.mjs.map +4 -4
  8. package/dist/lib/node-esm/meta.json +1 -1
  9. package/dist/types/src/components/Chessboard/Chessboard.d.ts +9 -4
  10. package/dist/types/src/components/Chessboard/Chessboard.d.ts.map +1 -1
  11. package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts +21 -18
  12. package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts.map +1 -1
  13. package/dist/types/src/components/Chessboard/chess.d.ts +15 -8
  14. package/dist/types/src/components/Chessboard/chess.d.ts.map +1 -1
  15. package/dist/types/src/components/Chessboard/chess.test.d.ts +2 -0
  16. package/dist/types/src/components/Chessboard/chess.test.d.ts.map +1 -0
  17. package/dist/types/src/components/Gameboard/Gameboard.d.ts +11 -7
  18. package/dist/types/src/components/Gameboard/Gameboard.d.ts.map +1 -1
  19. package/dist/types/src/components/Gameboard/Piece.d.ts +1 -1
  20. package/dist/types/src/components/Gameboard/Piece.d.ts.map +1 -1
  21. package/dist/types/src/components/Gameboard/Square.d.ts.map +1 -1
  22. package/dist/types/src/components/Gameboard/types.d.ts +3 -3
  23. package/dist/types/src/components/Gameboard/types.d.ts.map +1 -1
  24. package/dist/types/src/components/Gameboard/util.d.ts.map +1 -1
  25. package/dist/types/src/gen/pieces/chess/alpha/bB.d.ts.map +1 -1
  26. package/dist/types/src/gen/pieces/chess/alpha/bK.d.ts.map +1 -1
  27. package/dist/types/src/gen/pieces/chess/alpha/bN.d.ts.map +1 -1
  28. package/dist/types/src/gen/pieces/chess/alpha/bP.d.ts.map +1 -1
  29. package/dist/types/src/gen/pieces/chess/alpha/bQ.d.ts.map +1 -1
  30. package/dist/types/src/gen/pieces/chess/alpha/bR.d.ts.map +1 -1
  31. package/dist/types/src/gen/pieces/chess/alpha/wB.d.ts.map +1 -1
  32. package/dist/types/src/gen/pieces/chess/alpha/wK.d.ts.map +1 -1
  33. package/dist/types/src/gen/pieces/chess/alpha/wN.d.ts.map +1 -1
  34. package/dist/types/src/gen/pieces/chess/alpha/wP.d.ts.map +1 -1
  35. package/dist/types/src/gen/pieces/chess/alpha/wQ.d.ts.map +1 -1
  36. package/dist/types/src/gen/pieces/chess/alpha/wR.d.ts.map +1 -1
  37. package/dist/types/src/gen/pieces/chess/cburnett/bB.d.ts.map +1 -1
  38. package/dist/types/src/gen/pieces/chess/cburnett/bK.d.ts.map +1 -1
  39. package/dist/types/src/gen/pieces/chess/cburnett/bN.d.ts.map +1 -1
  40. package/dist/types/src/gen/pieces/chess/cburnett/bP.d.ts.map +1 -1
  41. package/dist/types/src/gen/pieces/chess/cburnett/bQ.d.ts.map +1 -1
  42. package/dist/types/src/gen/pieces/chess/cburnett/bR.d.ts.map +1 -1
  43. package/dist/types/src/gen/pieces/chess/cburnett/wB.d.ts.map +1 -1
  44. package/dist/types/src/gen/pieces/chess/cburnett/wK.d.ts.map +1 -1
  45. package/dist/types/src/gen/pieces/chess/cburnett/wN.d.ts.map +1 -1
  46. package/dist/types/src/gen/pieces/chess/cburnett/wP.d.ts.map +1 -1
  47. package/dist/types/src/gen/pieces/chess/cburnett/wQ.d.ts.map +1 -1
  48. package/dist/types/src/gen/pieces/chess/cburnett/wR.d.ts.map +1 -1
  49. package/dist/types/tsconfig.tsbuildinfo +1 -1
  50. package/package.json +27 -27
  51. package/src/components/Chessboard/Chessboard.stories.tsx +20 -18
  52. package/src/components/Chessboard/Chessboard.tsx +44 -30
  53. package/src/components/Chessboard/chess.test.ts +19 -0
  54. package/src/components/Chessboard/chess.ts +111 -59
  55. package/src/components/Gameboard/Gameboard.tsx +8 -9
  56. package/src/components/Gameboard/Piece.tsx +13 -8
  57. package/src/components/Gameboard/Square.tsx +5 -3
  58. package/src/components/Gameboard/types.ts +3 -3
@@ -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
  onDrop?: (move: Move) => boolean;
37
37
  }>;
@@ -39,11 +39,11 @@ type GameboardRootProps<M extends GameboardModel> = PropsWithChildren<{
39
39
  /**
40
40
  * Generic board container.
41
41
  */
42
- const GameboardRoot = <M extends GameboardModel>({ children, model, onDrop }: GameboardRootProps<M>) => {
42
+ const GameboardRoot = <M extends GameboardModel<any>>({ children, model, onDrop }: GameboardRootProps<M>) => {
43
43
  const [dragging, setDragging] = useState(false);
44
44
  const [promoting, setPromoting] = useState<PieceRecord | undefined>();
45
45
 
46
- const handlePromotion = useCallback<GameboardContextValue<M>['onPromotion']>((move) => {
46
+ const handlePromotion = useCallback<GameboardContextValue<GameboardModel<any>>['onPromotion']>((move) => {
47
47
  log('onPromotion', { move });
48
48
  setPromoting(undefined);
49
49
  onDrop?.(move);
@@ -106,11 +106,10 @@ const GameboardContent = forwardRef<HTMLDivElement, GameboardContentProps>(
106
106
  ({ children, classNames, grow, contain }, forwardedRef) => {
107
107
  return (
108
108
  <div
109
- role='none'
110
- 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)}
111
110
  ref={forwardedRef}
112
111
  >
113
- {contain ? <div className='is-[min(100cqw,100cqh)] bs-[min(100cqw,100cqh)]'>{children}</div> : children}
112
+ {contain ? <div className='w-[min(100cqw,100cqh)] h-[min(100cqw,100cqh)]'>{children}</div> : children}
114
113
  </div>
115
114
  );
116
115
  },
@@ -10,8 +10,8 @@ import { createPortal } from 'react-dom';
10
10
 
11
11
  import { invariant } from '@dxos/invariant';
12
12
  import { log } from '@dxos/log';
13
- import { type ThemedClassName, useDynamicRef, useTrackProps } from '@dxos/react-ui';
14
- import { mx } from '@dxos/react-ui-theme';
13
+ import { type ThemedClassName, useDynamicRef } from '@dxos/react-ui';
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,9 +26,10 @@ export type PieceProps = ThemedClassName<{
26
26
  onClick?: () => void;
27
27
  }>;
28
28
 
29
- export const Piece = memo(({ classNames, Component, piece, orientation, bounds, label, onClick }: PieceProps) => {
30
- useTrackProps({ classNames, Component, piece, orientation, bounds, label }, Piece.displayName, false);
31
- const { model, dragging: isDragging, promoting } = useGameboardContext(Piece.displayName!);
29
+ const PIECE_NAME = 'Piece';
30
+
31
+ export const Piece = memo(({ classNames, Component, piece, bounds, label, onClick }: PieceProps) => {
32
+ const { model, dragging: isDragging, promoting } = useGameboardContext(PIECE_NAME);
32
33
  const promotingRef = useDynamicRef(promoting);
33
34
  const [dragging, setDragging] = useState(false);
34
35
  const [preview, setPreview] = useState<HTMLElement>();
@@ -38,6 +39,10 @@ export const Piece = memo(({ classNames, Component, piece, orientation, bounds,
38
39
 
39
40
  const ref = useRef<HTMLDivElement>(null);
40
41
  useEffect(() => {
42
+ if (!model) {
43
+ return;
44
+ }
45
+
41
46
  const el = ref.current;
42
47
  invariant(el);
43
48
 
@@ -60,7 +65,7 @@ export const Piece = memo(({ classNames, Component, piece, orientation, bounds,
60
65
  nativeSetDragImage,
61
66
  });
62
67
  },
63
- canDrag: () => !promotingRef.current && model?.turn === piece.side,
68
+ canDrag: () => !promotingRef.current && !model.readonly && model.turn === piece.side,
64
69
  onDragStart: () => setDragging(true),
65
70
  onDrop: ({ location: { current } }) => {
66
71
  // TODO(burdon): Create wrapper function to catch errors.
@@ -87,7 +92,7 @@ export const Piece = memo(({ classNames, Component, piece, orientation, bounds,
87
92
 
88
93
  // Check if piece moved.
89
94
  if (!current.location || !isEqualLocation(current.location, piece.location)) {
90
- ref.current.style.transition = 'top 400ms ease-out, left 400ms ease-out';
95
+ ref.current.style.transition = 'top 250ms ease-out, left 250ms ease-out';
91
96
  ref.current.style.top = bounds.top + 'px';
92
97
  ref.current.style.left = bounds.left + 'px';
93
98
  setCurrent({ location: piece.location, bounds });
@@ -131,4 +136,4 @@ export const Piece = memo(({ classNames, Component, piece, orientation, bounds,
131
136
  );
132
137
  });
133
138
 
134
- 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';
@@ -52,9 +52,9 @@ export const isEqualLocation = (l1: Location, l2: Location): boolean => l1[0] ==
52
52
  * Generic board model.
53
53
  */
54
54
  export interface GameboardModel<T extends PieceType = PieceType> {
55
+ readonly: boolean;
55
56
  turn: Player;
56
- /** @reactive */
57
- pieces: ReadonlySignal<PieceMap<T>>;
57
+ pieces: Atom.Atom<PieceMap<T>>;
58
58
  isValidMove: (move: Move) => boolean;
59
59
  canPromote?: (move: Move) => boolean;
60
60
  }