Skip to content

Commit d0c3bf3

Browse files
committed
useLocation & formatting
1 parent 7141598 commit d0c3bf3

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

src/theme/Navbar/index.js

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import React, {useCallback, useState, useEffect} from 'react';
8+
import React, { useCallback, useState, useEffect } from 'react';
99
import classnames from 'classnames';
1010
import Link from '@docusaurus/Link';
1111
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
1212
import useBaseUrl from '@docusaurus/useBaseUrl';
1313
import isInternalUrl from '@docusaurus/isInternalUrl';
14+
import { useLocation } from '@docusaurus/router';
1415

1516
import SearchBar from '@theme/SearchBar';
1617
import Toggle from '@theme/Toggle';
@@ -20,18 +21,18 @@ import useLogo from '@theme/hooks/useLogo';
2021

2122
import styles from './styles.module.css';
2223

23-
function noop() {}
24+
function noop() { }
2425

2526
const useLinkLogo = (logo = {}) => {
2627
const {
27-
siteConfig: {baseUrl} = {},
28+
siteConfig: { baseUrl } = {},
2829
} = useDocusaurusContext();
29-
const {isDarkTheme} = useThemeContext();
30+
const { isDarkTheme } = useThemeContext();
3031
const logoLink = logo.href || baseUrl;
3132
let logoLinkProps = {};
3233

3334
if (logo.target) {
34-
logoLinkProps = {target: logo.target};
35+
logoLinkProps = { target: logo.target };
3536
} else if (!isInternalUrl(logoLink)) {
3637
logoLinkProps = {
3738
rel: 'noopener noreferrer',
@@ -48,10 +49,10 @@ const useLinkLogo = (logo = {}) => {
4849
};
4950
};
5051

51-
function NavLink({activeBasePath, to, href, logo, label, position, ...props}) {
52+
function NavLink({ activeBasePath, to, href, logo, label, position, ...props }) {
5253
const toUrl = useBaseUrl(to);
5354
const activeBaseUrl = useBaseUrl(activeBasePath);
54-
const {logoImageUrl, logoAlt} = useLinkLogo(logo);
55+
const { logoImageUrl, logoAlt } = useLinkLogo(logo);
5556

5657
const content = logoImageUrl != null ? <img
5758
className={classnames(styles.navbarIcon)}
@@ -62,27 +63,27 @@ function NavLink({activeBasePath, to, href, logo, label, position, ...props}) {
6263
<Link
6364
{...(href
6465
? {
65-
target: '_blank',
66-
rel: 'noopener noreferrer',
67-
href,
68-
}
66+
target: '_blank',
67+
rel: 'noopener noreferrer',
68+
href,
69+
}
6970
: {
70-
activeClassName: 'navbar__link--active',
71-
to: toUrl,
72-
...(activeBasePath
73-
? {
74-
isActive: (_match, location) =>
75-
location.pathname.startsWith(activeBaseUrl),
76-
}
77-
: null),
78-
})}
71+
activeClassName: 'navbar__link--active',
72+
to: toUrl,
73+
...(activeBasePath
74+
? {
75+
isActive: (_match, location) =>
76+
location.pathname.startsWith(activeBaseUrl),
77+
}
78+
: null),
79+
})}
7980
{...props}>
8081
{content}
8182
</Link>
8283
);
8384
}
8485

85-
function NavItem({items, emphasis, position, ...props}) {
86+
function NavItem({ items, emphasis, position, ...props }) {
8687
if (!items) {
8788
return <NavLink className={classnames('navbar__item', 'navbar__link', {
8889
'navbar__link--icon': props.logo,
@@ -111,25 +112,29 @@ function NavItem({items, emphasis, position, ...props}) {
111112
);
112113
}
113114

115+
function notPluginPage(pathname) {
116+
return pathname !== '/plugins' && pathname !== '/plugins/';
117+
}
114118

115119
function Navbar() {
116120
const {
117121
siteConfig: {
118122
themeConfig: {
119-
navbar: {title, links = [], hideOnScroll = false} = {},
123+
navbar: { title, links = [], hideOnScroll = false } = {},
120124
disableDarkMode = false,
121125
},
122126
},
123127
isClient,
124128
} = useDocusaurusContext();
125129

126-
const [disableSearchBarPlugIns, setDisableSearchBarPlugIns] = useState(location.pathname == "/plugins");
130+
const location = useLocation();
131+
const [enableSearch, setEnableSearch] = useState(notPluginPage(location.pathname));
127132
useEffect(() => {
128-
setDisableSearchBarPlugIns(location.pathname == "/plugins");
129-
}, [location.pathname]);
130-
const {isDarkTheme, setLightTheme, setDarkTheme} = useThemeContext();
131-
const {navbarRef, isNavbarVisible} = useHideableNavbar(hideOnScroll);
132-
const {logoLink, logoLinkProps, logoImageUrl, logoAlt} = useLogo();
133+
setEnableSearch(notPluginPage(location.pathname));
134+
}, [location]);
135+
const { isDarkTheme, setLightTheme, setDarkTheme } = useThemeContext();
136+
const { navbarRef, isNavbarVisible } = useHideableNavbar(hideOnScroll);
137+
const { logoLink, logoLinkProps, logoImageUrl, logoAlt } = useLogo();
133138
const onToggleChange = useCallback(
134139
e => (e.target.checked ? setDarkTheme() : setLightTheme()),
135140
[setLightTheme, setDarkTheme],
@@ -174,7 +179,7 @@ function Navbar() {
174179
onChange={onToggleChange}
175180
/>
176181
)}
177-
{!disableSearchBarPlugIns && (
182+
{enableSearch && (
178183
<SearchBar
179184
handleSearchBarToggle={noop}
180185
isSearchBarExpanded={true}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy