@acusti/styling 1.0.0-rc.0 → 1.0.0-rc.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/Style.js CHANGED
@@ -7,10 +7,9 @@ const Style = ({ children, href: _href, precedence = 'medium' }) => {
7
7
  // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/canary.d.ts
8
8
  // https://react.dev/reference/react-dom/components/style#props
9
9
  return (
10
- // @ts-expect-error @types/react is missing new <style> props
11
- // eslint-disable-next-line react/no-unknown-property
12
- React.createElement('style', { href: href, precedence: precedence }, styles)
13
- );
10
+ // @ts-expect-error @types/react is missing new <style> props
11
+ // eslint-disable-next-line react/no-unknown-property
12
+ React.createElement("style", { href: href, precedence: precedence }, styles));
14
13
  };
15
14
  export default Style;
16
- //# sourceMappingURL=Style.js.map
15
+ //# sourceMappingURL=Style.js.map
package/dist/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export { default as Style } from './Style.js';
2
- export declare const SYSTEM_UI_FONT =
3
- '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif';
2
+ export declare const SYSTEM_UI_FONT = "-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Oxygen-Sans, Ubuntu, Cantarell, \"Helvetica Neue\", sans-serif";
package/dist/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  /// <reference types="react/canary" />
2
2
  export { default as Style } from './Style.js';
3
- export const SYSTEM_UI_FONT =
4
- '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif';
5
- //# sourceMappingURL=index.js.map
3
+ export const SYSTEM_UI_FONT = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif';
4
+ //# sourceMappingURL=index.js.map
@@ -26,12 +26,7 @@ export function minifyStyles(css) {
26
26
  const preservedTokens = [];
27
27
  const comments = [];
28
28
  const totalLength = css.length;
29
- let startIndex = 0,
30
- endIndex = 0,
31
- i = 0,
32
- max = 0,
33
- token = '',
34
- placeholder = '';
29
+ let startIndex = 0, endIndex = 0, i = 0, max = 0, token = '', placeholder = '';
35
30
  // collect all comment blocks...
36
31
  while ((startIndex = css.indexOf('/*', startIndex)) >= 0) {
37
32
  endIndex = css.indexOf('*/', startIndex + 2);
@@ -42,10 +37,10 @@ export function minifyStyles(css) {
42
37
  comments.push(token);
43
38
  css =
44
39
  css.slice(0, startIndex + 2) +
45
- '___PRESERVE_CANDIDATE_COMMENT_' +
46
- (comments.length - 1) +
47
- '___' +
48
- css.slice(endIndex);
40
+ '___PRESERVE_CANDIDATE_COMMENT_' +
41
+ (comments.length - 1) +
42
+ '___' +
43
+ css.slice(endIndex);
49
44
  startIndex += 2;
50
45
  }
51
46
  // preserve strings so their content doesn't get accidentally minified
@@ -57,16 +52,11 @@ export function minifyStyles(css) {
57
52
  // one, maybe more? put'em back then
58
53
  if (match.indexOf('___PRESERVE_CANDIDATE_COMMENT_') >= 0) {
59
54
  for (i = 0, max = comments.length; i < max; i = i + 1) {
60
- match = match.replace(
61
- '___PRESERVE_CANDIDATE_COMMENT_' + i + '___',
62
- comments[i],
63
- );
55
+ match = match.replace('___PRESERVE_CANDIDATE_COMMENT_' + i + '___', comments[i]);
64
56
  }
65
57
  }
66
58
  preservedTokens.push(match);
67
- return (
68
- quote + '___PRESERVED_TOKEN_' + (preservedTokens.length - 1) + '___' + quote
69
- );
59
+ return (quote + '___PRESERVED_TOKEN_' + (preservedTokens.length - 1) + '___' + quote);
70
60
  });
71
61
  // strings are safe, now wrestle the comments
72
62
  for (i = 0, max = comments.length; i < max; i = i + 1) {
@@ -76,10 +66,7 @@ export function minifyStyles(css) {
76
66
  // so push to the preserved tokens keeping the !
77
67
  if (token.charAt(0) === '!') {
78
68
  preservedTokens.push(token);
79
- css = css.replace(
80
- placeholder,
81
- '___PRESERVED_TOKEN_' + (preservedTokens.length - 1) + '___',
82
- );
69
+ css = css.replace(placeholder, '___PRESERVED_TOKEN_' + (preservedTokens.length - 1) + '___');
83
70
  continue;
84
71
  }
85
72
  // otherwise, kill the comment
@@ -121,21 +108,15 @@ export function minifyStyles(css) {
121
108
  css = css.replace(/:0 0(;|\})/g, ':0$1');
122
109
  // Replace background-position:0; with background-position:0 0;
123
110
  // same for transform-origin
124
- css = css.replace(
125
- /(background-position|transform-origin):0(;|\})/gi,
126
- function (_all, prop, tail) {
127
- return prop.toLowerCase() + ':0 0' + tail;
128
- },
129
- );
111
+ css = css.replace(/(background-position|transform-origin):0(;|\})/gi, function (_all, prop, tail) {
112
+ return prop.toLowerCase() + ':0 0' + tail;
113
+ });
130
114
  // Replace 0.6 to .6, but only when preceded by : or a white-space
131
115
  css = css.replace(/(:|\s)0+\.(\d+)/g, '$1.$2');
132
116
  // border: none -> border:0
133
- css = css.replace(
134
- /(border|border-top|border-right|border-bottom|border-right|outline|background):none(;|\})/gi,
135
- function (_all, prop, tail) {
136
- return prop.toLowerCase() + ':0' + tail;
137
- },
138
- );
117
+ css = css.replace(/(border|border-top|border-right|border-bottom|border-right|outline|background):none(;|\})/gi, function (_all, prop, tail) {
118
+ return prop.toLowerCase() + ':0' + tail;
119
+ });
139
120
  // Remove empty rules.
