kitzo 2.0.1 → 2.0.2

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.
Files changed (2) hide show
  1. package/dist/react.esm.js +156 -241
  2. package/package.json +1 -1
package/dist/react.esm.js CHANGED
@@ -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,222 +203,158 @@ function subscribe(callback) {
203
203
  }
204
204
  function success(text = 'Toast success', options = {}) {
205
205
  const id = Date.now();
206
- options = Object.assign(
207
- {
208
- duration: 2000,
209
- id,
210
- },
211
- options
212
- );
206
+ options = Object.assign({
207
+ duration: 2000,
208
+ id
209
+ }, options);
213
210
  addToast({
214
211
  type: 'success',
215
212
  text,
216
- options,
213
+ options
217
214
  });
218
215
  }
219
216
  function error(text = 'Toast denied', options = {}) {
220
217
  const id = Date.now();
221
- options = Object.assign(
222
- {
223
- duration: 2000,
224
- id,
225
- },
226
- options
227
- );
218
+ options = Object.assign({
219
+ duration: 2000,
220
+ id
221
+ }, options);
228
222
  addToast({
229
223
  type: 'error',
230
224
  text,
231
- options,
225
+ options
232
226
  });
233
227
  }
234
228
  function promise(callback, msgs = {}, options = {}) {
235
229
  const id = Date.now();
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
- );
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);
251
239
  addToast({
252
240
  type: 'loading',
253
241
  text: msgs.loading,
254
242
  options: {
255
243
  ...options,
256
244
  duration: Infinity,
257
- id,
258
- },
245
+ id
246
+ }
259
247
  });
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);
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
279
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
263
+ });
264
+ return Promise.reject(normalizedError);
265
+ });
280
266
  }
281
267
 
282
268
  function SuccessSvg() {
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
- );
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
+ })));
306
284
  }
307
285
  function ErrorSvg() {
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
- );
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
+ })));
334
303
  }
335
304
  function LoadingSvg() {
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
- );
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
+ })));
359
320
  }
360
321
 
361
- const GAP = 10;
362
- function Toast({ toast, setToasts, position }) {
322
+ function Toast({
323
+ toast,
324
+ setToasts,
325
+ position
326
+ }) {
363
327
  const ref = useRef(null);
364
328
  useLayoutEffect(() => {
365
329
  if (!ref.current) return;
366
330
  const h = ref.current.getBoundingClientRect().height;
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
- );
331
+ setToasts(prev => prev.map(t => t.options.id === toast.id ? t : {
332
+ ...t,
333
+ offset: t.offset + h + 10
334
+ }));
377
335
  }, [setToasts, toast.id]);
378
336
  if (position.includes('bottom')) {
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
- {
337
+ return /*#__PURE__*/React.createElement("div", {
404
338
  ref: ref,
405
- className: 'toast',
339
+ className: "toast",
406
340
  style: {
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
- );
341
+ transform: `translateY(-${toast.offset}px)`,
342
+ ...getToastPosition(position)
343
+ }
344
+ }, /*#__PURE__*/React.createElement("div", {
345
+ className: `toast-content-bottom ${toast.leaving ? 'exit' : ''}`
346
+ }, 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)));
347
+ }
348
+ return /*#__PURE__*/React.createElement("div", {
349
+ ref: ref,
350
+ className: "toast",
351
+ style: {
352
+ transform: `translateY(${toast.offset}px)`,
353
+ ...getToastPosition(position)
354
+ }
355
+ }, /*#__PURE__*/React.createElement("div", {
356
+ className: `toast-content ${toast.leaving ? 'exit' : ''}`
357
+ }, 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)));
422
358
  }
423
359
  function getToastPosition(position) {
424
360
  const isLeft = position.includes('left');
@@ -428,7 +364,7 @@ function getToastPosition(position) {
428
364
  width: 'calc(100% - 2rem)',
429
365
  pointerEvents: 'none',
430
366
  transition: 'transform 230ms',
431
- display: 'flex',
367
+ display: 'flex'
432
368
  };
433
369
  if (position.includes('top')) {
434
370
  styles.top = '1rem';
@@ -441,89 +377,68 @@ function getToastPosition(position) {
441
377
  return styles;
442
378
  }
443
379
 
444
- const EXIT_ANIM_MS = 300;
445
380
  function ToastContainer(props) {
446
- props = Object.assign(
447
- {
448
- position: 'top-center',
449
- },
450
- props
451
- );
452
- const { position } = props;
381
+ props = Object.assign({
382
+ position: 'top-center'
383
+ }, props);
384
+ const {
385
+ position
386
+ } = props;
453
387
  const [toasts, setToasts] = useState([]);
454
388
  useEffect(() => {
455
- const unsub = subscribe((toast) => {
389
+ const unsub = subscribe(toast => {
456
390
  const duration = toast.options.duration;
457
391
  const id = toast.options.id;
458
- setToasts((prev) => {
459
- const exists = prev.some((t) => t.options.id === id);
392
+ setToasts(prev => {
393
+ const exists = prev.some(t => t.options.id === id);
460
394
  if (exists) {
461
- return prev.map((t) =>
462
- t.options.id === id
463
- ? {
464
- ...t,
465
- ...toast,
466
- }
467
- : t
468
- );
395
+ return prev.map(t => t.options.id === id ? {
396
+ ...t,
397
+ ...toast
398
+ } : t);
469
399
  }
470
- return [
471
- {
472
- id,
473
- ...toast,
474
- offset: 0,
475
- leaving: false,
476
- },
477
- ...prev,
478
- ];
400
+ return [{
401
+ id,
402
+ ...toast,
403
+ offset: 0,
404
+ leaving: false
405
+ }, ...prev];
479
406
  });
480
407
  if (toast.type !== 'loading') {
481
408
  setTimeout(() => {
482
- setToasts((prev) =>
483
- prev.map((t) =>
484
- t.options.id === id
485
- ? {
486
- ...t,
487
- leaving: true,
488
- }
489
- : t
490
- )
491
- );
492
- }, Math.max(0, duration - EXIT_ANIM_MS));
409
+ setToasts(prev => prev.map(t => t.options.id === id ? {
410
+ ...t,
411
+ leaving: true
412
+ } : t));
413
+ }, Math.max(0, duration - 300));
493
414
  setTimeout(() => {
494
- setToasts((prev) => prev.filter((t) => t.options.id !== id));
415
+ setToasts(prev => prev.filter(t => t.options.id !== id));
495
416
  }, duration);
496
417
  }
497
418
  });
498
419
  return () => unsub();
499
420
  }, []);
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
- );
421
+ return /*#__PURE__*/React.createElement("div", {
422
+ style: {
423
+ position: 'fixed',
424
+ inset: 0,
425
+ zIndex: 100000000,
426
+ pointerEvents: 'none',
427
+ fontFamily: 'inherit',
428
+ padding: '1rem'
429
+ }
430
+ }, toasts.map(t => /*#__PURE__*/React.createElement(Toast, {
431
+ key: t.options.id,
432
+ toast: t,
433
+ setToasts: setToasts,
434
+ position: position
435
+ })));
521
436
  }
522
437
 
523
438
  const toast = {
524
439
  success,
525
440
  error,
526
- promise,
441
+ promise
527
442
  };
528
443
 
529
444
  export { ToastContainer, toast };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kitzo",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "A lightweight JavaScript UI micro-library.",
5
5
  "type": "module",
6
6
  "main": "./dist/kitzo.umd.js",