Repository navigation
Expand file tree
/
Copy pathSidebarLink.tsx
More file actions
131 lines (125 loc) · 4.32 KB
/
Copy pathSidebarLink.tsx
File metadata and controls
131 lines (125 loc) · 4.32 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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/
/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/click-events-have-key-events */
import {useRef, useEffect} from 'react';
import * as React from 'react';
import cn from 'classnames';
import {IconNavArrow} from 'components/Icon/IconNavArrow';
import {IconCanary} from 'components/Icon/IconCanary';
import {IconExperimental} from 'components/Icon/IconExperimental';
import Link from 'next/link';
interface SidebarLinkProps {
href: string;
selected?: boolean;
title: string;
level: number;
version?: 'canary' | 'major' | 'experimental' | 'rc';
icon?: React.ReactNode;
isExpanded?: boolean;
hideArrow?: boolean;
onToggle?: () => void;
isPending: boolean;
}
export function SidebarLink({
href,
selected = false,
title,
version,
level,
isExpanded,
hideArrow,
onToggle,
isPending,
}: SidebarLinkProps) {
const ref = useRef<HTMLAnchorElement>(null);
useEffect(() => {
if (selected && ref && ref.current) {
// @ts-ignore
if (typeof ref.current.scrollIntoViewIfNeeded === 'function') {
// @ts-ignore
ref.current.scrollIntoViewIfNeeded();
}
}
}, [ref, selected]);
let target = '';
if (href.startsWith('https://')) {
target = '_blank';
}
return (
<div className="relative">
<Link
href={href}
ref={ref}
title={title}
target={target}
passHref
aria-current={selected ? 'page' : undefined}
className={cn(
'p-2 pe-2 w-full rounded-none lg:rounded-e-2xl text-start hover:bg-gray-5 dark:hover:bg-gray-80 relative flex items-center justify-between',
isExpanded != null && !hideArrow && 'pe-10',
{
'text-sm ps-6': level > 0,
'ps-5': level < 2,
'text-base font-bold': level === 0,
'text-primary dark:text-primary-dark': level === 0 && !selected,
'text-base text-secondary dark:text-secondary-dark':
level > 0 && !selected,
'text-base text-link dark:text-link-dark bg-highlight dark:bg-highlight-dark border-blue-40 hover:bg-highlight hover:text-link dark:hover:bg-highlight-dark dark:hover:text-link-dark':
selected,
'dark:bg-gray-70 bg-gray-3 dark:hover:bg-gray-70 hover:bg-gray-3':
isPending,
}
)}>
<div>
{title}{' '}
{version === 'major' && (
<span
title="- This feature is available in React 19 beta and the React canary channel"
className={`text-xs px-1 ms-1 rounded bg-gray-10 dark:bg-gray-40 dark:bg-opacity-20 text-gray-40 dark:text-gray-40`}>
React 19
</span>
)}
{version === 'canary' && (
<IconCanary
title=" - This feature is available in the latest Canary version of React"
className="ms-1 text-gray-30 dark:text-gray-60 inline-block w-3.5 h-3.5 align-[-3px]"
/>
)}
{version === 'experimental' && (
<IconExperimental
title=" - This feature is available in the latest Experimental version of React"
className="ms-1 text-gray-30 dark:text-gray-60 inline-block w-3.5 h-3.5 align-[-3px]"
/>
)}
{version === 'rc' && (
<IconCanary
title=" - This feature is available in the latest RC version"
className="ms-1 text-gray-30 dark:text-gray-60 inline-block w-3.5 h-3.5 align-[-3px]"
/>
)}
</div>
</Link>
{isExpanded != null && !hideArrow && onToggle != null && (
<button
type="button"
aria-label={`${isExpanded ? 'Collapse' : 'Expand'} ${title}`}
aria-expanded={isExpanded}
onClick={onToggle}
className={cn('absolute inset-y-0 end-0 flex items-center px-3', {
'text-link dark:text-link-dark': isExpanded,
'text-tertiary dark:text-tertiary-dark': !isExpanded,
})}>
<IconNavArrow displayDirection={isExpanded ? 'down' : 'end'} />
</button>
)}
</div>
);
}