Skip to content

Commit 69a83fb

Browse files
sgnyMoOx
authored andcommitted
Add documentation for StyleSheet API (#520)
1 parent 2abfd29 commit 69a83fb

2 files changed

Lines changed: 103 additions & 14 deletions

File tree

‎reason-react-native/src/apis/Style.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ the last style in the array has precedence, so you can use this to mix & inherit
1515
styles.
1616

1717
⚠️ _Note that when a component grows in complexity, it is often cleaner to use
18-
[`StyleSheet.create`](/bs-react-native/en/docs/apis/StyleSheet/) to define
19-
several styles in one place_.
18+
[`StyleSheet.create`](../StyleSheet/#create) to define several styles in one
19+
place_.
2020

2121
We have made different style constructors because React Native have various
2222
components that accept different styles props. For example `View` doesn't accept
Lines changed: 101 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,109 @@
11
---
22
id: apis/StyleSheet
33
title: StyleSheet
4-
wip: true
4+
officialDoc: https://facebook.github.io/react-native/docs/stylesheet
55
---
66

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+
774
```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+
);
19107
108+
let flatStyle = StyleSheet.flatten([|styles##container, styles##screen|]);
20109
```

0 commit comments

Comments
 (0)