Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/grumpy-boxes-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heroui/toast": patch
---

Enable the toast's endContent to close the toast using the onClose function
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {Button} from "@heroui/react";

export default function App() {
return (
<Button
variant="flat"
onPress={() => {
addToast({
title: "Sucessful!",
endContent: (onClose) => (
<Button color="danger" size="sm" variant="flat" onPress={onClose}>
Close
</Button>
),
});
}}
>
Show Toast
</Button>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import App from "./custom-close-button-in-endContent.raw.jsx?raw";

const react = {
"/App.jsx": App,
};

export default {
...react,
};
2 changes: 2 additions & 0 deletions apps/docs/content/components/toast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import placement from "./placement";
import usage from "./usage";
import customCloseIcon from "./custom-close-icon";
import close from "./close";
import customCloseButtonInEndContent from "./custom-close-button-in-endContent";

export const toastContent = {
color,
Expand All @@ -16,4 +17,5 @@ export const toastContent = {
placement,
usage,
customCloseIcon,
customCloseButtonInEndContent,
};
9 changes: 9 additions & 0 deletions apps/docs/content/docs/components/toast.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ You can pass a custom close icon to the toast by passing the `closeIcon` prop an
files={toastContent.customCloseIcon}
/>

### Custom Close Button in `endContent`

You can also customize a close button in the `endContent` by using the provided `onClose` function inside it.

<CodeDemo
title="Custom Close Button in endContent"
files={toastContent.customCloseButtonInEndContent}
/>

### Global Toast Props

You can pass global toast props to the `ToastProvider` to apply to all toasts.
Expand Down
7 changes: 6 additions & 1 deletion packages/components/toast/src/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ const Toast = forwardRef<"div", ToastProps>((props, ref) => {
? closeIcon({})
: isValidElement(closeIcon) && cloneElement(closeIcon as ReactElement, {});

const customEndContent =
typeof endContent === "function"
? endContent((getCloseButtonProps() as ButtonProps).onPress as () => void)
: endContent;

const toastContent = (
<Component ref={domRef} {...getToastProps()}>
<div {...getContentProps()}>
Expand All @@ -104,7 +109,7 @@ const Toast = forwardRef<"div", ToastProps>((props, ref) => {
<Button isIconOnly {...(getCloseButtonProps() as ButtonProps)}>
{customCloseIcon || <CloseIcon {...getCloseIconProps()} />}
</Button>
{endContent}
{customEndContent}
</Component>
);

Expand Down
2 changes: 1 addition & 1 deletion packages/components/toast/src/use-toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface ToastProps extends ToastVariantProps {
/**
* Content to be displayed in the end side of the toast
*/
endContent?: ReactNode;
endContent?: ReactNode | ((onClose: () => void) => ReactNode);
/**
* Icon to be displayed in the toast - overrides the default icon
*/
Expand Down
28 changes: 23 additions & 5 deletions packages/components/toast/stories/toast.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,7 @@ const WithEndContentTemplate = (args) => {
addToast({
title: "Toast Title",
description: "Toast Description",
endContent: (
<Button color="warning" size="sm" variant="flat">
Upgrade
</Button>
),
endContent: args.endContent,
color: "warning",
variant: "faded",
...args,
Expand Down Expand Up @@ -444,6 +440,28 @@ export const WithEndContent = {
render: WithEndContentTemplate,
args: {
...defaultProps,
endContent: (
<Button color="warning" size="sm" variant="flat">
Upgrade
</Button>
),
},
};

export const WithEndContentCloseFunction = {
render: WithEndContentTemplate,
args: {
...defaultProps,
endContent: (onClose) => (
<>
<Button color="warning" size="sm" variant="flat">
Upgrade
</Button>
<Button color="danger" size="sm" variant="flat" onPress={onClose}>
Maybe later
</Button>
</>
),
},
};

Expand Down
Loading