@acusti/styling 0.7.2 → 1.0.0-rc.0

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.
@@ -0,0 +1,88 @@
1
+ // @vitest-environment happy-dom
2
+ import { render } from '@testing-library/react';
3
+ import React from 'react';
4
+ import { beforeEach, describe, expect, it } from 'vitest';
5
+ import Style from './Style.js';
6
+ import { getStyleRegistry } from './useStyles.js';
7
+ describe('@acusti/styling', () => {
8
+ describe('useStyles.ts', () => {
9
+ const mockStylesA = '.test-a {\n color: cyan;\n}';
10
+ const mockStylesB = '.test-b {\n padding: 10px;\n}';
11
+ // reset styleRegistry before each test
12
+ beforeEach(() => {
13
+ getStyleRegistry().clear();
14
+ });
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);
37
+ 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);
45
+ rerender(React.createElement(Style, null, mockStylesB));
46
+ stylesItemA = styleRegistry.get(mockStylesA);
47
+ 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);
79
+ });
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
+ });
87
+ });
88
+ //# sourceMappingURL=useStyles.test.js.map
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Flowtype definitions for useStyles.test
3
+ * Generated by Flowgen from a Typescript Definition
4
+ * Flowgen v1.20.1
5
+ * @flow
6
+ */
@@ -0,0 +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,O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AAC,aAAa,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACvC,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mEAAmE,EAAE,GAAG,EAAE;QACzE,MAAM,CAAC,oBAAC,KAAK,QAAE,oCAAoC,CAAS,CAAC,CAAC;QAC9D,qFAAqF;QACrF,mFAAmF;QACnF,MAAM,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,CAAC;IAC9B,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acusti/styling",
3
- "version": "0.7.2",
3
+ "version": "1.0.0-rc.0",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "exports": "./dist/index.js",
@@ -37,18 +37,18 @@
37
37
  },
38
38
  "homepage": "https://github.com/acusti/uikit/tree/main/packages/styling#readme",
39
39
  "devDependencies": {
40
- "@testing-library/dom": "^9.3.1",
41
- "@testing-library/react": "^14.0.0",
42
- "@testing-library/user-event": "^14.4.3",
43
- "@types/react": "^18.2.45",
44
- "happy-dom": "^12.10.3",
45
- "react": "^18",
46
- "react-dom": "^18",
47
- "typescript": "^5.3.3",
40
+ "@testing-library/dom": "^10.4.0",
41
+ "@testing-library/react": "^16.0.1",
42
+ "@testing-library/user-event": "^14.5.2",
43
+ "@types/react": "^18.3.3",
44
+ "happy-dom": "^15.7.3",
45
+ "react": "^19.0.0-0",
46
+ "react-dom": "^19.0.0-0",
47
+ "typescript": "5.3.3",
48
48
  "vitest": "^1.1.0"
49
49
  },
50
50
  "peerDependencies": {
51
- "react": "^16.8 || ^17 || ^18",
52
- "react-dom": "^16.8 || ^17 || ^18"
51
+ "react": "^19.0.0-0",
52
+ "react-dom": "^19.0.0-0"
53
53
  }
54
54
  }
package/src/Style.tsx CHANGED
@@ -1,65 +1,26 @@
1
- import * as React from 'react';
1
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
+ import React from 'react';
2
3
 
3
- import {
4
- getRegisteredStyles,
5
- registerStyles,
6
- unregisterStyles,
7
- updateStyles,
8
- } from './style-registry.js';
9
-
10
- const { useCallback, useEffect, useMemo, useRef, useState } = React;
4
+ import { useStyles } from './useStyles.js';
11
5
 
12
6
  type Props = {
13
7
  children: string;
8
+ href?: string;
9
+ precedence?: string;
14
10
  };
15
11
 
