accessibility-plugin-react 2.2.0 → 2.2.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
@@ -349,7 +349,8 @@ function validValue(key, value, fallback, validate) {
349
349
 
350
350
  // src/core/effects.ts
351
351
  var import_react3 = require("react");
352
- var EXCLUDED = "[data-arap-ui], [data-arap-ui] *, script, style, link, meta";
352
+ var EXCLUDED = "[data-arap-ui], [data-arap-ui] *, script, style, link, meta, noscript, template";
353
+ var MAX_HOST_ELEMENTS = 4e3;
353
354
  var cursor = `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40'%3E%3Cpath d='M4 2v32l9-9 7 13 6-3-7-12h14z' fill='black' stroke='white' stroke-width='2'/%3E%3C/svg%3E") 4 2, auto`;
354
355
  var palette = {
355
356
  "yellow-on-black": ["#ffff00", "#000000"],
@@ -381,9 +382,10 @@ function applyPage(values) {
381
382
  else element.style.removeProperty(property);
382
383
  });
383
384
  }
384
- const elements = Array.from(
385
+ const allElements = Array.from(
385
386
  document.body.querySelectorAll("*")
386
387
  ).filter((el) => el instanceof HTMLElement && !el.matches(EXCLUDED));
388
+ const elements = allElements.length > MAX_HOST_ELEMENTS ? allElements.slice(0, MAX_HOST_ELEMENTS) : allElements;
387
389
  const font = Number(values.fontSize ?? 100) / 100;
388
390
  const line = Number(values.lineHeight ?? 100) / 100;
389
391
  const measurements = font !== 1 || line !== 1 ? elements.map((el) => {
@@ -449,6 +451,10 @@ function applyPage(values) {
449
451
  };
450
452
  }
451
453
  function readingAid(mask) {
454
+ const existing = document.body.querySelectorAll(
455
+ mask ? ".arap-reading-mask" : ".arap-reading-guide"
456
+ );
457
+ existing.forEach((node) => node.remove());
452
458
  const el = document.createElement("div");
453
459
  el.dataset.arapUi = "";
454
460
  el.className = mask ? "arap-reading-mask" : "arap-reading-guide";
@@ -480,6 +486,8 @@ function useBuiltinEffects(values) {
480
486
  ))
481
487
  return;
482
488
  let undo = applyPage(values);
489
+ let mutationTimer = null;
490
+ let resizeTimer = null;
483
491
  const observer = new MutationObserver((records) => {
484
492
  if (!records.some(
485
493
  (record) => record.target instanceof Element && !record.target.closest("[data-arap-ui]") && [...record.addedNodes, ...record.removedNodes].some(
@@ -487,16 +495,24 @@ function useBuiltinEffects(values) {
487
495
  )
488
496
  ))
489
497
  return;
490
- undo();
491
- undo = applyPage(values);
498
+ if (mutationTimer) clearTimeout(mutationTimer);
499
+ mutationTimer = setTimeout(() => {
500
+ undo();
501
+ undo = applyPage(values);
502
+ }, 16);
492
503
  });
493
504
  observer.observe(document.body, { childList: true, subtree: true });
494
505
  const resize = () => {
495
- undo();
496
- undo = applyPage(values);
506
+ if (resizeTimer) clearTimeout(resizeTimer);
507
+ resizeTimer = setTimeout(() => {
508
+ undo();
509
+ undo = applyPage(values);
510
+ }, 50);
497
511
  };
498
512
  window.addEventListener("resize", resize);
499
513
  return () => {
514
+ if (mutationTimer) clearTimeout(mutationTimer);
515
+ if (resizeTimer) clearTimeout(resizeTimer);
500
516
  observer.disconnect();
501
517
  window.removeEventListener("resize", resize);
502
518
  undo();
@@ -515,7 +531,10 @@ function CustomEffect({
515
531
  definition,
516
532
  value
517
533
  }) {
518
- (0, import_react3.useEffect)(() => definition.apply?.(value), [definition, value]);
534
+ (0, import_react3.useEffect)(() => {
535
+ const cleanup = definition.apply?.(value);
536
+ return typeof cleanup === "function" ? cleanup : void 0;
537
+ }, [definition, value]);
519
538
  return null;
520
539
  }
521
540