Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/components/toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const Toaster: React.FC<ToasterProps> = ({
children,
containerStyle,
containerClassName,
pauseOnFocus = false,
}) => {
const { toasts, handlers } = useToaster(toastOptions);

Expand All @@ -108,6 +109,8 @@ export const Toaster: React.FC<ToasterProps> = ({
className={containerClassName}
onMouseEnter={handlers.startPause}
onMouseLeave={handlers.endPause}
onFocus={pauseOnFocus ? handlers.startPause : undefined}
onBlur={pauseOnFocus ? handlers.endPause : undefined}
>
{toasts.map((t) => {
const toastPosition = t.position || position;
Expand Down
1 change: 1 addition & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export interface ToasterProps {
containerStyle?: React.CSSProperties;
containerClassName?: string;
children?: (toast: Toast) => JSX.Element;
pauseOnFocus?: boolean;
}

export interface ToastWrapperProps {
Expand Down
42 changes: 41 additions & 1 deletion test/toast.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ test('custom toaster renderer', async () => {
expect(screen.queryByText(/custom/i)).not.toHaveClass('custom-toast');
});

test('pause toast', async () => {
test('pause toast on hover', async () => {
render(
<>
<Toaster>
Expand Down Expand Up @@ -317,3 +317,43 @@ test('pause toast', async () => {

expect(toastElement).not.toBeInTheDocument();
});

test('pause toast on focus', async () => {
render(
<>
<Toaster pauseOnFocus>
{(t) => (
<div className="custom-toast">
<ToastIcon toast={t} />
{resolveValue(t.message, t)}
<button>Focus Me</button>
</div>
)}
</Toaster>
</>
);

act(() => {
toast.success('Title', {
duration: 1000,
});
});

waitTime(500);

const toastElement = screen.getByText(/focus me/i);

expect(toastElement).toBeInTheDocument();

fireEvent.focus(toastElement);

waitTime(10000);

expect(toastElement).toBeInTheDocument();

fireEvent.blur(toastElement);

waitTime(2000);

expect(toastElement).not.toBeInTheDocument();
});