next-chessground 1.2.0 → 1.3.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.
package/dist/index.es.js CHANGED
@@ -1,4 +1,4 @@
1
- import React, { createContext, useState, useContext, useEffect, forwardRef } from 'react';
1
+ import React, { createContext, useState, useContext, useEffect, forwardRef, useRef, useImperativeHandle } from 'react';
2
2
  import require$$1 from 'react-dom';
3
3
 
4
4
  function _defineProperty(e, r, t) {
@@ -6926,13 +6926,12 @@ const constants = {
6926
6926
 
6927
6927
  const useChess = props => {
6928
6928
  const [fen, setFen] = useState(props.fen || initialFen);
6929
- const [chess, setChess] = useState(new Chess(fen));
6929
+ const [chess] = useState(() => new Chess(props.fen));
6930
6930
 
6931
6931
  // reinitialize when FEN changes from props
6932
6932
  useEffect(() => {
6933
6933
  if (props.fen) {
6934
6934
  setFen(props.fen);
6935
- setChess(new Chess(props.fen));
6936
6935
  setLastMove([]);
6937
6936
  }
6938
6937
  }, [props.fen, props.reset]);
@@ -6954,6 +6953,15 @@ const useChess = props => {
6954
6953
  const move = onMove(lastMove[0], lastMove[1], promotion);
6955
6954
  return move;
6956
6955
  };
6956
+ const onUndo = () => {
6957
+ const undone = chess.undo();
6958
+ if (undone) {
6959
+ setFen(chess.fen());
6960
+ setLastMove([]);
6961
+ return undone;
6962
+ }
6963
+ return null;
6964
+ };
6957
6965
  return {
6958
6966
  chess,
6959
6967
  fen,
@@ -6962,7 +6970,8 @@ const useChess = props => {
6962
6970
  orientation,
6963
6971
  promotion,
6964
6972
  onMove,
6965
- onPromote
6973
+ onPromote,
6974
+ onUndo
6966
6975
  };
6967
6976
  };
6968
6977
 
@@ -16823,13 +16832,20 @@ const Chessboard = (props, ref) => {
16823
16832
  orientation,
16824
16833
  promotion,
16825
16834
  onMove,
16826
- onPromote
16835
+ onPromote,
16836
+ onUndo
16827
16837
  } = useChess(props);
16838
+
16839
+ // Expose methods through ref
16840
+ const boardRef = useRef();
16841
+ useImperativeHandle(ref, () => ({
16842
+ board: boardRef.current?.board,
16843
+ undo: onUndo
16844
+ }));
16828
16845
  const handleMove = async (from, to) => {
16829
16846
  const move = onMove(from, to, promotion);
16830
- // Show promotion modal only when the move is not an undo
16831
- if (!move && !props.isUndo) {
16832
- show();
16847
+ if (!move) {
16848
+ show(); // move is a promotion, show the promotion modal
16833
16849
  return false;
16834
16850
  }
16835
16851
  if (theme.playSounds) {
@@ -16868,7 +16884,7 @@ const Chessboard = (props, ref) => {
16868
16884
  return /*#__PURE__*/React.createElement("div", {
16869
16885
  className: mergeClassNames('chessground', theme.highlight && 'highlight', theme.board, theme.pieces)
16870
16886
  }, /*#__PURE__*/React.createElement(Chessground, _extends({
16871
- ref: ref,
16887
+ ref: boardRef,
16872
16888
  coordinates: theme.coordinates,
16873
16889
  onMove: handleMove,
16874
16890
  fen: fen,
package/dist/index.js CHANGED
@@ -6935,13 +6935,12 @@ const constants = {
6935
6935
 
6936
6936
  const useChess = props => {
6937
6937
  const [fen, setFen] = React.useState(props.fen || initialFen);
6938
- const [chess, setChess] = React.useState(new Chess(fen));
6938
+ const [chess] = React.useState(() => new Chess(props.fen));
6939
6939
 
6940
6940
  // reinitialize when FEN changes from props
6941
6941
  React.useEffect(() => {
6942
6942
  if (props.fen) {
6943
6943
  setFen(props.fen);
6944
- setChess(new Chess(props.fen));
6945
6944
  setLastMove([]);
6946
6945
  }
6947
6946
  }, [props.fen, props.reset]);
@@ -6963,6 +6962,15 @@ const useChess = props => {
6963
6962
  const move = onMove(lastMove[0], lastMove[1], promotion);
6964
6963
  return move;
6965
6964
  };
6965
+ const onUndo = () => {
6966
+ const undone = chess.undo();
6967
+ if (undone) {
6968
+ setFen(chess.fen());
6969
+ setLastMove([]);
6970
+ return undone;
6971
+ }
6972
+ return null;
6973
+ };
6966
6974
  return {
6967
6975
  chess,
6968
6976
  fen,
@@ -6971,7 +6979,8 @@ const useChess = props => {
6971
6979
  orientation,
6972
6980
  promotion,
6973
6981
  onMove,
6974
- onPromote
6982
+ onPromote,
6983
+ onUndo
6975
6984
  };
6976
6985
  };
6977
6986
 
@@ -16832,13 +16841,20 @@ const Chessboard = (props, ref) => {
16832
16841
  orientation,
16833
16842
  promotion,
16834
16843
  onMove,
16835
- onPromote
16844
+ onPromote,
16845
+ onUndo
16836
16846
  } = useChess(props);
16847
+
16848
+ // Expose methods through ref
16849
+ const boardRef = React.useRef();
16850
+ React.useImperativeHandle(ref, () => ({
16851
+ board: boardRef.current?.board,
16852
+ undo: onUndo
16853
+ }));
16837
16854
  const handleMove = async (from, to) => {
16838
16855
  const move = onMove(from, to, promotion);
16839
- // Show promotion modal only when the move is not an undo
16840
- if (!move && !props.isUndo) {
16841
- show();
16856
+ if (!move) {
16857
+ show(); // move is a promotion, show the promotion modal
16842
16858
  return false;
16843
16859
  }
16844
16860
  if (theme.playSounds) {
@@ -16877,7 +16893,7 @@ const Chessboard = (props, ref) => {
16877
16893
  return /*#__PURE__*/React__default["default"].createElement("div", {
16878
16894
  className: mergeClassNames('chessground', theme.highlight && 'highlight', theme.board, theme.pieces)
16879
16895
  }, /*#__PURE__*/React__default["default"].createElement(Chessground, _extends({
16880
- ref: ref,
16896
+ ref: boardRef,
16881
16897
  coordinates: theme.coordinates,
16882
16898
  onMove: handleMove,
16883
16899
  fen: fen,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-chessground",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "React and Next wrapper for Chessground with chessboard and pieces out of the box",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.es.js",