@codejq/react-simple-snackbar 1.2.0 → 1.2.1
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/README.md +35 -60
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
<p align="center">
|
|
6
6
|
<a
|
|
7
|
-
href="https://www.npmjs.com/package/react-simple-snackbar"
|
|
7
|
+
href="https://www.npmjs.com/package/@codejq/react-simple-snackbar"
|
|
8
8
|
title="NPM Version"
|
|
9
9
|
target="blank"
|
|
10
10
|
>
|
|
11
11
|
<img
|
|
12
|
-
src="https://img.shields.io/npm/v/react-simple-snackbar"
|
|
12
|
+
src="https://img.shields.io/npm/v/@codejq/react-simple-snackbar"
|
|
13
13
|
alt="NPM Version"
|
|
14
14
|
/>
|
|
15
15
|
</a>
|
|
@@ -23,16 +23,6 @@
|
|
|
23
23
|
alt="CI Status"
|
|
24
24
|
/>
|
|
25
25
|
</a>
|
|
26
|
-
<a
|
|
27
|
-
href="https://coveralls.io/github/codejq/react-simple-snackbar?branch=master"
|
|
28
|
-
title="Coverage Status"
|
|
29
|
-
target="blank"
|
|
30
|
-
>
|
|
31
|
-
<img
|
|
32
|
-
src="https://coveralls.io/repos/github/codejq/react-simple-snackbar/badge.svg?branch=master"
|
|
33
|
-
alt="Coverage Status"
|
|
34
|
-
/>
|
|
35
|
-
</a>
|
|
36
26
|
<a href="#" title="Gzipped size">
|
|
37
27
|
<img
|
|
38
28
|
src="https://img.badgesize.io/codejq/react-simple-snackbar/master/dist/index.js.svg?compression=gzip"
|
|
@@ -51,15 +41,19 @@
|
|
|
51
41
|
target="blank"
|
|
52
42
|
>
|
|
53
43
|
<img
|
|
54
|
-
src="https://img.shields.io/npm/l/@
|
|
44
|
+
src="https://img.shields.io/npm/l/@codejq/react-simple-snackbar.svg"
|
|
55
45
|
alt="MIT License"
|
|
56
46
|
/>
|
|
57
47
|
</a>
|
|
58
48
|
</p>
|
|
59
49
|
|
|
60
|
-
> **This is the actively maintained fork** of the original `react-simple-snackbar` by [@evandromacedo](https://github.com/evandromacedo). The original repository is no longer maintained.
|
|
50
|
+
> **This is the actively maintained fork** of the original `react-simple-snackbar` by [@evandromacedo](https://github.com/evandromacedo). The original repository is no longer maintained. This fork adds React 16.8–19 support, a modernized UI, new positions, per-call background color, and automated publishing.
|
|
51
|
+
|
|
52
|
+
## Live Demo
|
|
61
53
|
|
|
62
|
-
|
|
54
|
+
**[codejq.github.io/react-simple-snackbar](https://codejq.github.io/react-simple-snackbar/)**
|
|
55
|
+
|
|
56
|
+
The demo page lets you interactively try every position, custom color, custom duration, and styling option in real time.
|
|
63
57
|
|
|
64
58
|
## Getting Started
|
|
65
59
|
|
|
@@ -81,7 +75,7 @@ yarn add @codejq/react-simple-snackbar
|
|
|
81
75
|
|
|
82
76
|
### Basic Usage
|
|
83
77
|
|
|
84
|
-
First,
|
|
78
|
+
First, wrap your application in a `SnackbarProvider`:
|
|
85
79
|
|
|
86
80
|
```jsx
|
|
87
81
|
// App.js
|
|
@@ -98,9 +92,9 @@ export default function App() {
|
|
|
98
92
|
}
|
|
99
93
|
```
|
|
100
94
|
|
|
101
|
-
Then
|
|
95
|
+
Then use it in any descendant component:
|
|
102
96
|
|
|
103
|
-
#### 1. `useSnackbar()` hook
|
|
97
|
+
#### 1. `useSnackbar()` hook (function components)
|
|
104
98
|
|
|
105
99
|
<!-- prettier-ignore -->
|
|
106
100
|
```jsx
|
|
@@ -124,7 +118,7 @@ export default function SomeChildComponent() {
|
|
|
124
118
|
}
|
|
125
119
|
```
|
|
126
120
|
|
|
127
|
-
#### 2. `withSnackbar()` HoC
|
|
121
|
+
#### 2. `withSnackbar()` HoC (class components)
|
|
128
122
|
|
|
129
123
|
<!-- prettier-ignore -->
|
|
130
124
|
```jsx
|
|
@@ -156,28 +150,26 @@ export default withSnackbar(SomeChildComponent)
|
|
|
156
150
|
|
|
157
151
|
### Methods
|
|
158
152
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
```js noLines
|
|
153
|
+
```js
|
|
162
154
|
const [openSnackbar, closeSnackbar] = useSnackbar()
|
|
163
155
|
|
|
164
156
|
// You can also give different names as you wish
|
|
165
157
|
const [open, close] = useSnackbar()
|
|
166
158
|
```
|
|
167
159
|
|
|
168
|
-
Or
|
|
160
|
+
Or as props on components wrapped in `withSnackbar()`:
|
|
169
161
|
|
|
170
|
-
```js
|
|
162
|
+
```js
|
|
171
163
|
const { openSnackbar, closeSnackbar } = this.props
|
|
172
164
|
```
|
|
173
165
|
|
|
174
166
|
#### `openSnackbar(node [, duration [, backgroundColor]])`
|
|
175
167
|
|
|
176
|
-
- **`node`**: the
|
|
168
|
+
- **`node`**: the content to show. Can be a string or any React node: `'Hello!'` or `<p>Some <strong>rich</strong> text</p>`.
|
|
177
169
|
|
|
178
|
-
- **`duration`**:
|
|
170
|
+
- **`duration`**: milliseconds before auto-close. Default: `6000`.
|
|
179
171
|
|
|
180
|
-
- **`backgroundColor`**:
|
|
172
|
+
- **`backgroundColor`**: optional CSS color string to override the background color for this specific call. Takes precedence over the `style.backgroundColor` option.
|
|
181
173
|
|
|
182
174
|
```jsx
|
|
183
175
|
// Basic
|
|
@@ -188,38 +180,42 @@ openSnackbar('Hello!', 3000)
|
|
|
188
180
|
|
|
189
181
|
// With custom duration and background color
|
|
190
182
|
openSnackbar('Error occurred', 5000, '#e53935')
|
|
183
|
+
|
|
184
|
+
// Success green
|
|
185
|
+
openSnackbar('Saved!', 4000, '#22c55e')
|
|
191
186
|
```
|
|
192
187
|
|
|
193
188
|
#### `closeSnackbar()`
|
|
194
189
|
|
|
195
|
-
|
|
190
|
+
Closes the snackbar programmatically. Takes no arguments.
|
|
196
191
|
|
|
197
192
|
### Options
|
|
198
193
|
|
|
199
|
-
|
|
194
|
+
Pass an options object to `useSnackbar([options])` or as the second argument to `withSnackbar(Component [, options])`.
|
|
200
195
|
|
|
201
196
|
#### Position
|
|
202
197
|
|
|
203
|
-
- **`position`**:
|
|
198
|
+
- **`position`**: where the snackbar appears. Default: `center`.
|
|
204
199
|
|
|
205
200
|
| Value | Description |
|
|
206
201
|
| --- | --- |
|
|
202
|
+
| `center` | True viewport center — scale-in animation **(default)** |
|
|
207
203
|
| `top-left` | Top of viewport, left-aligned |
|
|
208
204
|
| `top-center` | Top of viewport, centered |
|
|
209
205
|
| `top-right` | Top of viewport, right-aligned |
|
|
210
206
|
| `bottom-left` | Bottom of viewport, left-aligned |
|
|
211
|
-
| `bottom-center` | Bottom of viewport, centered
|
|
207
|
+
| `bottom-center` | Bottom of viewport, centered |
|
|
212
208
|
| `bottom-right` | Bottom of viewport, right-aligned |
|
|
213
209
|
|
|
214
210
|
#### Styling
|
|
215
211
|
|
|
216
|
-
- **`style`**: a [style object](https://reactjs.org/docs/dom-elements.html#style) with `camelCased` properties
|
|
212
|
+
- **`style`**: a [style object](https://reactjs.org/docs/dom-elements.html#style) with `camelCased` properties applied to the snackbar. Use `style.backgroundColor` to set a global background color.
|
|
217
213
|
|
|
218
|
-
- **`closeStyle`**: same as above,
|
|
214
|
+
- **`closeStyle`**: same as above, applied to the close button.
|
|
219
215
|
|
|
220
216
|
#### Full Example
|
|
221
217
|
|
|
222
|
-
```jsx
|
|
218
|
+
```jsx
|
|
223
219
|
const options = {
|
|
224
220
|
position: 'bottom-right',
|
|
225
221
|
style: {
|
|
@@ -249,15 +245,10 @@ withSnackbar(Component, options)
|
|
|
249
245
|
|
|
250
246
|
## Testing
|
|
251
247
|
|
|
252
|
-
> The snackbar itself is [already tested](https://github.com/codejq/react-simple-snackbar/tree/master/src/__tests__)
|
|
253
|
-
|
|
254
|
-
To test components that use Snackbar functionalities, there are some approaches as described below. These examples use [Jest](https://jestjs.io/) and [@testing-library/react](https://testing-library.com/docs/react-testing-library/intro/).
|
|
255
|
-
|
|
256
|
-
### Testing components that use `useSnackbar()` hook
|
|
248
|
+
> The snackbar itself is [already tested](https://github.com/codejq/react-simple-snackbar/tree/master/src/__tests__) — you don't need to worry about it.
|
|
257
249
|
|
|
258
|
-
|
|
250
|
+
### Testing components that use `useSnackbar()`
|
|
259
251
|
|
|
260
|
-
<!-- prettier-ignore -->
|
|
261
252
|
```jsx
|
|
262
253
|
// Component.test.js
|
|
263
254
|
import React from 'react'
|
|
@@ -265,7 +256,6 @@ import { render, fireEvent } from '@testing-library/react'
|
|
|
265
256
|
import * as Snackbar from '@codejq/react-simple-snackbar'
|
|
266
257
|
import Component from './Component'
|
|
267
258
|
|
|
268
|
-
// Mocks the open and close functions
|
|
269
259
|
const openSnackbarMock = jest.fn()
|
|
270
260
|
const closeSnackbarMock = jest.fn()
|
|
271
261
|
jest.spyOn(Snackbar, 'useSnackbar').mockImplementation(() => [openSnackbarMock, closeSnackbarMock])
|
|
@@ -273,36 +263,26 @@ jest.spyOn(Snackbar, 'useSnackbar').mockImplementation(() => [openSnackbarMock,
|
|
|
273
263
|
it('can test the openSnackbar and closeSnackbar functions', () => {
|
|
274
264
|
const { getByRole } = render(<Component />)
|
|
275
265
|
|
|
276
|
-
// Simulates click on some buttons that opens and closes the Snackbar
|
|
277
266
|
fireEvent.click(getByRole('button', { name: /open/i }))
|
|
278
267
|
fireEvent.click(getByRole('button', { name: /close/i }))
|
|
279
268
|
|
|
280
|
-
// Some examples of how you can test the mocks
|
|
281
|
-
expect(openSnackbarMock).toHaveBeenCalled()
|
|
282
269
|
expect(openSnackbarMock).toHaveBeenCalledTimes(1)
|
|
283
270
|
expect(openSnackbarMock).toHaveBeenCalledWith('This is the text of the Snackbar.')
|
|
284
|
-
expect(closeSnackbarMock).toHaveBeenCalled()
|
|
285
271
|
expect(closeSnackbarMock).toHaveBeenCalledTimes(1)
|
|
286
272
|
})
|
|
287
273
|
```
|
|
288
274
|
|
|
289
|
-
### Testing components wrapped in `withSnackbar()`
|
|
275
|
+
### Testing components wrapped in `withSnackbar()`
|
|
290
276
|
|
|
291
|
-
|
|
277
|
+
Export the component as both a named export (for testing) and a default export (wrapped):
|
|
292
278
|
|
|
293
279
|
```jsx
|
|
294
280
|
// Component.js
|
|
295
|
-
|
|
296
|
-
// (...)
|
|
297
|
-
// * Here goes all the component's code *
|
|
298
|
-
// (...)
|
|
299
|
-
|
|
300
|
-
// Named export for testing, and default export for using
|
|
301
281
|
export { Component }
|
|
302
282
|
export default withSnackbar(Component)
|
|
303
283
|
```
|
|
304
284
|
|
|
305
|
-
|
|
285
|
+
Then in tests, import the unwrapped component and pass mock props:
|
|
306
286
|
|
|
307
287
|
```jsx
|
|
308
288
|
// Component.test.js
|
|
@@ -317,15 +297,10 @@ it('can test the openSnackbar and closeSnackbar functions', () => {
|
|
|
317
297
|
<Component openSnackbar={openSnackbarMock} closeSnackbar={closeSnackbarMock} />
|
|
318
298
|
)
|
|
319
299
|
|
|
320
|
-
// Simulates click on some buttons that opens and closes the Snackbar
|
|
321
300
|
fireEvent.click(getByRole('button', { name: /open/i }))
|
|
322
301
|
fireEvent.click(getByRole('button', { name: /close/i }))
|
|
323
302
|
|
|
324
|
-
// Some examples of how you can test the mocks
|
|
325
|
-
expect(openSnackbarMock).toHaveBeenCalled()
|
|
326
303
|
expect(openSnackbarMock).toHaveBeenCalledTimes(1)
|
|
327
|
-
expect(openSnackbarMock).toHaveBeenCalledWith('This is the text of the Snackbar.')
|
|
328
|
-
expect(closeSnackbarMock).toHaveBeenCalled()
|
|
329
304
|
expect(closeSnackbarMock).toHaveBeenCalledTimes(1)
|
|
330
305
|
})
|
|
331
306
|
```
|