rn-css 1.4.4 → 1.5.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/README.md +10 -0
- package/dist/convertStyle.js +7 -7
- package/dist/convertUnits.js +5 -3
- package/dist/cssToRN/index.js +13 -13
- package/dist/cssToRN/mediaQueries.js +2 -2
- package/dist/features.js +3 -3
- package/dist/index.d.ts +129 -216
- package/dist/index.js +5 -6
- package/dist/styleComponent.d.ts +1 -0
- package/dist/styleComponent.js +12 -12
- package/dist/useTheme.d.ts +17 -0
- package/dist/useTheme.js +31 -0
- package/package.json +33 -33
- package/src/convertUnits.ts +1 -0
- package/src/index.tsx +1 -5
- package/src/styleComponent.tsx +2 -2
- package/src/useTheme.tsx +27 -0
package/dist/index.js
CHANGED
|
@@ -18,6 +18,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
18
18
|
__setModuleDefault(result, mod);
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
22
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
23
|
+
};
|
|
21
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
25
|
exports.SharedValue = exports.FontSizeContext = exports.cssToRNStyle = void 0;
|
|
23
26
|
const RN = __importStar(require("react-native"));
|
|
@@ -28,14 +31,14 @@ var features_1 = require("./features");
|
|
|
28
31
|
Object.defineProperty(exports, "FontSizeContext", { enumerable: true, get: function () { return features_1.FontSizeContext; } });
|
|
29
32
|
var styleComponent_2 = require("./styleComponent");
|
|
30
33
|
Object.defineProperty(exports, "SharedValue", { enumerable: true, get: function () { return styleComponent_2.SharedValue; } });
|
|
31
|
-
|
|
34
|
+
__exportStar(require("./useTheme"), exports);
|
|
35
|
+
const styled = (Component) => (0, styleComponent_1.default)(Component);
|
|
32
36
|
styled.ActivityIndicator = styled(RN.ActivityIndicator);
|
|
33
37
|
styled.Button = styled(RN.Button);
|
|
34
38
|
styled.DrawerLayoutAndroid = styled(RN.DrawerLayoutAndroid);
|
|
35
39
|
styled.Image = styled(RN.Image);
|
|
36
40
|
styled.ImageBackground = styled(RN.ImageBackground);
|
|
37
41
|
styled.KeyboardAvoidingView = styled(RN.KeyboardAvoidingView);
|
|
38
|
-
styled.ListView = styled(RN.ListView);
|
|
39
42
|
styled.Modal = styled(RN.Modal);
|
|
40
43
|
styled.NavigatorIOS = styled(RN.NavigatorIOS);
|
|
41
44
|
styled.ScrollView = styled(RN.ScrollView);
|
|
@@ -45,17 +48,13 @@ styled.RecyclerViewBackedScrollView = styled(RN.RecyclerViewBackedScrollView);
|
|
|
45
48
|
styled.RefreshControl = styled(RN.RefreshControl);
|
|
46
49
|
styled.SafeAreaView = styled(RN.SafeAreaView);
|
|
47
50
|
styled.StatusBar = styled(RN.StatusBar);
|
|
48
|
-
styled.SwipeableListView = styled(RN.SwipeableListView);
|
|
49
|
-
styled.TabBarIOS = styled(RN.TabBarIOS);
|
|
50
51
|
styled.Text = styled(RN.Text);
|
|
51
52
|
styled.TextInput = styled(RN.TextInput);
|
|
52
|
-
styled.ToolbarAndroid = styled(RN.ToolbarAndroid);
|
|
53
53
|
styled.TouchableHighlight = styled(RN.TouchableHighlight);
|
|
54
54
|
styled.TouchableNativeFeedback = styled(RN.TouchableNativeFeedback);
|
|
55
55
|
styled.TouchableOpacity = styled(RN.TouchableOpacity);
|
|
56
56
|
styled.TouchableWithoutFeedback = styled(RN.TouchableWithoutFeedback);
|
|
57
57
|
styled.View = styled(RN.View);
|
|
58
|
-
styled.ViewPagerAndroid = styled(RN.ViewPagerAndroid);
|
|
59
58
|
styled.FlatList = styleComponent_1.styledFlatList;
|
|
60
59
|
styled.SectionList = styleComponent_1.styledSectionList;
|
|
61
60
|
styled.VirtualizedList = styleComponent_1.styledVirtualizedList;
|
package/dist/styleComponent.d.ts
CHANGED
package/dist/styleComponent.js
CHANGED
|
@@ -17,7 +17,7 @@ const styleMap = {};
|
|
|
17
17
|
// We use this to share value within the component (Theme, Translation, whatever)
|
|
18
18
|
exports.SharedValue = react_1.default.createContext(undefined);
|
|
19
19
|
function buildCSSString(chunks, functs, props, shared) {
|
|
20
|
-
let computedString = chunks.map((chunk, i) => ([chunk, (functs[i] instanceof Function) ? functs[i]({
|
|
20
|
+
let computedString = chunks.map((chunk, i) => ([chunk, (functs[i] instanceof Function) ? functs[i]({ shared, theme: shared, ...props }) : functs[i]])).flat().join('');
|
|
21
21
|
if (props.rnCSS)
|
|
22
22
|
computedString += props.rnCSS.replace(/=/gm, ':') + ';';
|
|
23
23
|
return computedString;
|
|
@@ -29,20 +29,20 @@ const styled = (Component) => {
|
|
|
29
29
|
const shared = react_1.default.useContext(exports.SharedValue);
|
|
30
30
|
// Store the style for mutualization
|
|
31
31
|
const cssString = react_1.default.useRef(buildCSSString(chunks, functs, props, shared));
|
|
32
|
-
const [rnStyle, setRNStyle] = react_1.default.useState(cssToRN_1.default(cssString.current));
|
|
32
|
+
const [rnStyle, setRNStyle] = react_1.default.useState((0, cssToRN_1.default)(cssString.current));
|
|
33
33
|
react_1.default.useEffect(() => {
|
|
34
34
|
// Build the css string with the context
|
|
35
35
|
const css = buildCSSString(chunks, functs, props, shared);
|
|
36
36
|
cssString.current = css;
|
|
37
37
|
// Try to load an existing style from the style map or save it for next time
|
|
38
|
-
const hash = generateHash_1.default(css);
|
|
38
|
+
const hash = (0, generateHash_1.default)(css);
|
|
39
39
|
const style = styleMap[hash];
|
|
40
40
|
if (style) {
|
|
41
41
|
setRNStyle(style.style);
|
|
42
42
|
style.usages++;
|
|
43
43
|
}
|
|
44
44
|
else {
|
|
45
|
-
const rns = cssToRN_1.default(css);
|
|
45
|
+
const rns = (0, cssToRN_1.default)(css);
|
|
46
46
|
setRNStyle(rns);
|
|
47
47
|
styleMap[hash] = { style: rns, usages: 1 };
|
|
48
48
|
}
|
|
@@ -70,29 +70,29 @@ const styled = (Component) => {
|
|
|
70
70
|
delete finalStyle.hover;
|
|
71
71
|
// Read all the data we might need
|
|
72
72
|
// Handle hover
|
|
73
|
-
const { onMouseEnter, onMouseLeave, style: hoverStyle } = features_1.useHover(rnStyle, props.onMouseEnter, props.onMouseLeave);
|
|
73
|
+
const { onMouseEnter, onMouseLeave, style: hoverStyle } = (0, features_1.useHover)(rnStyle, props.onMouseEnter, props.onMouseLeave);
|
|
74
74
|
if (hoverStyle)
|
|
75
75
|
Object.assign(finalStyle, hoverStyle);
|
|
76
76
|
// Calculate current em unit for media-queries
|
|
77
|
-
const { em: tempEm } = features_1.useFontSize(finalStyle.fontSize, units.current.rem);
|
|
77
|
+
const { em: tempEm } = (0, features_1.useFontSize)(finalStyle.fontSize, units.current.rem);
|
|
78
78
|
if (units.current.em !== tempEm)
|
|
79
79
|
units.current = { ...units.current, em: tempEm };
|
|
80
80
|
// Handle layout data needed for % units
|
|
81
|
-
const { width, height, onLayout } = features_1.useLayout(props.onLayout);
|
|
81
|
+
const { width, height, onLayout } = (0, features_1.useLayout)(props.onLayout);
|
|
82
82
|
if (needsLayout && (units.current.width !== width || units.current.height !== height)) {
|
|
83
83
|
units.current = { ...units.current, width, height };
|
|
84
84
|
}
|
|
85
85
|
// Handle screen size needed for vw and wh units
|
|
86
|
-
const screenUnits = features_1.useScreenSize();
|
|
86
|
+
const screenUnits = (0, features_1.useScreenSize)();
|
|
87
87
|
if ( /* needsScreenSize && */Object.keys(screenUnits).find(key => units.current[key] !== screenUnits[key])) {
|
|
88
88
|
units.current = { ...units.current, ...screenUnits };
|
|
89
89
|
}
|
|
90
90
|
// apply media queries
|
|
91
|
-
const mediaQuery = features_1.useMediaQuery(rnStyle.media, units.current);
|
|
91
|
+
const mediaQuery = (0, features_1.useMediaQuery)(rnStyle.media, units.current);
|
|
92
92
|
if (mediaQuery)
|
|
93
93
|
Object.assign(finalStyle, mediaQuery);
|
|
94
94
|
// Handle em units
|
|
95
|
-
const { em } = features_1.useFontSize(finalStyle.fontSize, units.current.rem);
|
|
95
|
+
const { em } = (0, features_1.useFontSize)(finalStyle.fontSize, units.current.rem);
|
|
96
96
|
if (units.current.em !== em)
|
|
97
97
|
units.current = { ...units.current, em };
|
|
98
98
|
if (finalStyle.fontSize)
|
|
@@ -102,8 +102,8 @@ const styled = (Component) => {
|
|
|
102
102
|
if (Object.keys(finalStyle).length !== Object.keys(calculatedStyle.current).length || Object.keys(finalStyle).find(key => calculatedStyle.current[key] !== finalStyle[key])) {
|
|
103
103
|
calculatedStyle.current = finalStyle;
|
|
104
104
|
}
|
|
105
|
-
const styleConvertedFromCSS = react_1.default.useMemo(() => convertStyle_1.default(calculatedStyle.current, units.current), [calculatedStyle.current, units.current]);
|
|
106
|
-
const zIndex = features_1.useZIndex(react_native_1.StyleSheet.flatten([props.style, styleConvertedFromCSS]).zIndex);
|
|
105
|
+
const styleConvertedFromCSS = react_1.default.useMemo(() => (0, convertStyle_1.default)(calculatedStyle.current, units.current), [calculatedStyle.current, units.current]);
|
|
106
|
+
const zIndex = (0, features_1.useZIndex)(react_native_1.StyleSheet.flatten([props.style, styleConvertedFromCSS]).zIndex);
|
|
107
107
|
const style = react_1.default.useMemo(() => {
|
|
108
108
|
const style = [];
|
|
109
109
|
style.push(styleConvertedFromCSS);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/** Provider for the theme. */
|
|
3
|
+
export declare const ThemeProvider: ({ theme, children }: {
|
|
4
|
+
theme: unknown;
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
}) => JSX.Element;
|
|
7
|
+
/**
|
|
8
|
+
* Returns the Theme
|
|
9
|
+
* @returns The Theme object
|
|
10
|
+
*/
|
|
11
|
+
export declare const useTheme: () => unknown;
|
|
12
|
+
/**
|
|
13
|
+
* Adds the theme prop to a non rn-css component
|
|
14
|
+
* @param Component A non rn-css component
|
|
15
|
+
* @returns A component with the theme prop
|
|
16
|
+
*/
|
|
17
|
+
export declare const withTheme: <T>(Component: React.ComponentType<T>) => React.ForwardRefExoticComponent<React.PropsWithoutRef<T> & React.RefAttributes<React.Component<{}, {}, any>>>;
|
package/dist/useTheme.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.withTheme = exports.useTheme = exports.ThemeProvider = void 0;
|
|
7
|
+
const react_1 = __importDefault(require("react"));
|
|
8
|
+
const styleComponent_1 = require("./styleComponent");
|
|
9
|
+
/** Provider for the theme. */
|
|
10
|
+
const ThemeProvider = ({ theme, children }) => {
|
|
11
|
+
return react_1.default.createElement(styleComponent_1.SharedValue.Provider, { value: theme }, children);
|
|
12
|
+
};
|
|
13
|
+
exports.ThemeProvider = ThemeProvider;
|
|
14
|
+
/**
|
|
15
|
+
* Returns the Theme
|
|
16
|
+
* @returns The Theme object
|
|
17
|
+
*/
|
|
18
|
+
const useTheme = () => react_1.default.useContext(styleComponent_1.SharedValue);
|
|
19
|
+
exports.useTheme = useTheme;
|
|
20
|
+
/**
|
|
21
|
+
* Adds the theme prop to a non rn-css component
|
|
22
|
+
* @param Component A non rn-css component
|
|
23
|
+
* @returns A component with the theme prop
|
|
24
|
+
*/
|
|
25
|
+
const withTheme = (Component) => {
|
|
26
|
+
const theme = (0, exports.useTheme)();
|
|
27
|
+
const ThemedComponent = react_1.default.forwardRef((props, ref) => react_1.default.createElement(Component, { ref: ref, theme: theme, ...props }));
|
|
28
|
+
ThemedComponent.displayName = 'ThemedComponent';
|
|
29
|
+
return ThemedComponent;
|
|
30
|
+
};
|
|
31
|
+
exports.withTheme = withTheme;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rn-css",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.3",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"test": "jest",
|
|
6
6
|
"prepare": "tsc",
|
|
@@ -17,44 +17,44 @@
|
|
|
17
17
|
"react-native": ">=0.62.2"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@babel/core": "^7.
|
|
21
|
-
"@babel/runtime": "^7.
|
|
22
|
-
"@react-native-community/eslint-config": "^
|
|
23
|
-
"@types/jest": "^
|
|
24
|
-
"@types/react": "^17.0.
|
|
25
|
-
"@types/react-native": "^0.
|
|
26
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
27
|
-
"@typescript-eslint/parser": "^
|
|
28
|
-
"babel-jest": "^27.
|
|
29
|
-
"babel-loader": "^8.2.
|
|
30
|
-
"dotenv": "^
|
|
31
|
-
"eslint": "^7.
|
|
32
|
-
"eslint-config-standard": "^16.0.
|
|
33
|
-
"eslint-plugin-import": "^2.
|
|
34
|
-
"eslint-plugin-jest": "^
|
|
20
|
+
"@babel/core": "^7.16.5",
|
|
21
|
+
"@babel/runtime": "^7.16.5",
|
|
22
|
+
"@react-native-community/eslint-config": "^3.0.1",
|
|
23
|
+
"@types/jest": "^27.0.3",
|
|
24
|
+
"@types/react": "^17.0.37",
|
|
25
|
+
"@types/react-native": "^0.66.9",
|
|
26
|
+
"@typescript-eslint/eslint-plugin": "^5.7.0",
|
|
27
|
+
"@typescript-eslint/parser": "^5.7.0",
|
|
28
|
+
"babel-jest": "^27.4.5",
|
|
29
|
+
"babel-loader": "^8.2.3",
|
|
30
|
+
"dotenv": "^10.0.0",
|
|
31
|
+
"eslint": "^7.32.0",
|
|
32
|
+
"eslint-config-standard": "^16.0.3",
|
|
33
|
+
"eslint-plugin-import": "^2.25.3",
|
|
34
|
+
"eslint-plugin-jest": "^25.3.0",
|
|
35
35
|
"eslint-plugin-node": "^11.1.0",
|
|
36
|
-
"eslint-plugin-promise": "^
|
|
37
|
-
"eslint-plugin-react": "^7.
|
|
36
|
+
"eslint-plugin-promise": "^5.2.0",
|
|
37
|
+
"eslint-plugin-react": "^7.27.1",
|
|
38
38
|
"eslint-plugin-standard": "^5.0.0",
|
|
39
|
-
"html-loader": "^
|
|
40
|
-
"html-webpack-plugin": "^5.
|
|
41
|
-
"husky": "^
|
|
42
|
-
"jest": "^
|
|
43
|
-
"lint-staged": "^
|
|
44
|
-
"metro-react-native-babel-preset": "^0.
|
|
39
|
+
"html-loader": "^3.0.1",
|
|
40
|
+
"html-webpack-plugin": "^5.5.0",
|
|
41
|
+
"husky": "^7.0.4",
|
|
42
|
+
"jest": "^27.4.5",
|
|
43
|
+
"lint-staged": "^12.1.2",
|
|
44
|
+
"metro-react-native-babel-preset": "^0.66.2",
|
|
45
45
|
"react": "^17.0.2",
|
|
46
46
|
"react-dom": "^17.0.2",
|
|
47
|
-
"react-native": "^0.
|
|
47
|
+
"react-native": "^0.66.4",
|
|
48
48
|
"react-native-typescript-transformer": "^1.2.13",
|
|
49
|
-
"react-native-web": "^0.17.
|
|
49
|
+
"react-native-web": "^0.17.5",
|
|
50
50
|
"react-test-renderer": "17.0.2",
|
|
51
|
-
"release-it": "^14.
|
|
52
|
-
"ts-jest": "^27.
|
|
53
|
-
"ts-loader": "^9.2.
|
|
54
|
-
"typescript": "^4.
|
|
55
|
-
"webpack": "^5.
|
|
56
|
-
"webpack-cli": "^4.
|
|
57
|
-
"webpack-dev-server": "^
|
|
51
|
+
"release-it": "^14.11.8",
|
|
52
|
+
"ts-jest": "^27.1.1",
|
|
53
|
+
"ts-loader": "^9.2.6",
|
|
54
|
+
"typescript": "^4.5.4",
|
|
55
|
+
"webpack": "^5.65.0",
|
|
56
|
+
"webpack-cli": "^4.9.1",
|
|
57
|
+
"webpack-dev-server": "^4.6.0"
|
|
58
58
|
},
|
|
59
59
|
"jest": {
|
|
60
60
|
"preset": "react-native"
|
package/src/convertUnits.ts
CHANGED
|
@@ -47,6 +47,7 @@ export function convertValue (key: keyof PartialStyle | keyof Transform, value:
|
|
|
47
47
|
if (convertedValue.startsWith('calc(')) return calculate(convertedValue.substring(4))// remove calc. We can keep the parenthesis
|
|
48
48
|
else if (convertedValue.startsWith('max(')) return max(convertedValue.substring(4, convertedValue.length - 1))// Remove max()
|
|
49
49
|
else if (convertedValue.startsWith('min(')) return min(convertedValue.substring(4, convertedValue.length - 1))// remove min()
|
|
50
|
+
else if (key === 'fontWeight') return convertedValue // fontWeight must be a string even when it is an integer value.
|
|
50
51
|
else if (parseFloat(convertedValue) + '' === convertedValue) return parseFloat(convertedValue)
|
|
51
52
|
else return convertedValue
|
|
52
53
|
}
|
package/src/index.tsx
CHANGED
|
@@ -4,6 +4,7 @@ import styledComponent, { styledFlatList, styledSectionList, styledVirtualizedLi
|
|
|
4
4
|
export { cssToRNStyle } from './cssToRN'
|
|
5
5
|
export { FontSizeContext } from './features'
|
|
6
6
|
export { SharedValue } from './styleComponent'
|
|
7
|
+
export * from './useTheme'
|
|
7
8
|
|
|
8
9
|
const styled = <T, >(Component: React.ComponentType<T>) => styledComponent<T>(Component)
|
|
9
10
|
|
|
@@ -13,7 +14,6 @@ styled.DrawerLayoutAndroid = styled(RN.DrawerLayoutAndroid)
|
|
|
13
14
|
styled.Image = styled(RN.Image)
|
|
14
15
|
styled.ImageBackground = styled(RN.ImageBackground)
|
|
15
16
|
styled.KeyboardAvoidingView = styled(RN.KeyboardAvoidingView)
|
|
16
|
-
styled.ListView = styled(RN.ListView)
|
|
17
17
|
styled.Modal = styled(RN.Modal)
|
|
18
18
|
styled.NavigatorIOS = styled(RN.NavigatorIOS)
|
|
19
19
|
styled.ScrollView = styled(RN.ScrollView)
|
|
@@ -23,17 +23,13 @@ styled.RecyclerViewBackedScrollView = styled(RN.RecyclerViewBackedScrollView)
|
|
|
23
23
|
styled.RefreshControl = styled(RN.RefreshControl)
|
|
24
24
|
styled.SafeAreaView = styled(RN.SafeAreaView)
|
|
25
25
|
styled.StatusBar = styled(RN.StatusBar)
|
|
26
|
-
styled.SwipeableListView = styled(RN.SwipeableListView)
|
|
27
|
-
styled.TabBarIOS = styled(RN.TabBarIOS)
|
|
28
26
|
styled.Text = styled(RN.Text)
|
|
29
27
|
styled.TextInput = styled(RN.TextInput)
|
|
30
|
-
styled.ToolbarAndroid = styled(RN.ToolbarAndroid)
|
|
31
28
|
styled.TouchableHighlight = styled(RN.TouchableHighlight)
|
|
32
29
|
styled.TouchableNativeFeedback = styled(RN.TouchableNativeFeedback)
|
|
33
30
|
styled.TouchableOpacity = styled(RN.TouchableOpacity)
|
|
34
31
|
styled.TouchableWithoutFeedback = styled(RN.TouchableWithoutFeedback)
|
|
35
32
|
styled.View = styled(RN.View)
|
|
36
|
-
styled.ViewPagerAndroid = styled(RN.ViewPagerAndroid)
|
|
37
33
|
styled.FlatList = styledFlatList
|
|
38
34
|
styled.SectionList = styledSectionList
|
|
39
35
|
styled.VirtualizedList = styledVirtualizedList
|
package/src/styleComponent.tsx
CHANGED
|
@@ -15,7 +15,7 @@ const styleMap: StyleMap = {}
|
|
|
15
15
|
export const SharedValue = React.createContext<unknown>(undefined)
|
|
16
16
|
|
|
17
17
|
type Primitive = number | string | null | undefined | boolean
|
|
18
|
-
type Functs<T> = (arg: T & { rnCSS?: string; shared: unknown }) => Primitive
|
|
18
|
+
type Functs<T> = (arg: T & { rnCSS?: string; shared: unknown, theme: unknown }) => Primitive
|
|
19
19
|
type OptionalProps = {
|
|
20
20
|
rnCSS?: `${string};`;
|
|
21
21
|
onMouseEnter?: (event: MouseEvent) => void;
|
|
@@ -25,7 +25,7 @@ type OptionalProps = {
|
|
|
25
25
|
style?: StyleProp<any>;
|
|
26
26
|
}
|
|
27
27
|
function buildCSSString<T extends { rnCSS?: string }> (chunks: TemplateStringsArray, functs: (Primitive | Functs<T>)[], props: T, shared: unknown) {
|
|
28
|
-
let computedString = chunks.map((chunk, i) => ([chunk, (functs[i] instanceof Function) ? (functs[i] as Functs<T>)({
|
|
28
|
+
let computedString = chunks.map((chunk, i) => ([chunk, (functs[i] instanceof Function) ? (functs[i] as Functs<T>)({ shared, theme: shared, ...props }) : functs[i]])).flat().join('')
|
|
29
29
|
if (props.rnCSS) computedString += props.rnCSS.replace(/=/gm, ':') + ';'
|
|
30
30
|
return computedString
|
|
31
31
|
}
|
package/src/useTheme.tsx
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { SharedValue } from './styleComponent'
|
|
3
|
+
|
|
4
|
+
/** Provider for the theme. */
|
|
5
|
+
export const ThemeProvider = ({ theme, children }: { theme: unknown; children: React.ReactNode }) => {
|
|
6
|
+
return <SharedValue.Provider value={theme}>
|
|
7
|
+
{children}
|
|
8
|
+
</SharedValue.Provider>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Returns the Theme
|
|
13
|
+
* @returns The Theme object
|
|
14
|
+
*/
|
|
15
|
+
export const useTheme = () => React.useContext(SharedValue)
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Adds the theme prop to a non rn-css component
|
|
19
|
+
* @param Component A non rn-css component
|
|
20
|
+
* @returns A component with the theme prop
|
|
21
|
+
*/
|
|
22
|
+
export const withTheme = <T, >(Component: React.ComponentType<T>) => {
|
|
23
|
+
const theme = useTheme()
|
|
24
|
+
const ThemedComponent = React.forwardRef<React.Component, T>((props: T, ref) => <Component ref={ref} theme={theme} {...props} />)
|
|
25
|
+
ThemedComponent.displayName = 'ThemedComponent'
|
|
26
|
+
return ThemedComponent
|
|
27
|
+
}
|