@dxos/react-ui-gameboard 0.8.4-main.c1de068 → 0.8.4-main.fd6878d

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 (61) hide show
  1. package/dist/lib/browser/index.mjs +309 -274
  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 +309 -274
  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.map +1 -0
  8. package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts +28 -0
  9. package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts.map +1 -0
  10. package/dist/types/src/{Chessboard → components/Chessboard}/chess.d.ts +19 -6
  11. package/dist/types/src/components/Chessboard/chess.d.ts.map +1 -0
  12. package/dist/types/src/components/Chessboard/index.d.ts.map +1 -0
  13. package/dist/types/src/components/Gameboard/Gameboard.d.ts +37 -0
  14. package/dist/types/src/components/Gameboard/Gameboard.d.ts.map +1 -0
  15. package/dist/types/src/{Gameboard → components/Gameboard}/Piece.d.ts +3 -2
  16. package/dist/types/src/components/Gameboard/Piece.d.ts.map +1 -0
  17. package/dist/types/src/components/Gameboard/Square.d.ts.map +1 -0
  18. package/dist/types/src/components/Gameboard/index.d.ts +4 -0
  19. package/dist/types/src/components/Gameboard/index.d.ts.map +1 -0
  20. package/dist/types/src/{Gameboard → components/Gameboard}/types.d.ts +1 -0
  21. package/dist/types/src/components/Gameboard/types.d.ts.map +1 -0
  22. package/dist/types/src/components/Gameboard/util.d.ts.map +1 -0
  23. package/dist/types/src/components/index.d.ts +3 -0
  24. package/dist/types/src/components/index.d.ts.map +1 -0
  25. package/dist/types/src/index.d.ts +1 -2
  26. package/dist/types/src/index.d.ts.map +1 -1
  27. package/dist/types/tsconfig.tsbuildinfo +1 -1
  28. package/package.json +14 -12
  29. package/src/{Chessboard → components/Chessboard}/Chessboard.stories.tsx +26 -25
  30. package/src/{Chessboard → components/Chessboard}/Chessboard.tsx +32 -35
  31. package/src/{Chessboard → components/Chessboard}/chess.ts +86 -26
  32. package/src/components/Gameboard/Gameboard.tsx +139 -0
  33. package/src/{Gameboard → components/Gameboard}/Piece.tsx +19 -20
  34. package/src/{Gameboard → components/Gameboard}/Square.tsx +4 -4
  35. package/src/{Gameboard → components/Gameboard}/index.ts +0 -3
  36. package/src/{Gameboard → components/Gameboard}/types.ts +2 -0
  37. package/src/components/index.ts +6 -0
  38. package/src/index.ts +1 -2
  39. package/dist/types/src/Chessboard/Chessboard.d.ts.map +0 -1
  40. package/dist/types/src/Chessboard/Chessboard.stories.d.ts +0 -16
  41. package/dist/types/src/Chessboard/Chessboard.stories.d.ts.map +0 -1
  42. package/dist/types/src/Chessboard/chess.d.ts.map +0 -1
  43. package/dist/types/src/Chessboard/index.d.ts.map +0 -1
  44. package/dist/types/src/Gameboard/Gameboard.d.ts +0 -23
  45. package/dist/types/src/Gameboard/Gameboard.d.ts.map +0 -1
  46. package/dist/types/src/Gameboard/Piece.d.ts.map +0 -1
  47. package/dist/types/src/Gameboard/Square.d.ts.map +0 -1
  48. package/dist/types/src/Gameboard/context.d.ts +0 -10
  49. package/dist/types/src/Gameboard/context.d.ts.map +0 -1
  50. package/dist/types/src/Gameboard/index.d.ts +0 -7
  51. package/dist/types/src/Gameboard/index.d.ts.map +0 -1
  52. package/dist/types/src/Gameboard/types.d.ts.map +0 -1
  53. package/dist/types/src/Gameboard/util.d.ts.map +0 -1
  54. package/src/Gameboard/Gameboard.tsx +0 -103
  55. package/src/Gameboard/context.ts +0 -22
  56. /package/dist/types/src/{Chessboard → components/Chessboard}/Chessboard.d.ts +0 -0
  57. /package/dist/types/src/{Chessboard → components/Chessboard}/index.d.ts +0 -0
  58. /package/dist/types/src/{Gameboard → components/Gameboard}/Square.d.ts +0 -0
  59. /package/dist/types/src/{Gameboard → components/Gameboard}/util.d.ts +0 -0
  60. /package/src/{Chessboard → components/Chessboard}/index.ts +0 -0
  61. /package/src/{Gameboard → components/Gameboard}/util.ts +0 -0
