@dxos/react-ui-gameboard 0.8.3 → 0.8.4-main.28f8d3d

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 (70) hide show
  1. package/dist/lib/browser/index.mjs +407 -381
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +407 -381
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/components/Chessboard/Chessboard.d.ts +15 -0
  8. package/dist/types/src/components/Chessboard/Chessboard.d.ts.map +1 -0
  9. package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts +28 -0
  10. package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts.map +1 -0
  11. package/dist/types/src/{Chessboard → components/Chessboard}/chess.d.ts +20 -7
  12. package/dist/types/src/components/Chessboard/chess.d.ts.map +1 -0
  13. package/dist/types/src/components/Chessboard/index.d.ts.map +1 -0
  14. package/dist/types/src/components/Gameboard/Gameboard.d.ts +37 -0
  15. package/dist/types/src/components/Gameboard/Gameboard.d.ts.map +1 -0
  16. package/dist/types/src/{Board → components/Gameboard}/Piece.d.ts +3 -2
  17. package/dist/types/src/components/Gameboard/Piece.d.ts.map +1 -0
  18. package/dist/types/src/components/Gameboard/Square.d.ts.map +1 -0
  19. package/dist/types/src/components/Gameboard/index.d.ts +4 -0
  20. package/dist/types/src/components/Gameboard/index.d.ts.map +1 -0
  21. package/dist/types/src/{Board → components/Gameboard}/types.d.ts +2 -1
  22. package/dist/types/src/components/Gameboard/types.d.ts.map +1 -0
  23. package/dist/types/src/components/Gameboard/util.d.ts.map +1 -0
  24. package/dist/types/src/components/index.d.ts +3 -0
  25. package/dist/types/src/components/index.d.ts.map +1 -0
  26. package/dist/types/src/index.d.ts +1 -2
  27. package/dist/types/src/index.d.ts.map +1 -1
  28. package/dist/types/tsconfig.tsbuildinfo +1 -1
  29. package/package.json +19 -14
  30. package/src/{Chessboard → components/Chessboard}/Chessboard.stories.tsx +33 -30
  31. package/src/components/Chessboard/Chessboard.tsx +191 -0
  32. package/src/{Chessboard → components/Chessboard}/chess.ts +88 -28
  33. package/src/components/Gameboard/Gameboard.tsx +139 -0
  34. package/src/{Board → components/Gameboard}/Piece.tsx +19 -20
  35. package/src/{Board → components/Gameboard}/Square.tsx +4 -4
  36. package/src/components/Gameboard/index.ts +8 -0
  37. package/src/{Board → components/Gameboard}/types.ts +3 -1
  38. package/src/components/index.ts +6 -0
  39. package/src/index.ts +1 -2
  40. package/dist/lib/node/index.cjs +0 -1039
  41. package/dist/lib/node/index.cjs.map +0 -7
  42. package/dist/lib/node/meta.json +0 -1
  43. package/dist/types/src/Board/Board.d.ts +0 -15
  44. package/dist/types/src/Board/Board.d.ts.map +0 -1
  45. package/dist/types/src/Board/Container.d.ts +0 -14
  46. package/dist/types/src/Board/Container.d.ts.map +0 -1
  47. package/dist/types/src/Board/Piece.d.ts.map +0 -1
  48. package/dist/types/src/Board/Square.d.ts.map +0 -1
  49. package/dist/types/src/Board/context.d.ts +0 -10
  50. package/dist/types/src/Board/context.d.ts.map +0 -1
  51. package/dist/types/src/Board/index.d.ts +0 -8
  52. package/dist/types/src/Board/index.d.ts.map +0 -1
  53. package/dist/types/src/Board/types.d.ts.map +0 -1
  54. package/dist/types/src/Board/util.d.ts.map +0 -1
  55. package/dist/types/src/Chessboard/Chessboard.d.ts +0 -14
  56. package/dist/types/src/Chessboard/Chessboard.d.ts.map +0 -1
  57. package/dist/types/src/Chessboard/Chessboard.stories.d.ts +0 -16
  58. package/dist/types/src/Chessboard/Chessboard.stories.d.ts.map +0 -1
  59. package/dist/types/src/Chessboard/chess.d.ts.map +0 -1
  60. package/dist/types/src/Chessboard/index.d.ts.map +0 -1
  61. package/src/Board/Board.tsx +0 -86
  62. package/src/Board/Container.tsx +0 -25
  63. package/src/Board/context.ts +0 -22
  64. package/src/Board/index.ts +0 -12
  65. package/src/Chessboard/Chessboard.tsx +0 -190
  66. /package/dist/types/src/{Chessboard → components/Chessboard}/index.d.ts +0 -0
  67. /package/dist/types/src/{Board → components/Gameboard}/Square.d.ts +0 -0
  68. /package/dist/types/src/{Board → components/Gameboard}/util.d.ts +0 -0
  69. /package/src/{Chessboard → components/Chessboard}/index.ts +0 -0
  70. /package/src/{Board → components/Gameboard}/util.ts +0 -0
