import React, { useCallback, useEffect, useRef, useState } from 'react'; import { Flex, SilkeBox } from '../silke-box'; import { SilkeTitle } from '../silke-text'; import { SilkeButton } from '../silke-button'; import { useKeyUp } from '@vev/utils'; import { useMouseDownOutside } from '../../hooks'; import classnames from 'classnames/bind'; import styles from './silke-drawer.scss'; import { SilkeTitleKind } from '../silke-text/silke-text-types'; const cl = classnames.bind(styles); export interface SilkeDrawerProps { title?: React.ReactNode; titleKind?: SilkeTitleKind; open: boolean; onRequestClose: () => void; displayCloseButton?: boolean; disableFocusTrap?: boolean; displayFader?: boolean; } export const SilkeDrawer = ({ open, onRequestClose, displayCloseButton = true, children, title, titleKind = 's', displayFader, }: React.PropsWithChildren) => { const [shouldRender, setShouldRender] = useState(false); const ref = useRef(null); useKeyUp('Escape', () => { if (open) { onRequestClose(); } }); useMouseDownOutside(ref, onRequestClose, !open); useEffect(() => { if (open) { setShouldRender(true); } }, [open]); const onTransitionEnd = useCallback(() => { if (!open) { setShouldRender(false); } }, [open]); const [doDisplayFader, setDoDisplayFader] = useState(displayFader); const onScroll = () => { if (ref.current) { const { scrollTop, scrollHeight, clientHeight } = ref.current; const isOnBottom = scrollTop + clientHeight === scrollHeight; setDoDisplayFader(displayFader && !isOnBottom); } }; return (
{title || displayCloseButton ? ( {title ? ( {title} ) : ( )} ) : null} {shouldRender ? children :
); };