-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
172 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
.dropdownWrapper { | ||
position: relative; | ||
} | ||
|
||
.dropdownMenu { | ||
position: absolute; | ||
right: 0; | ||
top: 0; | ||
min-width: 100%; | ||
background-color: #302457; | ||
border-radius: 4px; | ||
overflow: hidden auto; | ||
transition: max-height 0.3s ease, padding 0.3s ease; | ||
box-shadow: 0 0 20px rgba(0, 0, 0, 0.05); | ||
|
||
&::-webkit-scrollbar { | ||
display: none; | ||
} | ||
} | ||
|
||
.dropdownMenuOn { | ||
max-height: 210px; | ||
padding: 4px 0; | ||
|
||
@media all and (max-width: 768px) { | ||
max-height: 0; | ||
padding: 0; | ||
} | ||
} | ||
|
||
.dropdownMenuOnClickMod { | ||
max-height: 200px; | ||
padding: 4px 0; | ||
} | ||
|
||
.dropdownMenuOff { | ||
max-height: 0; | ||
padding: 0; | ||
} | ||
|
||
.menuItem { | ||
padding: 8px 12px; | ||
cursor: pointer; | ||
transition: background-color 0.3s ease; | ||
font-size: 14px; | ||
width: 100%; | ||
min-width: 160px; | ||
|
||
&:hover { | ||
background-color: #2b214f; | ||
} | ||
} | ||
.dropdownWrapper { | ||
position: relative; | ||
} | ||
|
||
.dropdownMenu { | ||
position: absolute; | ||
right: 0; | ||
top: 0; | ||
min-width: 100%; | ||
background-color: #302457; | ||
border-radius: 4px; | ||
overflow: hidden auto; | ||
transition: max-height 0.3s ease, padding 0.3s ease; | ||
box-shadow: 0 0 20px rgba(0, 0, 0, 0.05); | ||
|
||
&::-webkit-scrollbar { | ||
display: none; | ||
} | ||
} | ||
|
||
.dropdownMenuOn { | ||
max-height: 210px; | ||
padding: 4px 0; | ||
|
||
@media all and (max-width: 768px) { | ||
max-height: 0; | ||
padding: 0; | ||
} | ||
} | ||
|
||
.dropdownMenuOnClickMod { | ||
max-height: 200px; | ||
padding: 4px 0; | ||
} | ||
|
||
.dropdownMenuOff { | ||
max-height: 0; | ||
padding: 0; | ||
} | ||
|
||
.menuItem { | ||
padding: 8px 12px; | ||
cursor: pointer; | ||
transition: background-color 0.3s ease; | ||
font-size: 14px; | ||
width: 100%; | ||
min-width: 160px; | ||
|
||
&:hover { | ||
background-color: #2b214f; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,110 @@ | ||
import React, { | ||
FC, | ||
MouseEventHandler, | ||
PropsWithChildren, | ||
useRef, | ||
useState | ||
} from 'react' | ||
import Link from 'next/link' | ||
import classnames from 'classnames' | ||
|
||
import styles from './Dropdown.module.scss' | ||
import { useOutside } from '@hooks/useOutside' | ||
import { defineEmits } from '@helpers/defineEmits' | ||
import { DropdownProps } from './Dropdown.types' | ||
|
||
export const Dropdown: FC<PropsWithChildren<DropdownProps>> = ({ | ||
options, | ||
clickMod, | ||
onOpen, | ||
onClose, | ||
margin, | ||
children | ||
}) => { | ||
const [active, setActive] = useState<boolean>(false) | ||
|
||
const emit = defineEmits<'open' | 'close'>({ | ||
open: onOpen ?? (() => {}), | ||
close: onClose ?? (() => {}) | ||
}) | ||
|
||
let timeout = useRef<ReturnType<typeof setTimeout>>() | ||
const onMouseEnter: MouseEventHandler<HTMLDivElement> = () => { | ||
clearTimeout(timeout.current) | ||
setActive(true) | ||
emit('open') | ||
} | ||
const onMouseLeave: MouseEventHandler<HTMLDivElement> = () => { | ||
timeout.current = setTimeout(() => { | ||
setActive(false) | ||
emit('close') | ||
}, 200) | ||
} | ||
|
||
const open: MouseEventHandler<HTMLDivElement> = event => { | ||
event.stopPropagation() | ||
if (active) { | ||
setActive(false) | ||
emit('close') | ||
return | ||
} | ||
|
||
setActive(true) | ||
emit('open') | ||
} | ||
|
||
const dropdownChildren = useRef<HTMLDivElement>(null) | ||
useOutside(dropdownChildren, () => { | ||
if (!clickMod) return | ||
setActive(false) | ||
emit('close') | ||
}) | ||
|
||
const dropdownMenuActive = active | ||
? clickMod | ||
? styles.dropdownMenuOnClickMod | ||
: styles.dropdownMenuOn | ||
: styles.dropdownMenuOff | ||
|
||
const marginStyle = { margin } | ||
|
||
return ( | ||
<div | ||
onMouseEnter={!clickMod ? onMouseEnter : undefined} | ||
onMouseLeave={!clickMod ? onMouseLeave : undefined} | ||
> | ||
<div | ||
ref={dropdownChildren} | ||
style={marginStyle} | ||
onClick={clickMod ? open : undefined} | ||
> | ||
{children} | ||
</div> | ||
<div className={styles.dropdownWrapper}> | ||
<div className={classnames(styles.dropdownMenu, dropdownMenuActive)}> | ||
{options.map(option => ( | ||
<React.Fragment key={option.id}> | ||
{option.href ? ( | ||
<div className={styles.menuItem} onClick={option.onClick}> | ||
<Link href={option.href}>{option.label}</Link> | ||
</div> | ||
) : ( | ||
<div className={styles.menuItem} onClick={option.onClick}> | ||
{option.label} | ||
</div> | ||
)} | ||
</React.Fragment> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
import React, { | ||
FC, | ||
MouseEventHandler, | ||
PropsWithChildren, | ||
useRef, | ||
useState | ||
} from 'react' | ||
import Link from 'next/link' | ||
import classnames from 'classnames' | ||
|
||
import styles from './Dropdown.module.scss' | ||
import { useOutside } from '@hooks/useOutside' | ||
import { defineEmits } from '@helpers/defineEmits' | ||
import { DropdownProps } from './Dropdown.types' | ||
import { useRouter } from 'next/router' | ||
|
||
export const Dropdown: FC<PropsWithChildren<DropdownProps>> = ({ | ||
options, | ||
clickMod, | ||
onOpen, | ||
onClose, | ||
margin, | ||
children | ||
}) => { | ||
const router = useRouter() | ||
const [active, setActive] = useState<boolean>(false) | ||
|
||
const emit = defineEmits<'open' | 'close'>({ | ||
open: onOpen ?? (() => {}), | ||
close: onClose ?? (() => {}) | ||
}) | ||
|
||
let timeout = useRef<ReturnType<typeof setTimeout>>() | ||
const onMouseEnter: MouseEventHandler<HTMLDivElement> = () => { | ||
clearTimeout(timeout.current) | ||
setActive(true) | ||
emit('open') | ||
} | ||
const onMouseLeave: MouseEventHandler<HTMLDivElement> = () => { | ||
timeout.current = setTimeout(() => { | ||
setActive(false) | ||
emit('close') | ||
}, 200) | ||
} | ||
|
||
const open: MouseEventHandler<HTMLDivElement> = event => { | ||
event.stopPropagation() | ||
if (active) { | ||
setActive(false) | ||
emit('close') | ||
return | ||
} | ||
|
||
setActive(true) | ||
emit('open') | ||
} | ||
|
||
const goTo = (href: string) => { | ||
return () => { | ||
void router.push(href) | ||
} | ||
} | ||
|
||
const dropdownChildren = useRef<HTMLDivElement>(null) | ||
useOutside(dropdownChildren, () => { | ||
if (!clickMod) return | ||
setActive(false) | ||
emit('close') | ||
}) | ||
|
||
const dropdownMenuActive = active | ||
? clickMod | ||
? styles.dropdownMenuOnClickMod | ||
: styles.dropdownMenuOn | ||
: styles.dropdownMenuOff | ||
|
||
const marginStyle = { margin } | ||
|
||
return ( | ||
<div | ||
onMouseEnter={!clickMod ? onMouseEnter : undefined} | ||
onMouseLeave={!clickMod ? onMouseLeave : undefined} | ||
> | ||
<div | ||
ref={dropdownChildren} | ||
style={marginStyle} | ||
onClick={clickMod ? open : undefined} | ||
> | ||
{children} | ||
</div> | ||
<div className={styles.dropdownWrapper}> | ||
<div className={classnames(styles.dropdownMenu, dropdownMenuActive)}> | ||
{options.map(option => ( | ||
<React.Fragment key={option.id}> | ||
{option.href ? ( | ||
<div className={styles.menuItem} onClick={goTo(option.href)}> | ||
<Link href={option.href}>{option.label}</Link> | ||
</div> | ||
) : ( | ||
<div className={styles.menuItem} onClick={option.onClick}> | ||
{option.label} | ||
</div> | ||
)} | ||
</React.Fragment> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters