From 918420b68ab398b5240b03d828dc4f1ef686c8ca Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Mon, 28 Nov 2016 21:19:53 -0800 Subject: [PATCH] Simple approach to centering windows on load/resize --- js/components/WindowManager.jsx | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/js/components/WindowManager.jsx b/js/components/WindowManager.jsx index f6516d7f..19083d4f 100644 --- a/js/components/WindowManager.jsx +++ b/js/components/WindowManager.jsx @@ -9,19 +9,28 @@ const WINDOW_WIDTH = 275; class WindowManager extends React.Component { constructor(props) { super(props); - const state = {}; - this.validChildren().forEach((child, i) => { - state[i] = { - x: -(WINDOW_WIDTH / 2), - y: 0 - }; - }); - this.state = state; this.windowNodes = []; + this.state = {}; + window.addEventListener('resize', this.centerWindows.bind(this)); this.getRef = this.getRef.bind(this); this.handleMouseDown = this.handleMouseDown.bind(this); } + centerWindows() { + const {innerHeight, innerWidth} = window; + const state = {}; + const children = this.validChildren(); + const totalHeight = children.length * WINDOW_HEIGHT; + children.forEach((child, i) => { + const offset = WINDOW_HEIGHT * i; + state[i] = { + left: (innerWidth / 2) - WINDOW_WIDTH / 2, + top: (innerHeight / 2) - (totalHeight / 2) + offset + }; + }); + this.setState(state); + } + getRef(node) { this.windowNodes.push(node); } @@ -97,15 +106,15 @@ class WindowManager extends React.Component { position: 'absolute', width: 0, height: 0, - top: '50vh', - left: '50vw' + top: 0, + left: 0 }; return (
{this.validChildren().map((child, i) => { - const position = this.state[i] || {}; + const position = this.state[i]; /* eslint-disable react/jsx-no-bind */ - return ( + return position && (
this.handleMouseDown(i, e)} ref={this.getRef}