Skip to content

Commit

Permalink
Fixing clickOutside
Browse files Browse the repository at this point in the history
  • Loading branch information
okbel committed Dec 8, 2020
1 parent e29dd24 commit b67b87c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
12 changes: 9 additions & 3 deletions components/common/UserNav/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { Avatar } from '@components/common'
import { Moon, Sun } from '@components/icons'
import { useUI } from '@components/ui/context'
import ClickOutside from '@lib/click-outside'

import {
disableBodyScroll,
enableBodyScroll,
clearAllBodyScrollLocks,
} from 'body-scroll-lock'

import useLogout from '@bigcommerce/storefront-data-hooks/use-logout'

interface DropdownMenuProps {
open?: boolean
}
Expand Down Expand Up @@ -75,7 +77,10 @@ const DropdownMenu: FC<DropdownMenuProps> = ({ open = false }) => {
className={cn(s.link, {
[s.active]: pathname === href,
})}
onClick={closeSidebarIfPresent}
onClick={() => {
setDisplay(false)
closeSidebarIfPresent()
}}
>
{name}
</a>
Expand All @@ -86,9 +91,10 @@ const DropdownMenu: FC<DropdownMenuProps> = ({ open = false }) => {
<li>
<a
className={cn(s.link, 'justify-between')}
onClick={() =>
onClick={() => {
theme === 'dark' ? setTheme('light') : setTheme('dark')
}
setDisplay(false)
}}
>
<div>
Theme: <strong>{theme}</strong>{' '}
Expand Down
17 changes: 5 additions & 12 deletions lib/click-outside/click-outside.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef, useEffect, Ref, MouseEvent } from 'react'
import React, { useRef, useEffect, MouseEvent } from 'react'
import hasParent from './has-parent'

interface ClickOutsideProps {
Expand All @@ -7,18 +7,12 @@ interface ClickOutsideProps {
children: any
}

const ClickOutside = forwardRef(
(
{ active = true, onClick, children }: ClickOutsideProps,
ref: Ref<HTMLDivElement> | null | any = {}
) => {
const innerRef = ref?.current
const ClickOutside = ({ active = true, onClick, children }: ClickOutsideProps) => {
const innerRef = useRef()

const handleClick = (event: any) => {
if (!hasParent(event.target, innerRef)) {
if (!hasParent(event.target, innerRef?.current)) {
if (typeof onClick === 'function') {
event.preventDefault()
event.stopImmediatePropagation()
onClick(event)
}
}
Expand All @@ -38,8 +32,7 @@ const ClickOutside = forwardRef(
}
})

return React.cloneElement(children, { ref })
return React.cloneElement(children, { ref: innerRef })
}
)

export default ClickOutside
14 changes: 7 additions & 7 deletions public/vercel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b67b87c

Please sign in to comment.