Skip to content

Commit 2b2529b

Browse files
authored
Merge pull request rescript-lang#34 from reason-association/add-dom-api-docs
Add initial Dom api
2 parents 963fd2a + 6a3d5a2 commit 2b2529b

File tree

7 files changed

+1086
-0
lines changed

7 files changed

+1086
-0
lines changed

common/App.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ let default = (props: props): React.element => {
8484
| (_, Some("js")) => <JsDocsLayout.Docs> content </JsDocsLayout.Docs>
8585
| (_, Some("belt")) =>
8686
<BeltDocsLayout.Docs> content </BeltDocsLayout.Docs>
87+
| (_, Some("dom")) => <DomDocsLayout.Docs> content </DomDocsLayout.Docs>
8788
| _ => React.null
8889
}
8990
// common routes

layouts/ApiOverviewLayout.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Docs = {
1919
items: [|
2020
{name: "Js Module", href: "/apis/latest/js"},
2121
{name: "Belt Stdlib", href: "/apis/latest/belt"},
22+
{name: "Dom", href: "/apis/latest/dom"},
2223
|],
2324
},
2425
|];

layouts/DomDocsLayout.re

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
module Link = Next.Link;
2+
3+
// Structure defined by `scripts/extract-indices.js`
4+
let indexData:
5+
Js.Dict.t({
6+
.
7+
"moduleName": string,
8+
"headers":
9+
array({
10+
.
11+
"name": string,
12+
"href": string,
13+
}),
14+
}) = [%raw
15+
"require('../index_data/dom_api_index.json')"
16+
];
17+
18+
module Sidebar = SidebarLayout.Sidebar;
19+
module UrlPath = SidebarLayout.UrlPath;
20+
module NavItem = Sidebar.NavItem;
21+
module Category = Sidebar.Category;
22+
23+
let moduleNavs = [|
24+
NavItem.{name: "Storage", href: "/apis/latest/dom/storage"},
25+
NavItem.{name: "Storage2", href: "/apis/latest/dom/storage2"},
26+
|];
27+
28+
let categories = [|Category.{name: "Modules", items: moduleNavs}|];
29+
30+
module Docs = {
31+
[@react.component]
32+
let make = (~components=ApiMarkdown.default, ~children) => {
33+
let router = Next.Router.useRouter();
34+
let route = router.route;
35+
36+
// Gather data for the CollapsibleSection
37+
let headers =
38+
Belt.Option.(
39+
Js.Dict.get(indexData, route)
40+
->map(data => {
41+
data##headers
42+
->Belt.Array.map(header => (header##name, "#" ++ header##href))
43+
})
44+
->getWithDefault([||])
45+
);
46+
47+
let moduleName =
48+
Belt.Option.(
49+
Js.Dict.get(indexData, route)
50+
->map(data => data##moduleName)
51+
->getWithDefault("?")
52+
);
53+
54+
let (isSidebarOpen, setSidebarOpen) = React.useState(_ => false);
55+
let toggleSidebar = () => setSidebarOpen(prev => !prev);
56+
57+
let urlPath = UrlPath.parse(~base="/apis", route);
58+
59+
let breadcrumbs =
60+
Belt.Option.map(
61+
urlPath,
62+
v => {
63+
let {UrlPath.version} = v;
64+
let prefix =
65+
UrlPath.[{name: "Modules", href: "/apis/" ++ version}];
66+
UrlPath.toBreadCrumbs(~prefix, v);
67+
},
68+
);
69+
70+
let toplevelNav =
71+
switch (urlPath) {
72+
| Some(urlPath) =>
73+
let version = UrlPath.(urlPath.version);
74+
let backHref = Some(UrlPath.fullUpLink(urlPath));
75+
<Sidebar.ToplevelNav title="Dom" version ?backHref />;
76+
| None => React.null
77+
};
78+
79+
// Todo: We need to introduce router state to be able to
80+
// listen to anchor changes (#get, #map,...)
81+
let preludeSection = <Sidebar.CollapsibleSection headers moduleName />;
82+
83+
let sidebar =
84+
<Sidebar
85+
isOpen=isSidebarOpen
86+
toggle=toggleSidebar
87+
categories
88+
route={router.route}
89+
toplevelNav
90+
preludeSection
91+
/>;
92+
93+
let pageTitle =
94+
switch (breadcrumbs) {
95+
| Some([_, {name}]) => name
96+
| Some([_, _, {name}]) => "Dom." ++ name
97+
| _ => "Dom"
98+
};
99+
100+
<SidebarLayout
101+
metaTitle={pageTitle ++ " | ReScript API"}
102+
theme=`Reason
103+
components
104+
sidebarState=(isSidebarOpen, setSidebarOpen)
105+
sidebar
106+
?breadcrumbs>
107+
children
108+
</SidebarLayout>;
109+
};
110+
};
111+
112+
module Prose = {
113+
[@react.component]
114+
let make = (~children) => {
115+
<Docs components=Markdown.default> children </Docs>;
116+
};
117+
};

0 commit comments

Comments
 (0)