140
121
  css = css.replace(/[^};{/]+\{\}/g, '');
141
122
  // Replace multiple semi-colons in a row by a single one
@@ -148,4 +129,4 @@ export function minifyStyles(css) {
148
129
  return css.trim();
149
130
  }
150
131
  export default minifyStyles;
151
- //# sourceMappingURL=minifyStyles.js.map
132
+ //# sourceMappingURL=minifyStyles.js.map
@@ -3,30 +3,24 @@ import { minifyStyles } from './minifyStyles.js';
3
3
  describe('@acusti/styling', () => {
4
4
  describe('minifyStyles.ts', () => {
5
5
  it('minifies basic CSS declarations', () => {
6
- expect(
7
- minifyStyles(`
6
+ expect(minifyStyles(`
8
7
  .foo {
9
8
  padding: 10px;
10
9
  color: red;
11
- }`),
12
- ).toBe('.foo{padding:10px;color:red}');
10
+ }`)).toBe('.foo{padding:10px;color:red}');
13
11
  });
14
12
  it('preserves whitespace where needed in selectors', () => {
15
- expect(
16
- minifyStyles(`
13
+ expect(minifyStyles(`
17
14
  .foo > .bar :hover {
18
15
  background-color: cyan;
19
- }`),
20
- ).toBe('.foo>.bar :hover{background-color:cyan}');
16
+ }`)).toBe('.foo>.bar :hover{background-color:cyan}');
21
17
  });
22
18
  it('minifies 0.6 to .6, but only when preceded by : or a whitespace', () => {
23
- expect(
24
- minifyStyles(`
19
+ expect(minifyStyles(`
25
20
  .foo {
26
21
  opacity: 0.6;
27
- }`),
28
- ).toBe('.foo{opacity:.6}');
22
+ }`)).toBe('.foo{opacity:.6}');
29
23
  });
30
24
  });
31
25
  });
