@react-chess-tools/react-chess-game 1.0.0 → 1.0.1

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/index.js CHANGED
@@ -306,6 +306,7 @@ var Root = ({
306
306
  const mergedTheme = React4.useMemo(() => mergeTheme(theme), [theme]);
307
307
  return /* @__PURE__ */ React4.createElement(ChessGameContext.Provider, { value: context }, /* @__PURE__ */ React4.createElement(ThemeProvider, { theme: mergedTheme }, children));
308
308
  };
309
+ Root.displayName = "ChessGame.Root";
309
310
 
310
311
  // src/components/ChessGame/parts/Board.tsx
311
312
  import React5 from "react";
@@ -375,192 +376,199 @@ var deepMergeChessboardOptions = (baseOptions, customOptions) => {
375
376
  };
376
377
 
377
378
  // src/components/ChessGame/parts/Board.tsx
378
- var Board = ({ options = {} }) => {
379
- var _a, _b, _c;
380
- const gameContext = useChessGameContext();
381
- const theme = useChessGameTheme();
382
- if (!gameContext) {
383
- throw new Error("ChessGameContext not found");
384
- }
385
- const {
386
- game,
387
- currentFen,
388
- orientation,
389
- info,
390
- isLatestMove,
391
- methods: { makeMove }
392
- } = gameContext;
393
- const { turn, isGameOver } = info;
394
- const [activeSquare, setActiveSquare] = React5.useState(null);
395
- const [promotionMove, setPromotionMove] = React5.useState(null);
396
- const onSquareClick = (square) => {
397
- if (isGameOver) {
398
- return;
379
+ var Board = React5.forwardRef(
380
+ ({ options = {}, className, style: userStyle, ...rest }, ref) => {
381
+ var _a, _b, _c;
382
+ const gameContext = useChessGameContext();
383
+ const theme = useChessGameTheme();
384
+ if (!gameContext) {
385
+ throw new Error("ChessGameContext not found");
399
386
  }
400
- if (activeSquare === null) {
401
- const squadreInfo = game.get(square);
402
- if (squadreInfo && squadreInfo.color === turn) {
403
- return setActiveSquare(square);
387
+ const {
388
+ game,
389
+ currentFen,
390
+ orientation,
391
+ info,
392
+ isLatestMove,
393
+ methods: { makeMove }
394
+ } = gameContext;
395
+ const { turn, isGameOver } = info;
396
+ const [activeSquare, setActiveSquare] = React5.useState(null);
397
+ const [promotionMove, setPromotionMove] = React5.useState(null);
398
+ const onSquareClick = (square) => {
399
+ if (isGameOver) {
400
+ return;
404
401
  }
405
- return;
406
- }
407
- if (!isLegalMove(game, {
408
- from: activeSquare,
409
- to: square,
410
- promotion: "q"
411
- })) {
412
- return setActiveSquare(null);
413
- }
414
- if (requiresPromotion(game, {
415
- from: activeSquare,
416
- to: square,
417
- promotion: "q"
418
- })) {
419
- return setPromotionMove({
402
+ if (activeSquare === null) {
403
+ const squadreInfo = game.get(square);
404
+ if (squadreInfo && squadreInfo.color === turn) {
405
+ return setActiveSquare(square);
406
+ }
407
+ return;
408
+ }
409
+ if (!isLegalMove(game, {
420
410
  from: activeSquare,
421
- to: square
422
- });
423
- }
424
- setActiveSquare(null);
425
- makeMove({
426
- from: activeSquare,
427
- to: square
428
- });
429
- };
430
- const onPromotionPieceSelect = (piece) => {
431
- if ((promotionMove == null ? void 0 : promotionMove.from) && (promotionMove == null ? void 0 : promotionMove.to)) {
411
+ to: square,
412
+ promotion: "q"
413
+ })) {
414
+ return setActiveSquare(null);
415
+ }
416
+ if (requiresPromotion(game, {
417
+ from: activeSquare,
418
+ to: square,
419
+ promotion: "q"
420
+ })) {
421
+ return setPromotionMove({
422
+ from: activeSquare,
423
+ to: square
424
+ });
425
+ }
426
+ setActiveSquare(null);
432
427
  makeMove({
433
- from: promotionMove.from,
434
- to: promotionMove.to,
435
- promotion: piece.toLowerCase()
428
+ from: activeSquare,
429
+ to: square
436
430
  });
437
- setPromotionMove(null);
438
- }
439
- };
440
- const onSquareRightClick = () => {
441
- setActiveSquare(null);
442
- setPromotionMove(null);
443
- };
444
- const squareWidth = React5.useMemo(() => {
445
- var _a2;
446
- if (typeof document === "undefined") return 80;
447
- const squareElement = document.querySelector(`[data-square]`);
448
- return ((_a2 = squareElement == null ? void 0 : squareElement.getBoundingClientRect()) == null ? void 0 : _a2.width) ?? 80;
449
- }, [promotionMove]);
450
- const promotionSquareLeft = React5.useMemo(() => {
451
- var _a2;
452
- if (!(promotionMove == null ? void 0 : promotionMove.to)) return 0;
453
- const column = ((_a2 = promotionMove.to.match(/^[a-h]/)) == null ? void 0 : _a2[0]) ?? "a";
454
- return squareWidth * chessColumnToColumnIndex(
455
- column,
456
- 8,
457
- orientation === "b" ? "black" : "white"
458
- );
459
- }, [promotionMove, squareWidth, orientation]);
460
- const baseOptions = {
461
- squareStyles: getCustomSquareStyles(game, info, activeSquare, theme),
462
- boardOrientation: orientation === "b" ? "black" : "white",
463
- position: currentFen,
464
- showNotation: true,
465
- showAnimations: isLatestMove,
466
- lightSquareStyle: theme.board.lightSquare,
467
- darkSquareStyle: theme.board.darkSquare,
468
- canDragPiece: ({ piece }) => {
469
- if (isGameOver) return false;
470
- return piece.pieceType[0] === turn;
471
- },
472
- dropSquareStyle: theme.state.dropSquare,
473
- onPieceDrag: ({ piece, square }) => {
474
- if (piece.pieceType[0] === turn) {
475
- setActiveSquare(square);
431
+ };
432
+ const onPromotionPieceSelect = (piece) => {
433
+ if ((promotionMove == null ? void 0 : promotionMove.from) && (promotionMove == null ? void 0 : promotionMove.to)) {
434
+ makeMove({
435
+ from: promotionMove.from,
436
+ to: promotionMove.to,
437
+ promotion: piece.toLowerCase()
438
+ });
439
+ setPromotionMove(null);
476
440
  }
477
- },
478
- onPieceDrop: ({ sourceSquare, targetSquare }) => {
441
+ };
442
+ const onSquareRightClick = () => {
479
443
  setActiveSquare(null);
480
- const moveData = {
481
- from: sourceSquare,
482
- to: targetSquare
483
- };
484
- if (requiresPromotion(game, { ...moveData, promotion: "q" })) {
485
- setPromotionMove(moveData);
486
- return false;
487
- }
488
- return makeMove(moveData);
489
- },
490
- onSquareClick: ({ square }) => {
491
- if (square.match(/^[a-h][1-8]$/)) {
492
- onSquareClick(square);
493
- }
494
- },
495
- onSquareRightClick,
496
- allowDrawingArrows: true,
497
- animationDurationInMs: game.history().length === 0 ? 0 : 300
498
- };
499
- const mergedOptions = deepMergeChessboardOptions(baseOptions, options);
500
- return /* @__PURE__ */ React5.createElement("div", { style: { position: "relative" } }, /* @__PURE__ */ React5.createElement(Chessboard, { options: mergedOptions }), promotionMove && /* @__PURE__ */ React5.createElement(React5.Fragment, null, /* @__PURE__ */ React5.createElement(
501
- "div",
502
- {
503
- onClick: () => setPromotionMove(null),
504
- onContextMenu: (e) => {
505
- e.preventDefault();
506
- setPromotionMove(null);
444
+ setPromotionMove(null);
445
+ };
446
+ const squareWidth = React5.useMemo(() => {
447
+ var _a2;
448
+ if (typeof document === "undefined") return 80;
449
+ const squareElement = document.querySelector(`[data-square]`);
450
+ return ((_a2 = squareElement == null ? void 0 : squareElement.getBoundingClientRect()) == null ? void 0 : _a2.width) ?? 80;
451
+ }, [promotionMove]);
452
+ const promotionSquareLeft = React5.useMemo(() => {
453
+ var _a2;
454
+ if (!(promotionMove == null ? void 0 : promotionMove.to)) return 0;
455
+ const column = ((_a2 = promotionMove.to.match(/^[a-h]/)) == null ? void 0 : _a2[0]) ?? "a";
456
+ return squareWidth * chessColumnToColumnIndex(
457
+ column,
458
+ 8,
459
+ orientation === "b" ? "black" : "white"
460
+ );
461
+ }, [promotionMove, squareWidth, orientation]);
462
+ const baseOptions = {
463
+ squareStyles: getCustomSquareStyles(game, info, activeSquare, theme),
464
+ boardOrientation: orientation === "b" ? "black" : "white",
465
+ position: currentFen,
466
+ showNotation: true,
467
+ showAnimations: isLatestMove,
468
+ lightSquareStyle: theme.board.lightSquare,
469
+ darkSquareStyle: theme.board.darkSquare,
470
+ canDragPiece: ({ piece }) => {
471
+ if (isGameOver) return false;
472
+ return piece.pieceType[0] === turn;
507
473
  },
508
- style: {
509
- position: "absolute",
510
- top: 0,
511
- left: 0,
512
- right: 0,
513
- bottom: 0,
514
- backgroundColor: "rgba(0, 0, 0, 0.1)",
515
- zIndex: 1e3
516
- }
517
- }
518
- ), /* @__PURE__ */ React5.createElement(
519
- "div",
520
- {
521
- style: {
522
- position: "absolute",
523
- top: ((_b = (_a = promotionMove.to) == null ? void 0 : _a[1]) == null ? void 0 : _b.includes("8")) ? 0 : "auto",
524
- bottom: ((_c = promotionMove.to) == null ? void 0 : _c[1].includes("1")) ? 0 : "auto",
525
- left: promotionSquareLeft,
526
- backgroundColor: "white",
527
- width: squareWidth,
528
- zIndex: 1001,
529
- display: "flex",
530
- flexDirection: "column",
531
- boxShadow: "0 0 10px 0 rgba(0, 0, 0, 0.5)"
532
- }
533
- },
534
- ["q", "r", "n", "b"].map((piece) => /* @__PURE__ */ React5.createElement(
535
- "button",
474
+ dropSquareStyle: theme.state.dropSquare,
475
+ onPieceDrag: ({ piece, square }) => {
476
+ if (piece.pieceType[0] === turn) {
477
+ setActiveSquare(square);
478
+ }
479
+ },
480
+ onPieceDrop: ({ sourceSquare, targetSquare }) => {
481
+ setActiveSquare(null);
482
+ const moveData = {
483
+ from: sourceSquare,
484
+ to: targetSquare
485
+ };
486
+ if (requiresPromotion(game, { ...moveData, promotion: "q" })) {
487
+ setPromotionMove(moveData);
488
+ return false;
489
+ }
490
+ return makeMove(moveData);
491
+ },
492
+ onSquareClick: ({ square }) => {
493
+ if (square.match(/^[a-h][1-8]$/)) {
494
+ onSquareClick(square);
495
+ }
496
+ },
497
+ onSquareRightClick,
498
+ allowDrawingArrows: true,
499
+ animationDurationInMs: game.history().length === 0 ? 0 : 300
500
+ };
501
+ const mergedOptions = deepMergeChessboardOptions(baseOptions, options);
502
+ const mergedStyle = {
503
+ ...userStyle,
504
+ position: "relative"
505
+ };
506
+ return /* @__PURE__ */ React5.createElement("div", { ref, className, style: mergedStyle, ...rest }, /* @__PURE__ */ React5.createElement(Chessboard, { options: mergedOptions }), promotionMove && /* @__PURE__ */ React5.createElement(React5.Fragment, null, /* @__PURE__ */ React5.createElement(
507
+ "div",
536
508
  {
537
- key: piece,
538
- onClick: () => onPromotionPieceSelect(piece),
509
+ onClick: () => setPromotionMove(null),
539
510
  onContextMenu: (e) => {
540
511
  e.preventDefault();
512
+ setPromotionMove(null);
541
513
  },
542
514
  style: {
543
- width: "100%",
544
- aspectRatio: "1",
515
+ position: "absolute",
516
+ top: 0,
517
+ left: 0,
518
+ right: 0,
519
+ bottom: 0,
520
+ backgroundColor: "rgba(0, 0, 0, 0.1)",
521
+ zIndex: 1e3
522
+ }
523
+ }
524
+ ), /* @__PURE__ */ React5.createElement(
525
+ "div",
526
+ {
527
+ style: {
528
+ position: "absolute",
529
+ top: ((_b = (_a = promotionMove.to) == null ? void 0 : _a[1]) == null ? void 0 : _b.includes("8")) ? 0 : "auto",
530
+ bottom: ((_c = promotionMove.to) == null ? void 0 : _c[1].includes("1")) ? 0 : "auto",
531
+ left: promotionSquareLeft,
532
+ backgroundColor: "white",
533
+ width: squareWidth,
534
+ zIndex: 1001,
545
535
  display: "flex",
546
- alignItems: "center",
547
- justifyContent: "center",
548
- padding: 0,
549
- border: "none",
550
- cursor: "pointer",
551
- backgroundColor: "white"
552
- },
553
- onMouseEnter: (e) => {
554
- e.currentTarget.style.backgroundColor = "#f0f0f0";
555
- },
556
- onMouseLeave: (e) => {
557
- e.currentTarget.style.backgroundColor = "white";
536
+ flexDirection: "column",
537
+ boxShadow: "0 0 10px 0 rgba(0, 0, 0, 0.5)"
558
538
  }
559
539
  },
560
- defaultPieces[`${turn}${piece.toUpperCase()}`]()
561
- ))
562
- )));
563
- };
540
+ ["q", "r", "n", "b"].map((piece) => /* @__PURE__ */ React5.createElement(
541
+ "button",
542
+ {
543
+ key: piece,
544
+ onClick: () => onPromotionPieceSelect(piece),
545
+ onContextMenu: (e) => {
546
+ e.preventDefault();
547
+ },
548
+ style: {
549
+ width: "100%",
550
+ aspectRatio: "1",
551
+ display: "flex",
552
+ alignItems: "center",
553
+ justifyContent: "center",
554
+ padding: 0,
555
+ border: "none",
556
+ cursor: "pointer",
557
+ backgroundColor: "white"
558
+ },
559
+ onMouseEnter: (e) => {
560
+ e.currentTarget.style.backgroundColor = "#f0f0f0";
561
+ },
562
+ onMouseLeave: (e) => {
563
+ e.currentTarget.style.backgroundColor = "white";
564
+ }
565
+ },
566
+ defaultPieces[`${turn}${piece.toUpperCase()}`]()
567
+ ))
568
+ )));
569
+ }
570
+ );
571
+ Board.displayName = "ChessGame.Board";
564
572
 
565
573
  // src/components/ChessGame/parts/Sounds.tsx
566
574
  import { useMemo } from "react";
@@ -622,6 +630,7 @@ var Sounds = ({ sounds }) => {
622
630
  useBoardSounds(customSoundsAudios);
623
631
  return null;
624
632
  };
633
+ Sounds.displayName = "ChessGame.Sounds";
625
634
 
626
635
  // src/hooks/useKeyboardControls.ts
627
636
  import { useEffect as useEffect3 } from "react";
@@ -665,6 +674,7 @@ var KeyboardControls2 = ({
665
674
  useKeyboardControls(keyboardControls);
666
675
  return null;
667
676
  };
677
+ KeyboardControls2.displayName = "ChessGame.KeyboardControls";
668
678
 
669
679
  // src/components/ChessGame/index.ts
670
680
  var ChessGame = {