-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
217 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.root { | ||
@apply m-6 | ||
rounded | ||
border | ||
border-neutral-200 | ||
bg-white | ||
px-4 | ||
py-3 | ||
shadow-lg | ||
dark:border-neutral-800 | ||
dark:bg-neutral-900; | ||
} | ||
|
||
.message { | ||
@apply font-medium | ||
text-green-600 | ||
dark:text-white; | ||
} |
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,37 @@ | ||
import { CodeBracketIcon } from '@heroicons/react/24/solid'; | ||
import type { Meta as MetaObj, StoryObj } from '@storybook/react'; | ||
import { FormattedMessage } from 'react-intl'; | ||
|
||
import Notification from './index'; | ||
|
||
type Story = StoryObj<typeof Notification>; | ||
type Meta = MetaObj<typeof Notification>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
open: true, | ||
duration: 5000, | ||
children: 'Copied to clipboard!', | ||
}, | ||
}; | ||
|
||
export const TimedNotification: Story = { | ||
args: { | ||
duration: 5000, | ||
children: 'Copied to clipboard!', | ||
}, | ||
}; | ||
|
||
export const WithJSX: Story = { | ||
args: { | ||
open: true, | ||
children: ( | ||
<div className="flex items-center gap-3"> | ||
<CodeBracketIcon className="h-4 w-4" /> | ||
<FormattedMessage id="components.common.codebox.copied" /> | ||
</div> | ||
), | ||
}, | ||
}; | ||
|
||
export default { component: Notification } as Meta; |
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,34 @@ | ||
import * as ToastPrimitive from '@radix-ui/react-toast'; | ||
import classNames from 'classnames'; | ||
import type { FC } from 'react'; | ||
|
||
import styles from './index.module.css'; | ||
|
||
type NotificationProps = { | ||
open?: boolean; | ||
duration?: number; | ||
onChange?: (value: boolean) => void; | ||
children?: React.ReactNode; | ||
className?: string; | ||
}; | ||
|
||
const Notification: FC<NotificationProps> = ({ | ||
open, | ||
duration = 5000, | ||
onChange, | ||
children, | ||
className, | ||
}: NotificationProps) => ( | ||
<ToastPrimitive.Root | ||
open={open} | ||
duration={duration} | ||
onOpenChange={onChange} | ||
className={classNames(styles.root, className)} | ||
> | ||
<ToastPrimitive.Title className={styles.message}> | ||
{children} | ||
</ToastPrimitive.Title> | ||
</ToastPrimitive.Root> | ||
); | ||
|
||
export default Notification; |
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,9 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { NotificationDispatch } from '@/providers/notificationProvider'; | ||
|
||
export const useNotification = () => { | ||
const dispatch = useContext(NotificationDispatch); | ||
|
||
return dispatch; | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,55 @@ | ||
import * as Toast from '@radix-ui/react-toast'; | ||
import type { | ||
CSSProperties, | ||
Dispatch, | ||
FC, | ||
PropsWithChildren, | ||
SetStateAction, | ||
} from 'react'; | ||
import { createContext, useEffect, useState } from 'react'; | ||
|
||
import Notification from '@/components/Common/Notification'; | ||
|
||
const viewportStyles = { | ||
position: 'absolute', | ||
bottom: 0, | ||
right: 0, | ||
} satisfies CSSProperties; | ||
|
||
type NotificationContextType = { | ||
message: string | JSX.Element; | ||
duration: number; | ||
} | null; | ||
|
||
const NotificationContext = createContext<NotificationContextType>(null); | ||
|
||
export const NotificationDispatch = createContext< | ||
Dispatch<SetStateAction<NotificationContextType>> | ||
>(() => {}); | ||
|
||
export const NotificationProvider: FC<PropsWithChildren> = ({ children }) => { | ||
const [notification, dispatch] = useState<NotificationContextType>(null); | ||
|
||
useEffect(() => { | ||
const timeout = setTimeout(() => dispatch(null), notification?.duration); | ||
|
||
return () => clearTimeout(timeout); | ||
}, [notification]); | ||
|
||
return ( | ||
<NotificationContext.Provider value={notification}> | ||
<NotificationDispatch.Provider value={dispatch}> | ||
<Toast.Provider> | ||
{children} | ||
|
||
{notification && ( | ||
<Notification duration={notification.duration}> | ||
{notification.message} | ||
</Notification> | ||
)} | ||
<Toast.Viewport style={viewportStyles} /> | ||
</Toast.Provider> | ||
</NotificationDispatch.Provider> | ||
</NotificationContext.Provider> | ||
); | ||
}; |