Skip to content
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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a key

];
if (isMobile) {
[rightMostItems[0], rightMostItems[1]] = [
rightMostItems[1]!,
rightMostItems[0]!,
];
}
Comment on lines +69 to +74
Copy link
Collaborator

@Josh-Cena Josh-Cena Mar 24, 2022

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:

Option 1 (default) Option 2 (with swapping items on mobiles)
ezgif com-gif-maker (2) ezgif com-gif-maker (1)

Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

Choose a reason for hiding this comment

The 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:

  • you see first theme/search
  • it then flips to search/theme


return (
<NavbarContentLayout
Expand All @@ -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}
</>
}
/>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import React from 'react';
import NavbarColorModeToggle from '@theme/Navbar/ColorModeToggle';
import IconClose from '@theme/IconClose';
import NavbarLogo from '@theme/Navbar/Logo';
import {useNavbarMobileSidebar} from '@docusaurus/theme-common';
Expand All @@ -27,7 +26,6 @@ export default function NavbarMobileSidebarHeader(): JSX.Element {
return (
<div className="navbar-sidebar__brand">
<NavbarLogo />
<NavbarColorModeToggle className="margin-right--md" />
<CloseButton />
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

@media (max-width: 996px) {
.searchBox {
position: absolute;
right: var(--ifm-navbar-padding-horizontal);
}
}

@media (min-width: 997px) {
.searchBox {
padding: var(--ifm-navbar-item-padding-vertical)
Expand Down