Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components): support clearing Toast queues #1480

Merged
merged 1 commit into from
Nov 13, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/mean-ducks-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@launchpad-ui/components": patch
---

Support clearing `Toast` queues
12 changes: 12 additions & 0 deletions packages/components/src/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,26 @@ const snackbarQueue = new AriaToastQueue<SnackbarValue>({
const timeout = 6000;

const ToastQueue = {
clear: () => {
for (const toast of toastQueue.visibleToasts) {
toastQueue.close(toast.key);
}
},
error: (children: ToastContent['children'], options?: ToastOptions) =>
toastQueue.add({ children, status: 'error' }, { ...options, timeout }),
info: (children: ToastContent['children'], options?: ToastOptions) =>
toastQueue.add({ children, status: 'info' }, { ...options, timeout }),
success: (children: ToastContent['children'], options?: ToastOptions) =>
toastQueue.add({ children, status: 'success' }, { ...options, timeout }),
visibleToasts: () => toastQueue.visibleToasts,
};

const SnackbarQueue = {
clear: () => {
for (const toast of snackbarQueue.visibleToasts) {
snackbarQueue.close(toast.key);
}
},
error: (content: SnackbarContent, options?: ToastOptions) => {
const key = snackbarQueue.add({ ...content, status: 'error' }, { ...options });
return () => snackbarQueue.close(key);
Expand All @@ -208,6 +219,7 @@ const SnackbarQueue = {
const key = snackbarQueue.add({ ...content, status: 'success' }, { ...options });
return () => snackbarQueue.close(key);
},
visibleToasts: () => snackbarQueue.visibleToasts,
};

/**
Expand Down
60 changes: 43 additions & 17 deletions packages/components/stories/Toast.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import type { Meta, StoryFn, StoryObj } from '@storybook/react';

import { expect, userEvent, within } from '@storybook/test';

import { Button, Link, SnackbarContainer, SnackbarQueue, ToastContainer, ToastQueue } from '../src';
import {
Button,
ButtonGroup,
Link,
SnackbarContainer,
SnackbarQueue,
ToastContainer,
ToastQueue,
} from '../src';

const meta: Meta<typeof ToastContainer> = {
component: ToastContainer,
Expand Down Expand Up @@ -39,16 +47,25 @@ export const Example: Story = {
return (
<>
<ToastContainer {...args} />
<Button onPress={() => ToastQueue.success(<span>A success toast!</span>)}>
Show toast
</Button>
<ButtonGroup>
<Button onPress={() => ToastQueue.success(<span>A success toast!</span>)}>
Show toast
</Button>
<Button
onPress={() => {
ToastQueue.clear();
}}
>
Clear
</Button>
</ButtonGroup>
</>
);
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

await userEvent.click(canvas.getByRole('button'));
await userEvent.click(canvas.getAllByRole('button')[0]);
const body = canvasElement.ownerDocument.body;
await expect(await within(body).findByRole('alert'));
},
Expand All @@ -59,24 +76,33 @@ export const Snackbar: Story = {
return (
<>
<SnackbarContainer {...args} />
<Button
onPress={() =>
SnackbarQueue.info({
title: 'An info snackbar',
description: 'Dismiss me!',
action: <Link href="/">Link</Link>,
})
}
>
Show snackbar
</Button>
<ButtonGroup>
<Button
onPress={() => {
SnackbarQueue.info({
title: 'An info snackbar',
description: 'Dismiss me!',
action: <Link href="/">Link</Link>,
});
}}
>
Show snackbar
</Button>
<Button
onPress={() => {
SnackbarQueue.clear();
}}
>
Clear
</Button>
</ButtonGroup>
</>
);
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

await userEvent.click(canvas.getByRole('button'));
await userEvent.click(canvas.getAllByRole('button')[0]);
const body = canvasElement.ownerDocument.body;
await expect(await within(body).findByRole('alert'));
},
Expand Down
Loading