@didit-protocol/sdk-web 0.5.0 → 0.5.3

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Didit SDK for Web v0.5.0
2
+ * Didit SDK for Web v0.5.3
3
3
  * (c) 2026 Didit
4
4
  * @license MIT
5
5
  */
@@ -726,6 +726,9 @@ function submitEvent(options) {
726
726
  if (!isRecord(event) || !event.category || typeof event.actionType !== "string" || !event.actionType.trim()) {
727
727
  throw new DiditEventError("validation", "event.category and event.actionType are required.");
728
728
  }
729
+ if (event.occurredAt instanceof Date && Number.isNaN(event.occurredAt.getTime())) {
730
+ throw new DiditEventError("validation", "event.occurredAt must be a valid date.");
731
+ }
729
732
  if (typeof fetch !== "function") {
730
733
  throw new DiditEventError("network", "fetch is not available in this environment.");
731
734
  }
@@ -784,18 +787,20 @@ const CSS_CLASSES = {
784
787
  closeIcon: "didit-close-icon",
785
788
  closeIconLegacy: "didit-close-icon-legacy",
786
789
  flowSized: "didit-flow-sized",
790
+ sizeAnimated: "didit-size-animated",
787
791
  loading: "didit-loading",
788
792
  confirmOverlay: "didit-confirm-overlay",
789
793
  confirmBox: "didit-confirm-box",
790
794
  embedded: "didit-embedded"
791
795
  };
792
- const SDK_VERSION = "0.5.0";
796
+ const SDK_VERSION = "0.5.3";
793
797
  /**
794
798
  * The permissions the verification iframe gets, in a modal and embedded:
795
- * camera and microphone for capture, and clipboard-write for the flow's copy
796
- * buttons.
799
+ * camera and microphone for capture, clipboard-write for the flow's copy
800
+ * buttons, and digital-credentials-get so the flow can ask a digital ID wallet
801
+ * (Google Wallet) through the W3C Digital Credentials API from inside the iframe.
797
802
  */
798
- const IFRAME_ALLOW = "camera; microphone; fullscreen; autoplay; encrypted-media; geolocation; clipboard-write";
803
+ const IFRAME_ALLOW = "camera; microphone; fullscreen; autoplay; encrypted-media; geolocation; clipboard-write; digital-credentials-get";
799
804
  /** The verification-URL query parameter each camera option is forwarded as. */
800
805
  const CAMERA_QUERY_PARAMS = {
801
806
  defaultDocumentCamera: "document_camera",
@@ -816,7 +821,7 @@ const PRESENTATION_QUERY_PARAMS = {
816
821
  };
817
822
  /**
818
823
  * Posted by the hosted flow when it lays itself out for the modal, with
819
- * `data: { contentHeight, colorScheme?, dir? }`. The modal then grows to the
824
+ * `data: { contentHeight, colorScheme?, dir?, kind? }` (`kind` is `handoff` or `step`, `step` when missing). The modal then grows to the
820
825
  * handoff card on a computer and follows the content height in a narrow
821
826
  * window. Handled by the modal and never delivered to `onEvent`.
822
827
  */
@@ -1450,6 +1455,10 @@ function getTranslations(language) {
1450
1455
  return translationsMap[language] ?? EN;
1451
1456
  }
1452
1457
 
1458
+ /** The length of the one animation when the flow moves from the handoff card to a step or back. */
1459
+ const SIZE_ANIMATION_MS = 300;
1460
+ /** How long the modal waits for the first layout before it shows at step size, so it can never hang. */
1461
+ const FIRST_LAYOUT_TIMEOUT_MS = 3000;
1453
1462
  class VerificationModal {
1454
1463
  constructor(configuration, callbacks) {
1455
1464
  this.state = {
@@ -1467,9 +1476,13 @@ class VerificationModal {
1467
1476
  this.boundHandleKeydown = null;
1468
1477
  this.boundHandleFocusChange = null;
1469
1478
  this.confirmationFocusTimeout = null;
1479
+ this.firstLayoutTimeout = null;
1480
+ this.sizeTimeout = null;
1470
1481
  this.embedded = false;
1471
1482
  this.embeddedContainer = null;
1472
1483
  this.language = "en";
1484
+ /** The page's own background colours, kept while a full-screen modal paints over them. */
1485
+ this.pageBackground = null;
1473
1486
  /** body.style.overflow value observed right before this modal locked scroll, restored on close(). */
1474
1487
  this.previousBodyOverflow = null;
1475
1488
  this.modalId = generateModalId();
@@ -1514,9 +1527,9 @@ class VerificationModal {
1514
1527
  .${CSS_CLASSES.container} {
1515
1528
  position: relative;
1516
1529
  width: 100%;
1517
- max-width: 500px;
1518
- max-height: 90vh;
1519
- max-height: 90dvh;
1530
+ max-width: 440px;
1531
+ height: min(860px, calc(100vh - 2rem));
1532
+ height: min(860px, calc(100dvh - 2rem));
1520
1533
  border-radius: 16px;
1521
1534
  overflow: hidden;
1522
1535
  background: transparent;
@@ -1528,9 +1541,7 @@ class VerificationModal {
1528
1541
 
1529
1542
  .${CSS_CLASSES.iframe} {
1530
1543
  width: 100%;
1531
- height: 90vh;
1532
- height: 90dvh;
1533
- max-height: 700px;
1544
+ height: 100%;
1534
1545
  border: none;
1535
1546
  display: block;
1536
1547
  }
@@ -1571,6 +1582,12 @@ class VerificationModal {
1571
1582
  z-index: 5;
1572
1583
  }
1573
1584
 
1585
+ @media (prefers-color-scheme: dark) {
1586
+ .${CSS_CLASSES.loading} {
1587
+ background: #0b0b0d;
1588
+ }
1589
+ }
1590
+
1574
1591
  .${CSS_CLASSES.loading}.hidden {
1575
1592
  display: none;
1576
1593
  }
@@ -1692,6 +1709,58 @@ class VerificationModal {
1692
1709
  outline-offset: 2px;
1693
1710
  }
1694
1711
 
1712
+ /*
1713
+ * Steps are phone-sized (440 x 860, above). On a computer the handoff card is wider
1714
+ * (board Start-Embedded): 960 x 660 on a window of 1000px or more, never wider than the
1715
+ * window less its margin and never taller than 90% of it. In a narrower window the
1716
+ * page stacks and the height follows its content (--didit-content-height, set from the
1717
+ * flow's layout message). Until the flow reports what it shows, the modal is the handoff
1718
+ * card at its final size with the spinner inside it (the flow stays hidden and lays itself
1719
+ * out for that size): it never opens small and grows. The first change of kind (a step
1720
+ * instead of the handoff, or the person continues on this computer) animates once.
1721
+ */
1722
+ @media (min-width: 541px) {
1723
+ .${CSS_CLASSES.container}[data-didit-kind="handoff"],
1724
+ .${CSS_CLASSES.container}[data-didit-kind="pending"] {
1725
+ max-width: min(960px, calc(100vw - 2rem));
1726
+ height: min(var(--didit-content-height, 660px), calc(100vh * 0.9));
1727
+ height: min(var(--didit-content-height, 660px), calc(100dvh * 0.9));
1728
+ }
1729
+
1730
+ .${CSS_CLASSES.container}.${CSS_CLASSES.sizeAnimated} {
1731
+ transition: max-width 0.25s ease, height 0.25s ease;
1732
+ }
1733
+
1734
+ .${CSS_CLASSES.container}[data-didit-kind="pending"] .${CSS_CLASSES.iframe} {
1735
+ visibility: hidden;
1736
+ }
1737
+ }
1738
+
1739
+ /*
1740
+ * While pending the card already looks like the first screen (ground, radius, shadow and
1741
+ * close button come from the flow-sized rules below, with the device theme); the loader
1742
+ * sits inside it on the card's own ground.
1743
+ */
1744
+ .${CSS_CLASSES.container}[data-didit-kind="pending"] .${CSS_CLASSES.loading} {
1745
+ background: transparent;
1746
+ }
1747
+
1748
+ .${CSS_CLASSES.container}[data-didit-kind="pending"][data-didit-theme="dark"] .${CSS_CLASSES.loading} circle {
1749
+ stroke: rgba(255, 255, 255, 0.16);
1750
+ }
1751
+
1752
+ .${CSS_CLASSES.container}[data-didit-kind="pending"][data-didit-theme="dark"] .${CSS_CLASSES.loading} path {
1753
+ stroke: #f5f5f7;
1754
+ }
1755
+
1756
+ @media (min-width: 1000px) {
1757
+ .${CSS_CLASSES.container}[data-didit-kind="handoff"],
1758
+ .${CSS_CLASSES.container}[data-didit-kind="pending"] {
1759
+ height: min(660px, calc(100vh * 0.9));
1760
+ height: min(660px, calc(100dvh * 0.9));
1761
+ }
1762
+ }
1763
+
1695
1764
  @media (max-width: 540px) {
1696
1765
  .${CSS_CLASSES.overlay} {
1697
1766
  padding: 0;
@@ -1699,23 +1768,16 @@ class VerificationModal {
1699
1768
 
1700
1769
  .${CSS_CLASSES.container} {
1701
1770
  max-width: 100%;
1702
- max-height: 100vh;
1703
- max-height: 100dvh;
1704
- border-radius: 0;
1705
- }
1706
-
1707
- .${CSS_CLASSES.iframe} {
1708
1771
  height: 100vh;
1709
1772
  height: 100dvh;
1710
- max-height: none;
1773
+ border-radius: 0;
1711
1774
  }
1712
1775
  }
1713
1776
 
1714
1777
  /*
1715
- * Once the hosted flow reports its layout (didit:layout), the modal
1716
- * grows to the handoff card on a computer (960 x 660, at most 90% of
1717
- * the window) and follows the flow's content height in a narrow
1718
- * window. Colours are the design system tokens named beside them.
1778
+ * The hosted flow's layout message (didit:layout) brings the modal's size
1779
+ * (see above), theme and direction. Colours are the design system tokens
1780
+ * named beside them.
1719
1781
  */
1720
1782
  .${CSS_CLASSES.container}.${CSS_CLASSES.flowSized} {
1721
1783
  --didit-ground: #ffffff; /* site-ground */
@@ -1725,7 +1787,6 @@ class VerificationModal {
1725
1787
  --didit-close-fill: rgba(0, 0, 0, 0.06);
1726
1788
  --didit-drop: rgba(17, 24, 39, 0.35);
1727
1789
  --didit-ring: rgba(0, 0, 0, 0.06);
1728
- max-width: 500px;
1729
1790
  background: var(--didit-ground);
1730
1791
  box-shadow: 0 30px 80px -24px var(--didit-drop), 0 0 0 1px var(--didit-ring);
1731
1792
  }
@@ -1740,12 +1801,6 @@ class VerificationModal {
1740
1801
  --didit-ring: rgba(255, 255, 255, 0.08); /* c-shadow-pop, dark */
1741
1802
  }
1742
1803
 
1743
- .${CSS_CLASSES.flowSized} .${CSS_CLASSES.iframe} {
1744
- height: var(--didit-flow-height);
1745
- max-height: 90vh;
1746
- max-height: 90dvh;
1747
- }
1748
-
1749
1804
  .${CSS_CLASSES.closeIcon} {
1750
1805
  display: none;
1751
1806
  }
@@ -1810,34 +1865,20 @@ class VerificationModal {
1810
1865
  max-width: max(300px, 18.75rem);
1811
1866
  margin: auto;
1812
1867
  overflow-wrap: anywhere;
1868
+ /* Focusing the dialog scrolls it into view start-aligned; this keeps
1869
+ * the overlay's top padding visible so the title is never clipped. */
1870
+ scroll-margin-top: 1rem;
1813
1871
  }
1814
1872
 
1815
1873
  .${CSS_CLASSES.flowSized} .didit-confirm-actions {
1816
1874
  flex-wrap: wrap;
1817
1875
  }
1818
1876
 
1819
- @media (min-width: 1024px) {
1820
- .${CSS_CLASSES.container}.${CSS_CLASSES.flowSized} {
1821
- width: 90vw;
1822
- max-width: 960px;
1823
- }
1824
-
1825
- .${CSS_CLASSES.flowSized} .${CSS_CLASSES.iframe} {
1826
- height: 660px;
1827
- }
1828
- }
1829
-
1830
1877
  @media (max-width: 540px) {
1831
1878
  .${CSS_CLASSES.container}.${CSS_CLASSES.flowSized} {
1832
1879
  max-width: 100%;
1833
1880
  box-shadow: none;
1834
1881
  }
1835
-
1836
- .${CSS_CLASSES.flowSized} .${CSS_CLASSES.iframe} {
1837
- height: 100vh;
1838
- height: 100dvh;
1839
- max-height: none;
1840
- }
1841
1882
  }
1842
1883
 
1843
1884
  .${CSS_CLASSES.embedded} {
@@ -2070,12 +2111,13 @@ class VerificationModal {
2070
2111
  */
2071
2112
  readFlowLayout(data) {
2072
2113
  const fields = typeof data === "object" && data !== null ? data : {};
2073
- const { contentHeight, colorScheme, dir } = fields;
2114
+ const { contentHeight, colorScheme, dir, kind } = fields;
2074
2115
  if (typeof contentHeight !== "number" || !Number.isFinite(contentHeight) || contentHeight <= 0) {
2075
2116
  return null;
2076
2117
  }
2077
2118
  const prefersDark = window.matchMedia?.("(prefers-color-scheme: dark)").matches ?? false;
2078
2119
  return {
2120
+ kind: kind === "handoff" ? "handoff" : "step",
2079
2121
  contentHeight: Math.ceil(contentHeight),
2080
2122
  colorScheme: colorScheme === "light" || colorScheme === "dark" ? colorScheme : prefersDark ? "dark" : "light",
2081
2123
  dir: dir === "ltr" || dir === "rtl" ? dir : isRtlLanguage(this.language) ? "rtl" : "ltr"
@@ -2087,18 +2129,113 @@ class VerificationModal {
2087
2129
  return;
2088
2130
  SDKLogger.log("Flow layout:", layout);
2089
2131
  this.container.classList.add(CSS_CLASSES.flowSized);
2090
- this.container.style.setProperty("--didit-flow-height", `${layout.contentHeight}px`);
2091
2132
  this.container.dataset.diditTheme = layout.colorScheme;
2092
2133
  this.container.dataset.diditDir = layout.dir;
2134
+ // The height the person sees now, before the reported content height can change it.
2135
+ const shownHeight = this.container.offsetHeight;
2136
+ this.container.style.setProperty("--didit-content-height", `${layout.contentHeight}px`);
2137
+ this.matchPageBackground(layout.colorScheme);
2138
+ this.setModalKind(layout.kind, layout.kind === "handoff" && this.handoffHeight(layout) === shownHeight);
2139
+ }
2140
+ /** Whether the window is a computer-sized one: a phone is full screen and never waits for a layout. */
2141
+ isPhoneWindow() {
2142
+ return window.matchMedia?.("(max-width: 540px)").matches ?? false;
2143
+ }
2144
+ /** The height the handoff card takes for a layout, as the size rules above compute it (rounded like offsetHeight). */
2145
+ handoffHeight(layout) {
2146
+ const cap = window.innerHeight * 0.9;
2147
+ return Math.round(Math.min(window.innerWidth >= 1000 ? 660 : layout.contentHeight, cap));
2148
+ }
2149
+ /**
2150
+ * Sets what the modal is sized for. Leaving "pending" shows the modal at its final size with no
2151
+ * transition; any later change animates once.
2152
+ */
2153
+ setModalKind(kind, sizeUnchanged = false) {
2154
+ if (!this.container)
2155
+ return;
2156
+ const previous = this.container.dataset.diditKind;
2157
+ if (previous === kind)
2158
+ return;
2159
+ if (this.firstLayoutTimeout !== null && kind !== "pending") {
2160
+ window.clearTimeout(this.firstLayoutTimeout);
2161
+ this.firstLayoutTimeout = null;
2162
+ }
2163
+ // Every change of kind animates, the pending card included (it is on screen), and only for the
2164
+ // length of the change, so resizing the window never lags behind.
2165
+ // Pending already is the handoff card: a handoff first layout of the same size moves nothing,
2166
+ // while a different reported height (a narrow window) animates the correction once.
2167
+ const staysPut = previous === "pending" && kind === "handoff" && sizeUnchanged;
2168
+ if (previous !== undefined && !staysPut)
2169
+ this.animateSizeOnce();
2170
+ this.container.dataset.diditKind = kind;
2171
+ // The final size is in place before the loader goes; a flow still loading keeps its loader.
2172
+ if (previous === "pending" && !this.state.isLoading)
2173
+ this.loadingEl?.classList.add("hidden");
2174
+ }
2175
+ animateSizeOnce() {
2176
+ this.container?.classList.add(CSS_CLASSES.sizeAnimated);
2177
+ if (this.sizeTimeout !== null)
2178
+ window.clearTimeout(this.sizeTimeout);
2179
+ this.sizeTimeout = window.setTimeout(() => {
2180
+ this.sizeTimeout = null;
2181
+ this.container?.classList.remove(CSS_CLASSES.sizeAnimated);
2182
+ }, SIZE_ANIMATION_MS);
2183
+ }
2184
+ /** A flow that reports no layout (an older one) still shows, at step size, after a short wait. */
2185
+ waitForFirstLayout() {
2186
+ if (!this.container)
2187
+ return;
2188
+ // The card looks like the first screen from the first frame, in the device's theme.
2189
+ this.container.classList.add(CSS_CLASSES.flowSized);
2190
+ this.container.dataset.diditTheme = window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
2191
+ this.setModalKind("pending");
2192
+ this.firstLayoutTimeout = window.setTimeout(() => {
2193
+ this.firstLayoutTimeout = null;
2194
+ // A flow that never reported is an older one that paints itself: no card look around it.
2195
+ this.container?.classList.remove(CSS_CLASSES.flowSized);
2196
+ delete this.container?.dataset.diditTheme;
2197
+ this.setModalKind("step");
2198
+ }, FIRST_LAYOUT_TIMEOUT_MS);
2199
+ }
2200
+ /**
2201
+ * On a phone Safari paints the bottom edge of the screen with the page's own background, past
2202
+ * any fixed overlay, and shows it when the keyboard moves the page. A full-screen modal lends
2203
+ * the page its ground colour while it is open, so no strip of the host page shows around it.
2204
+ */
2205
+ matchPageBackground(colorScheme) {
2206
+ if (!window.matchMedia?.("(max-width: 540px)").matches)
2207
+ return;
2208
+ const root = document.documentElement;
2209
+ this.pageBackground ?? (this.pageBackground = { root: root.style.backgroundColor, body: document.body.style.backgroundColor });
2210
+ const ground = colorScheme === "dark" ? "#0b0b0d" : "#ffffff";
2211
+ root.style.backgroundColor = ground;
2212
+ document.body.style.backgroundColor = ground;
2213
+ }
2214
+ restorePageBackground() {
2215
+ if (!this.pageBackground)
2216
+ return;
2217
+ document.documentElement.style.backgroundColor = this.pageBackground.root;
2218
+ document.body.style.backgroundColor = this.pageBackground.body;
2219
+ this.pageBackground = null;
2093
2220
  }
2094
- /** Back to the fixed 500 x 700 modal until the newly loaded flow reports its layout. */
2221
+ /** Back to the neutral look until the newly loaded flow reports its theme and direction. */
2095
2222
  resetFlowLayout() {
2096
2223
  if (this.embedded || !this.container)
2097
2224
  return;
2098
- this.container.classList.remove(CSS_CLASSES.flowSized);
2099
- this.container.style.removeProperty("--didit-flow-height");
2225
+ this.restorePageBackground();
2226
+ this.container.classList.remove(CSS_CLASSES.flowSized, CSS_CLASSES.sizeAnimated);
2100
2227
  delete this.container.dataset.diditTheme;
2101
2228
  delete this.container.dataset.diditDir;
2229
+ delete this.container.dataset.diditKind;
2230
+ this.container.style.removeProperty("--didit-content-height");
2231
+ if (this.firstLayoutTimeout !== null) {
2232
+ window.clearTimeout(this.firstLayoutTimeout);
2233
+ this.firstLayoutTimeout = null;
2234
+ }
2235
+ if (this.sizeTimeout !== null) {
2236
+ window.clearTimeout(this.sizeTimeout);
2237
+ this.sizeTimeout = null;
2238
+ }
2102
2239
  }
2103
2240
  handleKeydown(event) {
2104
2241
  if (!this.state.isOpen)
@@ -2134,7 +2271,9 @@ class VerificationModal {
2134
2271
  handleIframeLoad() {
2135
2272
  if (this.iframe?.src && this.iframe.src !== "about:blank") {
2136
2273
  this.state.isLoading = false;
2137
- this.loadingEl?.classList.add("hidden");
2274
+ // A modal still waiting for its first layout keeps the loader over the hidden flow.
2275
+ if (this.container?.dataset.diditKind !== "pending")
2276
+ this.loadingEl?.classList.add("hidden");
2138
2277
  this.callbacks.onIframeLoad();
2139
2278
  }
2140
2279
  }
@@ -2223,6 +2362,8 @@ class VerificationModal {
2223
2362
  this.hideConfirmation();
2224
2363
  this.loadingEl?.classList.remove("hidden");
2225
2364
  this.resetFlowLayout();
2365
+ if (!this.embedded && !this.isPhoneWindow())
2366
+ this.waitForFirstLayout();
2226
2367
  if (this.iframe) {
2227
2368
  this.iframe.src = verificationUrl;
2228
2369
  }
@@ -2594,6 +2735,23 @@ async function pollTransactionAfterAction(args) {
2594
2735
  return latest;
2595
2736
  }
2596
2737
 
2738
+ /**
2739
+ * Embedded mode renders inside an element that already exists on the
2740
+ * integrator's page. Without it the modal never shows anything (embedded mode
2741
+ * never activates the overlay), so the launch fails with a message the
2742
+ * integrator can act on instead of silently presenting nothing.
2743
+ */
2744
+ function assertEmbeddedHostExists(configuration) {
2745
+ if (!configuration?.embedded)
2746
+ return;
2747
+ const containerId = configuration.embeddedContainerId;
2748
+ if (!containerId) {
2749
+ throw new Error("Embedded mode requires embeddedContainerId: the id of the element that hosts the verification.");
2750
+ }
2751
+ if (typeof document === "undefined" || !document.getElementById(containerId)) {
2752
+ throw new Error(`Embedded container "#${containerId}" was not found in the page.`);
2753
+ }
2754
+ }
2597
2755
  class DiditSdk {
2598
2756
  static get shared() {
2599
2757
  if (!DiditSdk._instance) {
@@ -2653,17 +2811,20 @@ class DiditSdk {
2653
2811
  this._modal.destroy();
2654
2812
  this._modal = null;
2655
2813
  }
2656
- this._modal = new VerificationModal(config, {
2657
- onClose: () => this.handleModalClose(),
2658
- onCloseConfirmed: () => this.handleModalCloseConfirmed(),
2659
- onMessage: (event) => this.handleVerificationEvent(event),
2660
- onIframeLoad: () => this.handleIframeLoad()
2661
- });
2814
+ // A new launch is not in an error state any more, whatever the last one did.
2815
+ this._errorMessage = undefined;
2662
2816
  try {
2663
2817
  const { url } = options;
2664
2818
  if (!url || typeof url !== "string") {
2665
2819
  throw new Error("Invalid options: url is required");
2666
2820
  }
2821
+ assertEmbeddedHostExists(config);
2822
+ this._modal = new VerificationModal(config, {
2823
+ onClose: () => this.handleModalClose(),
2824
+ onCloseConfirmed: () => this.handleModalCloseConfirmed(),
2825
+ onMessage: (event) => this.handleVerificationEvent(event),
2826
+ onIframeLoad: () => this.handleIframeLoad()
2827
+ });
2667
2828
  this._url = buildVerificationUrl(url, config);
2668
2829
  this.setState("loading");
2669
2830
  this.emitInternalEvent("didit:started", {});
@@ -2780,6 +2941,7 @@ class DiditSdk {
2780
2941
  this._modal?.destroy();
2781
2942
  this._modal = null;
2782
2943
  this.reset();
2944
+ this.setState("idle");
2783
2945
  }
2784
2946
  async runTransactionAction(baseUrl, transactionToken, submitResult, onActionCompleted) {
2785
2947
  const generation = this._actionGeneration;
@@ -2895,6 +3057,7 @@ class DiditSdk {
2895
3057
  const sessionData = this.buildSessionData();
2896
3058
  this._modal?.close();
2897
3059
  this.reset();
3060
+ this.setState("idle");
2898
3061
  const result = {
2899
3062
  type: "cancelled",
2900
3063
  session: sessionData
@@ -2915,6 +3078,11 @@ class DiditSdk {
2915
3078
  }
2916
3079
  handleVerificationEvent(event) {
2917
3080
  SDKLogger.log("Verification event:", event);
3081
+ // Keep the session the flow told us about so a modal the user closes by
3082
+ // hand still reports the session it belongs to in the cancelled result.
3083
+ if (event.data?.sessionId) {
3084
+ this._sessionId = event.data.sessionId;
3085
+ }
2918
3086
  this.onEvent?.(event);
2919
3087
  switch (event.type) {
2920
3088
  case "didit:ready":
@@ -2970,6 +3138,7 @@ class DiditSdk {
2970
3138
  if (this._configuration?.closeModalOnComplete) {
2971
3139
  this._modal?.close();
2972
3140
  this.reset();
3141
+ this.setState("idle");
2973
3142
  }
2974
3143
  const result = {
2975
3144
  type: "completed",
@@ -2982,6 +3151,7 @@ class DiditSdk {
2982
3151
  const sessionData = this.buildSessionData(event.data);
2983
3152
  this._modal?.close();
2984
3153
  this.reset();
3154
+ this.setState("idle");
2985
3155
  const result = {
2986
3156
  type: "cancelled",
2987
3157
  session: sessionData
@@ -3000,10 +3170,14 @@ class DiditSdk {
3000
3170
  else {
3001
3171
  verificationError = createVerificationError("unknown", "An unknown error occurred");
3002
3172
  }
3003
- this._errorMessage = verificationError.message;
3004
- this.setState("error");
3005
3173
  this._modal?.close();
3174
+ this._modal = null;
3175
+ // Clear what the failed launch left behind, then report the error as the
3176
+ // state the SDK is in: reset() alone would leave callers reading "idle"
3177
+ // with no error message right after onStateChange("error").
3006
3178
  this.reset();
3179
+ this._errorMessage = verificationError.message;
3180
+ this.setState("error");
3007
3181
  const result = {
3008
3182
  type: "failed",
3009
3183
  error: verificationError
@@ -3019,7 +3193,6 @@ class DiditSdk {
3019
3193
  }
3020
3194
  }
3021
3195
  reset() {
3022
- this._state = "idle";
3023
3196
  this._sessionId = undefined;
3024
3197
  this._url = undefined;
3025
3198
  this._errorMessage = undefined;