rn-css 1.6.2 → 1.6.3
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/styleComponent.js +12 -3
- package/package.json +1 -1
- package/src/styleComponent.tsx +11 -2
package/dist/styleComponent.js
CHANGED
|
@@ -25,6 +25,13 @@ function buildCSSString(chunks, functs, props, shared) {
|
|
|
25
25
|
computedString += props.rnCSS.replace(/=/gm, ':') + ';';
|
|
26
26
|
return computedString;
|
|
27
27
|
}
|
|
28
|
+
function initFromCSS(css) {
|
|
29
|
+
const hash = (0, generateHash_1.default)(css);
|
|
30
|
+
const rns = (0, cssToRN_1.default)(css);
|
|
31
|
+
(0, cssToRN_1.default)(css);
|
|
32
|
+
styleMap[hash] = { style: rns, usages: 0 };
|
|
33
|
+
return rns;
|
|
34
|
+
}
|
|
28
35
|
const styled = (Component) => {
|
|
29
36
|
const styledComponent = (chunks, ...functs) => {
|
|
30
37
|
const ForwardRefComponent = react_1.default.forwardRef((props, ref) => {
|
|
@@ -33,7 +40,7 @@ const styled = (Component) => {
|
|
|
33
40
|
// Build the css string with the context
|
|
34
41
|
const css = react_1.default.useMemo(() => buildCSSString(chunks, functs, props, shared), [props, shared]);
|
|
35
42
|
// Store the style in RN format
|
|
36
|
-
const [rnStyle, setRNStyle] = react_1.default.useState(
|
|
43
|
+
const [rnStyle, setRNStyle] = react_1.default.useState(() => initFromCSS(css));
|
|
37
44
|
react_1.default.useEffect(() => {
|
|
38
45
|
// Try to load an existing style from the style map or save it for next time
|
|
39
46
|
const hash = (0, generateHash_1.default)(css);
|
|
@@ -51,8 +58,10 @@ const styled = (Component) => {
|
|
|
51
58
|
return () => {
|
|
52
59
|
const style = styleMap[hash];
|
|
53
60
|
style.usages--;
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
setTimeout(() => {
|
|
62
|
+
if (style.usages <= 0)
|
|
63
|
+
delete styleMap[hash];
|
|
64
|
+
}, 3000);
|
|
56
65
|
};
|
|
57
66
|
}, [css]);
|
|
58
67
|
const { needsLayout, needsHover } = react_1.default.useMemo(() => ({
|
package/package.json
CHANGED
package/src/styleComponent.tsx
CHANGED
|
@@ -33,6 +33,13 @@ function buildCSSString<T extends { rnCSS?: string }> (chunks: TemplateStringsAr
|
|
|
33
33
|
if (props.rnCSS) computedString += props.rnCSS.replace(/=/gm, ':') + ';'
|
|
34
34
|
return computedString
|
|
35
35
|
}
|
|
36
|
+
function initFromCSS (css: string) {
|
|
37
|
+
const hash = calculHash(css)
|
|
38
|
+
const rns = cssToStyle(css)
|
|
39
|
+
cssToStyle(css)
|
|
40
|
+
styleMap[hash] = { style: rns, usages: 0 }
|
|
41
|
+
return rns
|
|
42
|
+
}
|
|
36
43
|
const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
37
44
|
const styledComponent = <S, >(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S & Props>)[]) => {
|
|
38
45
|
const ForwardRefComponent = React.forwardRef<React.ComponentType<S & Props & OptionalProps>, S & Props & OptionalProps>((props: S & Props & OptionalProps, ref) => {
|
|
@@ -41,7 +48,7 @@ const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
|
41
48
|
// Build the css string with the context
|
|
42
49
|
const css = React.useMemo(() => buildCSSString(chunks, functs, props, shared), [props, shared])
|
|
43
50
|
// Store the style in RN format
|
|
44
|
-
const [rnStyle, setRNStyle] = React.useState<Style>(
|
|
51
|
+
const [rnStyle, setRNStyle] = React.useState<Style>(() => initFromCSS(css))
|
|
45
52
|
React.useEffect(() => {
|
|
46
53
|
// Try to load an existing style from the style map or save it for next time
|
|
47
54
|
const hash = calculHash(css)
|
|
@@ -59,7 +66,9 @@ const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
|
59
66
|
return () => {
|
|
60
67
|
const style = styleMap[hash]
|
|
61
68
|
style.usages--
|
|
62
|
-
|
|
69
|
+
setTimeout(() => {
|
|
70
|
+
if (style.usages <= 0) delete styleMap[hash]
|
|
71
|
+
}, 3000)
|
|
63
72
|
}
|
|
64
73
|
}, [css])
|
|
65
74
|
|