Skip to content

Commit 365e8bb

Browse files
committed
Simplify responsive setup guide
1 parent f7f5b07 commit 365e8bb

File tree

1 file changed

+18
-154
lines changed

1 file changed

+18
-154
lines changed

docs/setup-responsive.md

Lines changed: 18 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,42 @@
11
## Setup CSS modules for React Native (with CSS Media Queries & CSS Viewport Units support)
22

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:
44

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.
95
- [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.
106

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
148

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)
1614

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
1816

19-
e.g.
17+
Remove old one:
2018

2119
```sh
22-
react-native init AwesomeProject
23-
cd AwesomeProject
20+
yarn remove babel-plugin-react-native-classname-to-style --dev
2421
```
2522

26-
Start packager:
23+
Add new one:
2724

2825
```sh
29-
yarn start
26+
yarn add babel-plugin-react-native-classname-to-dynamic-style --dev
3027
```
3128

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
4730

4831
`.babelrc` (or `babel.config.js`)
4932

50-
```json
51-
{
52-
"presets": ["module:metro-react-native-babel-preset"],
33+
```diff
5334
"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",
17337
```
17438

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
17640

17741
`styles.css`:
17842

@@ -219,7 +83,7 @@ export default class App extends Component<{}> {
21983
}
22084
```
22185

222-
### Step 7: Restart packager and clear cache
86+
### Step 5: Restart packager and clear cache
22387

22488
Restart React Native packager and clear it's cache (important) to see the styles that you added.
22589

0 commit comments

Comments
 (0)