rn-css 1.4.1 → 1.5.0
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 +2 -2
- package/dist/cssToRN/index.js +3 -2
- package/dist/features.js +2 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -0
- package/dist/useTheme.d.ts +7 -0
- package/dist/useTheme.js +21 -0
- package/package.json +1 -1
- package/src/convertStyle.tsx +3 -3
- package/src/cssToRN/index.ts +3 -2
- package/src/features.tsx +2 -2
- package/src/index.tsx +1 -0
- package/src/useTheme.tsx +17 -0
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
This is basically [styled-components](https://github.com/styled-components/styled-components) with a much better support for React-Native, and some awesome additional features. You can check the docs of [styled-components](https://github.com/styled-components/styled-components) for more details about the basic API. I'll focus here on the differences.
|
|
4
4
|
|
|
5
|
+
**Current version: 1.5** [See the Changelog](./CHANGELOG.md)
|
|
6
|
+
|
|
5
7
|
---
|
|
6
8
|
|
|
7
9
|
## Purpose
|
|
@@ -331,6 +333,14 @@ const View = styled.View`
|
|
|
331
333
|
|
|
332
334
|
---
|
|
333
335
|
|
|
336
|
+
## Theming:
|
|
337
|
+
|
|
338
|
+
To match the API of styled-components, we offer the same abilities for theming [See the documentation](https://styled-components.com/docs/advanced).
|
|
339
|
+
|
|
340
|
+
This relies on the [SharedValue](#shared-value) context. This means that you cannot use the Shared Value system **and** this theming système. Pick the one that best suits your needs.
|
|
341
|
+
|
|
342
|
+
---
|
|
343
|
+
|
|
334
344
|
## Coming later:
|
|
335
345
|
|
|
336
346
|
linear-gradient, background-repeat, transitions, animations
|
package/dist/convertStyle.js
CHANGED
|
@@ -41,8 +41,8 @@ const convertStyle = (rnStyle, units) => {
|
|
|
41
41
|
height: convertUnits_1.convertValue(key, rnStyle.textShadowOffset.height || '0', units)
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
|
-
// Font family should not be transformed
|
|
45
|
-
else if (
|
|
44
|
+
// Font family should not be transformed (same as cursor for web in case of base64 value)
|
|
45
|
+
else if (['cursor', 'fontFamily'].includes(key)) {
|
|
46
46
|
convertedStyle[key] = value;
|
|
47
47
|
}
|
|
48
48
|
else {
|
package/dist/cssToRN/index.js
CHANGED
|
@@ -61,8 +61,9 @@ function cssToRNStyle(css, units = {}) {
|
|
|
61
61
|
exports.cssToRNStyle = cssToRNStyle;
|
|
62
62
|
function cssChunkToStyle(css) {
|
|
63
63
|
const result = {};
|
|
64
|
-
css.split(/\s*;\s
|
|
65
|
-
const [rawKey,
|
|
64
|
+
css.split(/\s*;\s*(?!base64)/mg).forEach((entry) => {
|
|
65
|
+
const [rawKey, ...rest] = entry.split(':');
|
|
66
|
+
const rawValue = rest.join(':');
|
|
66
67
|
if (!rawValue)
|
|
67
68
|
return;
|
|
68
69
|
const key = kebab2camel(rawKey.trim());
|
package/dist/features.js
CHANGED
|
@@ -59,9 +59,8 @@ const useLayout = (onLayout) => {
|
|
|
59
59
|
if (unmounted.current)
|
|
60
60
|
return;
|
|
61
61
|
const { width, height } = event.nativeEvent.layout;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}, [onLayout, layout.width, layout.height]);
|
|
62
|
+
setLayout(layout => layout.width === width && layout.height === height ? layout : { width, height });
|
|
63
|
+
}, [onLayout]);
|
|
65
64
|
return { onLayout: updateLayout, ...layout };
|
|
66
65
|
};
|
|
67
66
|
exports.useLayout = useLayout;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import * as RN from 'react-native';
|
|
|
3
3
|
export { cssToRNStyle } from './cssToRN';
|
|
4
4
|
export { FontSizeContext } from './features';
|
|
5
5
|
export { SharedValue } from './styleComponent';
|
|
6
|
+
export * from './useTheme';
|
|
6
7
|
declare const styled: {
|
|
7
8
|
<T>(Component: React.ComponentType<T>): {
|
|
8
9
|
<S>(chunks: TemplateStringsArray, ...functs: ((string | number | boolean | null | undefined) | ((arg: S & T & {
|
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,6 +31,7 @@ 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; } });
|
|
34
|
+
__exportStar(require("./useTheme"), exports);
|
|
31
35
|
const styled = (Component) => styleComponent_1.default(Component);
|
|
32
36
|
styled.ActivityIndicator = styled(RN.ActivityIndicator);
|
|
33
37
|
styled.Button = styled(RN.Button);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export declare const ThemeProvider: ({ theme, children }: {
|
|
3
|
+
theme: unknown;
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
}) => JSX.Element;
|
|
6
|
+
export declare const useTheme: () => unknown;
|
|
7
|
+
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,21 @@
|
|
|
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
|
+
const ThemeProvider = ({ theme, children }) => {
|
|
10
|
+
return react_1.default.createElement(styleComponent_1.SharedValue.Provider, { value: theme }, children);
|
|
11
|
+
};
|
|
12
|
+
exports.ThemeProvider = ThemeProvider;
|
|
13
|
+
const useTheme = () => react_1.default.useContext(styleComponent_1.SharedValue);
|
|
14
|
+
exports.useTheme = useTheme;
|
|
15
|
+
const withTheme = (Component) => {
|
|
16
|
+
const theme = exports.useTheme();
|
|
17
|
+
const ThemedComponent = react_1.default.forwardRef((props, ref) => react_1.default.createElement(Component, { ref: ref, theme: theme, ...props }));
|
|
18
|
+
ThemedComponent.displayName = 'ThemedComponent';
|
|
19
|
+
return ThemedComponent;
|
|
20
|
+
};
|
|
21
|
+
exports.withTheme = withTheme;
|
package/package.json
CHANGED
package/src/convertStyle.tsx
CHANGED
|
@@ -42,9 +42,9 @@ const convertStyle = (rnStyle: PartialStyle, units: Units) => {
|
|
|
42
42
|
height: convertValue(key, rnStyle.textShadowOffset!.height || '0', units) as number
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
|
-
// Font family should not be transformed
|
|
46
|
-
else if (
|
|
47
|
-
convertedStyle[key] = value
|
|
45
|
+
// Font family should not be transformed (same as cursor for web in case of base64 value)
|
|
46
|
+
else if (['cursor', 'fontFamily'].includes(key)) {
|
|
47
|
+
convertedStyle[key as 'fontFamily'] = value
|
|
48
48
|
}
|
|
49
49
|
else {
|
|
50
50
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
package/src/cssToRN/index.ts
CHANGED
|
@@ -59,8 +59,9 @@ export function cssToRNStyle (css: string, units: { em?: number, width?: number,
|
|
|
59
59
|
|
|
60
60
|
function cssChunkToStyle (css: string) {
|
|
61
61
|
const result: PartialStyle = {}
|
|
62
|
-
css.split(/\s*;\s
|
|
63
|
-
const [rawKey,
|
|
62
|
+
css.split(/\s*;\s*(?!base64)/mg).forEach((entry: string) => {
|
|
63
|
+
const [rawKey, ...rest] = entry.split(':')
|
|
64
|
+
const rawValue = rest.join(':')
|
|
64
65
|
if (!rawValue) return
|
|
65
66
|
const key = kebab2camel(rawKey.trim())
|
|
66
67
|
const value = stripSpaces(rawValue.trim())// We need this to correctly read calc() values
|
package/src/features.tsx
CHANGED
|
@@ -51,8 +51,8 @@ export const useLayout = (onLayout?: (event: LayoutChangeEvent) => void) => {
|
|
|
51
51
|
if (onLayout) onLayout(event)
|
|
52
52
|
if (unmounted.current) return
|
|
53
53
|
const { width, height } = event.nativeEvent.layout
|
|
54
|
-
|
|
55
|
-
}, [onLayout
|
|
54
|
+
setLayout(layout => layout.width === width && layout.height === height ? layout : { width, height })
|
|
55
|
+
}, [onLayout])
|
|
56
56
|
return { onLayout: updateLayout, ...layout }
|
|
57
57
|
}
|
|
58
58
|
|
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
|
|
package/src/useTheme.tsx
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { SharedValue } from './styleComponent'
|
|
3
|
+
|
|
4
|
+
export const ThemeProvider = ({ theme, children }: { theme: unknown; children: React.ReactNode }) => {
|
|
5
|
+
return <SharedValue.Provider value={theme}>
|
|
6
|
+
{children}
|
|
7
|
+
</SharedValue.Provider>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const useTheme = () => React.useContext(SharedValue)
|
|
11
|
+
|
|
12
|
+
export const withTheme = <T, >(Component: React.ComponentType<T>) => {
|
|
13
|
+
const theme = useTheme()
|
|
14
|
+
const ThemedComponent = React.forwardRef<React.Component, T>((props: T, ref) => <Component ref={ref} theme={theme} {...props} />)
|
|
15
|
+
ThemedComponent.displayName = 'ThemedComponent'
|
|
16
|
+
return ThemedComponent
|
|
17
|
+
}
|