@didit-protocol/sdk-web 0.5.3 → 0.7.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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Didit SDK for Web v0.5.3
2
+ * Didit SDK for Web v0.7.0
3
3
  * (c) 2026 Didit
4
4
  * @license MIT
5
5
  */
@@ -26,9 +26,10 @@ const CSS_CLASSES = {
26
26
  loading: "didit-loading",
27
27
  confirmOverlay: "didit-confirm-overlay",
28
28
  confirmBox: "didit-confirm-box",
29
- embedded: "didit-embedded"
29
+ embedded: "didit-embedded",
30
+ curtain: "didit-modal-curtain"
30
31
  };
31
- const SDK_VERSION = "0.5.3";
32
+ const SDK_VERSION = "0.7.0";
32
33
  /**
33
34
  * The permissions the verification iframe gets, in a modal and embedded:
34
35
  * camera and microphone for capture, clipboard-write for the flow's copy
@@ -54,6 +55,13 @@ const PRESENTATION_QUERY_PARAMS = {
54
55
  presentation: "sdk_presentation",
55
56
  closeButton: "sdk_close_button"
56
57
  };
58
+ /**
59
+ * The verification-URL query parameter that carries the device id this page
60
+ * already keeps for Didit, when it keeps one. A browser gives the flow in the
61
+ * frame storage of its own, apart from this page's, so without it a
62
+ * verification and this site's events never share a device id.
63
+ */
64
+ const DEVICE_ID_QUERY_PARAM = "sdk_device_id";
57
65
  /**
58
66
  * Posted by the hosted flow when it lays itself out for the modal, with
59
67
  * `data: { contentHeight, colorScheme?, dir?, kind? }` (`kind` is `handoff` or `step`, `step` when missing). The modal then grows to the
@@ -180,15 +188,21 @@ const CAMERA_SWITCH_OPTIONS = ["showDocumentCameraSwitchButton", "showLivenessCa
180
188
  * `showDocumentCameraSwitchButton` -> `document_camera_switch=true|false`,
181
189
  * `showLivenessCameraSwitchButton` -> `liveness_camera_switch=true|false`.
182
190
  * How the SDK presents the flow is always added: `sdk_presentation=modal|embedded`
183
- * and, in a modal, `sdk_close_button=true|false`.
191
+ * and, in a modal, `sdk_close_button=true|false`. `storedDeviceId`, when the
192
+ * page has one, is added as `sdk_device_id`.
184
193
  * Each parameter is set (not appended) so the configuration wins over a value
185
194
  * already on the URL; existing query parameters and the hash are kept. A
186
195
  * value of the wrong shape is ignored with a warning rather than forwarded,
187
196
  * and a URL the browser cannot parse is returned as it came (the modal
188
197
  * reports it when it loads).
189
198
  */
