forked from solidjs/solid-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain-navigation.tsx
More file actions
223 lines (207 loc) · 7.01 KB
/
main-navigation.tsx
File metadata and controls
223 lines (207 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { For, Show, Suspense, createSignal } from "solid-js";
import { useBeforeLeave, useLocation, useMatch } from "@solidjs/router";
import { Collapsible, Tabs } from "@kobalte/core";
import { Icon } from "solid-heroicons";
import { chevronDown } from "solid-heroicons/solid";
import { setIsOpen } from "./mobile-navigation";
import { A } from "~/ui/i18n-anchor";
import { Dynamic } from "solid-js/web";
import { useI18n } from "~/i18n/i18n-context";
interface Entry {
title: string;
path: string;
children?: Entry[];
mainNavExclude: boolean;
isTranslated?: boolean;
isDeprecated?: boolean;
}
type EntryList = { learn: Entry[]; reference: Entry[] };
interface NavProps {
tree: {
learn: Entry[];
reference: Entry[];
};
}
// gets an array of entries and orders it alphabeticaly
const getAlphabeticalyOrderedList = (list: Entry[]) =>
list.slice().sort((firstChild, secondChild) => {
return firstChild.title
.toLowerCase()
.localeCompare(secondChild.title.toLowerCase());
});
// check if every item on the list has mainNavExclude as true
const shouldHideNavItem = (list: EntryList["learn" | "reference"]) =>
list.filter(({ mainNavExclude }) => mainNavExclude).length === list.length;
function ListItemLink(props: { item: Entry }) {
const location = useLocation();
const linkStyles = () =>
location.pathname === props.item.path.replace(/\\/g, "/")
? "font-semibold text-blue-700 before:bg-blue-700 dark:before:bg-blue-200 dark:text-blue-300 before:block"
: "text-slate-700 before:hidden before:bg-blue-600 before:dark:bg-blue-200 hover:text-blue-700 hover:before:block dark:text-slate-300 ";
return (
<Show when={!props.item.mainNavExclude}>
<li class="relative">
<Dynamic
component={props.item.isTranslated ? A : "a"}
onClick={() => setIsOpen(false)}
href={props.item.path}
class={`block w-full pl-3.5 before:pointer-events-none before:absolute before:-left-1 before:top-1/2 before:h-1.5 before:w-1.5 before:-translate-y-1/2 before:rounded-full hover:text-blue-700 lg:text-sm dark:hover:text-blue-300 ${linkStyles()}`}
>
{props.item.title}
<Show when={props.item.isDeprecated}> (deprecated)</Show>
<Show when={!props.item.isTranslated}>
<span>
<abbr
title="english"
class="relative -top-2 left-2 text-[0.7em] text-neutral-400 no-underline"
>
EN
</abbr>
</span>
</Show>
</Dynamic>
</li>
</Show>
);
}
function DirList(props: { list: Entry[]; sortAlphabeticaly?: boolean }) {
return (
<For each={props.list}>
{(item) => {
if (Array.isArray(item.children)) {
const itemChildren = props.sortAlphabeticaly
? getAlphabeticalyOrderedList(item.children)
: item.children;
return (
<li>
<span class="font-semibold text-slate-800 dark:text-slate-100">
{item.title}
</span>
<ul
role="list"
class="ml-2 mt-2 space-y-3 border-l-[1px] border-slate-400 lg:border-slate-400 dark:border-slate-700"
>
<For each={itemChildren}>
{(child) => {
if (
Array.isArray(child.children) &&
shouldHideNavItem(child.children)
)
return null;
return Array.isArray(child.children) ? (
<>
<li>
<Collapsible.Root defaultOpen={true}>
<Collapsible.Trigger class="group relative flex w-full justify-between pl-3.5 hover:cursor-pointer dark:text-slate-300">
<span class="font-semibold dark:text-slate-100">
{child.title}
</span>
<Icon
aria-hidden="true"
path={chevronDown}
class="my-auto h-4 transition-transform kb-group-closed:rotate-180"
/>
</Collapsible.Trigger>
<Collapsible.Content class="navigation_collapsible">
<ul
role="list"
class="ml-4 mt-3 space-y-3 border-l-[1px] border-slate-400 dark:border-slate-700 dark:lg:border-slate-700"
>
<DirList
sortAlphabeticaly={props.sortAlphabeticaly}
list={child.children}
/>
</ul>
</Collapsible.Content>
</Collapsible.Root>
</li>
</>
) : (
<ListItemLink item={child} />
);
}}
</For>
</ul>
</li>
);
} else {
return <ListItemLink item={item} />;
}
}}
</For>
);
}
export function MainNavigation(props: NavProps) {
const i18n = useI18n();
const learn = () => props.tree.learn;
const reference = () => props.tree.reference;
const isReference = useMatch(() => "/:project?/reference/*?", {
project: ["solid-router", "solid-meta", "solid-start"],
});
const initialTab = () => (isReference() ? "reference" : "learn");
const [selectedTab, setSelectedTab] = createSignal(initialTab());
/**
* Re-syncs the selected tab with the chosen route.
*/
useBeforeLeave(({ to }) => {
if (typeof to === "number") return;
if (to.includes("reference")) {
setSelectedTab("reference");
} else if (to.includes("learn")) {
setSelectedTab("learn");
}
});
return (
<Suspense>
<Show when={i18n.t}>
<nav class="custom-scrollbar h-full overflow-y-auto pb-20 md:h-[calc(100vh-7rem)]">
<Tabs.Root value={selectedTab()}>
<Tabs.List class="sticky top-0 z-10 grid w-full grid-cols-2 md:bg-slate-50 md:dark:bg-slate-900">
<Tabs.Trigger
value="learn"
class="inline-block py-3 font-medium outline-none hover:bg-blue-500/30 focus-visible:bg-blue-500/40 dark:text-slate-100 dark:hover:bg-blue-300/20 dark:focus-visible:bg-blue-800"
onClick={() => {
setSelectedTab("learn");
}}
>
{i18n.t("main.nav.tab.learn")}
</Tabs.Trigger>
<Tabs.Trigger
value="reference"
class="inline-block py-3 font-medium outline-none hover:bg-blue-500/30 focus-visible:bg-blue-500/40 dark:text-slate-100 dark:hover:bg-blue-300/20 dark:focus-visible:bg-blue-800"
onClick={() => {
setSelectedTab("reference");
}}
>
{i18n.t("main.nav.tab.reference")}
</Tabs.Trigger>
<Tabs.Indicator class="duration-250 absolute bottom-0 h-[2px] bg-blue-500 transition-all dark:bg-blue-500" />
</Tabs.List>
<Tabs.Content value="learn" class="relative mt-5 w-full">
<Show
when={learn()}
fallback={
<p class="text-white">{i18n.t("main.nav.no.routes")}</p>
}
>
<ul role="list" class="space-y-3 px-4">
<DirList list={learn()} />
</ul>
</Show>
</Tabs.Content>
<Tabs.Content value="reference" class="relative top-8 w-full">
<Show
when={reference()}
fallback={<p>{i18n.t("main.nav.no.routes")}</p>}
>
<ul role="list" class="space-y-3 px-4">
<DirList sortAlphabeticaly list={reference()} />
</ul>
</Show>
</Tabs.Content>
</Tabs.Root>
</nav>
</Show>
</Suspense>
);
}