Toast
Display a brief notification.
#
Documentation
#
#
Usage
#
Use toaster.create()
to add toasts. The method takes two arguments message
and options
.
import { Button, toaster } from "@optiaxiom/react";
export function App() {
return (
<Button onClick={() => toaster.create("This is an example toast message.")}>
Create Toast
</Button>
);
}
#
Appearance
#
Use the type
option in the second argument to select between the different toast types.
import { Button, Flex, toaster } from "@optiaxiom/react";
export function App() {
return (
<Flex flexDirection="row" flexWrap="wrap">
{(
["danger", "information", "neutral", "success", "warning"] as const
).map((type) => (
<Button
key={type}
onClick={() =>
toaster.create("This is an example toast message.", { type })
}
>
{type} toast
</Button>
))}
</Flex>
);
}
#
Action
#
Use the action
and onAction
options to include a short action button associated with the toast.
Task in-progress
import { Button, Flex, Text, toaster } from "@optiaxiom/react";
import { IconTrash } from "@tabler/icons-react";
import { useState } from "react";
export function App() {
const [status, setStatus] = useState("in-progress");
return (
<Flex flexDirection="row">
<Text>Task {status}</Text>
<Button
appearance="danger-outline"
disabled={status === "deleted"}
icon={<IconTrash />}
onClick={() => {
setStatus("deleted");
toaster.create("Task Deleted", {
action: "Undo",
onAction: () => setStatus("in-progress"),
});
}}
>
Delete
</Button>
<Button
disabled={status !== "deleted"}
onClick={() => setStatus("in-progress")}
>
Restore
</Button>
</Flex>
);
}
#
Dismiss
#
Use toaster.remove()
to remove toasts. The create()
method returns a unique ID for the toast which can then be passed to the remove()
method.
import { Button, toaster } from "@optiaxiom/react";
export function App() {
return (
<Button
onClick={async () => {
const id = toaster.create("Saving task");
// simulate network request
await new Promise((resolve) => setTimeout(resolve, 3_000));
toaster.remove(id);
toaster.create("Could not save task", { type: "danger" });
}}
>
Create Toast
</Button>
);
}
#
Related
#
Alert
Show inline messages about important or time-sensitive changes.
Banner
Display a prominent message at the top of the screen.
#
Props
#
#
Toast
#
Supports all Box props in addition to its own. Renders a <div>
element.
Prop |
---|
asChild Change the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
className
|
defaultOpen
|
duration Time in milliseconds that toast should remain visible for. Overrides value
given to
|
intent
Default: |
onEscapeKeyDown
|
onOpenChange
|
onPause
|
onResume
|
onSwipeCancel
|
onSwipeEnd
|
onSwipeMove
|
onSwipeStart
|
type
|
#
ToastTitle
#
Supports all Text props in addition to its own. Renders a <div>
element.
Prop |
---|
asChild Change the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
className
|
lineClamp Truncate the text at specific number of lines.
|
truncate Whether to truncate the text and add an ellipsis at the end.
|
#
ToastAction
#
Supports all Button props in addition to its own. Renders a <button>
element.
Prop |
---|
addonAfter Display content inside the button after
|
addonBefore Display content inside the button before
|
altText* A short description for an alternate way to carry out the action. For screen reader users who will not be able to navigate to the button easily/quickly.
|
appearance Control the appearance by selecting between the different button types.
Default: |
asChild Change the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
className
|
disabled Whether the button is disabled.
|
icon Display an icon before or after the button content or omit
|
iconPosition Control whether to show the icon before or after the button content.
|
loading Whether to show loading spinner inside the button.
|
size Control the size of the button.
Default: |
square Whether button should have square shape.
|
#
ToastProvider
#
Supports all Box props in addition to its own. Renders a <div>
element.
Prop |
---|
asChild Change the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
className
|
container
|
duration Time in milliseconds that each toast should remain visible for. @defaultValue 5000
|
hotkey The keys to use as the keyboard shortcut that will move focus to the toast viewport. @defaultValue [‘F8’]
|
label An author-localized label for each toast. Used to help screen reader users associate the interruption with a toast. @defaultValue ‘Notification’
|
position
Default: |
swipeDirection Direction of pointer swipe that should close the toast. @defaultValue ‘right’
|
swipeThreshold Distance in pixels that the swipe must pass before a close is triggered. @defaultValue 50
|
toaster An instance of toaster returned from the
|
#
Changelog
#
#
0.1.0
#
- Added component