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

Updated documentation for Toast component #2261

Merged
merged 31 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e226d0a
Updated documentation for toasts
smmr-dn Sep 24, 2024
193c165
Merge branch 'main' into uyen/toast-doc
smmr-dn Sep 24, 2024
3d63506
added close individual
smmr-dn Sep 24, 2024
061e58f
conflicts
smmr-dn Sep 24, 2024
3c0a08e
pnpm lock
smmr-dn Sep 24, 2024
5b3ee9e
pnpm lock
smmr-dn Sep 24, 2024
225a2fe
removed redundant import
smmr-dn Sep 24, 2024
3cd0da3
split toast functions
smmr-dn Sep 24, 2024
c197744
cleanup
smmr-dn Sep 24, 2024
f5a4222
comments
smmr-dn Sep 25, 2024
2e35773
more comments
smmr-dn Sep 25, 2024
d37397a
clean up
smmr-dn Sep 25, 2024
55a465b
added toast type exampled
smmr-dn Sep 25, 2024
6233f54
Update apps/website/src/content/docs/toast.mdx
smmr-dn Sep 26, 2024
037354a
Merge branch 'main' into uyen/toast-doc
smmr-dn Sep 26, 2024
b4e458b
Changes and TODOs from pairing session
mayank99 Sep 27, 2024
2bb79d8
combined sections and wording
smmr-dn Sep 27, 2024
81d46ca
cleaned up examples
smmr-dn Sep 27, 2024
2fbef06
stray comment
smmr-dn Sep 27, 2024
03156e7
trigger build
smmr-dn Sep 27, 2024
1de06cc
Merge branch 'main' into uyen/toast-doc
smmr-dn Sep 30, 2024
19584c7
comments
smmr-dn Sep 30, 2024
6801195
wording
smmr-dn Sep 30, 2024
a84693a
Added Select for order and placemenet
smmr-dn Sep 30, 2024
48c1291
added onChange in order Select
smmr-dn Sep 30, 2024
08bb6cd
wording
smmr-dn Sep 30, 2024
66fc50c
removed redundancies
smmr-dn Sep 30, 2024
6ed46ce
combined to onChange
smmr-dn Sep 30, 2024
86ee674
added aria-label to selects
smmr-dn Sep 30, 2024
ca011a8
wording
smmr-dn Oct 1, 2024
cda2768
fixed typo
smmr-dn Oct 1, 2024
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
88 changes: 85 additions & 3 deletions apps/website/src/content/docs/toast.mdx
Original file line number Diff line number Diff line change
@@ -1,18 +1,100 @@
---
title: Toast
description: A Toast is a non modal, unobtrusive window element used to display small amount of information to a user.
description: A toast is a non-disruptive message that provides feedback based on a user action.
thumbnail: Toast
---

<p>{frontmatter.description}</p>

<Placeholder componentPageName='toasts--positive' />
## Usage

Before triggering a toast, the toaster needs to be initialized using the `useToaster` hook. This hook returns an object which provides various methods for triggering the toast and changing the toaster settings.
mayank99 marked this conversation as resolved.
Show resolved Hide resolved

<LiveExample src='Toast.main.jsx'>
<AllExamples.ToastMainExample client:load />
</LiveExample>

mayank99 marked this conversation as resolved.
Show resolved Hide resolved
A toast notification provides a brief message about app processes at the top of the screen. Because these messages are so prominent, we need to be careful not to overuse or misuse them.
### Status

Toast statuses can be set using the status methods provided by the toaster object. The status method takes in a toast message and an `options` object for further customization. Four available statuses are:
mayank99 marked this conversation as resolved.
Show resolved Hide resolved

- `positive()`
- `negative()`
- `warning()`
- `informational()`

<LiveExample src='Toast.status.jsx'>
<AllExamples.ToastStatusExample client:load />
</LiveExample>

For further customization with `options`, you can utilize `link`, which allows taking further actions directly from the toast, such as navigating to another page or triggering a specific function.

<LiveExample src='Toast.link.jsx'>
<AllExamples.ToastLinkExample client:load />
</LiveExample>

### Settings

The object returned by `useToaster` also provides a `setSettings` method, which can be used to customize the `placement` and `order` of toasts.

