You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`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
+
34
47
### Other bundlers
35
48
36
49
`react-native-css` officially only supports Metro as the bundler, but we welcome community contributions to support other bundlers like Webpack, Vite or Turbopack.
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
+
exportdefaultfunction App() {
117
+
return (
118
+
<MyViewclassName="container">
119
+
<MyViewclassName="box"/>
120
+
</View>
121
+
);
122
+
}
123
+
```
92
124
93
-
#### `useCssElement`
125
+
###Via hooks
94
126
95
127
You can also use the `useCssElement` hook.
96
128
97
129
```ts
98
-
import { ViewasRNView} from'react-native';
130
+
import { View } from'react-native';
99
131
import { useCssElement } from'react-native-css';
100
132
101
133
exportdefaultfunction App() {
102
-
const Container =useCssElement(RNView, {
134
+
const Container =useCssElement(View, {
103
135
className: "container",
104
136
});
105
137
106
-
const Box =useCssElement(RNView, {
107
-
className: "container",
138
+
const Box =useCssElement(View, {
139
+
className: "box",
108
140
});
109
141
110
142
return (
@@ -116,11 +148,11 @@ export default function App() {
116
148
```
117
149
118
150
> [!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.
120
152
121
153
#### `useNativeCssStyle`
122
154
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
124
156
125
157
```ts
126
158
import { ViewasRNView } from'react-native';
@@ -152,7 +184,7 @@ If you just require a CSS variable value, you can use the `useNativeCssVariable`
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
+
exportdefaultwithReactNativeCSS(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.
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
+
exportdefaultwithReactNativeCSS(defaultConfig, {
295
+
inlineVariables: false,
296
+
});
297
+
```
298
+
171
299
## Contributing
172
300
173
301
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
0 commit comments