@faable/challenge 1.0.0 → 1.1.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/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-state="error"]{cursor:pointer}
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
- if (state === "error") {
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.textContent = text;
1310
- if (motion && !compact) {
1311
- r.txt.animate(
1312
- [
1313
- { opacity: 0, transform: "translateY(2px)" },
1314
- { opacity: 1, transform: "none" }
1315
- ],
1316
- { duration: 200, easing: EASE }
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
- const setState = (next) => {
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
- if (state !== "error") return;
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("faable-challenge:retry", { bubbles: true })
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);
@@ -1583,7 +1640,8 @@ var render2 = (el, opts) => {
1583
1640
  if (!(opts == null ? void 0 : opts.sitekey)) throw new Error("Faable Challenge: sitekey is required");
1584
1641
  const widget = mountChallengeWidget(el, opts, {
1585
1642
  followWindowEvents: false,
1586
- product: "challenge"
1643
+ product: "challenge",
1644
+ activateOnClick: true
1587
1645
  });
1588
1646
  const form = el.closest("form");
1589
1647
  let input = null;
@@ -1613,9 +1671,11 @@ var render2 = (el, opts) => {
1613
1671
  });
1614
1672
  const onRetry = () => start();
1615
1673
  el.addEventListener("faable-challenge:retry", onRetry);
1616
- inst.cleanup.push(
1617
- () => el.removeEventListener("faable-challenge:retry", onRetry)
1618
- );
1674
+ el.addEventListener("faable-challenge:activate", onRetry);
1675
+ inst.cleanup.push(() => {
1676
+ el.removeEventListener("faable-challenge:retry", onRetry);
1677
+ el.removeEventListener("faable-challenge:activate", onRetry);
1678
+ });
1619
1679
  if (form) {
1620
1680
  if (execution === "interaction") {
1621
1681
  const onInteract = () => start();