Skip to content

Commit 1154b35

Browse files
docs(Toast): explain how a toast can be shown (#528)
* docs(Toast): explain how a toast can be shown Closes #519 * Update packages/main/src/webComponents/Toast/Toast.mdx Co-authored-by: Lukas Harbarth <[email protected]> Co-authored-by: Lukas Harbarth <[email protected]>
1 parent 80b6240 commit 1154b35

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Story, Props, Title, Subtitle, Description, Primary, Stories } from '@storybook/addon-docs/blocks';
2+
3+
<Title />
4+
<Subtitle />
5+
<Description />
6+
<br />
7+
8+
## Show a toast
9+
The `Toast` component has an imperative API for getting displayed. It will not be displayed just because it is part of the DOM.
10+
In order to show the Toast, you have to get a reference to the `Toast` DOM element and call the `show`-method.
11+
You can either access the DOM element by using a React `ref` or work with IDs.
12+
13+
**Example**
14+
```jsx
15+
export const MyComponentWithAToast() {
16+
const toast = useRef();
17+
18+
const showToast = () => {
19+
toast.current.show();
20+
};
21+
return (
22+
<ThemeProvider>
23+
<Button onClick={showToast}>Show Toast</Button>
24+
<Toast ref={toast}>This is my Toast!</Toast>
25+
</ThemeProvider>
26+
);
27+
}
28+
```
29+
<Story id="ui5-web-components-toast--generated-default-story"/>
30+
31+
<Props />

packages/main/src/webComponents/Toast/Toast.stories.tsx

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,44 @@ import { number, select } from '@storybook/addon-knobs';
22
import { Button } from '@ui5/webcomponents-react/lib/Button';
33
import { Toast } from '@ui5/webcomponents-react/lib/Toast';
44
import { ToastPlacement } from '@ui5/webcomponents-react/lib/ToastPlacement';
5-
import React from 'react';
5+
import React, { useRef } from 'react';
6+
import mdx from './Toast.mdx';
67

78
export default {
89
title: 'UI5 Web Components / Toast',
9-
component: Toast
10+
component: Toast,
11+
parameters: {
12+
docs: {
13+
page: mdx
14+
}
15+
}
1016
};
1117

1218
const showToast = () => {
1319
// @ts-ignore
1420
document.querySelector('#web_components_react_toast_demo').show();
1521
};
1622

17-
export const generatedDefaultStory = () => (
18-
<>
19-
<Toast
20-
// @ts-ignore
21-
id="web_components_react_toast_demo"
22-
duration={number('duration', 3000)}
23-
placement={select('placement', ToastPlacement, ToastPlacement.BottomCenter)}
24-
>
25-
Some Content
26-
</Toast>
27-
<Button onClick={showToast}>Show Toast</Button>
28-
</>
29-
);
23+
export const generatedDefaultStory = () => {
24+
const toast = useRef();
25+
26+
const showToast = () => {
27+
toast.current.show();
28+
};
29+
return (
30+
<>
31+
<Toast
32+
ref={toast}
33+
duration={number('duration', 3000)}
34+
placement={select('placement', ToastPlacement, ToastPlacement.BottomCenter)}
35+
>
36+
Some Content
37+
</Toast>
38+
<Button onClick={showToast}>Show Toast</Button>
39+
</>
40+
);
41+
};
3042

3143
generatedDefaultStory.story = {
32-
name: 'Generated default story'
44+
name: 'Default story'
3345
};

0 commit comments

Comments
 (0)