-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(v2): toggle data-theme with vanilla js instead of react helmet #2127
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
|
||
import React, {useCallback, useState} from 'react'; | ||
import Link from '@docusaurus/Link'; | ||
import Head from '@docusaurus/Head'; | ||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
import useBaseUrl from '@docusaurus/useBaseUrl'; | ||
|
||
|
@@ -68,122 +67,116 @@ function Navbar() { | |
|
||
const logoUrl = useBaseUrl(logo.src); | ||
return ( | ||
<> | ||
<Head> | ||
{/* TODO: Do not assume that it is in english language */} | ||
<html lang="en" data-theme={theme} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
</Head> | ||
<nav | ||
ref={navbarRef} | ||
className={classnames('navbar', 'navbar--light', 'navbar--fixed-top', { | ||
'navbar-sidebar--show': sidebarShown, | ||
[styles.navbarHideable]: hideOnScroll, | ||
[styles.navbarHidden]: !isNavbarVisible, | ||
})}> | ||
<div className="navbar__inner"> | ||
<div className="navbar__items"> | ||
<div | ||
aria-label="Navigation bar toggle" | ||
className="navbar__toggle" | ||
role="button" | ||
tabIndex={0} | ||
onClick={showSidebar} | ||
onKeyDown={showSidebar}> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="30" | ||
height="30" | ||
viewBox="0 0 30 30" | ||
role="img" | ||
focusable="false"> | ||
<title>Menu</title> | ||
<path | ||
stroke="currentColor" | ||
strokeLinecap="round" | ||
strokeMiterlimit="10" | ||
strokeWidth="2" | ||
d="M4 7h22M4 15h22M4 23h22" | ||
/> | ||
</svg> | ||
</div> | ||
<Link className="navbar__brand" to={baseUrl}> | ||
{logo != null && ( | ||
<img className="navbar__logo" src={logoUrl} alt={logo.alt} /> | ||
)} | ||
{title != null && ( | ||
<strong | ||
className={isSearchBarExpanded ? styles.hideLogoText : ''}> | ||
{title} | ||
</strong> | ||
)} | ||
</Link> | ||
{links | ||
.filter(linkItem => linkItem.position !== 'right') | ||
.map((linkItem, i) => ( | ||
<NavLink {...linkItem} key={i} /> | ||
))} | ||
</div> | ||
<div className="navbar__items navbar__items--right"> | ||
{links | ||
.filter(linkItem => linkItem.position === 'right') | ||
.map((linkItem, i) => ( | ||
<NavLink {...linkItem} key={i} /> | ||
))} | ||
{!disableDarkMode && ( | ||
<Toggle | ||
className={styles.displayOnlyInLargeViewport} | ||
aria-label="Dark mode toggle" | ||
checked={theme === 'dark'} | ||
onChange={onToggleChange} | ||
<nav | ||
ref={navbarRef} | ||
className={classnames('navbar', 'navbar--light', 'navbar--fixed-top', { | ||
'navbar-sidebar--show': sidebarShown, | ||
[styles.navbarHideable]: hideOnScroll, | ||
[styles.navbarHidden]: !isNavbarVisible, | ||
})}> | ||
<div className="navbar__inner"> | ||
<div className="navbar__items"> | ||
<div | ||
aria-label="Navigation bar toggle" | ||
className="navbar__toggle" | ||
role="button" | ||
tabIndex={0} | ||
onClick={showSidebar} | ||
onKeyDown={showSidebar}> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="30" | ||
height="30" | ||
viewBox="0 0 30 30" | ||
role="img" | ||
focusable="false"> | ||
<title>Menu</title> | ||
<path | ||
stroke="currentColor" | ||
strokeLinecap="round" | ||
strokeMiterlimit="10" | ||
strokeWidth="2" | ||
d="M4 7h22M4 15h22M4 23h22" | ||
/> | ||
</svg> | ||
</div> | ||
<Link className="navbar__brand" to={baseUrl}> | ||
{logo != null && ( | ||
<img className="navbar__logo" src={logoUrl} alt={logo.alt} /> | ||
)} | ||
{title != null && ( | ||
<strong | ||
className={isSearchBarExpanded ? styles.hideLogoText : ''}> | ||
{title} | ||
</strong> | ||
)} | ||
<SearchBar | ||
handleSearchBarToggle={setIsSearchBarExpanded} | ||
isSearchBarExpanded={isSearchBarExpanded} | ||
</Link> | ||
{links | ||
.filter(linkItem => linkItem.position !== 'right') | ||
.map((linkItem, i) => ( | ||
<NavLink {...linkItem} key={i} /> | ||
))} | ||
</div> | ||
<div className="navbar__items navbar__items--right"> | ||
{links | ||
.filter(linkItem => linkItem.position === 'right') | ||
.map((linkItem, i) => ( | ||
<NavLink {...linkItem} key={i} /> | ||
))} | ||
{!disableDarkMode && ( | ||
<Toggle | ||
className={styles.displayOnlyInLargeViewport} | ||
aria-label="Dark mode toggle" | ||
checked={theme === 'dark'} | ||
onChange={onToggleChange} | ||
/> | ||
</div> | ||
)} | ||
<SearchBar | ||
handleSearchBarToggle={setIsSearchBarExpanded} | ||
isSearchBarExpanded={isSearchBarExpanded} | ||
/> | ||
</div> | ||
<div | ||
role="presentation" | ||
className="navbar-sidebar__backdrop" | ||
onClick={() => { | ||
setSidebarShown(false); | ||
}} | ||
/> | ||
<div className="navbar-sidebar"> | ||
<div className="navbar-sidebar__brand"> | ||
<Link className="navbar__brand" onClick={hideSidebar} to={baseUrl}> | ||
{logo != null && ( | ||
<img className="navbar__logo" src={logoUrl} alt={logo.alt} /> | ||
)} | ||
{title != null && <strong>{title}</strong>} | ||
</Link> | ||
{!disableDarkMode && sidebarShown && ( | ||
<Toggle | ||
aria-label="Dark mode toggle in sidebar" | ||
checked={theme === 'dark'} | ||
onChange={onToggleChange} | ||
/> | ||
</div> | ||
<div | ||
role="presentation" | ||
className="navbar-sidebar__backdrop" | ||
onClick={() => { | ||
setSidebarShown(false); | ||
}} | ||
/> | ||
<div className="navbar-sidebar"> | ||
<div className="navbar-sidebar__brand"> | ||
<Link className="navbar__brand" onClick={hideSidebar} to={baseUrl}> | ||
{logo != null && ( | ||
<img className="navbar__logo" src={logoUrl} alt={logo.alt} /> | ||
)} | ||
</div> | ||
<div className="navbar-sidebar__items"> | ||
<div className="menu"> | ||
<ul className="menu__list"> | ||
{links.map((linkItem, i) => ( | ||
<li className="menu__list-item" key={i}> | ||
<NavLink | ||
className="menu__link" | ||
{...linkItem} | ||
onClick={hideSidebar} | ||
/> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
{title != null && <strong>{title}</strong>} | ||
</Link> | ||
{!disableDarkMode && sidebarShown && ( | ||
<Toggle | ||
aria-label="Dark mode toggle in sidebar" | ||
checked={theme === 'dark'} | ||
onChange={onToggleChange} | ||
/> | ||
)} | ||
</div> | ||
<div className="navbar-sidebar__items"> | ||
<div className="menu"> | ||
<ul className="menu__list"> | ||
{links.map((linkItem, i) => ( | ||
<li className="menu__list-item" key={i}> | ||
<NavLink | ||
className="menu__link" | ||
{...linkItem} | ||
onClick={hideSidebar} | ||
/> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
</div> | ||
</nav> | ||
</> | ||
</div> | ||
</nav> | ||
); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, {useEffect} from 'react'; | ||
import React, {useEffect, useRef} from 'react'; | ||
import {NavLink} from 'react-router-dom'; | ||
|
||
const internalRegex = /^\/(?!\/)/; | ||
|
@@ -14,7 +14,7 @@ function Link(props) { | |
const {to, href} = props; | ||
const targetLink = to || href; | ||
const isInternal = internalRegex.test(targetLink); | ||
let preloaded = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoa this is such a bad bug, wonder how we didn't catch it earlier :| |
||
const preloaded = useRef(false); | ||
|
||
const IOSupported = | ||
typeof window !== 'undefined' && 'IntersectionObserver' in window; | ||
|
@@ -48,9 +48,9 @@ function Link(props) { | |
}; | ||
|
||
const onMouseEnter = () => { | ||
if (!preloaded) { | ||
if (!preloaded.current) { | ||
window.docusaurus.preload(targetLink); | ||
preloaded = true; | ||
preloaded.current = true; | ||
} | ||
}; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For sure this is faster, it's direct access. But fewer people have heard about this lol. It's fine since it's internal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it is fine because relevant people can simply google and learn what this is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I agree it's fine :)