Adding Click Outside

This commit is contained in:
Belen Curcio 2020-12-02 13:16:30 -03:00
parent c37ed3d418
commit 6020d5cfd2
5 changed files with 107 additions and 45 deletions

View File

@ -1,9 +1,10 @@
import cn from 'classnames' import cn from 'classnames'
import Link from 'next/link' import Link from 'next/link'
import { FC, useState } from 'react' import { useRef, FC, useState } from 'react'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import s from './I18nWidget.module.css' import s from './I18nWidget.module.css'
import { Cross, ChevronUp } from '@components/icons' import { Cross, ChevronUp } from '@components/icons'
import ClickOutside from '@lib/click-outside'
interface LOCALE_DATA { interface LOCALE_DATA {
name: string name: string
img: { img: {
@ -37,12 +38,18 @@ const I18nWidget: FC = () => {
defaultLocale = 'en-US', defaultLocale = 'en-US',
asPath: currentPath, asPath: currentPath,
} = useRouter() } = useRouter()
const options = locales?.filter((val) => val !== locale) const options = locales?.filter((val) => val !== locale)
const currentLocale = locale || defaultLocale const currentLocale = locale || defaultLocale
const ref = useRef<HTMLDivElement | null>(null)
return ( return (
<ClickOutside active={display} onClick={() => setDisplay(false)} ref={ref}>
<nav className={s.root}> <nav className={s.root}>
<div className="flex items-center relative"> <div
className="flex items-center relative"
onClick={() => setDisplay(!display)}
>
<button className={s.button} aria-label="Language selector"> <button className={s.button} aria-label="Language selector">
<img <img
className="block mr-2 w-5" className="block mr-2 w-5"
@ -50,10 +57,7 @@ const I18nWidget: FC = () => {
alt={LOCALES_MAP[currentLocale].img.alt} alt={LOCALES_MAP[currentLocale].img.alt}
/> />
{options && ( {options && (
<span <span className="cursor-pointer">
className="cursor-pointer"
onClick={() => setDisplay(!display)}
>
<ChevronUp className={cn({ [s.icon]: display })} /> <ChevronUp className={cn({ [s.icon]: display })} />
</span> </span>
)} )}
@ -75,7 +79,10 @@ const I18nWidget: FC = () => {
{options.map((locale) => ( {options.map((locale) => (
<li key={locale}> <li key={locale}>
<Link href={currentPath} locale={locale}> <Link href={currentPath} locale={locale}>
<a className={cn(s.item)} onClick={() => setDisplay(false)}> <a
className={cn(s.item)}
onClick={() => setDisplay(false)}
>
{LOCALES_MAP[locale].name} {LOCALES_MAP[locale].name}
</a> </a>
</Link> </Link>
@ -86,6 +93,7 @@ const I18nWidget: FC = () => {
) : null} ) : null}
</div> </div>
</nav> </nav>
</ClickOutside>
) )
} }

View File

@ -0,0 +1,45 @@
import React, { forwardRef, useEffect, Ref, MouseEvent } from 'react'
import hasParent from './has-parent'
interface ClickOutsideProps {
active: boolean
onClick: (e?: MouseEvent) => void
children: any
}
const ClickOutside = (
{ active = true, onClick, children }: ClickOutsideProps,
ref: Ref<HTMLDivElement> | null | any = {}
) => {
console.log('--------', active, '-----------')
const innerRef = ref?.current
const handleClick = (event: any) => {
console.log(innerRef, event.target)
if (!hasParent(event.target, innerRef)) {
if (typeof onClick === 'function') {
event.preventDefault()
event.stopImmediatePropagation()
onClick(event)
}
}
}
useEffect(() => {
if (active) {
document.addEventListener('mousedown', handleClick)
document.addEventListener('touchstart', handleClick)
}
return () => {
if (active) {
document.removeEventListener('mousedown', handleClick)
document.removeEventListener('touchstart', handleClick)
}
}
})
return React.cloneElement(children, { ref })
}
export default forwardRef(ClickOutside)

View File

@ -0,0 +1,5 @@
import isInDOM from './is-in-dom'
export default function hasParent(element, root) {
return root && root.contains(element) && isInDOM(element)
}

View File

@ -0,0 +1 @@
export { default } from './click-outside'

View File

@ -0,0 +1,3 @@
export default function isInDom(obj) {
return Boolean(obj.closest('body'))
}