Skip to content

Commit 8f4d7a3

Browse files
Merge pull request #799 from aspeddro/fix-menu-ui-api-docs
Responsive Docs Menu API "Types and values"
2 parents 023dfd4 + 5062cd8 commit 8f4d7a3

File tree

1 file changed

+67
-42
lines changed

1 file changed

+67
-42
lines changed

src/ApiDocs.res

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

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

4995
let moduleRoute =
@@ -54,6 +100,16 @@ module SidebarTree = {
54100
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 "
55101
let classNameActive = " bg-fire-5 text-red-500 -ml-2 pl-2 font-medium hover:bg-fire-70"
56102

103+
let subMenu = switch items->Js.Array2.length > 0 {
104+
| true =>
105+
<div className={"xl:hidden ml-5"}>
106+
<ul className={"list-none py-0.5"}>
107+
<RightSidebar items />
108+
</ul>
109+
</div>
110+
| false => React.null
111+
}
112+
57113
let rec renderNode = node => {
58114
let isCurrentRoute =
59115
Js.Array2.joinWith(moduleRoute, "/") === Js.Array2.joinWith(node.path, "/")
@@ -63,6 +119,8 @@ module SidebarTree = {
63119
let hasChildren = node.children->Js.Array2.length > 0
64120
let href = node.path->Js.Array2.joinWith("/")
65121

122+
let tocModule = isCurrentRoute ? subMenu : React.null
123+
66124
switch hasChildren {
67125
| true =>
68126
let open_ =
@@ -77,6 +135,7 @@ module SidebarTree = {
77135
{node.name->React.string}
78136
</Next.Link>
79137
</summary>
138+
tocModule
80139
{if hasChildren {
81140
<ul className={"ml-5"}>
82141
{node.children
@@ -92,6 +151,7 @@ module SidebarTree = {
92151
<summary className={summaryClassName ++ classNameActive}>
93152
<Next.Link className={"block"} href> {node.name->React.string} </Next.Link>
94153
</summary>
154+
tocModule
95155
</li>
96156
}
97157
}
@@ -160,6 +220,7 @@ module SidebarTree = {
160220
href={node.path->Js.Array2.joinWith("/")}>
161221
{node.name->React.string}
162222
</Next.Link>
223+
{moduleRoute->Js.Array2.length === 1 ? subMenu : React.null}
163224
</div>
164225
<div className="hl-overline text-gray-80 mt-5 mb-2"> {"submodules"->React.string} </div>
165226
{node.children
@@ -277,54 +338,17 @@ let default = (props: props) => {
277338

278339
let rightSidebar = switch props {
279340
| 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-
})
318341
<div className="hidden xl:block lg:w-1/5 md:h-auto md:relative overflow-y-visible bg-white">
319342
<aside
320343
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)]">
321344
<div className="hl-overline block text-gray-80 mt-16 mb-2">
322345
{"Types and values"->React.string}
323346
</div>
324-
<ul> {valuesAndTypes->React.array} </ul>
347+
<ul>
348+
<RightSidebar items />
349+
</ul>
325350
</aside>
326351
</div>
327-
328352
| _ => React.null
329353
}
330354

@@ -334,7 +358,8 @@ let default = (props: props) => {
334358
}
335359

336360
let sidebar = switch props {
337-
| Ok({toctree}) => <SidebarTree isOpen=isSidebarOpen toggle=toggleSidebar node={toctree} />
361+
| Ok({toctree, module_: {items}}) =>
362+
<SidebarTree isOpen=isSidebarOpen toggle=toggleSidebar node={toctree} items />
338363
| Error(_) => React.null
339364
}
340365

0 commit comments

Comments
 (0)