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 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
@@ -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 (key === 'fontFamily') {
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 {
@@ -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*/mg).forEach((entry) => {
65
- const [rawKey, rawValue] = entry.split(':');
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
- if (width !== layout.width || height !== layout.height)
63
- setLayout({ width, height });
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>>>;
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rn-css",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "scripts": {
5
5
  "test": "jest",
6
6
  "prepare": "tsc",
@@ -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 (key === 'fontFamily') {
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
@@ -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*/mg).forEach((entry: string) => {
63
- const [rawKey, rawValue] = entry.split(':')
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
- if (width !== layout.width || height !== layout.height) setLayout({ width, height })
55
- }, [onLayout, layout.width, layout.height])
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
 
@@ -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
+ }