@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
  */
@@ -732,6 +732,9 @@
732
732
  if (!isRecord(event) || !event.category || typeof event.actionType !== "string" || !event.actionType.trim()) {
733
733
  throw new DiditEventError("validation", "event.category and event.actionType are required.");
734
734
  }
735
+ if (event.occurredAt instanceof Date && Number.isNaN(event.occurredAt.getTime())) {
736
+ throw new DiditEventError("validation", "event.occurredAt must be a valid date.");
737
+ }
735
738
  if (typeof fetch !== "function") {
736
739
  throw new DiditEventError("network", "fetch is not available in this environment.");
737
740
  }
@@ -790,18 +793,20 @@
790
793
  closeIcon: "didit-close-icon",
791
794
  closeIconLegacy: "didit-close-icon-legacy",
792
795
  flowSized: "didit-flow-sized",
796
+ sizeAnimated: "didit-size-animated",
793
797
  loading: "didit-loading",
794
798
  confirmOverlay: "didit-confirm-overlay",
795
799
  confirmBox: "didit-confirm-box",
796
800
  embedded: "didit-embedded"
797
801
  };
798
- const SDK_VERSION = "0.5.0";
802
+ const SDK_VERSION = "0.5.3";
799
803
  /**
800
804
  * The permissions the verification iframe gets, in a modal and embedded:
801
- * camera and microphone for capture, and clipboard-write for the flow's copy
802
- * buttons.
805
+ * camera and microphone for capture, clipboard-write for the flow's copy
806
+ * buttons, and digital-credentials-get so the flow can ask a digital ID wallet
807
+ * (Google Wallet) through the W3C Digital Credentials API from inside the iframe.
803
808
  */
804
- const IFRAME_ALLOW = "camera; microphone; fullscreen; autoplay; encrypted-media; geolocation; clipboard-write";
809
+ const IFRAME_ALLOW = "camera; microphone; fullscreen; autoplay; encrypted-media; geolocation; clipboard-write; digital-credentials-get";
805
810
  /** The verification-URL query parameter each camera option is forwarded as. */
806
811
  const CAMERA_QUERY_PARAMS = {
807
812
  defaultDocumentCamera: "document_camera",
@@ -822,7 +827,7 @@
822
827
  };
823
828
  /**
824
829
  * Posted by the hosted flow when it lays itself out for the modal, with
825
- * `data: { contentHeight, colorScheme?, dir? }`. The modal then grows to the
830
+ * `data: { contentHeight, colorScheme?, dir?, kind? }` (`kind` is `handoff` or `step`, `step` when missing). The modal then grows to the
826
831
  * handoff card on a computer and follows the content height in a narrow
827
832
  * window. Handled by the modal and never delivered to `onEvent`.
828
833
  */
@@ -1456,6 +1461,10 @@
1456
1461
  return translationsMap[language] ?? EN;
1457
1462
  }
1458
1463
 
