react-strawberry-toast 1.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -0
- package/dist/headless.d.mts +53 -0
- package/dist/headless.d.ts +53 -0
- package/dist/headless.js +1 -0
- package/dist/headless.mjs +1 -0
- package/dist/index.css +1 -0
- package/dist/index.d.mts +85 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.js +2 -0
- package/dist/index.mjs +2 -0
- package/package.json +76 -0
- package/src/components/condition.tsx +27 -0
- package/src/components/if.tsx +27 -0
- package/src/components/toast-container.tsx +153 -0
- package/src/components/toast-default.tsx +96 -0
- package/src/components/toast.tsx +84 -0
- package/src/constants/index.ts +8 -0
- package/src/core/headless-toast.ts +49 -0
- package/src/core/store.ts +34 -0
- package/src/core/toast-handler.ts +108 -0
- package/src/core/toast.ts +98 -0
- package/src/global.d.ts +6 -0
- package/src/headless.ts +3 -0
- package/src/hooks/use-toasts.ts +11 -0
- package/src/index.ts +3 -0
- package/src/styles/index.scss +86 -0
- package/src/types/index.ts +48 -0
- package/src/types/utils.ts +1 -0
- package/src/utils/generate-id.ts +4 -0
- package/src/utils/get-animation.ts +10 -0
- package/src/utils/get-direction.ts +19 -0
- package/src/vite.env.d.ts +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# React-Strawberry-Toast
|
|
2
|
+
|
|
3
|
+
A simple and customizable React toast library
|
|
4
|
+
|
|
5
|
+
## Documentation
|
|
6
|
+
|
|
7
|
+
For more detailed information, please refer to the official [Documentation](https://dkpark10.github.io/react-strawberry-toast/)
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i --save react-strawberry-toast
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add react-strawberry-toast
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm i --save react-strawberry-toast
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```jsx
|
|
26
|
+
import { ToastContainer, toast } from 'react-strawberry-toast';
|
|
27
|
+
import 'react-strawberry-toast/dist/style.css';
|
|
28
|
+
|
|
29
|
+
function App() {
|
|
30
|
+
const click = () => {
|
|
31
|
+
toast('hello strawberry toast');
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<>
|
|
36
|
+
<ToastContainer />
|
|
37
|
+
<button type='button' onClick={click}>click</button>
|
|
38
|
+
</>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## API
|
|
44
|
+
|
|
45
|
+
### <ToastContainer />
|
|
46
|
+
|
|
47
|
+
| Option | Description | Type | Required |
|
|
48
|
+
|:-----------|:-----------|:-----------|:-----------|
|
|
49
|
+
| Position | Global toast's Position | top-left, top-center, top-right, bottom-left, bottom-center, bottom-right | false |
|
|
50
|
+
| ContainerId | ToastContainer's unique id | String | false |
|
|
51
|
+
| Reverse | Direction when toast appear | Boolean | false |
|
|
52
|
+
| Gap | Gap Between toasts | Boolean | false |
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
### toast
|
|
56
|
+
|
|
57
|
+
| Option | Description | Type | Required |
|
|
58
|
+
|:-----------|:-----------|:-----------|:-----------|
|
|
59
|
+
| toastId | Toast's unique Id | String | false |
|
|
60
|
+
| Position | Position per toast | top-left, top-center, top-right, bottom-left, bottom-center, bottom-right | false |
|
|
61
|
+
| ContainerId | ID shown in the Toast Container Unique ID | String | false |
|
|
62
|
+
| PauseOnHover | he Option that sets the timer to stop or not when a hover event occurs | Boolean | false |
|
|
63
|
+
| ToastType | Toast Type | default, custom, success, error, laoding, warn | false |
|
|
64
|
+
| TimeOut | Time for toast to disappear | Number | false |
|
|
65
|
+
| RemoveTimeOut | Time for toast to remove In Toast List | Number | false |
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
MIT
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
type RequiredExcept<T, K extends keyof T> = Required<Omit<T, K>> & Pick<T, K>;
|
|
4
|
+
|
|
5
|
+
type Position = 'bottom-left' | 'bottom-center' | 'bottom-right' | 'top-left' | 'top-center' | 'top-right';
|
|
6
|
+
type ToastType = 'default' | 'custom' | 'success' | 'error' | 'loading' | 'warn';
|
|
7
|
+
interface BaseOptions {
|
|
8
|
+
toastId?: string;
|
|
9
|
+
timeOut?: number;
|
|
10
|
+
removeTimeOut?: number;
|
|
11
|
+
}
|
|
12
|
+
interface ToastBaseState {
|
|
13
|
+
toastId: string;
|
|
14
|
+
data: ReactNode;
|
|
15
|
+
isVisible: boolean;
|
|
16
|
+
createdAt: number;
|
|
17
|
+
pausedAt?: number;
|
|
18
|
+
updated?: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface Options extends BaseOptions {
|
|
21
|
+
position?: Position;
|
|
22
|
+
containerId?: string;
|
|
23
|
+
pauseOnHover?: boolean;
|
|
24
|
+
toastType?: ToastType;
|
|
25
|
+
}
|
|
26
|
+
interface ToastDataCallback {
|
|
27
|
+
close: () => void;
|
|
28
|
+
immediatelyClose: () => void;
|
|
29
|
+
isVisible: boolean;
|
|
30
|
+
icons: Record<Exclude<ToastType, 'default' | 'custom'>, ReactNode>;
|
|
31
|
+
}
|
|
32
|
+
type ToastState = ToastBaseState & BaseOptions;
|
|
33
|
+
type ToastDataWithCallback = (props: ToastDataCallback) => ReactNode;
|
|
34
|
+
type NonHeadlessToastState = RequiredExcept<Options, 'containerId' | 'position'> & Omit<ToastState, 'data'> & {
|
|
35
|
+
data: ToastDataWithCallback | ReactNode;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
declare const useToasts: () => Array<ToastState>;
|
|
39
|
+
|
|
40
|
+
declare const toast: {
|
|
41
|
+
(data: ToastState["data"], options?: BaseOptions): string;
|
|
42
|
+
setActive: (toastId: ToastState["toastId"]) => void;
|
|
43
|
+
isActive: (toastId: ToastState["toastId"]) => boolean;
|
|
44
|
+
disappear: (toastId: ToastState["toastId"], timeOut: number) => void;
|
|
45
|
+
resume: (toastId: ToastState["toastId"]) => void;
|
|
46
|
+
pause: (toastId: ToastState["toastId"]) => void;
|
|
47
|
+
replace: (toastId: ToastState["toastId"], data: ToastState["data"], options?: (Options & {
|
|
48
|
+
toastType: NonHeadlessToastState["toastType"];
|
|
49
|
+
}) | undefined) => void;
|
|
50
|
+
remove: (toastId: ToastState["toastId"], timeOut?: number) => void;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export { type BaseOptions, type ToastState, toast, useToasts };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
type RequiredExcept<T, K extends keyof T> = Required<Omit<T, K>> & Pick<T, K>;
|
|
4
|
+
|
|
5
|
+
type Position = 'bottom-left' | 'bottom-center' | 'bottom-right' | 'top-left' | 'top-center' | 'top-right';
|
|
6
|
+
type ToastType = 'default' | 'custom' | 'success' | 'error' | 'loading' | 'warn';
|
|
7
|
+
interface BaseOptions {
|
|
8
|
+
toastId?: string;
|
|
9
|
+
timeOut?: number;
|
|
10
|
+
removeTimeOut?: number;
|
|
11
|
+
}
|
|
12
|
+
interface ToastBaseState {
|
|
13
|
+
toastId: string;
|
|
14
|
+
data: ReactNode;
|
|
15
|
+
isVisible: boolean;
|
|
16
|
+
createdAt: number;
|
|
17
|
+
pausedAt?: number;
|
|
18
|
+
updated?: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface Options extends BaseOptions {
|
|
21
|
+
position?: Position;
|
|
22
|
+
containerId?: string;
|
|
23
|
+
pauseOnHover?: boolean;
|
|
24
|
+
toastType?: ToastType;
|
|
25
|
+
}
|
|
26
|
+
interface ToastDataCallback {
|
|
27
|
+
close: () => void;
|
|
28
|
+
immediatelyClose: () => void;
|
|
29
|
+
isVisible: boolean;
|
|
30
|
+
icons: Record<Exclude<ToastType, 'default' | 'custom'>, ReactNode>;
|
|
31
|
+
}
|
|
32
|
+
type ToastState = ToastBaseState & BaseOptions;
|
|
33
|
+
type ToastDataWithCallback = (props: ToastDataCallback) => ReactNode;
|
|
34
|
+
type NonHeadlessToastState = RequiredExcept<Options, 'containerId' | 'position'> & Omit<ToastState, 'data'> & {
|
|
35
|
+
data: ToastDataWithCallback | ReactNode;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
declare const useToasts: () => Array<ToastState>;
|
|
39
|
+
|
|
40
|
+
declare const toast: {
|
|
41
|
+
(data: ToastState["data"], options?: BaseOptions): string;
|
|
42
|
+
setActive: (toastId: ToastState["toastId"]) => void;
|
|
43
|
+
isActive: (toastId: ToastState["toastId"]) => boolean;
|
|
44
|
+
disappear: (toastId: ToastState["toastId"], timeOut: number) => void;
|
|
45
|
+
resume: (toastId: ToastState["toastId"]) => void;
|
|
46
|
+
pause: (toastId: ToastState["toastId"]) => void;
|
|
47
|
+
replace: (toastId: ToastState["toastId"], data: ToastState["data"], options?: (Options & {
|
|
48
|
+
toastType: NonHeadlessToastState["toastType"];
|
|
49
|
+
}) | undefined) => void;
|
|
50
|
+
remove: (toastId: ToastState["toastId"], timeOut?: number) => void;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export { type BaseOptions, type ToastState, toast, useToasts };
|
package/dist/headless.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var I=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var _=Object.prototype.hasOwnProperty;var g=(t,e)=>{for(var o in e)I(t,o,{get:e[o],enumerable:!0})},w=(t,e,o,c)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of x(e))!_.call(t,n)&&n!==o&&I(t,n,{get:()=>e[n],enumerable:!(c=b(e,n))||c.enumerable});return t};var U=t=>w(I({},"__esModule",{value:!0}),t);var H={};g(H,{toast:()=>T,useToasts:()=>R});module.exports=U(H);var h=require("react");var y=()=>{let t=0;return()=>String(t++)};var l=class{state=[];listeners=new Set;activatedToasts=new Set;toastTimers=new Map;constructor(){}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}setState(e){this.state=typeof e=="function"?e(this.state):e,this.listeners.forEach(o=>o())}getSnapShot(){return this.state}};var M=t=>{let e=s=>{let a=t.toastTimers.get(s);clearTimeout(a),t.toastTimers.delete(s)},o=s=>{t.activatedToasts.add(s)},c=s=>t.activatedToasts.has(s),n=(s,a=200)=>{setTimeout(()=>{t.state=t.state.filter(r=>r.toastId!==s),t.setState([...t.state])},a),e(s)},d=(s,a,r)=>{t.state=t.state.map(p=>p.toastId===s?{...p,...r,updated:!0,data:a}:p),t.setState([...t.state])},A=s=>{let a=new Date().getTime();t.state=t.state.map(r=>r.toastId===s?{...r,pausedAt:a}:r),e(s),t.setState([...t.state])},f=(s,a)=>{let r=setTimeout(()=>{var O;t.state=t.state.map(u=>u.toastId===s?{...u,isVisible:!1}:u),t.setState([...t.state]);let p=(O=t.state.find(u=>u.toastId===s))==null?void 0:O.removeTimeOut;n(s,p)},a>2147483647?2147483647:a);t.toastTimers.set(s,r)};return{setActive:o,isActive:c,disappear:f,resume:s=>{if(t.toastTimers.has(s))return;let a=t.state.find(p=>p.toastId===s);if(!a)return;let r=a.createdAt+(a.timeOut||3e3)-(a.pausedAt||0);f(s,r)},pause:A,replace:d,remove:n}};var i=new l,P=y(),D=()=>(t,e={})=>{let{timeOut:o=3e3,removeTimeOut:c=200,toastId:n}=e,d=n||P();if(T.isActive(d))throw new Error("A duplicate custom ID is not available.");let A=new Date().getTime(),f={...e,timeOut:o>2147483647?2147483647:o,toastId:d,data:t,createdAt:A,removeTimeOut:c,isVisible:!0};return i.state.push(f),i.setState([...i.state]),d},T=(t,e={})=>D()(t,e),m=M(i);T.setActive=m.setActive;T.isActive=m.isActive;T.disappear=m.disappear;T.resume=m.resume;T.pause=m.pause;T.replace=m.replace;T.remove=m.remove;var R=()=>(0,h.useSyncExternalStore)(i.subscribe.bind(i),i.getSnapShot.bind(i),i.getSnapShot.bind(i));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{useSyncExternalStore as b}from"react";var O=()=>{let t=0;return()=>String(t++)};var f=class{state=[];listeners=new Set;activatedToasts=new Set;toastTimers=new Map;constructor(){}subscribe(a){return this.listeners.add(a),()=>{this.listeners.delete(a)}}setState(a){this.state=typeof a=="function"?a(this.state):a,this.listeners.forEach(p=>p())}getSnapShot(){return this.state}};var y=t=>{let a=e=>{let s=t.toastTimers.get(e);clearTimeout(s),t.toastTimers.delete(e)},p=e=>{t.activatedToasts.add(e)},l=e=>t.activatedToasts.has(e),u=(e,s=200)=>{setTimeout(()=>{t.state=t.state.filter(o=>o.toastId!==e),t.setState([...t.state])},s),a(e)},m=(e,s,o)=>{t.state=t.state.map(n=>n.toastId===e?{...n,...o,updated:!0,data:s}:n),t.setState([...t.state])},A=e=>{let s=new Date().getTime();t.state=t.state.map(o=>o.toastId===e?{...o,pausedAt:s}:o),a(e),t.setState([...t.state])},S=(e,s)=>{let o=setTimeout(()=>{var E;t.state=t.state.map(c=>c.toastId===e?{...c,isVisible:!1}:c),t.setState([...t.state]);let n=(E=t.state.find(c=>c.toastId===e))==null?void 0:E.removeTimeOut;u(e,n)},s>2147483647?2147483647:s);t.toastTimers.set(e,o)};return{setActive:p,isActive:l,disappear:S,resume:e=>{if(t.toastTimers.has(e))return;let s=t.state.find(n=>n.toastId===e);if(!s)return;let o=s.createdAt+(s.timeOut||3e3)-(s.pausedAt||0);S(e,o)},pause:A,replace:m,remove:u}};var r=new f,M=O(),h=()=>(t,a={})=>{let{timeOut:p=3e3,removeTimeOut:l=200,toastId:u}=a,m=u||M();if(i.isActive(m))throw new Error("A duplicate custom ID is not available.");let A=new Date().getTime(),S={...a,timeOut:p>2147483647?2147483647:p,toastId:m,data:t,createdAt:A,removeTimeOut:l,isVisible:!0};return r.state.push(S),r.setState([...r.state]),m},i=(t,a={})=>h()(t,a),T=y(r);i.setActive=T.setActive;i.isActive=T.isActive;i.disappear=T.disappear;i.resume=T.resume;i.pause=T.pause;i.replace=T.replace;i.remove=T.remove;var X=()=>b(r.subscribe.bind(r),r.getSnapShot.bind(r),r.getSnapShot.bind(r));export{i as toast,X as useToasts};
|
package/dist/index.css
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@keyframes l3{to{transform:rotate(1turn)}}@keyframes react-strawberry-toast_fade-in{0%{transform:translateY(-100%);opacity:0}to{transform:translateY(0);opacity:1}}@keyframes react-strawberry-toast_fade-out{0%{transform:translateY(0);opacity:1}to{transform:translateY(-100%);opacity:0}}@keyframes react-strawberry-toast_fade-in-reverse{0%{transform:translateY(100%);opacity:0}to{transform:translateY(0);opacity:1}}@keyframes react-strawberry-toast_fade-out-reverse{0%{transform:translateY(0);opacity:1}to{transform:translateY(100%);opacity:0}}.react-strawberry-toast__fade-in{animation:react-strawberry-toast_fade-in .3s ease-out}.react-strawberry-toast__fade-out{animation:react-strawberry-toast_fade-out .3s ease-out}.react-strawberry-toast__fade-in-reverse{animation:react-strawberry-toast_fade-in-reverse .3s ease-out}.react-strawberry-toast__fade-out-reverse{animation:react-strawberry-toast_fade-out-reverse .3s ease-out}.react-strawberry-toast__loading{width:15px;padding:3px;aspect-ratio:1;border-radius:50%;background:#6b6875;--_m: conic-gradient(#0000 10%, #000), linear-gradient(#000 0 0) content-box;-webkit-mask:var(--_m);mask:var(--_m);-webkit-mask-composite:source-out;mask-composite:subtract;animation:l3 1s infinite linear}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
type RequiredExcept<T, K extends keyof T> = Required<Omit<T, K>> & Pick<T, K>;
|
|
4
|
+
|
|
5
|
+
type Position = 'bottom-left' | 'bottom-center' | 'bottom-right' | 'top-left' | 'top-center' | 'top-right';
|
|
6
|
+
type ToastType = 'default' | 'custom' | 'success' | 'error' | 'loading' | 'warn';
|
|
7
|
+
interface BaseOptions {
|
|
8
|
+
toastId?: string;
|
|
9
|
+
timeOut?: number;
|
|
10
|
+
removeTimeOut?: number;
|
|
11
|
+
}
|
|
12
|
+
interface ToastBaseState {
|
|
13
|
+
toastId: string;
|
|
14
|
+
data: ReactNode;
|
|
15
|
+
isVisible: boolean;
|
|
16
|
+
createdAt: number;
|
|
17
|
+
pausedAt?: number;
|
|
18
|
+
updated?: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface Options extends BaseOptions {
|
|
21
|
+
position?: Position;
|
|
22
|
+
containerId?: string;
|
|
23
|
+
pauseOnHover?: boolean;
|
|
24
|
+
toastType?: ToastType;
|
|
25
|
+
}
|
|
26
|
+
interface ToastDataCallback {
|
|
27
|
+
close: () => void;
|
|
28
|
+
immediatelyClose: () => void;
|
|
29
|
+
isVisible: boolean;
|
|
30
|
+
icons: Record<Exclude<ToastType, 'default' | 'custom'>, ReactNode>;
|
|
31
|
+
}
|
|
32
|
+
type ToastState = ToastBaseState & BaseOptions;
|
|
33
|
+
type ToastDataWithCallback = (props: ToastDataCallback) => ReactNode;
|
|
34
|
+
type NonHeadlessToastState = RequiredExcept<Options, 'containerId' | 'position'> & Omit<ToastState, 'data'> & {
|
|
35
|
+
data: ToastDataWithCallback | ReactNode;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
type Listener = () => void;
|
|
39
|
+
declare class ToastStore<T = ToastState> {
|
|
40
|
+
state: Array<T>;
|
|
41
|
+
listeners: Set<Listener>;
|
|
42
|
+
/** @description list containing toast activated since mount */
|
|
43
|
+
activatedToasts: Set<string>;
|
|
44
|
+
/** @description list with a timer for toast */
|
|
45
|
+
toastTimers: Map<string, number>;
|
|
46
|
+
constructor();
|
|
47
|
+
subscribe(listener: Listener): Listener;
|
|
48
|
+
/** @description must put a new memory value in the nextState. */
|
|
49
|
+
setState(nextState: Array<T> | ((state: Array<T>) => Array<T>)): void;
|
|
50
|
+
getSnapShot(): Array<T>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
declare const toastStore: ToastStore<NonHeadlessToastState>;
|
|
54
|
+
declare const toast: {
|
|
55
|
+
(data: ToastState["data"] | ToastDataWithCallback, options?: Options): string;
|
|
56
|
+
setActive: (toastId: ToastState["toastId"]) => void;
|
|
57
|
+
isActive: (toastId: ToastState["toastId"]) => boolean;
|
|
58
|
+
disappear: (toastId: ToastState["toastId"], timeOut: number) => void;
|
|
59
|
+
resume: (toastId: ToastState["toastId"]) => void;
|
|
60
|
+
pause: (toastId: ToastState["toastId"]) => void;
|
|
61
|
+
replace: (toastId: ToastState["toastId"], data: ToastState["data"], options?: (Options & {
|
|
62
|
+
toastType: NonHeadlessToastState["toastType"];
|
|
63
|
+
}) | undefined) => void;
|
|
64
|
+
remove: (toastId: ToastState["toastId"], timeOut?: number) => void;
|
|
65
|
+
success: (data: ReactNode, options?: Options) => ToastState["toastId"];
|
|
66
|
+
error: (data: ReactNode, options?: Options) => ToastState["toastId"];
|
|
67
|
+
warn: (data: ReactNode, options?: Options) => ToastState["toastId"];
|
|
68
|
+
loading: (data: ReactNode, options?: Options) => ToastState["toastId"];
|
|
69
|
+
custom: (data: ReactNode | ToastDataWithCallback, options?: Options) => ToastState["toastId"];
|
|
70
|
+
promise(promise: Promise<any>, promiseOption: {
|
|
71
|
+
loading: ReactNode;
|
|
72
|
+
success: ReactNode;
|
|
73
|
+
error: ReactNode;
|
|
74
|
+
}, options?: Options): void;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
interface ToastContainerProps {
|
|
78
|
+
position?: Position;
|
|
79
|
+
containerId?: string;
|
|
80
|
+
reverse?: boolean;
|
|
81
|
+
gap?: number;
|
|
82
|
+
}
|
|
83
|
+
declare function ToastContainer({ position: globalPosition, containerId, gap, reverse, }: ToastContainerProps): React.JSX.Element;
|
|
84
|
+
|
|
85
|
+
export { type BaseOptions, type NonHeadlessToastState, type Options, type Position, type ToastBaseState, ToastContainer, type ToastDataWithCallback, type ToastState, type ToastType, toast, toastStore };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
type RequiredExcept<T, K extends keyof T> = Required<Omit<T, K>> & Pick<T, K>;
|
|
4
|
+
|
|
5
|
+
type Position = 'bottom-left' | 'bottom-center' | 'bottom-right' | 'top-left' | 'top-center' | 'top-right';
|
|
6
|
+
type ToastType = 'default' | 'custom' | 'success' | 'error' | 'loading' | 'warn';
|
|
7
|
+
interface BaseOptions {
|
|
8
|
+
toastId?: string;
|
|
9
|
+
timeOut?: number;
|
|
10
|
+
removeTimeOut?: number;
|
|
11
|
+
}
|
|
12
|
+
interface ToastBaseState {
|
|
13
|
+
toastId: string;
|
|
14
|
+
data: ReactNode;
|
|
15
|
+
isVisible: boolean;
|
|
16
|
+
createdAt: number;
|
|
17
|
+
pausedAt?: number;
|
|
18
|
+
updated?: boolean;
|
|
19
|
+
}
|
|
20
|
+
interface Options extends BaseOptions {
|
|
21
|
+
position?: Position;
|
|
22
|
+
containerId?: string;
|
|
23
|
+
pauseOnHover?: boolean;
|
|
24
|
+
toastType?: ToastType;
|
|
25
|
+
}
|
|
26
|
+
interface ToastDataCallback {
|
|
27
|
+
close: () => void;
|
|
28
|
+
immediatelyClose: () => void;
|
|
29
|
+
isVisible: boolean;
|
|
30
|
+
icons: Record<Exclude<ToastType, 'default' | 'custom'>, ReactNode>;
|
|
31
|
+
}
|
|
32
|
+
type ToastState = ToastBaseState & BaseOptions;
|
|
33
|
+
type ToastDataWithCallback = (props: ToastDataCallback) => ReactNode;
|
|
34
|
+
type NonHeadlessToastState = RequiredExcept<Options, 'containerId' | 'position'> & Omit<ToastState, 'data'> & {
|
|
35
|
+
data: ToastDataWithCallback | ReactNode;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
type Listener = () => void;
|
|
39
|
+
declare class ToastStore<T = ToastState> {
|
|
40
|
+
state: Array<T>;
|
|
41
|
+
listeners: Set<Listener>;
|
|
42
|
+
/** @description list containing toast activated since mount */
|
|
43
|
+
activatedToasts: Set<string>;
|
|
44
|
+
/** @description list with a timer for toast */
|
|
45
|
+
toastTimers: Map<string, number>;
|
|
46
|
+
constructor();
|
|
47
|
+
subscribe(listener: Listener): Listener;
|
|
48
|
+
/** @description must put a new memory value in the nextState. */
|
|
49
|
+
setState(nextState: Array<T> | ((state: Array<T>) => Array<T>)): void;
|
|
50
|
+
getSnapShot(): Array<T>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
declare const toastStore: ToastStore<NonHeadlessToastState>;
|
|
54
|
+
declare const toast: {
|
|
55
|
+
(data: ToastState["data"] | ToastDataWithCallback, options?: Options): string;
|
|
56
|
+
setActive: (toastId: ToastState["toastId"]) => void;
|
|
57
|
+
isActive: (toastId: ToastState["toastId"]) => boolean;
|
|
58
|
+
disappear: (toastId: ToastState["toastId"], timeOut: number) => void;
|
|
59
|
+
resume: (toastId: ToastState["toastId"]) => void;
|
|
60
|
+
pause: (toastId: ToastState["toastId"]) => void;
|
|
61
|
+
replace: (toastId: ToastState["toastId"], data: ToastState["data"], options?: (Options & {
|
|
62
|
+
toastType: NonHeadlessToastState["toastType"];
|
|
63
|
+
}) | undefined) => void;
|
|
64
|
+
remove: (toastId: ToastState["toastId"], timeOut?: number) => void;
|
|
65
|
+
success: (data: ReactNode, options?: Options) => ToastState["toastId"];
|
|
66
|
+
error: (data: ReactNode, options?: Options) => ToastState["toastId"];
|
|
67
|
+
warn: (data: ReactNode, options?: Options) => ToastState["toastId"];
|
|
68
|
+
loading: (data: ReactNode, options?: Options) => ToastState["toastId"];
|
|
69
|
+
custom: (data: ReactNode | ToastDataWithCallback, options?: Options) => ToastState["toastId"];
|
|
70
|
+
promise(promise: Promise<any>, promiseOption: {
|
|
71
|
+
loading: ReactNode;
|
|
72
|
+
success: ReactNode;
|
|
73
|
+
error: ReactNode;
|
|
74
|
+
}, options?: Options): void;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
interface ToastContainerProps {
|
|
78
|
+
position?: Position;
|
|
79
|
+
containerId?: string;
|
|
80
|
+
reverse?: boolean;
|
|
81
|
+
gap?: number;
|
|
82
|
+
}
|
|
83
|
+
declare function ToastContainer({ position: globalPosition, containerId, gap, reverse, }: ToastContainerProps): React.JSX.Element;
|
|
84
|
+
|
|
85
|
+
export { type BaseOptions, type NonHeadlessToastState, type Options, type Position, type ToastBaseState, ToastContainer, type ToastDataWithCallback, type ToastState, type ToastType, toast, toastStore };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";var $=Object.create;var _=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var F=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty;var q=(t,e)=>{for(var o in e)_(t,o,{get:e[o],enumerable:!0})},P=(t,e,o,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let d of j(e))!G.call(t,d)&&d!==o&&_(t,d,{get:()=>e[d],enumerable:!(i=B(e,d))||i.enumerable});return t};var M=(t,e,o)=>(o=t!=null?$(F(t)):{},P(e||!t||!t.__esModule?_(o,"default",{value:t,enumerable:!0}):o,t)),J=t=>P(_({},"__esModule",{value:!0}),t);var ot={};q(ot,{ToastContainer:()=>et,toast:()=>s,toastStore:()=>l});module.exports=J(ot);var H=()=>{let t=0;return()=>String(t++)};var A=class{state=[];listeners=new Set;activatedToasts=new Set;toastTimers=new Map;constructor(){}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}setState(e){this.state=typeof e=="function"?e(this.state):e,this.listeners.forEach(o=>o())}getSnapShot(){return this.state}};var g="react-strawberry-toast";var z=t=>{let e=a=>{let n=t.toastTimers.get(a);clearTimeout(n),t.toastTimers.delete(a)},o=a=>{t.activatedToasts.add(a)},i=a=>t.activatedToasts.has(a),d=(a,n=200)=>{setTimeout(()=>{t.state=t.state.filter(f=>f.toastId!==a),t.setState([...t.state])},n),e(a)},y=(a,n,f)=>{t.state=t.state.map(b=>b.toastId===a?{...b,...f,updated:!0,data:n}:b),t.setState([...t.state])},r=a=>{let n=new Date().getTime();t.state=t.state.map(f=>f.toastId===a?{...f,pausedAt:n}:f),e(a),t.setState([...t.state])},c=(a,n)=>{let f=setTimeout(()=>{var W;t.state=t.state.map(w=>w.toastId===a?{...w,isVisible:!1}:w),t.setState([...t.state]);let b=(W=t.state.find(w=>w.toastId===a))==null?void 0:W.removeTimeOut;d(a,b)},n>2147483647?2147483647:n);t.toastTimers.set(a,f)};return{setActive:o,isActive:i,disappear:c,resume:a=>{if(t.toastTimers.has(a))return;let n=t.state.find(b=>b.toastId===a);if(!n)return;let f=n.createdAt+(n.timeOut||3e3)-(n.pausedAt||0);c(a,f)},pause:r,replace:y,remove:d}};var l=new A,K=H(),S=(t="default")=>(e,o={})=>{let{timeOut:i=3e3,removeTimeOut:d=200,pauseOnHover:y=!0,toastId:r}=o,c=r||K();if(s.isActive(c))throw new Error("A duplicate custom ID is not available.");let v=new Date().getTime(),a={...o,timeOut:i>2147483647?2147483647:i,toastId:c,data:e,createdAt:v,toastType:t,pauseOnHover:y,removeTimeOut:d,isVisible:!0};return l.state.push(a),l.setState([...l.state]),c},s=(t,e={})=>S()(t,e),I=z(l);s.setActive=I.setActive;s.isActive=I.isActive;s.disappear=I.disappear;s.resume=I.resume;s.pause=I.pause;s.replace=I.replace;s.remove=I.remove;s.success=S("success");s.error=S("error");s.warn=S("warn");s.loading=S("loading");s.custom=S("custom");s.promise=(t,e,o={})=>{let{loading:i,success:d,error:y}=e,r=s.loading(i,{...o,timeOut:2147483647});t.then(()=>{s.replace(r,d,{...o,toastType:"success"})}).catch(()=>{s.replace(r,y,{...o,toastType:"error"})})};var T=M(require("react")),X=require("react");var p=M(require("react"));var L=require("react");function O({condition:t,children:e}){let o=L.Children.toArray(e);if(o.length>2)throw new Error("The number of children exceeds two.");return o.find(i=>t?i.key===".0":i.key===".1")}function k({children:t}){return t}function C({children:t}){return t}var V=({isVisible:t,position:e})=>t?/top/i.test(e)?`${g}__fade-in`:`${g}__fade-in-reverse`:/bottom/i.test(e)?`${g}__fade-out-reverse`:`${g}__fade-out`;var u=M(require("react"));function Q(){return u.default.createElement("svg",{stroke:"none",fill:"none",strokeWidth:"2",viewBox:"0 0 24 24",strokeLinecap:"round",strokeLinejoin:"round",height:"22",width:"22",xmlns:"http://www.w3.org/2000/svg"},u.default.createElement("path",{stroke:"none",d:"M0 0h24v24H0z"}),u.default.createElement("path",{d:"M17 3.34a10 10 0 1 1 -14.995 8.984l-.005 -.324l.005 -.324a10 10 0 0 1 14.995 -8.336zm-1.293 5.953a1 1 0 0 0 -1.32 -.083l-.094 .083l-3.293 3.292l-1.293 -1.292l-.094 -.083a1 1 0 0 0 -1.403 1.403l.083 .094l2 2l.094 .083a1 1 0 0 0 1.226 0l.094 -.083l4 -4l.083 -.094a1 1 0 0 0 -.083 -1.32z",strokeWidth:"0",fill:"#1ab173"}))}function Z(){return u.default.createElement("svg",{stroke:"none",fill:"#eb2639",strokeWidth:"2",viewBox:"0 0 24 24",height:"22",width:"22",xmlns:"http://www.w3.org/2000/svg"},u.default.createElement("path",{d:"M11.953 2C6.465 2 2 6.486 2 12s4.486 10 10 10 10-4.486 10-10S17.493 2 11.953 2zM13 17h-2v-2h2v2zm0-4h-2V7h2v6z"}))}function R(){return u.default.createElement("svg",{stroke:"none",fill:"#fcba03",strokeWidth:"0",viewBox:"0 0 1024 1024",height:"22",width:"22",xmlns:"http://www.w3.org/2000/svg"},u.default.createElement("path",{d:"M955.7 856l-416-720c-6.2-10.7-16.9-16-27.7-16s-21.6 5.3-27.7 16l-416 720C56 877.4 71.4 904 96 904h832c24.6 0 40-26.6 27.7-48zM480 416c0-4.4 3.6-8 8-8h48c4.4 0 8 3.6 8 8v184c0 4.4-3.6 8-8 8h-48c-4.4 0-8-3.6-8-8V416zm32 352a48.01 48.01 0 0 1 0-96 48.01 48.01 0 0 1 0 96z"}))}var x={success:Q,error:Z,loading:()=>u.default.createElement("div",{className:`${g}__loading`}),warn:R};function U({status:t,children:e}){let o=t==="custom"||t==="default"?null:x[t];return u.default.createElement("div",{style:{boxSizing:"border-box",backgroundColor:"white",padding:"12px 14px 12px 12px",display:"flex",alignItems:"center",gap:5,borderRadius:8,boxShadow:"2px 4px 10px rgba(0, 0, 0, 0.1)"}},o&&u.default.createElement("span",{style:{minWidth:20,maxWidth:20}},u.default.createElement(o,null)),e)}function N({toastProps:t,...e}){let o=V({isVisible:t.isVisible,position:t.position}),i=typeof t.data=="function"?t.data({close:()=>s.disappear(t.toastId,0),immediatelyClose:()=>{s.disappear(t.toastId,0),s.remove(t.toastId,0)},icons:{success:p.default.createElement(x.success,null),error:p.default.createElement(x.error,null),warn:p.default.createElement(x.warn,null),loading:p.default.createElement(x.loading,null)},isVisible:t.isVisible}):t.data,d=()=>{t.pauseOnHover&&s.pause(t.toastId)},y=()=>{t.pauseOnHover&&s.resume(t.toastId)};return(0,p.useEffect)(()=>{s.isActive(t.toastId)||(s.setActive(t.toastId),s.disappear(t.toastId,t.timeOut))},[t.toastId]),(0,p.useEffect)(()=>{if(t.updated!==void 0){let r=t.timeOut>=2147483647?3e3:t.timeOut;s.disappear(t.toastId,r)}},[t.updated]),p.default.createElement("div",{role:"alert","data-testid":`container-${t.containerId}`,className:t.toastType==="custom"?"":o,onMouseEnter:d,onMouseLeave:y,...e},p.default.createElement(O,{condition:t.toastType!=="custom"},p.default.createElement(k,null,p.default.createElement(U,{status:t.toastType},i)),p.default.createElement(C,null,i)))}var Y=({position:t,reverse:e=!1})=>/top/i.test(t)?e?"column-reverse":"column":/bottom/i.test(t)?e?"column":"column-reverse":"column";var m=16,tt={"top-left":{top:m,left:m},"top-center":{top:m,left:"50%",transform:"translateX(-50%)"},"top-right":{top:m,right:m},"bottom-left":{bottom:m,left:m},"bottom-center":{bottom:m,left:"50%",transform:"translateX(-50%)"},"bottom-right":{bottom:m,right:m}};function et({position:t="top-center",containerId:e="",gap:o=9,reverse:i=!1}){let d=(0,X.useSyncExternalStore)(l.subscribe.bind(l),l.getSnapShot.bind(l),l.getSnapShot.bind(l)),y=d.filter(r=>r.containerId===void 0).reduce((r,c)=>{let v=c.position||t;return c.position=v,r[v]=r[v]||[],r[v].push(c),r},{});return T.default.createElement(O,{condition:!!e},T.default.createElement(k,null,T.default.createElement("div",{style:{position:"absolute",zIndex:9999,pointerEvents:"none"}},T.default.createElement("div",{style:{pointerEvents:"auto",display:"flex",flexDirection:i?"column-reverse":"column",gap:o}},d.filter(r=>r.containerId===e).map(r=>T.default.createElement(N,{key:r.toastId,toastProps:r,style:{display:"flex"}}))))),T.default.createElement(C,null,T.default.createElement("div",{style:{position:"fixed",zIndex:9999,top:m,left:m,right:m,bottom:m,pointerEvents:"none"}},Object.entries(y).map(([r,c])=>{let v=tt[r],a=Y({position:r,reverse:i});return T.default.createElement("div",{key:r,"data-testid":r,style:{pointerEvents:"auto",position:"fixed",display:"flex",flexDirection:a,gap:o,...v}},c.map(n=>T.default.createElement(N,{key:n.toastId,toastProps:n,style:{display:"flex",justifyContent:"center"}})))}))))}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
var N=()=>{let t=0;return()=>String(t++)};var _=class{state=[];listeners=new Set;activatedToasts=new Set;toastTimers=new Map;constructor(){}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}setState(e){this.state=typeof e=="function"?e(this.state):e,this.listeners.forEach(r=>r())}getSnapShot(){return this.state}};var g="react-strawberry-toast";var W=t=>{let e=o=>{let i=t.toastTimers.get(o);clearTimeout(i),t.toastTimers.delete(o)},r=o=>{t.activatedToasts.add(o)},n=o=>t.activatedToasts.has(o),p=(o,i=200)=>{setTimeout(()=>{t.state=t.state.filter(c=>c.toastId!==o),t.setState([...t.state])},i),e(o)},f=(o,i,c)=>{t.state=t.state.map(b=>b.toastId===o?{...b,...c,updated:!0,data:i}:b),t.setState([...t.state])},a=o=>{let i=new Date().getTime();t.state=t.state.map(c=>c.toastId===o?{...c,pausedAt:i}:c),e(o),t.setState([...t.state])},m=(o,i)=>{let c=setTimeout(()=>{var D;t.state=t.state.map(w=>w.toastId===o?{...w,isVisible:!1}:w),t.setState([...t.state]);let b=(D=t.state.find(w=>w.toastId===o))==null?void 0:D.removeTimeOut;p(o,b)},i>2147483647?2147483647:i);t.toastTimers.set(o,c)};return{setActive:r,isActive:n,disappear:m,resume:o=>{if(t.toastTimers.has(o))return;let i=t.state.find(b=>b.toastId===o);if(!i)return;let c=i.createdAt+(i.timeOut||3e3)-(i.pausedAt||0);m(o,c)},pause:a,replace:f,remove:p}};var l=new _,V=N(),S=(t="default")=>(e,r={})=>{let{timeOut:n=3e3,removeTimeOut:p=200,pauseOnHover:f=!0,toastId:a}=r,m=a||V();if(s.isActive(m))throw new Error("A duplicate custom ID is not available.");let T=new Date().getTime(),o={...r,timeOut:n>2147483647?2147483647:n,toastId:m,data:e,createdAt:T,toastType:t,pauseOnHover:f,removeTimeOut:p,isVisible:!0};return l.state.push(o),l.setState([...l.state]),m},s=(t,e={})=>S()(t,e),I=W(l);s.setActive=I.setActive;s.isActive=I.isActive;s.disappear=I.disappear;s.resume=I.resume;s.pause=I.pause;s.replace=I.replace;s.remove=I.remove;s.success=S("success");s.error=S("error");s.warn=S("warn");s.loading=S("loading");s.custom=S("custom");s.promise=(t,e,r={})=>{let{loading:n,success:p,error:f}=e,a=s.loading(n,{...r,timeOut:2147483647});t.then(()=>{s.replace(a,p,{...r,toastType:"success"})}).catch(()=>{s.replace(a,f,{...r,toastType:"error"})})};import v from"react";import{useSyncExternalStore as B}from"react";import y,{useEffect as z}from"react";import{Children as U}from"react";function A({condition:t,children:e}){let r=U.toArray(e);if(r.length>2)throw new Error("The number of children exceeds two.");return r.find(n=>t?n.key===".0":n.key===".1")}function O({children:t}){return t}function k({children:t}){return t}var P=({isVisible:t,position:e})=>t?/top/i.test(e)?`${g}__fade-in`:`${g}__fade-in-reverse`:/bottom/i.test(e)?`${g}__fade-out-reverse`:`${g}__fade-out`;import u from"react";function Y(){return u.createElement("svg",{stroke:"none",fill:"none",strokeWidth:"2",viewBox:"0 0 24 24",strokeLinecap:"round",strokeLinejoin:"round",height:"22",width:"22",xmlns:"http://www.w3.org/2000/svg"},u.createElement("path",{stroke:"none",d:"M0 0h24v24H0z"}),u.createElement("path",{d:"M17 3.34a10 10 0 1 1 -14.995 8.984l-.005 -.324l.005 -.324a10 10 0 0 1 14.995 -8.336zm-1.293 5.953a1 1 0 0 0 -1.32 -.083l-.094 .083l-3.293 3.292l-1.293 -1.292l-.094 -.083a1 1 0 0 0 -1.403 1.403l.083 .094l2 2l.094 .083a1 1 0 0 0 1.226 0l.094 -.083l4 -4l.083 -.094a1 1 0 0 0 -.083 -1.32z",strokeWidth:"0",fill:"#1ab173"}))}function X(){return u.createElement("svg",{stroke:"none",fill:"#eb2639",strokeWidth:"2",viewBox:"0 0 24 24",height:"22",width:"22",xmlns:"http://www.w3.org/2000/svg"},u.createElement("path",{d:"M11.953 2C6.465 2 2 6.486 2 12s4.486 10 10 10 10-4.486 10-10S17.493 2 11.953 2zM13 17h-2v-2h2v2zm0-4h-2V7h2v6z"}))}function $(){return u.createElement("svg",{stroke:"none",fill:"#fcba03",strokeWidth:"0",viewBox:"0 0 1024 1024",height:"22",width:"22",xmlns:"http://www.w3.org/2000/svg"},u.createElement("path",{d:"M955.7 856l-416-720c-6.2-10.7-16.9-16-27.7-16s-21.6 5.3-27.7 16l-416 720C56 877.4 71.4 904 96 904h832c24.6 0 40-26.6 27.7-48zM480 416c0-4.4 3.6-8 8-8h48c4.4 0 8 3.6 8 8v184c0 4.4-3.6 8-8 8h-48c-4.4 0-8-3.6-8-8V416zm32 352a48.01 48.01 0 0 1 0-96 48.01 48.01 0 0 1 0 96z"}))}var x={success:Y,error:X,loading:()=>u.createElement("div",{className:`${g}__loading`}),warn:$};function H({status:t,children:e}){let r=t==="custom"||t==="default"?null:x[t];return u.createElement("div",{style:{boxSizing:"border-box",backgroundColor:"white",padding:"12px 14px 12px 12px",display:"flex",alignItems:"center",gap:5,borderRadius:8,boxShadow:"2px 4px 10px rgba(0, 0, 0, 0.1)"}},r&&u.createElement("span",{style:{minWidth:20,maxWidth:20}},u.createElement(r,null)),e)}function M({toastProps:t,...e}){let r=P({isVisible:t.isVisible,position:t.position}),n=typeof t.data=="function"?t.data({close:()=>s.disappear(t.toastId,0),immediatelyClose:()=>{s.disappear(t.toastId,0),s.remove(t.toastId,0)},icons:{success:y.createElement(x.success,null),error:y.createElement(x.error,null),warn:y.createElement(x.warn,null),loading:y.createElement(x.loading,null)},isVisible:t.isVisible}):t.data,p=()=>{t.pauseOnHover&&s.pause(t.toastId)},f=()=>{t.pauseOnHover&&s.resume(t.toastId)};return z(()=>{s.isActive(t.toastId)||(s.setActive(t.toastId),s.disappear(t.toastId,t.timeOut))},[t.toastId]),z(()=>{if(t.updated!==void 0){let a=t.timeOut>=2147483647?3e3:t.timeOut;s.disappear(t.toastId,a)}},[t.updated]),y.createElement("div",{role:"alert","data-testid":`container-${t.containerId}`,className:t.toastType==="custom"?"":r,onMouseEnter:p,onMouseLeave:f,...e},y.createElement(A,{condition:t.toastType!=="custom"},y.createElement(O,null,y.createElement(H,{status:t.toastType},n)),y.createElement(k,null,n)))}var L=({position:t,reverse:e=!1})=>/top/i.test(t)?e?"column-reverse":"column":/bottom/i.test(t)?e?"column":"column-reverse":"column";var d=16,j={"top-left":{top:d,left:d},"top-center":{top:d,left:"50%",transform:"translateX(-50%)"},"top-right":{top:d,right:d},"bottom-left":{bottom:d,left:d},"bottom-center":{bottom:d,left:"50%",transform:"translateX(-50%)"},"bottom-right":{bottom:d,right:d}};function _t({position:t="top-center",containerId:e="",gap:r=9,reverse:n=!1}){let p=B(l.subscribe.bind(l),l.getSnapShot.bind(l),l.getSnapShot.bind(l)),f=p.filter(a=>a.containerId===void 0).reduce((a,m)=>{let T=m.position||t;return m.position=T,a[T]=a[T]||[],a[T].push(m),a},{});return v.createElement(A,{condition:!!e},v.createElement(O,null,v.createElement("div",{style:{position:"absolute",zIndex:9999,pointerEvents:"none"}},v.createElement("div",{style:{pointerEvents:"auto",display:"flex",flexDirection:n?"column-reverse":"column",gap:r}},p.filter(a=>a.containerId===e).map(a=>v.createElement(M,{key:a.toastId,toastProps:a,style:{display:"flex"}}))))),v.createElement(k,null,v.createElement("div",{style:{position:"fixed",zIndex:9999,top:d,left:d,right:d,bottom:d,pointerEvents:"none"}},Object.entries(f).map(([a,m])=>{let T=j[a],o=L({position:a,reverse:n});return v.createElement("div",{key:a,"data-testid":a,style:{pointerEvents:"auto",position:"fixed",display:"flex",flexDirection:o,gap:r,...T}},m.map(i=>v.createElement(M,{key:i.toastId,toastProps:i,style:{display:"flex",justifyContent:"center"}})))}))))}export{_t as ToastContainer,s as toast,l as toastStore};
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-strawberry-toast",
|
|
3
|
+
"description": "simple headless free position react toast library",
|
|
4
|
+
"version": "1.0.0-alpha.1",
|
|
5
|
+
"author": "dkpark10",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dkpark10/react-strawberry-toast.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/dkpark10/react-strawberry-toast/issues"
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"require": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"default": "./dist/index.mjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"./dist/headless": {
|
|
28
|
+
"require": {
|
|
29
|
+
"types": "./dist/headless.d.ts",
|
|
30
|
+
"default": "./dist/headless.js"
|
|
31
|
+
},
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./dist/headless.d.mts",
|
|
34
|
+
"default": "./dist/headless.mjs"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"./dist/index.css": "./dist/index.css"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"src",
|
|
41
|
+
"dist"
|
|
42
|
+
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"dev": "pnpm run --filter=react-strawberry-toast-playground dev",
|
|
45
|
+
"dev:docs": "pnpm run --filter=react-strawberry-toast-docs dev",
|
|
46
|
+
"deploy:docs": "pnpm run --filter=react-strawberry-toast-docs deploy",
|
|
47
|
+
"build": "tsup",
|
|
48
|
+
"test": "vitest",
|
|
49
|
+
"test:watch": "vitest --watch",
|
|
50
|
+
"test:ui": "vitest --ui",
|
|
51
|
+
"test:coverage": "vitest --coverage"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
55
|
+
"@testing-library/react": "^15.0.7",
|
|
56
|
+
"@types/react": "^18.3.12",
|
|
57
|
+
"@types/react-dom": "^18.3.1",
|
|
58
|
+
"concurrently": "^9.1.0",
|
|
59
|
+
"esbuild-sass-plugin": "^3.3.1",
|
|
60
|
+
"jsdom": "^24.0.0",
|
|
61
|
+
"react": "^18.2.0",
|
|
62
|
+
"react-dom": "^18.2.0",
|
|
63
|
+
"rollup-preserve-directives": "^1.1.3",
|
|
64
|
+
"sass": "^1.83.0",
|
|
65
|
+
"tsup": "^8.3.5",
|
|
66
|
+
"typescript": "^5.7.2",
|
|
67
|
+
"vite": "^5.2.0",
|
|
68
|
+
"vite-plugin-dts": "^4.3.0",
|
|
69
|
+
"vite-plugin-svgr": "^4.3.0",
|
|
70
|
+
"vitest": "^1.6.0"
|
|
71
|
+
},
|
|
72
|
+
"peerDependencies": {
|
|
73
|
+
"react": ">=18",
|
|
74
|
+
"react-dom": ">=18"
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Children, type PropsWithChildren } from 'react';
|
|
2
|
+
|
|
3
|
+
interface ConditionProps {
|
|
4
|
+
condition: boolean;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function Condition({ condition, children }: ConditionProps & PropsWithChildren) {
|
|
8
|
+
const childrenArray = Children.toArray(children);
|
|
9
|
+
if (childrenArray.length > 2) {
|
|
10
|
+
throw new Error('The number of children exceeds two.');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return (childrenArray as any[]).find((child) => {
|
|
14
|
+
if (condition) {
|
|
15
|
+
return child.key === '.0';
|
|
16
|
+
}
|
|
17
|
+
return child.key === '.1';
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function If({ children }: PropsWithChildren) {
|
|
22
|
+
return children;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function Else({ children }: PropsWithChildren) {
|
|
26
|
+
return children;
|
|
27
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Children, type PropsWithChildren } from 'react';
|
|
2
|
+
|
|
3
|
+
interface IfProps {
|
|
4
|
+
condition: boolean;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function If({ condition, children }: IfProps & PropsWithChildren) {
|
|
8
|
+
const childrenArray = Children.toArray(children);
|
|
9
|
+
if (childrenArray.length > 2) {
|
|
10
|
+
throw new Error('The number of children exceeds two.');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return (childrenArray as any[]).find((child) => {
|
|
14
|
+
if (condition) {
|
|
15
|
+
return child.key === '.0';
|
|
16
|
+
}
|
|
17
|
+
return child.key === '.1';
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function Then({ children }: PropsWithChildren) {
|
|
22
|
+
return children;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function Else({ children }: PropsWithChildren) {
|
|
26
|
+
return children;
|
|
27
|
+
}
|