1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

handle click outside to collapse Network switcher dropdown, remove rendering network switcher in case of metamask

This commit is contained in:
Max Berman 2020-01-14 13:32:15 +01:00
parent e153421771
commit b4fc9883e2
2 changed files with 30 additions and 11 deletions

View File

@ -28,11 +28,11 @@
.networkList { .networkList {
position: absolute; position: absolute;
border: 1px solid $brand-grey-lighter; border: 1px solid $brand-grey-lighter;
padding: 8px;
border-radius: $border-radius; border-radius: $border-radius;
max-width: 200px; max-width: 200px;
display: none; display: none;
margin: 0; margin: 0;
padding: 0;
width: 100%; width: 100%;
} }
@ -55,6 +55,7 @@
cursor: pointer; cursor: pointer;
font-size: $font-size-mini; font-size: $font-size-mini;
text-align: left; text-align: left;
padding: 4px;
} }
.networkList li span { .networkList li span {
@ -64,6 +65,7 @@
.networkList li:hover { .networkList li:hover {
color: $brand-violet; color: $brand-violet;
font-size: $font-size-mini; font-size: $font-size-mini;
background-color: $brand-grey-lighter;
} }
.networkList li:before { .networkList li:before {

View File

@ -1,4 +1,4 @@
import React, { useState, useContext, useEffect } from 'react' import React, { useState, useContext, useEffect, useRef } from 'react'
import { urlq } from '../../utils/utils' import { urlq } from '../../utils/utils'
import { CONNECTIONS } from '../../config' import { CONNECTIONS } from '../../config'
import { User } from '../../context' import { User } from '../../context'
@ -21,8 +21,19 @@ export const oceanConfig =
/* NETWORK SWITCHER */ /* NETWORK SWITCHER */
export function NetworkSwitcher() { export function NetworkSwitcher() {
const node: any = useRef()
const [isToggled, setIsToggled] = useState(false) const [isToggled, setIsToggled] = useState(false)
/*
useEffect(() => {
// Handle click outside to collapse Network switcher dropdown
// add listener when mounted
document.addEventListener('mousedown', handleTogle)
// return function when unmounted
return () => {
document.removeEventListener('mousedown', handleTogle)
}
}, [])
/*
useEffect(() => { useEffect(() => {
if (networkUrlParam !== '') { if (networkUrlParam !== '') {
switchNetwork(networkUrlParam) switchNetwork(networkUrlParam)
@ -30,24 +41,30 @@ export function NetworkSwitcher() {
}, []) }, [])
*/ */
const handleTogle = () => { const handleTogle = (e: any) => {
setIsToggled(!isToggled) const isClickedInside = node.current.contains(e.target)
setIsToggled(isClickedInside)
} }
const { network } = useContext(User) const { network, isBurner } = useContext(User)
const switchNetwork = (networkName: string): any => console.log(isBurner)
const switchNetwork = (networkName: string): any => {
// Force page to get refreshed // Force page to get refreshed
(window.location.href = `${window.location.origin}?network=${networkName}`) window.location.href = `${window.location.origin}?network=${networkName}`
//userContext.switchNetwork(networkName, getNetworkConfig(networkName)) //userContext.switchNetwork(networkName, getNetworkConfig(networkName))
setIsToggled(false) // for the case without force page refresh
}
return ( return !isBurner ? null : (
<div <div
ref={node}
className={`${styles.networkListWrapper} ${ className={`${styles.networkListWrapper} ${
isToggled ? styles.on : '' isToggled ? styles.on : ''
}`} }`}
> >
<em onClick={() => handleTogle()}> <em onClick={e => handleTogle(e)}>
<span>Change Network</span> <span>Change Network</span>
</em> </em>
<ul className={styles.networkList}> <ul className={styles.networkList}>