Skip to content

Commit d6c9fbd

Browse files
committed
feat: update toast component to include action button text & fix max width
1 parent ecf9962 commit d6c9fbd

File tree

5 files changed

+41
-26
lines changed

5 files changed

+41
-26
lines changed

library/src/components/toast.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ const Toast = (props: ToastComponentProps) => {
102102
</div>
103103
<div className="t_actions">
104104
{props.action && (
105-
<button onClick={props.action} title="Action button">
106-
Action
105+
<button onClick={props.action.onClick} title="Action button">
106+
{props.action.text ?? 'Action'}
107107
</button>
108108
)}
109109
<button onClick={props.onClose} title="Close toast">

library/src/styles/toast-component.css

+6-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@
5151
list-style: none;
5252
outline: none;
5353
background-color: var(--background-color);
54-
width: 100%;
54+
width: auto;
5555
box-shadow:
5656
0 1px 3px 0 var(--box-shadow),
5757
0 1px 2px -1px var(--box-shadow);
5858
border: 1px solid var(--border-color);
59-
max-width: 20rem;
6059
border-radius: 0.375rem;
6160
font-size: 0.875rem;
6261
line-height: 1.25rem;
@@ -70,9 +69,12 @@
7069
.t_container {
7170
display: flex;
7271
width: 100%;
72+
max-width: 20rem;
7373
height: 100wh;
7474
gap: 0.5rem;
7575
padding: 12px;
76+
word-wrap: break-word;
77+
overflow-wrap: break-word;
7678
}
7779

7880
.t_icon {
@@ -82,6 +84,8 @@
8284
.t_content {
8385
display: flex;
8486
flex-direction: column;
87+
justify-content: center;
88+
max-width: 100%;
8589
}
8690

8791
.t_content p {

library/src/types/toast.types.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export type Position =
1010
| 'bottom-center';
1111
export type Theme = 'light' | 'dark' | 'system';
1212

13+
export interface Action {
14+
text?: string;
15+
onClick: () => void | (() => Promise<void>);
16+
}
17+
1318
export type ToastProps = {
1419
id?: number;
1520
text: string;
@@ -18,7 +23,7 @@ export type ToastProps = {
1823
iconSize?: number;
1924
delayDuration?: number;
2025
theme?: Theme;
21-
action?: () => void | (() => Promise<void>);
26+
action?: Action;
2227
};
2328

2429
export type ToastProviderProperties = {

website/app/components/examples/useToast.tsx

+21-17
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,27 @@ const UseToastActionsExamples = () => {
9292

9393
const handleChangeVariant = () => {
9494
t.default({
95-
text: `A toast with 👀 action button`,
96-
description: 'Click the action button to see the confetti 🥹',
97-
action: () => {
98-
if (typeof window !== 'undefined') {
99-
const confetti = new JSConfetti();
100-
confetti.addConfetti({
101-
confettiRadius: 3,
102-
confettiNumber: 100,
103-
confettiColors: [
104-
'#14532d',
105-
'#ff477e',
106-
'#f7f7f7',
107-
'#ffcc00',
108-
'#ffcc00',
109-
],
110-
});
111-
}
95+
text: `A toast with confetti 🎉`,
96+
description: 'Click the button to see the confetti',
97+
delayDuration: 400000,
98+
icon: <PartyPopperIcon size={24} />,
99+
action: {
100+
onClick: () => {
101+
if (typeof window !== 'undefined') {
102+
const confetti = new JSConfetti();
103+
confetti.addConfetti({
104+
confettiRadius: 3,
105+
confettiNumber: 100,
106+
confettiColors: [
107+
'#14532d',
108+
'#ff477e',
109+
'#f7f7f7',
110+
'#ffcc00',
111+
'#ffcc00',
112+
],
113+
});
114+
}
115+
},
112116
},
113117
});
114118
};

website/app/routes/useToast.mdx

+6-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ export default function Index() {
3939
```tsx
4040
t.default({
4141
text: `A toast with 👀 action button`,
42-
action: () => {
43-
// Do something
44-
}
42+
action: {
43+
label: 'Action', // Button label
44+
onClick: () => {
45+
// Do something
46+
},
4547
},
4648
});
4749
```
@@ -56,6 +58,6 @@ The `toast.variant` function accepts the following options:
5658
| `description` | Toast's description | `string` | - |
5759
| `icon` | Icon to display in the toast | `ReactNode` | - |
5860
| `iconSize` | Size of the icon | `number` | - |
59-
| `delayDuration` | Duration before the toast disappears | `number` | - |
61+
| `delayDuration` | Duration before the toast disappears | `number` (default: `4000`) | - |
6062
| `theme` | Theme of the toast | `Theme` (`light` / `dark` / `system`) | - |
6163
| `action` | Show a _Action_ button and execute a function | `() => void \| () => Promise<void>` | - |

0 commit comments

Comments
 (0)