Snackbar
Properties
SnackbarProvider
Name | Type | Default | Description |
---|---|---|---|
zIndex | number | Tuỳ chỉnh z-index cho snackbar |
SnackbarContext
SnackbarProvider cung cấp các hooks
Name | Type | Default | Description |
---|---|---|---|
openSnackbar | function | Function để trigger tạo mới và mở một Snackbar. Có thể mở Snackbar với các SnackbarOptions | |
closeSnackbar | function | Function để đóng một Snackbar, có thể sử dụng để đóng nhanh khi chưa hết thời gian hiển thị | |
setDownloadProgress | function | Set phần trăm tiến trình download (0 - 100) khi type="download" |
SnackbarAction
Name | Type | Default | Description |
---|---|---|---|
close | boolean | Action close | |
text | string | Phần text hiển thị cho action | |
onClick | function | Thêm event handler cho click event khi user nhấn vào action |
SnackbarOptions
Name | Type | Default | Description |
---|---|---|---|
position | SnackbarPosition | Vị trí snackbar | |
duration | number | Thời gian tồn tại của snackbar tính theo ms | |
text | string | Phần text hiển thị trên snackbar | |
type | SnackbarType | Loại snackbar | |
action | SnackbarAction | Thêm action cho snackbar | |
prefixIcon | ReactNode | Thêm lead icon cho snackbar | |
icon | boolean | Hiển thị icon cho snackbar trường hợp type default | |
verticalAction | boolean | Hiển thị action tại hàng mới, thường sử dụng khi action có độ dài text dài | |
zIndex | number | Tuỳ chỉnh z-index cho snackbar | |
onClose | function | Thêm event handler khi snackbar đóng |
Type
SnackbarPosition
Name | Description |
---|---|
"top" | Mở Snackbar phía trên |
"bottom" | Mở Snackbar phía dưới |
SnackbarType
Name | Description |
---|---|
"default" | Snackbar mặc định |
"success" | Snackbar dùng hiển thị khi thực hiện một tác vụ thành công |
"info" | Snackbar dùng để hiển thị thông tin |
"error" | Snackbar dùng để hiển thị phản hồi một tác vụ thực hiện bị lỗi |
"warning" | Snackbar dùng để đưa ra cảnh báo |
"loading" | Snackbar dùng để hiển thị trạng thái loading |
"download" | Snackbar hiển thị tiến trình download |
"countdown" | Snackbar hiển thị với countdowwn |
"wifi-connected" | Snackbar hiển thị khi network kết nối |
"wifi-disconnected" | Snackbar hiẻn thị khi network ngắt kết nối |
Example
import React from "react";
import { Route } from "react-router-dom";
import { SnackbarProvider, App, ZMPRouter, AnimationRoutes} from "zmp-ui";
import HomePage from "./HomePage";
export default () => {
return (
<App>
<SnackbarProvider>
<ZMPRouter>
<AnimationRoutes>
<Route path="/" element={<HomePage />} />
</AnimationRoutes>
</ZMPRouter>
</SnackbarProvider>
</App>
)
}
import React from "react";
import { useSnackbar, Button, Page } from "zmp-ui";
export default () => {
const { openSnackbar, setDownloadProgress, closeSnackbar } = useSnackbar();
const timerId = useRef();
useEffect(() => {
return () => {
closeSnackbar();
clearInterval(timerId.current);
};
}, []);
return (
<Page>
<Button
size="large"
onClick={() => {
let i = 0;
timerId.current = setInterval(() => {
if (i === 0) {
openSnackbar({
text: "download...",
type: "download",
duration: 10000000,
onClose() {
clearInterval(timerId.current);
},
});
}
if (i < 100) {
// eslint-disable-next-line no-plusplus
setDownloadProgress(++i);
}
if (i === 100) {
setTimeout(() => {
closeSnackbar();
clearInterval(timerId.current);
}, 1000);
}
}, 100);
}}
>
Download Snackbar
</Button>
<Button
size="large"
onClick={() => {
openSnackbar({
text: "Snackbar...",
action: {
text: "close",
close: true
}
});
}}
>
Download Default Snackbar
</Button>
</Page>
)
}