Skip to content

Commit 24b1d6d

Browse files
kakulguptargabs
authored andcommitted
Proofing chapter styling (#26)
1 parent a114227 commit 24b1d6d

File tree

4 files changed

+41
-40
lines changed

4 files changed

+41
-40
lines changed

8-styling/8.0-intro.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Styling in React Native
22

3-
In react native we use StyleSheets to write our styling. StyleSheets can be thought of something like a subset of CSS along with few additional helpers for RN. StyleSheets unlike CSS are pure JavaScript objects. This might confuse some web developers in the beginning and introduces a slight learning curve. StyleSheets in React Native are used somewhat similar to that of inline styles in web. Hence, you do not have access to pseudo classes like `:hover`,`:active`, etc.
3+
In React Native we use StyleSheets to write our styling. StyleSheets can be thought of as a subset of CSS along with a few additional helpers for RN. StyleSheets, unlike CSS, are pure JavaScript objects. This might confuse some web developers in the beginning and introduce a slight learning curve. StyleSheets in React Native are used somewhat similarly to inline styles in web. Hence, you do not have access to pseudo-classes like `:hover`, `:active`, etc.
44

55
### Example
66

7-
Imagine we want to add padding and border to a span. In CSS we will write:
7+
To add padding and border to a span in CSS we will write:
88

99
```css
1010
.button {
@@ -13,15 +13,15 @@ Imagine we want to add padding and border to a span. In CSS we will write:
1313
border: 1px solid black;
1414
}
1515
```
16-
and then we will add this class to our span like this:
16+
Then we will add this class to our span like this:
1717
```html
1818
<span class="button"> Test </span>
1919
```
2020
Resulting in :
2121

2222
<span style='padding:15px;border:1px solid black;'> Test </span>
2323

24-
In React Native, there is no concept of pixels or classes. Instead, our sizes will be specified in "units" which will then be translated to pixels based on the pixel density of the screen automagically by RN. If we write the same `View` in a React Native StyleSheet, it would look something like this.
24+
In React Native, there is no concept of pixels or classes. Instead, our sizes will be specified in "units" which will then be translated to pixels based on the pixel density of the screen automatically by RN. If we write the same `View` in a React Native StyleSheet, it would look something like this:
2525

2626
>styles.js
2727
```js
@@ -36,7 +36,7 @@ export default StyleSheet.create({
3636
});
3737
```
3838

39-
and we will add styles to our View by:
39+
These styles can be added to our View using:
4040

4141
```js
4242
import styles from './styles.js';
@@ -48,10 +48,10 @@ import styles from './styles.js';
4848
<View style={styles.button}></View>
4949
```
5050

51-
Note that since we are writing JavaScript objects, immediately we notice that:
52-
- We write each style in its camel-cased version of the CSS
53-
- We then import our StyleSheet into our components and used them by declaring a style attribute as shown instead of classes.
51+
Note that since we are writing JavaScript objects:
52+
- We write each style attribute in its camel-cased version of CSS
53+
- We import our StyleSheet into our components and used them by declaring a style attribute as shown instead of classes.
5454
- There are no shorthands like `border:'1px solid black'`
5555

5656
>Just like web, it is very easy to go wrong with CSS and end up with a style code that is unmanageable.
57-
Hence, we would like to introduce few basic guidelines in the next chapters, so that we can get most from our styles.
57+
Hence, we would like to introduce a few basic guidelines in the next chapters, so that we can get the most from our styles.

8-styling/8.1-theme-variables.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Theme Variables
22

3-
In general every app should have few fixed font sizes, colors, spacing, etc. This is done so that the app looks consistent across screens.
4-
Now this can be achieved by keeping a convention across the app.
5-
For example, the devs and UX designers can decide **fontSize**:
3+
In general, every app should have well defined font sizes, colors, spacing, etc. This is done so that the app looks consistent across screens.
4+
Now this can be achieved by maintaining a convention across the app.
5+
For example, the devs and UX designers can decide **fontSize** as:
66

77
- 16 - Large
88
- 14 - Medium
99
- 12 - Small
1010

11-
Hence our style sheet may look like this :
11+
Hence our stylesheet may look like this:
1212

1313
>styles.js
1414
```js
@@ -28,17 +28,17 @@ export default StyleSheet.create({
2828
});
2929
```
3030

31-
Even though this convention is good and will help you maintain consistency in small,medium and large text sizes across the app,
32-
it has few fundamental faults:
31+
Even though this convention is good and will help you maintain consistency in small, medium and large text sizes across the app, it does have a few fundamental flaws:
32+
33+
- Business/marketing team may come up with a change in the requirement that the large font size needs to be 18 instead of 16.
3334

34-
- Business/Marketing team may come up with a new requirement that now they want the large font size to be 18 instead of 16.
3535
Now as a developer, you will need to make changes in the entire app and replace every instance of fontSize:16 with fontSize:18, which kind of sucks!.
36-
- A new developer who joined the team might not be aware of all the conventions followed by the team and may create a component with fontSize other than 12,14 or 16 thus accidently destroying the code standard/convention.
36+
- A new developer who joins the team might not be aware of all the conventions followed by the team and may create a component with fontSize other than 12, 14 or 16, thus accidentaly overlooking the code standard/convention.
3737

3838

3939
Enter **`theme variables`**.
4040

41-
In order to solve the above mentioned issues we introduce a common file `theme.style.js` which will be located at `app/styles/theme.style.js`.
41+
In order to solve the above-mentioned issues, we introduce a common file `theme.style.js` which will be located at `app/styles/theme.style.js`.
4242

4343
In the theme file we define our `theme` variables as follows:
4444

@@ -57,7 +57,7 @@ export default {
5757
};
5858
```
5959

60-
and we can use it like this
60+
and we can use them like this:
6161

6262
>styles.js
6363
@@ -84,14 +84,14 @@ Now our theme file dictates the size of fonts and the primary color, etc.
8484

8585
This gives us two benefits:
8686
- If our business team now tell us to change the font sizes, we can change the theme variables at one place and it gets reflected in the entire app.
87-
- This will enable us to write multiple theme files which inturn adds basic themeing support to our app. For example: we can write two theme files - one for light theme and one for dark theme and give our app users the option to switch the themes.
87+
- This will enable us to write multiple theme files which in turn adds basic theming support to our app. For example we can write two theme files - one for a light theme and one for a dark theme and give our app users the option to switch between the themes.
8888

8989

9090
### Integrating theme variables into our NoteTaker demo
9191

92-
Enough of the theory, lets try and inculcate this concept into our demo app.
92+
Enough of theory. Let's try and inculcate this concept into our demo app.
9393

94-
First of all add the file `app/style/theme.style.js`.
94+
First of all, add the file `app/style/theme.style.js`.
9595

9696
>app/style/theme.style.js
9797

8-styling/8.2-common-styles-mixins.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
In React Native, each component is styled using inline styles. This means that it becomes slightly tricky to share styles as you can in web.
44

5-
In web we write a class
5+
In web, we write a class
66

77
```css
88
.btn {
@@ -17,7 +17,7 @@ Now if we want to apply this class to two different divs we will do so as follow
1717
<div class='btn second-btn'>Second button</div>
1818
```
1919

20-
Now same is possible in react native as follows:
20+
The same is possible in React Native in the following way:
2121

2222

2323
**`styles.js`**
@@ -54,13 +54,13 @@ import styles from './styles.js';
5454
</View>
5555
```
5656

57-
While this solves the problem only if the style objects are in the same component because in RN we do not import styles from other components (each component has its own style). But in web, we could have just reused the class anywhere (since css is global).
57+
This solves the problem only if the style objects are in the same component because in RN we do not import styles from other components (each component has its own style). But in web, we could have just reused the class anywhere (since css is global).
5858

5959
To solve the problem of reusable styles in React Native, we introduce another file named **`app/style/common.style.js`**
60-
This is where we will write our mixins / common styles.
60+
This is where we will write our mixins/common styles.
6161

6262

63-
Hence,if all the buttons in our app have similar style we can write a style with the similar properties inside the **common.style.js**
63+
Hence, if all the buttons in our app have a similar style we can write a style with similar properties inside the **common.style.js**
6464

6565
**`app/style/common.style.js`**
6666
```js
@@ -107,13 +107,13 @@ import styles from './styles.js';
107107
```
108108

109109
This way our mixins/common style file will provide us the base styles which are common across the app and we write component specific styles in the component style file.
110-
Thus allowing significant style reuse and avoiding code duplication.
110+
This allows significant style reuse and avoids code duplication.
111111

112112

113113
### Integrating common/mixin styles into our NoteTaker demo
114114

115-
Before we go into common styles , lets modify our code a bit to add another component for entering title for our note.
116-
Modify the following files as follows:
115+
Before we go into common styles, let's modify our code a bit to add another component for entering a title for our note.
116+
Modify the files as follows:
117117

118118
**`app/components/Home/Home.component.js`**
119119

@@ -208,11 +208,12 @@ Our app should now look like this:
208208
<br>
209209

210210

211-
If you notice, even though we have theme file, our style code has lot of duplicated code. Primarily because we are repeating our styling for text input and also for the heading.
211+
If you notice, even though we have a theme file, our style code has a lot of duplicated code. This is primarily because we are repeating our styling for text input and also for the heading.
212212

213-
The solution to this problem as discussed before is common styles/ mixins
213+
The solution to this problem, as discussed before, is common styles/mixins.
214+
215+
Let's create the file `common.style.js`
214216

215-
Lets create the file `common.style.js`
216217
**`app/styles/common.style.js`**
217218
```js
218219
import theme from './theme.style';
@@ -264,11 +265,11 @@ export default StyleSheet.create({
264265
});
265266
```
266267

267-
If you see our style code looks much more consice and we are resuing the styles for similar components with slight style changes.
268+
If you see our style code looks much more concise and we are resuing the styles for similar components with slight style changes.
268269
Hence, we import our base styles for the components from common.style.js and add our custom styles later on top of it.
269270
This way we reduce our work and minimize code duplication.
270271

271-
We see no change in the output but our code becomes much much cleaner.
272+
We see no change in the output but our code becomes much cleaner.
272273
<br>
273274
<div style="text-align:center">
274275
<img src="/assets/images/8/8.2/8.2-before-common.png" style="width: 40%;display:inline-block;" hspace="40">

8-styling/8.3-separating-styles-from-component.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Separating styles from component code
22

3-
Lets assume we want to build a Button component.
3+
Let's assume we want to build a Button component.
44

55
A simple button will look something like this:
66

@@ -77,10 +77,10 @@ export default StyleSheet.create({
7777
});
7878
```
7979

80-
This has few benefits:
80+
This has a few benefits:
8181

8282
- This makes the component code much cleaner. The style is present in its own separate file.
83-
- This allows you two write two different style files for android and ios when needed. Thus you keep the same functionality but the button looks different as needed in two different platforms.
83+
- This allows you to write two different style files for Android and iOS if required. Thus, you can keep the same functionality but the button can look different based on the requirement for the platform.
8484

8585
For example:
8686

@@ -140,10 +140,10 @@ export default StyleSheet.create({
140140
});
141141
```
142142

143-
Thus by simply moving the styles into a separate file we could achieve a style code that behaves exactly the way we needed in different platforms. Also, we could reuse the component logic.
143+
Thus by simply moving the styles into a separate file, we could achieve a style code that behaves exactly the way we needed on different platforms. Also, we could reuse the component logic.
144144

145145

146146

147147
### Conclusion
148148

149-
In Web, we have lots of production grade tools like Sass, Less, etc which allows us to write modular, scoped CSS which are easier to manage. These tools then take care of building all our style code into one cohesive stylesheet for our entire application. In React Native, we must think of styling in a slightly different manner. By doing some pre-planning and organization before writing the code for the components, we can reduce code duplication and unnecessary confusions. It takes a bit of getting used to, but styling in React Native is as powerful as the CSS for the web and is the fastest way to build multiplatform native applications.
149+
In Web, we have lots of production grade tools like Sass, Less, etc which allow us to write modular, scoped CSS which is easier to manage. These tools then take care of building all our style code into one cohesive stylesheet for our entire application. In React Native, we must think of styling in a slightly different manner. By doing some pre-planning and organization before writing the code for the components, we can reduce code duplication and unnecessary confusions. It takes a bit of getting used to, but styling in React Native is as powerful as CSS for the web and is the fastest way to build multi-platform native applications.

0 commit comments

Comments
 (0)