-
Notifications
You must be signed in to change notification settings - Fork 178
/
DropdownNavbarItem.tsx
220 lines (199 loc) · 5.82 KB
/
DropdownNavbarItem.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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* REASONS TO SWIZZLE:
* - Label change reflecting selected item in dropdown.
*/
import React, { useState, useRef, useEffect } from 'react';
import clsx from 'clsx';
import {
useCollapsible,
Collapsible,
isRegexpStringMatch,
} from '@docusaurus/theme-common';
import {
isSamePath,
useLocalPathname,
} from '@docusaurus/theme-common/internal';
import type {
DesktopOrMobileNavBarItemProps,
Props as DropdownNavbarItemProps,
} from '@theme/NavbarItem/DropdownNavbarItem';
import type { LinkLikeNavbarItemProps } from '@theme/NavbarItem';
import NavLink from '@site/src/components/NavLink';
import NavbarItem from '@theme/NavbarItem';
const dropdownLinkActiveClass = 'dropdown__link--active';
interface ExtendedDropdownNavbarItemProps extends DropdownNavbarItemProps {
isDropdownItem: boolean;
}
function isItemActive(
item: LinkLikeNavbarItemProps,
localPathname: string,
): boolean {
if (isSamePath(item.to, localPathname)) {
return true;
}
if (isRegexpStringMatch(item.activeBaseRegex, localPathname)) {
return true;
}
if (item.activeBasePath && localPathname.startsWith(item.activeBasePath)) {
return true;
}
return false;
}
function containsActiveItems(
items: readonly LinkLikeNavbarItemProps[],
localPathname: string,
): boolean {
return items.some((item) => isItemActive(item, localPathname));
}
/**
Added function to add support for a changing label in dropdowns
according to the selected dropdown item
**/
function getDropdownProps(props, items, localPathname) {
const activeItem = items.filter((item) => isItemActive(item, localPathname));
if (activeItem.length) {
return {
...activeItem[0],
label: props.label + ' > ' + activeItem[0].label,
};
}
return props;
}
function DropdownNavbarItemDesktop({
items,
position,
className,
...props
}: DesktopOrMobileNavBarItemProps) {
const dropdownRef = useRef<HTMLDivElement>(null);
const dropdownMenuRef = useRef<HTMLUListElement>(null);
const [showDropdown, setShowDropdown] = useState(false);
/**
Added const to get the dropdown label if a dropdown item is selected
**/
const dropdownProps = getDropdownProps(props, items, useLocalPathname());
useEffect(() => {
const handleClickOutside = (event: MouseEvent | TouchEvent) => {
if (
!dropdownRef.current ||
dropdownRef.current.contains(event.target as Node)
) {
return;
}
setShowDropdown(false);
};
document.addEventListener('mousedown', handleClickOutside);
document.addEventListener('touchstart', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
document.removeEventListener('touchstart', handleClickOutside);
};
}, [dropdownRef]);
return (
<div
ref={dropdownRef}
className={clsx('dropdown', 'dropdown--hoverable', {
'dropdown--right': position === 'right',
'dropdown--show': showDropdown,
})}
>
<NavLink
className={clsx('navbar__item navbar__link', className)}
{...dropdownProps}
onClick={props.to ? undefined : (e) => e.preventDefault()}
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault();
setShowDropdown(!showDropdown);
}
}}
>
{props.children ?? props.label}
</NavLink>
<ul ref={dropdownMenuRef} className='dropdown__menu'>
{items.map((childItemProps, i) => (
<NavbarItem
isDropdownItem
onKeyDown={(e) => {
if (i === items.length - 1 && e.key === 'Tab') {
e.preventDefault();
setShowDropdown(false);
const nextNavbarItem = dropdownRef.current?.nextElementSibling;
if (nextNavbarItem) {
(nextNavbarItem as HTMLElement).focus();
}
}
}}
activeClassName={dropdownLinkActiveClass}
{...childItemProps}
key={i}
/>
))}
</ul>
</div>
);
}
function DropdownNavbarItemMobile({
items,
className,
...props
}: DesktopOrMobileNavBarItemProps) {
delete props.position;
const localPathname = useLocalPathname();
const containsActive = containsActiveItems(items, localPathname);
const { collapsed, toggleCollapsed, setCollapsed } = useCollapsible({
initialState: () => !containsActive,
});
// Expand/collapse if any item active after a navigation
useEffect(() => {
if (containsActive) {
setCollapsed(!containsActive);
}
}, [localPathname, containsActive, setCollapsed]);
return (
<li
className={clsx('menu__list-item', {
'menu__list-item--collapsed': collapsed,
})}
>
<NavLink
role='button'
className={clsx('menu__link menu__link--sublist', className)}
{...props}
onClick={(e) => {
e.preventDefault();
toggleCollapsed();
}}
>
{props.children ?? props.label}
</NavLink>
<Collapsible lazy as='ul' className='menu__list' collapsed={collapsed}>
{items.map((childItemProps, i) => (
<NavbarItem
mobile
isDropdownItem
onClick={props.onClick}
activeClassName='menu__link--active'
{...childItemProps}
key={i}
/>
))}
</Collapsible>
</li>
);
}
function DropdownNavbarItem({
mobile = false,
...props
}: ExtendedDropdownNavbarItemProps): JSX.Element {
delete props.isDropdownItem;
const Comp = mobile ? DropdownNavbarItemMobile : DropdownNavbarItemDesktop;
return <Comp {...props} />;
}
export default DropdownNavbarItem;