react-native-chess-kit 0.1.0 → 0.2.0

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 (109) hide show
  1. package/lib/commonjs/board-annotations.js +131 -0
  2. package/lib/commonjs/board-annotations.js.map +1 -0
  3. package/lib/commonjs/board-arrows.js +164 -0
  4. package/lib/commonjs/board-arrows.js.map +1 -0
  5. package/lib/commonjs/board-highlights.js +212 -0
  6. package/lib/commonjs/board-highlights.js.map +1 -0
  7. package/lib/commonjs/board-piece.js +41 -12
  8. package/lib/commonjs/board-piece.js.map +1 -1
  9. package/lib/commonjs/board-pieces.js +2 -0
  10. package/lib/commonjs/board-pieces.js.map +1 -1
  11. package/lib/commonjs/board.js +392 -42
  12. package/lib/commonjs/board.js.map +1 -1
  13. package/lib/commonjs/constants.js +104 -0
  14. package/lib/commonjs/constants.js.map +1 -0
  15. package/lib/commonjs/index.js +128 -0
  16. package/lib/commonjs/index.js.map +1 -1
  17. package/lib/commonjs/pieces/default-pieces.js +536 -0
  18. package/lib/commonjs/pieces/default-pieces.js.map +1 -0
  19. package/lib/commonjs/pieces/index.js +13 -0
  20. package/lib/commonjs/pieces/index.js.map +1 -0
  21. package/lib/commonjs/promotion-picker.js +129 -0
  22. package/lib/commonjs/promotion-picker.js.map +1 -0
  23. package/lib/commonjs/static-board.js +150 -0
  24. package/lib/commonjs/static-board.js.map +1 -0
  25. package/lib/commonjs/themes.js +175 -0
  26. package/lib/commonjs/themes.js.map +1 -0
  27. package/lib/commonjs/use-board-gesture.js +184 -11
  28. package/lib/commonjs/use-board-gesture.js.map +1 -1
  29. package/lib/commonjs/use-premove.js +44 -0
  30. package/lib/commonjs/use-premove.js.map +1 -0
  31. package/lib/module/board-annotations.js +126 -0
  32. package/lib/module/board-annotations.js.map +1 -0
  33. package/lib/module/board-arrows.js +161 -0
  34. package/lib/module/board-arrows.js.map +1 -0
  35. package/lib/module/board-highlights.js +206 -0
  36. package/lib/module/board-highlights.js.map +1 -0
  37. package/lib/module/board-piece.js +42 -13
  38. package/lib/module/board-piece.js.map +1 -1
  39. package/lib/module/board-pieces.js +2 -0
  40. package/lib/module/board-pieces.js.map +1 -1
  41. package/lib/module/board.js +395 -44
  42. package/lib/module/board.js.map +1 -1
  43. package/lib/module/constants.js +100 -0
  44. package/lib/module/constants.js.map +1 -0
  45. package/lib/module/index.js +29 -1
  46. package/lib/module/index.js.map +1 -1
  47. package/lib/module/pieces/default-pieces.js +530 -0
  48. package/lib/module/pieces/default-pieces.js.map +1 -0
  49. package/lib/module/pieces/index.js +4 -0
  50. package/lib/module/pieces/index.js.map +1 -0
  51. package/lib/module/promotion-picker.js +124 -0
  52. package/lib/module/promotion-picker.js.map +1 -0
  53. package/lib/module/static-board.js +146 -0
  54. package/lib/module/static-board.js.map +1 -0
  55. package/lib/module/themes.js +171 -0
  56. package/lib/module/themes.js.map +1 -0
  57. package/lib/module/use-board-gesture.js +185 -11
  58. package/lib/module/use-board-gesture.js.map +1 -1
  59. package/lib/module/use-premove.js +40 -0
  60. package/lib/module/use-premove.js.map +1 -0
  61. package/lib/typescript/board-annotations.d.ts +30 -0
  62. package/lib/typescript/board-annotations.d.ts.map +1 -0
  63. package/lib/typescript/board-arrows.d.ts +27 -0
  64. package/lib/typescript/board-arrows.d.ts.map +1 -0
  65. package/lib/typescript/board-highlights.d.ts +65 -0
  66. package/lib/typescript/board-highlights.d.ts.map +1 -0
  67. package/lib/typescript/board-piece.d.ts +10 -4
  68. package/lib/typescript/board-piece.d.ts.map +1 -1
  69. package/lib/typescript/board-pieces.d.ts +2 -1
  70. package/lib/typescript/board-pieces.d.ts.map +1 -1
  71. package/lib/typescript/board.d.ts +11 -2
  72. package/lib/typescript/board.d.ts.map +1 -1
  73. package/lib/typescript/constants.d.ts +54 -0
  74. package/lib/typescript/constants.d.ts.map +1 -0
  75. package/lib/typescript/index.d.ts +9 -1
  76. package/lib/typescript/index.d.ts.map +1 -1
  77. package/lib/typescript/pieces/default-pieces.d.ts +3 -0
  78. package/lib/typescript/pieces/default-pieces.d.ts.map +1 -0
  79. package/lib/typescript/pieces/index.d.ts +2 -0
  80. package/lib/typescript/pieces/index.d.ts.map +1 -0
  81. package/lib/typescript/promotion-picker.d.ts +30 -0
  82. package/lib/typescript/promotion-picker.d.ts.map +1 -0
  83. package/lib/typescript/static-board.d.ts +12 -0
  84. package/lib/typescript/static-board.d.ts.map +1 -0
  85. package/lib/typescript/themes.d.ts +15 -0
  86. package/lib/typescript/themes.d.ts.map +1 -0
  87. package/lib/typescript/types.d.ts +194 -24
  88. package/lib/typescript/types.d.ts.map +1 -1
  89. package/lib/typescript/use-board-gesture.d.ts +28 -2
  90. package/lib/typescript/use-board-gesture.d.ts.map +1 -1
  91. package/lib/typescript/use-premove.d.ts +31 -0
  92. package/lib/typescript/use-premove.d.ts.map +1 -0
  93. package/package.json +4 -2
  94. package/src/board-annotations.tsx +147 -0
  95. package/src/board-arrows.tsx +197 -0
  96. package/src/board-highlights.tsx +226 -0
  97. package/src/board-piece.tsx +50 -12
  98. package/src/board-pieces.tsx +4 -1
  99. package/src/board.tsx +462 -46
  100. package/src/constants.ts +100 -0
  101. package/src/index.ts +62 -1
  102. package/src/pieces/default-pieces.tsx +383 -0
  103. package/src/pieces/index.ts +1 -0
  104. package/src/promotion-picker.tsx +147 -0
  105. package/src/static-board.tsx +150 -0
  106. package/src/themes.ts +129 -0
  107. package/src/types.ts +251 -25
  108. package/src/use-board-gesture.ts +219 -8
  109. package/src/use-premove.ts +59 -0
