pixel-react 1.21.99 → 1.22.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.
@@ -1,12 +1,18 @@
1
1
  const DEFAULT_FONT_SIZE = 12;
2
2
  const DEFAULT_FONT_FAMILY = 'Poppins';
3
+ // One canvas for every measurement. Components call this on each render (e.g.
4
+ // in every table row), and a new canvas and 2D context per call was most of
5
+ // the cost.
6
+ let canvasContext;
3
7
  const getCanvasContext = (fontSize = DEFAULT_FONT_SIZE, fontFamily = DEFAULT_FONT_FAMILY) => {
4
- const canvas = document.createElement('canvas');
5
- const context = canvas.getContext('2d');
6
- if (context) {
7
- context.font = `${fontSize}px ${fontFamily}`;
8
+ if (canvasContext === undefined) {
9
+ canvasContext = document.createElement('canvas').getContext('2d');
8
10
  }
9
- return context;
11
+ const font = `${fontSize}px ${fontFamily}`;
12
+ if (canvasContext && canvasContext.font !== font) {
13
+ canvasContext.font = font;
14
+ }
15
+ return canvasContext;
10
16
  };
11
17
  export const truncateText = (text, maxLimit = 100, truncateType = 'count', fontSize = DEFAULT_FONT_SIZE, fontFamily = DEFAULT_FONT_FAMILY, letterSpacing = 0) => {
12
18
  if (!text || maxLimit <= 0)
@@ -25,17 +31,25 @@ export const truncateText = (text, maxLimit = 100, truncateType = 'count', fontS
25
31
  const totalLetterSpacing = letterSpacing > 0 ? letterSpacing * Math.max(0, text.length - 1) : 0;
26
32
  if (baseWidth + totalLetterSpacing <= maxLimit)
27
33
  return text;
28
- let truncated = '';
29
- for (let i = 0; i < text.length; i++) {
30
- const temp = text.slice(0, i + 1);
31
- const baseWidth = context.measureText(temp).width;
32
- const spacingWidth = letterSpacing > 0 ? letterSpacing * i : 0;
33
- if (baseWidth + spacingWidth + ellipsisWidth > maxLimit) {
34
- return `${truncated}${ellipsis}`;
35
- }
36
- truncated = temp;
34
+ // The longest prefix that fits with the ellipsis. Width only grows with
35
+ // length, so a binary search finds it in log(n) measurements, not n.
36
+ const fits = (length) => {
37
+ const spacingWidth = letterSpacing > 0 ? letterSpacing * (length - 1) : 0;
38
+ return (context.measureText(text.slice(0, length)).width +
39
+ spacingWidth +
40
+ ellipsisWidth <=
41
+ maxLimit);
42
+ };
43
+ let low = 0;
44
+ let high = text.length;
45
+ while (low < high) {
46
+ const middle = Math.ceil((low + high) / 2);
47
+ if (fits(middle))
48
+ low = middle;
49
+ else
50
+ high = middle - 1;
37
51
  }
38
- return truncated;
52
+ return low === text.length ? text : `${text.slice(0, low)}${ellipsis}`;
39
53
  };
40
54
  export const isTextTruncated = (text, maxLimit = 100, truncateType = 'count', fontSize = DEFAULT_FONT_SIZE, fontFamily = DEFAULT_FONT_FAMILY) => {
41
55
  if (!text || maxLimit <= 0)
@@ -1 +1 @@
1
- {"version":3,"file":"truncateText.js","sourceRoot":"","sources":["../../../src/utils/truncateText/truncateText.ts"],"names":[],"mappings":"AAAA,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAC7B,MAAM,mBAAmB,GAAG,SAAS,CAAC;AAEtC,MAAM,gBAAgB,GAAG,CACvB,WAAmB,iBAAiB,EACpC,aAAqB,mBAAmB,EACP,EAAE;IACnC,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IAChD,MAAM,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IACxC,IAAI,OAAO,EAAE,CAAC;QACZ,OAAO,CAAC,IAAI,GAAG,GAAG,QAAQ,MAAM,UAAU,EAAE,CAAC;IAC/C,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,IAAY,EACZ,WAAmB,GAAG,EACtB,eAAkC,OAAO,EACzC,WAAmB,iBAAiB,EACpC,aAAqB,mBAAmB,EACxC,gBAAwB,CAAC,EACjB,EAAE;IACV,IAAI,CAAC,IAAI,IAAI,QAAQ,IAAI,CAAC;QAAE,OAAO,EAAE,CAA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CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;IACvD,IAAI,CAAC,OAAO;QAAE,OAAO,KAAK,CAAC;IAE3B,OAAO,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,KAAK,GAAG,QAAQ,CAAC;AACpD,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pixel-react",
3
3
  "description": "Great for pixel-perfect, design-focused components in React",
4
- "version": "1.21.99",
4
+ "version": "1.22.01",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.js",
7
7
  "types": "lib/index.d.ts",