Skip to content

Commit ad51a8b

Browse files
dabbottMorgan Pretty
authored andcommitted
Add docs pages for basics: Dimensions and Layout
Summary: These pages should sufficiently give a beginner enough information to make most layouts in React Native. They should go after the basics-style page, whenever that is ready. Having a single page for Layout was too much, so I split it into two: Dimensions and Layout. ![dimensions react native a framework for building native apps using react](https://cloud.githubusercontent.com/assets/1198882/16311045/c6918b64-3923-11e6-8cc9-daeda9eb40e6.png) ![layout react native a framework for building native apps using react](https://cloud.githubusercontent.com/assets/1198882/16310233/9a66405a-3920-11e6-9ef6-1594f7228e83.png) lacker Closes facebook#8364 Differential Revision: D3477147 Pulled By: lacker fbshipit-source-id: 1ef31ac0a64e43166a7581b38fa8263282672eeb
1 parent e77758c commit ad51a8b

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed

docs/Basics-Component-ListView.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: ListView
44
layout: docs
55
category: The Basics
66
permalink: docs/basics-component-listview.html
7-
next: basics-network
7+
next: basics-dimensions
88
---
99

1010
On mobile devices, lists are a core element in many applications. The [`ListView`](/react-native/docs/listview.html#content) component is a special type of [`View`](/react-native/docs/basics-component-view.html) that displays a *vertically* scrolling list of changing, but similarly structured, data.

docs/Basics-Dimensions.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
id: basics-dimensions
3+
title: Dimensions
4+
layout: docs
5+
category: The Basics
6+
permalink: docs/basics-dimensions.html
7+
next: basics-layout
8+
---
9+
10+
A component's dimensions determine its size on the screen.
11+
12+
#### Fixed Dimensions
13+
14+
The simplest way to set the dimensions of a component is by adding a fixed `width` and `height` to style. All dimensions in React Native are unitless, and represent density-independent pixels.
15+
16+
```ReactNativeWebPlayer
17+
import React from 'react';
18+
import { AppRegistry, View } from 'react-native';
19+
20+
class AwesomeProject {
21+
render() {
22+
return (
23+
<View>
24+
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
25+
<View style={{width: 100, height: 100, backgroundColor: 'skyblue'}} />
26+
<View style={{width: 150, height: 150, backgroundColor: 'steelblue'}} />
27+
</View>
28+
);
29+
}
30+
};
31+
32+
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);
33+
```
34+
35+
Setting dimensions this way is common for components that should always render at exactly the same size, regardless of screen dimensions.
36+
37+
#### Flex Dimensions
38+
39+
Use `flex` in a component's style to have the component expand and shrink dynamically based on available space. Normally you will use `flex: 1`, which tells a component to fill all available space, shared evenly amongst each other component with the same parent. The larger the `flex` given, the higher the ratio of space a component will take compared to its siblings.
40+
41+
> A component can only expand to fill available space if its parent has dimensions greater than 0. If a parent does not have either a fixed `width` and `height` or `flex`, the parent will have dimensions of 0 and the `flex` children will not be visible.
42+
43+
```ReactNativeWebPlayer
44+
import React from 'react';
45+
import { AppRegistry, View } from 'react-native';
46+
47+
class AwesomeProject {
48+
render() {
49+
return (
50+
// Try removing the `flex: 1` on the parent View.
51+
// The parent will not have dimensions, so the children can't expand.
52+
// What if you add `height: 300` instead of `flex: 1`?
53+
<View style={{flex: 1}}>
54+
<View style={{flex: 1, backgroundColor: 'powderblue'}} />
55+
<View style={{flex: 2, backgroundColor: 'skyblue'}} />
56+
<View style={{flex: 3, backgroundColor: 'steelblue'}} />
57+
</View>
58+
);
59+
}
60+
};
61+
62+
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);
63+
```

docs/Basics-Layout.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
id: basics-layout
3+
title: Layout
4+
layout: docs
5+
category: The Basics
6+
permalink: docs/basics-layout.html
7+
next: basics-network
8+
---
9+
10+
A component can specify the layout of its children using the flexbox algorithm. Flexbox is designed to provide a consistent layout on different screen sizes.
11+
12+
You will normally use a combination of `flexDirection`, `alignItems`, and `justifyContent` to achieve the right layout.
13+
14+
> Flexbox works the same way in React Native as it does in CSS on the web, with a few exceptions. The most notable one: the defaults are different, with `flexDirection` defaulting to `column` instead of `row`, and `alignItems` defaulting to `stretch` instead of `flex-start`.
15+
16+
#### Flex Direction
17+
18+
Adding `flexDirection` to a component's `style` determines the **primary axis** of its layout. Should the children be organized horizontally (`row`) or vertically (`column`)? The default is `column`.
19+
20+
```ReactNativeWebPlayer
21+
import React from 'react';
22+
import { AppRegistry, View } from 'react-native';
23+
24+
class AwesomeProject {
25+
render() {
26+
return (
27+
// Try setting `flexDirection` to `column`.
28+
<View style={{flex: 1, flexDirection: 'row'}}>
29+
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
30+
<View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />
31+
<View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />
32+
</View>
33+
);
34+
}
35+
};
36+
37+
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);
38+
```
39+
40+
#### Justify Content
41+
42+
Adding `justifyContent` to a component's style determines the **distribution** of children along the **primary axis**. Should children be distributed at the start, the center, the end, or spaced evenly? Available options are `flex-start`, `center`, `flex-end`, `space-around`, and `space-between`.
43+
44+
```ReactNativeWebPlayer
45+
import React from 'react';
46+
import { AppRegistry, View } from 'react-native';
47+
48+
class AwesomeProject {
49+
render() {
50+
return (
51+
// Try setting `justifyContent` to `center`.
52+
// Try setting `flexDirection` to `row`.
53+
<View style={{
54+
flex: 1,
55+
flexDirection: 'column',
56+
justifyContent: 'space-between',
57+
}}>
58+
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
59+
<View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />
60+
<View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />
61+
</View>
62+
);
63+
}
64+
};
65+
66+
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);
67+
```
68+
69+
#### Align Items
70+
71+
Adding `alignItems` to a component's style determines the **alignment** of children along the **secondary axis** (if the primary axis is `row`, then the secondary is `column`, and vice versa). Should children be aligned at the start, the center, the end, or stretched to fill? Available options are `flex-start`, `center`, `flex-end`, and `stretch`.
72+
73+
> For `stretch` to have an effect, children must not have a fixed dimension along the secondary axis. In the following example, setting `alignItems: stretch` does nothing until the `width: 50` is removed from the children.
74+
75+
```ReactNativeWebPlayer
76+
import React from 'react';
77+
import { AppRegistry, View } from 'react-native';
78+
79+
class AwesomeProject {
80+
render() {
81+
return (
82+
// Try setting `alignItems` to 'flex-start'
83+
// Try setting `justifyContent` to `flex-end`.
84+
// Try setting `flexDirection` to `row`.
85+
<View style={{
86+
flex: 1,
87+
flexDirection: 'column',
88+
justifyContent: 'center',
89+
alignItems: 'center',
90+
}}>
91+
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
92+
<View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />
93+
<View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />
94+
</View>
95+
);
96+
}
97+
};
98+
99+
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);
100+
```
101+
102+
#### API Reference
103+
104+
We've covered the basics, but there are many other styles you may need for layouts. The full list is available [here](./docs/flexbox.html).

0 commit comments

Comments
 (0)