rn-css 1.6.5 → 1.6.6
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 +3 -28
- package/dist/types.d.ts +0 -7
- package/package.json +1 -1
- package/src/styleComponent.tsx +4 -29
- package/src/types.ts +0 -8
package/dist/styleComponent.js
CHANGED
|
@@ -11,12 +11,9 @@ const react_native_1 = require("react-native");
|
|
|
11
11
|
const convertStyle_1 = __importDefault(require("./convertStyle"));
|
|
12
12
|
const cssToRN_1 = __importDefault(require("./cssToRN"));
|
|
13
13
|
const features_1 = require("./features");
|
|
14
|
-
const generateHash_1 = __importDefault(require("./generateHash"));
|
|
15
14
|
exports.defaultUnits = { em: 16, vw: 1, vh: 1, vmin: 1, vmax: 1, rem: 16, px: 1, pt: 72 / 96, in: 96, pc: 9, cm: 96 / 2.54, mm: 96 / 25.4 };
|
|
16
15
|
exports.RemContext = react_1.default.createContext(exports.defaultUnits.rem);
|
|
17
16
|
exports.FontSizeContext = react_1.default.createContext(exports.defaultUnits.em);
|
|
18
|
-
// We use this to cache the computed styles
|
|
19
|
-
const styleMap = {};
|
|
20
17
|
// We use this to share value within the component (Theme, Translation, whatever)
|
|
21
18
|
exports.SharedValue = react_1.default.createContext(undefined);
|
|
22
19
|
function buildCSSString(chunks, functs, props, shared) {
|
|
@@ -25,20 +22,6 @@ function buildCSSString(chunks, functs, props, shared) {
|
|
|
25
22
|
computedString += props.rnCSS.replace(/=/gm, ':') + ';';
|
|
26
23
|
return computedString;
|
|
27
24
|
}
|
|
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
|
-
}
|
|
42
25
|
const styled = (Component) => {
|
|
43
26
|
const styledComponent = (chunks, ...functs) => {
|
|
44
27
|
const ForwardRefComponent = react_1.default.forwardRef((props, ref) => {
|
|
@@ -47,19 +30,11 @@ const styled = (Component) => {
|
|
|
47
30
|
// Build the css string with the context
|
|
48
31
|
const css = react_1.default.useMemo(() => buildCSSString(chunks, functs, props, shared), [props, shared]);
|
|
49
32
|
// Store the style in RN format
|
|
50
|
-
const [rnStyle, setRNStyle] = react_1.default.useState(() =>
|
|
33
|
+
const [rnStyle, setRNStyle] = react_1.default.useState(() => (0, cssToRN_1.default)(css));
|
|
51
34
|
react_1.default.useEffect(() => {
|
|
52
35
|
// Try to load an existing style from the style map or save it for next time
|
|
53
|
-
const style =
|
|
54
|
-
setRNStyle(style
|
|
55
|
-
// When the style is not used anymore, we destroy it
|
|
56
|
-
return () => {
|
|
57
|
-
setTimeout(() => {
|
|
58
|
-
style.usages--;
|
|
59
|
-
if (style.usages <= 0)
|
|
60
|
-
delete styleMap[style.hash];
|
|
61
|
-
}, 300);
|
|
62
|
-
};
|
|
36
|
+
const style = (0, cssToRN_1.default)(css);
|
|
37
|
+
setRNStyle(style);
|
|
63
38
|
}, [css]);
|
|
64
39
|
const { needsLayout, needsHover } = react_1.default.useMemo(() => ({
|
|
65
40
|
// needsFontSize: !!css.match(/\b(\d+)(\.\d+)?em\b/)
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
package/src/styleComponent.tsx
CHANGED
|
@@ -5,16 +5,12 @@ import { FlatList, FlatListProps, LayoutChangeEvent, Platform, SectionList, Sect
|
|
|
5
5
|
import convertStyle from './convertStyle'
|
|
6
6
|
import cssToStyle from './cssToRN'
|
|
7
7
|
import { useFontSize, useHover, useLayout, useScreenSize, useMediaQuery } from './features'
|
|
8
|
-
import
|
|
9
|
-
import type { Style, StyleMap, Units } from './types'
|
|
8
|
+
import type { Style, Units } from './types'
|
|
10
9
|
|
|
11
10
|
export const defaultUnits: Units = { em: 16, vw: 1, vh: 1, vmin: 1, vmax: 1, rem: 16, px: 1, pt: 72 / 96, in: 96, pc: 9, cm: 96 / 2.54, mm: 96 / 25.4 }
|
|
12
11
|
export const RemContext = React.createContext<number>(defaultUnits.rem)
|
|
13
12
|
export const FontSizeContext = React.createContext(defaultUnits.em)
|
|
14
13
|
|
|
15
|
-
// We use this to cache the computed styles
|
|
16
|
-
const styleMap: StyleMap = {}
|
|
17
|
-
|
|
18
14
|
// We use this to share value within the component (Theme, Translation, whatever)
|
|
19
15
|
export const SharedValue = React.createContext<unknown>(undefined)
|
|
20
16
|
|
|
@@ -33,20 +29,6 @@ function buildCSSString<T extends { rnCSS?: string }> (chunks: TemplateStringsAr
|
|
|
33
29
|
if (props.rnCSS) computedString += props.rnCSS.replace(/=/gm, ':') + ';'
|
|
34
30
|
return computedString
|
|
35
31
|
}
|
|
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
|
-
}
|
|
50
32
|
const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
51
33
|
const styledComponent = <S, >(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S & Props>)[]) => {
|
|
52
34
|
const ForwardRefComponent = React.forwardRef<React.ComponentType<S & Props & OptionalProps>, S & Props & OptionalProps>((props: S & Props & OptionalProps, ref) => {
|
|
@@ -55,18 +37,11 @@ const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
|
55
37
|
// Build the css string with the context
|
|
56
38
|
const css = React.useMemo(() => buildCSSString(chunks, functs, props, shared), [props, shared])
|
|
57
39
|
// Store the style in RN format
|
|
58
|
-
const [rnStyle, setRNStyle] = React.useState<Style>(() =>
|
|
40
|
+
const [rnStyle, setRNStyle] = React.useState<Style>(() => cssToStyle(css))
|
|
59
41
|
React.useEffect(() => {
|
|
60
42
|
// Try to load an existing style from the style map or save it for next time
|
|
61
|
-
const style =
|
|
62
|
-
setRNStyle(style
|
|
63
|
-
// When the style is not used anymore, we destroy it
|
|
64
|
-
return () => {
|
|
65
|
-
setTimeout(() => {
|
|
66
|
-
style.usages--
|
|
67
|
-
if (style.usages <= 0) delete styleMap[style.hash]
|
|
68
|
-
}, 300)
|
|
69
|
-
}
|
|
43
|
+
const style = cssToStyle(css)
|
|
44
|
+
setRNStyle(style)
|
|
70
45
|
}, [css])
|
|
71
46
|
|
|
72
47
|
const { needsLayout, needsHover } = React.useMemo(() => ({
|