`placement` supports the following values:

- `"top"` (default)
- `"top-start"`
- `"top-end"`
- `"bottom"`
- `"bottom-start"`
- `"bottom-end"`

`"start"` indicates the left side of viewport, while `"end"` represents the right side.

<LiveExample src='Toast.placement.jsx'>
<AllExamples.ToastPlacementExample client:load />
</LiveExample>

`order` supports the following values:

- `"auto"` (default)
- `"descending"`
- `"ascending"`

When set to `"descending"`, the most recent toasts are on top. When set to `"ascending"`, the most recent toasts are on bottom. When set to `"auto"`, it will behave like `"descending"` when `placement` is set to a value beginning with `"top"`, otherwise it will behave like `"ascending"`.

<LiveExample src='Toast.order.jsx'>
<AllExamples.ToastOrderExample client:load />
</LiveExample>

### Closing toasts

When setting the `hasCloseButton` option available in the status method to `true`, each toast will be displayed with a close button, which allows you to close the toast manually.
mayank99 marked this conversation as resolved.
Show resolved Hide resolved

<LiveExample src='Toast.hasCloseButton.jsx'>
<AllExamples.ToastHasCloseButtonExample client:load />
</LiveExample>

Additionally, to implement further actions upon the closing of each toast or chain one toast after another, you can retrieve the `close()` function returned from the status method.

<LiveExample src='Toast.close.jsx'>
<AllExamples.ToastCloseExample client:load />
</LiveExample>

Closing also depends on the type of toast when used without `hasCloseButton`. By default, persisting toasts will not be closed automatically and will contain a close button while temporary toasts will automatically close after 7 seconds and will not contain a close button. The type of toasts can be specified by passing either `"persisting"` or `"temporary"` into the `type` option.

<LiveExample src='Toast.type.jsx'>
<AllExamples.ToastTypeExample client:load />
</LiveExample>

To close all toasters on viewport at once, use the `closeAll()` method offered by the toaster object.
mayank99 marked this conversation as resolved.
Show resolved Hide resolved

<LiveExample src='Toast.closeAll.jsx'>
<AllExamples.ToastCloseAllExample client:load />
</LiveExample>

The `animateOutTo` prop allows you to control the direction to which the toast animates out when closing. Specifically, it helps determine an "anchor" element that the toast will go towards when it is dismissed by pointing to a ref passed into that element.
mayank99 marked this conversation as resolved.
Show resolved Hide resolved

<LiveExample src='Toast.anchorToButton.jsx'>
<AllExamples.ToastAnchorToButtonExample client:load />
</LiveExample>

## Props

Expand Down
5 changes: 5 additions & 0 deletions examples/Toast.anchorToButton.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.demo-container {
display: flex;
align-items: center;
flex-direction: column;
}
32 changes: 32 additions & 0 deletions examples/Toast.anchorToButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import * as React from 'react';
import { useToaster, Button } from '@itwin/itwinui-react';

export default () => {
const toaster = useToaster();
const buttonRef = React.useRef(null);

React.useEffect(() => {
toaster.setSettings({
placement: 'top-end',
});
}, []);

return (
<div className='demo-container'>
<Button
ref={buttonRef}
onClick={() =>
toaster.positive('This is a positive toast message', {
animateOutTo: buttonRef.current,
})
}
>
Anchor to button
</Button>
</div>
);
};
28 changes: 28 additions & 0 deletions examples/Toast.close.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import * as React from 'react';
import { useToaster, Button, Flex, ProgressRadial } from '@itwin/itwinui-react';

export default () => {
const toaster = useToaster();

const displayProcessToast = () => {
const { close } = toaster.informational(
<Flex>
<ProgressRadial size='small' />
Your process is running...
</Flex>,
);

setTimeout(() => {
close();
toaster.positive('Process completed', {
hasCloseButton: true,
});
}, 1000);
};

return <Button onClick={displayProcessToast}>Start process</Button>;
};
6 changes: 6 additions & 0 deletions examples/Toast.closeAll.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.demo-container {
display: flex;
align-items: center;
flex-direction: column;
gap: var(--iui-size-xs);
}
22 changes: 22 additions & 0 deletions examples/Toast.closeAll.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import * as React from 'react';
import { useToaster, Button } from '@itwin/itwinui-react';

