Skip to content

Commit 8c2d0a1

Browse files
authored
chore: update readme (#175)
1 parent 2216c3c commit 8c2d0a1

File tree

1 file changed

+138
-10
lines changed

1 file changed

+138
-10
lines changed

README.md

Lines changed: 138 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ The goal of this library is to provide the most complete CSS support for React N
1313
### Metro based projects
1414

1515
> [!TIP]
16-
> All Expo and React Native Community CLI projects use Metro as the bundler, so this guide applies to them.
16+
> Most React Native projects use Metro as the bundler.
1717
1818
You will need to add `withReactNativeCSS` to your Metro configuration.
1919

20+
#### Expo Projects
21+
2022
```ts
2123
import { getDefaultConfig } from "expo/metro-config";
2224
import { withReactNativeCSS } from "react-native-css/metro";
@@ -31,6 +33,17 @@ export default withReactNativeCSS(defaultConfig, {
3133
});
3234
```
3335

36+
#### Non-Expo Projects
37+
38+
`react-native-css` relies on the bundler to process CSS files. Currently only Expo provides a CSS asset pipeline. Since the Expo SDK is modular, you can add this functionality by just using the `@expo/metro-config` package.
39+
40+
Follow the Expo instructions, but replace the `expo` package with `@expo/metro-config`.
41+
42+
```diff
43+
- import { getDefaultConfig } from "expo/metro-config";
44+
+ import { getDefaultConfig } from "@expo/metro-config";
45+
```
46+
3447
### Other bundlers
3548

3649
`react-native-css` officially only supports Metro as the bundler, but we welcome community contributions to support other bundlers like Webpack, Vite or Turbopack.
@@ -88,23 +101,42 @@ module.exports = withReactNativeCSS(
88101
);
89102
```
90103

91-
### Via hooks
104+
### Via `styled`
105+
106+
You can also use the `styled` function to get styled components.
107+
108+
```ts
109+
import { View } from 'react-native';
110+
import { styled } from 'react-native-css';
111+
112+
import "./styles.css";
113+
114+
const MyView = styled(View)
115+
116+
export default function App() {
117+
return (
118+
<MyView className="container">
119+
<MyView className="box" />
120+
</View>
121+
);
122+
}
123+
```
92124

93-
#### `useCssElement`
125+
### Via hooks
94126

95127
You can also use the `useCssElement` hook.
96128

97129
```ts
98-
import { View as RNView } from 'react-native';
130+
import { View } from 'react-native';
99131
import { useCssElement } from 'react-native-css';
100132

101133
export default function App() {
102-
const Container = useCssElement(RNView, {
134+
const Container = useCssElement(View, {
103135
className: "container",
104136
});
105137

106-
const Box = useCssElement(RNView, {
107-
className: "container",
138+
const Box = useCssElement(View, {
139+
className: "box",
108140
});
109141

110142
return (
@@ -116,11 +148,11 @@ export default function App() {
116148
```
117149

118150
> [!IMPORTANT]
119-
> The hook returns a React Element, not a style object. The element will work on all platforms and will correctly apply the React context needed to support all features of the library
151+
> The hook returns a React Element, not a style object.
120152
121153
#### `useNativeCssStyle`
122154

123-
If you just require the style object, you can use the `useNativeCssStyle` hook:
155+
If you just require the style object, you can use the `useNativeCssStyle` hook
124156

125157
```ts
126158
import { View as RNView } from 'react-native';
@@ -152,7 +184,7 @@ If you just require a CSS variable value, you can use the `useNativeCssVariable`
152184
import { useNativeCssVariable } from 'react-native-css';
153185

154186
export default function App() {
155-
const myColor = useNativeCssVariable("my-color");
187+
const myColor = useNativeCssVariable("--my-color");
156188

157189
return (
158190
<View style={{ backgroundColor: myColor }}>
@@ -168,6 +200,102 @@ export default function App() {
168200
> This hook may will only work on native platforms. It will return `undefined` on web.
169201
> This hook may not support all features of the library.
170202
203+
## CSS variables
204+
205+
It is preferable that all CSS variables are set via CSS. If you need values to change dynamically, we recommend using a class to change the values.
206+
207+
```css
208+
.theme-red {
209+
--brand-color: red;
210+
}
211+
212+
.theme-blue {
213+
--brand-color: blue;
214+
}
215+
```
216+
217+
As a last resort, you can use `VariableContext` to dynamically set CSS variables in JavaScript
218+
219+
```ts
220+
import { VariableContext } from 'react-native-css';
221+
222+
export default function App() {
223+
return (
224+
<VariableContext values={{ "--my-color": "red" }}>
225+
<Text className="my-color-text">
226+
Hello, world!
227+
</Text>
228+
</VariableContext>
229+
)
230+
}
231+
```
232+
233+
This API only allows for setting CSS variables as primitive values. For more complex styles, you will need to use a helper CSS class.
234+
235+
> [!IMPORTANT]
236+
> By using `VariableContext` you may need to disable the `inlineVariable` optimization
237+
238+
## Optimizations
239+
240+
CSS is a dynamic styling language that use highly optimized engines that are not available in React Native. Instead, we optimize the styles to improve performance
241+
242+
These optimizations are only applied in native environments and are enabled by default.
243+
244+
### Inline REM units
245+
246+
All `rem` units are converted to `dp` units at build time. On native, the default dp is 14. You can change the default `rem` by passing a `inlineRem` option to the `withReactNativeCSS` function.
247+
248+
```tsx
249+
export default withReactNativeCSS(defaultConfig, {
250+
inlineRem: 16, // change to 16dp,
251+
});
252+
```
253+
254+
### Inline CSS Custom Properties (variables)
255+
256+
Custom properties (sometimes referred to as CSS variables or cascading variables) are a way to store values that can be reused throughout a CSS document. They are defined using a property name that starts with `--`, and their values can be accessed using the `var()` function.
257+
258+
To improve performance, Custom properties that are only set **once** in the CSS file are inlined at build time.
259+
260+
For example
261+
262+
```css
263+
:root {
264+
--my-var: red;
265+
--var-with-two-possible-values: blue;
266+
}
267+
268+
.my-class {
269+
--var-with-two-possible-values: green;
270+
color: var(--my-var);
271+
background-color: var(--var-with-two-possible-values);
272+
}
273+
```
274+
275+
Is converted to:
276+
277+
```css
278+
:root {
279+
--var-with-two-possible-values: blue;
280+
}
281+
282+
.my-class {
283+
color: red; /* This was inlined and the variable was removed */
284+
285+
/* These are preserved as there are multiple possible values */
286+
--var-with-two-possible-values: green;
287+
background-color: var(--var-with-two-possible-values);
288+
}
289+
```
290+
291+
Using `VariableContext` with `inlineVariables` may have unexpected results, as rules may have been rewritten not to use a variable. You can disable this behavior by setting `inlineVariables: false`
292+
293+
```tsx
294+
export default withReactNativeCSS(defaultConfig, {
295+
inlineVariables: false,
296+
});
297+
```
298+
171299
## Contributing
172300

173301
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.

0 commit comments

Comments
 (0)