generated from nisabmohd/Aria-Docs
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathsublink.tsx
More file actions
114 lines (104 loc) · 2.94 KB
/
sublink.tsx
File metadata and controls
114 lines (104 loc) · 2.94 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
'use client';
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from '@/components/ui/collapsible';
import {SheetClose} from '@/components/ui/sheet';
import {EachRoute} from '@/lib/routes-config';
import {cn} from '@/lib/utils';
import {ChevronRight} from 'lucide-react';
import {usePathname} from 'next/navigation';
import {useEffect, useMemo, useState} from 'react';
import Anchor from './anchor';
import {Button} from './ui/button';
export default function SubLink({
title,
href,
items,
level,
isSheet,
new: isNew,
noLink,
}: EachRoute & {level: number; isSheet: boolean; noLink: boolean}) {
const path = usePathname();
const itemsIncludesPath = useMemo(
() => items?.some(item => item.href && path.endsWith(item.href)),
[items, path],
);
const [isOpen, setIsOpen] = useState(level == 0);
useEffect(() => {
if (itemsIncludesPath) {
setIsOpen(true);
}
}, [itemsIncludesPath]);
const Comp = (
<Anchor
className="flex items-center hover:text-primary"
activeClassName="text-primary font-semibold"
href={href ?? ''}
>
<span>{title}</span>
{isNew && (
<span className="new-badge ml-2 rounded px-1 py-0.5 text-xs font-semibold border">
NEW
</span>
)}
</Anchor>
);
const titleOrLink = !noLink ? (
isSheet ? (
<SheetClose asChild>{Comp}</SheetClose>
) : (
Comp
)
) : (
<h4 className="font-semibold sm:text-sm text-primary">{title}</h4>
);
if (!items) {
return titleOrLink;
}
return (
<div className="flex flex-col gap-1 w-full">
<Collapsible open={isOpen} onOpenChange={setIsOpen}>
<CollapsibleTrigger asChild>
<div className="flex select-none cursor-pointer items-center gap-2">
{titleOrLink}
<Button
className="ml-auto mr-3.5 h-6 w-6"
variant="link"
size="icon"
>
<ChevronRight
className={cn(
'h-[0.9rem] w-[0.9rem] transition ease-out',
isOpen && 'rotate-90',
)}
/>
<span className="sr-only">Toggle</span>
</Button>
</div>
</CollapsibleTrigger>
<CollapsibleContent>
<div
className={cn(
'flex flex-col items-start sm:text-sm dark:text-neutral-300/85 text-neutral-800 ml-0.5 mt-2.5 gap-3',
level > 0 && 'pl-4 border-l ml-1',
)}
>
{items?.map(innerLink => {
const modifiedItems = {
...innerLink,
href: `${href ?? ''}${innerLink.href ?? ''}`,
level: level + 1,
isSheet,
noLink: false,
};
return <SubLink key={modifiedItems.href} {...modifiedItems} />;
})}
</div>
</CollapsibleContent>
</Collapsible>
</div>
);
}