kitzo 2.0.0 → 2.0.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 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.0/dist/kitzo.umd.min.js"></script>
28
+ <script src="https://cdn.jsdelivr.net/npm/kitzo@2.0.1/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.
package/dist/react.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import { useRef, useLayoutEffect, useState, useEffect } from 'react';
1
+ import React, { useRef, useLayoutEffect, useState, useEffect } from 'react';
2
2
 
3
3
  const listeners = new Set();
4
4
  let isStyleAdded = false;
@@ -187,7 +187,7 @@ function toastStyles() {
187
187
  `;
188
188
  }
189
189
  function addToast(toast) {
190
- listeners.forEach(callback => {
190
+ listeners.forEach((callback) => {
191
191
  callback(toast);
192
192
  });
193
193
  }
@@ -203,159 +203,222 @@ function subscribe(callback) {
203
203
  }
204
204
  function success(text = 'Toast success', options = {}) {
205
205
  const id = Date.now();
206
- options = Object.assign({
207
- duration: 2000,
208
- id
209
- }, options);
206
+ options = Object.assign(
207
+ {
208
+ duration: 2000,
209
+ id,
210
+ },
211
+ options
212
+ );
210
213
  addToast({
211
214
  type: 'success',
212
215
  text,
213
- options
216
+ options,
214
217
  });
215
218
  }
216
219
  function error(text = 'Toast denied', options = {}) {
217
220
  const id = Date.now();
218
- options = Object.assign({
219
- duration: 2000,
220
- id
221
- }, options);
221
+ options = Object.assign(
222
+ {
223
+ duration: 2000,
224
+ id,
225
+ },
226
+ options
227
+ );
222
228
  addToast({
223
229
  type: 'error',
224
230
  text,
225
- options
231
+ options,
226
232
  });
227
233
  }
228
234
  function promise(callback, msgs = {}, options = {}) {
229
235
  const id = Date.now();
230
- options = Object.assign({
231
- duration: 2000,
232
- id
233
- }, options);
234
- msgs = Object.assign({
235
- loading: 'Saving...',
236
- success: 'Success',
237
- error: 'Something went wrong'
238
- }, msgs);
236
+ options = Object.assign(
237
+ {
238
+ duration: 2000,
239
+ id,
240
+ },
241
+ options
242
+ );
243
+ msgs = Object.assign(
244
+ {
245
+ loading: 'Saving...',
246
+ success: 'Success',
247
+ error: 'Something went wrong',
248
+ },
249
+ msgs
250
+ );
239
251
  addToast({
240
252
  type: 'loading',
241
253
  text: msgs.loading,
242
254
  options: {
243
255
  ...options,
244
256
  duration: Infinity,
245
- id
246
- }
257
+ id,
258
+ },
247
259
  });
248
- callback.then(res => {
249
- const successMsg = typeof msgs.success === 'function' ? msgs.success(res) : msgs.success;
250
- addToast({
251
- type: 'success',
252
- text: successMsg,
253
- options
254
- });
255
- return res;
256
- }).catch(err => {
257
- const normalizedError = err instanceof Error ? err : new Error(String(err));
258
- const errorMsg = typeof msgs.error === 'function' ? msgs.error(normalizedError) : msgs.error;
259
- addToast({
260
- type: 'error',
261
- text: errorMsg,
262
- options
260
+ callback
261
+ .then((res) => {
262
+ const successMsg = typeof msgs.success === 'function' ? msgs.success(res) : msgs.success;
263
+ addToast({
264
+ type: 'success',
265
+ text: successMsg,
266
+ options,
267
+ });
268
+ return res;
269
+ })
270
+ .catch((err) => {
271
+ const normalizedError = err instanceof Error ? err : new Error(String(err));
272
+ const errorMsg = typeof msgs.error === 'function' ? msgs.error(normalizedError) : msgs.error;
273
+ addToast({
274
+ type: 'error',
275
+ text: errorMsg,
276
+ options,
277
+ });
278
+ return Promise.reject(normalizedError);
263
279
  });
264
- return Promise.reject(normalizedError);
265
- });
266
280
  }
267
281
 
268
282
  function SuccessSvg() {
269
- return /*#__PURE__*/React.createElement("div", {
270
- className: "svg-container success"
271
- }, /*#__PURE__*/React.createElement("svg", {
272
- xmlns: "http://www.w3.org/2000/svg",
273
- width: "24",
274
- height: "24",
275
- viewBox: "0 0 24 24",
276
- fill: "none",
277
- stroke: "currentColor",
278
- strokeWidth: "2",
279
- strokeLinecap: "round",
280
- strokeLinejoin: "round"
281
- }, /*#__PURE__*/React.createElement("path", {
282
- d: "M20 6 9 17l-5-5"
283
- })));
283
+ return /*#__PURE__*/ React.createElement(
284
+ 'div',
285
+ {
286
+ className: 'svg-container success',
287
+ },
288
+ /*#__PURE__*/ React.createElement(
289
+ 'svg',
290
+ {
291
+ xmlns: 'http://www.w3.org/2000/svg',
292
+ width: '24',
293
+ height: '24',
294
+ viewBox: '0 0 24 24',
295
+ fill: 'none',
296
+ stroke: 'currentColor',
297
+ strokeWidth: '2',
298
+ strokeLinecap: 'round',
299
+ strokeLinejoin: 'round',
300
+ },
301
+ /*#__PURE__*/ React.createElement('path', {
302
+ d: 'M20 6 9 17l-5-5',
303
+ })
304
+ )
305
+ );
284
306
  }
285
307
  function ErrorSvg() {
286
- return /*#__PURE__*/React.createElement("div", {
287
- className: "svg-container error"
288
- }, /*#__PURE__*/React.createElement("svg", {
289
- xmlns: "http://www.w3.org/2000/svg",
290
- width: "24",
291
- height: "24",
292
- viewBox: "0 0 24 24",
293
- fill: "none",
294
- stroke: "currentColor",
295
- strokeWidth: "2",
296
- strokeLinecap: "round",
297
- strokeLinejoin: "round"
298
- }, /*#__PURE__*/React.createElement("path", {
299
- d: "M18 6 6 18"
300
- }), /*#__PURE__*/React.createElement("path", {
301
- d: "m6 6 12 12"
302
- })));
308
+ return /*#__PURE__*/ React.createElement(
309
+ 'div',
310
+ {
311
+ className: 'svg-container error',
312
+ },
313
+ /*#__PURE__*/ React.createElement(
314
+ 'svg',
315
+ {
316
+ xmlns: 'http://www.w3.org/2000/svg',
317
+ width: '24',
318
+ height: '24',
319
+ viewBox: '0 0 24 24',
320
+ fill: 'none',
321
+ stroke: 'currentColor',
322
+ strokeWidth: '2',
323
+ strokeLinecap: 'round',
324
+ strokeLinejoin: 'round',
325
+ },
326
+ /*#__PURE__*/ React.createElement('path', {
327
+ d: 'M18 6 6 18',
328
+ }),
329
+ /*#__PURE__*/ React.createElement('path', {
330
+ d: 'm6 6 12 12',
331
+ })
332
+ )
333
+ );
303
334
  }
304
335
  function LoadingSvg() {
305
- return /*#__PURE__*/React.createElement("div", {
306
- className: "promise-svg-container"
307
- }, /*#__PURE__*/React.createElement("svg", {
308
- xmlns: "http://www.w3.org/2000/svg",
309
- width: "24",
310
- height: "24",
311
- viewBox: "0 0 24 24",
312
- fill: "none",
313
- stroke: "currentColor",
314
- strokeWidth: "2",
315
- strokeLinecap: "round",
316
- strokeLinejoin: "round"
317
- }, /*#__PURE__*/React.createElement("path", {
318
- d: "M21 12a9 9 0 1 1-6.219-8.56"
319
- })));
336
+ return /*#__PURE__*/ React.createElement(
337
+ 'div',
338
+ {
339
+ className: 'promise-svg-container',
340
+ },
341
+ /*#__PURE__*/ React.createElement(
342
+ 'svg',
343
+ {
344
+ xmlns: 'http://www.w3.org/2000/svg',
345
+ width: '24',
346
+ height: '24',
347
+ viewBox: '0 0 24 24',
348
+ fill: 'none',
349
+ stroke: 'currentColor',
350
+ strokeWidth: '2',
351
+ strokeLinecap: 'round',
352
+ strokeLinejoin: 'round',
353
+ },
354
+ /*#__PURE__*/ React.createElement('path', {
355
+ d: 'M21 12a9 9 0 1 1-6.219-8.56',
356
+ })
357
+ )
358
+ );
320
359
  }
321
360
 
322
361
  const GAP = 10;
323
- function Toast({
324
- toast,
325
- setToasts,
326
- position
327
- }) {
362
+ function Toast({ toast, setToasts, position }) {
328
363
  const ref = useRef(null);
329
364
  useLayoutEffect(() => {
330
365
  if (!ref.current) return;
331
366
  const h = ref.current.getBoundingClientRect().height;
332
- setToasts(prev => prev.map(t => t.options.id === toast.id ? t : {
333
- ...t,
334
- offset: t.offset + h + GAP
335
- }));
367
+ setToasts((prev) =>
368
+ prev.map((t) =>
369
+ t.options.id === toast.id
370
+ ? t
371
+ : {
372
+ ...t,
373
+ offset: t.offset + h + GAP,
374
+ }
375
+ )
376
+ );
336
377
  }, [setToasts, toast.id]);
337
378
  if (position.includes('bottom')) {
338
- return /*#__PURE__*/React.createElement("div", {
379
+ return /*#__PURE__*/ React.createElement(
380
+ 'div',
381
+ {
382
+ ref: ref,
383
+ className: 'toast',
384
+ style: {
385
+ transform: `translateY(-${toast.offset}px)`,
386
+ ...getToastPosition(position),
387
+ },
388
+ },
389
+ /*#__PURE__*/ React.createElement(
390
+ 'div',
391
+ {
392
+ className: `toast-content-bottom ${toast.leaving ? 'exit' : ''}`,
393
+ },
394
+ toast.type === 'loading' && /*#__PURE__*/ React.createElement(LoadingSvg, null),
395
+ toast.type === 'success' && /*#__PURE__*/ React.createElement(SuccessSvg, null),
396
+ toast.type === 'error' && /*#__PURE__*/ React.createElement(ErrorSvg, null),
397
+ /*#__PURE__*/ React.createElement('span', null, toast.text)
398
+ )
399
+ );
400
+ }
401
+ return /*#__PURE__*/ React.createElement(
402
+ 'div',
403
+ {
339
404
  ref: ref,
340
- className: "toast",
405
+ className: 'toast',
341
406
  style: {
342
- transform: `translateY(-${toast.offset}px)`,
343
- ...getToastPosition(position)
344
- }
345
- }, /*#__PURE__*/React.createElement("div", {
346
- className: `toast-content-bottom ${toast.leaving ? 'exit' : ''}`
347
- }, toast.type === 'loading' && /*#__PURE__*/React.createElement(LoadingSvg, null), toast.type === 'success' && /*#__PURE__*/React.createElement(SuccessSvg, null), toast.type === 'error' && /*#__PURE__*/React.createElement(ErrorSvg, null), /*#__PURE__*/React.createElement("span", null, toast.text)));
348
- }
349
- return /*#__PURE__*/React.createElement("div", {
350
- ref: ref,
351
- className: "toast",
352
- style: {
353
- transform: `translateY(${toast.offset}px)`,
354
- ...getToastPosition(position)
355
- }
356
- }, /*#__PURE__*/React.createElement("div", {
357
- className: `toast-content ${toast.leaving ? 'exit' : ''}`
358
- }, toast.type === 'loading' && /*#__PURE__*/React.createElement(LoadingSvg, null), toast.type === 'success' && /*#__PURE__*/React.createElement(SuccessSvg, null), toast.type === 'error' && /*#__PURE__*/React.createElement(ErrorSvg, null), /*#__PURE__*/React.createElement("span", null, toast.text)));
407
+ transform: `translateY(${toast.offset}px)`,
408
+ ...getToastPosition(position),
409
+ },
410
+ },
411
+ /*#__PURE__*/ React.createElement(
412
+ 'div',
413
+ {
414
+ className: `toast-content ${toast.leaving ? 'exit' : ''}`,
415
+ },
416
+ toast.type === 'loading' && /*#__PURE__*/ React.createElement(LoadingSvg, null),
417
+ toast.type === 'success' && /*#__PURE__*/ React.createElement(SuccessSvg, null),
418
+ toast.type === 'error' && /*#__PURE__*/ React.createElement(ErrorSvg, null),
419
+ /*#__PURE__*/ React.createElement('span', null, toast.text)
420
+ )
421
+ );
359
422
  }
360
423
  function getToastPosition(position) {
361
424
  const isLeft = position.includes('left');
@@ -365,7 +428,7 @@ function getToastPosition(position) {
365
428
  width: 'calc(100% - 2rem)',
366
429
  pointerEvents: 'none',
367
430
  transition: 'transform 230ms',
368
- display: 'flex'
431
+ display: 'flex',
369
432
  };
370
433
  if (position.includes('top')) {
371
434
  styles.top = '1rem';
@@ -380,67 +443,87 @@ function getToastPosition(position) {
380
443
 
381
444
  const EXIT_ANIM_MS = 300;
382
445
  function ToastContainer(props) {
383
- props = Object.assign({
384
- position: 'top-center'
385
- }, props);
386
- const {
387
- position
388
- } = props;
446
+ props = Object.assign(
447
+ {
448
+ position: 'top-center',
449
+ },
450
+ props
451
+ );
452
+ const { position } = props;
389
453
  const [toasts, setToasts] = useState([]);
390
454
  useEffect(() => {
391
- const unsub = subscribe(toast => {
455
+ const unsub = subscribe((toast) => {
392
456
  const duration = toast.options.duration;
393
457
  const id = toast.options.id;
394
- setToasts(prev => {
395
- const exists = prev.some(t => t.options.id === id);
458
+ setToasts((prev) => {
459
+ const exists = prev.some((t) => t.options.id === id);
396
460
  if (exists) {
397
- return prev.map(t => t.options.id === id ? {
398
- ...t,
399
- ...toast
400
- } : t);
461
+ return prev.map((t) =>
462
+ t.options.id === id
463
+ ? {
464
+ ...t,
465
+ ...toast,
466
+ }
467
+ : t
468
+ );
401
469
  }
402
- return [{
403
- id,
404
- ...toast,
405
- offset: 0,
406
- leaving: false
407
- }, ...prev];
470
+ return [
471
+ {
472
+ id,
473
+ ...toast,
474
+ offset: 0,
475
+ leaving: false,
476
+ },
477
+ ...prev,
478
+ ];
408
479
  });
409
480
  if (toast.type !== 'loading') {
410
481
  setTimeout(() => {
411
- setToasts(prev => prev.map(t => t.options.id === id ? {
412
- ...t,
413
- leaving: true
414
- } : t));
482
+ setToasts((prev) =>
483
+ prev.map((t) =>
484
+ t.options.id === id
485
+ ? {
486
+ ...t,
487
+ leaving: true,
488
+ }
489
+ : t
490
+ )
491
+ );
415
492
  }, Math.max(0, duration - EXIT_ANIM_MS));
416
493
  setTimeout(() => {
417
- setToasts(prev => prev.filter(t => t.options.id !== id));
494
+ setToasts((prev) => prev.filter((t) => t.options.id !== id));
418
495
  }, duration);
419
496
  }
420
497
  });
421
498
  return () => unsub();
422
499
  }, []);
423
- return /*#__PURE__*/React.createElement("div", {
424
- style: {
425
- position: 'fixed',
426
- inset: 0,
427
- zIndex: 100000000,
428
- pointerEvents: 'none',
429
- fontFamily: 'inherit',
430
- padding: '1rem'
431
- }
432
- }, toasts.map(t => /*#__PURE__*/React.createElement(Toast, {
433
- key: t.options.id,
434
- toast: t,
435
- setToasts: setToasts,
436
- position: position
437
- })));
500
+ return /*#__PURE__*/ React.createElement(
501
+ 'div',
502
+ {
503
+ style: {
504
+ position: 'fixed',
505
+ inset: 0,
506
+ zIndex: 100000000,
507
+ pointerEvents: 'none',
508
+ fontFamily: 'inherit',
509
+ padding: '1rem',
510
+ },
511
+ },
512
+ toasts.map((t) =>
513
+ /*#__PURE__*/ React.createElement(Toast, {
514
+ key: t.options.id,
515
+ toast: t,
516
+ setToasts: setToasts,
517
+ position: position,
518
+ })
519
+ )
520
+ );
438
521
  }
439
522
 
440
523
  const toast = {
441
524
  success,
442
525
  error,
443
- promise
526
+ promise,
444
527
  };
445
528
 
446
529
  export { ToastContainer, toast };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kitzo",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "A lightweight JavaScript UI micro-library.",
5
5
  "type": "module",
6
6
  "main": "./dist/kitzo.umd.js",