-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
117 additions
and
91 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
MutableRefObject, useCallback, useEffect, useRef, useState, | ||
} from 'react'; | ||
|
||
interface UseModalProps { | ||
onClose?: () => void; | ||
isOpen?: boolean; | ||
animationDelay: number; | ||
} | ||
|
||
export function useModal({ | ||
animationDelay, isOpen, onClose, | ||
}: UseModalProps) { | ||
const [isClosing, setIsClosing] = useState(false); | ||
const [isMounted, setIsMounted] = useState(false); | ||
const timerRef = useRef() as MutableRefObject<ReturnType<typeof setTimeout>>; | ||
|
||
useEffect(() => { | ||
if (isOpen) { | ||
setIsMounted(true); | ||
} | ||
}, [isOpen]); | ||
|
||
const close = useCallback(() => { | ||
if (onClose) { | ||
setIsClosing(true); | ||
timerRef.current = setTimeout(() => { | ||
onClose(); | ||
setIsClosing(false); | ||
}, animationDelay); | ||
} | ||
}, [animationDelay, onClose]); | ||
|
||
// Новые ссылки!!! | ||
const onKeyDown = useCallback((e: KeyboardEvent) => { | ||
if (e.key === 'Escape') { | ||
close(); | ||
} | ||
}, [close]); | ||
|
||
useEffect(() => { | ||
if (isOpen) { | ||
window.addEventListener('keydown', onKeyDown); | ||
} | ||
|
||
return () => { | ||
clearTimeout(timerRef.current); | ||
window.removeEventListener('keydown', onKeyDown); | ||
}; | ||
}, [isOpen, onKeyDown]); | ||
|
||
return { | ||
isClosing, | ||
isMounted, | ||
close, | ||
}; | ||
} |
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 |
---|---|---|
|
@@ -49,3 +49,9 @@ | |
transform: translateY(0%); | ||
} | ||
} | ||
|
||
.isClosing { | ||
.content { | ||
transform: translateY(100%); | ||
} | ||
} |
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,40 +1,50 @@ | ||
import { classNames, Mods } from 'shared/lib/classNames/classNames'; | ||
import React, { memo, ReactNode } from 'react'; | ||
import { useTheme } from 'app/providers/ThemeProvider'; | ||
import { Overlay } from '../Overlay/Overlay'; | ||
import cls from './Drawer.module.scss'; | ||
import { Portal } from '../Portal/Portal'; | ||
import { classNames, Mods } from "shared/lib/classNames/classNames"; | ||
import React, { memo, ReactNode } from "react"; | ||
import { useTheme } from "app/providers/ThemeProvider"; | ||
import { useModal } from "shared/lib/hooks/useModal/useModal"; | ||
import { Overlay } from "../Overlay/Overlay"; | ||
import cls from "./Drawer.module.scss"; | ||
import { Portal } from "../Portal/Portal"; | ||
|
||
interface DrawerProps { | ||
className?: string; | ||
children: ReactNode; | ||
isOpen?: boolean; | ||
onClose?: () => void; | ||
className?: string; | ||
children: ReactNode; | ||
isOpen?: boolean; | ||
onClose?: () => void; | ||
lazy?: boolean; | ||
} | ||
|
||
export const Drawer = memo((props: DrawerProps) => { | ||
const { | ||
className, | ||
children, | ||
onClose, | ||
isOpen, | ||
} = props; | ||
const { theme } = useTheme(); | ||
const { className, children, onClose, isOpen, lazy } = props; | ||
const { theme } = useTheme(); | ||
|
||
const mods: Mods = { | ||
[cls.opened]: isOpen, | ||
}; | ||
const { close, isClosing, isMounted } = useModal({ | ||
animationDelay: 300, | ||
onClose, | ||
isOpen, | ||
}); | ||
|
||
return ( | ||
<Portal> | ||
<div className={classNames(cls.Drawer, mods, [className, theme, 'app_drawer'])}> | ||
<Overlay onClick={onClose} /> | ||
<div | ||
className={cls.content} | ||
> | ||
{children} | ||
</div> | ||
</div> | ||
</Portal> | ||
); | ||
const mods: Mods = { | ||
[cls.opened]: isOpen, | ||
[cls.isClosing]: isClosing, | ||
}; | ||
|
||
if (lazy && !isMounted) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Portal> | ||
<div | ||
className={classNames(cls.Drawer, mods, [ | ||
className, | ||
theme, | ||
"app_drawer", | ||
])} | ||
> | ||
<Overlay onClick={close} /> | ||
<div className={cls.content}>{children}</div> | ||
</div> | ||
</Portal> | ||
); | ||
}); |
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