190
- function buildVerificationUrl(url, configuration = {}) {
199
+ function buildVerificationUrl(url, configuration = {}, storedDeviceId = null) {
191
200
  const params = presentationParams(configuration);
201
+ // The id this page already keeps for Didit (see getStoredDeviceId): the flow in the frame, whose
202
+ // own storage the browser keeps apart from this page's, can then be told apart as the same
203
+ // device as this site's events. Absent when the page keeps none; nothing is created for it.
204
+ if (storedDeviceId)
205
+ params.push([DEVICE_ID_QUERY_PARAM, storedDeviceId]);
192
206
  for (const option of CAMERA_LENS_OPTIONS) {
193
207
  const value = configuration[option];
194
208
  if (value === undefined)
@@ -694,6 +708,10 @@ function getTranslations(language) {
694
708
  const SIZE_ANIMATION_MS = 300;
695
709
  /** How long the modal waits for the first layout before it shows at step size, so it can never hang. */
696
710
  const FIRST_LAYOUT_TIMEOUT_MS = 3000;
711
+ /** A strip is a line or two of text: a taller value is not one, and would push the close button out of reach. */
712
+ const MAX_TOP_INSET = 160;
713
+ /** The flow's ground (site-ground in the design system), which a full-screen modal paints the page with. */
714
+ const FLOW_GROUND = { light: "#ffffff", dark: "#0b0b0d" };
697
715
  class VerificationModal {
698
716
  constructor(configuration, callbacks) {
699
717
  this.state = {
@@ -716,10 +734,9 @@ class VerificationModal {
716
734
  this.embedded = false;
717
735
  this.embeddedContainer = null;
718
736
  this.language = "en";
719
- /** The page's own background colours, kept while a full-screen modal paints over them. */
720
- this.pageBackground = null;
721
- /** body.style.overflow value observed right before this modal locked scroll, restored on close(). */
722
- this.previousBodyOverflow = null;
737
+ /** The ground below the layout viewport (see showCurtain), while this modal is open. */
738
+ this.curtain = null;
739
+ this.boundPlaceCurtain = null;
723
740
  this.modalId = generateModalId();
724
741
  this.config = {
725
742
  zIndex: configuration?.zIndex ?? DEFAULT_CONFIG.zIndex,
@@ -813,13 +830,14 @@ class VerificationModal {
813
830
  display: flex;
814
831
  align-items: center;
815
832
  justify-content: center;
816
- background: #fafafa;
833
+ /* The flow's ground, so a phone's loader meets the ground below the viewport without a seam. */
834
+ background: ${FLOW_GROUND.light};
817
835
  z-index: 5;
818
836
  }
819
837
 
820
838
  @media (prefers-color-scheme: dark) {
821
839
  .${CSS_CLASSES.loading} {
822
- background: #0b0b0d;
840
+ background: ${FLOW_GROUND.dark};
823
841
  }
824
842
  }
825
843
 
@@ -1009,6 +1027,40 @@ class VerificationModal {
1009
1027
  }
1010
1028
  }
1011
1029
 
1030
+ /*
1031
+ * The flow's ground right below the layout viewport, in the page itself (see showCurtain):
1032
+ * on a phone, iOS browsers with their toolbars collapsed show a band of the page there,
1033
+ * below every fixed element, the full-screen modal included.
1034
+ */
1035
+ .${CSS_CLASSES.curtain} {
1036
+ display: none;
1037
+ position: absolute;
1038
+ top: 0;
1039
+ left: 0;
1040
+ width: 100vw;
1041
+ height: 100vh;
1042
+ margin: 0;
1043
+ pointer-events: none;
1044
+ /* Under the modal where they meet for a frame while the toolbars move, above the page. */
1045
+ z-index: ${this.config.zIndex - 1};
1046
+ background: ${FLOW_GROUND.light};
1047
+ }
1048
+
1049
+ .${CSS_CLASSES.curtain}[data-didit-theme="dark"] {
1050
+ background: ${FLOW_GROUND.dark};
1051
+ }
1052
+
1053
+ /* The exit confirmation's dimming continues over the ground below the viewport. */
1054
+ .${CSS_CLASSES.curtain}[data-didit-dimmed] {
1055
+ background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5));
1056
+ }
1057
+
1058
+ @media (max-width: 540px) {
1059
+ .${CSS_CLASSES.curtain} {
1060
+ display: block;
1061
+ }
1062
+ }
1063
+
1012
1064
  /*
1013
1065
  * The hosted flow's layout message (didit:layout) brings the modal's size
1014
1066
  * (see above), theme and direction. Colours are the design system tokens
@@ -1041,7 +1093,7 @@ class VerificationModal {
1041
1093
  }
1042
1094
 
1043
1095
  .${CSS_CLASSES.flowSized} .${CSS_CLASSES.closeButton} {
1044
- top: 8px;
1096
+ top: calc(8px + var(--didit-top-inset, 0px));
1045
1097
  right: 8px;
1046
1098
  width: 44px;
1047
1099
  height: 44px;
@@ -1342,11 +1394,12 @@ class VerificationModal {
1342
1394
  /**
1343
1395
  * Reads a didit:layout message's data. Without a positive contentHeight the
1344
1396
  * message is ignored; a missing or unknown colorScheme follows the device,
1345
- * and a missing or unknown dir follows the verification URL's language.
1397
+ * a missing or unknown dir follows the verification URL's language, and the
1398
+ * close button keeps its place without a plausible topInset.
1346
1399
  */
1347
1400
  readFlowLayout(data) {
1348
1401
  const fields = typeof data === "object" && data !== null ? data : {};
1349
- const { contentHeight, colorScheme, dir, kind } = fields;
1402
+ const { contentHeight, colorScheme, dir, kind, topInset } = fields;
1350
1403
  if (typeof contentHeight !== "number" || !Number.isFinite(contentHeight) || contentHeight <= 0) {
1351
1404
  return null;
1352
1405
  }
@@ -1355,7 +1408,8 @@ class VerificationModal {
1355
1408
  kind: kind === "handoff" ? "handoff" : "step",
1356
1409
  contentHeight: Math.ceil(contentHeight),
1357
1410
  colorScheme: colorScheme === "light" || colorScheme === "dark" ? colorScheme : prefersDark ? "dark" : "light",
1358
- dir: dir === "ltr" || dir === "rtl" ? dir : isRtlLanguage(this.language) ? "rtl" : "ltr"
1411
+ dir: dir === "ltr" || dir === "rtl" ? dir : isRtlLanguage(this.language) ? "rtl" : "ltr",
1412
+ topInset: typeof topInset === "number" && topInset > 0 && topInset <= MAX_TOP_INSET ? Math.ceil(topInset) : 0
1359
1413
  };
1360
1414
  }
1361
1415
  /** Embedded mode keeps filling the integrator's host element, whatever the flow reports. */
@@ -1369,7 +1423,11 @@ class VerificationModal {
1369
1423
  // The height the person sees now, before the reported content height can change it.
1370
1424
  const shownHeight = this.container.offsetHeight;
1371
1425
  this.container.style.setProperty("--didit-content-height", `${layout.contentHeight}px`);
1426
+ // The close button sits under the flow's strip, over its top bar.
1427
+ this.container.style.setProperty("--didit-top-inset", `${layout.topInset}px`);
1372
1428
  this.matchPageBackground(layout.colorScheme);
1429
+ if (this.curtain)
1430
+ this.curtain.dataset.diditTheme = layout.colorScheme;
1373
1431
  this.setModalKind(layout.kind, layout.kind === "handoff" && this.handoffHeight(layout) === shownHeight);
1374
1432
  }
1375
1433
  /** Whether the window is a computer-sized one: a phone is full screen and never waits for a layout. */
@@ -1440,18 +1498,125 @@ class VerificationModal {
1440
1498
  matchPageBackground(colorScheme) {
1441
1499
  if (!window.matchMedia?.("(max-width: 540px)").matches)
1442
1500
  return;
1443
- const root = document.documentElement;
1444
- this.pageBackground ?? (this.pageBackground = { root: root.style.backgroundColor, body: document.body.style.backgroundColor });
1445
- const ground = colorScheme === "dark" ? "#0b0b0d" : "#ffffff";
1446
- root.style.backgroundColor = ground;
1447
- document.body.style.backgroundColor = ground;
1501
+ VerificationModal.pageBackground ?? (VerificationModal.pageBackground = {
1502
+ saved: { root: document.documentElement.style.backgroundColor, body: document.body.style.backgroundColor },
1503
+ grounds: new Map()
1504
+ });
1505
+ VerificationModal.pageBackground.grounds.set(this, colorScheme);
1506
+ VerificationModal.paintPageBackground();
1448
1507
  }
1449
1508
  restorePageBackground() {
1450
- if (!this.pageBackground)
1509
+ const background = VerificationModal.pageBackground;
1510
+ if (!background?.grounds.delete(this))
1511
+ return;
1512
+ if (background.grounds.size > 0) {
1513
+ VerificationModal.paintPageBackground();
1451
1514
  return;
1452
- document.documentElement.style.backgroundColor = this.pageBackground.root;
1453
- document.body.style.backgroundColor = this.pageBackground.body;
1454
- this.pageBackground = null;
1515
+ }
1516
+ VerificationModal.pageBackground = null;
1517
+ document.documentElement.style.backgroundColor = background.saved.root;
1518
+ document.body.style.backgroundColor = background.saved.body;
1519
+ }
1520
+ /** The page shows the ground of the topmost open modal that lends one. */
1521
+ static paintPageBackground() {
1522
+ const background = VerificationModal.pageBackground;
1523
+ if (!background)
1524
+ return;
1525
+ const lender = [...VerificationModal.openModals].reverse().find((modal) => background.grounds.has(modal));
1526
+ const colorScheme = lender ? background.grounds.get(lender) : [...background.grounds.values()].pop();
1527
+ if (!colorScheme)
1528
+ return;
1529
+ document.documentElement.style.backgroundColor = FLOW_GROUND[colorScheme];
1530
+ document.body.style.backgroundColor = FLOW_GROUND[colorScheme];
1531
+ }
1532
+ /**
1533
+ * Stops the page scrolling under the modal. The lock is on the root element: Chrome for iOS keeps
1534
+ * scrolling a page whose body alone is `overflow: hidden` (a swipe over the modal scrolls the page
1535
+ * and toggles the browser's toolbars), and stops only when the root element is. A page that
1536
+ * scrolls its body instead of the window has its body locked too. The first modal to open keeps
1537
+ * the page's own values and the last one to close gives them back.
1538
+ */
1539
+ lockPageScroll() {
1540
+ const lock = VerificationModal.pageScrollLock;
1541
+ if (lock) {
1542
+ lock.holders.add(this);
1543
+ return;
1544
+ }
1545
+ const root = document.documentElement;
1546
+ const body = document.body;
1547
+ // The body's overflow is the viewport's only while the root's is visible; otherwise a body with
1548
+ // an overflow of its own is a scroll container. Locking a body that is not one would clip the page
1549
+ // to the body's box.
1550
+ const clips = (overflow) => overflow !== "" && overflow !== "visible";
1551
+ const bodyScrollsItself = clips(window.getComputedStyle(root).overflowY) && clips(window.getComputedStyle(body).overflowY);
1552
+ VerificationModal.pageScrollLock = {
1553
+ holders: new Set([this]),
1554
+ root: { overflow: root.style.overflow, overscrollBehavior: root.style.overscrollBehavior },
1555
+ body: bodyScrollsItself ? { overflow: body.style.overflow } : null
1556
+ };
1557
+ root.style.overflow = "hidden";
1558
+ root.style.overscrollBehavior = "none";
1559
+ if (bodyScrollsItself)
1560
+ body.style.overflow = "hidden";
1561
+ }
1562
+ unlockPageScroll() {
1563
+ const lock = VerificationModal.pageScrollLock;
1564
+ if (!lock || !lock.holders.delete(this) || lock.holders.size > 0)
1565
+ return;
1566
+ VerificationModal.pageScrollLock = null;
1567
+ const root = document.documentElement;
1568
+ root.style.overflow = lock.root.overflow;
1569
+ root.style.overscrollBehavior = lock.root.overscrollBehavior;
1570
+ if (lock.body)
1571
+ document.body.style.overflow = lock.body.overflow;
1572
+ }
1573
+ /**
1574
+ * On a phone, Chrome for iOS (and the other iOS browsers built on WebKit's web view) shows a band
1575
+ * of the page below the layout viewport while its toolbars are collapsed: the home indicator's
1576
+ * area, and more while the toolbars move. Fixed elements end at the layout viewport, so the
1577
+ * full-screen modal cannot cover it and the host page's own content showed under the flow. The
1578
+ * curtain is part of the page instead: right below the viewport, in the flow's ground, so that band
1579
+ * shows the modal's ground. The page cannot scroll while the modal is open (lockPageScroll), and the
1580
+ * curtain follows the viewport when it resizes. Only phone-sized windows draw it (see the styles).
1581
+ */
1582
+ showCurtain() {
1583
+ if (!this.curtain) {
1584
+ const curtain = document.createElement("div");
1585
+ curtain.className = CSS_CLASSES.curtain;
1586
+ curtain.setAttribute("aria-hidden", "true");
1587
+ document.body.appendChild(curtain);
1588
+ this.curtain = curtain;
1589
+ this.boundPlaceCurtain = () => this.placeCurtain();
1590
+ window.addEventListener("resize", this.boundPlaceCurtain);
1591
+ window.addEventListener("scroll", this.boundPlaceCurtain);
1592
+ window.visualViewport?.addEventListener("resize", this.boundPlaceCurtain);
1593
+ }
1594
+ // The device's theme until the newly loaded flow reports its own (applyFlowLayout).
1595
+ this.curtain.dataset.diditTheme = window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
1596
+ this.placeCurtain();
1597
+ }
1598
+ /** Puts the curtain's top at the bottom of the layout viewport, whatever its containing block is. */
1599
+ placeCurtain() {
1600
+ const curtain = this.curtain;
1601
+ if (!curtain)
1602
+ return;
1603
+ curtain.style.top = "0px";
1604
+ curtain.style.left = "0px";
1605
+ // At 0, 0 the curtain's box starts at its containing block's corner (the page's, or a positioned
1606
+ // body's), in viewport coordinates: the offset that brings its top to the viewport's bottom edge.
1607
+ const origin = curtain.getBoundingClientRect();
1608
+ curtain.style.top = `${window.innerHeight - origin.top}px`;
1609
+ curtain.style.left = `${-origin.left}px`;
1610
+ }
1611
+ removeCurtain() {
1612
+ if (this.boundPlaceCurtain) {
1613
+ window.removeEventListener("resize", this.boundPlaceCurtain);
1614
+ window.removeEventListener("scroll", this.boundPlaceCurtain);
1615
+ window.visualViewport?.removeEventListener("resize", this.boundPlaceCurtain);
1616
+ this.boundPlaceCurtain = null;
1617
+ }
1618
+ this.curtain?.remove();
1619
+ this.curtain = null;
1455
1620
  }
1456
1621
  /** Back to the neutral look until the newly loaded flow reports its theme and direction. */
1457
1622
  resetFlowLayout() {
@@ -1463,6 +1628,7 @@ class VerificationModal {
1463
1628
  delete this.container.dataset.diditDir;
1464
1629
  delete this.container.dataset.diditKind;
1465
1630
  this.container.style.removeProperty("--didit-content-height");
1631
+ this.container.style.removeProperty("--didit-top-inset");
1466
1632
  if (this.firstLayoutTimeout !== null) {
1467
1633
  window.clearTimeout(this.firstLayoutTimeout);
1468
1634
  this.firstLayoutTimeout = null;
@@ -1529,6 +1695,7 @@ class VerificationModal {
1529
1695
  : this.container?.querySelector(`.${CSS_CLASSES.closeButton}`) ?? this.iframe);
1530
1696
  this.state.showConfirmation = true;
1531
1697
  this.confirmOverlay.classList.add("active");
1698
+ this.curtain?.setAttribute("data-didit-dimmed", "");
1532
1699
  if (this.iframe)
1533
1700
  this.iframe.inert = true;
1534
1701
  const close = this.container?.querySelector(`.${CSS_CLASSES.closeButton}`);
@@ -1552,6 +1719,7 @@ class VerificationModal {
1552
1719
  return;
1553
1720
  this.state.showConfirmation = false;
1554
1721
  this.confirmOverlay?.classList.remove("active");
1722
+ this.curtain?.removeAttribute("data-didit-dimmed");
1555
1723
  if (this.iframe)
1556
1724
  this.iframe.inert = false;
1557
1725
  const close = this.container?.querySelector(`.${CSS_CLASSES.closeButton}`);
@@ -1579,11 +1747,6 @@ class VerificationModal {
1579
1747
  this.callbacks.onCloseConfirmed();
1580
1748
  }
1581
1749
  open(verificationUrl) {
1582
- // Whether this modal already held its scroll lock before this call. Re-opening
1583
- // an already-open instance must not re-capture previousBodyOverflow, or it
1584
- // would record this modal's own "hidden" lock and close() could never restore
1585
- // the real pre-lock value.
1586
- const wasAlreadyOpen = this.state.isOpen;
1587
1750
  this.language = detectLanguageFromUrl(verificationUrl);
1588
1751
  if (!this.overlay && !this.container) {
1589
1752
  this.createDOM();
@@ -1608,16 +1771,10 @@ class VerificationModal {
1608
1771
  return;
1609
1772
  }
1610
1773
  this.overlay?.classList.add("active");
1611
- // Capture whatever overflow value is currently in effect (which may
1612
- // already be "hidden" because another modal is open) so close() can
1613
- // restore exactly that value instead of clobbering a scroll lock owned
1614
- // by a concurrently open modal. Skip the capture on a re-open: the value
1615
- // in effect is this modal's own lock, and previousBodyOverflow already
1616
- // holds the correct pre-lock value.
1617
- if (!wasAlreadyOpen) {
1618
- this.previousBodyOverflow = document.body.style.overflow;
1619
- }
1620
- document.body.style.overflow = "hidden";
1774
+ // Idempotent: a re-open of a modal that is still open keeps its one hold on the lock, so
1775
+ // close() still gives the page back the values it had before any modal opened.
1776
+ this.lockPageScroll();
1777
+ this.showCurtain();
1621
1778
  }
1622
1779
  close() {
1623
1780
  SDKLogger.log("Closing");
@@ -1635,8 +1792,8 @@ class VerificationModal {
1635
1792
  return;
1636
1793
  }
1637
1794
  this.overlay?.classList.remove("active");
1638
- document.body.style.overflow = this.previousBodyOverflow ?? "";
1639
- this.previousBodyOverflow = null;
1795
+ this.removeCurtain();
1796
+ this.unlockPageScroll();
1640
1797
  }
1641
1798
  destroy() {
1642
1799
  SDKLogger.log("Destroying");
@@ -1668,6 +1825,16 @@ class VerificationModal {
1668
1825
  * a transaction action modal stacked over the verification modal).
1669
1826
  */
1670
1827
  VerificationModal.openModals = [];
1828
+ /**
1829
+ * The page's own background colours, kept while full-screen modals lend it their ground, shared by
1830
+ * every open modal: the topmost one's ground shows and the last one to give it back restores them.
1831
+ */
1832
+ VerificationModal.pageBackground = null;
1833
+ /**
1834
+ * The page's scroll styles from before the first open modal locked them, shared by every open
1835
+ * modal: the last one to close gives them back.
1836
+ */
1837
+ VerificationModal.pageScrollLock = null;
1671
1838
 
1672
1839
  const DEFAULT_TRANSACTION_BASE_URL = "https://verification.didit.me";
1673
1840
  const ACTION_POLL_INTERVAL_MS = 1500;
@@ -1987,6 +2154,18 @@ function assertEmbeddedHostExists(configuration) {
1987
2154
  throw new Error(`Embedded container "#${containerId}" was not found in the page.`);
1988
2155
  }
1989
2156
  }
2157
+ /**
2158
+ * The device id this page already keeps for Didit (it has sent events, transactions or sign-ins),
2159
+ * or null. Reading it never creates one, and a browser that refuses the read is not an error.
2160
+ */
2161
+ function storedDeviceId() {
2162
+ try {
2163
+ return signals.getStoredDeviceId();
2164
+ }
2165
+ catch {
2166
+ return null;
2167
+ }
2168
+ }
1990
2169
  class DiditSdk {
1991
2170
  static get shared() {
1992
2171
  if (!DiditSdk._instance) {
@@ -2060,7 +2239,7 @@ class DiditSdk {
2060
2239
  onMessage: (event) => this.handleVerificationEvent(event),
2061
2240
  onIframeLoad: () => this.handleIframeLoad()
2062
2241
  });
2063
- this._url = buildVerificationUrl(url, config);
2242
+ this._url = buildVerificationUrl(url, config, storedDeviceId());
2064
2243
  this.setState("loading");
2065
2244
  this.emitInternalEvent("didit:started", {});
2066
2245
  this._modal?.open(this._url);