Skip to content

Commit a7009f3

Browse files
committed
getting sidebar working
1 parent 074a5f8 commit a7009f3

File tree

4 files changed

+78
-37
lines changed

4 files changed

+78
-37
lines changed

app/routes/MdxRoute.res

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Sidebar = SidebarLayout.Sidebar
66
module NavItem = Sidebar.NavItem
77
module Category = Sidebar.Category
88

9-
type loaderData = {...Mdx.t}
9+
type loaderData = {...Mdx.t, categories: array<SidebarLayout.Sidebar.Category.t>}
1010

1111
/**
1212
This configures the MDX component to use our custom markdown components
@@ -45,15 +45,35 @@ let components = {
4545

4646
let loader: Loader.t<loaderData> = async ({request}) => {
4747
let mdx = await loadMdx(request)
48-
let res: loaderData = {__raw: mdx.__raw, attributes: mdx.attributes}
48+
Console.log(
49+
(await loadAllMdx())->Array.filter(page =>
50+
(page.path :> string)->String.includes("docs/manual")
51+
),
52+
)
53+
let res: loaderData = {
54+
__raw: mdx.__raw,
55+
attributes: mdx.attributes,
56+
categories: [
57+
{
58+
name: "overview",
59+
items: [{name: "Introduction", href: #"/docs/manual/installation"}],
60+
},
61+
],
62+
}
4963
res
5064
}
5165

5266
let default = () => {
67+
let {pathname} = useLocation()
5368
let component = useMdxComponent(~components)
5469
let attributes = useMdxAttributes()
55-
let _ = Toc.useToc(~category="manual")
5670

71+
let {categories} = useLoaderData()
72+
73+
let metaTitleCategory =
74+
(pathname :> string)->String.includes("docs/manual")
75+
? "ReScript Language Manual"
76+
: "Some other page"
5777
// TODO directly use layout and pass props
5878

5979
// Console.log(attributes)
@@ -64,7 +84,7 @@ let default = () => {
6484
// nextVersion=?Constants.nextVersion>
6585
// // {React.string(attributes.title)} </h1>
6686
<div>
67-
<DocsLayout metaTitleCategory="Foo" categories=[]>
87+
<DocsLayout metaTitleCategory categories>
6888
<div className="markdown-body"> {component()} </div>
6989
</DocsLayout>
7090
</div>

docs/react/installation.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Installation
33
description: "Installation and Setup"
44
canonical: "/docs/react/latest/installation"
5+
category: "manual"
56
---
67

78
# Installation

src/bindings/ReactRouter.res

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ external useNavigate: unit => string => unit = "useNavigate"
1010
@module("react-router")
1111
external useSearchParams: unit => ({..}, {..} => unit) = "useSearchParams"
1212

13+
@module("react-router")
14+
external useLoaderData: unit => 'a = "useLoaderData"
15+
1316
/* The types for this are auto-generated from the react-router.config.mjs file */
1417
type path = {pathname: Path.t, search?: string, hash?: string}
1518

