fetta 1.4.0 → 1.4.2

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.
@@ -19,6 +19,33 @@ var __spreadValues = (a, b) => {
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
20
 
21
21
  // src/core/splitText.ts
22
+ var ARIA_LABEL_ALLOWED_TAGS = /* @__PURE__ */ new Set([
23
+ "h1",
24
+ "h2",
25
+ "h3",
26
+ "h4",
27
+ "h5",
28
+ "h6",
29
+ "a",
30
+ "button",
31
+ "img",
32
+ "input",
33
+ "select",
34
+ "textarea",
35
+ "table",
36
+ "figure",
37
+ "form",
38
+ "fieldset",
39
+ "dialog",
40
+ "details",
41
+ "section",
42
+ "article",
43
+ "nav",
44
+ "aside",
45
+ "header",
46
+ "footer",
47
+ "main"
48
+ ]);
22
49
  var BREAK_CHARS = /* @__PURE__ */ new Set([
23
50
  "\u2014",
24
51
  // em-dash
@@ -171,7 +198,7 @@ function measureKerningCanvas(styleSource, chars, styles) {
171
198
  const char2 = chars[i + 1];
172
199
  const pairWidth = ctx.measureText(char1 + char2).width;
173
200
  const kerning = pairWidth - charWidths.get(char1) - charWidths.get(char2);
174
- if (Math.abs(kerning) > 0.01) {
201
+ if (Math.abs(kerning) > 1e-3) {
175
202
  kerningMap.set(i + 1, kerning);
176
203
  }
177
204
  }
@@ -208,7 +235,7 @@ function measureKerningDOM(container, styleSource, chars, styles) {
208
235
  measurer.textContent = char1 + char2;
209
236
  const pairWidth = measurer.getBoundingClientRect().width;
210
237
  const kerning = pairWidth - charWidths.get(char1) - charWidths.get(char2);
211
- if (Math.abs(kerning) > 0.01) {
238
+ if (Math.abs(kerning) > 1e-3) {
212
239
  kerningMap.set(i + 1, kerning);
213
240
  }
214
241
  }
@@ -669,7 +696,7 @@ function performSplit(element, measuredWords, charClass, wordClass, lineClass, s
669
696
  let totalKerning = 0;
670
697
  if (kerningMap.has(1)) totalKerning += kerningMap.get(1);
671
698
  if (kerningMap.has(2)) totalKerning += kerningMap.get(2);
672
- if (Math.abs(totalKerning) > 0.01 && Math.abs(totalKerning) < 20) {
699
+ if (Math.abs(totalKerning) > 1e-3 && Math.abs(totalKerning) < 20) {
673
700
  const targetElement = (options == null ? void 0 : options.mask) === "chars" && firstCharSpan.parentElement ? firstCharSpan.parentElement : firstCharSpan;
674
701
  targetElement.style.marginLeft = `${totalKerning}px`;
675
702
  }
@@ -690,7 +717,7 @@ function performSplit(element, measuredWords, charClass, wordClass, lineClass, s
690
717
  let totalKerning = 0;
691
718
  if (kerningMap.has(1)) totalKerning += kerningMap.get(1);
692
719
  if (kerningMap.has(2)) totalKerning += kerningMap.get(2);
693
- if (Math.abs(totalKerning) > 0.01 && Math.abs(totalKerning) < 20) {
720
+ if (Math.abs(totalKerning) > 1e-3 && Math.abs(totalKerning) < 20) {
694
721
  const targetElement = (options == null ? void 0 : options.mask) === "words" && currWord.parentElement ? currWord.parentElement : currWord;
695
722
  targetElement.style.marginLeft = `${totalKerning}px`;
696
723
  }
@@ -908,11 +935,13 @@ function splitText(element, {
908
935
  let currentChars = [];
909
936
  let currentWords = [];
910
937
  let currentLines = [];
938
+ let originalAriaLabel = null;
911
939
  if (splitChars) {
912
940
  element.style.fontVariantLigatures = "none";
913
941
  }
914
942
  const trackAncestors = hasInlineDescendants(element);
915
943
  const measuredWords = collectTextStructure(element, trackAncestors);
944
+ const useAriaLabel = !trackAncestors && ARIA_LABEL_ALLOWED_TAGS.has(element.tagName.toLowerCase());
916
945
  const { chars, words, lines } = performSplit(
917
946
  element,
918
947
  measuredWords,
@@ -922,7 +951,7 @@ function splitText(element, {
922
951
  splitChars,
923
952
  splitWords,
924
953
  splitLines,
925
- { propIndex, mask, ariaHidden: !trackAncestors, disableKerning, initialStyles, initialClasses }
954
+ { propIndex, mask, ariaHidden: useAriaLabel, disableKerning, initialStyles, initialClasses }
926
955
  );
927
956
  currentChars = chars;
928
957
  currentWords = words;
@@ -937,8 +966,21 @@ function splitText(element, {
937
966
  }
938
967
  element.appendChild(visualWrapper);
939
968
  element.appendChild(createScreenReaderCopy(originalHTML));
969
+ } else if (useAriaLabel) {
970
+ originalAriaLabel = element.getAttribute("aria-label");
971
+ if (originalAriaLabel === null) {
972
+ element.setAttribute("aria-label", text);
973
+ }
940
974
  } else {
941
- element.setAttribute("aria-label", text);
975
+ injectSrOnlyStyles();
976
+ const visualWrapper = document.createElement("span");
977
+ visualWrapper.setAttribute("aria-hidden", "true");
978
+ visualWrapper.dataset.fettaVisual = "true";
979
+ while (element.firstChild) {
980
+ visualWrapper.appendChild(element.firstChild);
981
+ }
982
+ element.appendChild(visualWrapper);
983
+ element.appendChild(createScreenReaderCopy(originalHTML));
942
984
  }
943
985
  const dispose = () => {
944
986
  if (!isActive) return;
@@ -955,8 +997,12 @@ function splitText(element, {
955
997
  const revert = () => {
956
998
  if (!isActive) return;
957
999
  element.innerHTML = originalHTML;
958
- if (!trackAncestors) {
959
- element.removeAttribute("aria-label");
1000
+ if (useAriaLabel) {
1001
+ if (originalAriaLabel !== null) {
1002
+ element.setAttribute("aria-label", originalAriaLabel);
1003
+ } else {
1004
+ element.removeAttribute("aria-label");
1005
+ }
960
1006
  }
961
1007
  if (splitChars) {
962
1008
  element.style.fontVariantLigatures = "none";
@@ -997,12 +1043,12 @@ function splitText(element, {
997
1043
  splitChars,
998
1044
  splitWords,
999
1045
  splitLines,
1000
- { propIndex, mask, ariaHidden: !trackAncestors, disableKerning, initialStyles, initialClasses }
1046
+ { propIndex, mask, ariaHidden: useAriaLabel, disableKerning, initialStyles, initialClasses }
1001
1047
  );
1002
1048
  currentChars = result.chars;
1003
1049
  currentWords = result.words;
1004
1050
  currentLines = result.lines;
1005
- if (trackAncestors) {
1051
+ if (trackAncestors || !useAriaLabel) {
1006
1052
  const visualWrapper = document.createElement("span");
1007
1053
  visualWrapper.setAttribute("aria-hidden", "true");
1008
1054
  visualWrapper.dataset.fettaVisual = "true";
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { splitText } from './chunk-NTRJ6XDH.js';
1
+ export { splitText } from './chunk-24TCJGUM.js';
package/dist/react.js CHANGED
@@ -1,4 +1,4 @@
1
- import { splitText, __spreadProps, __spreadValues, normalizeToPromise } from './chunk-NTRJ6XDH.js';
1
+ import { splitText, __spreadProps, __spreadValues, normalizeToPromise } from './chunk-24TCJGUM.js';
2
2
  import { forwardRef, useRef, useCallback, useState, useLayoutEffect, useEffect, isValidElement, cloneElement } from 'react';
3
3
  import { jsx } from 'react/jsx-runtime';
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fetta",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Text splitting library with kerning compensation for animations",
5
5
  "type": "module",
6
6
  "sideEffects": false,