-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: baegofda <[email protected]>
- Loading branch information
Showing
8 changed files
with
3,455 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./useToast"; |
22 changes: 22 additions & 0 deletions
22
packages/travelmakers-design-hooks/src/useToast/stories/useToast.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Meta } from "@storybook/react"; | ||
import { Toaster } from "react-hot-toast"; | ||
import { useToast } from "../useToast"; | ||
|
||
export default { | ||
title: "@travelmakers-design-v2/hooks/useToast", | ||
} as Meta; | ||
|
||
export const Toast = () => { | ||
const { toast, success, error } = useToast(); | ||
|
||
return ( | ||
<> | ||
<div style={{ display: "flex", gap: "4px" }}> | ||
<button onClick={() => toast({ text: "기본 테스트" })}>Toast</button> | ||
<button onClick={() => success({ text: "성공" })}>Success</button> | ||
<button onClick={() => error({ text: "실패" })}>Error</button> | ||
</div> | ||
<Toaster /> | ||
</> | ||
); | ||
}; |
83 changes: 83 additions & 0 deletions
83
packages/travelmakers-design-hooks/src/useToast/useToast.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Icon } from "@travelmakers-design-v2/core"; | ||
import { TmTheme, useTmTheme } from "@travelmakers-design-v2/styles"; | ||
import { Property } from "csstype"; | ||
import _toast from "react-hot-toast"; | ||
import { ToastOptions } from "react-hot-toast"; | ||
import { IconTypes } from "../../../travelmakers-design-core/src/components/Icon/Icon.type"; | ||
|
||
type ToastAlignType = "left" | "center" | "right"; | ||
type ToastIconType = "success" | "error"; | ||
type ToastProps = ToastOptions & { | ||
text: string; | ||
align?: ToastAlignType; | ||
}; | ||
|
||
const TOAST_ALIGN: { [key in ToastAlignType]: Property.JustifyContent } = { | ||
left: "flex-start", | ||
center: "center", | ||
right: "flex-end", | ||
}; | ||
const TOAST_ICON: { [key in ToastIconType]: IconTypes } = { | ||
success: "IcResultSuccess", | ||
error: "IcResultFail", | ||
}; | ||
|
||
const getDefaultOptions = (theme: TmTheme) => { | ||
const { typography, colors, shadows } = theme; | ||
|
||
return { | ||
style: { | ||
...typography.body2, | ||
width: "328px", | ||
padding: "11px 6px", | ||
color: colors.white, | ||
backgroundColor: colors.black30, | ||
boxShadow: shadows.elevation4, | ||
}, | ||
duration: 2000, | ||
}; | ||
}; | ||
const getDefaultStyle = (align: ToastAlignType) => { | ||
return { | ||
display: "flex", | ||
justifyContent: TOAST_ALIGN[align ?? "center"], | ||
width: "100%", | ||
}; | ||
}; | ||
|
||
export const useToast = () => { | ||
const theme = useTmTheme(); | ||
const { spacing } = theme; | ||
|
||
const toastGenerator = ( | ||
{ text, align, ...options }: ToastProps, | ||
iconType?: ToastIconType | ||
) => { | ||
_toast( | ||
<div | ||
style={{ | ||
...getDefaultStyle(align), | ||
padding: "1px 0", | ||
wordBreak: "break-all", | ||
}} | ||
> | ||
{iconType && ( | ||
<Icon | ||
src={TOAST_ICON[iconType]} | ||
width={24} | ||
height={24} | ||
style={{ flexShrink: 0, marginRight: spacing.spacing10 }} | ||
/> | ||
)} | ||
<p style={{ margin: 0, wordBreak: "break-all" }}>{text}</p> | ||
</div>, | ||
{ ...getDefaultOptions(theme), ...options } | ||
); | ||
}; | ||
|
||
const toast = (props: ToastProps) => toastGenerator(props); | ||
const success = (props: ToastProps) => toastGenerator(props, "success"); | ||
const error = (props: ToastProps) => toastGenerator(props, "error"); | ||
|
||
return { toast, success, error }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.