-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNavMenuGroup.tsx
More file actions
192 lines (180 loc) · 5.02 KB
/
NavMenuGroup.tsx
File metadata and controls
192 lines (180 loc) · 5.02 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
import {
isExternalUrl,
type NavItem,
type NavItemWithChildren,
type NavItemWithLink,
type NavItemWithLinkAndChildren,
} from '@rspress/shared'
import { useRef, useState, type ReactNode } from 'react'
import { Tag } from 'rspress/theme'
import { matchNavbar } from '../../shared/index.js'
import { Down } from './Down.js'
import {
NavMenuSingleItem,
type NavMenuSingleItemProps,
} from './NavMenuSingleItem.js'
import { SvgWrapper } from './SvgWrapper.js'
export interface NavMenuGroupItem {
text?: string | ReactNode
link?: string
items: NavItem[]
tag?: string
// Design for i18n highlight.
activeValue?: string
// Current pathname.
pathname?: string
// Base path.
base?: string
// Locales
langs?: string[]
}
function ActiveGroupItem({ item }: { item: NavItemWithLink }) {
return (
<div
key={item.link}
className="rp-rounded-2xl rp-my-1 rp-flex"
style={{
padding: '0.4rem 1.5rem 0.4rem 0.75rem',
}}
>
{item.tag && <Tag tag={item.tag} />}
<span className="rp-text-brand">{item.text}</span>
</div>
)
}
function NormalGroupItem({ item }: { item: NavItemWithLink }) {
return (
<div key={item.link} className="rp-font-medium rp-my-1">
<a
href={item.link}
target={isExternalUrl(item.link) ? '_blank' : undefined}
rel="noopener noreferrer"
>
<div
className="rp-rounded-2xl hover:rp-bg-mute"
style={{
padding: '0.4rem 1.5rem 0.4rem 0.75rem',
}}
>
<div className="rp-flex">
{item.tag && <Tag tag={item.tag} />}
<span>{item.text}</span>
</div>
</div>
</a>
</div>
)
}
export function NavMenuGroup(item: NavMenuGroupItem) {
const {
activeValue,
items: groupItems,
base = '',
link = '',
pathname = '',
} = item
const [isOpen, setIsOpen] = useState(false)
const closeTimerRef = useRef<number>(null)
const clearCloseTimer = () => {
if (closeTimerRef.current) {
clearTimeout(closeTimerRef.current)
closeTimerRef.current = null
}
}
/**
* Handle mouse leave event for the dropdown menu
* Closes the menu after a 150ms delay to allow diagonal mouse movement
* to the dropdown content area
*/
const handleMouseLeave = () => {
closeTimerRef.current = window.setTimeout(() => {
setIsOpen(false)
}, 150)
}
const handleMouseEnter = () => {
clearCloseTimer()
setIsOpen(true)
}
const renderLinkItem = (item: NavItemWithLink) => {
const isLinkActive = matchNavbar(item, pathname, base)
if (activeValue === item.text || (!activeValue && isLinkActive)) {
return <ActiveGroupItem key={item.link} item={item} />
}
return <NormalGroupItem key={item.link} item={item} />
}
const renderGroup = (
item: NavItemWithChildren | NavItemWithLinkAndChildren,
) => {
return (
<div>
{'link' in item ? (
renderLinkItem(item)
) : (
<p className="rp-font-bold rp-text-gray-400 rp-my-1 not:first:rp-border">
{item.text}
</p>
)}
{item.items.map(renderLinkItem)}
</div>
)
}
return (
<div
className="rp-relative rp-flex rp-items-center rp-justify-center rp-h-14"
onMouseLeave={handleMouseLeave}
>
<div
onMouseEnter={handleMouseEnter}
className="rspress-nav-menu-group-button rp-flex rp-justify-center rp-items-center rp-font-medium rp-text-sm rp-text-text-1 hover:rp-text-text-2 rp-transition-colors rp-duration-200 rp-cursor-pointer"
>
{link ? (
<NavMenuSingleItem
{...(item as NavMenuSingleItemProps)}
rightIcon={<SvgWrapper icon={Down} />}
/>
) : (
<>
<span
className="rp-text-sm rp-font-medium rp-flex"
style={{
marginRight: '2px',
}}
>
<Tag tag={item.tag} />
{item.text}
</span>
<SvgWrapper icon={Down} />
</>
)}
</div>
<div
className="rspress-nav-menu-group-content rp-absolute rp-mx-0.8 rp-transition-opacity rp-duration-300"
style={{
opacity: isOpen ? 1 : 0,
visibility: isOpen ? 'visible' : 'hidden',
right: 0,
top: '52px',
}}
onMouseEnter={clearCloseTimer}
>
<div
className="rp-p-3 rp-pr-2 rp-w-full rp-h-full rp-max-h-100vh rp-whitespace-nowrap"
style={{
boxShadow: 'var(--rp-shadow-3)',
zIndex: 100,
border: '1px solid var(--rp-c-divider-light)',
borderRadius: 'var(--rp-radius-large)',
background: 'var(--rp-c-bg)',
}}
>
{/* The item could be a link or a sub group */}
{groupItems.map((item) => (
<div key={item.text}>
{'items' in item ? renderGroup(item) : renderLinkItem(item)}
</div>
))}
</div>
</div>
</div>
)
}