├── .gitignore
├── LICENSE
├── README.md
├── package-lock.json
├── package.json
├── rollup.config.js
├── setupTests.ts
├── src
├── NotificationContainer
│ ├── NotificationContainer.test.tsx
│ ├── NotificationContainer.tsx
│ └── styles.css
└── index.tsx
└── tsconfig.json
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | dist
3 | .cache
4 | .DS_Store
5 | coverage
6 | build
7 | .history
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Alex Permyakov
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Simple-React-Notifications
2 |
3 | Tiny library (only 1kb gzip) that allows you to add notifications to your app.
4 | We don't want to blow up our bundle size because of notifications, right?
5 |
6 | ## Demo
7 |
8 | https://alexpermyakov.github.io/simple-react-notifications/
9 |
10 | Despite the small size, it supports:
11 |
12 | - [Rendering success and error default notifications](#rendering-success-and-error-default-notifications)
13 | - [Rendering user defined component](#rendering-user-defined-component)
14 | - [Positioning](#positioning)
15 | - [Configuring all in one place](#configuring-all-in-one-place)
16 | - [Animation](#animation)
17 | - [Remove notification items programmatically](#remove-notification-items-programmatically)
18 |
19 | ## Installation
20 |
21 | ```
22 | $ npm install simple-react-notifications
23 | $ yarn add simple-react-notifications
24 | ```
25 |
26 | ## Usage
27 |
28 | ### Rendering success and error default notifications
29 |
30 | Notifier has a few built-in components for displaying an error or a successfull operation:
31 |
32 | ```javascript
33 | import React from "react";
34 | import notifier from "simple-react-notifications";
35 | import "simple-react-notifications/dist/index.css";
36 |
37 | const App = () => (
38 |
39 |
47 |
48 | );
49 | ```
50 |
51 | ### Rendering user defined component
52 |
53 | The real power comes with rendering our own component. In this case it's not even a notification, just a view with real data:
54 |
55 | ```javascript
56 | const RouteInfo = ({ header, onClosePanel }) => (
57 |
69 |
{header}
70 |
Bicycle 2.4 km, 8 min.
71 |
Use caution - may involve errors or sections not suited for bicycling
72 |
78 |
79 | );
80 | ```
81 |
82 | It completely up to us the way we add styles. We can use styled-components or whatever we like. The notify() method will just render it:
83 |
84 | ```javascript
85 | const App = () => (
86 |
87 |
102 |
103 | );
104 | ```
105 |
106 | As you can see here, render() receives onClose callback, which we have to pass inside our component in order to close the notification when user click on the button.
107 |
108 | ### Positioning
109 |
110 | By default, all items will be positioned in the top right corner. The following values are allowed: top-right, top-center, top-left, bottom-right, bottom-center, bottom-left.
111 |
112 | ```javascript
113 | const App = () => (
114 |
115 |
136 |
137 | );
138 | ```
139 |
140 | ### Configuring all in one place
141 |
142 | Instead of specifing all params again and again for each item, we can put it in one place:
143 |
144 | ```javascript
145 | notifier.configure({
146 | autoClose: 2000,
147 | position: "top-center",
148 | delay: 500,
149 | single: false,
150 | width: "480px"
151 | });
152 |
153 | const App = () => (
154 |
155 |
162 |
163 | );
164 | ```
165 |
166 | Params in notifier function will override their default values in configure().
167 |
168 | ### Animation
169 |
170 | First, define the css-animation somewhere in your .css file:
171 |
172 | ```css
173 | @keyframes fadeIn {
174 | from {
175 | opacity: 0;
176 | }
177 | to {
178 | opacity: 1;
179 | }
180 | }
181 |
182 | @keyframes fadeOut {
183 | from {
184 | opacity: 1;
185 | }
186 | to {
187 | opacity: 0;
188 | }
189 | }
190 | ```
191 |
192 | Second, specify it during the notifier() call or in configure():
193 |
194 | ```javascript
195 | notifier.configure({
196 | position: "top-center",
197 | animation: {
198 | in: "fadeIn", // try to comment it out
199 | out: "fadeOut",
200 | duration: 600 // overriding the default(300ms) value
201 | }
202 | });
203 |
204 | const App = () => (
205 |
206 |
213 |
214 | );
215 | ```
216 |
217 | You can specify only in or out params as well.
218 |
219 | ### Remove notification items programmatically
220 |
221 | ```javascript
222 | import React from "react";
223 | import notifier from "simple-react-notifications";
224 |
225 | notifier.configure({
226 | render: ({ id, onClose }) => (
227 |
232 | )
233 | });
234 |
235 | class App extends React.Component {
236 | id = null;
237 |
238 | render() {
239 | return (
240 |