@faable/challenge 1.0.0 → 1.2.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.
- package/dist/api.js +8 -7
- package/dist/api.js.map +3 -3
- package/dist/index.js +85 -23
- package/dist/index.js.map +2 -2
- package/dist/types/widget.d.ts +13 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1204,7 +1204,8 @@ var css = (T, dark, state) => `
|
|
|
1204
1204
|
.tag{overflow:hidden;transition:background-color .2s,color .2s,border-color .2s,box-shadow .2s,height .2s}
|
|
1205
1205
|
.tag.compact{width:32px;height:32px;padding:0;gap:0;justify-content:center}
|
|
1206
1206
|
.measurer{position:absolute;left:0;top:0;visibility:hidden;pointer-events:none;width:max-content}
|
|
1207
|
-
.tag[data-
|
|
1207
|
+
.tag[data-clickable]{cursor:pointer}
|
|
1208
|
+
.tag[data-clickable]:focus-visible{outline:2px solid #7c3aed;outline-offset:2px}
|
|
1208
1209
|
.measure{position:absolute;left:0;top:0;visibility:hidden;pointer-events:none}
|
|
1209
1210
|
.ic{position:relative;display:inline-flex;align-items:center;justify-content:center;width:26px;height:26px}
|
|
1210
1211
|
.ic .layer{position:absolute;inset:0;display:flex;align-items:center;justify-content:center}
|
|
@@ -1251,6 +1252,7 @@ var refsOf = (root) => {
|
|
|
1251
1252
|
};
|
|
1252
1253
|
};
|
|
1253
1254
|
var EASE = "cubic-bezier(.2,.7,.2,1)";
|
|
1255
|
+
var MIN_SOLVING_MS = 1200;
|
|
1254
1256
|
var render = (root, state, o, host, animate) => {
|
|
1255
1257
|
var _a;
|
|
1256
1258
|
const r = refsOf(root);
|
|
@@ -1269,12 +1271,23 @@ var render = (root, state, o, host, animate) => {
|
|
|
1269
1271
|
const prevState = r.tag.dataset.state;
|
|
1270
1272
|
r.tag.dataset.state = state;
|
|
1271
1273
|
r.tag.classList.toggle("compact", compact);
|
|
1272
|
-
|
|
1274
|
+
const clickable = state === "error" || o.activatable && state === "idle";
|
|
1275
|
+
if (clickable) {
|
|
1273
1276
|
r.tag.setAttribute("role", "button");
|
|
1274
1277
|
r.tag.setAttribute("tabindex", "0");
|
|
1278
|
+
r.tag.setAttribute("data-clickable", "");
|
|
1275
1279
|
} else {
|
|
1276
1280
|
r.tag.removeAttribute("role");
|
|
1277
1281
|
r.tag.removeAttribute("tabindex");
|
|
1282
|
+
r.tag.removeAttribute("data-clickable");
|
|
1283
|
+
}
|
|
1284
|
+
const label = `${textsFor(o.locale)[state]} \xB7 ${PRODUCT[o.product].name}`;
|
|
1285
|
+
if (compact) {
|
|
1286
|
+
r.tag.setAttribute("aria-label", label);
|
|
1287
|
+
r.tag.setAttribute("title", label);
|
|
1288
|
+
} else {
|
|
1289
|
+
r.tag.removeAttribute("aria-label");
|
|
1290
|
+
r.tag.removeAttribute("title");
|
|
1278
1291
|
}
|
|
1279
1292
|
if (prevState !== state || !r.ic.firstElementChild) {
|
|
1280
1293
|
const layer = document.createElement("span");
|
|
@@ -1306,15 +1319,34 @@ var render = (root, state, o, host, animate) => {
|
|
|
1306
1319
|
const text = textsFor(o.locale)[state];
|
|
1307
1320
|
r.txt.className = compact ? "txt vh" : "txt";
|
|
1308
1321
|
if (r.txt.textContent !== text) {
|
|
1309
|
-
r.txt.
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1322
|
+
const fadeIn = () => r.txt.animate(
|
|
1323
|
+
[
|
|
1324
|
+
{ opacity: 0, transform: "translateY(2px)" },
|
|
1325
|
+
{ opacity: 1, transform: "none" }
|
|
1326
|
+
],
|
|
1327
|
+
{ duration: 260, easing: EASE }
|
|
1328
|
+
);
|
|
1329
|
+
if (motion && !compact && r.txt.textContent) {
|
|
1330
|
+
const token = String(Date.now() + Math.random());
|
|
1331
|
+
r.txt.dataset.fade = token;
|
|
1332
|
+
r.txt.animate([{ opacity: 1 }, { opacity: 0 }], {
|
|
1333
|
+
duration: 140,
|
|
1334
|
+
easing: "ease-in",
|
|
1335
|
+
fill: "forwards"
|
|
1336
|
+
}).finished.then(
|
|
1337
|
+
() => {
|
|
1338
|
+
if (r.txt.dataset.fade !== token) return;
|
|
1339
|
+
r.txt.textContent = text;
|
|
1340
|
+
r.txt.getAnimations().forEach((a) => a.cancel());
|
|
1341
|
+
fadeIn();
|
|
1342
|
+
},
|
|
1343
|
+
() => {
|
|
1344
|
+
r.txt.textContent = text;
|
|
1345
|
+
}
|
|
1317
1346
|
);
|
|
1347
|
+
} else {
|
|
1348
|
+
r.txt.textContent = text;
|
|
1349
|
+
if (motion && !compact) fadeIn();
|
|
1318
1350
|
}
|
|
1319
1351
|
}
|
|
1320
1352
|
const showBrand = o.brand && !compact;
|
|
@@ -1352,6 +1384,7 @@ var mountChallengeWidget = (el, opts = {}, driver = {}) => {
|
|
|
1352
1384
|
var _a2, _b2;
|
|
1353
1385
|
return {
|
|
1354
1386
|
product: (_a2 = driver.product) != null ? _a2 : "auth",
|
|
1387
|
+
activatable: !!driver.activateOnClick,
|
|
1355
1388
|
theme: "auto",
|
|
1356
1389
|
size: "fit",
|
|
1357
1390
|
brand: true,
|
|
@@ -1373,13 +1406,31 @@ var mountChallengeWidget = (el, opts = {}, driver = {}) => {
|
|
|
1373
1406
|
run2();
|
|
1374
1407
|
}
|
|
1375
1408
|
};
|
|
1376
|
-
|
|
1409
|
+
let solvingSince = 0;
|
|
1410
|
+
let pending = null;
|
|
1411
|
+
const apply = (next) => {
|
|
1377
1412
|
state = next;
|
|
1413
|
+
if (next === "solving") solvingSince = Date.now();
|
|
1378
1414
|
render(root, state, options(), el, true);
|
|
1379
1415
|
el.dispatchEvent(
|
|
1380
1416
|
new CustomEvent(`faable-challenge:${next}`, { bubbles: true })
|
|
1381
1417
|
);
|
|
1382
1418
|
};
|
|
1419
|
+
const setState = (next) => {
|
|
1420
|
+
if (pending) {
|
|
1421
|
+
clearTimeout(pending);
|
|
1422
|
+
pending = null;
|
|
1423
|
+
}
|
|
1424
|
+
const shown = Date.now() - solvingSince;
|
|
1425
|
+
if (state === "solving" && (next === "done" || next === "error") && shown < MIN_SOLVING_MS && !reducedMotion()) {
|
|
1426
|
+
pending = setTimeout(() => {
|
|
1427
|
+
pending = null;
|
|
1428
|
+
apply(next);
|
|
1429
|
+
}, MIN_SOLVING_MS - shown);
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
apply(next);
|
|
1433
|
+
};
|
|
1383
1434
|
const onEvent = (ev) => {
|
|
1384
1435
|
var _a2;
|
|
1385
1436
|
const s = (_a2 = ev.detail) == null ? void 0 : _a2.state;
|
|
@@ -1387,13 +1438,18 @@ var mountChallengeWidget = (el, opts = {}, driver = {}) => {
|
|
|
1387
1438
|
};
|
|
1388
1439
|
const onActivate = (ev) => {
|
|
1389
1440
|
var _a2;
|
|
1390
|
-
|
|
1441
|
+
const idle = state === "idle" && !!driver.activateOnClick;
|
|
1442
|
+
if (state !== "error" && !idle) return;
|
|
1391
1443
|
if (ev instanceof KeyboardEvent && ev.key !== "Enter" && ev.key !== " ")
|
|
1392
1444
|
return;
|
|
1393
1445
|
const target = ev.composedPath()[0];
|
|
1394
1446
|
if ((_a2 = target == null ? void 0 : target.closest) == null ? void 0 : _a2.call(target, ".brand")) return;
|
|
1447
|
+
if (ev instanceof KeyboardEvent) ev.preventDefault();
|
|
1395
1448
|
el.dispatchEvent(
|
|
1396
|
-
new CustomEvent(
|
|
1449
|
+
new CustomEvent(
|
|
1450
|
+
idle ? "faable-challenge:activate" : "faable-challenge:retry",
|
|
1451
|
+
{ bubbles: true }
|
|
1452
|
+
)
|
|
1397
1453
|
);
|
|
1398
1454
|
};
|
|
1399
1455
|
root.addEventListener("click", onActivate);
|
|
@@ -1422,6 +1478,7 @@ var mountChallengeWidget = (el, opts = {}, driver = {}) => {
|
|
|
1422
1478
|
reset: () => setState("idle"),
|
|
1423
1479
|
unmount: () => {
|
|
1424
1480
|
var _a2;
|
|
1481
|
+
if (pending) clearTimeout(pending);
|
|
1425
1482
|
form == null ? void 0 : form.removeEventListener("reset", onReset);
|
|
1426
1483
|
if (followWindow) window.removeEventListener(CHALLENGE_EVENT, onEvent);
|
|
1427
1484
|
root.removeEventListener("click", onActivate);
|
|
@@ -1441,16 +1498,16 @@ var resetChallengeWidgets = (root = document) => {
|
|
|
1441
1498
|
const els = root instanceof Element && root.matches("[data-faable-challenge]") ? [root] : Array.from(root.querySelectorAll("[data-faable-challenge]"));
|
|
1442
1499
|
for (const el of els) (_a = MOUNTED.get(el)) == null ? void 0 : _a.reset();
|
|
1443
1500
|
};
|
|
1444
|
-
var mountChallengeWidgets = (root = document) => {
|
|
1445
|
-
root.querySelectorAll("[data-faable-challenge]").forEach((el) => mountChallengeWidget(el));
|
|
1501
|
+
var mountChallengeWidgets = (root = document, driver = {}) => {
|
|
1502
|
+
root.querySelectorAll("[data-faable-challenge]").forEach((el) => mountChallengeWidget(el, {}, driver));
|
|
1446
1503
|
};
|
|
1447
1504
|
var watching = false;
|
|
1448
|
-
var watchChallengeWidgets = () => {
|
|
1505
|
+
var watchChallengeWidgets = (driver = {}) => {
|
|
1449
1506
|
if (watching || typeof document === "undefined") return;
|
|
1450
1507
|
if (typeof window === "undefined" || typeof MutationObserver !== "function")
|
|
1451
1508
|
return;
|
|
1452
1509
|
watching = true;
|
|
1453
|
-
const scan = () => mountChallengeWidgets(document);
|
|
1510
|
+
const scan = () => mountChallengeWidgets(document, driver);
|
|
1454
1511
|
if (document.readyState === "loading") {
|
|
1455
1512
|
document.addEventListener("DOMContentLoaded", scan, { once: true });
|
|
1456
1513
|
} else {
|
|
@@ -1460,8 +1517,10 @@ var watchChallengeWidgets = () => {
|
|
|
1460
1517
|
for (const r of records) {
|
|
1461
1518
|
r.addedNodes.forEach((node) => {
|
|
1462
1519
|
if (!(node instanceof Element)) return;
|
|
1463
|
-
if (node.matches("[data-faable-challenge]"))
|
|
1464
|
-
|
|
1520
|
+
if (node.matches("[data-faable-challenge]")) {
|
|
1521
|
+
mountChallengeWidget(node, {}, driver);
|
|
1522
|
+
}
|
|
1523
|
+
mountChallengeWidgets(node, driver);
|
|
1465
1524
|
});
|
|
1466
1525
|
}
|
|
1467
1526
|
}).observe(document.documentElement, { childList: true, subtree: true });
|
|
@@ -1583,7 +1642,8 @@ var render2 = (el, opts) => {
|
|
|
1583
1642
|
if (!(opts == null ? void 0 : opts.sitekey)) throw new Error("Faable Challenge: sitekey is required");
|
|
1584
1643
|
const widget = mountChallengeWidget(el, opts, {
|
|
1585
1644
|
followWindowEvents: false,
|
|
1586
|
-
product: "challenge"
|
|
1645
|
+
product: "challenge",
|
|
1646
|
+
activateOnClick: true
|
|
1587
1647
|
});
|
|
1588
1648
|
const form = el.closest("form");
|
|
1589
1649
|
let input = null;
|
|
@@ -1613,9 +1673,11 @@ var render2 = (el, opts) => {
|
|
|
1613
1673
|
});
|
|
1614
1674
|
const onRetry = () => start();
|
|
1615
1675
|
el.addEventListener("faable-challenge:retry", onRetry);
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1676
|
+
el.addEventListener("faable-challenge:activate", onRetry);
|
|
1677
|
+
inst.cleanup.push(() => {
|
|
1678
|
+
el.removeEventListener("faable-challenge:retry", onRetry);
|
|
1679
|
+
el.removeEventListener("faable-challenge:activate", onRetry);
|
|
1680
|
+
});
|
|
1619
1681
|
if (form) {
|
|
1620
1682
|
if (execution === "interaction") {
|
|
1621
1683
|
const onInteract = () => start();
|