@eztrak/shared 0.16.6 → 0.17.0

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.
@@ -318,7 +318,138 @@ function classifyError(error) {
318
318
  };
319
319
  }
320
320
  var BRAND_ORANGE = "#FF7335";
321
- var CANCEL_RED = "#ff0000";
321
+ var DANGER_RED = "#DC2626";
322
+ var DANGER_ICON_BG = "#FEE2E2";
323
+ var CANCEL_BG = "#ffffff";
324
+ var CANCEL_TEXT = "#374151";
325
+ var CONFIRM_STYLE_ID = "eztrak-confirm-alert-styles";
326
+ function cssSize(value, fallback) {
327
+ if (value === void 0) return fallback;
328
+ return typeof value === "number" ? `${value}px` : value;
329
+ }
330
+ var DEFAULT_CONFIRM_ICONS = {
331
+ warning: `
332
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
333
+ <path d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L4.082 16.5c-.77.833.192 2.5 1.732 2.5z" />
334
+ </svg>
335
+ `.trim()
336
+ };
337
+ var VARIANT_DEFAULTS = {
338
+ brand: {
339
+ confirmButtonText: "Yes",
340
+ confirmButtonColor: BRAND_ORANGE,
341
+ confirmButtonTextColor: "#ffffff",
342
+ cancelButtonColor: CANCEL_BG,
343
+ cancelButtonTextColor: CANCEL_TEXT,
344
+ iconColor: BRAND_ORANGE,
345
+ iconBgColor: "#FFEDD5",
346
+ iconHtml: DEFAULT_CONFIRM_ICONS.warning,
347
+ icon: "warning"
348
+ },
349
+ danger: {
350
+ title: "Delete this item",
351
+ confirmButtonText: "Yes, delete it!",
352
+ confirmButtonColor: DANGER_RED,
353
+ confirmButtonTextColor: "#ffffff",
354
+ cancelButtonColor: CANCEL_BG,
355
+ cancelButtonTextColor: CANCEL_TEXT,
356
+ iconColor: DANGER_RED,
357
+ iconBgColor: DANGER_ICON_BG,
358
+ iconHtml: DEFAULT_CONFIRM_ICONS.warning,
359
+ icon: "warning",
360
+ showLoaderOnConfirm: true
361
+ }
362
+ };
363
+ function ensureConfirmAlertStyles() {
364
+ if (typeof document === "undefined") return;
365
+ if (document.getElementById(CONFIRM_STYLE_ID)) return;
366
+ const style = document.createElement("style");
367
+ style.id = CONFIRM_STYLE_ID;
368
+ style.textContent = `
369
+ .ez-confirm-popup {
370
+ border-radius: var(--ez-confirm-radius, 1rem) !important;
371
+ padding: 1.5rem !important;
372
+ max-width: 24rem !important;
373
+ width: var(--ez-confirm-width, 100%) !important;
374
+ box-shadow: 0 25px 50px -12px rgb(0 0 0 / 0.25) !important;
375
+ }
376
+ .ez-confirm-title {
377
+ font-size: 1.125rem !important;
378
+ font-weight: 600 !important;
379
+ color: #111827 !important;
380
+ margin: 0 0 0.25rem !important;
381
+ padding: 0 !important;
382
+ }
383
+ .ez-confirm-html {
384
+ font-size: 0.875rem !important;
385
+ color: #6b7280 !important;
386
+ margin: 0 0 1.5rem !important;
387
+ }
388
+ .ez-confirm-icon {
389
+ border: none !important;
390
+ background: var(--ez-confirm-icon-bg, #fee2e2) !important;
391
+ color: var(--ez-confirm-icon-color, #dc2626) !important;
392
+ width: 3rem !important;
393
+ height: 3rem !important;
394
+ margin: 0 auto 1rem !important;
395
+ border-radius: 9999px !important;
396
+ }
397
+ .ez-confirm-icon .swal2-icon-content {
398
+ font-size: inherit !important;
399
+ display: flex !important;
400
+ align-items: center !important;
401
+ justify-content: center !important;
402
+ color: inherit !important;
403
+ }
404
+ .ez-confirm-actions {
405
+ width: 100% !important;
406
+ margin: 0 !important;
407
+ gap: 0.75rem !important;
408
+ }
409
+ .ez-confirm-confirm,
410
+ .ez-confirm-cancel {
411
+ flex: 1 1 0 !important;
412
+ margin: 0 !important;
413
+ border-radius: var(--ez-confirm-btn-radius, 0.5rem) !important;
414
+ font-size: 0.875rem !important;
415
+ font-weight: 500 !important;
416
+ padding: 0.625rem 1rem !important;
417
+ box-shadow: none !important;
418
+ }
419
+ .ez-confirm-confirm {
420
+ background: var(--ez-confirm-confirm-bg, #ff7335) !important;
421
+ color: var(--ez-confirm-confirm-fg, #fff) !important;
422
+ border: none !important;
423
+ }
424
+ .ez-confirm-confirm:hover {
425
+ filter: brightness(0.95);
426
+ }
427
+ .ez-confirm-cancel {
428
+ background: var(--ez-confirm-cancel-bg, #fff) !important;
429
+ color: var(--ez-confirm-cancel-fg, #374151) !important;
430
+ border: 1px solid #d1d5db !important;
431
+ }
432
+ .ez-confirm-cancel:hover {
433
+ background: #f9fafb !important;
434
+ }
435
+ `;
436
+ document.head.appendChild(style);
437
+ }
438
+ function pick(explicit, fromVariant, fallback) {
439
+ if (explicit !== void 0) return explicit;
440
+ if (fromVariant !== void 0) return fromVariant;
441
+ return fallback;
442
+ }
443
+ function mergeCustomClass(base, extra) {
444
+ if (!extra || typeof extra !== "object") return base;
445
+ const merged = { ...base };
446
+ for (const [key, value] of Object.entries(extra)) {
447
+ if (typeof value === "string" && value) {
448
+ merged[key] = merged[key] ? `${merged[key]} ${value}` : value;
449
+ }
450
+ }
451
+ return merged;
452
+ }
322
453
  function getConfirmErrorMessage(error) {
323
454
  if (error && typeof error === "object") {
324
455
  const err = error;
@@ -327,23 +458,59 @@ function getConfirmErrorMessage(error) {
327
458
  return "Something went wrong. Please try again.";
328
459
  }
329
460
  function confirmationAlert(onConfirm, options = {}) {
330
- const {
331
- title = options.inputExpectedValue ? "Confirmation Required" : "Are you sure?",
332
- text,
333
- html,
334
- icon = "warning",
335
- confirmButtonText = "Yes",
336
- cancelButtonText = "Cancel",
337
- confirmButtonColor = BRAND_ORANGE,
338
- cancelButtonColor = CANCEL_RED,
339
- showLoaderOnConfirm = false,
340
- inputExpectedValue,
341
- inputPlaceholder = "Type confirmation text to proceed",
342
- reverseButtons,
343
- focusCancel,
344
- zIndex
345
- } = options;
461
+ ensureConfirmAlertStyles();
462
+ const variant = options.variant ?? "brand";
463
+ const vd = VARIANT_DEFAULTS[variant];
464
+ const inputExpectedValue = options.inputExpectedValue;
346
465
  const hasInput = Boolean(inputExpectedValue);
466
+ const title = pick(
467
+ options.title,
468
+ vd.title,
469
+ hasInput ? "Confirmation Required" : "Delete this item"
470
+ );
471
+ const text = options.text;
472
+ const html = options.html;
473
+ const icon = pick(options.icon, vd.icon, "warning");
474
+ const iconHtml = pick(options.iconHtml, vd.iconHtml, void 0);
475
+ const iconColor = pick(options.iconColor, vd.iconColor, BRAND_ORANGE);
476
+ const iconBgColor = pick(options.iconBgColor, vd.iconBgColor, "#FFEDD5");
477
+ const showIcon = options.showIcon !== false;
478
+ const confirmButtonText = pick(options.confirmButtonText, vd.confirmButtonText, "Yes");
479
+ const cancelButtonText = options.cancelButtonText ?? "Cancel";
480
+ const confirmButtonColor = pick(
481
+ options.confirmButtonColor,
482
+ vd.confirmButtonColor,
483
+ BRAND_ORANGE
484
+ );
485
+ const cancelButtonColor = pick(
486
+ options.cancelButtonColor,
487
+ vd.cancelButtonColor,
488
+ CANCEL_BG
489
+ );
490
+ const confirmButtonTextColor = pick(
491
+ options.confirmButtonTextColor,
492
+ vd.confirmButtonTextColor,
493
+ "#ffffff"
494
+ );
495
+ const cancelButtonTextColor = pick(
496
+ options.cancelButtonTextColor,
497
+ vd.cancelButtonTextColor,
498
+ CANCEL_TEXT
499
+ );
500
+ const showCancelButton = options.showCancelButton !== false;
501
+ const showLoaderOnConfirm = pick(
502
+ options.showLoaderOnConfirm,
503
+ vd.showLoaderOnConfirm,
504
+ false
505
+ );
506
+ const inputPlaceholder = options.inputPlaceholder ?? "Type confirmation text to proceed";
507
+ const reverseButtons = options.reverseButtons ?? false;
508
+ const focusCancel = options.focusCancel;
509
+ const zIndex = options.zIndex;
510
+ const width = options.width;
511
+ const borderRadius = cssSize(options.borderRadius, "1rem");
512
+ const buttonBorderRadius = cssSize(options.buttonBorderRadius, "0.5rem");
513
+ const onCancel = options.onCancel;
347
514
  const useLoader = showLoaderOnConfirm || hasInput;
348
515
  const inputHtml = html ?? (hasInput ? `
349
516
  ${text ? `<p style="margin-bottom: 15px;">${text}</p>` : ""}
@@ -351,24 +518,56 @@ function confirmationAlert(onConfirm, options = {}) {
351
518
  Please type "<span style="color: ${BRAND_ORANGE}; font-weight: bold;">${inputExpectedValue}</span>" to confirm:
352
519
  </p>
353
520
  ` : void 0);
521
+ const baseCustomClass = {
522
+ popup: "ez-confirm-popup",
523
+ title: "ez-confirm-title",
524
+ htmlContainer: "ez-confirm-html",
525
+ actions: "ez-confirm-actions",
526
+ confirmButton: "ez-confirm-confirm",
527
+ cancelButton: "ez-confirm-cancel",
528
+ icon: "ez-confirm-icon"
529
+ };
354
530
  const swalOptions = {
355
531
  title,
356
- icon,
357
- showCancelButton: true,
532
+ showCancelButton,
358
533
  confirmButtonText,
359
534
  cancelButtonText,
360
535
  confirmButtonColor,
361
536
  cancelButtonColor,
362
537
  reverseButtons,
363
538
  focusCancel,
539
+ customClass: mergeCustomClass(baseCustomClass, options.customClass),
364
540
  allowOutsideClick: () => !import_sweetalert2.default.isLoading(),
365
541
  allowEscapeKey: () => !import_sweetalert2.default.isLoading(),
366
542
  didOpen: () => {
543
+ const popup = import_sweetalert2.default.getPopup();
544
+ if (popup) {
545
+ popup.style.setProperty("--ez-confirm-confirm-bg", confirmButtonColor);
546
+ popup.style.setProperty("--ez-confirm-confirm-fg", confirmButtonTextColor);
547
+ popup.style.setProperty("--ez-confirm-cancel-bg", cancelButtonColor);
548
+ popup.style.setProperty("--ez-confirm-cancel-fg", cancelButtonTextColor);
549
+ popup.style.setProperty("--ez-confirm-icon-color", iconColor);
550
+ popup.style.setProperty("--ez-confirm-icon-bg", iconBgColor);
551
+ popup.style.setProperty("--ez-confirm-radius", borderRadius);
552
+ popup.style.setProperty("--ez-confirm-btn-radius", buttonBorderRadius);
553
+ if (width != null) {
554
+ popup.style.setProperty(
555
+ "--ez-confirm-width",
556
+ typeof width === "number" ? `${width}px` : String(width)
557
+ );
558
+ }
559
+ }
367
560
  if (zIndex == null) return;
368
561
  const container = import_sweetalert2.default.getContainer();
369
562
  if (container) container.style.zIndex = String(zIndex);
370
563
  }
371
564
  };
565
+ if (showIcon) {
566
+ swalOptions.icon = icon;
567
+ if (iconHtml) {
568
+ swalOptions.iconHtml = iconHtml;
569
+ }
570
+ }
372
571
  if (hasInput) {
373
572
  swalOptions.html = inputHtml;
374
573
  swalOptions.input = "text";
@@ -403,6 +602,10 @@ function confirmationAlert(onConfirm, options = {}) {
403
602
  void import_sweetalert2.default.fire(swalOptions).then((result) => {
404
603
  if (result.isConfirmed && !useLoader) {
405
604
  void Promise.resolve(onConfirm());
605
+ return;
606
+ }
607
+ if (result.isDismissed) {
608
+ onCancel?.();
406
609
  }
407
610
  });
408
611
  }