-
Notifications
You must be signed in to change notification settings - Fork 225
/
main-navigation.tsx
225 lines (206 loc) · 6.91 KB
/
main-navigation.tsx
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
224
225
import { For, Show, Suspense, createEffect, createSignal } from "solid-js";
import { useLocation } 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;
}
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 }) {
if (props.item.mainNavExclude) return null;
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:font-bold hover:before:block dark:text-slate-300 ";
return (
<li class="relative">
<Dynamic
component={props.item.isTranslated ? A : "a"}
onClick={() => setIsOpen(false)}
href={props.item.path}
class={`hover:text-blue-700 dark:hover:text-blue-300 block w-full lg:text-sm 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 ${linkStyles()}`}
>
{props.item.title}
<Show when={!props.item.isTranslated}>
<span>
<abbr
title="english"
class="text-[0.7em] relative -top-2 left-2 no-underline text-neutral-400 "
>
EN
</abbr>
</span>
</Show>
</Dynamic>
</li>
);
}
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 dark:border-slate-700 lg:border-slate-400"
>
<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="relative flex justify-between hover:cursor-pointer w-full pl-3.5 dark:text-slate-300 group">
<span class="font-semibold dark:text-slate-100">
{child.title}
</span>
<Icon
path={chevronDown}
class="h-4 my-auto 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>
);
}
type Tab = "learn" | "reference";
const useSelectedTab = () => {
const loc = useLocation();
const path = () => loc.pathname;
const initialTab = path().includes("reference") ? "reference" : "learn";
const [selectedTab, setSelectedTab] = createSignal<Tab>(initialTab);
createEffect((previousTab) => {
if(path().includes("reference") && previousTab !== "reference"){
setSelectedTab("reference");
}else if( previousTab !== "learn"){
setSelectedTab("learn");
}
}, initialTab);
return { selectedTab, setSelectedTab
};
};
export function MainNavigation(props: NavProps) {
const i18n = useI18n();
const learn = () => props.tree.learn;
const reference = () => props.tree.reference;
const {selectedTab, setSelectedTab} = useSelectedTab();
return (
<Suspense>
<Show when={i18n.t}>
<nav class="overflow-y-auto custom-scrollbar h-full md:h-[calc(100vh-7rem)] pb-20">
<Tabs.Root
value={selectedTab()}
>
<Tabs.List class="sticky top-0 grid grid-cols-2 w-full z-10 md:dark:bg-slate-900 md:bg-slate-50">
<Tabs.Trigger
value="learn"
class="inline-block py-3 outline-none hover:bg-blue-500/30 dark:hover:bg-blue-300/20 dark:focus-visible:bg-blue-800 dark:text-slate-100 hover:font-bold font-medium"
onClick={() => {
setSelectedTab("learn");
}}
>
{i18n.t("main.nav.tab.learn")}
</Tabs.Trigger>
<Tabs.Trigger
value="reference"
class="inline-block py-3 outline-none hover:bg-blue-500/30 dark:hover:bg-blue-300/20 dark:focus-visible:bg-blue-800 dark:text-slate-100 hover:font-bold font-medium"
onClick={() => {
setSelectedTab("reference");
}}
>
{i18n.t("main.nav.tab.reference")}
</Tabs.Trigger>
<Tabs.Indicator class="absolute bottom-0 bg-blue-500 dark:bg-blue-500 transition-all duration-250 h-[2px]" />
</Tabs.List>
<Tabs.Content value="learn" class="w-full relative mt-5">
<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="w-full relative top-8">
<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>
);
}