From 88a4e13f9a041fbbfbaa2d5843d0fb630da26bb8 Mon Sep 17 00:00:00 2001 From: Ru Singh Date: Mon, 6 Dec 2021 18:50:26 +0530 Subject: [PATCH] Site switcher toggle functionality (#1500) * chore(docker): improve repeat contributions workflow * This change adds two new commands to gracefully stop and remove the Postgres and Clickhouse docker containers. To do so, it also gives them a recognizable name. * Additionally, the Postgres container is updated to use a named volume for its data. This lower friction for repeat contributions where one would otherwise sign up and activate their accounts again and again each time. * fix(ui): site switcher should toggle, instead of just opening * fix(ux): toggling the site switcher & closing from anywhere --- assets/js/dashboard/site-switcher.js | 33 +++++++++++++++++++++------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/assets/js/dashboard/site-switcher.js b/assets/js/dashboard/site-switcher.js index 4d0c08615..54c7385c7 100644 --- a/assets/js/dashboard/site-switcher.js +++ b/assets/js/dashboard/site-switcher.js @@ -4,9 +4,11 @@ import { Transition } from '@headlessui/react' export default class SiteSwitcher extends React.Component { constructor() { super() - this.handleClick = this.handleClick.bind(this) + this.handleClick = this.handleClick.bind(this); this.handleKeydown = this.handleKeydown.bind(this); this.populateSites = this.populateSites.bind(this); + this.toggle = this.toggle.bind(this); + this.siteSwitcherButton = React.createRef(); this.state = { open: false, sites: null, @@ -17,13 +19,15 @@ export default class SiteSwitcher extends React.Component { componentDidMount() { this.populateSites(); + this.siteSwitcherButton.current.addEventListener("click", this.toggle); document.addEventListener("keydown", this.handleKeydown); - document.addEventListener('mousedown', this.handleClick, false); + document.addEventListener('click', this.handleClick, false); } - + componentWillUnmount() { + this.siteSwitcherButton.current.removeEventListener("click", this.toggle); document.removeEventListener("keydown", this.handleKeydown); - document.removeEventListener('mousedown', this.handleClick, false); + document.removeEventListener('click', this.handleClick, false); } populateSites() { @@ -39,10 +43,14 @@ export default class SiteSwitcher extends React.Component { } handleClick(e) { + // If this is an interaction with the dropdown menu itself, do nothing. if (this.dropDownNode && this.dropDownNode.contains(e.target)) return; + + // If the dropdown is not open, do nothing. if (!this.state.open) return; - this.setState({open: false}) + // In any other case, close it. + this.setState({ open: false }); } handleKeydown(e) { @@ -60,10 +68,19 @@ export default class SiteSwitcher extends React.Component { } - toggle() { + toggle(e) { + /** + * React doesn't seem to prioritise its own events when events are bubbling, and is unable to stop its events from propagating to the document's (root) event listeners which are attached on the DOM. + * + * A simple trick is to hook up our own click event listener via a ref node, which allows React to manage events in this situation better between the two. + */ + e.stopPropagation(); + e.preventDefault(); if (!this.props.loggedIn) return; - this.setState({open: !this.state.open}) + this.setState((prevState) => ({ + open: !prevState.open + })) if (!this.state.sites) { this.populateSites(); @@ -138,7 +155,7 @@ export default class SiteSwitcher extends React.Component { return (
-