-
Notifications
You must be signed in to change notification settings - Fork 173
/
MenuLink.tsx
104 lines (98 loc) · 3.33 KB
/
MenuLink.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
import { createEventListener } from '@solid-primitives/event-listener';
import { NavLink } from 'solid-app-router';
import { batch, createSignal, onMount, ParentComponent, Show } from 'solid-js';
import { setRouteReadyState, page, reflow } from '../../utils';
export type LinkTypes = {
title: string;
description: string;
path: string;
external?: boolean;
links?: LinkTypes[];
direction?: 'ltr' | 'rtl';
};
export type MenuLinkProps = {
setSubnav: (children: LinkTypes[]) => void;
setSubnavPosition: (position: number) => void;
closeSubnav: () => void;
clearSubnavClose: () => void;
} & LinkTypes;
export const MenuLink: ParentComponent<MenuLinkProps> = (props) => {
let linkEl!: HTMLAnchorElement;
// Only rerender event listener when children change
if (props.links) {
onMount(() => {
createEventListener(linkEl, 'mouseenter', () => {
props.clearSubnavClose();
batch(() => {
props.setSubnav(props.links!);
props.setSubnavPosition(linkEl.getBoundingClientRect().left);
});
});
createEventListener(linkEl, 'mouseleave', () => props.closeSubnav());
});
}
onMount(() => {
createEventListener(linkEl, 'mousedown', () => {
setRouteReadyState((prev) => ({ ...prev, loadingBar: true }));
page.scrollY = window.scrollY;
reflow();
const [targets, setTargets] = createSignal([linkEl]);
createEventListener(targets, 'mouseleave', () => {
setRouteReadyState((prev) => ({ ...prev, loadingBar: false }));
removeEvents();
});
createEventListener(targets, 'click', () => {
setRouteReadyState((prev) => ({ ...prev, loadingBar: false }));
removeEvents();
});
const removeEvents = () => setTargets([]);
});
if (!window.location.pathname.startsWith(props.path)) return;
linkEl.scrollIntoView({ inline: 'center', behavior: 'instant' as ScrollBehavior });
});
const onClick = () => {
if (window.location.pathname.startsWith(props.path)) {
window.scrollTo({ top: 0 });
return;
}
const pageEl = document.body;
pageEl.style.minHeight = `${document.body.scrollHeight}px`;
reflow();
setRouteReadyState((prev) => ({
...prev,
loadingBar: true,
loading: true,
routeChanged: true,
}));
};
return (
<li>
<NavLink
href={props.path}
target={props.external ? '_blank' : undefined}
class="inline-flex items-center transition text-[15px] dark:hover:bg-solid-darkLighterBg sm:text-base m-0 sm:m-1 px-3 sm:px-4 py-3 rounded pointer-fine:hover:text-white pointer-fine:hover:bg-solid-medium whitespace-nowrap"
activeClass="bg-solid-medium dark:bg-solid-light text-white"
onClick={() => !props.external && onClick()}
noScroll
ref={linkEl}
>
<span>{props.title}</span>
<Show when={props.external}>
<svg
class="h-5 z-50 -mt-1 ltr:ml-1 rtl:mr-1 opacity-30"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"
/>
</svg>
</Show>
</NavLink>
</li>
);
};