mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
docs(toast-notification): add "Autoclose" example
This commit is contained in:
parent
2404244221
commit
ced9673681
2 changed files with 40 additions and 0 deletions
|
@ -14,6 +14,14 @@ See also: [InlineNotification](InlineNotification)
|
||||||
|
|
||||||
<ToastNotification title="Error" subtitle="An internal server error occurred." caption="{new Date().toLocaleString()}" />
|
<ToastNotification title="Error" subtitle="An internal server error occurred." caption="{new Date().toLocaleString()}" />
|
||||||
|
|
||||||
|
## Autoclose
|
||||||
|
|
||||||
|
By default, `ToastNotification` does not automatically close.
|
||||||
|
|
||||||
|
Specify the `timeout` prop to automatically close the notification after a specified duration (in milliseconds).
|
||||||
|
|
||||||
|
<FileSource src="/framed/ToastNotification/ToastNotificationTimeout" />
|
||||||
|
|
||||||
## Prevent default close behavior
|
## Prevent default close behavior
|
||||||
|
|
||||||
`ToastNotification` is a controlled component. Prevent the default close behavior using the `e.preventDefault()` method in the dispatched `on:close` event.
|
`ToastNotification` is a controlled component. Prevent the default close behavior using the `e.preventDefault()` method in the dispatched `on:close` event.
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
<script>
|
||||||
|
import { Button, ToastNotification } from "carbon-components-svelte";
|
||||||
|
import { fade } from "svelte/transition";
|
||||||
|
|
||||||
|
let timeout = undefined;
|
||||||
|
$: showNotification = timeout !== undefined;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
disabled="{showNotification}"
|
||||||
|
on:click="{() => {
|
||||||
|
timeout = 3_000; // 3 seconds
|
||||||
|
}}"
|
||||||
|
>
|
||||||
|
Show notification
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
{#if showNotification}
|
||||||
|
<div transition:fade>
|
||||||
|
<ToastNotification
|
||||||
|
timeout="{timeout}"
|
||||||
|
kind="success"
|
||||||
|
title="Success"
|
||||||
|
subtitle="This notification will autoclose in {timeout.toLocaleString()} ms."
|
||||||
|
caption="{new Date().toLocaleString()}"
|
||||||
|
on:close="{(e) => {
|
||||||
|
timeout = undefined;
|
||||||
|
console.log(e.detail.timeout); // true if closed via timeout
|
||||||
|
}}"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{/if}
|
Loading…
Add table
Add a link
Reference in a new issue