@availity/mui-snackbar 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +61 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +60 -0
- package/dist/index.mjs +26 -0
- package/introduction.stories.mdx +7 -0
- package/jest.config.js +7 -0
- package/package.json +52 -0
- package/project.json +41 -0
- package/src/index.ts +1 -0
- package/src/lib/Snackbar.stories.tsx +265 -0
- package/src/lib/Snackbar.test.tsx +9 -0
- package/src/lib/Snackbar.tsx +10 -0
- package/tsconfig.json +5 -0
- package/tsconfig.spec.json +10 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
|
+
|
|
5
|
+
## 0.1.0 (2024-10-15)
|
|
6
|
+
|
|
7
|
+
### Dependency Updates
|
|
8
|
+
|
|
9
|
+
* `mui-alert` updated to version `0.1.0`
|
|
10
|
+
* `mui-button` updated to version `0.1.0`
|
|
11
|
+
* `mui-layout` updated to version `0.1.0`
|
|
12
|
+
* `mui-transitions` updated to version `0.1.0`
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
* **mui-snackbar:** add snackbar component ([a8e5e8f](https://github.com/Availity/element/commit/a8e5e8f64c0b810ecbd3e2099d8a6354e3c005bc))
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# @availity/mui-snackbar
|
|
2
|
+
|
|
3
|
+
> Availity MUI Snackbar component to be used with @availity/element design system.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@availity/mui-snackbar)
|
|
6
|
+
[](https://www.npmjs.com/package/@availity/mui-snackbar)
|
|
7
|
+
[](https://github.com/Availity/element/blob/main/packages/mui-snackbar/package.json)
|
|
8
|
+
|
|
9
|
+
## Documentation
|
|
10
|
+
|
|
11
|
+
This package extends the MUI Snackbar component: [MUI Snackbar Docs](https://mui.com/components/snackbar/)
|
|
12
|
+
|
|
13
|
+
Live demo and documentation in our [Storybook](https://availity.github.io/element/?path=/docs/components-snackbar-introduction--docs)
|
|
14
|
+
|
|
15
|
+
Availity standards for design and usage can be found in the [Availity Design Guide](https://zeroheight.com/2e36e50c7)
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
### Import Through @availity/element (Recommended)
|
|
20
|
+
|
|
21
|
+
#### NPM
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @availity/element
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
#### Yarn
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
yarn add @availity/element
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Direct Import
|
|
34
|
+
|
|
35
|
+
#### NPM
|
|
36
|
+
|
|
37
|
+
_This package has a few peer dependencies. Add `@mui/material` & `@emotion/react` to your project if not already installed._
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install @availity/mui-snackbar
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### Yarn
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
yarn add @availity/mui-snackbar
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Usage
|
|
50
|
+
|
|
51
|
+
#### Import through @availity/element
|
|
52
|
+
|
|
53
|
+
```tsx
|
|
54
|
+
import { Snackbar } from '@availity/element';
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### Direct import
|
|
58
|
+
|
|
59
|
+
```tsx
|
|
60
|
+
import { Snackbar } from '@availity/mui-snackbar';
|
|
61
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { SnackbarProps as SnackbarProps$1, SnackbarCloseReason as SnackbarCloseReason$1 } from '@mui/material/Snackbar';
|
|
2
|
+
|
|
3
|
+
type SnackbarProps = Omit<SnackbarProps$1, 'action' | 'ContentProps' | 'message' | 'TransitionComponent' | 'transitionDuration'>;
|
|
4
|
+
type SnackbarCloseReason = SnackbarCloseReason$1;
|
|
5
|
+
declare const Snackbar: (props: SnackbarProps) => JSX.Element;
|
|
6
|
+
|
|
7
|
+
export { Snackbar, type SnackbarCloseReason, type SnackbarProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { SnackbarProps as SnackbarProps$1, SnackbarCloseReason as SnackbarCloseReason$1 } from '@mui/material/Snackbar';
|
|
2
|
+
|
|
3
|
+
type SnackbarProps = Omit<SnackbarProps$1, 'action' | 'ContentProps' | 'message' | 'TransitionComponent' | 'transitionDuration'>;
|
|
4
|
+
type SnackbarCloseReason = SnackbarCloseReason$1;
|
|
5
|
+
declare const Snackbar: (props: SnackbarProps) => JSX.Element;
|
|
6
|
+
|
|
7
|
+
export { Snackbar, type SnackbarCloseReason, type SnackbarProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __export = (target, all) => {
|
|
23
|
+
for (var name in all)
|
|
24
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
25
|
+
};
|
|
26
|
+
var __copyProps = (to, from, except, desc) => {
|
|
27
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
28
|
+
for (let key of __getOwnPropNames(from))
|
|
29
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
30
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
31
|
+
}
|
|
32
|
+
return to;
|
|
33
|
+
};
|
|
34
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
35
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
36
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
37
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
38
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
39
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
40
|
+
mod
|
|
41
|
+
));
|
|
42
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
43
|
+
|
|
44
|
+
// src/index.ts
|
|
45
|
+
var src_exports = {};
|
|
46
|
+
__export(src_exports, {
|
|
47
|
+
Snackbar: () => Snackbar
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(src_exports);
|
|
50
|
+
|
|
51
|
+
// src/lib/Snackbar.tsx
|
|
52
|
+
var import_Snackbar = __toESM(require("@mui/material/Snackbar"));
|
|
53
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
54
|
+
var Snackbar = (props) => {
|
|
55
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Snackbar.default, __spreadValues({}, props));
|
|
56
|
+
};
|
|
57
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
58
|
+
0 && (module.exports = {
|
|
59
|
+
Snackbar
|
|
60
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/lib/Snackbar.tsx
|
|
19
|
+
import MuiSnackbar from "@mui/material/Snackbar";
|
|
20
|
+
import { jsx } from "react/jsx-runtime";
|
|
21
|
+
var Snackbar = (props) => {
|
|
22
|
+
return /* @__PURE__ */ jsx(MuiSnackbar, __spreadValues({}, props));
|
|
23
|
+
};
|
|
24
|
+
export {
|
|
25
|
+
Snackbar
|
|
26
|
+
};
|
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@availity/mui-snackbar",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Availity MUI Snackbar Component - part of the @availity/element design system",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"typescript",
|
|
8
|
+
"availity",
|
|
9
|
+
"mui"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://availity.github.io/element/?path=/docs/components-snackbar-introduction--docs",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/Availity/element/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/Availity/element.git",
|
|
18
|
+
"directory": "packages/snackbar"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Availity Developers <AVOSS@availity.com>",
|
|
22
|
+
"browser": "./dist/index.js",
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"module": "./dist/index.mjs",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
28
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
29
|
+
"clean": "rm -rf dist",
|
|
30
|
+
"clean:nm": "rm -rf node_modules",
|
|
31
|
+
"publish": "yarn npm publish --tolerate-republish --access public",
|
|
32
|
+
"publish:canary": "yarn npm publish --access public --tag canary"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@availity/mui-alert": "0.6.0",
|
|
36
|
+
"@availity/mui-button": "0.6.10",
|
|
37
|
+
"@availity/mui-layout": "0.1.6",
|
|
38
|
+
"@availity/mui-transitions": "0.2.4",
|
|
39
|
+
"@mui/material": "^5.15.15",
|
|
40
|
+
"react": "18.2.0",
|
|
41
|
+
"react-dom": "18.2.0",
|
|
42
|
+
"tsup": "^8.0.2",
|
|
43
|
+
"typescript": "^5.4.5"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@mui/material": "^5.11.9",
|
|
47
|
+
"react": ">=16.3.0"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/project.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mui-snackbar",
|
|
3
|
+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "packages/snackbar/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"tags": [],
|
|
7
|
+
"targets": {
|
|
8
|
+
"lint": {
|
|
9
|
+
"executor": "@nx/eslint:lint",
|
|
10
|
+
"options": {
|
|
11
|
+
"eslintConfig": ".eslintrc.json",
|
|
12
|
+
"silent": false,
|
|
13
|
+
"fix": false,
|
|
14
|
+
"cache": true,
|
|
15
|
+
"cacheLocation": "./node_modules/.cache/snackbar/.eslintcache",
|
|
16
|
+
"maxWarnings": -1,
|
|
17
|
+
"quiet": false,
|
|
18
|
+
"noEslintrc": false,
|
|
19
|
+
"hasTypeAwareRules": true,
|
|
20
|
+
"cacheStrategy": "metadata"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"test": {
|
|
24
|
+
"executor": "@nx/jest:jest",
|
|
25
|
+
"outputs": ["{workspaceRoot}/coverage/snackbar"],
|
|
26
|
+
"options": {
|
|
27
|
+
"jestConfig": "packages/snackbar/jest.config.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"version": {
|
|
31
|
+
"executor": "@jscutlery/semver:version",
|
|
32
|
+
"options": {
|
|
33
|
+
"preset": "conventional",
|
|
34
|
+
"commitMessageFormat": "chore({projectName}): release version ${version} [skip ci]",
|
|
35
|
+
"tagPrefix": "@availity/{projectName}@",
|
|
36
|
+
"trackDeps": true,
|
|
37
|
+
"skipCommitTypes": ["docs"]
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lib/Snackbar';
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// Each exported component in the package should have its own stories file
|
|
2
|
+
|
|
3
|
+
import { SyntheticEvent, useEffect, useState } from 'react';
|
|
4
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
5
|
+
import { Alert, AlertTitle } from '@availity/mui-alert';
|
|
6
|
+
import { Button } from '@availity/mui-button';
|
|
7
|
+
import { Box, Grid } from '@availity/mui-layout';
|
|
8
|
+
import { Collapse } from '@availity/mui-transitions';
|
|
9
|
+
import { Link } from '@mui/material';
|
|
10
|
+
import { Snackbar, SnackbarProps, SnackbarCloseReason } from './Snackbar';
|
|
11
|
+
import { styled } from '@mui/material/styles';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Snackbars (also known as toasts) are used for brief notifications of processes that have been or will be performed.
|
|
15
|
+
*
|
|
16
|
+
* Availity does not currently use the Material `SnackbarContent` component for basic messages,
|
|
17
|
+
* instead every `Snackbar` should contain an `Alert` to convey messages.
|
|
18
|
+
*/
|
|
19
|
+
const meta: Meta<typeof Snackbar> = {
|
|
20
|
+
title: 'Components/Snackbar/Snackbar',
|
|
21
|
+
component: Snackbar,
|
|
22
|
+
tags: ['autodocs'],
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default meta;
|
|
26
|
+
|
|
27
|
+
const SnackbarStoryPreview = styled("div", {
|
|
28
|
+
name: 'MuiSnackbar',
|
|
29
|
+
slot: 'Root',
|
|
30
|
+
overridesResolver: (props, styles) => styles.root,
|
|
31
|
+
})(({}));
|
|
32
|
+
|
|
33
|
+
export const _Snackbar: StoryObj<typeof Snackbar> = {
|
|
34
|
+
render: (args: SnackbarProps) => {
|
|
35
|
+
const [open, setOpen] = useState(false);
|
|
36
|
+
|
|
37
|
+
const handleClick = () => {
|
|
38
|
+
setOpen(true);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const handleClose = (
|
|
42
|
+
event: SyntheticEvent | Event,
|
|
43
|
+
reason?: SnackbarCloseReason,
|
|
44
|
+
) => {
|
|
45
|
+
if (reason === 'clickaway') {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
setOpen(false);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const action = (
|
|
53
|
+
<Link component="button" onClick={handleClose}>
|
|
54
|
+
Action
|
|
55
|
+
</Link>
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<Box sx={{minHeight: '200px'}}>
|
|
60
|
+
<Button onClick={handleClick}>Open Snackbar</Button>
|
|
61
|
+
<Snackbar
|
|
62
|
+
{...args}
|
|
63
|
+
open={open}
|
|
64
|
+
>
|
|
65
|
+
<Alert icon={false} onClose={handleClose} action={action} severity='info'>Alert Text</Alert>
|
|
66
|
+
</Snackbar>
|
|
67
|
+
</Box>
|
|
68
|
+
);
|
|
69
|
+
},
|
|
70
|
+
args: {
|
|
71
|
+
anchorOrigin: { vertical: 'bottom', horizontal: 'left'},
|
|
72
|
+
autoHideDuration: 6000,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const _Actions: StoryObj<typeof Snackbar> = {
|
|
77
|
+
render: () => {
|
|
78
|
+
const [visible, setVisible] = useState(true);
|
|
79
|
+
|
|
80
|
+
const onClose = () => {
|
|
81
|
+
setVisible(false);
|
|
82
|
+
setTimeout(() => setVisible(true), 1000);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const actionButton = <Link component="button" onClick={onClose}>Action Button</Link>;
|
|
86
|
+
|
|
87
|
+
const actionLink = <Link href='#' >Action Link</Link>;
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<Grid container spacing={1} maxWidth="620px">
|
|
91
|
+
<Grid>
|
|
92
|
+
<Collapse in={visible}>
|
|
93
|
+
<SnackbarStoryPreview>
|
|
94
|
+
<Alert severity="info" icon={false}>
|
|
95
|
+
No Action, No Close
|
|
96
|
+
</Alert>
|
|
97
|
+
</SnackbarStoryPreview>
|
|
98
|
+
</Collapse>
|
|
99
|
+
</Grid>
|
|
100
|
+
<Grid>
|
|
101
|
+
<Collapse in={visible}>
|
|
102
|
+
<SnackbarStoryPreview>
|
|
103
|
+
<Alert severity="info" icon={false} onClose={onClose}>
|
|
104
|
+
Close
|
|
105
|
+
</Alert>
|
|
106
|
+
</SnackbarStoryPreview>
|
|
107
|
+
</Collapse>
|
|
108
|
+
</Grid>
|
|
109
|
+
<Grid>
|
|
110
|
+
<Collapse in={visible}>
|
|
111
|
+
<SnackbarStoryPreview>
|
|
112
|
+
<Alert severity="info" icon={false} action={actionButton}>
|
|
113
|
+
Action Button
|
|
114
|
+
</Alert>
|
|
115
|
+
</SnackbarStoryPreview>
|
|
116
|
+
</Collapse>
|
|
117
|
+
</Grid>
|
|
118
|
+
<Grid>
|
|
119
|
+
<Collapse in={visible}>
|
|
120
|
+
<SnackbarStoryPreview>
|
|
121
|
+
<Alert severity="info" icon={false} action={actionLink}>
|
|
122
|
+
Action Link
|
|
123
|
+
</Alert>
|
|
124
|
+
</SnackbarStoryPreview>
|
|
125
|
+
</Collapse>
|
|
126
|
+
</Grid>
|
|
127
|
+
<Grid>
|
|
128
|
+
<Collapse in={visible}>
|
|
129
|
+
<SnackbarStoryPreview>
|
|
130
|
+
<Alert severity="info" icon={false} onClose={onClose} action={actionButton}>
|
|
131
|
+
Action Button & Close
|
|
132
|
+
</Alert>
|
|
133
|
+
</SnackbarStoryPreview>
|
|
134
|
+
</Collapse>
|
|
135
|
+
</Grid>
|
|
136
|
+
</Grid>
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export const _Alerts: StoryObj<typeof Snackbar> = {
|
|
142
|
+
render: () => {
|
|
143
|
+
const [visible, setVisible] = useState(true);
|
|
144
|
+
|
|
145
|
+
const onClose = () => {
|
|
146
|
+
setVisible(false);
|
|
147
|
+
setTimeout(() => setVisible(true), 1000);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
return (
|
|
151
|
+
<Grid container spacing={1} maxWidth="620px">
|
|
152
|
+
<Grid>
|
|
153
|
+
<Collapse in={visible}>
|
|
154
|
+
<SnackbarStoryPreview>
|
|
155
|
+
<Alert severity="info" onClose={onClose}>
|
|
156
|
+
<AlertTitle>Info Alert Snackbar</AlertTitle>
|
|
157
|
+
Additional text.
|
|
158
|
+
</Alert>
|
|
159
|
+
</SnackbarStoryPreview>
|
|
160
|
+
</Collapse>
|
|
161
|
+
</Grid>
|
|
162
|
+
<Grid>
|
|
163
|
+
<Collapse in={visible}>
|
|
164
|
+
<SnackbarStoryPreview>
|
|
165
|
+
<Alert severity="success" onClose={onClose}>
|
|
166
|
+
<AlertTitle>Success Alert Snackbar</AlertTitle>
|
|
167
|
+
Additional text.
|
|
168
|
+
</Alert>
|
|
169
|
+
</SnackbarStoryPreview>
|
|
170
|
+
</Collapse>
|
|
171
|
+
</Grid>
|
|
172
|
+
<Grid>
|
|
173
|
+
<Collapse in={visible}>
|
|
174
|
+
<SnackbarStoryPreview>
|
|
175
|
+
<Alert severity="warning" onClose={onClose}>
|
|
176
|
+
<AlertTitle>Warning Alert Snackbar</AlertTitle>
|
|
177
|
+
Additional text.
|
|
178
|
+
</Alert>
|
|
179
|
+
</SnackbarStoryPreview>
|
|
180
|
+
</Collapse>
|
|
181
|
+
</Grid>
|
|
182
|
+
<Grid>
|
|
183
|
+
<Collapse in={visible}>
|
|
184
|
+
<SnackbarStoryPreview>
|
|
185
|
+
<Alert severity="error" onClose={onClose}>
|
|
186
|
+
<AlertTitle>Error Alert Snackbar</AlertTitle>
|
|
187
|
+
Additional text. Don't make a snackbar more than a couple sentences!
|
|
188
|
+
</Alert>
|
|
189
|
+
</SnackbarStoryPreview>
|
|
190
|
+
</Collapse>
|
|
191
|
+
</Grid>
|
|
192
|
+
</Grid>
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
interface SnackbarMessage {
|
|
198
|
+
message: string;
|
|
199
|
+
key: number;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** This demo shows how to display multiple Snackbars without stacking them by using a consecutive animation. */
|
|
203
|
+
export const _ConsecutiveSnackbars: StoryObj<typeof Snackbar> = {
|
|
204
|
+
render: () => {
|
|
205
|
+
const [snackPack, setSnackPack] = useState<readonly SnackbarMessage[]>([]);
|
|
206
|
+
const [open, setOpen] = useState(false);
|
|
207
|
+
const [messageInfo, setMessageInfo] = useState<SnackbarMessage | undefined>(
|
|
208
|
+
undefined,
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
useEffect(() => {
|
|
212
|
+
if (snackPack.length && !messageInfo) {
|
|
213
|
+
// Set a new snack when we don't have an active one
|
|
214
|
+
setMessageInfo({ ...snackPack[0] });
|
|
215
|
+
setSnackPack((prev) => prev.slice(1));
|
|
216
|
+
setOpen(true);
|
|
217
|
+
} else if (snackPack.length && messageInfo && open) {
|
|
218
|
+
// Close an active snack when a new one is added
|
|
219
|
+
setOpen(false);
|
|
220
|
+
}
|
|
221
|
+
}, [snackPack, messageInfo, open]);
|
|
222
|
+
|
|
223
|
+
const handleClick = (message: string) => () => {
|
|
224
|
+
setSnackPack((prev) => [...prev, { message, key: new Date().getTime() }]);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const handleClose = (
|
|
228
|
+
event: SyntheticEvent | Event,
|
|
229
|
+
reason?: SnackbarCloseReason,
|
|
230
|
+
) => {
|
|
231
|
+
if (reason === 'clickaway') {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
setOpen(false);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const handleExited = () => {
|
|
238
|
+
setMessageInfo(undefined);
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
return (
|
|
242
|
+
<Box minHeight="250px" gap="8px">
|
|
243
|
+
<Grid container spacing={2}>
|
|
244
|
+
<Grid xs={12}>
|
|
245
|
+
<Button onClick={handleClick('Message A')}>Show message A</Button>
|
|
246
|
+
</Grid>
|
|
247
|
+
<Grid xs={12}>
|
|
248
|
+
<Button onClick={handleClick('Message B')}>Show message B</Button>
|
|
249
|
+
</Grid>
|
|
250
|
+
</Grid>
|
|
251
|
+
<Snackbar
|
|
252
|
+
key={messageInfo ? messageInfo.key : undefined}
|
|
253
|
+
open={open}
|
|
254
|
+
autoHideDuration={6000}
|
|
255
|
+
onClose={handleClose}
|
|
256
|
+
TransitionProps={{ onExited: handleExited }}
|
|
257
|
+
>
|
|
258
|
+
<Alert severity="info" icon={false} onClose={handleClose}>
|
|
259
|
+
{messageInfo?.message}
|
|
260
|
+
</Alert>
|
|
261
|
+
</Snackbar>
|
|
262
|
+
</Box>
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { render } from '@testing-library/react';
|
|
2
|
+
import { Snackbar } from './Snackbar';
|
|
3
|
+
|
|
4
|
+
describe('Snackbar', () => {
|
|
5
|
+
test('should render successfully', () => {
|
|
6
|
+
const { getByText } = render(<Snackbar open><div>Test</div></Snackbar>);
|
|
7
|
+
expect(getByText('Test')).toBeTruthy();
|
|
8
|
+
});
|
|
9
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// For bundling purposes, always use the direct import for an mui component, i.e. '@mui/material/xxx'
|
|
2
|
+
import MuiSnackbar, { SnackbarProps as MuiSnackbarProps, SnackbarCloseReason as MuiSnackbarCloseReason } from '@mui/material/Snackbar';
|
|
3
|
+
|
|
4
|
+
export type SnackbarProps = Omit<MuiSnackbarProps, 'action' | 'ContentProps' | 'message' | 'TransitionComponent' | 'transitionDuration'>;
|
|
5
|
+
|
|
6
|
+
export type SnackbarCloseReason = MuiSnackbarCloseReason;
|
|
7
|
+
|
|
8
|
+
export const Snackbar = (props: SnackbarProps): JSX.Element => {
|
|
9
|
+
return <MuiSnackbar {...props}/>;
|
|
10
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "../../dist/out-tsc",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"types": ["jest", "node", "@testing-library/jest-dom"],
|
|
7
|
+
"allowJs": true
|
|
8
|
+
},
|
|
9
|
+
"include": ["**/*.test.js", "**/*.test.ts", "**/*.test.tsx", "**/*.d.ts"]
|
|
10
|
+
}
|