@seanyeh77/ui 0.8.0 → 0.8.1

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
@@ -1286,13 +1286,22 @@ function useReturnFocus(open) {
1286
1286
  if (!open && wasOpen.current) {
1287
1287
  const back = opener.current;
1288
1288
  opener.current = null;
1289
- const t = setTimeout(() => {
1290
- if (!lost()) return;
1291
- if (back?.isConnected) back.focus();
1292
- else focusMain();
1293
- }, 0);
1289
+ const held = document.activeElement;
1290
+ const until = performance.now() + 2e3;
1291
+ let frame = 0;
1292
+ const look = () => {
1293
+ if (lost()) {
1294
+ if (back?.isConnected) back.focus();
1295
+ else focusMain();
1296
+ return;
1297
+ }
1298
+ const at = document.activeElement;
1299
+ if (!(at === held || !!at?.closest("[data-ending-style]")) || performance.now() > until) return;
1300
+ frame = requestAnimationFrame(look);
1301
+ };
1302
+ frame = requestAnimationFrame(look);
1294
1303
  wasOpen.current = open;
1295
- return () => clearTimeout(t);
1304
+ return () => cancelAnimationFrame(frame);
1296
1305
  }
1297
1306
  wasOpen.current = open;
1298
1307
  }, [open]);
@@ -1316,13 +1325,24 @@ function useFocusRescue(ref, fallback) {
1316
1325
  last = e.target;
1317
1326
  index = Array.from(box.querySelectorAll(FOCUSABLE)).indexOf(last);
1318
1327
  };
1319
- const mo = new MutationObserver(() => {
1320
- if (!last || last.isConnected && !last.matches(":disabled") || !lost()) return;
1328
+ const rescue = () => {
1321
1329
  const now = list();
1322
1330
  const next = fb.current?.() ?? now[Math.min(index, now.length - 1)] ?? null;
1323
1331
  last = null;
1324
1332
  if (next) next.focus();
1325
1333
  else focusMain();
1334
+ };
1335
+ let frame = 0;
1336
+ const mo = new MutationObserver(() => {
1337
+ if (!last || last.isConnected && !last.matches(":disabled")) return;
1338
+ if (lost()) return rescue();
1339
+ const dead = last;
1340
+ cancelAnimationFrame(frame);
1341
+ frame = requestAnimationFrame(() => {
1342
+ frame = requestAnimationFrame(() => {
1343
+ if (last === dead && (lost() || document.activeElement === dead)) rescue();
1344
+ });
1345
+ });
1326
1346
  });
1327
1347
  box.addEventListener("focusin", onIn);
1328
1348
  mo.observe(box, {
@@ -1334,6 +1354,7 @@ function useFocusRescue(ref, fallback) {
1334
1354
  return () => {
1335
1355
  box.removeEventListener("focusin", onIn);
1336
1356
  mo.disconnect();
1357
+ cancelAnimationFrame(frame);
1337
1358
  };
1338
1359
  }, [ref]);
1339
1360
  }