Skip to content

Commit b2f4611

Browse files
committed
Update docs for 2.0
1 parent ad49ba0 commit b2f4611

File tree

7 files changed

+413
-72
lines changed

7 files changed

+413
-72
lines changed

site/components/docs-layout.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const TableItem: React.FC<{
99
href: string;
1010
}> = ({ children, href }) => (
1111
<Link href={href}>
12-
<a className="rounded px-3 py-2 transition-colors duration-200 relative block hover:text-toast-500 text-toast-700">
12+
<a className="rounded px-3 py-1.5 transition-colors duration-200 relative block hover:text-toast-500 text-toast-700">
1313
{children}
1414
</a>
1515
</Link>
@@ -53,22 +53,25 @@ export default function DocsLayout({ children, meta }) {
5353
GitHub
5454
</a>
5555
</header>
56-
<nav className="font-medium rounded-lg">
57-
<div className="flex flex-col mb-8">
56+
<nav className="font-medium rounded-lg ">
57+
<div className="flex flex-col mb-8 sticky top-0">
5858
<TableHeader>Overview</TableHeader>
5959

6060
<TableItem href="/docs">Get Started</TableItem>
6161

6262
<TableHeader>API</TableHeader>
6363

6464
<TableItem href="/docs/toast">toast()</TableItem>
65-
<TableItem href="/docs/toaster">Toaster</TableItem>
65+
<TableItem href="/docs/toaster">{`Toaster`}</TableItem>
66+
<TableItem href="/docs/toast-bar">{`ToastBar`}</TableItem>
6667
<TableItem href="/docs/use-toaster">useToaster()</TableItem>
6768
<TableItem href="/docs/use-toaster-store">
6869
useToasterStore()
6970
</TableItem>
7071
<TableHeader>Guides</TableHeader>
7172
<TableItem href="/docs/styling">Styling</TableItem>
73+
<TableHeader>Releases</TableHeader>
74+
<TableItem href="/docs/version-2">New in 2.0</TableItem>
7275
</div>
7376
</nav>
7477

site/pages/docs/styling.mdx

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,41 @@ toast('I have a border.', {
5555
});
5656
```
5757

58-
## Changing the offset
58+
## Change the offset
5959

60-
If you want to change the offset of your noticiations, add a margin to your `toastOptions`.
60+
If you want to change the offset of your notifications, you can adapt the absolute position in `containerStyle`.
6161

6262
```jsx
6363
<Toaster
64-
toastOptions={{
65-
style: {
66-
margin: '50px',
67-
},
64+
containerStyle={{
65+
top: 20,
66+
left: 20,
67+
bottom: 20,
68+
right: 20,
69+
}}
70+
/>
71+
```
72+
73+
## Change position of the toaster
74+
75+
By default, the toaster is position fixed in the window. If you want to place it somewhere else, you can ovewrite the position with `containerStyle`.
76+
77+
```jsx
78+
<Toaster
79+
containerStyle={{
80+
position: 'relative',
6881
}}
6982
/>
7083
```
7184

85+
## Change offset between toasts
86+
87+
If you want to change the offset between notifications change the gutter.
88+
89+
```jsx
90+
<Toaster gutter={24} />
91+
```
92+
7293
## Change icon color
7394

7495
All icon colors can be changed by supplying a `iconTheme` with a `primary` & `secondary` color.
@@ -85,3 +106,23 @@ All icon colors can be changed by supplying a `iconTheme` with a `primary` & `se
85106
}}
86107
/>
87108
```
109+
110+
## Change enter and exit animations
111+
112+
In this example, we provide a render function with the default `<ToastBar />`. We overwrite the animation style based on the current state.
113+
114+
```jsx
115+
import { Toaster, ToastBar } from 'react-hot-toast';
116+
117+
<Toaster>
118+
{(t) => (
119+
<ToastBar
120+
toast={t}
121+
style={{
122+
...t.style,
123+
animation: t.visible ? 'custom-enter 1s ease' : 'custom-exit 1s ease',
124+
}}
125+
/>
126+
)}
127+
</Toaster>;
128+
```

site/pages/docs/toast-bar.mdx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Layout from '../../components/docs-layout';
2+
import toast from 'react-hot-toast';
3+
4+
export const meta = {
5+
title: '<ToasterBar/> API',
6+
};
7+
8+
export default ({ children, meta }) => <Layout meta={meta}>{children}</Layout>;
9+
10+
# `<ToasterBar />` API
11+
12+
This is the **default toast component** rendered by the [Toaster](/docs/toaster). You can use this component in a [Toaster](/docs/toaster) with a custom render function to overwrite its defaults.
13+
14+
## Available options
15+
16+
```jsx
17+
<ToastBar
18+
toast={t}
19+
style={{}} // Overwrite styles
20+
position="top-center" // Used to adapt the animation
21+
/>
22+
```
23+
24+
## Add custom content
25+
26+
You can add a **render function to the ToastBar to modify its content**. An object containing The `icon` as well as the `message` are passed into the function.
27+
28+
### Add a dismiss button
29+
30+
In this example we add a basic dismiss button to all toasts, except if the loading one.
31+
32+
```jsx
33+
import { Toaster, ToastBar } from 'react-hot-toast';
34+
35+
<Toaster>
36+
{(t) => (
37+
<ToastBar toast={t}>
38+
{({ icon, message }) => (
39+
<>
40+
{icon}
41+
{message}
42+
{t.type !== 'loading' && (
43+
<button onClick={() => toast.dismiss(t.id)}>X</button>
44+
)}
45+
</>
46+
)}
47+
</ToastBar>
48+
)}
49+
</Toaster>;
50+
```

site/pages/docs/toast.mdx

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,38 @@ export const meta = {
77

88
# `toast()` API
99

10-
Call it to create a toast from anywhere, even outside React. Make sure you add the `<Toaster/>` component to your app first.
10+
Call it to create a toast from anywhere, even outside React. Make sure you add the [`<Toaster/>`](/docs/toaster) component to your app first.
11+
12+
## Available toast options
13+
14+
You can provide `ToastOptions` as the second argument. They will overwrite all options received from [`<Toaster/>`](/docs/toaster).
15+
16+
```js
17+
toast('Hello World', {
18+
duration: 4000,
19+
20+
// Styling
21+
style: {},
22+
className: '',
23+
24+
// Custom Icon
25+
icon: '👏',
26+
27+
// Change colors of success/error/loading icon
28+
iconTheme: {
29+
primary: '#000',
30+
secondary: '#fff',
31+
},
32+
33+
// Aria
34+
ariaProps: {
35+
role: 'status',
36+
'aria-live': 'polite',
37+
},
38+
});
39+
```
40+
41+
## Creating a toast
1142

1243
### Blank
1344

@@ -23,23 +54,31 @@ The most basic variant. It does not have an icon by default, but you can provide
2354
toast.success('Successfully created!');
2455
```
2556

26-
Creates a notification with an animated checkmark. It can be themed with `iconTheme`.
57+
Creates a notification with an animated checkmark. It can be themed with the `iconTheme` option.
2758

2859
### Error
2960

3061
```js
3162
toast.error('This is an error!');
3263
```
3364

34-
Creates a notification with an animated error icon. It can be themed with `iconTheme`.
65+
Creates a notification with an animated error icon. It can be themed with the `iconTheme` option.
66+
67+
### Custom (JSX)
68+
69+
```js
70+
toast.custom(<div>Hello World</div>);
71+
```
72+
73+
Creates a custom notification with JSX without default styles.
3574

3675
### Loading
3776

3877
```js
3978
toast.loading('Waiting...');
4079
```
4180

42-
This will create a loading notification. Most likely you want to update it manually afterwards. For a friendly alternative, check out `toast.promise()`, which takes care of that automatically.
81+
This will create a loading notification. Most likely, you want to update it manually afterwards. For a friendly alternative, check out `toast.promise()`, which takes care of that automatically.
4382

4483
### Promise
4584

@@ -83,42 +122,16 @@ toast.promise(
83122
);
84123
```
85124

86-
## Available options
87-
88-
You can provide `ToastOptions` as second argument. They will overwrite all options received from `<Toaster/>`.
89-
90-
```js
91-
toast('Hello World', {
92-
duration: 4000,
93-
94-
// Styling
95-
style: {},
96-
className: '',
97-
98-
// Custom Icon
99-
icon: '👏',
100-
101-
// Change colors of success/error/loading icon
102-
iconTheme: {
103-
primary: '#000',
104-
secondary: '#fff',
105-
},
106-
107-
// Aria
108-
role: 'status',
109-
ariaLive: 'polite',
110-
});
111-
```
112-
113125
## Default durations
114126

115-
Every type has their own duration. You can overwrite them `duration` with the toast options. This can be done per toast options or globally by the [`<Toaster/>`](/docs/toaster).
127+
Every type has its own duration. You can overwrite them `duration` with the toast options. This can be done per toast options or globally by the [`<Toaster/>`](/docs/toaster).
116128

117129
| type | duration |
118130
| --------- | -------- |
119131
| `blank` | 4000 |
120132
| `error` | 4000 |
121133
| `success` | 2000 |
134+
| `custom` | 4000 |
122135
| `loading` | Infinity |
123136

124137
### Dismiss toast programmatically
@@ -157,6 +170,8 @@ toast.remove()
157170

158171
### Update an existing toast
159172

173+
Each toast call returns a unique id. Use in the toast options to update the existing toast.
174+
160175
```js
161176
const toastId = toast.loading('Loading...');
162177

@@ -167,9 +182,19 @@ toast.success('This worked', {
167182
});
168183
```
169184

185+
### Prevent duplicate toasts
186+
187+
To prevent duplicates of the same kind, you can provide a unique permanent id.
188+
189+
```js
190+
toast.success('Copied to clipboard!', {
191+
id: 'clipboard',
192+
});
193+
```
194+
170195
### Render JSX custom content
171196

172-
You can provide a React components instead of text.
197+
You can provide a React component instead of text. If you don't want any default styles use `toast.custom()` instead.
173198

174199
```jsx
175200
toast(
@@ -182,7 +207,7 @@ toast(
182207
);
183208
```
184209

185-
You can also supply a function that receives the `Toast` as argument. This can be usefull to add a custom dismiss button.
210+
You can also supply a function that receives the `Toast` as an argument, giving you access to all properties. This allows you to access the toast id, which can be used to add a dismiss button.
186211

187212
```jsx
188213
toast(

0 commit comments

Comments
 (0)