-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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: move color mode toggle in navbar on mobiles #6976
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,10 +14,10 @@ import { | |||||
splitNavbarItems, | ||||||
useNavbarMobileSidebar, | ||||||
useThemeConfig, | ||||||
useWindowSize, | ||||||
} from '@docusaurus/theme-common'; | ||||||
import NavbarMobileSidebarToggle from '@theme/Navbar/MobileSidebar/Toggle'; | ||||||
import NavbarLogo from '@theme/Navbar/Logo'; | ||||||
import styles from './styles.module.css'; | ||||||
|
||||||
function useNavbarItems() { | ||||||
// TODO temporary casting until ThemeConfig type is improved | ||||||
|
@@ -51,11 +51,27 @@ function NavbarContentLayout({ | |||||
|
||||||
export default function NavbarContent(): JSX.Element { | ||||||
const mobileSidebar = useNavbarMobileSidebar(); | ||||||
const windowSize = useWindowSize(); | ||||||
const isMobile = windowSize === 'mobile'; | ||||||
|
||||||
const items = useNavbarItems(); | ||||||
const [leftItems, rightItems] = splitNavbarItems(items); | ||||||
const isSearchItem = (item: NavbarItemConfig) => item.type === 'search'; | ||||||
const hasExplicitSearchItem = items.some(isSearchItem); | ||||||
|
||||||
const autoAddSearchBar = !items.some((item) => item.type === 'search'); | ||||||
const rightMostItems = [ | ||||||
<NavbarColorModeToggle | ||||||
key="toggle" | ||||||
className={isMobile ? 'margin-left--sm' : undefined} | ||||||
/>, | ||||||
!hasExplicitSearchItem || isMobile ? <SearchBar /> : null, | ||||||
]; | ||||||
if (isMobile) { | ||||||
[rightMostItems[0], rightMostItems[1]] = [ | ||||||
rightMostItems[1]!, | ||||||
rightMostItems[0]!, | ||||||
]; | ||||||
} | ||||||
Comment on lines
+69
to
+74
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. Since the search button just opens a modal, can we not do the swap and always put the search as the rightmost item? 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. I did this intentionally because some custom search input can be expanded by click, so it's probably better to try to freeze the color mode toggle position. I may be wrong though, and it's a completely normal option, just compare:
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. I see. If there are custom search inputs that don't open a modal then it makes sense 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. Not so fan of this change in terms of UX and SSR/CSR behavior This swap of theme/search leads to an issue. On a mobile with slow connexion:
|
||||||
|
||||||
return ( | ||||||
<NavbarContentLayout | ||||||
|
@@ -64,16 +80,21 @@ export default function NavbarContent(): JSX.Element { | |||||
<> | ||||||
{!mobileSidebar.disabled && <NavbarMobileSidebarToggle />} | ||||||
<NavbarLogo /> | ||||||
<NavbarItems items={leftItems} /> | ||||||
<NavbarItems | ||||||
items={ | ||||||
isMobile && hasExplicitSearchItem | ||||||
? leftItems.filter((item) => !isSearchItem(item)) | ||||||
: leftItems | ||||||
} | ||||||
/> | ||||||
</> | ||||||
} | ||||||
right={ | ||||||
// TODO stop hardcoding items? | ||||||
// Ask the user to add the respective navbar items => more flexible | ||||||
<> | ||||||
<NavbarItems items={rightItems} /> | ||||||
<NavbarColorModeToggle className={styles.colorModeToggle} /> | ||||||
{autoAddSearchBar && <SearchBar />} | ||||||
{rightMostItems} | ||||||
</> | ||||||
} | ||||||
/> | ||||||
|
This file was deleted.
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.
This needs a
key