Improve style docs

This commit is contained in:
Christopher Chedeau 2015-03-25 11:36:37 -07:00
parent d6a921e94d
commit 4eab005009
2 changed files with 14 additions and 8 deletions

View File

@ -7,7 +7,11 @@ permalink: docs/style.html
next: nativemodulesios
---
## Declaring Styles
React Native doesn't implement CSS but instead relies on JavaScript to let you style your application. This has been a controversial decision and you can read through those slides for the rationale behind it.
<script async class="speakerdeck-embed" data-id="2e15908049bb013230960224c1b4b8bd" data-ratio="2" src="//speakerdeck.com/assets/embed.js"></script>
## Declare Styles
The way to declare styles in React Native is the following:
@ -29,6 +33,8 @@ var styles = StyleSheet.create({
`StyleSheet.create` construct is optional but provides some key advantages. It ensures that the values are **immutable** and **opaque** by transforming them into plain numbers that reference an internal table. By putting it at the end of the file, you also ensure that they are only created once for the application and not on every render.
All the attribute names and values are a subset of what works on the web. For layout, React Native implements [Flexbox](/react-native/docs/flexbox.html).
## Using Styles
All the core components accept a style attribute
@ -41,20 +47,20 @@ All the core components accept a style attribute
and also accepts an array of styles
```javascript
<View style={[style.base, style.background]} />
<View style={[styles.base, styles.background]} />
```
The behavior is the same as `Object.assign`: in case of conflicting values, the one from the right-most element will have precedence and falsy values like `false`, `undefined` and `null` will be ignored. A common pattern is to conditionally add a style based on some condition.
```javascript
<View style={[style.base, this.state.active && style.active]} />
<View style={[styles.base, this.state.active && styles.active]} />
```
Finally, if you really have to, you can also create style objects in render, but they are highly discouraged. Put them last in the array definition.
```javascript
<View
style={[style.base, {
style={[styles.base, {
width: this.state.width,
height: this.state.width * this.state.aspectRatio
}]}
@ -63,13 +69,13 @@ Finally, if you really have to, you can also create style objects in render, but
## Pass Styles Around
In order to let a call site customize the style of your component children, you can pass styles around. Use `View.stylePropType` and `Text.stylePropType` in order to make sure only styles are being passed.
In order to let a call site customize the style of your component children, you can pass styles around. Use `View.propTypes.style` and `Text.propTypes.style` in order to make sure only styles are being passed.
```javascript
var List = React.createClass({
propTypes: {
style: View.stylePropType,
elementStyle: View.stylePropType,
style: View.propTypes.style,
elementStyle: View.propTypes.style,
},
render: function() {
return (

View File

@ -356,7 +356,7 @@ Lexer.prototype.token = function(src, top) {
type: this.options.sanitize
? 'paragraph'
: 'html',
pre: cap[1] === 'pre' || cap[1] === 'script',
pre: cap[1] === 'pre',
text: cap[0]
});
continue;