@@ -83,11 +86,14 @@ module Routes = {
8386

8487
module Mdx = {
8588
type attributes = {
86-
title: string,
87-
description?: string,
89+
canonical: Path.t,
8890
category?: string,
89-
section?: string,
91+
description?: string,
9092
order?: int,
93+
path: Path.t,
94+
section?: string,
95+
slug: string,
96+
title: string,
9197
}
9298

9399
type t = {
@@ -108,5 +114,9 @@ module Mdx = {
108114
external useMdxComponent: (~components: {..}=?) => Jsx.component<'a> = "useMdxComponent"
109115

110116
@module("react-router-mdx/server")
111-
external loadAllMdx: (~filterByPaths: array<string>=?) => promise<array<t>> = "loadAllMdx"
117+
external loadAllMdx: (~filterByPaths: array<string>=?) => promise<array<attributes>> =
118+
"loadAllMdx"
119+
120+
@module("react-router-mdx/client")
121+
external useMdxFiles: unit => {..} = "useMdxFiles"
112122
}

src/layouts/SidebarLayout.res

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ module Toc = {
3131
<ul className="mt-3 py-1 mb-4 border-l border-fire-10">
3232
{Array.map(entries, ({header, href}) =>
3333
<li key=header className="pl-2 mt-2 first:mt-1">
34-
// <Link
35-
// to=Url(href)
36-
// className="font-normal block text-14 text-gray-40 leading-tight hover:text-gray-80">
37-
// {//links, nested
38-
// React.string(header)}
39-
// </Link>
34+
<Link.String
35+
to=href
36+
className="font-normal block text-14 text-gray-40 leading-tight hover:text-gray-80"
37+
>
38+
{//links, nested
39+
React.string(header)}
40+
</Link.String>
4041
</li>
4142
)->React.array}
4243
</ul>
@@ -78,12 +79,13 @@ module Sidebar = {
7879
}
7980

8081
<li key=m.name className={hidden ++ " mt-1 leading-4"}>
81-
// <Link
82-
// to=Url(m.href)
83-
// className={"truncate block py-1 md:h-auto tracking-tight text-gray-60 rounded-sm hover:bg-gray-20 hover:-ml-2 hover:py-1 hover:pl-2 " ++
84-
// active}>
85-
// {React.string(m.name)}
86-
// </Link>
82+
<Link
83+
to=m.href
84+
className={"truncate block py-1 md:h-auto tracking-tight text-gray-60 rounded-sm hover:bg-gray-20 hover:-ml-2 hover:py-1 hover:pl-2 " ++
85+
active}
86+
>
87+
{React.string(m.name)}
88+
</Link>
8789
{switch activeToc {
8890
| Some({entries}) =>
8991
if Array.length(entries) === 0 {
@@ -138,16 +140,19 @@ module Sidebar = {
138140
id="sidebar"
139141
className={(
140142
isOpen ? "fixed w-full left-0 h-full z-20 min-w-320" : "hidden "
141-
) ++ " md:block md:w-48 md:-ml-4 lg:w-1/5 h-auto md:relative overflow-y-visible bg-white mt-28 md:mt-0"}>
143+
) ++ " md:block md:w-48 md:-ml-4 lg:w-1/5 h-auto md:relative overflow-y-visible bg-white mt-28 md:mt-0"}
144+
>
142145
<aside
143146
id="sidebar-content"
144-
className="relative top-0 px-4 w-full block md:pt-10 md:top-28 md:sticky border-r border-gray-20 overflow-y-auto pb-24 h-auto max-h-[calc(100vh-7rem)]">
147+
className="relative top-0 px-4 w-full block md:pt-10 md:top-28 md:sticky border-r border-gray-20 overflow-y-auto pb-24 h-auto max-h-[calc(100vh-7rem)]"
148+
>
145149
<button
146150
onClick={evt => {
147151
ReactEvent.Mouse.preventDefault(evt)
148152
toggle()
149153
}}
150-
className="md:hidden h-16 flex pt-2 right-4 absolute">
154+
className="md:hidden h-16 flex pt-2 right-4 absolute"
155+
>
151156
<Icon.Close />
152157
</button>
153158
<div className="flex justify-between">
@@ -182,8 +187,9 @@ module BreadCrumbs = {
182187
let item = if i === List.length(crumbs) - 1 {
183188
<span key={Int.toString(i)}> {React.string(crumb.name)} </span>
184189
} else {
185-
React.null
186-
// <Link key={Int.toString(i)} to=Url(crumb.href)> {React.string(crumb.name)} </Link>
190+
<Link.String key={Int.toString(i)} to=crumb.href>
191+
{React.string(crumb.name)}
192+
</Link.String>
187193
}
188194
if i > 0 {
189195
<span key={Int.toString(i)}>
@@ -241,7 +247,7 @@ let make = (
241247
}
242248

243249
React.useEffect(() => {
244-
// TODO: figure out how to watch for route changes
250+
// TODO RR7: figure out how to watch for route changes
245251
// open Next.Router.Events
246252
// let {Next.Router.events: events} = router
247253

@@ -283,20 +289,22 @@ let make = (
283289
| Some({name, href}) =>
284290
<Link
285291
to=href
286-
className={"flex items-center text-fire hover:text-fire-70 border-2 border-red-300 rounded py-1.5 px-3"}>
292+
className={"flex items-center text-fire hover:text-fire-70 border-2 border-red-300 rounded py-1.5 px-3"}
293+
>
287294
<Icon.ArrowRight className={"rotate-180 mr-2"} />
288295
{React.string(name)}
289296
</Link>
290297
| None => React.null
291298
}
292299
let next = switch items->Array.get(i + 1) {
293-
| Some({name, href}) => React.null
294-
// <Link
295-
// to=Url(href)
296-
// className={"flex items-center text-fire hover:text-fire-70 ml-auto border-2 border-red-300 rounded py-1.5 px-3"}>
297-
// {React.string(name)}
298-
// <Icon.ArrowRight className={"ml-2"} />
299-
// </Link>
300+
| Some({name, href}) =>
301+
<Link
302+
to=href
303+
className={"flex items-center text-fire hover:text-fire-70 ml-auto border-2 border-red-300 rounded py-1.5 px-3"}
304+
>
305+
{React.string(name)}
306+
<Icon.ArrowRight className={"ml-2"} />
307+
</Link>
300308
| None => React.null
301309
}
302310
<div className={"flex justify-between mt-9"}>
@@ -309,7 +317,7 @@ let make = (
309317

310318
<>
311319
<Meta title=metaTitle />
312-
<EnableCollapsibleNavbar isEnabled={!isSidebarOpen && !isNavOpen}>
320+
<EnableCollapsibleNavbar isEnabled={isSidebarOpen && isNavOpen}>
313321
<div className={"mt-16 min-w-320 " ++ theme}>
314322
<div className="w-full">
315323
<Navigation isOverlayOpen=isNavOpen setOverlayOpen=setNavOpen />
@@ -319,10 +327,12 @@ let make = (
319327
<main className="px-4 w-full pt-20 md:ml-12 lg:mr-8 mb-32 md:max-w-576 lg:max-w-740">
320328
//width of the right content part
321329
<div
322-
className={"z-10 fixed border-b shadow top-[112px] left-0 pl-4 bg-white w-full py-4 md:relative md:border-none md:shadow-none md:p-0 md:top-auto flex items-center transition duration-300 ease-out group-[.nav-disappear]:-translate-y-64 md:group-[.nav-disappear]:-translate-y-0"}>
330+
className={"z-10 fixed border-b shadow top-[112px] left-0 pl-4 bg-white w-full py-4 md:relative md:border-none md:shadow-none md:p-0 md:top-auto flex items-center transition duration-300 ease-out group-[.nav-disappear]:-translate-y-64 md:group-[.nav-disappear]:-translate-y-0"}
331+
>
323332
<MobileDrawerButton hidden=isNavOpen onClick={handleDrawerButtonClick} />
324333
<div
325-
className="truncate overflow-x-auto touch-scroll flex items-center space-x-4 md:justify-between mr-4 w-full">
334+
className="truncate overflow-x-auto touch-scroll flex items-center space-x-4 md:justify-between mr-4 w-full"
335+
>
326336
breadcrumbs
327337
editLinkEl
328338
</div>

0 commit comments

Comments
 (0)