Skip to content

Commit bd9146d

Browse files
committed
feat: initial commit
1 parent 413bcd7 commit bd9146d

File tree

16 files changed

+883
-0
lines changed

16 files changed

+883
-0
lines changed

packages/components/toast/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# @nextui-org/toast
2+
3+
Toast Component helps to provide feedback on user-actions.
4+
5+
> This is an internal utility, not intended for public usage.
6+
7+
## Installation
8+
9+
```sh
10+
yarn add @nextui-org/toast
11+
# or
12+
npm i @nextui-org/toast
13+
```
14+
15+
## Contribution
16+
17+
Yes please! See the
18+
[contributing guidelines](https://github.com/nextui-org/nextui/blob/master/CONTRIBUTING.md)
19+
for details.
20+
21+
## License
22+
23+
This project is licensed under the terms of the
24+
[MIT license](https://github.com/nextui-org/nextui/blob/master/LICENSE).
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"name": "@nextui-org/toast",
3+
"version": "2.0.0",
4+
"description": "adding toast",
5+
"keywords": [
6+
"toast"
7+
],
8+
"author": "Junior Garcia <[email protected]>",
9+
"homepage": "https://nextui.org",
10+
"license": "MIT",
11+
"main": "src/index.ts",
12+
"sideEffects": false,
13+
"files": [
14+
"dist"
15+
],
16+
"publishConfig": {
17+
"access": "public"
18+
},
19+
"repository": {
20+
"type": "git",
21+
"url": "git+https://github.com/nextui-org/nextui.git",
22+
"directory": "packages/components/toast"
23+
},
24+
"bugs": {
25+
"url": "https://github.com/nextui-org/nextui/issues"
26+
},
27+
"scripts": {
28+
"build": "tsup src --dts",
29+
"build:fast": "tsup src",
30+
"dev": "pnpm build:fast --watch",
31+
"clean": "rimraf dist .turbo",
32+
"typecheck": "tsc --noEmit",
33+
"prepack": "clean-package",
34+
"postpack": "clean-package restore"
35+
},
36+
"peerDependencies": {
37+
"@nextui-org/system": ">=2.0.0",
38+
"@nextui-org/theme": ">=2.0.0",
39+
"react": ">=18",
40+
"react-dom": ">=18"
41+
},
42+
"dependencies": {
43+
"@nextui-org/react-utils": "workspace:*",
44+
"@nextui-org/shared-utils": "workspace:*",
45+
"@nextui-org/shared-icons": "workspace:*",
46+
"@react-aria/button": "3.9.5",
47+
"@react-aria/toast": "3.0.0-beta.15",
48+
"@react-aria/utils": "3.24.1",
49+
"@react-stately/toast": "3.0.0-beta.5"
50+
},
51+
"devDependencies": {
52+
"@nextui-org/system": "workspace:*",
53+
"@nextui-org/theme": "workspace:*",
54+
"@nextui-org/button": "workspace:*",
55+
"clean-package": "2.2.0",
56+
"react": "^18.0.0",
57+
"react-dom": "^18.0.0"
58+
},
59+
"clean-package": "../../../clean-package.config.json"
60+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Toast from "./toast";
2+
import {ToastProvider} from "./toast-provider";
3+
4+
// export types
5+
export type {ToastProps} from "./toast";
6+
7+
// export hooks
8+
export {useToast} from "./use-toast";
9+
export {addToast} from "./toast-provider";
10+
11+
// export component
12+
export {Toast};
13+
export {ToastProvider};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {ToastOptions, ToastQueue, useToastQueue} from "@react-stately/toast";
2+
import {ToastVariantProps} from "@nextui-org/theme";
3+
4+
import {ToastRegion} from "./toast-region";
5+
import {ToastType} from "./use-toast";
6+
7+
let globalToastQueue: ToastQueue<ToastType> | null = null;
8+
9+
interface ToastProviderProps {
10+
maxVisibleToasts?: number;
11+
}
12+
13+
export const getToastQueue = (maxVisibleToasts: number) => {
14+
if (!globalToastQueue) {
15+
globalToastQueue = new ToastQueue({
16+
maxVisibleToasts,
17+
});
18+
}
19+
20+
return globalToastQueue;
21+
};
22+
23+
export const ToastProvider = ({maxVisibleToasts = 5}: ToastProviderProps) => {
24+
const toastQueue = useToastQueue(getToastQueue(maxVisibleToasts));
25+
26+
if (toastQueue.visibleToasts.length == 0) {
27+
return null;
28+
}
29+
30+
return <>{<ToastRegion toastQueue={toastQueue} />}</>;
31+
};
32+
33+
export const addToast = ({
34+
title,
35+
description,
36+
priority,
37+
timeout,
38+
...config
39+
}: {
40+
title: string;
41+
description: string;
42+
} & ToastOptions &
43+
ToastVariantProps) => {
44+
if (!globalToastQueue) {
45+
return;
46+
}
47+
48+
const content: ToastType = {
49+
title,
50+
description,
51+
config: config,
52+
};
53+
54+
const options: Partial<ToastOptions> = {
55+
timeout,
56+
priority,
57+
};
58+
59+
globalToastQueue.add(content, options);
60+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {useRef} from "react";
2+
import {useToastRegion, AriaToastRegionProps} from "@react-aria/toast";
3+
import {QueuedToast, ToastState} from "@react-stately/toast";
4+
5+
import Toast from "./toast";
6+
import {ToastType} from "./use-toast";
7+
8+
interface ToastRegionProps<T> extends AriaToastRegionProps {
9+
toastQueue: ToastState<T>;
10+
}
11+
12+
export function ToastRegion<T extends ToastType>({toastQueue, ...props}: ToastRegionProps<T>) {
13+
const ref = useRef(null);
14+
const {regionProps} = useToastRegion(props, toastQueue, ref);
15+
16+
return (
17+
<>
18+
<div
19+
{...regionProps}
20+
ref={ref}
21+
className="fixed bottom-0 left-0 w-screen flex flex-col items-center justify-center"
22+
>
23+
{toastQueue.visibleToasts.map((toast: QueuedToast<ToastType>) => {
24+
return (
25+
<Toast key={toast.key} state={toastQueue} toast={toast} {...toast.content.config} />
26+
);
27+
})}
28+
</div>
29+
</>
30+
);
31+
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {forwardRef} from "@nextui-org/system";
2+
import {Button, ButtonProps} from "@nextui-org/button";
3+
import {CloseIcon} from "@nextui-org/shared-icons";
4+
5+
import {Progress} from "../../../core/react/src";
6+
7+
import {UseToastProps, useToast} from "./use-toast";
8+
9+
export interface ToastProps extends UseToastProps {}
10+
11+
const Toast = forwardRef<"div", ToastProps>((props, ref) => {
12+
const {
13+
Component,
14+
Icon,
15+
domRef,
16+
endContent,
17+
closeProgressBarValue,
18+
getToastProps,
19+
getContentProps,
20+
getTitleProps,
21+
getDescriptionProps,
22+
getProgressBarProps,
23+
getCloseButtonProps,
24+
getIconProps,
25+
} = useToast({
26+
...props,
27+
ref,
28+
});
29+
30+
return (
31+
<Component ref={domRef} {...getToastProps()}>
32+
<main {...getContentProps()}>
33+
<Icon {...getIconProps()} />
34+
<div>
35+
<div {...getTitleProps()}>{props.toast.content.title}</div>
36+
<div {...getDescriptionProps()}>{props.toast.content.description}</div>
37+
</div>
38+
</main>
39+
<Button {...(getCloseButtonProps() as ButtonProps)} isIconOnly variant="bordered">
40+
<CloseIcon />
41+
</Button>
42+
{endContent}
43+
<Progress
44+
{...getProgressBarProps()}
45+
aria-label="toast-close-indicator"
46+
value={closeProgressBarValue}
47+
/>
48+
</Component>
49+
);
50+
});
51+
52+
Toast.displayName = "NextUI.Toast";
53+
54+
export default Toast;

0 commit comments

Comments
 (0)