Skip to content

Commit

Permalink
add stories
Browse files Browse the repository at this point in the history
  • Loading branch information
vascofg committed Dec 9, 2024
1 parent ccd8369 commit 5699cc2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,39 @@ Change the position of notifications by passing the `position` prop. Supported v

<Story of={Stories.Position} />

### Dismissing toasts programmatically

You can use the function returned from the `setToast` method to dismiss a toast programmatically:

```tsx
import { useNotificationToast, Button } from '@sumup-oss/circuit-ui';

export function App({}) {
const { setToast } = useNotificationToast();
const dismissRef = useRef(() => {});

const handleClick = () => {
dismissRef.current = setToast({
variant: 'success',
body: 'This is a toast message',
});
};

const handleDismiss = () => {
dismissRef.current();
};

return (
<>
<Button onClick={handleClick}>Open toast</Button>
<Button onClick={handleDismiss}>Dismiss toast</Button>
</>
);
}
```

<Story of={Stories.Dismiss} />

## Accessibility

By their nature as status messages, toasts are rendered inside a [live region](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions) using `role="status"` and `aria-live="polite"`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
NotificationToast,
type NotificationToastProps,
} from './NotificationToast.js';
import { useRef } from 'react';

Check failure on line 29 in packages/circuit-ui/components/NotificationToast/NotificationToast.stories.tsx

View workflow job for this annotation

GitHub Actions / ci (20)

`react` import should occur before import of `../../../../.storybook/components/index.js`

Check failure on line 29 in packages/circuit-ui/components/NotificationToast/NotificationToast.stories.tsx

View workflow job for this annotation

GitHub Actions / ci (22)

`react` import should occur before import of `../../../../.storybook/components/index.js`

export default {
title: 'Notification/NotificationToast',
Expand Down Expand Up @@ -110,6 +111,36 @@ Position.args = {

Position.play = play;

const DismissApp = ({ toast }: { toast: NotificationToastProps }) => {
const { setToast } = useNotificationToast();
const dismissRef = useRef(() => {});
const randomIndex = isChromatic()
? 1
: Math.floor(Math.random() * TOASTS.length);
return (
<>
<Button
type="button"
onClick={() => {
dismissRef.current = setToast({ ...toast, ...TOASTS[randomIndex] });
}}
>
Open toast
</Button>
&nbsp;
<Button type="button" onClick={() => dismissRef.current()}>
Dismiss toast
</Button>
</>
);
};

export const Dismiss = (toast: NotificationToastProps) => (
<ToastProvider>
<DismissApp toast={toast} />
</ToastProvider>
);

const variants = ['info', 'success', 'warning', 'danger'] as const;

export const Variants = (toast: NotificationToastProps) => (
Expand Down

0 comments on commit 5699cc2

Please sign in to comment.