kitzo 2.0.27 → 2.0.29

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 CHANGED
@@ -25,7 +25,7 @@ npm i kitzo
25
25
  or
26
26
 
27
27
  ```javascript
28
- <script src="https://cdn.jsdelivr.net/npm/kitzo@2.0.27/dist/kitzo.umd.min.js"></script>
28
+ <script src="https://cdn.jsdelivr.net/npm/kitzo@2.0.29/dist/kitzo.umd.min.js"></script>
29
29
  ```
30
30
 
31
31
  > Attach this script tag in the html head tag and you are good to go.
@@ -126,13 +126,14 @@ kitzo.clippath(selectors | element | NodeList, {
126
126
  style: object,
127
127
  });
128
128
  ```
129
+
129
130
  ##### TypeCheck API:
130
131
 
131
132
  ```javascript
132
133
  kitzo.getType(type);
133
134
 
134
135
  const data = [];
135
- console.log(kitzo.getType(data)) // 'array'
136
+ console.log(kitzo.getType(data)); // 'array'
136
137
  ```
137
138
 
138
139
  ---
@@ -156,17 +157,32 @@ import { ToastContainer, toast, ... } from 'kitzo/react';
156
157
  ```jsx
157
158
  import { toast } from 'kitzo/react';
158
159
 
159
- toast.success('toast message', {});
160
- toast.error('toast message', {});
160
+ toast.success('toast message', { duration: 2000, style: {}, showIcon: true });
161
+ toast.error('toast message', { duration: 2000, style: {}, showIcon: true });
162
+
161
163
  toast.promise(
162
- promise(),
164
+ promise(), // call a function that returns promise
163
165
  {
164
166
  loading: 'Saving...',
165
- success: 'Saved',
166
- error: 'Error occured',
167
+ success: 'Saved' | (response) => 'Saved',
168
+ error: 'Error occured' | (error) => 'Error occured',
167
169
  },
168
- { duration: 2000 }
170
+ { duration: 2000, style: {}, showIcon: true },
169
171
  );
172
+
173
+ // JSX element
174
+ toast.custom(<MyCustomComponent />, { duration: 2000 });
175
+
176
+ // Function that receives a dismiss handler
177
+ toast.custom((dismiss) => (
178
+ <div>
179
+ <span>Custom toast message</span>
180
+ <button onClick={dismiss}>Close</button>
181
+ </div>
182
+ ), { duration: 3000 | Infinity });
183
+
184
+ // (and optionally)
185
+ toast.custom("string");
170
186
  ```
171
187
 
172
188
  ##### Toast API Usage
@@ -179,29 +195,42 @@ function App() {
179
195
  return new Promise((resolved, rejected) => {
180
196
  setTimeout(() => {
181
197
  Math.random() > 0.5 ? resolved('resolved') : rejected('rejected');
182
- }, 2000)
183
- })
198
+ }, 2000);
199
+ });
200
+ }
201
+
202
+ function promiseToast() {
203
+ toast.promise(
204
+ fakePromise(),
205
+ {
206
+ loading: 'Saving data...',
207
+ success: 'Data saved',
208
+ error: 'Failed saving data',
209
+ },
210
+ { duration: 2500 },
211
+ );
212
+ }
213
+
214
+ function customToast() {
215
+ toast.custom(
216
+ (dismiss) => (
217
+ <div>
218
+ <span>Funtion that return jsx</span>
219
+ <button onClick={dismiss}>Close</button>
220
+ </div>
221
+ ),
222
+ {
223
+ duration: 2000,
224
+ },
225
+ );
184
226
  }
185
227
 
186
228
  return (
187
229
  <div>
188
230
  <button onClick={() => toast.success('✅ Success toast message')}>Succes</button>
189
231
  <button onClick={() => toast.error('❌ Error toast message')}>Error</button>
190
- <button
191
- onClick={() => {
192
- toast.promise(
193
- fakePromise(),
194
- {
195
- loading: 'Saving data...',
196
- success: 'Data saved',
197
- error: 'Failed saving data',
198
- },
199
- { duration: 2500 }
200
- );
201
- }}
202
- >
203
- Promise
204
- </button>
232
+ <button onClick={promiseToast}>Promise</button>
233
+ <button onClick={customToast}>Custom Toast</button>
205
234
 
206
235
  {/* Toast container must needed */}
207
236
  <ToastContainer position="top-center" />
package/dist/react.d.ts CHANGED
@@ -1,9 +1,13 @@
1
+ import React from 'react';
2
+
1
3
  export interface ToastOptions {
2
4
  duration?: number;
3
5
  style?: React.CSSProperties;
4
6
  showIcon?: boolean;
5
7
  }
6
8
 
9
+ export type CustomContent = string | React.ReactNode | ((dismiss: () => void) => React.ReactNode);
10
+
7
11
  export interface ToastAPI {
8
12
  success(text?: string, options?: ToastOptions): void;
9
13
  error(text?: string, options?: ToastOptions): void;
@@ -14,12 +18,11 @@ export interface ToastAPI {
14
18
  success?: string | ((res: T) => string);
15
19
  error?: string | ((err: Error) => string);
16
20
  },
17
- options?: ToastOptions
21
+ options?: ToastOptions,
18
22
  ): Promise<T>;
23
+ custom(content: CustomContent, options?: { duration?: number | Infinity; }): void;
19
24
  }
20
25
 
21
26
  export declare const toast: ToastAPI;
22
- export declare function ToastContainer(props: {
23
- position?: 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right';
24
- gap?: number;
25
- }): JSX.Element;
27
+
28
+ export declare function ToastContainer(props: { position?: 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right'; gap?: number }): JSX.Element;
package/dist/react.esm.js CHANGED
@@ -1,7 +1,5 @@
1
1
  import React, { useRef, useLayoutEffect, useState, useEffect } from 'react';
2
2
 
3
- const listeners = new Set();
4
- let isStyleAdded = false;
5
3
  function toastStyles() {
6
4
  return `.toast-content,
