Skip to content

Commit

Permalink
feat: duration 설정 가능하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
gxxrxn committed Oct 16, 2023
1 parent 901f59e commit c81db46
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/stories/Base/Toast.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ type Story = StoryObj<typeof ToastItem>;
const NormalToast = () => {
const toast = useToast();
const handleButtonClick = () =>
toast.show({ type: 'normal', message: '토스트 메시지에요' });
toast.show({
type: 'normal',
message: '5초 동안 보여지는 토스트 메세지에요.',
duration: 5000,
});

return <Button onClick={handleButtonClick}>토스트 띄우기</Button>;
};
Expand Down
17 changes: 5 additions & 12 deletions src/ui/Base/Toast/ToastProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createContext, PropsWithChildren, useMemo, useState } from 'react';
import { createContext, ReactNode, useMemo, useState } from 'react';

import Portal from '@/ui/Base/Portal';

Expand All @@ -8,10 +8,6 @@ import type { ToastController, ToastOption } from './types';

export const ToastContext = createContext({} as ToastController);

type ToastProviderProps = {
duration?: number;
};

type SlideAnimation = 'slide-in' | 'slide-out' | 'slide-init';

const animations = {
Expand All @@ -20,19 +16,16 @@ const animations = {
'slide-init': 'animate-slide-init',
} as const;

const ToastProvider = ({
duration = 1000,
children,
}: PropsWithChildren<ToastProviderProps>) => {
const ToastProvider = ({ children }: { children?: ReactNode }) => {
const [toast, setToast] = useState<ToastOption | null>(null);
const [animation, setAnimation] = useState<SlideAnimation>('slide-init');

const timer = useTimeout();

const controller = useMemo<ToastController>(
() => ({
show: ({ type, message }) => {
setToast({ type, message });
show: ({ type, message, duration = 1000 }) => {
setToast({ type, message, duration });

setAnimation('slide-init');
setAnimation('slide-in');
Expand All @@ -42,7 +35,7 @@ const ToastProvider = ({
}, duration);
},
}),
[timer, duration]
[timer]
);

return (
Expand Down
1 change: 1 addition & 0 deletions src/ui/Base/Toast/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type ToastType = 'normal' | 'success' | 'error';
export type ToastOption = {
type?: ToastType;
message: string;
duration?: number;
};

export type ToastController = {
Expand Down

0 comments on commit c81db46

Please sign in to comment.