|
1 | 1 | --- |
2 | 2 | id: apis/StyleSheet |
3 | 3 | title: StyleSheet |
4 | | -wip: true |
| 4 | +officialDoc: https://facebook.github.io/react-native/docs/stylesheet |
5 | 5 | --- |
6 | 6 |
|
| 7 | +`StyleSheet` mainly has a method to [create](#create) an object containing many |
| 8 | +styles, similar to a CSS StyleSheet. This is helpful to keep style definitions |
| 9 | +at fewer locations, away from the render function. Separating concerns in your |
| 10 | +code in this manner should help readability. Named styles might also help |
| 11 | +differentiate between your components. Finally, allowing components to refer to |
| 12 | +styles by ID, it also reduces the amount of data transferred over the JS bridge. |
| 13 | + |
| 14 | +There is also a [`flatten`](#flatten) method to merge multiple styles into a single style |
| 15 | +and several [constants](#constants) for commonly used style definitions. |
| 16 | + |
| 17 | +## Methods |
| 18 | + |
| 19 | +### `create` |
| 20 | + |
| 21 | +Takes and returns `Js.t('a)` objects, with `string` keys and `Style.t` values. |
| 22 | +Individual styles can be accessed using `##`, followed by the name specified as |
| 23 | +the key. |
| 24 | + |
| 25 | +Note that using `Style.array` or `Style.list` to pass `array(Style.t)` or |
| 26 | +`list(Style.t)` is illegal within a `StyleSheet`, even those are valid `style` |
| 27 | +props for components. You may, howevever, use the [`flatten`](#flatten) method |
| 28 | +to convert `array(Style.t)` into a valid `Style.t` object. |
| 29 | + |
| 30 | +```reason |
| 31 | +create: Js.t('a) => Js.t('a) |
| 32 | +``` |
| 33 | + |
| 34 | +### `flatten` |
| 35 | + |
| 36 | +Takes an array of styles (of type `Style.t`) and returns a single style (also of |
| 37 | +type `Style.t`). Creates a `Style.t` object which is valid in a `StyleSheet`. |
| 38 | + |
| 39 | +```reason |
| 40 | +flatten: array(Style.t) => Style.t |
| 41 | +``` |
| 42 | + |
| 43 | +## Constants |
| 44 | + |
| 45 | +### `hairlineWidth` |
| 46 | + |
| 47 | +To ensure the resulting line will look sharp, this specifies an integer number |
| 48 | +of pixels which should approximate the standard thickness of a thin line on the |
| 49 | +platform given the screen density. |
| 50 | + |
| 51 | +```reason |
| 52 | +hairlineWidth: float |
| 53 | +``` |
| 54 | + |
| 55 | +### `absoluteFill` |
| 56 | + |
| 57 | +This is the style |
| 58 | + |
| 59 | +``` |
| 60 | +{ |
| 61 | + position: 'absolute', |
| 62 | + left: 0, |
| 63 | + right: 0, |
| 64 | + top: 0, |
| 65 | + bottom: 0 |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +which is frequently used to create overlays. A common use is to add these props |
| 70 | +to a style using the spread operator (`...`), but as ReasonML does not allow the |
| 71 | +operator when fields are not explicitly set, you may use the `flatten` method |
| 72 | +instead. |
| 73 | + |
7 | 74 | ```reason |
8 | | -[@bs.module "react-native"] [@bs.scope "StyleSheet"] |
9 | | -external hairlineWidth: float = ""; |
10 | | -[@bs.module "react-native"] [@bs.scope "StyleSheet"] |
11 | | -external absoluteFill: Style.t = ""; |
12 | | -[@bs.module "react-native"] [@bs.scope "StyleSheet"] |
13 | | -external absoluteFillObject: Style.t = ""; |
14 | | -
|
15 | | -[@bs.module "react-native"] [@bs.scope "StyleSheet"] |
16 | | -external create: Js.t('a) => Js.t('a) = ""; |
17 | | -[@bs.module "react-native"] [@bs.scope "StyleSheet"] |
18 | | -external flatten: array(Style.t) => Style.t = ""; |
| 75 | +absoluteFill: Style.t |
| 76 | +``` |
| 77 | + |
| 78 | +### `absoluteFillObject` |
| 79 | + |
| 80 | +This is identical to `absoluteFill` when used with pure functions. |
| 81 | + |
| 82 | +```reason |
| 83 | +absoluteFillObject: Style.t |
| 84 | +``` |
| 85 | + |
| 86 | +## Example |
| 87 | + |
| 88 | +Please also see the [example](../Style/#style-example) in documentation of the |
| 89 | +`Style` API. |
| 90 | + |
| 91 | +```reason |
| 92 | +open ReactNative; |
| 93 | +
|
| 94 | +let borderStyle = Style.(style(~borderWidth=StyleSheet.hairlineWidth, ())); |
| 95 | +
|
| 96 | +let styles = |
| 97 | + Style.( |
| 98 | + StyleSheet.create({ |
| 99 | + // style may be defined inline |
| 100 | + "container": style(~flex=1., ~flexDirection=`column, ()), |
| 101 | + "screen": style(~width=pt(windowWidth), ()), |
| 102 | + // or already defined elsewhere |
| 103 | + "borderStyle": borderStyle, |
| 104 | + "overlay": StyleSheet.absoluteFill, |
| 105 | + }) |
| 106 | + ); |
19 | 107 |
|
| 108 | +let flatStyle = StyleSheet.flatten([|styles##container, styles##screen|]); |
20 | 109 | ``` |
0 commit comments