32
- //# sourceMappingURL=minifyStyles.test.js.map
26
+ //# sourceMappingURL=minifyStyles.test.js.map
@@ -1,19 +1,12 @@
1
- type StyleRegistry = Map<
2
- string,
3
- {
4
- href: string;
5
- referenceCount: number;
6
- styles: string;
7
- }
8
- >;
9
- export declare const getStyleRegistry: () => StyleRegistry;
10
- export declare function useStyles(
11
- styles: string,
12
- initialHref?: string,
13
- ): {
1
+ type StyleCache = Map<string, {
2
+ href: string;
3
+ referenceCount: number;
4
+ styles: string;
5
+ }>;
6
+ export declare function useStyles(styles: string, initialHref?: string): {
14
7
  href: string;
15
8
  referenceCount: number;
16
9
  styles: string;
17
10
  };
18
11
  export default useStyles;
19
- export declare const clearRegistry: () => void;
12
+ export declare const getStyleCache: () => StyleCache;
package/dist/useStyles.js CHANGED
@@ -1,48 +1,43 @@
1
1
  import { useEffect, useState } from 'react';
2
2
  import { minifyStyles } from './minifyStyles.js';
3
- const styleRegistry = new Map();
4
- export const getStyleRegistry = () => styleRegistry;
3
+ const styleCache = new Map();
4
+ const EMPTY_STYLES_ITEM = { href: '', referenceCount: 0, styles: '' };
5
5
  export function useStyles(styles, initialHref) {
6
6
  const [stylesItem, setStylesItem] = useState(() => {
7
- if (!styles) return { href: '', referenceCount: 0, styles: '' };
7
+ if (!styles)
8
+ return EMPTY_STYLES_ITEM;
8
9
  const key = initialHref !== null && initialHref !== void 0 ? initialHref : styles;
9
- let item = styleRegistry.get(key);
10
+ let item = styleCache.get(key);
10
11
  if (item) {
11
12
  item.referenceCount++;
12
- } else {
13
+ }
14
+ else {
13
15
  const minified = minifyStyles(styles);
14
16
  item = {
15
- href: sanitizeHref(
16
- initialHref !== null && initialHref !== void 0
17
- ? initialHref
18
- : minified,
19
- ),
17
+ href: sanitizeHref(initialHref !== null && initialHref !== void 0 ? initialHref : minified),
20
18
  referenceCount: 1,
21
19
  styles: minified,
22
20
  };
23
- styleRegistry.set(key, item);
21
+ styleCache.set(key, item);
24
22
  }
25
23
  return item;
26
24
  });
27
25
  useEffect(() => {
28
- if (!styles) return;
26
+ if (!styles)
27
+ return;
29
28
  const key = initialHref !== null && initialHref !== void 0 ? initialHref : styles;
30
- if (!styleRegistry.get(key)) {
29
+ if (!styleCache.get(key)) {
31
30
  const minified = minifyStyles(styles);
32
31
  const item = {
33
- href: sanitizeHref(
34
- initialHref !== null && initialHref !== void 0
35
- ? initialHref
36
- : minified,
37
- ),
32
+ href: sanitizeHref(initialHref !== null && initialHref !== void 0 ? initialHref : minified),
38
33
  referenceCount: 1,
39
34
  styles: minified,
40
35
  };
41
- styleRegistry.set(key, item);
36
+ styleCache.set(key, item);
42
37
  setStylesItem(item);
43
38
  }
44
39
  return () => {
45
- const existingItem = styleRegistry.get(styles);
40
+ const existingItem = styleCache.get(styles);
46
41
  if (existingItem) {
47
42
  existingItem.referenceCount--;
48
43
  if (!existingItem.referenceCount) {
@@ -50,7 +45,7 @@ export function useStyles(styles, initialHref) {
50
45
  // and add another referenceCount check
51
46
  // to deal with instance where existing <Style>
52
47
  // component is moved in the tree or re-keyed
53
- styleRegistry.delete(styles);
48
+ styleCache.delete(styles);
54
49
  }
55
50
  }
56
51
  };
@@ -58,12 +53,11 @@ export function useStyles(styles, initialHref) {
58
53
  return stylesItem;
59
54
  }
60
55
  export default useStyles;
61
- export const clearRegistry = () => {
62
- styleRegistry.clear();
63
- };
64
56
  // Dashes in selectors in href prop create happy-dom / jsdom test errors:
65
57
  // Invalid regular expression (“Range out of order in character class”)
66
58
  function sanitizeHref(text) {
67
59
  return text.replace(/-/g, '');
68
60
  }
69
- //# sourceMappingURL=useStyles.js.map
61
+ // The following export is for test usage only
62
+ export const getStyleCache = () => styleCache;
63
+ //# sourceMappingURL=useStyles.js.map
@@ -5,7 +5,7 @@
5
5
  * @flow
6
6
  */
7
7
 
8
- declare type StyleRegistry = Map<
8
+ declare type StyleCache = Map<
9
9
  string,
10
10
  {|
11
11
  href: string,
@@ -13,7 +13,6 @@ declare type StyleRegistry = Map<
13
13
  styles: string,
14
14
  |}
15
15
  >;
16
- declare export var getStyleRegistry: () => StyleRegistry;
17
16
  declare export function useStyles(
18
17
  styles: string,
19
18
  initialHref?: string
@@ -23,4 +22,4 @@ declare export function useStyles(
23
22
  styles: string,
24
23
  |};
25
24
  declare export default typeof useStyles;
26
- declare export var clearRegistry: () => void;
25
+ declare export var getStyleCache: () => StyleCache;
@@ -1 +1 @@
1
- {"version":3,"file":"useStyles.js","sourceRoot":"","sources":["../src/useStyles.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAE5C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAOjD,MAAM,aAAa,GAAkB,IAAI,GAAG,EAAE,CAAC;AAE/C,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,EAAE,CAAC,aAAa,CAAC;AAEpD,MAAM,UAAU,SAAS,CAAC,MAAc,EAAE,WAAoB;IAC1D,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE;QAC9C,IAAI,CAAC,MAAM;YAAE,OAAO,EAAE,IAAI,EAAE,EAAE,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Y,CAAC,cAAc,EAAE,CAAC;oBAC/B,0CAA0C;oBAC1C,uCAAuC;oBACvC,+CAA+C;oBAC/C,6CAA6C;oBAC7C,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBAC9B,CAAC;YACL,CAAC;QACL,CAAC,CAAC;IACN,CAAC,EAAE,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC,CAAC;IAE1B,OAAO,UAAU,CAAC;AACtB,CAAC;AAED,eAAe,SAAS,CAAC;AAEzB,yEAAyE;AACzE,uEAAuE;AACvE,SAAS,YAAY,CAAC,IAAY;IAC9B,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AAClC,CAAC;AAED,8CAA8C;AAC9C,MAAM,CAAC,MAAM,aAAa,GAAG,GAAG,EAAE,CAAC,UAAU,CAAC"}
@@ -3,86 +3,77 @@ import { render } from '@testing-library/react';
3
3
  import React from 'react';
4
4
  import { beforeEach, describe, expect, it } from 'vitest';
5
5
  import Style from './Style.js';
6
- import { getStyleRegistry } from './useStyles.js';
6
+ import { getStyleCache } from './useStyles.js';
7
7
  describe('@acusti/styling', () => {
8
8
  describe('useStyles.ts', () => {
9
9
  const mockStylesA = '.test-a {\n color: cyan;\n}';
10
10
  const mockStylesB = '.test-b {\n padding: 10px;\n}';
11
- // reset styleRegistry before each test
11
+ // reset styleCache before each test
12
12
  beforeEach(() => {
13
- getStyleRegistry().clear();
13
+ getStyleCache().clear();
14
14
  });
15
15
  it('should cache minified styles in the registry keyed by the style string', () => {
16
- const styleRegistry = getStyleRegistry();
17
- const { rerender } = render(
18
- React.createElement(
19
- React.Fragment,
20
- null,
21
- React.createElement(Style, null, mockStylesA),
22
- React.createElement(Style, null, mockStylesA),
23
- ),
24
- );
25
- let stylesItemA = styleRegistry.get(mockStylesA);
26
- expect(
27
- stylesItemA === null || stylesItemA === void 0
28
- ? void 0
29
- : stylesItemA.referenceCount,
30
- ).toBe(2);
31
- expect(
32
- stylesItemA === null || stylesItemA === void 0
33
- ? void 0
34
- : stylesItemA.styles,
35
- ).toBe('.test-a{color:cyan}');
36
- expect(styleRegistry.size).toBe(1);
16
+ const styleCache = getStyleCache();
17
+ const { rerender } = render(React.createElement(React.Fragment, null,
18
+ React.createElement(Style, null, mockStylesA),
19
+ React.createElement(Style, null, mockStylesA)));
20
+ let stylesItemA = styleCache.get(mockStylesA);
21
+ expect(stylesItemA === null || stylesItemA === void 0 ? void 0 : stylesItemA.referenceCount).toBe(2);
22
+ expect(stylesItemA === null || stylesItemA === void 0 ? void 0 : stylesItemA.styles).toBe('.test-a{color:cyan}');
23
+ expect(styleCache.size).toBe(1);
37
24
  rerender(React.createElement(Style, null, mockStylesA));
38
- expect(
39
- stylesItemA === null || stylesItemA === void 0
40
- ? void 0
41
- : stylesItemA.referenceCount,
42
- ).toBe(1);
43
- expect(stylesItemA).toBe(styleRegistry.get(mockStylesA));
44
- expect(styleRegistry.size).toBe(1);
25
+ expect(stylesItemA === null || stylesItemA === void 0 ? void 0 : stylesItemA.referenceCount).toBe(1);
26
+ expect(stylesItemA).toBe(styleCache.get(mockStylesA));
27
+ expect(styleCache.size).toBe(1);
45
28
  rerender(React.createElement(Style, null, mockStylesB));
46
- stylesItemA = styleRegistry.get(mockStylesA);
29
+ stylesItemA = styleCache.get(mockStylesA);
47
30
  expect(stylesItemA).toBe(undefined);
48
- let stylesItemB = styleRegistry.get(mockStylesB);
49
- expect(
50
- stylesItemB === null || stylesItemB === void 0
51
- ? void 0
52
- : stylesItemB.referenceCount,
53
- ).toBe(1);
54
- expect(styleRegistry.size).toBe(1);
55
- rerender(
56
- React.createElement(
57
- React.Fragment,
58
- null,
59
- React.createElement(Style, null, mockStylesA),
60
- React.createElement(Style, null, mockStylesB),
61
- ),
62
- );
63
- stylesItemA = styleRegistry.get(mockStylesA);
64
- expect(
65
- stylesItemA === null || stylesItemA === void 0
66
- ? void 0
67
- : stylesItemA.referenceCount,
68
- ).toBe(1);
69
- expect(stylesItemA).toBe(styleRegistry.get(mockStylesA));
70
- stylesItemB = styleRegistry.get(mockStylesB);
71
- expect(
72
- stylesItemB === null || stylesItemB === void 0
73
- ? void 0
74
- : stylesItemB.referenceCount,
75
- ).toBe(1);
76
- expect(styleRegistry.size).toBe(2);
77
- rerender(React.createElement('div', null));
78
- expect(styleRegistry.size).toBe(0);
31
+ let stylesItemB = styleCache.get(mockStylesB);
32
+ expect(stylesItemB === null || stylesItemB === void 0 ? void 0 : stylesItemB.referenceCount).toBe(1);
33
+ expect(styleCache.size).toBe(1);
34
+ rerender(React.createElement(React.Fragment, null,
35
+ React.createElement(Style, null, mockStylesA),
36
+ React.createElement(Style, null, mockStylesB)));
37
+ stylesItemA = styleCache.get(mockStylesA);
38
+ expect(stylesItemA === null || stylesItemA === void 0 ? void 0 : stylesItemA.referenceCount).toBe(1);
39
+ expect(stylesItemA).toBe(styleCache.get(mockStylesA));
40
+ stylesItemB = styleCache.get(mockStylesB);
41
+ expect(stylesItemB === null || stylesItemB === void 0 ? void 0 : stylesItemB.referenceCount).toBe(1);
42
+ expect(styleCache.size).toBe(2);
43
+ rerender(React.createElement("div", null));
44
+ expect(styleCache.size).toBe(0);
45
+ });
46
+ it('should preserve style cache across component position changes and re-keying', () => {
47
+ const styleCache = getStyleCache();
48
+ const { rerender } = render(React.createElement(React.Fragment, null,
49
+ React.createElement(Style, null, mockStylesA)));
50
+ const stylesItemA = styleCache.get(mockStylesA);
51
+ expect(stylesItemA === null || stylesItemA === void 0 ? void 0 : stylesItemA.referenceCount).toBe(1);
52
+ expect(stylesItemA === null || stylesItemA === void 0 ? void 0 : stylesItemA.styles).toBe('.test-a{color:cyan}');
53
+ expect(styleCache.size).toBe(1);
54
+ rerender(React.createElement(React.Fragment, null,
55
+ React.createElement(Style, null, mockStylesB),
56
+ React.createElement(Style, null, mockStylesA)));
57
+ expect(stylesItemA === null || stylesItemA === void 0 ? void 0 : stylesItemA.referenceCount).toBe(1);
58
+ expect(stylesItemA).toBe(styleCache.get(mockStylesA));
59
+ rerender(React.createElement(Style, { key: "new-a" }, mockStylesA));
60
+ expect(stylesItemA === null || stylesItemA === void 0 ? void 0 : stylesItemA.referenceCount).toBe(1);
61
+ expect(stylesItemA).toBe(styleCache.get(mockStylesA));
62
+ rerender(React.createElement(React.Fragment, null,
63
+ React.createElement(Style, null, mockStylesA),
64
+ React.createElement(Style, { key: "new-a" }, mockStylesA)));
65
+ expect(stylesItemA === null || stylesItemA === void 0 ? void 0 : stylesItemA.referenceCount).toBe(2);
66
+ expect(stylesItemA).toBe(styleCache.get(mockStylesA));
67
+ rerender(React.createElement("div", null));
68
+ expect(stylesItemA === null || stylesItemA === void 0 ? void 0 : stylesItemA.referenceCount).toBe(0);
69
+ expect(styleCache.size).toBe(0);
70
+ });
71
+ it('should sanitize styles used as href prop if no href prop provided', () => {
72
+ render(React.createElement(Style, null, `div[data-foo-bar] { color: cyan; }`));
73
+ // the two-dash attribute selector results in “Range out of order in character class”
74
+ // and render() fails with SyntaxError: Invalid regular expression if not sanitized
75
+ expect(true).toBeTruthy();
79
76
  });
80
- });
81
- it('should sanitize styles used as href prop if no href prop provided', () => {
82
- render(React.createElement(Style, null, `div[data-foo-bar] { color: cyan; }`));
83
- // the two-dash attribute selector results in “Range out of order in character class”
84
- // and render() fails with SyntaxError: Invalid regular expression if not sanitized
85
- expect(true).toBeTruthy();
86
77
  });
87
78
  });
88
- //# sourceMappingURL=useStyles.test.js.map
79
+ //# sourceMappingURL=useStyles.test.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useStyles.test.js","sourceRoot":"","sources":["../src/useStyles.test.tsx"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAE1D,OAAO,KAAK,MAAM,YAAY,CAAC;AA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C,CAAC;QACpC,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,mEAAmE,EAAE,GAAG,EAAE;YACzE,MAAM,CAAC,oBAAC,KAAK,QAAE,oCAAoC,CAAS,CAAC,CAAC;YAC9D,qFAAqF;YACrF,mFAAmF;YACnF,MAAM,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,CAAC;QAC9B,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acusti/styling",
3
- "version": "1.0.0-rc.0",
3
+ "version": "1.0.0-rc.1",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "exports": "./dist/index.js",
@@ -4,20 +4,20 @@ import React from 'react';
4
4
  import { beforeEach, describe, expect, it } from 'vitest';
5
5
 
6
6
  import Style from './Style.js';
7
- import { getStyleRegistry } from './useStyles.js';
7
+ import { getStyleCache } from './useStyles.js';
8
8
 
9
9
  describe('@acusti/styling', () => {
10
10
  describe('useStyles.ts', () => {
11
11
  const mockStylesA = '.test-a {\n color: cyan;\n}';
12
12
  const mockStylesB = '.test-b {\n padding: 10px;\n}';
13
13
 
14
- // reset styleRegistry before each test
14
+ // reset styleCache before each test
15
15
  beforeEach(() => {
16
- getStyleRegistry().clear();
16
+ getStyleCache().clear();
17
17
  });
18
18
 
19
19
  it('should cache minified styles in the registry keyed by the style string', () => {
20
- const styleRegistry = getStyleRegistry();
20
+ const styleCache = getStyleCache();
21
21
 
22
22
  const { rerender } = render(
23
23
  <React.Fragment>
@@ -26,22 +26,22 @@ describe('@acusti/styling', () => {
26
26
  </React.Fragment>,
27
27
  );
28
28
 
29
- let stylesItemA = styleRegistry.get(mockStylesA);
29
+ let stylesItemA = styleCache.get(mockStylesA);
30
30
  expect(stylesItemA?.referenceCount).toBe(2);
31
31
  expect(stylesItemA?.styles).toBe('.test-a{color:cyan}');
32
- expect(styleRegistry.size).toBe(1);
32
+ expect(styleCache.size).toBe(1);
33
33
 
34
34
  rerender(<Style>{mockStylesA}</Style>);
35
35
  expect(stylesItemA?.referenceCount).toBe(1);
36
- expect(stylesItemA).toBe(styleRegistry.get(mockStylesA));
37
- expect(styleRegistry.size).toBe(1);
36
+ expect(stylesItemA).toBe(styleCache.get(mockStylesA));
37
+ expect(styleCache.size).toBe(1);
38
38
 
39
39
  rerender(<Style>{mockStylesB}</Style>);
40
- stylesItemA = styleRegistry.get(mockStylesA);
40
+ stylesItemA = styleCache.get(mockStylesA);
41
41
  expect(stylesItemA).toBe(undefined);
42
- let stylesItemB = styleRegistry.get(mockStylesB);
42
+ let stylesItemB = styleCache.get(mockStylesB);
43
43
  expect(stylesItemB?.referenceCount).toBe(1);
44
- expect(styleRegistry.size).toBe(1);
44
+ expect(styleCache.size).toBe(1);
45
45
 
46
46
  rerender(
47
47
  <React.Fragment>
@@ -49,22 +49,63 @@ describe('@acusti/styling', () => {
49
49
  <Style>{mockStylesB}</Style>
50
50
  </React.Fragment>,
51
51
  );
52
- stylesItemA = styleRegistry.get(mockStylesA);
52
+ stylesItemA = styleCache.get(mockStylesA);
53
53
  expect(stylesItemA?.referenceCount).toBe(1);
54
- expect(stylesItemA).toBe(styleRegistry.get(mockStylesA));
55
- stylesItemB = styleRegistry.get(mockStylesB);
54
+ expect(stylesItemA).toBe(styleCache.get(mockStylesA));
55
+ stylesItemB = styleCache.get(mockStylesB);
56
56
  expect(stylesItemB?.referenceCount).toBe(1);
57
- expect(styleRegistry.size).toBe(2);
57
+ expect(styleCache.size).toBe(2);
58
58
 
59
59
  rerender(<div />);
60
- expect(styleRegistry.size).toBe(0);
60
+ expect(styleCache.size).toBe(0);
61
61
  });
62
- });
63
62
 
64
- it('should sanitize styles used as href prop if no href prop provided', () => {
65
- render(<Style>{`div[data-foo-bar] { color: cyan; }`}</Style>);
66
- // the two-dash attribute selector results in “Range out of order in character class”
67
- // and render() fails with SyntaxError: Invalid regular expression if not sanitized
68
- expect(true).toBeTruthy();
63
+ it('should preserve style cache across component position changes and re-keying', () => {
64
+ const styleCache = getStyleCache();
65
+
66
+ const { rerender } = render(
67
+ <React.Fragment>
68
+ <Style>{mockStylesA}</Style>
69
+ </React.Fragment>,
70
+ );
71
+
72
+ const stylesItemA = styleCache.get(mockStylesA);
73
+ expect(stylesItemA?.referenceCount).toBe(1);
74
+ expect(stylesItemA?.styles).toBe('.test-a{color:cyan}');
75
+ expect(styleCache.size).toBe(1);
76
+
77
+ rerender(
78
+ <React.Fragment>
79
+ <Style>{mockStylesB}</Style>
80
+ <Style>{mockStylesA}</Style>
81
+ </React.Fragment>,
82
+ );
83
+ expect(stylesItemA?.referenceCount).toBe(1);
84
+ expect(stylesItemA).toBe(styleCache.get(mockStylesA));
85
+
86
+ rerender(<Style key="new-a">{mockStylesA}</Style>);
87
+ expect(stylesItemA?.referenceCount).toBe(1);
88
+ expect(stylesItemA).toBe(styleCache.get(mockStylesA));
89
+
90
+ rerender(
91
+ <React.Fragment>
92
+ <Style>{mockStylesA}</Style>
93
+ <Style key="new-a">{mockStylesA}</Style>
94
+ </React.Fragment>,
95
+ );
96
+ expect(stylesItemA?.referenceCount).toBe(2);
97
+ expect(stylesItemA).toBe(styleCache.get(mockStylesA));
98
+
99
+ rerender(<div />);
100
+ expect(stylesItemA?.referenceCount).toBe(0);
101
+ expect(styleCache.size).toBe(0);
102
+ });
103
+
104
+ it('should sanitize styles used as href prop if no href prop provided', () => {
105
+ render(<Style>{`div[data-foo-bar] { color: cyan; }`}</Style>);
106
+ // the two-dash attribute selector results in “Range out of order in character class”
107
+ // and render() fails with SyntaxError: Invalid regular expression if not sanitized
108
+ expect(true).toBeTruthy();
109
+ });
69
110
  });
70
111
  });
package/src/useStyles.ts CHANGED
@@ -2,21 +2,18 @@ import { useEffect, useState } from 'react';
2
2
 
3
3
  import { minifyStyles } from './minifyStyles.js';
4
4
 
5
- type StyleRegistry = Map<
6
- string,
7
- { href: string; referenceCount: number; styles: string }
8
- >;
5
+ type StyleCache = Map<string, { href: string; referenceCount: number; styles: string }>;
9
6
 
10
- const styleRegistry: StyleRegistry = new Map();
7
+ const styleCache: StyleCache = new Map();
11
8
 
12
- export const getStyleRegistry = () => styleRegistry;
9
+ const EMPTY_STYLES_ITEM = { href: '', referenceCount: 0, styles: '' };
13
10
 
14
11
  export function useStyles(styles: string, initialHref?: string) {
15
12
  const [stylesItem, setStylesItem] = useState(() => {
16
- if (!styles) return { href: '', referenceCount: 0, styles: '' };
13
+ if (!styles) return EMPTY_STYLES_ITEM;
17
14
 
18
15
  const key = initialHref ?? styles;
19
- let item = styleRegistry.get(key);
16
+ let item = styleCache.get(key);
20
17
 
21
18
  if (item) {
22
19
  item.referenceCount++;
@@ -27,7 +24,7 @@ export function useStyles(styles: string, initialHref?: string) {
27
24
  referenceCount: 1,
28
25
  styles: minified,
29
26
  };
30
- styleRegistry.set(key, item);
27
+ styleCache.set(key, item);
31
28
  }
32
29
 
33
30
  return item;
@@ -38,19 +35,19 @@ export function useStyles(styles: string, initialHref?: string) {
38
35
 
39
36
  const key = initialHref ?? styles;
40
37
 
41
- if (!styleRegistry.get(key)) {
38
+ if (!styleCache.get(key)) {
42
39
  const minified = minifyStyles(styles);
43
40
  const item = {
44
41
  href: sanitizeHref(initialHref ?? minified),
45
42
  referenceCount: 1,
46
43
  styles: minified,
47
44
  };
48
- styleRegistry.set(key, item);
45
+ styleCache.set(key, item);
49
46
  setStylesItem(item);
50
47
  }
51
48
 
52
49
  return () => {
53
- const existingItem = styleRegistry.get(styles);
50
+ const existingItem = styleCache.get(styles);
54
51
  if (existingItem) {
55
52
  existingItem.referenceCount--;
56
53
  if (!existingItem.referenceCount) {
@@ -58,7 +55,7 @@ export function useStyles(styles: string, initialHref?: string) {
58
55
  // and add another referenceCount check
59
56
  // to deal with instance where existing <Style>
60
57
  // component is moved in the tree or re-keyed
61
- styleRegistry.delete(styles);
58
+ styleCache.delete(styles);
62
59
  }
63
60
  }
64
61
  };
@@ -69,12 +66,11 @@ export function useStyles(styles: string, initialHref?: string) {
69
66
 
70
67
  export default useStyles;
71
68
 
72
- export const clearRegistry = () => {
73
- styleRegistry.clear();
74
- };
75
-
76
69
  // Dashes in selectors in href prop create happy-dom / jsdom test errors:
77
70
  // Invalid regular expression (“Range out of order in character class”)
78
71
  function sanitizeHref(text: string) {
79
72
  return text.replace(/-/g, '');
80
73
  }
74
+
75
+ // The following export is for test usage only
76
+ export const getStyleCache = () => styleCache;