export default () => {
const toaster = useToaster();

return (
<div className='demo-container'>
<Button onClick={() => toaster.positive('Job 1 processing completed.')}>
Open toast 1
</Button>
<Button onClick={() => toaster.positive('Job 2 processing completed.')}>
Open toast 2
</Button>
<Button onClick={() => toaster.closeAll()}>Close All</Button>
</div>
);
};
24 changes: 24 additions & 0 deletions examples/Toast.hasCloseButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import * as React from 'react';
import { useToaster, Button } from '@itwin/itwinui-react';

export default () => {
const toaster = useToaster();

return (
<div className='demo-container'>
<Button
onClick={() =>
toaster.positive('This is a positive message', {
hasCloseButton: true,
})
}
>
Open toast
</Button>
</div>
);
};
27 changes: 27 additions & 0 deletions examples/Toast.link.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import * as React from 'react';
import { useToaster, Button } from '@itwin/itwinui-react';

export default () => {
const toaster = useToaster();

const displayToast = () => {
toaster.positive('Job processing completed.', {
link: {
title: 'Link',
onClick: () => {
alert('Link was clicked!');
},
},
});
};

return (
<div className='demo-container'>
<Button onClick={displayToast}>Toast with link</Button>
</div>
);
};
5 changes: 5 additions & 0 deletions examples/Toast.main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.demo-container {
display: flex;
align-items: center;
flex-direction: column;
}
22 changes: 5 additions & 17 deletions examples/Toast.main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,10 @@ export default () => {
const toaster = useToaster();

return (
<Button
onClick={() => {
toaster.setSettings({
placement: 'bottom-end',
order: 'ascending',
});
toaster.positive('Job processing completed.', {
hasCloseButton: true,
link: {
onClick: () => {},
title: 'View the report',
},
});
}}
>
Open toast
</Button>
<div className='demo-container'>
<Button onClick={() => toaster.positive('Job processing completed.')}>
Open toast
</Button>
</div>
);
};
24 changes: 24 additions & 0 deletions examples/Toast.order.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import * as React from 'react';
import { useToaster, Button } from '@itwin/itwinui-react';

export default () => {
const toaster = useToaster();

React.useEffect(() => {
toaster.setSettings({
order: 'ascending',
mayank99 marked this conversation as resolved.
Show resolved Hide resolved
});
}, []);

return (
<div className='demo-container'>
<Button onClick={() => toaster.informational('This is a toast message.')}>
Ascending toast
</Button>
</div>
);
};
5 changes: 5 additions & 0 deletions examples/Toast.placement.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.demo-container {
display: flex;
align-items: center;
flex-direction: column;
}
24 changes: 24 additions & 0 deletions examples/Toast.placement.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import * as React from 'react';
import { useToaster, Button } from '@itwin/itwinui-react';

export default () => {
const toaster = useToaster();

React.useEffect(() => {
toaster.setSettings({
placement: 'bottom-end',
mayank99 marked this conversation as resolved.
Show resolved Hide resolved
});
}, []);

return (
<div className='demo-container'>
<Button onClick={() => toaster.informational('This is a toast message.')}>
Bottom-end toast
</Button>
</div>
);
};
4 changes: 4 additions & 0 deletions examples/Toast.status.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.demo-container {
display: flex;
gap: var(--iui-size-xs);
}
37 changes: 37 additions & 0 deletions examples/Toast.status.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import * as React from 'react';
import { useToaster, Button } from '@itwin/itwinui-react';

export default () => {
const toaster = useToaster();

return (
<div className='demo-container'>
<Button
onClick={() => toaster.positive('This is a positive toast message.')}
>
Positive
</Button>
<Button
onClick={() => toaster.negative('This is a negative toast message.')}
>
Negative
</Button>
<Button
onClick={() => toaster.warning('This is a warning toast message.')}
>
Warning
</Button>
<Button
onClick={() =>
toaster.informational('This is an informational toast message.')
}
>
Informational
</Button>
</div>
);
};
4 changes: 4 additions & 0 deletions examples/Toast.type.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.demo-container {
display: flex;
gap: var(--iui-size-xs);
}
Loading
Loading