Demos
How to import
import { Toast } from '@fibrous/semi-ui-solid';
Basic Usage
Calling Toast's related methods to show timely feedback to user's operations. It is recommended to set the stack property to apply the stacking style to multiple Toasts on the same screen, and expand them by hovering, which can effectively prevent multiple parallel Toasts at one time.
Code
import { throttle } from 'es-toolkit';
import { Button, Toast } from '@fibrous/semi-ui-solid';
const opts = {
content: 'Hi, Bytedance dance dance',
duration: 3,
stack: true,
};
export default function ToastDemo() {
const throttled = throttle(
() =>
Toast.info({
content: 'Hi, Bytedance dance dance',
duration: 10,
onClose: () => throttled.cancel(),
stack: true,
}),
10000,
{ edges: ['leading'] }
);
return (
<div>
<Button onClick={() => Toast.info(opts)}>Display Toast</Button>
<br />
<br />
<Button onClick={throttled}>Throttled Toast</Button>
</div>
);
}Other Types
Use different methods to show different Toast including success, warning, error and info.
Code
import { Button, Toast } from '@fibrous/semi-ui-solid';
const opts = {
content: 'Hi, Bytedance dance dance',
duration: 3,
};
export default function ToastDemo() {
return (
<>
<Button style={{ color: 'var(--semi-color-success)' }} onClick={() => Toast.success('Hi,Bytedance dance dance')}>
Success
</Button>
<br />
<br />
<Button type="warning" onClick={() => Toast.warning(opts)}>
Warning
</Button>
<br />
<br />
<Button type="danger" onClick={() => Toast.error(opts)}>
Error
</Button>
</>
);
}Colored Background
You could use theme for a colored background style. Default is normal.
Code
import { Button, Toast } from '@fibrous/semi-ui-solid';
const opts = {
content: 'Hi, Bytedance dance dance',
duration: 3,
theme: 'light',
} as const;
export default function ToastDemo() {
return (
<>
<Button onClick={() => Toast.info(opts)}>Info</Button>
<br />
<br />
<Button style={{ color: 'var(--semi-color-success)' }} onClick={() => Toast.success(opts)}>
Success
</Button>
<br />
<br />
<Button type="warning" onClick={() => Toast.warning(opts)}>
Warning
</Button>
<br />
<br />
<Button type="danger" onClick={() => Toast.error(opts)}>
Error
</Button>
</>
);
}Stacking styles
With stack, the Toasts on screen stack once there are more than the threshold, and expand on hover. true sets the threshold to 3, and a number sets a custom one; a stack shows its front 3 layers only.
Code
import { Button, Toast } from '@fibrous/semi-ui-solid';
const opts = {
content: 'Hi, Bytedance dance dance',
duration: 10,
stack: true,
};
export default function ToastDemo() {
return <Button onClick={() => Toast.info(opts)}>多次点击</Button>;
}Custom Children with Link
Informational feedback
Code
import { Button, Toast, Typography } from '@fibrous/semi-ui-solid';
const { Text } = Typography;
// content as a function: the toast creates it when shown, under its own root
const opts = {
content: () => (
<span>
<Text>Hi, Bytedance dance dance</Text>
<Text link style={{ 'margin-left': '12px' }}>
更多
</Text>
</span>
),
duration: 3,
};
const multiLineOpts = {
content: () => (
<>
<div>Hi, Bytedance dance dance</div>
<div style={{ 'margin-top': '8px' }}>
<Text link>查看详情</Text>
<Text link style={{ 'margin-left': '20px' }}>
一会再看
</Text>
</div>
</>
),
duration: 3,
};
export default function ToastDemo() {
return (
<>
<Button onClick={() => Toast.info(opts)}>Display Toast</Button>
<br />
<br />
<Button onClick={() => Toast.info(multiLineOpts)}>Display Multi-line Toast</Button>
</>
);
}Delay
Use duration to set up time delay. By default it closes after 3 seconds.
Code
import { Button, Toast } from '@fibrous/semi-ui-solid';
const opts = {
content: 'Hi, Bytedance dance dance',
duration: 10,
};
export default function ToastDemo() {
return <Button onClick={() => Toast.info(opts)}>Close After 10s</Button>;
}Manual Close
Set duration to 0 if you do not want the Toast to close by itself. In this case, it could only be closed manually.
Code
import { createSignal } from 'solid-js';
import { Button, Toast } from '@fibrous/semi-ui-solid';
export default function ToastDemo() {
const [toastId, setToastId] = createSignal<string | null>(null);
const show = () => {
if (toastId()) {
return;
}
setToastId(Toast.info({ content: 'Not auto close', duration: 0, onClose: () => setToastId(null) }));
};
const hide = () => {
const id = toastId();
if (id) {
Toast.close(id);
}
setToastId(null);
};
return (
<>
<Button type="primary" onClick={show}>
Show Toast
</Button>
<br />
<br />
<Button type="primary" onClick={hide}>
Hide Toast
</Button>
</>
);
}Update Toast Content
Use unique Toast id to update toast content.
Code
import { Button, Toast } from '@fibrous/semi-ui-solid';
export default function ToastDemo() {
const show = () => {
const id = 'toastid';
Toast.info({ content: 'Update Content By Id', id });
setTimeout(() => {
Toast.success({ content: 'Id By Content Update', id });
}, 1000);
};
return (
<Button type="primary" onClick={show}>
Update Content By Id
</Button>
);
}Destroy all
Globally Destroy:
Toast.destroyAll()
Consume Context
You could use Toast.useToast() to create a contextHolder that could access context. Created toast will be inserted to where contextHolder is placed and inherits the context, ConfigProvider and locale there. The holder is created under the owner that calls useToast(); place it only once.
Toast.useToast() returns the tuple [toast, contextHolder]: info, success, warning, error and open of the toast object take the Options without type, id, zIndex and getPopupContainer and return the toastId; close(toastId) closes one manually. An element passed as content or icon is created inside an event handler, which has no owner and cannot read context; pass a function instead, which is called where the toast renders. The values of ConfigProvider and LocaleProvider cannot be read by the static Toast; use useToast() when you need them.
Code
import { createContext, useContext } from 'solid-js';
import { Button, Toast } from '@fibrous/semi-ui-solid';
const ReachableContext = createContext<string>();
export default function ToastDemo() {
const [toast, contextHolder] = Toast.useToast();
return (
<ReachableContext.Provider value="Light">
<div>
<Button
onClick={() => {
// A function is called where the toast renders, so it reads the context around contextHolder
toast.success({ duration: 0, content: () => `ReachableContext: ${useContext(ReachableContext)}` });
}}
>
Hook Toast
</Button>
</div>
{contextHolder}
</ReachableContext.Provider>
);
}Create Toast with different configurations
ToastFactory.create(config) => Toast
If you need Toast with different configs in your application, you can use ToastFactory.create(config)to create a new Toast:
Code
import { onCleanup } from 'solid-js';
import { Button, Toast, ToastFactory } from '@fibrous/semi-ui-solid';
const ToastInCustomContainer = ToastFactory.create({
getPopupContainer: () => document.getElementById('custom-toast-container') as HTMLElement,
});
export default function ToastDemo() {
onCleanup(() => ToastInCustomContainer.destroyAll());
return (
<div>
<Button onClick={() => Toast.info('Toast')}>Default Toast</Button>
<br />
<br />
<Button onClick={() => ToastInCustomContainer.info('Toast in some container')}>Toast in custom container</Button>
<div id="custom-toast-container">custom container</div>
</div>
);
}API Reference
The static methods provided are as follows: Display: You can pass in options object or string directly. Methods return the value of toastId: const toastId = Toast.info({ /*...options*/ })
The global configuration is set before any method call, and takes effect only once
Toast.config(config)Toast.resetConfig()restores the global configuration to its defaults; only toasts shown afterwards are affected. An instance created byToastFactory.create(config)is restored to theconfigit was created with
Show Toast Directly
Toast.info(options || string)Toast.error(options || string)Toast.warning(options || string)Toast.success(options || string)
info error warning success return the toastId, can be used for manually closing
Toast.close(toastId)Close Manually
Options
Toast Options supports the following APIs as well as the APIs in Config
| Property | Description | Type | Default |
|---|---|---|---|
| content | Toast content, an element or a function returning one; the function is called where the toast renders, so with Toast.useToast() it can read the context around the holder | JSX.Element | (() => JSX.Element) | '' |
| icon | Custom icon, an element or a function returning one, replacing the icon of type; a Semi icon without a size of its own is sized large | JSX.Element | (() => JSX.Element) | - |
| showClose | Whether to show the close button | boolean | true |
| textMaxWidth | Maximum width of the content; a number is in px | number | string | 450 |
| onClose | Callback function when the toast closes | () => void | - |
| stack | Stack the toasts on screen once there are more than this many; true means 3, false never stacks | boolean | number | false |
| id | Custom ToastId; a toast with the same id is updated | string | - |
| motion | Whether to animate entering and leaving | boolean | true |
| class | Class name | string | - |
| style | Inline style | JSX.CSSProperties | string | - |
class, style and undeclared options (such as data-* and aria-*) land on .semi-toast, in stack mode too.
Config
The following API supports global configuration to change the default configuration of the current Toast
| Properties | Instructions | type | Default |
|---|---|---|---|
| bottom | Pop-up position bottom | number | string | - |
| left | Pop-up position left | number | string | - |
| right | Pop-up position right | number | string | - |
| top | Pop-up position top | number | string | - |
| zIndex | Z-index value | number | 1010 |
| theme | Style of background fill, one of light, normal | string | normal |
| duration | Automatic close delay, no auto-close when set to 0 | number | 3 |
| getPopupContainer | Specifies the parent DOM, and the bullet layer will be rendered to the DOM, you need to set container and inner .semi-toast-wrapper 'position: relative` This will change the DOM tree position, but not the view's rendering position. | () => HTMLElement | () => document.body |
Accessibility
ARIA
- The role of Toast depends on its type:
alertforwarninganderror,statusforinfo,successand a Toast withouttype statusToasts are read by a persistentaria-live="polite"region owned by the list; the Toast itself isaria-live="off", and only the text ofcontentis announced.warninganderrorstill announce themselves asalert
Keyboard and Focus
- The close button is a native
button: focus it withTab, and activate it withEnterorSpace - A Toast does not take focus when it appears
- When the pointer or keyboard focus enters a Toast, its auto-close timer pauses and a stack expands; both resume when it leaves
Content Guidelines
- Keep it simple
- Do not use periods at the end of sentences
- Explain using the noun + verb format
| ✅ Recommended usage | ❌ Deprecated usage |
|---|---|
| Language added | New language has been added successfully |
| Ticket transfer failed | Can't transfer ticket |
- Provide prompt message for action
- only provide one action
- Don't use actions like "read" like OK, Got it, Dismiss, Cancel
| ✅ Recommended usage | ❌ Deprecated usage |
|---|---|
Ticket transfer failed Retry | Ticket transfer failed Dismiss |
Design Tokens
| Sass variable | Value | Usage |
|---|---|---|
| $animation_duration-toast-show | 300ms | |
| $animation_duration-toast-hide | 300ms | |
| $animation_function-toast-show | cubic-bezier(0.22, 0.57, 0.02, 1.2) | |
| $animation_function-toast-hide | cubic-bezier(0.22, 0.57, 0.02, 1.2) | |
| $animation_delay-toast-show | 0s | |
| $animation_delay-toast-hide | 0s | |
| $animation_opacity-toast-show | 0 | |
| $animation_opacity-toast-hide | 0 | |
| $animation_transform_translateY-toast-show | -100% | |
| $animation_transform_translateY-toast-hide | -100% | |
| $animation_duration-toast-stack | 300ms | |
| $animation_function-toast-stack | cubic-bezier(0.22, 0.57, 0.02, 1.2) | |
| $color-toast-bg-default | var(--semi-color-bg-3) | 提示背景颜色 - 默认 |
| $color-toast-text-default | var(--semi-color-text-0) | 提示文字颜色 - 默认 |
| $color-toast_warning-icon | var(--semi-color-warning) | 警告提示图标颜色 |
| $color-toast_success-icon | var(--semi-color-success) | 成功提示图标颜色 |
| $color-toast_info-icon | var(--semi-color-info) | 通知提示图标颜色 |
| $color-toast_danger-icon | var(--semi-color-danger) | 错误提示图标颜色 |
| $color-toast_warning_light-bg | var(--semi-color-warning-light-default) | 多色样式 警告提示背景颜色 |
| $color-toast_warning_light-icon | $color-toast_warning-icon | 多色样式 警告提示图标颜色 |
| $color-toast_warning_light-border | var(--semi-color-warning) | 多色样式 警告提示描边颜色 |
| $color-toast_success_light-bg | var(--semi-color-success-light-default) | 多色样式 成功提示背景颜色 |
| $color-toast_success_light-icon | $color-toast_success-icon | 多色样式 成功提示图标颜色 |
| $color-toast_success_light-border | var(--semi-color-success) | 多色样式 成功提示描边颜色 |
| $color-toast_info_light-bg | var(--semi-color-info-light-default) | 多色样式 通知提示背景颜色 |
| $color-toast_info_light-icon | $color-toast_info-icon | 多色样式 通知提示图标颜色 |
| $color-toast_info_light-border | var(--semi-color-info) | 多色样式 通知提示描边颜色 |
| $color-toast_danger_light-bg | var(--semi-color-danger-light-default) | 多色样式 错误提示背景颜色 |
| $color-toast_danger_light-icon | $color-toast_danger-icon | 多色样式 错误提示图标颜色 |
| $color-toast_danger_light-border | var(--semi-color-danger) | 多色样式 错误提示描边颜色 |
| $spacing-toast_wrapper-top | 0 | 通知容器顶部位置 |
| $spacing-toast_content-paddingY | $spacing-base-tight | 通知内容垂直内边距 |
| $spacing-toast_content-paddingX | $spacing-tight | 通知内容水平内边距 |
| $spacing-toast_content-paddingTop | $spacing-toast_content-paddingY | 通知内容 top 内边距 |
| $spacing-toast_content-paddingBottom | $spacing-toast_content-paddingY | 通知内容 bottom 内边距 |
| $spacing-toast_content-paddingLeft | $spacing-toast_content-paddingX | 通知内容 left 内边距 |
| $spacing-toast_content-paddingRight | $spacing-toast_content-paddingX | 通知内容 right 内边距 |
| $spacing-toast_content-margin | $spacing-base-tight | 通知内容外边距 |
| $spacing-toast_content_close_btn-marginTop | -2px | 通知关闭按钮顶部外边距 |
| $spacing-toast_content_text-marginLeft | $spacing-base-tight | 通知文本左侧外边距 |
| $spacing-toast_content_text-marginRight | $spacing-base-tight | 通知文本右侧外边距 |
| $spacing-toast-perspective-originY | 280px | 通知透视原点 Y 轴位置 |
| $spacing-toast-perspective | 280px | 通知透视距离 |
| $width-toast_wrapper | 100% | 通知容器整体宽度 |
| $width-toast_light-border | 1px | 多色样式 通知描边宽度 |
| $radius-toast_content | var(--semi-border-radius-medium) | 通知圆角 |
| $font-toast_content-fontWeight | $font-weight-bold | 通知文本字重 |