Skip to content

Commit

Permalink
refactor: (#738) Drawer와 Toast의 연관 관계가 없도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilpop8663 committed Nov 14, 2023
1 parent 0bf256b commit d1a526b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 45 deletions.
20 changes: 4 additions & 16 deletions frontend/src/components/common/Drawer/Drawer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,25 @@ const meta: Meta<typeof Drawer> = {
export default meta;

export const LeftSideBar = () => {
const { drawerRef, openDrawer, closeDrawer } = useDrawer('left', 'drawer-category-toast-content');
const { drawerRef, openDrawer, closeDrawer } = useDrawer('left');

return (
<div>
<NarrowMainHeader handleCategoryOpenClick={openDrawer} handleAlarmOpenClick={() => {}} />
<Drawer
toastContentId="drawer-category-toast-content"
width="225px"
handleDrawerClose={closeDrawer}
placement="left"
ref={drawerRef}
>
<Drawer width="225px" handleDrawerClose={closeDrawer} placement="left" ref={drawerRef}>
<Dashboard />
</Drawer>
</div>
);
};

export const RightSideBar = () => {
const { drawerRef, openDrawer, closeDrawer } = useDrawer('right', 'drawer-alarm-toast-content');
const { drawerRef, openDrawer, closeDrawer } = useDrawer('right');

return (
<div>
<NarrowMainHeader handleCategoryOpenClick={openDrawer} handleAlarmOpenClick={() => {}} />
<Drawer
toastContentId="drawer-alarm-toast-content"
width="225px"
handleDrawerClose={closeDrawer}
placement="right"
ref={drawerRef}
>
<Drawer width="225px" handleDrawerClose={closeDrawer} placement="right" ref={drawerRef}>
<Dashboard />
</Drawer>
</div>
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/components/common/Drawer/DrawerToastWrapper/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { HTMLAttributes } from 'react';

import { ToastContentId } from '@type/toast';

import * as S from './style';

interface DrawerToastWrapperProps extends HTMLAttributes<HTMLDivElement> {
id: ToastContentId;
placement: 'left' | 'right';
}

export default function DrawerToastWrapper({ placement, ...rest }: DrawerToastWrapperProps) {
return <S.ToastWrapper {...rest} $placement={placement} />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { styled } from 'styled-components';

export const ToastWrapper = styled.div<{ $placement: 'left' | 'right' }>`
position: absolute;
width: 100vw;
left: ${({ $placement }) => ($placement === 'left' ? '0' : 'auto')};
right: ${({ $placement }) => ($placement === 'right' ? '0' : 'auto')};
`;
6 changes: 1 addition & 5 deletions frontend/src/components/common/Drawer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { ForwardedRef, KeyboardEvent, MouseEvent, PropsWithChildren, forwardRef } from 'react';

import { DrawerToastContentId } from '@type/toast';

import * as S from './style';

interface DrawerProps extends PropsWithChildren {
handleDrawerClose: () => void;
width: string;
placement: 'left' | 'right';
toastContentId: DrawerToastContentId;
}

const ARIA_MESSAGE =
'사용자 정보 및 카테고리 정보가 있는 사이드바가 열렸습니다. 사이드바 닫기 버튼을 누르거나 ESC를 누르면 닫을 수 있습니다.';

export default forwardRef(function Drawer(
{ handleDrawerClose, width, placement, toastContentId, children }: DrawerProps,
{ handleDrawerClose, width, placement, children }: DrawerProps,
ref: ForwardedRef<HTMLDialogElement>
) {
const handleCloseClick = (event: MouseEvent<HTMLDialogElement>) => {
Expand Down Expand Up @@ -51,7 +48,6 @@ export default forwardRef(function Drawer(
onClose={handleCloseClick}
onClick={handleCloseClick}
>
<S.ToastWrapper id={toastContentId} $placement={placement} />
<S.CloseButton onClick={handleDrawerClose}>사이드바 닫기버튼</S.CloseButton>
{children}
</S.Dialog>
Expand Down
7 changes: 0 additions & 7 deletions frontend/src/components/common/Drawer/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,3 @@ export const Dialog = styled.dialog<{
background-color: rgba(0, 0, 0, 0.35);
}
`;

export const ToastWrapper = styled.div<{ $placement: 'left' | 'right' }>`
position: absolute;
width: 100vw;
left: ${({ $placement }) => ($placement === 'left' ? '0' : 'auto')};
right: ${({ $placement }) => ($placement === 'right' ? '0' : 'auto')};
`;
11 changes: 2 additions & 9 deletions frontend/src/hooks/useDrawer.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { useContext, useEffect, useRef } from 'react';
import { useEffect, useRef } from 'react';

import { DrawerToastContentId } from '@type/toast';

import { ToastContext } from './context/toast';

export const useDrawer = (placement: 'left' | 'right', toastElementId: DrawerToastContentId) => {
export const useDrawer = (placement: 'left' | 'right') => {
const drawerRef = useRef<HTMLDialogElement>(null);
const { setElementId } = useContext(ToastContext);

const openDrawer = () => {
if (!drawerRef.current) return;

setElementId(toastElementId);
drawerRef.current.showModal();
drawerRef.current.style.transform = 'translateX(0)';
};
Expand All @@ -21,7 +15,6 @@ export const useDrawer = (placement: 'left' | 'right', toastElementId: DrawerToa

drawerRef.current.style.transform =
placement === 'left' ? 'translateX(-100%)' : 'translateX(100%)';
setElementId('toast-content');

setTimeout(() => {
if (!drawerRef.current) return;
Expand Down
34 changes: 26 additions & 8 deletions frontend/src/pages/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import AddButton from '@components/common/AddButton';
import AppInstallPrompt from '@components/common/AppInstallPrompt';
import Dashboard from '@components/common/Dashboard';
import Drawer from '@components/common/Drawer';
import DrawerToastWrapper from '@components/common/Drawer/DrawerToastWrapper';
import Layout from '@components/common/Layout';
import NarrowMainHeader from '@components/common/NarrowMainHeader';
import Skeleton from '@components/common/Skeleton';
Expand All @@ -38,13 +39,14 @@ export default function HomePage() {
drawerRef: categoryDrawerRdf,
openDrawer: openCategoryDrawer,
closeDrawer: closeCategoryDrawer,
} = useDrawer('left', 'drawer-category-toast-content');
} = useDrawer('left');
const {
drawerRef: alarmDrawerRef,
openDrawer: openAlarmDrawer,
closeDrawer: closeAlarmDrawer,
} = useDrawer('right', 'drawer-alarm-toast-content');
} = useDrawer('right');

const { setElementId } = useContext(ToastContext);
const { isBannerOpen, closeBanner } = useBannerToggle();
const { addMessage } = useContext(ToastContext);
const loggedInfo = useContext(AuthContext).loggedInfo;
Expand All @@ -55,15 +57,31 @@ export default function HomePage() {
if (!loggedInfo.isLoggedIn) return addMessage('알림은 로그인 후 이용할 수 있습니다.');

openAlarmDrawer();
setElementId('drawer-alarm-toast-content');
mutate();
};

const handleAlarmDrawerClose = () => {
closeAlarmDrawer();
setElementId('toast-content');
};

const handleCategoryDrawerOpen = () => {
openCategoryDrawer();
setElementId('drawer-category-toast-content');
};

const handleCategoryDrawerClose = () => {
closeCategoryDrawer();
setElementId('toast-content');
};

return (
<Layout isSidebarVisible={true} isMobileDefaultHeaderVisible={false}>
<S.Container>
<S.HeaderWrapper>
<NarrowMainHeader
handleCategoryOpenClick={openCategoryDrawer}
handleCategoryOpenClick={handleCategoryDrawerOpen}
handleAlarmOpenClick={handleToolTipOpen}
isAlarmActive={isAlarmActive ?? false}
/>
Expand All @@ -79,23 +97,23 @@ export default function HomePage() {
)}
<S.DrawerWrapper>
<Drawer
toastContentId="drawer-category-toast-content"
handleDrawerClose={closeCategoryDrawer}
handleDrawerClose={handleCategoryDrawerClose}
placement="left"
width="225px"
ref={categoryDrawerRdf}
>
<DrawerToastWrapper placement="left" id="drawer-category-toast-content" />
<Dashboard />
</Drawer>
<Drawer
toastContentId="drawer-alarm-toast-content"
handleDrawerClose={closeAlarmDrawer}
handleDrawerClose={handleAlarmDrawerClose}
placement="right"
width="310px"
ref={alarmDrawerRef}
>
<DrawerToastWrapper id="drawer-alarm-toast-content" placement="right" />
{loggedInfo.isLoggedIn && (
<AlarmContainer closeToolTip={closeAlarmDrawer} style={alarmDrawerStyle} />
<AlarmContainer closeToolTip={handleAlarmDrawerClose} style={alarmDrawerStyle} />
)}
</Drawer>
</S.DrawerWrapper>
Expand Down

0 comments on commit d1a526b

Please sign in to comment.