@@ -0,0 +1,139 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { monitorForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter';
6
+ import { createContext } from '@radix-ui/react-context';
7
+ import React, { type PropsWithChildren, forwardRef, useCallback, useEffect, useState } from 'react';
8
+
9
+ import { log } from '@dxos/log';
10
+ import { type ThemedClassName } from '@dxos/react-ui';
11
+ import { mx } from '@dxos/react-ui-theme';
12
+
13
+ import { Piece, type PieceProps } from './Piece';
14
+ import { Square, type SquareProps } from './Square';
15
+ import { type GameboardModel, type Move, type PieceRecord, isLocation, isPiece } from './types';
16
+
17
+ export type GameboardContextValue<M extends GameboardModel> = {
18
+ model: M;
19
+ dragging?: boolean; // TODO(burdon): Change to PieceRecord.
20
+ promoting?: PieceRecord;
21
+ onPromotion: (move: Move) => void;
22
+ };
23
+
24
+ const [GameboardContextProvider, useRadixGameboardContext] = createContext<GameboardContextValue<any>>('Gameboard');
25
+
26
+ const useGameboardContext = <M extends GameboardModel>(consumerName: string): GameboardContextValue<M> => {
27
+ return useRadixGameboardContext(consumerName);
28
+ };
29
+
30
+ //
31
+ // Root
32
+ //
33
+
34
+ type GameboardRootProps<M extends GameboardModel> = PropsWithChildren<{
35
+ model?: M;
36
+ onDrop?: (move: Move) => boolean;
37
+ }>;
38
+
39
+ /**
40
+ * Generic board container.
41
+ */
42
+ const GameboardRoot = <M extends GameboardModel>({ children, model, onDrop }: GameboardRootProps<M>) => {
43
+ const [dragging, setDragging] = useState(false);
44
+ const [promoting, setPromoting] = useState<PieceRecord | undefined>();
45
+
46
+ const handlePromotion = useCallback<GameboardContextValue<M>['onPromotion']>((move) => {
47
+ log('onPromotion', { move });
48
+ setPromoting(undefined);
49
+ onDrop?.(move);
50
+ }, []);
51
+
52
+ useEffect(() => {
53
+ if (!model) {
54
+ return;
55
+ }
56
+
57
+ // TODO(burdon): Should target specific container.
58
+ return monitorForElements({
59
+ onDragStart: ({ source }) => {
60
+ log('onDragStart', { source });
61
+ setDragging(true);
62
+ },
63
+ onDrop: ({ source, location }) => {
64
+ log('onDrop', { source, location });
65
+ const target = location.current.dropTargets[0];
66
+ if (!target) {
67
+ return;
68
+ }
69
+
70
+ const targetLocation = target.data.location;
71
+ const piece = source.data.piece;
72
+ if (!isLocation(targetLocation) || !isPiece(piece)) {
73
+ return;
74
+ }
75
+
76
+ const move: Move = { from: piece.location, to: targetLocation, piece: piece.type };
77
+ if (model.isValidMove(move)) {
78
+ if (model.canPromote?.(move)) {
79
+ setPromoting({ ...piece, location: targetLocation });
80
+ } else {
81
+ onDrop?.(move);
82
+ }
83
+ }
84
+
85
+ setDragging(false);
86
+ },
87
+ });
88
+ }, [model]);
89
+
90
+ return (
91
+ <GameboardContextProvider model={model} dragging={dragging} promoting={promoting} onPromotion={handlePromotion}>
92
+ {children}
93
+ </GameboardContextProvider>
94
+ );
95
+ };
96
+
97
+ GameboardRoot.displayName = 'Gameboard.Root';
98
+
99
+ //
100
+ // Content
101
+ //
102
+
103
+ type GameboardContentProps = ThemedClassName<PropsWithChildren<{ grow?: boolean; contain?: boolean }>>;
104
+
105
+ const GameboardContent = forwardRef<HTMLDivElement, GameboardContentProps>(
106
+ ({ children, classNames, grow, contain }, forwardedRef) => {
107
+ return (
108
+ <div
109
+ role='none'
110
+ className={mx(grow && 'grid is-full bs-full size-container place-content-center', classNames)}
111
+ ref={forwardedRef}
112
+ >
113
+ {contain ? <div className='is-[min(100cqw,100cqh)] bs-[min(100cqw,100cqh)]'>{children}</div> : children}
114
+ </div>
115
+ );
116
+ },
117
+ );
118
+
119
+ GameboardContent.displayName = 'Gameboard.Content';
120
+
121
+ //
122
+ // Gameboard
123
+ //
124
+
125
+ export const Gameboard = {
126
+ Root: GameboardRoot,
127
+ Content: GameboardContent,
128
+ Piece,
129
+ Square,
130
+ };
131
+
132
+ export { useGameboardContext };
133
+
134
+ export type {
135
+ GameboardRootProps,
136
+ GameboardContentProps,
137
+ PieceProps as GameboardPieceProps,
138
+ SquareProps as GameboardSquareProps,
139
+ };
@@ -3,34 +3,32 @@
3
3
  //
4
4
 
5
5
  import { draggable } from '@atlaskit/pragmatic-drag-and-drop/element/adapter';
6
- // import { preserveOffsetOnSource } from '@atlaskit/pragmatic-drag-and-drop/element/preserve-offset-on-source';
7
6
  import { centerUnderPointer } from '@atlaskit/pragmatic-drag-and-drop/element/center-under-pointer';
8
7
  import { setCustomNativeDragPreview } from '@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview';
9
- import React, { useState, useRef, useEffect, type FC, type SVGProps, memo } from 'react';
8
+ import React, { type FC, type SVGProps, memo, useEffect, useRef, useState } from 'react';
10
9
  import { createPortal } from 'react-dom';
11
10
 
12
11
  import { invariant } from '@dxos/invariant';
13
12
  import { log } from '@dxos/log';
14
- import { useDynamicRef, useTrackProps, type ThemedClassName } from '@dxos/react-ui';
13
+ import { type ThemedClassName, useDynamicRef, useTrackProps } from '@dxos/react-ui';
15
14
  import { mx } from '@dxos/react-ui-theme';
16
15
 
17
- import { useBoardContext } from './context';
18
- import { isEqualLocation, isLocation, type Location, type PieceRecord, type Player } from './types';
16
+ import { useGameboardContext } from './Gameboard';
17
+ import { type Location, type PieceRecord, type Player, isEqualLocation, isLocation } from './types';
19
18
  import { type DOMRectBounds } from './util';
20
19
 
21
20
  export type PieceProps = ThemedClassName<{
21
+ Component: FC<SVGProps<SVGSVGElement>>;
22
22
  piece: PieceRecord;
23
23
  bounds: DOMRectBounds;
24
24
  label?: string;
25
25
  orientation?: Player;
26
- Component: FC<SVGProps<SVGSVGElement>>;
26
+ onClick?: () => void;
27
27
  }>;
28
28
 
29
- export const Piece = memo(({ classNames, piece, orientation, bounds, label, Component }: PieceProps) => {
30
- useTrackProps({ classNames, piece, orientation, bounds, label, Component }, Piece.displayName, false);
31
- const { model } = useBoardContext();
32
-
33
- const { dragging: isDragging, promoting } = useBoardContext();
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!);
34
32
  const promotingRef = useDynamicRef(promoting);
35
33
  const [dragging, setDragging] = useState(false);
36
34
  const [preview, setPreview] = useState<HTMLElement>();
@@ -46,14 +44,10 @@ export const Piece = memo(({ classNames, piece, orientation, bounds, label, Comp
46
44
  return draggable({
47
45
  element: el,
48
46
  getInitialData: () => ({ piece }),
49
- onGenerateDragPreview: ({ nativeSetDragImage, location, source }) => {
47
+ onGenerateDragPreview: ({ nativeSetDragImage, source }) => {
50
48
  log('onGenerateDragPreview', { source: source.data });
51
49
  setCustomNativeDragPreview({
52
50
  getOffset: centerUnderPointer,
53
- // getOffset: preserveOffsetOnSource({
54
- // element: source.element,
55
- // input: location.current.input,
56
- // }),
57
51
  render: ({ container }) => {
58
52
  setPreview(container);
59
53
  const { width, height } = el.getBoundingClientRect();
@@ -69,9 +63,14 @@ export const Piece = memo(({ classNames, piece, orientation, bounds, label, Comp
69
63
  canDrag: () => !promotingRef.current && model?.turn === piece.side,
70
64
  onDragStart: () => setDragging(true),
71
65
  onDrop: ({ location: { current } }) => {
72
- const location = current.dropTargets[0].data.location;
73
- if (isLocation(location)) {
74
- setCurrent((current) => ({ ...current, location }));
66
+ // TODO(burdon): Create wrapper function to catch errors.
67
+ try {
68
+ const location = current.dropTargets[0]?.data.location;
69
+ if (isLocation(location)) {
70
+ setCurrent((current) => ({ ...current, location }));
71
+ }
72
+ } catch {
73
+ // Ignore.
75
74
  }
76
75
 
77
76
  setDragging(false);
@@ -112,10 +111,10 @@ export const Piece = memo(({ classNames, piece, orientation, bounds, label, Comp
112
111
  className={mx(
113
112
  'absolute',
114
113
  classNames,
115
- // orientation === 'black' && '_rotate-180',
116
114
  dragging && 'opacity-20', // Must not unmount component while dragging.
117
115
  isDragging && 'pointer-events-none', // Don't block the square's drop target.
118
116
  )}
117
+ onClick={onClick}
119
118
  >
120
119
  <Component className='grow' />
121
120
  {label && <div className='absolute inset-1 text-xs text-black'>{label}</div>}
@@ -3,15 +3,15 @@
3
3
  //
4
4
 
5
5
  import { dropTargetForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter';
6
- import React, { useRef, useState, useEffect, memo } from 'react';
6
+ import React, { memo, useEffect, useRef, useState } from 'react';
7
7
 
8
8
  import { invariant } from '@dxos/invariant';
9
9
  import { log } from '@dxos/log';
10
10
  import { type ThemedClassName } from '@dxos/react-ui';
11
11
  import { mx } from '@dxos/react-ui-theme';
12
12
 
13
- import { useBoardContext } from './context';
14
- import { isPiece, type Location } from './types';
13
+ import { useGameboardContext } from './Gameboard';
14
+ import { type Location, isPiece } from './types';
15
15
  import { type DOMRectBounds } from './util';
16
16
 
17
17
  type HoveredState = 'idle' | 'validMove' | 'invalidMove';
@@ -25,7 +25,7 @@ export type SquareProps = ThemedClassName<{
25
25
  export const Square = memo(({ location, bounds, label, classNames }: SquareProps) => {
26
26
  const ref = useRef<HTMLDivElement>(null);
27
27
  const [state, setState] = useState<HoveredState>('idle');
28
- const { model } = useBoardContext();
28
+ const { model } = useGameboardContext(Square.displayName!);
29
29
 
30
30
  useEffect(() => {
31
31
  const el = ref.current;
@@ -0,0 +1,8 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export * from './types';
6
+ export * from './util';
7
+
8
+ export * from './Gameboard';
@@ -4,6 +4,7 @@
4
4
 
5
5
  import { type ReadonlySignal } from '@preact/signals-core';
6
6
 
7
+ // TODO(burdon): Don't make this assumption.
7
8
  export type Player = 'black' | 'white';
8
9
 
9
10
  export type Location = [number, number];
@@ -50,8 +51,9 @@ export const isEqualLocation = (l1: Location, l2: Location): boolean => l1[0] ==
50
51
  /**
51
52
  * Generic board model.
52
53
  */
53
- export interface BoardModel<T extends PieceType = PieceType> {
54
+ export interface GameboardModel<T extends PieceType = PieceType> {
54
55
  turn: Player;
56
+ /** @reactive */
55
57
  pieces: ReadonlySignal<PieceMap<T>>;
56
58
  isValidMove: (move: Move) => boolean;
57
59
  canPromote?: (move: Move) => boolean;
@@ -0,0 +1,6 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export * from './Gameboard';
6
+ export * from './Chessboard';
package/src/index.ts CHANGED
@@ -2,5 +2,4 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * from './Board';
6
- export * from './Chessboard';
5
+ export * from './components';