Skip to content

Commit e442bb0

Browse files
committed
fix api docs ui
1 parent a0b68b0 commit e442bb0

File tree

1 file changed

+69
-42
lines changed

1 file changed

+69
-42
lines changed

src/ApiDocs.res

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,58 @@ type item =
4141
detail: Js.Null.t<detail>,
4242
})
4343

44+
module RightSidebar = {
45+
@react.component
46+
let make = (~items: array<item>) => {
47+
switch items->Js.Array2.length === 0 {
48+
| true => React.null
49+
| false =>
50+
let valuesAndTypes = items->Js.Array2.map(item => {
51+
switch item {
52+
| Value({name, deprecated}) as kind | Type({name, deprecated}) as kind =>
53+
let (icon, textColor, bgColor, href) = switch kind {
54+
| Type(_) => ("t", "text-fire-30", "bg-fire-5", `#type-${name}`)
55+
| Value(_) => ("v", "text-sky-30", "bg-sky-5", `#value-${name}`)
56+
}
57+
let deprecatedIcon = switch deprecated->Js.Null.toOption {
58+
| Some(_) =>
59+
<div
60+
className={`bg-orange-100 min-w-[20px] min-h-[20px] w-5 h-5 mr-3 flex justify-center items-center rounded-xl ml-auto`}>
61+
<span className={"text-[10px] text-orange-400"}> {"D"->React.string} </span>
62+
</div>->Some
63+
| None => None
64+
}
65+
let title = `${Belt.Option.isSome(deprecatedIcon) ? "Deprecated " : ""}` ++ name
66+
let result =
67+
<li className="my-3">
68+
<a
69+
title
70+
className="flex items-center w-full font-normal text-14 text-gray-40 leading-tight hover:text-gray-80"
71+
href>
72+
<div
73+
className={`${bgColor} min-w-[20px] min-h-[20px] w-5 h-5 mr-3 flex justify-center items-center rounded-xl`}>
74+
<span className={"text-[10px] font-normal " ++ textColor}>
75+
{icon->React.string}
76+
</span>
77+
</div>
78+
<span className={"truncate"}> {React.string(name)} </span>
79+
{switch deprecatedIcon {
80+
| Some(icon) => icon
81+
| None => React.null
82+
}}
83+
</a>
84+
</li>
85+
result
86+
}
87+
})
88+
valuesAndTypes->React.array
89+
}
90+
}
91+
}
92+
4493
module SidebarTree = {
4594
@react.component
46-
let make = (~isOpen: bool, ~toggle: unit => unit, ~node: node) => {
95+
let make = (~isOpen: bool, ~toggle: unit => unit, ~node: node, ~items: array<item>) => {
4796
let router = Next.Router.useRouter()
4897

4998
let moduleRoute =
@@ -54,6 +103,16 @@ module SidebarTree = {
54103
let summaryClassName = "truncate py-1 md:h-auto tracking-tight text-gray-60 font-medium text-14 rounded-sm hover:bg-gray-20 hover:-ml-2 hover:py-1 hover:pl-2 "
55104
let classNameActive = " bg-fire-5 text-red-500 -ml-2 pl-2 font-medium hover:bg-fire-70"
56105

106+
let subMenu = switch items->Js.Array2.length > 0 {
107+
| true =>
108+
<div className={"xl:hidden ml-5"}>
109+
<ul className={"list-none py-0.5"}>
110+
<RightSidebar items />
111+
</ul>
112+
</div>
113+
| false => React.null
114+
}
115+
57116
let rec renderNode = node => {
58117
let isCurrentRoute =
59118
Js.Array2.joinWith(moduleRoute, "/") === Js.Array2.joinWith(node.path, "/")
@@ -63,6 +122,8 @@ module SidebarTree = {
63122
let hasChildren = node.children->Js.Array2.length > 0
64123
let href = node.path->Js.Array2.joinWith("/")
65124

125+
let tocModule = isCurrentRoute ? subMenu : React.null
126+
66127
switch hasChildren {
67128
| true =>
68129
let open_ =
@@ -77,6 +138,7 @@ module SidebarTree = {
77138
{node.name->React.string}
78139
</Next.Link>
79140
</summary>
141+
tocModule
80142
{if hasChildren {
81143
<ul className={"ml-5"}>
82144
{node.children
@@ -92,6 +154,7 @@ module SidebarTree = {
92154
<summary className={summaryClassName ++ classNameActive}>
93155
<Next.Link className={"block"} href> {node.name->React.string} </Next.Link>
94156
</summary>
157+
tocModule
95158
</li>
96159
}
97160
}
@@ -160,6 +223,7 @@ module SidebarTree = {
160223
href={node.path->Js.Array2.joinWith("/")}>
161224
{node.name->React.string}
162225
</Next.Link>
226+
{moduleRoute->Js.Array2.length === 1 ? subMenu : React.null}
163227
</div>
164228
<div className="hl-overline text-gray-80 mt-5 mb-2"> {"submodules"->React.string} </div>
165229
{node.children
@@ -277,54 +341,16 @@ let default = (props: props) => {
277341

278342
let rightSidebar = switch props {
279343
| Ok({module_: {items}}) if Js.Array2.length(items) > 0 =>
280-
let valuesAndTypes = items->Belt.Array.keepMap(item => {
281-
switch item {
282-
| Value({name, deprecated}) as kind | Type({name, deprecated}) as kind =>
283-
let (icon, textColor, bgColor, href) = switch kind {
284-
| Type(_) => ("t", "text-fire-30", "bg-fire-5", `#type-${name}`)
285-
| Value(_) => ("v", "text-sky-30", "bg-sky-5", `#value-${name}`)
286-
}
287-
let deprecatedIcon = switch deprecated->Js.Null.toOption {
288-
| Some(_) =>
289-
<div
290-
className={`bg-orange-100 min-w-[20px] min-h-[20px] w-5 h-5 mr-3 flex justify-center items-center rounded-xl ml-auto`}>
291-
<span className={"text-[10px] text-orange-400"}> {"D"->React.string} </span>
292-
</div>->Some
293-
| None => None
294-
}
295-
let title = `${Belt.Option.isSome(deprecatedIcon) ? "Deprecated " : ""}` ++ name
296-
let result =
297-
<li className="my-3">
298-
<a
299-
title
300-
className="flex items-center w-full font-normal text-14 text-gray-40 leading-tight hover:text-gray-80"
301-
href>
302-
<div
303-
className={`${bgColor} min-w-[20px] min-h-[20px] w-5 h-5 mr-3 flex justify-center items-center rounded-xl`}>
304-
<span className={"text-[10px] font-normal " ++ textColor}>
305-
{icon->React.string}
306-
</span>
307-
</div>
308-
<span className={"truncate"}> {React.string(name)} </span>
309-
{switch deprecatedIcon {
310-
| Some(icon) => icon
311-
| None => React.null
312-
}}
313-
</a>
314-
</li>
315-
Some(result)
316-
}
317-
})
344+
let rightSidebar = <RightSidebar items />
318345
<div className="hidden xl:block lg:w-1/5 md:h-auto md:relative overflow-y-visible bg-white">
319346
<aside
320347
className="relative top-0 pl-4 w-full block md:top-16 md:pt-16 md:sticky border-l border-gray-20 overflow-y-auto pb-24 h-[calc(100vh-4.5rem)]">
321348
<div className="hl-overline block text-gray-80 mt-16 mb-2">
322349
{"Types and values"->React.string}
323350
</div>
324-
<ul> {valuesAndTypes->React.array} </ul>
351+
<ul> {rightSidebar} </ul>
325352
</aside>
326353
</div>
327-
328354
| _ => React.null
329355
}
330356

@@ -334,7 +360,8 @@ let default = (props: props) => {
334360
}
335361

336362
let sidebar = switch props {
337-
| Ok({toctree}) => <SidebarTree isOpen=isSidebarOpen toggle=toggleSidebar node={toctree} />
363+
| Ok({toctree, module_: {items}}) =>
364+
<SidebarTree isOpen=isSidebarOpen toggle=toggleSidebar node={toctree} items />
338365
| Error(_) => React.null
339366
}
340367

0 commit comments

Comments
 (0)