rn-css 1.6.2 → 1.6.5

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.
@@ -25,6 +25,20 @@ function buildCSSString(chunks, functs, props, shared) {
25
25
  computedString += props.rnCSS.replace(/=/gm, ':') + ';';
26
26
  return computedString;
27
27
  }
28
+ function convertCSS(css, init) {
29
+ (0, cssToRN_1.default)(css);
30
+ const hash = (0, generateHash_1.default)(css);
31
+ const style = styleMap[hash];
32
+ if (style) {
33
+ style.usages++;
34
+ return style;
35
+ }
36
+ else {
37
+ const rns = (0, cssToRN_1.default)(css);
38
+ styleMap[hash] = { style: rns, usages: init ? 0 : 1, hash };
39
+ return styleMap[hash];
40
+ }
41
+ }
28
42
  const styled = (Component) => {
29
43
  const styledComponent = (chunks, ...functs) => {
30
44
  const ForwardRefComponent = react_1.default.forwardRef((props, ref) => {
@@ -33,26 +47,18 @@ const styled = (Component) => {
33
47
  // Build the css string with the context
34
48
  const css = react_1.default.useMemo(() => buildCSSString(chunks, functs, props, shared), [props, shared]);
35
49
  // Store the style in RN format
36
- const [rnStyle, setRNStyle] = react_1.default.useState({});
50
+ const [rnStyle, setRNStyle] = react_1.default.useState(() => convertCSS(css, true).style);
37
51
  react_1.default.useEffect(() => {
38
52
  // Try to load an existing style from the style map or save it for next time
39
- const hash = (0, generateHash_1.default)(css);
40
- const style = styleMap[hash];
41
- if (style) {
42
- setRNStyle(style.style);
43
- style.usages++;
44
- }
45
- else {
46
- const rns = (0, cssToRN_1.default)(css);
47
- setRNStyle(rns);
48
- styleMap[hash] = { style: rns, usages: 1 };
49
- }
53
+ const style = convertCSS(css, false);
54
+ setRNStyle(style.style);
50
55
  // When the style is not used anymore, we destroy it
51
56
  return () => {
52
- const style = styleMap[hash];
53
- style.usages--;
54
- if (style.usages <= 0)
55
- delete styleMap[hash];
57
+ setTimeout(() => {
58
+ style.usages--;
59
+ if (style.usages <= 0)
60
+ delete styleMap[style.hash];
61
+ }, 300);
56
62
  };
57
63
  }, [css]);
58
64
  const { needsLayout, needsHover } = react_1.default.useMemo(() => ({
package/dist/types.d.ts CHANGED
@@ -83,5 +83,6 @@ export declare type StyleMap = {
83
83
  [key: string]: {
84
84
  usages: number;
85
85
  style: Style;
86
+ hash: string;
86
87
  };
87
88
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rn-css",
3
- "version": "1.6.2",
3
+ "version": "1.6.5",
4
4
  "scripts": {
5
5
  "test": "jest",
6
6
  "prepare": "tsc",
@@ -33,6 +33,20 @@ 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 convertCSS (css: string, init?: boolean) {
37
+ cssToStyle(css)
38
+ const hash = calculHash(css)
39
+ const style = styleMap[hash]
40
+ if (style) {
41
+ style.usages++
42
+ return style
43
+ }
44
+ else {
45
+ const rns = cssToStyle(css)
46
+ styleMap[hash] = { style: rns, usages: init ? 0 : 1, hash }
47
+ return styleMap[hash]
48
+ }
49
+ }
36
50
  const styled = <Props, >(Component: React.ComponentType<Props>) => {
37
51
  const styledComponent = <S, >(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S & Props>)[]) => {
38
52
  const ForwardRefComponent = React.forwardRef<React.ComponentType<S & Props & OptionalProps>, S & Props & OptionalProps>((props: S & Props & OptionalProps, ref) => {
@@ -41,25 +55,17 @@ const styled = <Props, >(Component: React.ComponentType<Props>) => {
41
55
  // Build the css string with the context
42
56
  const css = React.useMemo(() => buildCSSString(chunks, functs, props, shared), [props, shared])
43
57
  // Store the style in RN format
44
- const [rnStyle, setRNStyle] = React.useState<Style>({})
58
+ const [rnStyle, setRNStyle] = React.useState<Style>(() => convertCSS(css, true).style)
45
59
  React.useEffect(() => {
46
60
  // Try to load an existing style from the style map or save it for next time
47
- const hash = calculHash(css)
48
- const style = styleMap[hash]
49
- if (style) {
50
- setRNStyle(style.style)
51
- style.usages++
52
- }
53
- else {
54
- const rns = cssToStyle(css)
55
- setRNStyle(rns)
56
- styleMap[hash] = { style: rns, usages: 1 }
57
- }
61
+ const style = convertCSS(css, false)
62
+ setRNStyle(style.style)
58
63
  // When the style is not used anymore, we destroy it
59
64
  return () => {
60
- const style = styleMap[hash]
61
- style.usages--
62
- if (style.usages <= 0) delete styleMap[hash]
65
+ setTimeout(() => {
66
+ style.usages--
67
+ if (style.usages <= 0) delete styleMap[style.hash]
68
+ }, 300)
63
69
  }
64
70
  }, [css])
65
71
 
package/src/types.ts CHANGED
@@ -90,5 +90,6 @@ export type StyleMap = {
90
90
  [key: string]: {
91
91
  usages: number;
92
92
  style: Style;
93
+ hash: string;
93
94
  };
94
95
  }