rn-css 1.6.5 → 1.6.8
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/features.js +1 -1
- package/dist/styleComponent.js +6 -35
- package/dist/types.d.ts +0 -7
- package/package.json +1 -1
- package/src/features.tsx +1 -1
- package/src/styleComponent.tsx +7 -36
- package/src/types.ts +0 -8
package/dist/features.js
CHANGED
|
@@ -68,7 +68,7 @@ const useLayout = (onLayout, needsLayout) => {
|
|
|
68
68
|
exports.useLayout = useLayout;
|
|
69
69
|
/** Apply the new fontSize to the component before we can calculate em units */
|
|
70
70
|
const useFontSize = (fontSizeTarget, rem, em) => {
|
|
71
|
-
const [fontSize, fontUnit] = react_1.default.useMemo(() => fontSizeTarget === undefined ? [
|
|
71
|
+
const [fontSize, fontUnit] = react_1.default.useMemo(() => fontSizeTarget === undefined ? [] : (0, convertUnits_1.parseValue)(fontSizeTarget), [fontSizeTarget]);
|
|
72
72
|
const isRelative = fontUnit && ['rem', 'em', '%'].includes(fontUnit);
|
|
73
73
|
const newSize = react_1.default.useMemo(() => {
|
|
74
74
|
if (fontSize && isRelative) {
|
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,20 +30,7 @@ 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
|
|
51
|
-
react_1.default.useEffect(() => {
|
|
52
|
-
// Try to load an existing style from the style map or save it for next time
|
|
53
|
-
const style = convertCSS(css, false);
|
|
54
|
-
setRNStyle(style.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
|
-
};
|
|
63
|
-
}, [css]);
|
|
33
|
+
const rnStyle = react_1.default.useMemo(() => (0, cssToRN_1.default)(css), [css]);
|
|
64
34
|
const { needsLayout, needsHover } = react_1.default.useMemo(() => ({
|
|
65
35
|
// needsFontSize: !!css.match(/\b(\d+)(\.\d+)?em\b/)
|
|
66
36
|
// needsScreenSize: !!cssString.current.match(/\b(\d+)(\.\d+)?v([hw]|min|max)\b/) || !!rnStyle.media,
|
|
@@ -108,17 +78,18 @@ const styled = (Component) => {
|
|
|
108
78
|
}, [em, mediaQuery, tempStyle]);
|
|
109
79
|
const units = react_1.default.useMemo(() => ({ ...baseUnits, em }), [baseUnits, em]);
|
|
110
80
|
const styleConvertedFromCSS = react_1.default.useMemo(() => (0, convertStyle_1.default)(finalStyle, units), [finalStyle, units]);
|
|
111
|
-
const style = react_1.default.useMemo(() =>
|
|
81
|
+
const style = react_1.default.useMemo(() => [styleConvertedFromCSS, props.style], [props.style, styleConvertedFromCSS]);
|
|
82
|
+
const flatStyle = react_1.default.useMemo(() => react_native_1.StyleSheet.flatten(style), [style]);
|
|
112
83
|
const newProps = react_1.default.useMemo(() => {
|
|
113
84
|
const newProps = { style, onMouseEnter, onMouseLeave, onLayout };
|
|
114
|
-
if (
|
|
85
|
+
if (flatStyle.textOverflow === 'ellipsis')
|
|
115
86
|
Object.assign(newProps, { numberOfLines: 1 });
|
|
116
87
|
return newProps;
|
|
117
|
-
}, [onLayout, onMouseEnter, onMouseLeave, style]);
|
|
88
|
+
}, [flatStyle.textOverflow, onLayout, onMouseEnter, onMouseLeave, style]);
|
|
118
89
|
// The lines below can improve perfs, but it causes the component to remount when the font size changes
|
|
119
90
|
// const currentFontSize = React.useContext(FontSizeContext)
|
|
120
91
|
// if (em !== currentFontSize) {
|
|
121
|
-
if (
|
|
92
|
+
if (flatStyle.fontSize) {
|
|
122
93
|
return react_1.default.createElement(exports.FontSizeContext.Provider, { value: em },
|
|
123
94
|
react_1.default.createElement(Component, { ref: ref, ...props, ...newProps }));
|
|
124
95
|
}
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
package/src/features.tsx
CHANGED
|
@@ -59,7 +59,7 @@ export const useLayout = (onLayout: undefined | ((event: LayoutChangeEvent) => v
|
|
|
59
59
|
|
|
60
60
|
/** Apply the new fontSize to the component before we can calculate em units */
|
|
61
61
|
export const useFontSize = (fontSizeTarget: string | undefined, rem: number, em: number): { em: number } => {
|
|
62
|
-
const [fontSize, fontUnit] = React.useMemo(() => fontSizeTarget === undefined ? [
|
|
62
|
+
const [fontSize, fontUnit] = React.useMemo(() => fontSizeTarget === undefined ? [] : parseValue(fontSizeTarget), [fontSizeTarget])
|
|
63
63
|
const isRelative = fontUnit && ['rem', 'em', '%'].includes(fontUnit)
|
|
64
64
|
const newSize = React.useMemo(() => {
|
|
65
65
|
if (fontSize && isRelative) {
|
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,19 +37,7 @@ 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
|
|
59
|
-
React.useEffect(() => {
|
|
60
|
-
// Try to load an existing style from the style map or save it for next time
|
|
61
|
-
const style = convertCSS(css, false)
|
|
62
|
-
setRNStyle(style.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
|
-
}
|
|
70
|
-
}, [css])
|
|
40
|
+
const rnStyle = React.useMemo(() => cssToStyle(css), [css])
|
|
71
41
|
|
|
72
42
|
const { needsLayout, needsHover } = React.useMemo(() => ({
|
|
73
43
|
// needsFontSize: !!css.match(/\b(\d+)(\.\d+)?em\b/)
|
|
@@ -120,17 +90,18 @@ const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
|
120
90
|
const units = React.useMemo<Units>(() => ({ ...baseUnits, em }), [baseUnits, em])
|
|
121
91
|
|
|
122
92
|
const styleConvertedFromCSS = React.useMemo(() => convertStyle(finalStyle, units), [finalStyle, units])
|
|
123
|
-
const style: StyleProp<any> = React.useMemo(() =>
|
|
93
|
+
const style: StyleProp<any> = React.useMemo(() => [styleConvertedFromCSS, props.style], [props.style, styleConvertedFromCSS])
|
|
94
|
+
const flatStyle = React.useMemo(() => StyleSheet.flatten(style), [style])
|
|
124
95
|
const newProps = React.useMemo(() => {
|
|
125
96
|
const newProps: OptionalProps = { style, onMouseEnter, onMouseLeave, onLayout }
|
|
126
|
-
if (
|
|
97
|
+
if (flatStyle.textOverflow === 'ellipsis') Object.assign(newProps, { numberOfLines: 1 })
|
|
127
98
|
return newProps
|
|
128
|
-
}, [onLayout, onMouseEnter, onMouseLeave, style])
|
|
99
|
+
}, [flatStyle.textOverflow, onLayout, onMouseEnter, onMouseLeave, style])
|
|
129
100
|
|
|
130
101
|
// The lines below can improve perfs, but it causes the component to remount when the font size changes
|
|
131
102
|
// const currentFontSize = React.useContext(FontSizeContext)
|
|
132
103
|
// if (em !== currentFontSize) {
|
|
133
|
-
if (
|
|
104
|
+
if (flatStyle.fontSize) {
|
|
134
105
|
return <FontSizeContext.Provider value={em}>
|
|
135
106
|
<Component ref={ref} {...props} {...newProps} />
|
|
136
107
|
</FontSizeContext.Provider>
|