1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 20:36:40 +07:00
vault-frontend/src/containers/sidebars/SidebarWrapper/index.tsx
2022-01-08 18:01:38 +07:00

33 lines
834 B
TypeScript

import React, { FC, useEffect, useRef } from "react";
import styles from "./styles.module.scss";
import { createPortal } from "react-dom";
import { clearAllBodyScrollLocks, disableBodyScroll } from "body-scroll-lock";
import { useCloseOnEscape } from "~/hooks";
interface IProps {
onClose?: () => void;
}
const SidebarWrapper: FC<IProps> = ({ children, onClose }) => {
const ref = useRef<HTMLDivElement>(null);
useCloseOnEscape(onClose);
useEffect(() => {
if (!ref.current) return;
disableBodyScroll(ref.current, { reserveScrollBarGap: true });
return () => clearAllBodyScrollLocks();
}, []);
return createPortal(
<div className={styles.wrapper} ref={ref}>
<div className={styles.clicker} onClick={onClose} />
{children}
</div>,
document.body
);
};
export { SidebarWrapper };