From 1b015309297b77ce4e132e5b5d8679beba58d9d2 Mon Sep 17 00:00:00 2001 From: Martin Heidegger Date: Thu, 18 Jan 2018 15:59:28 +0900 Subject: [PATCH] Fixed & simplified header hamburger display. --- elements/header.js | 58 +++++++++++++++++++++++++--------------------- pages/main.js | 1 + 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/elements/header.js b/elements/header.js index d17180cd..d7ddbaa3 100644 --- a/elements/header.js +++ b/elements/header.js @@ -19,6 +19,9 @@ const header = css` background-color: var(--color-neutral); color: var(--color-white); -webkit-app-region: drag; + z-index: 1000; + position: relative; + width: 100%; } ` @@ -45,15 +48,10 @@ HeaderElement.prototype = Object.create(Nanocomponent.prototype) HeaderElement.prototype.createElement = function (props) { this.props = props - var { isReady, onimport, oncreate, onreport } = this.props - var { showMenu, willShowMenu } = this.state + var { isReady, onimport, oncreate, onreport, onupdate } = this.props + var { showMenu } = this.state var importButton = DatImport() - if (typeof willShowMenu === 'boolean') { - showMenu = this.state.showMenu = willShowMenu - this.state.willShowMenu = null - } - assert.equal(typeof isReady, 'boolean', 'elements/header: isReady should be type boolean') assert.equal(typeof onimport, 'function', 'elements/header: onimport should be type function') assert.equal(typeof oncreate, 'function', 'elements/header: oncreate should be type function') @@ -76,29 +74,30 @@ HeaderElement.prototype.createElement = function (props) { var menuButton = button.icon('Open Menu', { icon: icon('menu', { class: menuButtonIcon }), class: 'ml3 v-mid color-neutral-20 hover-color-white pointer', - onclick: toggle + onclick: function () { + toggle(true) + return false + } }) var self = this - function toggle () { - if (self.state.showMenu) hide() - else show() - } - - function show () { - document.body.addEventListener('click', clickedOutside) - self.state.willShowMenu = true - self.createElement(self.props) - } - - function hide () { - document.body.removeEventListener('click', clickedOutside) - self.state.willShowMenu = false - self.createElement(self.props) + function toggle (newState) { + if (self.state.showMenu !== newState) { + self.state.showMenu = newState + setTimeout(function () { + if (newState) { + document.body.addEventListener('click', clickedOutside) + } else { + document.body.removeEventListener('click', clickedOutside) + } + }, 1) + self.state.update = true + onupdate() + } } function clickedOutside (e) { - if (!self.createElement(self.props).contains(e.target)) hide() + if (!self.createElement(self.props).contains(e.target)) toggle(false) } return html` @@ -130,7 +129,14 @@ HeaderElement.prototype.createElement = function (props) { ` } +HeaderElement.prototype.consumeUpdate = function () { + if (!this.state.update) { + return false + } + this.state.update = false + return true +} + HeaderElement.prototype.update = function (props) { - return props.isReady !== this.props.isReady || - typeof this.state.willShowMenu === 'boolean' + return props.isReady !== this.props.isReady || this.consumeUpdate() } diff --git a/pages/main.js b/pages/main.js index 6434a2a8..07a1ffdb 100644 --- a/pages/main.js +++ b/pages/main.js @@ -31,6 +31,7 @@ function mainView (state, emit) { const isReady = state.dats.ready const headerProps = { isReady: isReady, + onupdate: () => emit('render'), oncreate: () => emit('dats:create'), onimport: (link) => emit('dats:download', link), onreport: () => shell.openExternal('https://github.com/datproject/dat-desktop/issues')