1464
+ /** The length of the one animation when the flow moves from the handoff card to a step or back. */
1465
+ const SIZE_ANIMATION_MS = 300;
1466
+ /** How long the modal waits for the first layout before it shows at step size, so it can never hang. */
1467
+ const FIRST_LAYOUT_TIMEOUT_MS = 3000;
1459
1468
  class VerificationModal {
1460
1469
  constructor(configuration, callbacks) {
1461
1470
  this.state = {
@@ -1473,9 +1482,13 @@
1473
1482
  this.boundHandleKeydown = null;
1474
1483
  this.boundHandleFocusChange = null;
1475
1484
  this.confirmationFocusTimeout = null;
1485
+ this.firstLayoutTimeout = null;
1486
+ this.sizeTimeout = null;
1476
1487
  this.embedded = false;
1477
1488
  this.embeddedContainer = null;
1478
1489
  this.language = "en";
1490
+ /** The page's own background colours, kept while a full-screen modal paints over them. */
1491
+ this.pageBackground = null;
1479
1492
  /** body.style.overflow value observed right before this modal locked scroll, restored on close(). */
1480
1493
  this.previousBodyOverflow = null;
1481
1494
  this.modalId = generateModalId();
@@ -1520,9 +1533,9 @@
1520
1533
  .${CSS_CLASSES.container} {
1521
1534
  position: relative;
1522
1535
  width: 100%;
1523
- max-width: 500px;
1524
- max-height: 90vh;
1525
- max-height: 90dvh;
1536
+ max-width: 440px;
1537
+ height: min(860px, calc(100vh - 2rem));
1538
+ height: min(860px, calc(100dvh - 2rem));
1526
1539
  border-radius: 16px;
1527
1540
  overflow: hidden;
1528
1541
  background: transparent;
@@ -1534,9 +1547,7 @@
1534
1547
 
1535
1548
  .${CSS_CLASSES.iframe} {
1536
1549
  width: 100%;
1537
- height: 90vh;
1538
- height: 90dvh;
1539
- max-height: 700px;
1550
+ height: 100%;
1540
1551
  border: none;
1541
1552
  display: block;
1542
1553
  }
@@ -1577,6 +1588,12 @@
1577
1588
  z-index: 5;
1578
1589
  }
1579
1590
 
1591
+ @media (prefers-color-scheme: dark) {
1592
+ .${CSS_CLASSES.loading} {
1593
+ background: #0b0b0d;
1594
+ }
1595
+ }
1596
+
1580
1597
  .${CSS_CLASSES.loading}.hidden {
1581
1598
  display: none;
1582
1599
  }
@@ -1698,6 +1715,58 @@
1698
1715
  outline-offset: 2px;
1699
1716
  }
1700
1717
 
1718
+ /*
1719
+ * Steps are phone-sized (440 x 860, above). On a computer the handoff card is wider
1720
+ * (board Start-Embedded): 960 x 660 on a window of 1000px or more, never wider than the
1721
+ * window less its margin and never taller than 90% of it. In a narrower window the
1722
+ * page stacks and the height follows its content (--didit-content-height, set from the
1723
+ * flow's layout message). Until the flow reports what it shows, the modal is the handoff
1724
+ * card at its final size with the spinner inside it (the flow stays hidden and lays itself
1725
+ * out for that size): it never opens small and grows. The first change of kind (a step
1726
+ * instead of the handoff, or the person continues on this computer) animates once.
1727
+ */
1728
+ @media (min-width: 541px) {
1729
+ .${CSS_CLASSES.container}[data-didit-kind="handoff"],
1730
+ .${CSS_CLASSES.container}[data-didit-kind="pending"] {
1731
+ max-width: min(960px, calc(100vw - 2rem));
1732
+ height: min(var(--didit-content-height, 660px), calc(100vh * 0.9));
1733
+ height: min(var(--didit-content-height, 660px), calc(100dvh * 0.9));
1734
+ }
1735
+
1736
+ .${CSS_CLASSES.container}.${CSS_CLASSES.sizeAnimated} {
1737
+ transition: max-width 0.25s ease, height 0.25s ease;
1738
+ }
1739
+
1740
+ .${CSS_CLASSES.container}[data-didit-kind="pending"] .${CSS_CLASSES.iframe} {
1741
+ visibility: hidden;
1742
+ }
1743
+ }
1744
+
1745
+ /*
1746
+ * While pending the card already looks like the first screen (ground, radius, shadow and
1747
+ * close button come from the flow-sized rules below, with the device theme); the loader
1748
+ * sits inside it on the card's own ground.
1749
+ */
1750
+ .${CSS_CLASSES.container}[data-didit-kind="pending"] .${CSS_CLASSES.loading} {
1751
+ background: transparent;
1752
+ }
1753
+
1754
+ .${CSS_CLASSES.container}[data-didit-kind="pending"][data-didit-theme="dark"] .${CSS_CLASSES.loading} circle {
1755
+ stroke: rgba(255, 255, 255, 0.16);
1756
+ }
1757
+
1758
+ .${CSS_CLASSES.container}[data-didit-kind="pending"][data-didit-theme="dark"] .${CSS_CLASSES.loading} path {
1759
+ stroke: #f5f5f7;
1760
+ }
1761
+
1762
+ @media (min-width: 1000px) {
1763
+ .${CSS_CLASSES.container}[data-didit-kind="handoff"],
1764
+ .${CSS_CLASSES.container}[data-didit-kind="pending"] {
1765
+ height: min(660px, calc(100vh * 0.9));
1766
+ height: min(660px, calc(100dvh * 0.9));
1767
+ }
1768
+ }
1769
+
1701
1770
  @media (max-width: 540px) {
1702
1771
  .${CSS_CLASSES.overlay} {
1703
1772
  padding: 0;
@@ -1705,23 +1774,16 @@
1705
1774
 
1706
1775
  .${CSS_CLASSES.container} {
1707
1776
  max-width: 100%;
1708
- max-height: 100vh;
1709
- max-height: 100dvh;
1710
- border-radius: 0;
1711
- }
1712
-
1713
- .${CSS_CLASSES.iframe} {
1714
1777
  height: 100vh;
1715
1778
  height: 100dvh;
1716
- max-height: none;
1779
+ border-radius: 0;
1717
1780
  }
1718
1781
  }
1719
1782
 
1720
1783
  /*
1721
- * Once the hosted flow reports its layout (didit:layout), the modal
1722
- * grows to the handoff card on a computer (960 x 660, at most 90% of
1723
- * the window) and follows the flow's content height in a narrow
1724
- * window. Colours are the design system tokens named beside them.
1784
+ * The hosted flow's layout message (didit:layout) brings the modal's size
1785
+ * (see above), theme and direction. Colours are the design system tokens
1786
+ * named beside them.
1725
1787
  */
1726
1788
  .${CSS_CLASSES.container}.${CSS_CLASSES.flowSized} {
1727
1789
  --didit-ground: #ffffff; /* site-ground */
@@ -1731,7 +1793,6 @@
1731
1793
  --didit-close-fill: rgba(0, 0, 0, 0.06);
1732
1794
  --didit-drop: rgba(17, 24, 39, 0.35);
1733
1795
  --didit-ring: rgba(0, 0, 0, 0.06);
1734
- max-width: 500px;
1735
1796
  background: var(--didit-ground);
1736
1797
  box-shadow: 0 30px 80px -24px var(--didit-drop), 0 0 0 1px var(--didit-ring);
1737
1798
  }
@@ -1746,12 +1807,6 @@
1746
1807
  --didit-ring: rgba(255, 255, 255, 0.08); /* c-shadow-pop, dark */
1747
1808
  }
1748
1809
 
1749
- .${CSS_CLASSES.flowSized} .${CSS_CLASSES.iframe} {
1750
- height: var(--didit-flow-height);
1751
- max-height: 90vh;
1752
- max-height: 90dvh;
1753
- }
1754
-
1755
1810
  .${CSS_CLASSES.closeIcon} {
1756
1811
  display: none;
1757
1812
  }
@@ -1816,34 +1871,20 @@
1816
1871
  max-width: max(300px, 18.75rem);
1817
1872
  margin: auto;
1818
1873
  overflow-wrap: anywhere;
1874
+ /* Focusing the dialog scrolls it into view start-aligned; this keeps
1875
+ * the overlay's top padding visible so the title is never clipped. */
1876
+ scroll-margin-top: 1rem;
1819
1877
  }
1820
1878
 
1821
1879
  .${CSS_CLASSES.flowSized} .didit-confirm-actions {
1822
1880
  flex-wrap: wrap;
1823
1881
  }
1824
1882
 
1825
- @media (min-width: 1024px) {
1826
- .${CSS_CLASSES.container}.${CSS_CLASSES.flowSized} {
1827
- width: 90vw;
1828
- max-width: 960px;
1829
- }
1830
-
1831
- .${CSS_CLASSES.flowSized} .${CSS_CLASSES.iframe} {
1832
- height: 660px;
1833
- }
1834
- }
1835
-
1836
1883
  @media (max-width: 540px) {
1837
1884
  .${CSS_CLASSES.container}.${CSS_CLASSES.flowSized} {
1838
1885
  max-width: 100%;
1839
1886
  box-shadow: none;
1840
1887
  }
1841
-
1842
- .${CSS_CLASSES.flowSized} .${CSS_CLASSES.iframe} {
1843
- height: 100vh;
1844
- height: 100dvh;
1845
- max-height: none;
1846
- }
1847
1888
  }
1848
1889
 
1849
1890
  .${CSS_CLASSES.embedded} {
@@ -2076,12 +2117,13 @@
2076
2117
  */
2077
2118
  readFlowLayout(data) {
2078
2119
  const fields = typeof data === "object" && data !== null ? data : {};
2079
- const { contentHeight, colorScheme, dir } = fields;
2120
+ const { contentHeight, colorScheme, dir, kind } = fields;
2080
2121
  if (typeof contentHeight !== "number" || !Number.isFinite(contentHeight) || contentHeight <= 0) {
2081
2122
  return null;
2082
2123
  }
2083
2124
  const prefersDark = window.matchMedia?.("(prefers-color-scheme: dark)").matches ?? false;
2084
2125
  return {
2126
+ kind: kind === "handoff" ? "handoff" : "step",
2085
2127
  contentHeight: Math.ceil(contentHeight),
2086
2128
  colorScheme: colorScheme === "light" || colorScheme === "dark" ? colorScheme : prefersDark ? "dark" : "light",
2087
2129
  dir: dir === "ltr" || dir === "rtl" ? dir : isRtlLanguage(this.language) ? "rtl" : "ltr"
@@ -2093,18 +2135,113 @@
2093
2135
  return;
2094
2136
  SDKLogger.log("Flow layout:", layout);
2095
2137
  this.container.classList.add(CSS_CLASSES.flowSized);
2096
- this.container.style.setProperty("--didit-flow-height", `${layout.contentHeight}px`);
2097
2138
  this.container.dataset.diditTheme = layout.colorScheme;
2098
2139
  this.container.dataset.diditDir = layout.dir;
2140
+ // The height the person sees now, before the reported content height can change it.
2141
+ const shownHeight = this.container.offsetHeight;
2142
+ this.container.style.setProperty("--didit-content-height", `${layout.contentHeight}px`);
2143
+ this.matchPageBackground(layout.colorScheme);
2144
+ this.setModalKind(layout.kind, layout.kind === "handoff" && this.handoffHeight(layout) === shownHeight);
2145
+ }
2146
+ /** Whether the window is a computer-sized one: a phone is full screen and never waits for a layout. */
2147
+ isPhoneWindow() {
2148
+ return window.matchMedia?.("(max-width: 540px)").matches ?? false;
2149
+ }
2150
+ /** The height the handoff card takes for a layout, as the size rules above compute it (rounded like offsetHeight). */
2151
+ handoffHeight(layout) {
2152
+ const cap = window.innerHeight * 0.9;
2153
+ return Math.round(Math.min(window.innerWidth >= 1000 ? 660 : layout.contentHeight, cap));
2154
+ }
2155
+ /**
2156
+ * Sets what the modal is sized for. Leaving "pending" shows the modal at its final size with no
2157
+ * transition; any later change animates once.
2158
+ */
2159
+ setModalKind(kind, sizeUnchanged = false) {
2160
+ if (!this.container)
2161
+ return;
2162
+ const previous = this.container.dataset.diditKind;
2163
+ if (previous === kind)
2164
+ return;
2165
+ if (this.firstLayoutTimeout !== null && kind !== "pending") {
2166
+ window.clearTimeout(this.firstLayoutTimeout);
2167
+ this.firstLayoutTimeout = null;
2168
+ }
2169
+ // Every change of kind animates, the pending card included (it is on screen), and only for the
2170
+ // length of the change, so resizing the window never lags behind.
2171
+ // Pending already is the handoff card: a handoff first layout of the same size moves nothing,
2172
+ // while a different reported height (a narrow window) animates the correction once.
2173
+ const staysPut = previous === "pending" && kind === "handoff" && sizeUnchanged;
2174
+ if (previous !== undefined && !staysPut)
2175
+ this.animateSizeOnce();
2176
+ this.container.dataset.diditKind = kind;
2177
+ // The final size is in place before the loader goes; a flow still loading keeps its loader.
2178
+ if (previous === "pending" && !this.state.isLoading)
2179
+ this.loadingEl?.classList.add("hidden");
2180
+ }
2181
+ animateSizeOnce() {
2182
+ this.container?.classList.add(CSS_CLASSES.sizeAnimated);
2183
+ if (this.sizeTimeout !== null)
2184
+ window.clearTimeout(this.sizeTimeout);
2185
+ this.sizeTimeout = window.setTimeout(() => {
2186
+ this.sizeTimeout = null;
2187
+ this.container?.classList.remove(CSS_CLASSES.sizeAnimated);
2188
+ }, SIZE_ANIMATION_MS);
2189
+ }
2190
+ /** A flow that reports no layout (an older one) still shows, at step size, after a short wait. */
2191
+ waitForFirstLayout() {
2192
+ if (!this.container)
2193
+ return;
2194
+ // The card looks like the first screen from the first frame, in the device's theme.
2195
+ this.container.classList.add(CSS_CLASSES.flowSized);
2196
+ this.container.dataset.diditTheme = window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
2197
+ this.setModalKind("pending");
2198
+ this.firstLayoutTimeout = window.setTimeout(() => {
2199
+ this.firstLayoutTimeout = null;
2200
+ // A flow that never reported is an older one that paints itself: no card look around it.
2201
+ this.container?.classList.remove(CSS_CLASSES.flowSized);
2202
+ delete this.container?.dataset.diditTheme;
2203
+ this.setModalKind("step");
2204
+ }, FIRST_LAYOUT_TIMEOUT_MS);
2205
+ }
2206
+ /**
2207
+ * On a phone Safari paints the bottom edge of the screen with the page's own background, past
2208
+ * any fixed overlay, and shows it when the keyboard moves the page. A full-screen modal lends
2209
+ * the page its ground colour while it is open, so no strip of the host page shows around it.
2210
+ */
2211
+ matchPageBackground(colorScheme) {
2212
+ if (!window.matchMedia?.("(max-width: 540px)").matches)
2213
+ return;
2214
+ const root = document.documentElement;
2215
+ this.pageBackground ?? (this.pageBackground = { root: root.style.backgroundColor, body: document.body.style.backgroundColor });
2216
+ const ground = colorScheme === "dark" ? "#0b0b0d" : "#ffffff";
2217
+ root.style.backgroundColor = ground;
2218
+ document.body.style.backgroundColor = ground;
2219
+ }
2220
+ restorePageBackground() {
2221
+ if (!this.pageBackground)
2222
+ return;
2223
+ document.documentElement.style.backgroundColor = this.pageBackground.root;
2224
+ document.body.style.backgroundColor = this.pageBackground.body;
2225
+ this.pageBackground = null;
2099
2226
  }
2100
- /** Back to the fixed 500 x 700 modal until the newly loaded flow reports its layout. */
2227
+ /** Back to the neutral look until the newly loaded flow reports its theme and direction. */
2101
2228
  resetFlowLayout() {
2102
2229
  if (this.embedded || !this.container)
2103
2230
  return;
2104
- this.container.classList.remove(CSS_CLASSES.flowSized);
2105
- this.container.style.removeProperty("--didit-flow-height");
2231
+ this.restorePageBackground();
2232
+ this.container.classList.remove(CSS_CLASSES.flowSized, CSS_CLASSES.sizeAnimated);
2106
2233
  delete this.container.dataset.diditTheme;
2107
2234
  delete this.container.dataset.diditDir;
2235
+ delete this.container.dataset.diditKind;
2236
+ this.container.style.removeProperty("--didit-content-height");
2237
+ if (this.firstLayoutTimeout !== null) {
2238
+ window.clearTimeout(this.firstLayoutTimeout);
2239
+ this.firstLayoutTimeout = null;
2240
+ }
2241
+ if (this.sizeTimeout !== null) {
2242
+ window.clearTimeout(this.sizeTimeout);
2243
+ this.sizeTimeout = null;
2244
+ }
2108
2245
  }
2109
2246
  handleKeydown(event) {
2110
2247
  if (!this.state.isOpen)
@@ -2140,7 +2277,9 @@
2140
2277
  handleIframeLoad() {
2141
2278
  if (this.iframe?.src && this.iframe.src !== "about:blank") {
2142
2279
  this.state.isLoading = false;
2143
- this.loadingEl?.classList.add("hidden");
2280
+ // A modal still waiting for its first layout keeps the loader over the hidden flow.
2281
+ if (this.container?.dataset.diditKind !== "pending")
2282
+ this.loadingEl?.classList.add("hidden");
2144
2283
  this.callbacks.onIframeLoad();
2145
2284
  }
2146
2285
  }
@@ -2229,6 +2368,8 @@
2229
2368
  this.hideConfirmation();
2230
2369
  this.loadingEl?.classList.remove("hidden");
2231
2370
  this.resetFlowLayout();
2371
+ if (!this.embedded && !this.isPhoneWindow())
2372
+ this.waitForFirstLayout();
2232
2373
  if (this.iframe) {
2233
2374
  this.iframe.src = verificationUrl;
2234
2375
  }
@@ -2600,6 +2741,23 @@
2600
2741
  return latest;
2601
2742
  }
2602
2743
 
2744
+ /**
2745
+ * Embedded mode renders inside an element that already exists on the
2746
+ * integrator's page. Without it the modal never shows anything (embedded mode
2747
+ * never activates the overlay), so the launch fails with a message the
2748
+ * integrator can act on instead of silently presenting nothing.
2749
+ */
2750
+ function assertEmbeddedHostExists(configuration) {
2751
+ if (!configuration?.embedded)
2752
+ return;
2753
+ const containerId = configuration.embeddedContainerId;
2754
+ if (!containerId) {
2755
+ throw new Error("Embedded mode requires embeddedContainerId: the id of the element that hosts the verification.");
2756
+ }
2757
+ if (typeof document === "undefined" || !document.getElementById(containerId)) {
2758
+ throw new Error(`Embedded container "#${containerId}" was not found in the page.`);
2759
+ }
2760
+ }
2603
2761
  class DiditSdk {
2604
2762
  static get shared() {
2605
2763
  if (!DiditSdk._instance) {
@@ -2659,17 +2817,20 @@
2659
2817
  this._modal.destroy();
2660
2818
  this._modal = null;
2661
2819
  }
2662
- this._modal = new VerificationModal(config, {
2663
- onClose: () => this.handleModalClose(),
2664
- onCloseConfirmed: () => this.handleModalCloseConfirmed(),
2665
- onMessage: (event) => this.handleVerificationEvent(event),
2666
- onIframeLoad: () => this.handleIframeLoad()
2667
- });
2820
+ // A new launch is not in an error state any more, whatever the last one did.
2821
+ this._errorMessage = undefined;
2668
2822
  try {
2669
2823
  const { url } = options;
2670
2824
  if (!url || typeof url !== "string") {
2671
2825
  throw new Error("Invalid options: url is required");
2672
2826
  }
2827
+ assertEmbeddedHostExists(config);
2828
+ this._modal = new VerificationModal(config, {
2829
+ onClose: () => this.handleModalClose(),
2830
+ onCloseConfirmed: () => this.handleModalCloseConfirmed(),
2831
+ onMessage: (event) => this.handleVerificationEvent(event),
2832
+ onIframeLoad: () => this.handleIframeLoad()
2833
+ });
2673
2834
  this._url = buildVerificationUrl(url, config);
2674
2835
  this.setState("loading");
2675
2836
  this.emitInternalEvent("didit:started", {});
@@ -2786,6 +2947,7 @@
2786
2947
  this._modal?.destroy();
2787
2948
  this._modal = null;
2788
2949
  this.reset();
2950
+ this.setState("idle");
2789
2951
  }
2790
2952
  async runTransactionAction(baseUrl, transactionToken, submitResult, onActionCompleted) {
2791
2953
  const generation = this._actionGeneration;
@@ -2901,6 +3063,7 @@
2901
3063
  const sessionData = this.buildSessionData();
2902
3064
  this._modal?.close();
2903
3065
  this.reset();
3066
+ this.setState("idle");
2904
3067
  const result = {
2905
3068
  type: "cancelled",
2906
3069
  session: sessionData
@@ -2921,6 +3084,11 @@
2921
3084
  }
2922
3085
  handleVerificationEvent(event) {
2923
3086
  SDKLogger.log("Verification event:", event);
3087
+ // Keep the session the flow told us about so a modal the user closes by
3088
+ // hand still reports the session it belongs to in the cancelled result.
3089
+ if (event.data?.sessionId) {
3090
+ this._sessionId = event.data.sessionId;
3091
+ }
2924
3092
  this.onEvent?.(event);
2925
3093
  switch (event.type) {
2926
3094
  case "didit:ready":
@@ -2976,6 +3144,7 @@
2976
3144
  if (this._configuration?.closeModalOnComplete) {
2977
3145
  this._modal?.close();
2978
3146
  this.reset();
3147
+ this.setState("idle");
2979
3148
  }
2980
3149
  const result = {
2981
3150
  type: "completed",
@@ -2988,6 +3157,7 @@
2988
3157
  const sessionData = this.buildSessionData(event.data);
2989
3158
  this._modal?.close();
2990
3159
  this.reset();
3160
+ this.setState("idle");
2991
3161
  const result = {
2992
3162
  type: "cancelled",
2993
3163
  session: sessionData
@@ -3006,10 +3176,14 @@
3006
3176
  else {
3007
3177
  verificationError = createVerificationError("unknown", "An unknown error occurred");
3008
3178
  }
3009
- this._errorMessage = verificationError.message;
3010
- this.setState("error");
3011
3179
  this._modal?.close();
3180
+ this._modal = null;
3181
+ // Clear what the failed launch left behind, then report the error as the
3182
+ // state the SDK is in: reset() alone would leave callers reading "idle"
3183
+ // with no error message right after onStateChange("error").
3012
3184
  this.reset();
3185
+ this._errorMessage = verificationError.message;
3186
+ this.setState("error");
3013
3187
  const result = {
3014
3188
  type: "failed",
3015
3189
  error: verificationError
@@ -3025,7 +3199,6 @@
3025
3199
  }
3026
3200
  }
3027
3201
  reset() {
3028
- this._state = "idle";
3029
3202
  this._sessionId = undefined;
3030
3203
  this._url = undefined;
3031
3204
  this._errorMessage = undefined;