Skip to content

Commit cf4370f

Browse files
authored
Platform docs + add web as a safe platform (#410)
* Platform docs + add web as a safe platform * Update config.yml * Update config.yml * Update Platform.md * Update Platform.md
1 parent 4b241a6 commit cf4370f

10 files changed

Lines changed: 96 additions & 15 deletions

File tree

‎.circleci/config.yml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ aliases:
77
paths:
88
- ~/.cache/yarn
99
key: yarn-cache-{{ checksum "package.json" }}-{{ checksum "yarn.lock" }}
10-
1110
- &opam-cache-restore
1211
keys:
1312
- opam-cache-{{ checksum ".circleci/config.yml" }}
14-
- opam-cache
1513
- &opam-cache-save
1614
key: opam-cache-{{ checksum ".circleci/config.yml" }}
1715
paths:

‎bs-react-native-next/src/apis/AppRegistry.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ wip: true
99
external registerComponent: (string, unit => React.component('a)) => unit =
1010
"registerComponent";
1111
12-
/* react-native-web */
12+
// react-native-web
1313
type app = {
1414
.
1515
"element": React.element,

‎bs-react-native-next/src/apis/AppRegistry.re‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external registerComponent: (string, unit => React.component('a)) => unit =
33
"registerComponent";
44

5-
/* react-native-web */
5+
// react-native-web
66
type app = {
77
.
88
"element": React.element,
Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,73 @@
11
---
22
id: apis/Platform
33
title: Platform
4-
wip: true
4+
officialDoc: https://facebook.github.io/react-native/docs/platform-specific-code
55
---
66

7+
## `Platform.os`
8+
9+
React Native provides a module that detects the platform in which the app is
10+
running. You can use the detection logic to implement platform-specific code.
11+
Use this option when only small parts of a component are platform-specific.
12+
713
```reason
8-
type os = pri string;
14+
if (Platform.os === Platform.ios) {
15+
// your code
16+
}
17+
```
18+
19+
`Platform.os` can be
920

10-
[@bs.module "react-native"] [@bs.scope "Platform"] external os: os = "OS";
21+
- `Platform.ios` when running on iOS
22+
- `Platform.android` when running on Android.
23+
- `Platform.web` when running on the web (via
24+
[`react-native-web`](https://github.com/necolas/react-native-web)).
1125

12-
[@bs.inline "ios"]
13-
let ios: os;
26+
If you need an unsupported platform, you can use `Platform.unsafe(string)`.
1427

15-
[@bs.inline "android"]
16-
let android: os;
28+
For conditional style depending on the platform, you can do the following:
29+
30+
```reason
31+
let styles =
32+
StyleSheet.create(
33+
Style.{
34+
"wrapper":
35+
style(
36+
~width=Size.pct(Platform.os == Platform.ios ? 100. : 200.),
37+
(),
38+
),
39+
},
40+
);
1741
```
42+
43+
## `Platform.select`
44+
45+
_⚠️ Unsupported._
46+
47+
This feature isn't relevant with ReasonML. Instead you can do the following:
48+
49+
```reason
50+
let styles =
51+
StyleSheet.create(
52+
Style.{
53+
"wrapper":
54+
style(
55+
~width=
56+
(
57+
switch (Platform.os) {
58+
| os when os == Platform.ios => 100.
59+
| os when os == Platform.android => 200.
60+
| os when os == Platform.web => 250.
61+
| os when os == Platform.unsafe("windows") => 300.
62+
| _ => 150.
63+
}
64+
)
65+
->Size.pct,
66+
(),
67+
),
68+
},
69+
);
70+
```
71+
72+
Also since spreading things in ReasonML is not supported (because of the
73+
unsafety aspect), it's even less relevant to have this binding.

‎bs-react-native-next/src/apis/Platform.re‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ let ios = "ios";
77

88
[@bs.inline]
99
let android = "android";
10+
11+
// react-native-web
12+
[@bs.inline]
13+
let web = "web";
14+
15+
external unsafe: string => os = "%identity";

‎bs-react-native-next/src/apis/Platform.rei‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ type os = pri string;
66
let ios: os;
77

88
[@bs.inline "android"]
9-
let android: os;
9+
let android: os;
10+
11+
// react-native-web
12+
[@bs.inline "web"]
13+
let web: os;
14+
15+
external unsafe: string => os = "%identity";

‎website/scripts/PrepareMdToJson.re‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Path.join([|sourcePath, "**/*.md"|])
150150
"wip": front##attributes##wip,
151151
"autoLinkToOfficialDoc":
152152
front##attributes##autoLinkToOfficialDoc,
153+
"officialDoc": front##attributes##officialDoc,
153154
"body": Remarkable.render(remarkable, front##body),
154155
}
155156
->Js.Json.stringifyAny;

‎website/scripts/Prerender.re‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ let file = (file: FsUtils.t, modulesIndex: array(string)) => {
7474
| Js.Json.JSONFalse => Some(false)
7575
| _ => failwith("Expected a boolean for `wip`")
7676
}
77-
| None => None // failwith("Expected an `wip` property")
77+
| None => None
7878
};
7979
let autoLinkToOfficialDoc =
8080
switch (Js.Dict.get(value, "autoLinkToOfficialDoc")) {
@@ -84,7 +84,16 @@ let file = (file: FsUtils.t, modulesIndex: array(string)) => {
8484
| Js.Json.JSONFalse => Some(false)
8585
| _ => failwith("Expected a boolean for `autoLinkToOfficialDoc`")
8686
}
87-
| None => None // failwith("Expected an `autoLinkToOfficialDoc` property")
87+
| None => None
88+
};
89+
let officialDoc =
90+
switch (Js.Dict.get(value, "officialDoc")) {
91+
| Some(v) =>
92+
switch (Js.Json.classify(v)) {
93+
| Js.Json.JSONString(v) => Some(v)
94+
| _ => failwith("Expected a string for `officialDoc`")
95+
}
96+
| None => None
8897
};
8998
let body =
9099
switch (Js.Dict.get(value, "body")) {
@@ -99,6 +108,7 @@ let file = (file: FsUtils.t, modulesIndex: array(string)) => {
99108
id,
100109
title,
101110
wip,
111+
officialDoc,
102112
autoLinkToOfficialDoc,
103113
body,
104114
modulesIndex,

‎website/src/Consts.re‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ let sections = [
159159
("PanResponder", api ++ "PanResponder/"),
160160
("PermissionsAndroid", api ++ "PermissionsAndroid/"),
161161
("PixelRatio", api ++ "PixelRatio/"),
162+
("Platform", api ++ "Platform/"),
162163
("PushNotificationIOS", api ++ "PushNotificationIOS/"),
163164
("Settings", api ++ "Settings/"),
164165
("Shadow Props", api ++ "ShadowProps/"),

‎website/src/components/PageContent.re‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type pageData = {
66
id: string,
77
title: string,
88
wip: option(bool),
9+
officialDoc: option(string),
910
autoLinkToOfficialDoc: option(bool),
1011
body: string,
1112
modulesIndex: array(string),
@@ -70,7 +71,9 @@ let make = (~pageData) => {
7071
Js.log3("@todo", pageData.title, "docs is still WIP");
7172
};
7273
let officialDocHref =
73-
if (!pageData.autoLinkToOfficialDoc->Option.getWithDefault(true)) {
74+
if (pageData.officialDoc->Option.isSome) {
75+
Some(pageData.officialDoc->Option.getWithDefault(""));
76+
} else if (!pageData.autoLinkToOfficialDoc->Option.getWithDefault(true)) {
7477
None;
7578
} else if (pageData.id
7679
|> Js.String.startsWith("apis/")

0 commit comments

Comments
 (0)