|
1 | 1 | ## Setup CSS modules for React Native (with CSS Media Queries & CSS Viewport Units support) |
2 | 2 |
|
3 | | -_This example only shows how to setup CSS support using the CSS transformer. Have a look at the setup documentation if you need PostCSS, Sass, Less or Stylus support (you need to use a different transformer and file extensions)._ |
| 3 | +Following library needs to be taken into use: |
4 | 4 |
|
5 | | -Following libraries are needed: |
6 | | - |
7 | | -- [react-native-css-transformer](https://github.com/kristerkari/react-native-css-transformer) - Transforms CSS to a React Native compatible style object and handles live reloading |
8 | | -- [babel-plugin-react-native-platform-specific-extensions](https://github.com/kristerkari/babel-plugin-react-native-platform-specific-extensions) - Transforms ES6 `import` statements to platform specific `require` statements if the platform specific files exist on disk. |
9 | 5 | - [babel-plugin-react-native-classname-to-dynamic-style](https://github.com/kristerkari/babel-plugin-react-native-classname-to-dynamic-style) - Transforms `className` property to `style` property and matches dynamic styles (media queries and viewport units) at runtime with React Native. |
10 | 6 |
|
11 | | -### Step 1: Install depencies to run React Native |
12 | | - |
13 | | -Make sure that you have `react-native-cli` installed (`npm install -g react-native-cli`) and [XCode](https://developer.apple.com/xcode/) (for iOS development) / [Android Studio](https://developer.android.com/studio/index.html) (for Android development) installed and working. |
| 7 | +### Step 1: Setup React Native CSS modules |
14 | 8 |
|
15 | | -- Go to "Building Projects with Native Code" tab and follow the guide: https://facebook.github.io/react-native/docs/getting-started.html |
| 9 | +- [Setup React Native CSS modules with CSS support](setup-css.md) |
| 10 | +- [Setup React Native CSS modules with PostCSS support](setup-postcss.md) |
| 11 | +- [Setup React Native CSS modules with Sass support](setup-sass.md) |
| 12 | +- [Setup React Native CSS modules with Less support](setup-less.md) |
| 13 | +- [Setup React Native CSS modules with Stylus support](setup-stylus.md) |
16 | 14 |
|
17 | | -### Step 2: Create a new React Native app and test that it works |
| 15 | +### Step 2: Change the className Babel plugin to a dynamic one |
18 | 16 |
|
19 | | -e.g. |
| 17 | +Remove old one: |
20 | 18 |
|
21 | 19 | ```sh |
22 | | -react-native init AwesomeProject |
23 | | -cd AwesomeProject |
| 20 | +yarn remove babel-plugin-react-native-classname-to-style --dev |
24 | 21 | ``` |
25 | 22 |
|
26 | | -Start packager: |
| 23 | +Add new one: |
27 | 24 |
|
28 | 25 | ```sh |
29 | | -yarn start |
| 26 | +yarn add babel-plugin-react-native-classname-to-dynamic-style --dev |
30 | 27 | ``` |
31 | 28 |
|
32 | | -Run project on iOS simulator: |
33 | | - |
34 | | -```sh |
35 | | -react-native run-ios |
36 | | -``` |
37 | | - |
38 | | -### Step 3: Install dependencies for React Native CSS modules |
39 | | - |
40 | | -```sh |
41 | | -yarn add babel-plugin-react-native-classname-to-dynamic-style babel-plugin-react-native-platform-specific-extensions react-native-css-transformer --dev |
42 | | -``` |
43 | | - |
44 | | -### Step 4: Setup Babel configuration |
45 | | - |
46 | | -#### For React Native v0.57 or newer |
| 29 | +### Step 3: Change Babel configuration |
47 | 30 |
|
48 | 31 | `.babelrc` (or `babel.config.js`) |
49 | 32 |
|
50 | | -```json |
51 | | -{ |
52 | | - "presets": ["module:metro-react-native-babel-preset"], |
| 33 | +```diff |
53 | 34 | "plugins": [ |
54 | | - "react-native-classname-to-dynamic-style", |
55 | | - [ |
56 | | - "react-native-platform-specific-extensions", |
57 | | - { |
58 | | - "extensions": ["css"] |
59 | | - } |
60 | | - ] |
61 | | - ] |
62 | | -} |
63 | | -``` |
64 | | - |
65 | | ---- |
66 | | - |
67 | | -#### For React Native v0.56 or older |
68 | | - |
69 | | -`.babelrc` |
70 | | - |
71 | | -```json |
72 | | -{ |
73 | | - "presets": ["react-native"], |
74 | | - "plugins": [ |
75 | | - "react-native-classname-to-dynamic-style", |
76 | | - [ |
77 | | - "react-native-platform-specific-extensions", |
78 | | - { |
79 | | - "extensions": ["css"] |
80 | | - } |
81 | | - ] |
82 | | - ] |
83 | | -} |
84 | | -``` |
85 | | - |
86 | | ---- |
87 | | - |
88 | | -#### For Expo |
89 | | - |
90 | | -`babel.config.js` (older Expo versions use `.babelrc`) |
91 | | - |
92 | | -```js |
93 | | -module.exports = function(api) { |
94 | | - api.cache(true); |
95 | | - return { |
96 | | - presets: ["babel-preset-expo"], |
97 | | - plugins: [ |
98 | | - "react-native-classname-to-dynamic-style", |
99 | | - ["react-native-platform-specific-extensions", { extensions: ["css"] }] |
100 | | - ] |
101 | | - }; |
102 | | -}; |
103 | | -``` |
104 | | - |
105 | | -### Step 5: Setup Metro bundler configuration |
106 | | - |
107 | | -#### For React Native v0.57 or newer / Expo SDK v31.0.0 or newer |
108 | | - |
109 | | -Add this to `metro.config.js` in your project's root (create the file if you don't have one already): |
110 | | - |
111 | | -```js |
112 | | -const { getDefaultConfig } = require("metro-config"); |
113 | | - |
114 | | -module.exports = (async () => { |
115 | | - const { |
116 | | - resolver: { sourceExts } |
117 | | - } = await getDefaultConfig(); |
118 | | - return { |
119 | | - transformer: { |
120 | | - babelTransformerPath: require.resolve("react-native-css-transformer") |
121 | | - }, |
122 | | - resolver: { |
123 | | - sourceExts: [...sourceExts, "css"] |
124 | | - } |
125 | | - }; |
126 | | -})(); |
127 | | -``` |
128 | | - |
129 | | -If you are using [Expo](https://expo.io/), you also need to add this to `app.json`: |
130 | | - |
131 | | -```json |
132 | | -{ |
133 | | - "expo": { |
134 | | - "packagerOpts": { |
135 | | - "config": "metro.config.js" |
136 | | - } |
137 | | - } |
138 | | -} |
139 | | -``` |
140 | | - |
141 | | ---- |
142 | | - |
143 | | -#### For React Native v0.56 or older |
144 | | - |
145 | | -If you are using React Native without Expo, add this to `rn-cli.config.js` in your project's root (create the file if you don't have one already): |
146 | | - |
147 | | -```js |
148 | | -module.exports = { |
149 | | - getTransformModulePath() { |
150 | | - return require.resolve("react-native-css-transformer"); |
151 | | - }, |
152 | | - getSourceExts() { |
153 | | - return ["js", "jsx", "css"]; |
154 | | - } |
155 | | -}; |
156 | | -``` |
157 | | - |
158 | | ---- |
159 | | - |
160 | | -#### For Expo SDK v30.0.0 or older |
161 | | - |
162 | | -If you are using [Expo](https://expo.io/), instead of adding the `rn-cli.config.js` file, you need to add this to `app.json`: |
163 | | - |
164 | | -```json |
165 | | -{ |
166 | | - "expo": { |
167 | | - "packagerOpts": { |
168 | | - "sourceExts": ["js", "jsx", "css"], |
169 | | - "transformer": "node_modules/react-native-css-transformer/index.js" |
170 | | - } |
171 | | - } |
172 | | -} |
| 35 | +- "react-native-classname-to-style", |
| 36 | ++ "react-native-classname-to-dynamic-style", |
173 | 37 | ``` |
174 | 38 |
|
175 | | -### Step 6: Add some CSS with media queries to your project and use it inside a React component |
| 39 | +### Step 4: Add some CSS with media queries to your project and use it inside a React component |
176 | 40 |
|
177 | 41 | `styles.css`: |
178 | 42 |
|
@@ -219,7 +83,7 @@ export default class App extends Component<{}> { |
219 | 83 | } |
220 | 84 | ``` |
221 | 85 |
|
222 | | -### Step 7: Restart packager and clear cache |
| 86 | +### Step 5: Restart packager and clear cache |
223 | 87 |
|
224 | 88 | Restart React Native packager and clear it's cache (important) to see the styles that you added. |
225 | 89 |
|
|
0 commit comments