16
- const Style = ({ children }: Props) => {
12
+ const Style = ({ children, href: _href, precedence = 'medium' }: Props) => {
17
13
  // Minify CSS styles by replacing consecutive whitespace (including \n) with ' '
18
- const styles = useMemo(() => children.replace(/\s+/gm, ' '), [children]);
19
- const [ownerDocument, setOwnerDocument] = useState<Document | null>(null);
20
- const isMountedRef = useRef<boolean>(false);
21
-
22
- useEffect(() => {
23
- isMountedRef.current = true;
24
- unregisterStyles({ ownerDocument: 'global', styles });
25
- }, []); // eslint-disable-line react-hooks/exhaustive-deps
26
-
27
- useEffect(
28
- () => () => {
29
- if (!ownerDocument) return;
30
- unregisterStyles({ ownerDocument, styles });
31
- },
32
- [ownerDocument], // eslint-disable-line react-hooks/exhaustive-deps
14
+ const { href, styles } = useStyles(children, _href);
15
+ // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/canary.d.ts
16
+ // https://react.dev/reference/react-dom/components/style#props
17
+ return (
18
+ // @ts-expect-error @types/react is missing new <style> props
19
+ // eslint-disable-next-line react/no-unknown-property
20
+ <style href={href} precedence={precedence}>
21
+ {styles}
22
+ </style>
33
23
  );
34
-
35
- const previousStylesRef = useRef<string>('');
36
-
37
- useEffect(() => {
38
- if (!ownerDocument) return;
39
-
40
- updateStyles({
41
- ownerDocument,
42
- previousStyles: previousStylesRef.current,
43
- styles,
44
- });
45
-
46
- previousStylesRef.current = styles;
47
- }, [ownerDocument, styles]);
48
-
49
- const handleRef = useCallback((element: HTMLElement | null) => {
50
- if (!element) return;
51
- setOwnerDocument(element.ownerDocument);
52
- }, []);
53
-
54
- if (ownerDocument) return null;
55
-
56
- // Avoid duplicate style rendering during SSR via style registry
57
- if (!isMountedRef.current) {
58
- if (getRegisteredStyles({ ownerDocument: 'global', styles })) return null;
59
- registerStyles({ ownerDocument: 'global', styles });
60
- }
61
-
62
- return <style dangerouslySetInnerHTML={{ __html: styles }} ref={handleRef} />;
63
24
  };
64
25
 
65
26
  export default Style;
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="react/canary" />
1
2
  export { default as Style } from './Style.js';
2
3
 
3
4
  export const SYSTEM_UI_FONT =
@@ -0,0 +1,35 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { minifyStyles } from './minifyStyles.js';
4
+
5
+ describe('@acusti/styling', () => {
6
+ describe('minifyStyles.ts', () => {
7
+ it('minifies basic CSS declarations', () => {
8
+ expect(
9
+ minifyStyles(`
10
+ .foo {
11
+ padding: 10px;
12
+ color: red;
13
+ }`),
14
+ ).toBe('.foo{padding:10px;color:red}');
15
+ });
16
+
17
+ it('preserves whitespace where needed in selectors', () => {
18
+ expect(
19
+ minifyStyles(`
20
+ .foo > .bar :hover {
21
+ background-color: cyan;
22
+ }`),
23
+ ).toBe('.foo>.bar :hover{background-color:cyan}');
24
+ });
25
+
26
+ it('minifies 0.6 to .6, but only when preceded by : or a whitespace', () => {
27
+ expect(
28
+ minifyStyles(`
29
+ .foo {
30
+ opacity: 0.6;
31
+ }`),
32
+ ).toBe('.foo{opacity:.6}');
33
+ });
34
+ });
35
+ });
@@ -0,0 +1,181 @@
1
+ /**
2
+ * Adapted from:
3
+ * https://github.com/jbleuzen/node-cssmin/blob/master/cssmin.js
4
+ * node-cssmin
5
+ * A simple module for Node.js that minify CSS
6
+ * Author : Johan Bleuzen
7
+ */
8
+
9
+ /**
10
+ * cssmin.js
11
+ * Author: Stoyan Stefanov - http://phpied.com/
12
+ * This is a JavaScript port of the CSS minification tool
13
+ * distributed with YUICompressor, itself a port
14
+ * of the cssmin utility by Isaac Schlueter - http://foohack.com/
15
+ * Permission is hereby granted to use the JavaScript version under the same
16
+ * conditions as the YUICompressor (original YUICompressor note below).
17
+ */
18
+
19
+ /*
20
+ * YUI Compressor
21
+ * http://developer.yahoo.com/yui/compressor/
22
+ * Author: Julien Lecomte - http://www.julienlecomte.net/
23
+ * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
24
+ * The copyrights embodied in the content of this file are licensed
25
+ * by Yahoo! Inc. under the BSD (revised) open source license.
26
+ */
27
+
28
+ export function minifyStyles(css: string) {
29
+ const preservedTokens: Array<string> = [];
30
+ const comments: Array<string> = [];
31
+ const totalLength = css.length;
32
+ let startIndex = 0,
33
+ endIndex = 0,
34
+ i = 0,
35
+ max = 0,
36
+ token = '',
37
+ placeholder = '';
38
+
39
+ // collect all comment blocks...
40
+ while ((startIndex = css.indexOf('/*', startIndex)) >= 0) {
41
+ endIndex = css.indexOf('*/', startIndex + 2);
42
+ if (endIndex < 0) {
43
+ endIndex = totalLength;
44
+ }
45
+ token = css.slice(startIndex + 2, endIndex);
46
+ comments.push(token);
47
+ css =
48
+ css.slice(0, startIndex + 2) +
49
+ '___PRESERVE_CANDIDATE_COMMENT_' +
50
+ (comments.length - 1) +
51
+ '___' +
52
+ css.slice(endIndex);
53
+ startIndex += 2;
54
+ }
55
+
56
+ // preserve strings so their content doesn't get accidentally minified
57
+ css = css.replace(/("([^\\"]|\\.|\\)*")|('([^\\']|\\.|\\)*')/g, function (match) {
58
+ const quote = match.substring(0, 1);
59
+ let i, max;
60
+
61
+ match = match.slice(1, -1);
62
+
63
+ // maybe the string contains a comment-like substring?
64
+ // one, maybe more? put'em back then
65
+ if (match.indexOf('___PRESERVE_CANDIDATE_COMMENT_') >= 0) {
66
+ for (i = 0, max = comments.length; i < max; i = i + 1) {
67
+ match = match.replace(
68
+ '___PRESERVE_CANDIDATE_COMMENT_' + i + '___',
69
+ comments[i],
70
+ );
71
+ }
72
+ }
73
+
74
+ preservedTokens.push(match);
75
+ return (
76
+ quote + '___PRESERVED_TOKEN_' + (preservedTokens.length - 1) + '___' + quote
77
+ );
78
+ });
79
+
80
+ // strings are safe, now wrestle the comments
81
+ for (i = 0, max = comments.length; i < max; i = i + 1) {
82
+ token = comments[i];
83
+ placeholder = '___PRESERVE_CANDIDATE_COMMENT_' + i + '___';
84
+
85
+ // ! in the first position of the comment means preserve
86
+ // so push to the preserved tokens keeping the !
87
+ if (token.charAt(0) === '!') {
88
+ preservedTokens.push(token);
89
+ css = css.replace(
90
+ placeholder,
91
+ '___PRESERVED_TOKEN_' + (preservedTokens.length - 1) + '___',
92
+ );
93
+ continue;
94
+ }
95
+
96
+ // otherwise, kill the comment
97
+ css = css.replace('/*' + placeholder + '*/', '');
98
+ }
99
+
100
+ // Normalize all whitespace strings to single spaces. Easier to work with that way.
101
+ css = css.replace(/\s+/g, ' ');
102
+
103
+ // Remove the spaces before the things that should not have spaces before them.
104
+ // But, be careful not to turn "p :link {...}" into "p:link{...}"
105
+ // Swap out any pseudo-class colons with the token, and then swap back.
106
+ css = css.replace(/(^|\})(([^{:])+:)+([^{]*\{)/g, function (m) {
107
+ return m.replace(/:/g, '___PSEUDOCLASSCOLON___');
108
+ });
109
+
110
+ // Preserve spaces in calc expressions
111
+ css = css.replace(/calc\s*\(\s*(.*?)\s*\)/g, function (m, c: string) {
112
+ return m.replace(c, c.replace(/\s+/g, '___SPACE_IN_CALC___'));
113
+ });
114
+
115
+ css = css.replace(/\s+([!{};:>+()\],])/g, '$1');
116
+ css = css.replace(/___PSEUDOCLASSCOLON___/g, ':');
117
+
118
+ // no space after the end of a preserved comment
119
+ css = css.replace(/\*\/ /g, '*/');
120
+
121
+ // If there is a @charset, then only allow one, and push to the top of the file.
122
+ css = css.replace(/^(.*)(@charset "[^"]*";)/gi, '$2$1');
123
+ css = css.replace(/^(\s*@charset [^;]+;\s*)+/gi, '$1');
124
+
125
+ // Put the space back in some cases, to support stuff like
126
+ // @media screen and (-webkit-min-device-pixel-ratio:0){
127
+ css = css.replace(/\band\(/gi, 'and (');
128
+
129
+ // Remove the spaces after the things that should not have spaces after them.
130
+ css = css.replace(/([!{}:;>+([,])\s+/g, '$1');
131
+
132
+ // Restore preserved spaces in calc expressions
133
+ css = css.replace(/___SPACE_IN_CALC___/g, ' ');
134
+
135
+ // remove unnecessary semicolons
136
+ css = css.replace(/;+\}/g, '}');
137
+
138
+ // Replace 0(px,em,%) with 0.
139
+ css = css.replace(/([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)/gi, '$1$2');
140
+
141
+ // Replace 0 0 0 0; with 0.
142
+ css = css.replace(/:0 0 0 0(;|\})/g, ':0$1');
143
+ css = css.replace(/:0 0 0(;|\})/g, ':0$1');
144
+ css = css.replace(/:0 0(;|\})/g, ':0$1');
145
+
146
+ // Replace background-position:0; with background-position:0 0;
147
+ // same for transform-origin
148
+ css = css.replace(
149
+ /(background-position|transform-origin):0(;|\})/gi,
150
+ function (_all, prop: string, tail: string) {
151
+ return prop.toLowerCase() + ':0 0' + tail;
152
+ },
153
+ );
154
+
155
+ // Replace 0.6 to .6, but only when preceded by : or a white-space
156
+ css = css.replace(/(:|\s)0+\.(\d+)/g, '$1.$2');
157
+
158
+ // border: none -> border:0
159
+ css = css.replace(
160
+ /(border|border-top|border-right|border-bottom|border-right|outline|background):none(;|\})/gi,
161
+ function (_all, prop: string, tail: string) {
162
+ return prop.toLowerCase() + ':0' + tail;
163
+ },
164
+ );
165
+
166
+ // Remove empty rules.
167
+ css = css.replace(/[^};{/]+\{\}/g, '');
168
+
169
+ // Replace multiple semi-colons in a row by a single one
170
+ // See SF bug #1980989
171
+ css = css.replace(/;;+/g, ';');
172
+
173
+ // restore preserved comments and strings
174
+ for (i = 0, max = preservedTokens.length; i < max; i = i + 1) {
175
+ css = css.replace('___PRESERVED_TOKEN_' + i + '___', preservedTokens[i]);
176
+ }
177
+
178
+ return css.trim();
179
+ }
180
+
181
+ export default minifyStyles;
@@ -0,0 +1,70 @@
1
+ // @vitest-environment happy-dom
2
+ import { render } from '@testing-library/react';
3
+ import React from 'react';
4
+ import { beforeEach, describe, expect, it } from 'vitest';
5
+
6
+ import Style from './Style.js';
7
+ import { getStyleRegistry } from './useStyles.js';
8
+
9
+ describe('@acusti/styling', () => {
10
+ describe('useStyles.ts', () => {
11
+ const mockStylesA = '.test-a {\n color: cyan;\n}';
12
+ const mockStylesB = '.test-b {\n padding: 10px;\n}';
13
+
14
+ // reset styleRegistry before each test
15
+ beforeEach(() => {
16
+ getStyleRegistry().clear();
17
+ });
18
+
19
+ it('should cache minified styles in the registry keyed by the style string', () => {
20
+ const styleRegistry = getStyleRegistry();
21
+
22
+ const { rerender } = render(
23
+ <React.Fragment>
24
+ <Style>{mockStylesA}</Style>
25
+ <Style>{mockStylesA}</Style>
26
+ </React.Fragment>,
27
+ );
28
+
29
+ let stylesItemA = styleRegistry.get(mockStylesA);
30
+ expect(stylesItemA?.referenceCount).toBe(2);
31
+ expect(stylesItemA?.styles).toBe('.test-a{color:cyan}');
32
+ expect(styleRegistry.size).toBe(1);
33
+
34
+ rerender(<Style>{mockStylesA}</Style>);
35
+ expect(stylesItemA?.referenceCount).toBe(1);
36
+ expect(stylesItemA).toBe(styleRegistry.get(mockStylesA));
37
+ expect(styleRegistry.size).toBe(1);
38
+
39
+ rerender(<Style>{mockStylesB}</Style>);
40
+ stylesItemA = styleRegistry.get(mockStylesA);
41
+ expect(stylesItemA).toBe(undefined);
42
+ let stylesItemB = styleRegistry.get(mockStylesB);
43
+ expect(stylesItemB?.referenceCount).toBe(1);
44
+ expect(styleRegistry.size).toBe(1);
45
+
46
+ rerender(
47
+ <React.Fragment>
48
+ <Style>{mockStylesA}</Style>
49
+ <Style>{mockStylesB}</Style>
50
+ </React.Fragment>,
51
+ );
52
+ stylesItemA = styleRegistry.get(mockStylesA);
53
+ expect(stylesItemA?.referenceCount).toBe(1);
54
+ expect(stylesItemA).toBe(styleRegistry.get(mockStylesA));
55
+ stylesItemB = styleRegistry.get(mockStylesB);
56
+ expect(stylesItemB?.referenceCount).toBe(1);
57
+ expect(styleRegistry.size).toBe(2);
58
+
59
+ rerender(<div />);
60
+ expect(styleRegistry.size).toBe(0);
61
+ });
62
+ });
63
+
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();
69
+ });
70
+ });
@@ -0,0 +1,80 @@
1
+ import { useEffect, useState } from 'react';
2
+
3
+ import { minifyStyles } from './minifyStyles.js';
4
+
5
+ type StyleRegistry = Map<
6
+ string,
7
+ { href: string; referenceCount: number; styles: string }
8
+ >;
9
+
10
+ const styleRegistry: StyleRegistry = new Map();
11
+
12
+ export const getStyleRegistry = () => styleRegistry;
13
+
14
+ export function useStyles(styles: string, initialHref?: string) {
15
+ const [stylesItem, setStylesItem] = useState(() => {
16
+ if (!styles) return { href: '', referenceCount: 0, styles: '' };
17
+
18
+ const key = initialHref ?? styles;
19
+ let item = styleRegistry.get(key);
20
+
21
+ if (item) {
22
+ item.referenceCount++;
23
+ } else {
24
+ const minified = minifyStyles(styles);
25
+ item = {
26
+ href: sanitizeHref(initialHref ?? minified),
27
+ referenceCount: 1,
28
+ styles: minified,
29
+ };
30
+ styleRegistry.set(key, item);
31
+ }
32
+
33
+ return item;
34
+ });
35
+
36
+ useEffect(() => {
37
+ if (!styles) return;
38
+
39
+ const key = initialHref ?? styles;
40
+
41
+ if (!styleRegistry.get(key)) {
42
+ const minified = minifyStyles(styles);
43
+ const item = {
44
+ href: sanitizeHref(initialHref ?? minified),
45
+ referenceCount: 1,
46
+ styles: minified,
47
+ };
48
+ styleRegistry.set(key, item);
49
+ setStylesItem(item);
50
+ }
51
+
52
+ return () => {
53
+ const existingItem = styleRegistry.get(styles);
54
+ if (existingItem) {
55
+ existingItem.referenceCount--;
56
+ if (!existingItem.referenceCount) {
57
+ // TODO try scheduling this via setTimeout
58
+ // and add another referenceCount check
59
+ // to deal with instance where existing <Style>
60
+ // component is moved in the tree or re-keyed
61
+ styleRegistry.delete(styles);
62
+ }
63
+ }
64
+ };
65
+ }, [initialHref, styles]);
66
+
67
+ return stylesItem;
68
+ }
69
+
70
+ export default useStyles;
71
+
72
+ export const clearRegistry = () => {
73
+ styleRegistry.clear();
74
+ };
75
+
76
+ // Dashes in selectors in href prop create happy-dom / jsdom test errors:
77
+ // Invalid regular expression (“Range out of order in character class”)
78
+ function sanitizeHref(text: string) {
79
+ return text.replace(/-/g, '');
80
+ }