rn-css 1.3.0 → 1.4.2

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
@@ -163,6 +163,23 @@ const Extended = styled(MyComponent)`
163
163
 
164
164
  ---
165
165
 
166
+ ## Access current font size value
167
+
168
+ If, somewhere within your app, you need to access the current font size value in px to be able to manually convert em into px, you can use the `FontSizeContext`. This can be helpful if you want to change some behaviour within your app depending on the font size.
169
+
170
+
171
+ ```javascript
172
+ import { FontSizeContext } from 'rn-css'
173
+
174
+ ...
175
+ const [width, setWidth] = React.useState(0)
176
+ const em = React.useContext(FontSizeContext)
177
+ if(width < 70 * em) { /* Do something when width is lesser than 70em */ }
178
+ return <View onLayout={event => setWidth(event.nativeEvent.layout.width)}>...</View>
179
+ ```
180
+
181
+ ---
182
+
166
183
  ## Convert a CSS string to React-Native Style
167
184
 
168
185
  If, for some reason, you just want to convert a css string to a ReactNative Style object, you can use this feature:
@@ -175,24 +192,21 @@ const style = cssToRNStyle('width: 2em; border-width: 12px; background: blue;')
175
192
  const { width = 32, borderLeftWidth = 12, backgroundColor = 'blue' } = style
176
193
  ```
177
194
 
178
- ---
179
-
180
- ## Access current font size value
181
-
182
- If, somewhere within your app, you need to access the current font size value in px to be able to manually convert em into px, you can use the `FontSizeContext`. This can be helpful if you want to change some behaviour within your app depending on the font size.
183
-
195
+ The second parameter lets you provide:
196
+ * **em** : *(Default: 16)* the current value of em unit for font size. You can retrieve the current context value with the `FontSizeContext`.
197
+ * **width** *(Default: 100)* the reference width that will be used to calculate percentages for the following properties: `marginLeft`, `marginRight`, `translateX` and `borderRadius`
198
+ * **height** *(Default: 100)* the reference width that will be used to calculate percentages for the following properties: `marginTop`, `marginBottom`, `translateY` and `borderRadius`
184
199
 
185
200
  ```javascript
186
- import { FontSizeContext } from 'rn-css'
201
+ import { cssToRNStyle, FontSizeContext } from 'rn-css'
187
202
 
188
203
  ...
189
- const [width, setWidth] = React.useState(0)
190
- const em = React.useContext(FontSizeContext)
191
- if(width < 70 * em) { /* Do something when width is lesser than 70em */ }
192
- return <View onLayout={event => setWidth(event.nativeEvent.layout.width)}>...</View>
204
+ const style = cssToRNStyle('width: 2em; margin: 10%;', { em: React.useContext(FontSizeContext), width: 100, height: 100 })
205
+ const { width: 32, marginTop: 10, marginLeft: 10, marginRight: 10, marginBottom: 10 } = style
193
206
  ```
194
207
 
195
208
  ---
209
+
196
210
  ## Extended CSS support
197
211
 
198
212
  We support some cool CSS feature that React-Native doesn't normally handle
@@ -44,8 +44,8 @@ function cssToRNStyle(css, units = {}) {
44
44
  vh: height / 100,
45
45
  vmin: Math.min(width, height) / 100,
46
46
  vmax: Math.max(width, height) / 100,
47
- width: 1,
48
- height: 1,
47
+ width: 100,
48
+ height: 100,
49
49
  rem: 16,
50
50
  px: 1,
51
51
  pt: 72 / 96,
@@ -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());
@@ -18,7 +18,7 @@ export declare const useHover: (rnStyle: Style, onMouseEnter?: ((event: MouseEve
18
18
  };
19
19
  /** HOC that will apply the style provided in the media queries */
20
20
  export declare const useMediaQuery: (media: undefined | MediaQuery[], units: Units) => Style | undefined;
21
- /** HOC that will apply the font size to the styles defined with em units */
21
+ /** HOC that will measure the layout to handle styles that use % units */
22
22
  export declare const useLayout: (onLayout?: ((event: LayoutChangeEvent) => void) | undefined) => {
23
23
  width: number;
24
24
  height: number;
package/dist/features.js CHANGED
@@ -47,12 +47,17 @@ const useMediaQuery = (media, units) => {
47
47
  }
48
48
  };
49
49
  exports.useMediaQuery = useMediaQuery;
50
- /** HOC that will apply the font size to the styles defined with em units */
50
+ /** HOC that will measure the layout to handle styles that use % units */
51
51
  const useLayout = (onLayout) => {
52
52
  const [layout, setLayout] = react_1.default.useState({ width: 0, height: 0 });
53
+ // Prevent calling setState if the component is unmounted
54
+ const unmounted = react_1.default.useRef(false);
55
+ react_1.default.useEffect(() => () => { unmounted.current = true; }, []);
53
56
  const updateLayout = react_1.default.useCallback((event) => {
54
57
  if (onLayout)
55
58
  onLayout(event);
59
+ if (unmounted.current)
60
+ return;
56
61
  const { width, height } = event.nativeEvent.layout;
57
62
  if (width !== layout.width || height !== layout.height)
58
63
  setLayout({ width, height });