rn-css 1.6.4 → 1.6.7
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 -36
- package/dist/types.d.ts +0 -6
- package/package.json +1 -1
- package/src/styleComponent.tsx +4 -37
- package/src/types.ts +0 -7
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,13 +22,6 @@ function buildCSSString(chunks, functs, props, shared) {
|
|
|
25
22
|
computedString += props.rnCSS.replace(/=/gm, ':') + ';';
|
|
26
23
|
return computedString;
|
|
27
24
|
}
|
|
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
|
-
}
|
|
35
25
|
const styled = (Component) => {
|
|
36
26
|
const styledComponent = (chunks, ...functs) => {
|
|
37
27
|
const ForwardRefComponent = react_1.default.forwardRef((props, ref) => {
|
|
@@ -40,30 +30,7 @@ const styled = (Component) => {
|
|
|
40
30
|
// Build the css string with the context
|
|
41
31
|
const css = react_1.default.useMemo(() => buildCSSString(chunks, functs, props, shared), [props, shared]);
|
|
42
32
|
// Store the style in RN format
|
|
43
|
-
const
|
|
44
|
-
react_1.default.useEffect(() => {
|
|
45
|
-
// Try to load an existing style from the style map or save it for next time
|
|
46
|
-
const hash = (0, generateHash_1.default)(css);
|
|
47
|
-
const style = styleMap[hash];
|
|
48
|
-
if (style) {
|
|
49
|
-
setRNStyle(style.style);
|
|
50
|
-
style.usages++;
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
const rns = (0, cssToRN_1.default)(css);
|
|
54
|
-
setRNStyle(rns);
|
|
55
|
-
styleMap[hash] = { style: rns, usages: 1 };
|
|
56
|
-
}
|
|
57
|
-
// When the style is not used anymore, we destroy it
|
|
58
|
-
return () => {
|
|
59
|
-
const style = styleMap[hash];
|
|
60
|
-
setTimeout(() => {
|
|
61
|
-
style.usages--;
|
|
62
|
-
if (style.usages <= 0)
|
|
63
|
-
delete styleMap[hash];
|
|
64
|
-
}, 3000);
|
|
65
|
-
};
|
|
66
|
-
}, [css]);
|
|
33
|
+
const rnStyle = react_1.default.useMemo(() => (0, cssToRN_1.default)(css), [css]);
|
|
67
34
|
const { needsLayout, needsHover } = react_1.default.useMemo(() => ({
|
|
68
35
|
// needsFontSize: !!css.match(/\b(\d+)(\.\d+)?em\b/)
|
|
69
36
|
// needsScreenSize: !!cssString.current.match(/\b(\d+)(\.\d+)?v([hw]|min|max)\b/) || !!rnStyle.media,
|
|
@@ -111,10 +78,10 @@ const styled = (Component) => {
|
|
|
111
78
|
}, [em, mediaQuery, tempStyle]);
|
|
112
79
|
const units = react_1.default.useMemo(() => ({ ...baseUnits, em }), [baseUnits, em]);
|
|
113
80
|
const styleConvertedFromCSS = react_1.default.useMemo(() => (0, convertStyle_1.default)(finalStyle, units), [finalStyle, units]);
|
|
114
|
-
const style = react_1.default.useMemo(() =>
|
|
81
|
+
const style = react_1.default.useMemo(() => [styleConvertedFromCSS, props.style], [props.style, styleConvertedFromCSS]);
|
|
115
82
|
const newProps = react_1.default.useMemo(() => {
|
|
116
83
|
const newProps = { style, onMouseEnter, onMouseLeave, onLayout };
|
|
117
|
-
if (style.textOverflow === 'ellipsis')
|
|
84
|
+
if (react_native_1.StyleSheet.flatten(style).textOverflow === 'ellipsis')
|
|
118
85
|
Object.assign(newProps, { numberOfLines: 1 });
|
|
119
86
|
return newProps;
|
|
120
87
|
}, [onLayout, onMouseEnter, onMouseLeave, style]);
|
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,13 +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 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
|
-
}
|
|
43
32
|
const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
44
33
|
const styledComponent = <S, >(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S & Props>)[]) => {
|
|
45
34
|
const ForwardRefComponent = React.forwardRef<React.ComponentType<S & Props & OptionalProps>, S & Props & OptionalProps>((props: S & Props & OptionalProps, ref) => {
|
|
@@ -48,29 +37,7 @@ const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
|
48
37
|
// Build the css string with the context
|
|
49
38
|
const css = React.useMemo(() => buildCSSString(chunks, functs, props, shared), [props, shared])
|
|
50
39
|
// Store the style in RN format
|
|
51
|
-
const
|
|
52
|
-
React.useEffect(() => {
|
|
53
|
-
// Try to load an existing style from the style map or save it for next time
|
|
54
|
-
const hash = calculHash(css)
|
|
55
|
-
const style = styleMap[hash]
|
|
56
|
-
if (style) {
|
|
57
|
-
setRNStyle(style.style)
|
|
58
|
-
style.usages++
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
const rns = cssToStyle(css)
|
|
62
|
-
setRNStyle(rns)
|
|
63
|
-
styleMap[hash] = { style: rns, usages: 1 }
|
|
64
|
-
}
|
|
65
|
-
// When the style is not used anymore, we destroy it
|
|
66
|
-
return () => {
|
|
67
|
-
const style = styleMap[hash]
|
|
68
|
-
setTimeout(() => {
|
|
69
|
-
style.usages--
|
|
70
|
-
if (style.usages <= 0) delete styleMap[hash]
|
|
71
|
-
}, 3000)
|
|
72
|
-
}
|
|
73
|
-
}, [css])
|
|
40
|
+
const rnStyle = React.useMemo(() => cssToStyle(css), [css])
|
|
74
41
|
|
|
75
42
|
const { needsLayout, needsHover } = React.useMemo(() => ({
|
|
76
43
|
// needsFontSize: !!css.match(/\b(\d+)(\.\d+)?em\b/)
|
|
@@ -123,10 +90,10 @@ const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
|
123
90
|
const units = React.useMemo<Units>(() => ({ ...baseUnits, em }), [baseUnits, em])
|
|
124
91
|
|
|
125
92
|
const styleConvertedFromCSS = React.useMemo(() => convertStyle(finalStyle, units), [finalStyle, units])
|
|
126
|
-
const style: StyleProp<any> = React.useMemo(() =>
|
|
93
|
+
const style: StyleProp<any> = React.useMemo(() => [styleConvertedFromCSS, props.style], [props.style, styleConvertedFromCSS])
|
|
127
94
|
const newProps = React.useMemo(() => {
|
|
128
95
|
const newProps: OptionalProps = { style, onMouseEnter, onMouseLeave, onLayout }
|
|
129
|
-
if (style.textOverflow === 'ellipsis') Object.assign(newProps, { numberOfLines: 1 })
|
|
96
|
+
if (StyleSheet.flatten(style).textOverflow === 'ellipsis') Object.assign(newProps, { numberOfLines: 1 })
|
|
130
97
|
return newProps
|
|
131
98
|
}, [onLayout, onMouseEnter, onMouseLeave, style])
|
|
132
99
|
|