@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.
- package/dist/lib/browser/index.mjs +407 -381
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +407 -381
- 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 +15 -0
- package/dist/types/src/components/Chessboard/Chessboard.d.ts.map +1 -0
- package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts +28 -0
- package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts.map +1 -0
- package/dist/types/src/{Chessboard → components/Chessboard}/chess.d.ts +20 -7
- package/dist/types/src/components/Chessboard/chess.d.ts.map +1 -0
- package/dist/types/src/components/Chessboard/index.d.ts.map +1 -0
- package/dist/types/src/components/Gameboard/Gameboard.d.ts +37 -0
- package/dist/types/src/components/Gameboard/Gameboard.d.ts.map +1 -0
- package/dist/types/src/{Board → components/Gameboard}/Piece.d.ts +3 -2
- package/dist/types/src/components/Gameboard/Piece.d.ts.map +1 -0
- package/dist/types/src/components/Gameboard/Square.d.ts.map +1 -0
- package/dist/types/src/components/Gameboard/index.d.ts +4 -0
- package/dist/types/src/components/Gameboard/index.d.ts.map +1 -0
- package/dist/types/src/{Board → components/Gameboard}/types.d.ts +2 -1
- package/dist/types/src/components/Gameboard/types.d.ts.map +1 -0
- package/dist/types/src/components/Gameboard/util.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +3 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +1 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +19 -14
- package/src/{Chessboard → components/Chessboard}/Chessboard.stories.tsx +33 -30
- package/src/components/Chessboard/Chessboard.tsx +191 -0
- package/src/{Chessboard → components/Chessboard}/chess.ts +88 -28
- package/src/components/Gameboard/Gameboard.tsx +139 -0
- package/src/{Board → components/Gameboard}/Piece.tsx +19 -20
- package/src/{Board → components/Gameboard}/Square.tsx +4 -4
- package/src/components/Gameboard/index.ts +8 -0
- package/src/{Board → components/Gameboard}/types.ts +3 -1
- package/src/components/index.ts +6 -0
- package/src/index.ts +1 -2
- package/dist/lib/node/index.cjs +0 -1039
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
- package/dist/types/src/Board/Board.d.ts +0 -15
- package/dist/types/src/Board/Board.d.ts.map +0 -1
- package/dist/types/src/Board/Container.d.ts +0 -14
- package/dist/types/src/Board/Container.d.ts.map +0 -1
- package/dist/types/src/Board/Piece.d.ts.map +0 -1
- package/dist/types/src/Board/Square.d.ts.map +0 -1
- package/dist/types/src/Board/context.d.ts +0 -10
- package/dist/types/src/Board/context.d.ts.map +0 -1
- package/dist/types/src/Board/index.d.ts +0 -8
- package/dist/types/src/Board/index.d.ts.map +0 -1
- package/dist/types/src/Board/types.d.ts.map +0 -1
- package/dist/types/src/Board/util.d.ts.map +0 -1
- package/dist/types/src/Chessboard/Chessboard.d.ts +0 -14
- package/dist/types/src/Chessboard/Chessboard.d.ts.map +0 -1
- package/dist/types/src/Chessboard/Chessboard.stories.d.ts +0 -16
- package/dist/types/src/Chessboard/Chessboard.stories.d.ts.map +0 -1
- package/dist/types/src/Chessboard/chess.d.ts.map +0 -1
- package/dist/types/src/Chessboard/index.d.ts.map +0 -1
- package/src/Board/Board.tsx +0 -86
- package/src/Board/Container.tsx +0 -25
- package/src/Board/context.ts +0 -22
- package/src/Board/index.ts +0 -12
- package/src/Chessboard/Chessboard.tsx +0 -190
- /package/dist/types/src/{Chessboard → components/Chessboard}/index.d.ts +0 -0
- /package/dist/types/src/{Board → components/Gameboard}/Square.d.ts +0 -0
- /package/dist/types/src/{Board → components/Gameboard}/util.d.ts +0 -0
- /package/src/{Chessboard → components/Chessboard}/index.ts +0 -0
- /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, {
|
|
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
|
|
13
|
+
import { type ThemedClassName, useDynamicRef, useTrackProps } from '@dxos/react-ui';
|
|
15
14
|
import { mx } from '@dxos/react-ui-theme';
|
|
16
15
|
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
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
|
-
|
|
26
|
+
onClick?: () => void;
|
|
27
27
|
}>;
|
|
28
28
|
|
|
29
|
-
export const Piece = memo(({ classNames, piece, orientation, bounds, label,
|
|
30
|
-
useTrackProps({ classNames, piece, orientation, bounds, label
|
|
31
|
-
const { model } =
|
|
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,
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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, {
|
|
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 {
|
|
14
|
-
import {
|
|
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 } =
|
|
28
|
+
const { model } = useGameboardContext(Square.displayName!);
|
|
29
29
|
|
|
30
30
|
useEffect(() => {
|
|
31
31
|
const el = ref.current;
|
|
@@ -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
|
|
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;
|
package/src/index.ts
CHANGED