-
-
Notifications
You must be signed in to change notification settings - Fork 149
Add documentation for StyleSheet API #520
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,109 @@ | ||
--- | ||
id: apis/StyleSheet | ||
title: StyleSheet | ||
wip: true | ||
officialDoc: https://facebook.github.io/react-native/docs/stylesheet | ||
--- | ||
|
||
`StyleSheet` mainly has a method to [create](#create) an object containing many | ||
styles, similar to a CSS StyleSheet. This is helpful to keep style definitions | ||
at fewer locations, away from the render function. Separating concerns in your | ||
code in this manner should help readability. Named styles might also help | ||
differentiate between your components. Finally, allowing components to refer to | ||
styles by ID, it also reduces the amount of data transferred over the JS bridge. | ||
|
||
There is also a [`flatten`](#flatten) method to merge multiple styles into a single style | ||
and several [constants](#constants) for commonly used style definitions. | ||
|
||
## Methods | ||
|
||
### `create` | ||
|
||
Takes and returns `Js.t('a)` objects, with `string` keys and `Style.t` values. | ||
Individual styles can be accessed using `##`, followed by the name specified as | ||
the key. | ||
|
||
Note that using `Style.array` or `Style.list` to pass `array(Style.t)` or | ||
`list(Style.t)` is illegal within a `StyleSheet`, even those are valid `style` | ||
props for components. You may, howevever, use the [`flatten`](#flatten) method | ||
to convert `array(Style.t)` into a valid `Style.t` object. | ||
|
||
```reason | ||
create: Js.t('a) => Js.t('a) | ||
``` | ||
|
||
### `flatten` | ||
|
||
Takes an array of styles (of type `Style.t`) and returns a single style (also of | ||
type `Style.t`). Creates a `Style.t` object which is valid in a `StyleSheet`. | ||
|
||
```reason | ||
flatten: array(Style.t) => Style.t | ||
``` | ||
|
||
## Constants | ||
|
||
### `hairlineWidth` | ||
|
||
To ensure the resulting line will look sharp, this specifies an integer number | ||
of pixels which should approximate the standard thickness of a thin line on the | ||
platform given the screen density. | ||
|
||
```reason | ||
hairlineWidth: float | ||
``` | ||
|
||
### `absoluteFill` | ||
|
||
This is the style | ||
|
||
``` | ||
{ | ||
position: 'absolute', | ||
left: 0, | ||
right: 0, | ||
top: 0, | ||
bottom: 0 | ||
} | ||
``` | ||
|
||
which is frequently used to create overlays. A common use is to add these props | ||
to a style using the spread operator (`...`), but as ReasonML does not allow the | ||
operator when fields are not explicitly set, you may use the `flatten` method | ||
instead. | ||
|
||
```reason | ||
[@bs.module "react-native"] [@bs.scope "StyleSheet"] | ||
external hairlineWidth: float = ""; | ||
[@bs.module "react-native"] [@bs.scope "StyleSheet"] | ||
external absoluteFill: Style.t = ""; | ||
[@bs.module "react-native"] [@bs.scope "StyleSheet"] | ||
external absoluteFillObject: Style.t = ""; | ||
|
||
[@bs.module "react-native"] [@bs.scope "StyleSheet"] | ||
external create: Js.t('a) => Js.t('a) = ""; | ||
[@bs.module "react-native"] [@bs.scope "StyleSheet"] | ||
external flatten: array(Style.t) => Style.t = ""; | ||
absoluteFill: Style.t | ||
``` | ||
|
||
### `absoluteFillObject` | ||
|
||
This is identical to `absoluteFill` when used with pure functions. | ||
sgny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```reason | ||
absoluteFillObject: Style.t | ||
``` | ||
|
||
## Example | ||
|
||
Please also see the [example](../Style/#style-example) in documentation of the | ||
`Style` API. | ||
|
||
```reason | ||
open ReactNative; | ||
|
||
let borderStyle = Style.(style(~borderWidth=StyleSheet.hairlineWidth, ())); | ||
|
||
let styles = | ||
Style.( | ||
StyleSheet.create({ | ||
// style may be defined inline | ||
"container": style(~flex=1., ~flexDirection=`column, ()), | ||
"screen": style(~width=pt(windowWidth), ()), | ||
// or already defined elsewhere | ||
"borderStyle": borderStyle, | ||
"overlay": StyleSheet.absoluteFill, | ||
}) | ||
); | ||
|
||
let flatStyle = StyleSheet.flatten([|styles##container, styles##screen|]); | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question as https://github.com/reasonml-community/bs-react-native/pull/519#discussion_r289291691
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we revisit this issue later, but I don't think there is a way to exclude
array(Style.t)
without a lot of pain elsewhere.