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 {
position: absolute;
border: 1px solid $brand-grey-lighter;
padding: 8px;
border-radius: $border-radius;
max-width: 200px;
display: none;
margin: 0;
padding: 0;
width: 100%;
}
@ -55,6 +55,7 @@
cursor: pointer;
font-size: $font-size-mini;
text-align: left;
padding: 4px;
}
.networkList li span {
@ -64,6 +65,7 @@
.networkList li:hover {
color: $brand-violet;
font-size: $font-size-mini;
background-color: $brand-grey-lighter;
}
.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 { CONNECTIONS } from '../../config'
import { User } from '../../context'
@ -21,8 +21,19 @@ export const oceanConfig =
/* NETWORK SWITCHER */
export function NetworkSwitcher() {
const node: any = useRef()
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(() => {
if (networkUrlParam !== '') {
switchNetwork(networkUrlParam)
@ -30,24 +41,30 @@ export function NetworkSwitcher() {
}, [])
*/
const handleTogle = () => {
setIsToggled(!isToggled)
const handleTogle = (e: any) => {
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
(window.location.href = `${window.location.origin}?network=${networkName}`)
//userContext.switchNetwork(networkName, getNetworkConfig(networkName))
window.location.href = `${window.location.origin}?network=${networkName}`
//userContext.switchNetwork(networkName, getNetworkConfig(networkName))
setIsToggled(false) // for the case without force page refresh
}
return (
return !isBurner ? null : (
<div
ref={node}
className={`${styles.networkListWrapper} ${
isToggled ? styles.on : ''
}`}
>
<em onClick={() => handleTogle()}>
<em onClick={e => handleTogle(e)}>
<span>Change Network</span>
</em>
<ul className={styles.networkList}>