Skip to content

Commit

Permalink
[#571] Drawer 컴포넌트 style 개선 (#573)
Browse files Browse the repository at this point in the history
* fix: drawer body scroll 되는 문제 해결

- drawer style fixed로 수정

* fix: useBodyScrollLock 훅 개선
  • Loading branch information
gxxrxn committed Jun 17, 2024
1 parent bbd1123 commit f21ada4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
36 changes: 36 additions & 0 deletions src/hooks/useBodyScrollLock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useCallback, useEffect, useRef } from 'react';

type Options = {
enabled?: boolean;
};

export function useBodyScrollLock(options?: Options) {
const enabled = options?.enabled;
const scrollRef = useRef(0);

const lockScroll = useCallback(() => {
scrollRef.current = window.scrollY;
document.body.style.overflow = 'hidden';
document.body.style.marginTop = `-${scrollRef.current}px`;
}, []);

const openScroll = useCallback(() => {
document.body.style.removeProperty('overflow');
document.body.style.removeProperty('margin-top');
window.scrollTo(0, scrollRef.current);
}, []);

useEffect(() => {
if (enabled === undefined) {
return;
}

if (enabled) {
lockScroll();
} else {
openScroll();
}
}, [enabled, lockScroll, openScroll]);

return { lockScroll, openScroll };
}
17 changes: 11 additions & 6 deletions src/v1/base/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createContext, PropsWithChildren, ReactNode, useContext } from 'react';

import { IconClose } from '@public/icons';
import Portal from './Portal';
import { useBodyScrollLock } from '@/hooks/useBodyScrollLock';

import Portal from '@/v1/base/Portal';

interface DrawerProps {
isOpen: boolean;
Expand All @@ -18,25 +20,28 @@ const Drawer = ({
onClose,
children,
}: PropsWithChildren<DrawerProps>) => {
useBodyScrollLock({ enabled: isOpen });

return (
<DrawerContext.Provider value={{ isOpen, onClose }}>
<Portal id="drawer">
<Portal id="drawer-root">
<div
className={`absolute inset-0 z-10 transform overflow-hidden ease-in-out ${
className={`fixed inset-0 z-10 flex w-screen transform justify-center overflow-hidden ease-in-out ${
isOpen
? 'translate-x-0 scale-x-100 opacity-100 transition-opacity duration-500'
: 'translate-x-full scale-x-0 opacity-0 transition-all delay-100 duration-500'
}`}
>
{/** overlay */}
<div className="absolute h-full w-full" onClick={onClose} />
{/** drawer section */}
<section
className={`duration-400 absolute right-0 flex h-full w-full max-w-[43rem] transform flex-col gap-[2rem] overflow-hidden bg-white p-[2rem] shadow-bookcard transition-all ease-in-out ${
className={`duration-400 ease-out-in relative flex h-full w-full max-w-[43rem] transform flex-col gap-[2rem] overflow-hidden bg-white p-[2rem] shadow-bookcard transition-all ${
isOpen ? 'translate-x-0' : 'translate-x-full'
}`}
>
{isOpen && children}
</section>
{/** overlay */}
<section className="h-full w-full" onClick={onClose}></section>
</div>
</Portal>
</DrawerContext.Provider>
Expand Down
2 changes: 2 additions & 0 deletions src/v1/base/Portal.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import { PropsWithChildren, useEffect, useState } from 'react';
import { createPortal } from 'react-dom';

Expand Down
3 changes: 2 additions & 1 deletion src/v1/comment/CommentDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const CommentDrawer = forwardRef<HTMLTextAreaElement, CommentDrawerProps>(
</Drawer.Header>
<Drawer.Content>
<textarea
className="h-[60vh] w-full resize-none border-none text-md focus:outline-none"
className="h-full w-full resize-none border-none text-md focus:outline-none"
rows={15}
defaultValue={defaultComment}
placeholder={placeholder}
ref={ref}
Expand Down

0 comments on commit f21ada4

Please sign in to comment.