@@ -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;
@@ -2,10 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * from './context';
6
5
  export * from './types';
7
6
  export * from './util';
8
7
 
9
8
  export * from './Gameboard';
10
- export * from './Square';
11
- export * from './Piece';
@@ -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];
@@ -52,6 +53,7 @@ export const isEqualLocation = (l1: Location, l2: Location): boolean => l1[0] ==
52
53
  */
53
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 './Gameboard';
6
- export * from './Chessboard';
5
+ export * from './components';
@@ -1 +0,0 @@
1
- {"version":3,"file":"Chessboard.d.ts","sourceRoot":"","sources":["../../../../src/Chessboard/Chessboard.tsx"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,EAAE,KAAK,iBAAiB,EAA8C,MAAM,OAAO,CAAC;AAGlG,OAAO,EAAE,KAAK,eAAe,EAAiB,MAAM,gBAAgB,CAAC;AAKrE,OAAO,EAIL,KAAK,MAAM,EAMZ,MAAM,cAAc,CAAC;AAEtB,MAAM,MAAM,eAAe,GAAG,eAAe,CAC3C,iBAAiB,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC,CACH,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,UAAU,yFACgD,eAAe,uBA0GrF,CAAC"}
@@ -1,16 +0,0 @@
1
- import '@dxos-theme';
2
- import type { Meta, StoryObj } from '@storybook/react-vite';
3
- import React from 'react';
4
- import { type ChessboardProps } from './Chessboard';
5
- type RenderProps = Pick<ChessboardProps, 'orientation' | 'showLabels' | 'debug'> & {
6
- fen: string;
7
- };
8
- declare const DefaultStory: ({ fen, orientation: _orientation, ...props }: RenderProps) => React.JSX.Element;
9
- declare const meta: Meta<typeof DefaultStory>;
10
- export default meta;
11
- type Story = StoryObj<typeof DefaultStory>;
12
- export declare const Default: Story;
13
- export declare const Promotion: Story;
14
- export declare const Debug: Story;
15
- export declare const Nine: Story;
16
- //# sourceMappingURL=Chessboard.stories.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Chessboard.stories.d.ts","sourceRoot":"","sources":["../../../../src/Chessboard/Chessboard.stories.tsx"],"names":[],"mappings":"AAIA,OAAO,aAAa,CAAC;AAErB,OAAO,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,KAAoD,MAAM,OAAO,CAAC;AAMzE,OAAO,EAAc,KAAK,eAAe,EAAE,MAAM,cAAc,CAAC;AAIhE,KAAK,WAAW,GAAG,IAAI,CAAC,eAAe,EAAE,aAAa,GAAG,YAAY,GAAG,OAAO,CAAC,GAAG;IACjF,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,QAAA,MAAM,YAAY,GAAI,8CAA8C,WAAW,sBA+B9E,CAAC;AA2BF,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,OAAO,YAAY,CAKnC,CAAC;AAEF,eAAe,IAAI,CAAC;AAEpB,KAAK,KAAK,GAAG,QAAQ,CAAC,OAAO,YAAY,CAAC,CAAC;AAE3C,eAAO,MAAM,OAAO,EAAE,KAAU,CAAC;AAEjC,eAAO,MAAM,SAAS,EAAE,KAIvB,CAAC;AAEF,eAAO,MAAM,KAAK,EAAE,KAOnB,CAAC;AAEF,eAAO,MAAM,IAAI,EAAE,KAElB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"chess.d.ts","sourceRoot":"","sources":["../../../../src/Chessboard/chess.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,cAAc,EAAU,MAAM,sBAAsB,CAAC;AACnE,OAAO,EAAE,KAAK,EAAe,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,EAAE,KAAK,QAAQ,EAAE,MAAM,OAAO,CAAC;AAI/C,OAAO,EACL,KAAK,IAAI,EACT,KAAK,QAAQ,EACb,KAAK,QAAQ,EAEb,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,KAAK,MAAM,EACZ,MAAM,cAAc,CAAC;AAGtB,MAAM,MAAM,UAAU,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;AAE3G,eAAO,MAAM,WAAW,EAAE,MAAM,CAAC,UAAU,EAAE,EAAE,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAS,CAAC;AAElF,eAAO,MAAM,aAAa,GAAI,KAAK,MAAM,KAAG,QAI3C,CAAC;AAEF,eAAO,MAAM,aAAa,GAAI,YAAY,QAAQ,KAAG,MAEpD,CAAC;AAyBF,eAAO,MAAM,WAAW;;;;CAAkB,CAAC;AAE3C,eAAO,MAAM,cAAc,GAAI,YAAY,QAAQ,WAElD,CAAC;AAmBF;;GAEG;AACH,qBAAa,UAAW,YAAW,cAAc,CAAC,UAAU,CAAC;IAC3D,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAoC;gBAEhD,GAAG,CAAC,EAAE,MAAM;IAIxB,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,IAAI,MAAM,IAAI,cAAc,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAEjD;IAED,IAAI,IAAI,IAAI,KAAK,CAEhB;IAED,IAAI,GAAG,IAAI,MAAM,CAEhB;IAED,UAAU,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI;IAM9B,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO;IAIhC,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO;IAM/B,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO;IAW7B,cAAc,IAAI,OAAO;IAYzB;;OAEG;IACH,OAAO,CAAC,OAAO;CAsBhB;AAED;;GAEG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,SAAS,EAAE,QAAQ,QAAQ,CAAC,CAAC,CAAC,EAAE,OAAO,QAAQ,CAAC,CAAC,CAAC,KAAG,QAAQ,CAAC,CAAC,CAmClG,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/Chessboard/index.ts"],"names":[],"mappings":"AAIA,cAAc,SAAS,CAAC;AAExB,cAAc,cAAc,CAAC"}
@@ -1,23 +0,0 @@
1
- import React, { type CSSProperties, type PropsWithChildren } from 'react';
2
- import { type ThemedClassName } from '@dxos/react-ui';
3
- import { type GameboardModel, type Move } from './types';
4
- type GameboardRootProps = PropsWithChildren<{
5
- model?: GameboardModel;
6
- onDrop?: (move: Move) => boolean;
7
- }>;
8
- type GameboardContentsProps = ThemedClassName<PropsWithChildren<{
9
- style?: CSSProperties;
10
- }>>;
11
- export declare const Gameboard: {
12
- Root: {
13
- ({ children, model, onDrop }: GameboardRootProps): React.JSX.Element;
14
- displayName: string;
15
- };
16
- Content: React.ForwardRefExoticComponent<Omit<React.PropsWithChildren<{
17
- style?: CSSProperties;
18
- }>, "className"> & {
19
- classNames?: import("@dxos/react-ui-types").ClassNameValue;
20
- } & React.RefAttributes<HTMLDivElement>>;
21
- };
22
- export type { GameboardRootProps, GameboardContentsProps };
23
- //# sourceMappingURL=Gameboard.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Gameboard.d.ts","sourceRoot":"","sources":["../../../../src/Gameboard/Gameboard.tsx"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,EAAE,KAAK,aAAa,EAAc,KAAK,iBAAiB,EAAoC,MAAM,OAAO,CAAC;AAGxH,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAItD,OAAO,EAAE,KAAK,cAAc,EAAuB,KAAK,IAAI,EAAoB,MAAM,SAAS,CAAC;AAEhG,KAAK,kBAAkB,GAAG,iBAAiB,CAAC;IAC1C,KAAK,CAAC,EAAE,cAAc,CAAC;IACvB,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC;CAClC,CAAC,CAAC;AA6DH,KAAK,sBAAsB,GAAG,eAAe,CAC3C,iBAAiB,CAAC;IAChB,KAAK,CAAC,EAAE,aAAa,CAAC;CACvB,CAAC,CACH,CAAC;AAeF,eAAO,MAAM,SAAS;;sCA3E8B,kBAAkB;;;;gBA0D1D,aAAa;;;;CAoBxB,CAAC;AAEF,YAAY,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"Piece.d.ts","sourceRoot":"","sources":["../../../../src/Gameboard/Piece.tsx"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,EAA+B,KAAK,EAAE,EAAE,KAAK,QAAQ,EAAQ,MAAM,OAAO,CAAC;AAKzF,OAAO,EAAgC,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAIpF,OAAO,EAA8C,KAAK,WAAW,EAAE,KAAK,MAAM,EAAE,MAAM,SAAS,CAAC;AACpG,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,QAAQ,CAAC;AAE5C,MAAM,MAAM,UAAU,GAAG,eAAe,CAAC;IACvC,KAAK,EAAE,WAAW,CAAC;IACnB,MAAM,EAAE,aAAa,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,EAAE,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC;CACxC,CAAC,CAAC;AAEH,eAAO,MAAM,KAAK,2FAAuE,UAAU,uBAwGjG,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"Square.d.ts","sourceRoot":"","sources":["../../../../src/Gameboard/Square.tsx"],"names":[],"mappings":"AAKA,OAAO,KAA4C,MAAM,OAAO,CAAC;AAIjE,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAItD,OAAO,EAAW,KAAK,QAAQ,EAAE,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,QAAQ,CAAC;AAI5C,MAAM,MAAM,WAAW,GAAG,eAAe,CAAC;IACxC,QAAQ,EAAE,QAAQ,CAAC;IACnB,MAAM,EAAE,aAAa,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC,CAAC;AAEH,eAAO,MAAM,MAAM,sEAAkD,WAAW,uBA+C9E,CAAC"}
@@ -1,10 +0,0 @@
1
- import { type PieceRecord, type GameboardModel, type Move } from './types';
2
- export type GameboardContextType = {
3
- model?: GameboardModel;
4
- dragging?: boolean;
5
- promoting?: PieceRecord;
6
- onPromotion: (move: Move) => void;
7
- };
8
- export declare const GameboardContext: import("react").Context<GameboardContextType | undefined>;
9
- export declare const useBoardContext: () => GameboardContextType;
10
- //# sourceMappingURL=context.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../../src/Gameboard/context.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,cAAc,EAAE,KAAK,IAAI,EAAE,MAAM,SAAS,CAAC;AAE3E,MAAM,MAAM,oBAAoB,GAAG;IACjC,KAAK,CAAC,EAAE,cAAc,CAAC;IACvB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,WAAW,CAAC;IACxB,WAAW,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;CACnC,CAAC;AAEF,eAAO,MAAM,gBAAgB,2DAA6D,CAAC;AAE3F,eAAO,MAAM,eAAe,4BAE3B,CAAC"}
@@ -1,7 +0,0 @@
1
- export * from './context';
2
- export * from './types';
3
- export * from './util';
4
- export * from './Gameboard';
5
- export * from './Square';
6
- export * from './Piece';
7
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/Gameboard/index.ts"],"names":[],"mappings":"AAIA,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AAEvB,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/Gameboard/types.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3D,MAAM,MAAM,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC;AAEvC,MAAM,MAAM,QAAQ,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAExC,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC;AAE/B,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,SAAS,GAAG,SAAS,IAAI;IACzD,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,CAAC,CAAC;IACR,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,QAAQ,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,SAAS,GAAG,SAAS,IAAI,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;AAEvF,MAAM,MAAM,IAAI,GAAG;IACjB,IAAI,EAAE,QAAQ,CAAC;IACf,EAAE,EAAE,QAAQ,CAAC;IACb,KAAK,EAAE,SAAS,CAAC;IACjB,SAAS,CAAC,EAAE,SAAS,CAAC;CACvB,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,UAAU,QAAQ,KAAG,MAA4B,CAAC;AACnF,eAAO,MAAM,gBAAgB,GAAI,KAAK,MAAM,KAAG,QAAkD,CAAC;AAGlG,eAAO,MAAM,OAAO,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,WAMtB,CAAC;AAG7B,eAAO,MAAM,UAAU,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,QACwC,CAAC;AAE9F,eAAO,MAAM,eAAe,GAAI,IAAI,QAAQ,EAAE,IAAI,QAAQ,KAAG,OAA6C,CAAC;AAE3G;;GAEG;AACH,MAAM,WAAW,cAAc,CAAC,CAAC,SAAS,SAAS,GAAG,SAAS;IAC7D,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,cAAc,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;IACpC,WAAW,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC;IACrC,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC;CACtC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../../src/Gameboard/util.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC,CAAC;AAE/E,eAAO,MAAM,iBAAiB,GAAI,WAAW,WAAW,EAAE,SAAS,WAAW,KAAG,aAShF,CAAC"}
@@ -1,103 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { monitorForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter';
6
- import React, { type CSSProperties, forwardRef, type PropsWithChildren, useCallback, useEffect, useState } from 'react';
7
-
8
- import { log } from '@dxos/log';
9
- import { type ThemedClassName } from '@dxos/react-ui';
10
- import { mx } from '@dxos/react-ui-theme';
11
-
12
- import { GameboardContext, type GameboardContextType } from './context';
13
- import { type GameboardModel, isLocation, isPiece, type Move, type PieceRecord } from './types';
14
-
15
- type GameboardRootProps = PropsWithChildren<{
16
- model?: GameboardModel;
17
- onDrop?: (move: Move) => boolean;
18
- }>;
19
-
20
- /**
21
- * Generic board container.
22
- */
23
- const GameboardRoot = ({ children, model, onDrop }: GameboardRootProps) => {
24
- const [dragging, setDragging] = useState(false);
25
- const [promoting, setPromoting] = useState<PieceRecord | undefined>();
26
-
27
- // Handle promotion.
28
- const onPromotion = useCallback<GameboardContextType['onPromotion']>((move) => {
29
- log('onPromotion', { move });
30
- setPromoting(undefined);
31
- onDrop?.(move);
32
- }, []);
33
-
34
- useEffect(() => {
35
- if (!model) {
36
- return;
37
- }
38
-
39
- // TODO(burdon): Should target specific container.
40
- return monitorForElements({
41
- onDragStart: ({ source }) => {
42
- log('onDragStart', { source });
43
- setDragging(true);
44
- },
45
- onDrop: ({ source, location }) => {
46
- log('onDrop', { source, location });
47
- const target = location.current.dropTargets[0];
48
- if (!target) {
49
- return;
50
- }
51
-
52
- const targetLocation = target.data.location;
53
- const piece = source.data.piece;
54
- if (!isLocation(targetLocation) || !isPiece(piece)) {
55
- return;
56
- }
57
-
58
- const move: Move = { from: piece.location, to: targetLocation, piece: piece.type };
59
- if (model.canPromote?.(move)) {
60
- setPromoting({ ...piece, location: targetLocation });
61
- } else {
62
- onDrop?.(move);
63
- }
64
-
65
- setDragging(false);
66
- },
67
- });
68
- }, [model]);
69
-
70
- return (
71
- <GameboardContext.Provider value={{ model, dragging, promoting, onPromotion }}>
72
- {children}
73
- </GameboardContext.Provider>
74
- );
75
- };
76
-
77
- GameboardRoot.displayName = 'Gameboard.Root';
78
-
79
- type GameboardContentsProps = ThemedClassName<
80
- PropsWithChildren<{
81
- style?: CSSProperties;
82
- }>
83
- >;
84
-
85
- /**
86
- * Container centers the board.
87
- */
88
- const GameboardContent = forwardRef<HTMLDivElement, GameboardContentsProps>(
89
- ({ children, classNames, style }, forwardedRef) => {
90
- return (
91
- <div ref={forwardedRef} style={style} className='flex w-full h-full justify-center overflow-hidden'>
92
- <div className={mx('max-w-full max-h-full content-center aspect-square', classNames)}>{children}</div>
93
- </div>
94
- );
95
- },
96
- );
97
-
98
- export const Gameboard = {
99
- Root: GameboardRoot,
100
- Content: GameboardContent,
101
- };
102
-
103
- export type { GameboardRootProps, GameboardContentsProps };
@@ -1,22 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { createContext, useContext } from 'react';
6
-
7
- import { raise } from '@dxos/debug';
8
-
9
- import { type PieceRecord, type GameboardModel, type Move } from './types';
10
-
11
- export type GameboardContextType = {
12
- model?: GameboardModel;
13
- dragging?: boolean; // TODO(burdon): Change to PieceRecord.
14
- promoting?: PieceRecord;
15
- onPromotion: (move: Move) => void;
16
- };
17
-
18
- export const GameboardContext = createContext<GameboardContextType | undefined>(undefined);
19
-
20
- export const useBoardContext = () => {
21
- return useContext(GameboardContext) ?? raise(new Error('Missing BoardContext'));
22
- };
File without changes