@@ -0,0 +1,147 @@
1
+ import React, { useCallback } from 'react';
2
+ import { View, Pressable } from 'react-native';
3
+
4
+ import type { ChessColor, PromotionPiece, PieceSetMap } from './types';
5
+ import { squareToXY } from './use-board-pieces';
6
+ import { PROMOTION_PIECE_PADDING } from './constants';
7
+
8
+ // ---------------------------------------------------------------------------
9
+ // Props
10
+ // ---------------------------------------------------------------------------
11
+
12
+ type PromotionPickerProps = {
13
+ /** Square the pawn is promoting on */
14
+ square: string;
15
+ /** Color of the promoting pawn */
16
+ pieceColor: 'w' | 'b';
17
+ /** Board size in pixels */
18
+ boardSize: number;
19
+ squareSize: number;
20
+ orientation: ChessColor;
21
+ /** Piece renderer (from renderPiece prop or pieceSet) */
22
+ renderPiece: (code: string, size: number) => React.ReactElement;
23
+ /** Called when user picks a promotion piece */
24
+ onSelect: (piece: PromotionPiece) => void;
25
+ /** Called when user cancels (taps outside) */
26
+ onCancel: () => void;
27
+ };
28
+
29
+ // ---------------------------------------------------------------------------
30
+ // Promotion pieces in order
31
+ // ---------------------------------------------------------------------------
32
+
33
+ const PROMOTION_PIECES: PromotionPiece[] = ['q', 'r', 'b', 'n'];
34
+
35
+ function promotionPieceCode(color: 'w' | 'b', piece: PromotionPiece): string {
36
+ return `${color}${piece}`;
37
+ }
38
+
39
+ // ---------------------------------------------------------------------------
40
+ // Component
41
+ // ---------------------------------------------------------------------------
42
+
43
+ /**
44
+ * Promotion piece picker overlay.
45
+ *
46
+ * Appears as a vertical column of 4 pieces (Q, R, B, N) anchored to the
47
+ * promotion square. Expands downward when promoting on the top edge,
48
+ * upward when promoting on the bottom edge.
49
+ *
50
+ * A semi-transparent backdrop covers the board. Tapping outside cancels.
51
+ */
52
+ export const PromotionPicker = React.memo(function PromotionPicker({
53
+ square,
54
+ pieceColor,
55
+ boardSize,
56
+ squareSize,
57
+ orientation,
58
+ renderPiece,
59
+ onSelect,
60
+ onCancel,
61
+ }: PromotionPickerProps) {
62
+ const { x, y } = squareToXY(square, squareSize, orientation);
63
+ const pieceSize = squareSize * (1 - PROMOTION_PIECE_PADDING * 2);
64
+ const padding = squareSize * PROMOTION_PIECE_PADDING;
65
+
66
+ // Determine if the picker should expand downward or upward
67
+ // If the promotion square is in the top half, expand downward
68
+ const expandDown = y < boardSize / 2;
69
+
70
+ const handleSelect = useCallback(
71
+ (piece: PromotionPiece) => {
72
+ onSelect(piece);
73
+ },
74
+ [onSelect],
75
+ );
76
+
77
+ return (
78
+ <View
79
+ style={{
80
+ position: 'absolute',
81
+ width: boardSize,
82
+ height: boardSize,
83
+ zIndex: 200,
84
+ }}
85
+ >
86
+ {/* Backdrop -- dims the board and captures cancel taps */}
87
+ <Pressable
88
+ style={{
89
+ position: 'absolute',
90
+ width: boardSize,
91
+ height: boardSize,
92
+ backgroundColor: 'rgba(0, 0, 0, 0.4)',
93
+ }}
94
+ onPress={onCancel}
95
+ />
96
+
97
+ {/* Piece column */}
98
+ <View
99
+ style={{
100
+ position: 'absolute',
101
+ left: x,
102
+ top: expandDown ? y : y - squareSize * 3,
103
+ width: squareSize,
104
+ backgroundColor: '#ffffff',
105
+ borderRadius: 4,
106
+ // Subtle shadow for elevation
107
+ shadowColor: '#000',
108
+ shadowOffset: { width: 0, height: 2 },
109
+ shadowOpacity: 0.25,
110
+ shadowRadius: 4,
111
+ elevation: 8,
112
+ overflow: 'hidden',
113
+ }}
114
+ >
115
+ {PROMOTION_PIECES.map((piece) => {
116
+ const code = promotionPieceCode(pieceColor, piece);
117
+ return (
118
+ <Pressable
119
+ key={piece}
120
+ onPress={() => handleSelect(piece)}
121
+ style={({ pressed }) => ({
122
+ width: squareSize,
123
+ height: squareSize,
124
+ alignItems: 'center',
125
+ justifyContent: 'center',
126
+ backgroundColor: pressed ? 'rgba(0, 0, 0, 0.1)' : 'transparent',
127
+ })}
128
+ accessibilityLabel={`Promote to ${PIECE_NAMES[piece]}`}
129
+ accessibilityRole="button"
130
+ >
131
+ <View style={{ width: pieceSize, height: pieceSize }}>
132
+ {renderPiece(code, pieceSize)}
133
+ </View>
134
+ </Pressable>
135
+ );
136
+ })}
137
+ </View>
138
+ </View>
139
+ );
140
+ });
141
+
142
+ const PIECE_NAMES: Record<PromotionPiece, string> = {
143
+ q: 'Queen',
144
+ r: 'Rook',
145
+ b: 'Bishop',
146
+ n: 'Knight',
147
+ };
@@ -0,0 +1,150 @@
1
+ import React, { useState, useCallback, useMemo } from 'react';
2
+ import { View, type LayoutChangeEvent } from 'react-native';
3
+
4
+ import type { StaticBoardProps, ChessColor, PieceCode } from './types';
5
+ import { DEFAULT_BOARD_COLORS, DEFAULT_LAST_MOVE_COLOR } from './constants';
6
+ import { DefaultPieceSet } from './pieces';
7
+ import { useBoardPieces, squareToXY } from './use-board-pieces';
8
+ import { BoardBackground } from './board-background';
9
+ import { BoardCoordinates } from './board-coordinates';
10
+ import { BoardHighlights } from './board-highlights';
11
+ import { BoardArrows } from './board-arrows';
12
+ import { BoardAnnotations } from './board-annotations';
13
+
14
+ /**
15
+ * Non-interactive chess board — optimized for lists, thumbnails, and analysis previews.
16
+ *
17
+ * No gesture handler, no drag ghost, no legal move dots.
18
+ * Just: background + coordinates + highlights + pieces + arrows + annotations.
19
+ *
20
+ * Significantly lighter than the full Board component.
21
+ */
22
+ export const StaticBoard = React.memo(function StaticBoard({
23
+ fen,
24
+ orientation = 'white',
25
+ boardSize: boardSizeProp,
26
+ colors,
27
+ withCoordinates = false,
28
+ renderPiece,
29
+ pieceSet,
30
+ lastMove,
31
+ highlights,
32
+ arrows,
33
+ annotations,
34
+ }: StaticBoardProps) {
35
+ // --- Auto-sizing via onLayout when boardSize not provided ---
36
+ const [measuredSize, setMeasuredSize] = useState(0);
37
+ const handleLayout = useCallback((e: LayoutChangeEvent) => {
38
+ const { width, height } = e.nativeEvent.layout;
39
+ setMeasuredSize(Math.min(width, height));
40
+ }, []);
41
+
42
+ const boardSize = boardSizeProp ?? measuredSize;
43
+ const squareSize = boardSize / 8;
44
+ const boardColors = colors ?? DEFAULT_BOARD_COLORS;
45
+
46
+ // --- Piece data from FEN ---
47
+ const pieces = useBoardPieces(fen);
48
+
49
+ // --- Resolve piece renderer: renderPiece > pieceSet > DefaultPieceSet ---
50
+ const resolvedRenderer = useMemo(() => {
51
+ if (renderPiece) return renderPiece;
52
+ const set = pieceSet ?? DefaultPieceSet;
53
+ return (code: string, size: number) => {
54
+ const renderer = set[code as PieceCode];
55
+ if (renderer) return renderer(size);
56
+ // Fallback for unknown piece codes
57
+ return <View style={{ width: size, height: size }} />;
58
+ };
59
+ }, [renderPiece, pieceSet]);
60
+
61
+ // If no size yet (auto-sizing), render invisible container for measurement
62
+ if (boardSize === 0) {
63
+ return (
64
+ <View style={{ flex: 1, aspectRatio: 1 }} onLayout={handleLayout} />
65
+ );
66
+ }
67
+
68
+ return (
69
+ <View
70
+ style={{ width: boardSize, height: boardSize }}
71
+ onLayout={boardSizeProp ? undefined : handleLayout}
72
+ accessibilityLabel="Chess board"
73
+ accessibilityRole="image"
74
+ >
75
+ {/* Layer 1: Board background */}
76
+ <BoardBackground
77
+ boardSize={boardSize}
78
+ lightColor={boardColors.light}
79
+ darkColor={boardColors.dark}
80
+ />
81
+
82
+ {/* Layer 2: Coordinates */}
83
+ {withCoordinates && (
84
+ <BoardCoordinates
85
+ boardSize={boardSize}
86
+ orientation={orientation}
87
+ lightColor={boardColors.light}
88
+ darkColor={boardColors.dark}
89
+ withLetters
90
+ withNumbers
91
+ />
92
+ )}
93
+
94
+ {/* Layer 3: Highlights */}
95
+ <BoardHighlights
96
+ boardSize={boardSize}
97
+ orientation={orientation}
98
+ squareSize={squareSize}
99
+ lastMove={lastMove}
100
+ lastMoveColor={DEFAULT_LAST_MOVE_COLOR}
101
+ checkColor="transparent"
102
+ selectedColor="transparent"
103
+ premoveColor="transparent"
104
+ highlights={highlights}
105
+ />
106
+
107
+ {/* Layer 4: Pieces (static — no animation needed) */}
108
+ <View
109
+ style={{ position: 'absolute', width: boardSize, height: boardSize }}
110
+ pointerEvents="none"
111
+ >
112
+ {pieces.map((piece) => {
113
+ const { x, y } = squareToXY(piece.square, squareSize, orientation);
114
+ return (
115
+ <View
116
+ key={piece.id}
117
+ style={{
118
+ position: 'absolute',
119
+ width: squareSize,
120
+ height: squareSize,
121
+ transform: [{ translateX: x }, { translateY: y }],
122
+ }}
123
+ >
124
+ {resolvedRenderer(piece.code, squareSize)}
125
+ </View>
126
+ );
127
+ })}
128
+ </View>
129
+
130
+ {/* Layer 5: Arrows */}
131
+ {arrows && arrows.length > 0 && (
132
+ <BoardArrows
133
+ boardSize={boardSize}
134
+ orientation={orientation}
135
+ arrows={arrows}
136
+ />
137
+ )}
138
+
139
+ {/* Layer 6: Annotations */}
140
+ {annotations && annotations.length > 0 && (
141
+ <BoardAnnotations
142
+ boardSize={boardSize}
143
+ orientation={orientation}
144
+ squareSize={squareSize}
145
+ annotations={annotations}
146
+ />
147
+ )}
148
+ </View>
149
+ );
150
+ });
package/src/themes.ts ADDED
@@ -0,0 +1,129 @@
1
+ import type { BoardTheme, BoardColors } from './types';
2
+
3
+ // ---------------------------------------------------------------------------
4
+ // Board color presets (matching popular chess platforms)
5
+ // ---------------------------------------------------------------------------
6
+
7
+ const greenTheme: BoardTheme = {
8
+ name: 'Green',
9
+ board: { light: '#eeeed2', dark: '#769656' },
10
+ lastMove: 'rgba(255, 255, 0, 0.4)',
11
+ check: 'rgba(235, 97, 80, 0.8)',
12
+ selected: 'rgba(20, 85, 200, 0.5)',
13
+ legalDot: 'rgba(0, 0, 0, 0.25)',
14
+ premove: 'rgba(20, 85, 200, 0.3)',
15
+ arrow: 'rgba(243, 166, 50, 0.85)',
16
+ coordinates: { light: '#769656', dark: '#eeeed2' },
17
+ };
18
+
19
+ const brownTheme: BoardTheme = {
20
+ name: 'Brown',
21
+ board: { light: '#f0d9b5', dark: '#b58863' },
22
+ lastMove: 'rgba(255, 255, 0, 0.4)',
23
+ check: 'rgba(235, 97, 80, 0.8)',
24
+ selected: 'rgba(20, 85, 200, 0.5)',
25
+ legalDot: 'rgba(0, 0, 0, 0.25)',
26
+ premove: 'rgba(20, 85, 200, 0.3)',
27
+ arrow: 'rgba(243, 166, 50, 0.85)',
28
+ coordinates: { light: '#b58863', dark: '#f0d9b5' },
29
+ };
30
+
31
+ const blueTheme: BoardTheme = {
32
+ name: 'Blue',
33
+ board: { light: '#dee3e6', dark: '#8ca2ad' },
34
+ lastMove: 'rgba(255, 255, 0, 0.4)',
35
+ check: 'rgba(235, 97, 80, 0.8)',
36
+ selected: 'rgba(20, 85, 200, 0.5)',
37
+ legalDot: 'rgba(0, 0, 0, 0.25)',
38
+ premove: 'rgba(20, 85, 200, 0.3)',
39
+ arrow: 'rgba(243, 166, 50, 0.85)',
40
+ coordinates: { light: '#8ca2ad', dark: '#dee3e6' },
41
+ };
42
+
43
+ const purpleTheme: BoardTheme = {
44
+ name: 'Purple',
45
+ board: { light: '#e8daf0', dark: '#9b72b0' },
46
+ lastMove: 'rgba(255, 255, 0, 0.4)',
47
+ check: 'rgba(235, 97, 80, 0.8)',
48
+ selected: 'rgba(20, 85, 200, 0.5)',
49
+ legalDot: 'rgba(0, 0, 0, 0.25)',
50
+ premove: 'rgba(20, 85, 200, 0.3)',
51
+ arrow: 'rgba(243, 166, 50, 0.85)',
52
+ coordinates: { light: '#9b72b0', dark: '#e8daf0' },
53
+ };
54
+
55
+ const grayTheme: BoardTheme = {
56
+ name: 'Gray',
57
+ board: { light: '#e0e0e0', dark: '#888888' },
58
+ lastMove: 'rgba(255, 255, 0, 0.4)',
59
+ check: 'rgba(235, 97, 80, 0.8)',
60
+ selected: 'rgba(20, 85, 200, 0.5)',
61
+ legalDot: 'rgba(0, 0, 0, 0.25)',
62
+ premove: 'rgba(20, 85, 200, 0.3)',
63
+ arrow: 'rgba(243, 166, 50, 0.85)',
64
+ coordinates: { light: '#888888', dark: '#e0e0e0' },
65
+ };
66
+
67
+ const woodTheme: BoardTheme = {
68
+ name: 'Wood',
69
+ board: { light: '#e6c88c', dark: '#a67c52' },
70
+ lastMove: 'rgba(255, 255, 0, 0.35)',
71
+ check: 'rgba(235, 97, 80, 0.8)',
72
+ selected: 'rgba(20, 85, 200, 0.5)',
73
+ legalDot: 'rgba(0, 0, 0, 0.2)',
74
+ premove: 'rgba(20, 85, 200, 0.3)',
75
+ arrow: 'rgba(243, 166, 50, 0.85)',
76
+ coordinates: { light: '#a67c52', dark: '#e6c88c' },
77
+ };
78
+
79
+ const iceTheme: BoardTheme = {
80
+ name: 'Ice',
81
+ board: { light: '#e8f0f8', dark: '#7ba0c4' },
82
+ lastMove: 'rgba(255, 255, 0, 0.35)',
83
+ check: 'rgba(235, 97, 80, 0.8)',
84
+ selected: 'rgba(20, 85, 200, 0.5)',
85
+ legalDot: 'rgba(0, 0, 0, 0.25)',
86
+ premove: 'rgba(20, 85, 200, 0.3)',
87
+ arrow: 'rgba(243, 166, 50, 0.85)',
88
+ coordinates: { light: '#7ba0c4', dark: '#e8f0f8' },
89
+ };
90
+
91
+ const tournamentTheme: BoardTheme = {
92
+ name: 'Tournament',
93
+ board: { light: '#fffff0', dark: '#3d945e' },
94
+ lastMove: 'rgba(255, 255, 0, 0.4)',
95
+ check: 'rgba(235, 97, 80, 0.8)',
96
+ selected: 'rgba(20, 85, 200, 0.5)',
97
+ legalDot: 'rgba(0, 0, 0, 0.25)',
98
+ premove: 'rgba(20, 85, 200, 0.3)',
99
+ arrow: 'rgba(243, 166, 50, 0.85)',
100
+ coordinates: { light: '#3d945e', dark: '#fffff0' },
101
+ };
102
+
103
+ // ---------------------------------------------------------------------------
104
+ // Exports
105
+ // ---------------------------------------------------------------------------
106
+
107
+ /** All built-in board themes */
108
+ export const BOARD_THEMES = {
109
+ green: greenTheme,
110
+ brown: brownTheme,
111
+ blue: blueTheme,
112
+ purple: purpleTheme,
113
+ gray: grayTheme,
114
+ wood: woodTheme,
115
+ ice: iceTheme,
116
+ tournament: tournamentTheme,
117
+ } as const;
118
+
119
+ /** Shorthand board color presets (just light/dark, no overlay colors) */
120
+ export const BOARD_COLORS: Record<keyof typeof BOARD_THEMES, BoardColors> = {
121
+ green: greenTheme.board,
122
+ brown: brownTheme.board,
123
+ blue: blueTheme.board,
124
+ purple: purpleTheme.board,
125
+ gray: grayTheme.board,
126
+ wood: woodTheme.board,
127
+ ice: iceTheme.board,
128
+ tournament: tournamentTheme.board,
129
+ };