Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add percentage dimensions section to the Height and Width page #2101

Merged
merged 2 commits into from
Aug 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions docs/height-and-width.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ import React from 'react';
import { View } from 'react-native';

const FixedDimensionsBasics = () => {
return (
<View>
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
<View style={{width: 100, height: 100, backgroundColor: 'skyblue'}} />
<View style={{width: 150, height: 150, backgroundColor: 'steelblue'}} />
</View>
);
return (
<View>
<View style={{
width: 50, height: 50, backgroundColor: 'powderblue'
}} />
<View style={{
width: 100, height: 100, backgroundColor: 'skyblue'
}} />
<View style={{
width: 150, height: 150, backgroundColor: 'steelblue'
}} />
</View>
);
};

export default FixedDimensionsBasics;
Expand All @@ -32,26 +38,55 @@ Setting dimensions this way is common for components that should always render a

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 other components with the same parent. The larger the `flex` given, the higher the ratio of space a component will take compared to its siblings.

> 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.
> 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.

```SnackPlayer name=Flex%20Dimensions
import React from 'react';
import { View } from 'react-native';

const FlexDimensionsBasics = () => {
return (
// Try removing the `flex: 1` on the parent View.
// The parent will not have dimensions, so the children can't expand.
// What if you add `height: 300` instead of `flex: 1`?
<View style={{flex: 1}}>
<View style={{flex: 1, backgroundColor: 'powderblue'}} />
<View style={{flex: 2, backgroundColor: 'skyblue'}} />
<View style={{flex: 3, backgroundColor: 'steelblue'}} />
</View>
);
return (
// Try removing the `flex: 1` on the parent View.
// The parent will not have dimensions, so the children can't expand.
// What if you add `height: 300` instead of `flex: 1`?
<View style={{ flex: 1 }}>
<View style={{ flex: 1, backgroundColor: 'powderblue' }} />
<View style={{ flex: 2, backgroundColor: 'skyblue' }} />
<View style={{ flex: 3, backgroundColor: 'steelblue' }} />
</View>
);
};

export default FlexDimensionsBasics;
```

After you can control a component's size, the next step is to [learn how to lay it out on the screen](flexbox.md).

## Percentage Dimensions

If you want to fill a certain portion of the screen, but you _don't_ want to use the `flex` layout, you _can_ use **percentage values** in the component's style. Similar to flex dimensions, percentage dimensions require parent with a defined size.

```SnackPlayer name=Percentage%20Dimensions
import React from 'react';
import { View } from 'react-native';

const PercentageDimensionsBasics = () => {
// Try removing the `height: '100%'` on the parent View.
// The parent will not have dimensions, so the children can't expand.
return (
<View style={{ height: '100%' }}>
<View style={{
height: '15%', backgroundColor: 'powderblue'
}} />
<View style={{
width: '66%', height: '35%', backgroundColor: 'skyblue'
}} />
<View style={{
width: '33%', height: '50%', backgroundColor: 'steelblue'
}} />
</View>
);
};

export default PercentageDimensionsBasics;
```