Skip to content

Commit

Permalink
fix(square): reset square coordinates when screen size changes
Browse files Browse the repository at this point in the history
  • Loading branch information
willb335 committed Jun 26, 2018
1 parent f386656 commit 4a27a34
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"lint": "eslint src",
"validate": "npm-run-all --parallel test lint build",
"travis-deploy-once": "travis-deploy-once",
"semantic-release": "semantic-release -d",
"semantic-release": "npx semantic-release",
"commitmsg": "commitlint -e $GIT_PARAMS",
"start": "webpack-dev-server --open --config webpack.dev.js",
"report-coverage": "cat ./coverage/lcov.info | codecov"
Expand Down
6 changes: 3 additions & 3 deletions src/Chessboard/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ class Board extends Component {
onMouseOverSquare={context.onMouseOverSquare}
onMouseOutSquare={context.onMouseOutSquare}
onHoverSquareStyle={context.onHoverSquareStyle}
selectedSquareStyle={
context.selectedSquareStyle
}
selectedSquareStyle={context.selectedSquareStyle}
id={context.id}
screenWidth={context.screenWidth}
screenHeight={context.screenHeight}
>
{this.hasPiece(context.currentPosition, square) ? (
<Fragment>
Expand Down
22 changes: 21 additions & 1 deletion src/Chessboard/Square.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class Square extends Component {
onMouseOverSquare: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
onMouseOutSquare: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
onHoverSquareStyle: PropTypes.object,
selectedSquareStyle: PropTypes.object
selectedSquareStyle: PropTypes.object,
screenWidth: PropTypes.number,
screenHeight: PropTypes.number
};

componentDidMount() {
Expand All @@ -31,6 +33,24 @@ class Square extends Component {
setSquareCoordinates(x, y, square);
}

componentDidUpdate(prevProps) {
const {
screenWidth,
screenHeight,
square,
setSquareCoordinates
} = this.props;

const didScreenSizeChange =
prevProps.screenWidth !== screenWidth ||
prevProps.screenHeight !== screenHeight;

if (didScreenSizeChange) {
const { x, y } = this[square].getBoundingClientRect();
setSquareCoordinates(x, y, square);
}
}

render() {
const {
connectDropTarget,
Expand Down
4 changes: 3 additions & 1 deletion src/Chessboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,9 @@ class Chessboard extends Component {
dropSquare,
setAnimation: this.setAnimation,
setTouchState: this.setTouchState,
currentPosition
currentPosition,
screenWidth,
screenHeight
}
}}
>
Expand Down
3 changes: 2 additions & 1 deletion src/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import HumanVsHuman from './integrations/HumanVsHuman';
import { roughSquare } from './integrations/customRough';
import RandomFEN from './integrations/RandomFEN';

const calcWidth = screenWidth => (screenWidth < 500 ? 320 : 480);
const calcWidth = screenWidth => (screenWidth < 500 ? 150 : 480);
const boardStyle = {
borderRadius: '5px',
boxShadow: `0 5px 15px rgba(0, 0, 0, 0.5)`
Expand Down Expand Up @@ -91,6 +91,7 @@ class Demo extends Component {
{({ position }) => (
<Chessboard
calcWidth={calcWidth}
// width={320}
id="random"
orientation="black"
position={position}
Expand Down

0 comments on commit 4a27a34

Please sign in to comment.