7
5
  .toast-content-bottom {
@@ -183,6 +181,8 @@ function toastStyles() {
183
181
  }
184
182
  `;
185
183
  }
184
+ const listeners = new Set();
185
+ let isStyleAdded = false;
186
186
  function addToast(toast) {
187
187
  listeners.forEach(callback => {
188
188
  callback(toast);
@@ -267,6 +267,27 @@ function promise(callback, msgs = {}, options = {}) {
267
267
  return Promise.reject(normalizedError);
268
268
  });
269
269
  }
270
+ function custom(render, options = {}) {
271
+ const id = Date.now();
272
+ options = Object.assign({
273
+ duration: 3000,
274
+ id,
275
+ exitDelay: 50
276
+ }, options);
277
+ addToast({
278
+ type: 'custom',
279
+ render,
280
+ options
281
+ });
282
+ return id;
283
+ }
284
+ function dismiss(id, exitDelay) {
285
+ addToast({
286
+ type: 'dismiss',
287
+ id,
288
+ exitDelay
289
+ });
290
+ }
270
291
 
271
292
  function SuccessSvg() {
272
293
  return /*#__PURE__*/React.createElement("div", {
@@ -361,7 +382,7 @@ function Toast({
361
382
  ...style
362
383
  },
363
384
  className: `toast-content${position.includes('bottom') ? '-bottom' : ''} ${leaving ? 'exit' : ''}`
364
- }, showIcon && /*#__PURE__*/React.createElement(React.Fragment, null, type === 'loading' && /*#__PURE__*/React.createElement(LoadingSvg, null), type === 'success' && /*#__PURE__*/React.createElement(SuccessSvg, null), type === 'error' && /*#__PURE__*/React.createElement(ErrorSvg, null)), /*#__PURE__*/React.createElement("span", null, text)));
385
+ }, type === 'custom' ? typeof toast.render === 'function' ? toast.render(() => dismiss(toast.id, toast.options.exitDelay)) : typeof toast.render === 'string' ? /*#__PURE__*/React.createElement("span", null, toast.render) : toast.render : /*#__PURE__*/React.createElement(React.Fragment, null, showIcon && /*#__PURE__*/React.createElement(React.Fragment, null, type === 'loading' && /*#__PURE__*/React.createElement(LoadingSvg, null), type === 'success' && /*#__PURE__*/React.createElement(SuccessSvg, null), type === 'error' && /*#__PURE__*/React.createElement(ErrorSvg, null)), /*#__PURE__*/React.createElement("span", null, text))));
365
386
  }
366
387
  function getToastPosition(position) {
367
388
  const isLeft = position.includes('left');
@@ -396,6 +417,18 @@ function ToastContainer(props) {
396
417
  const [toasts, setToasts] = useState([]);
397
418
  useEffect(() => {
398
419
  const unsub = subscribe(toast => {
420
+ if (toast.type === 'dismiss') {
421
+ setTimeout(() => {
422
+ setToasts(prev => prev.map(t => t.options.id === toast.id ? {
423
+ ...t,
424
+ leaving: true
425
+ } : t));
426
+ }, Math.max(0, Number(toast.exitDelay)));
427
+ setTimeout(() => {
428
+ setToasts(prev => prev.filter(t => t.options.id !== toast.id));
429
+ }, Math.max(0, Number(toast.exitDelay) + 300));
430
+ return;
431
+ }
399
432
  const duration = toast.options.duration;
400
433
  const id = toast.options.id;
401
434
  setToasts(prev => {
@@ -414,7 +447,7 @@ function ToastContainer(props) {
414
447
  leaving: false
415
448
  }, ...prev];
416
449
  });
417
- if (toast.type !== 'loading') {
450
+ if (toast.type !== 'loading' && isFinite(duration)) {
418
451
  setTimeout(() => {
419
452
  setToasts(prev => prev.map(t => t.options.id === id ? {
420
453
  ...t,
@@ -468,7 +501,8 @@ function ToastContainer(props) {
468
501
  const toast = {
469
502
  success,
470
503
  error,
471
- promise
504
+ promise,
505
+ custom
472
506
  };
473
507
 
474
508
  export { ToastContainer, toast };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kitzo",
3
- "version": "2.0.27",
3
+ "version": "2.0.29",
4
4
  "description": "A lightweight JavaScript UI micro-library.",
5
5
  "type": "module",
6
6
  "main": "./dist/kitzo.umd.js",