Skip to content

Commit

Permalink
fix(theme): preserve url ?search#hash on navbar version/locale dropdo…
Browse files Browse the repository at this point in the history
…wns navigations (#8059)
  • Loading branch information
slorber authored Sep 7, 2022
1 parent 67c6923 commit a492025
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import {useDocsPreferredVersion} from '@docusaurus/theme-common';
import {useDocsVersionCandidates} from '@docusaurus/theme-common/internal';
import {translate} from '@docusaurus/Translate';
import {useLocation} from '@docusaurus/router';
import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem';
import type {Props} from '@theme/NavbarItem/DocsVersionDropdownNavbarItem';
Expand All @@ -29,6 +30,7 @@ export default function DocsVersionDropdownNavbarItem({
dropdownItemsAfter,
...props
}: Props): JSX.Element {
const {search, hash} = useLocation();
const activeDocContext = useActiveDocContext(docsPluginId);
const versions = useVersions(docsPluginId);
const {savePreferredVersionName} = useDocsPreferredVersion(docsPluginId);
Expand All @@ -40,7 +42,8 @@ export default function DocsVersionDropdownNavbarItem({
getVersionMainDoc(version);
return {
label: version.label,
to: versionDoc.path,
// preserve ?search#hash suffix on version switches
to: `${versionDoc.path}${search}${hash}`,
isActive: () => version === activeDocContext.activeVersion,
onClick: () => savePreferredVersionName(version.name),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import React from 'react';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import {useAlternatePageUtils} from '@docusaurus/theme-common/internal';
import {translate} from '@docusaurus/Translate';
import {useLocation} from '@docusaurus/router';
import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem';
import IconLanguage from '@theme/Icon/Language';
import type {LinkLikeNavbarItemProps} from '@theme/NavbarItem';
Expand All @@ -26,12 +27,15 @@ export default function LocaleDropdownNavbarItem({
i18n: {currentLocale, locales, localeConfigs},
} = useDocusaurusContext();
const alternatePageUtils = useAlternatePageUtils();
const {search, hash} = useLocation();

const localeItems = locales.map((locale): LinkLikeNavbarItemProps => {
const to = `pathname://${alternatePageUtils.createUrl({
const baseTo = `pathname://${alternatePageUtils.createUrl({
locale,
fullyQualified: false,
})}`;
// preserve ?search#hash suffix on locale switches
const to = `${baseTo}${search}${hash}`;
return {
label: localeConfigs[locale]!.label,
lang: localeConfigs[locale]!.htmlLang,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ export function useHideableNavbar(hideOnScroll: boolean): {
return;
}

if (locationChangeEvent.location.hash) {
// See https://github.com/facebook/docusaurus/pull/8059#issuecomment-1239639480
const currentHash = locationChangeEvent.location.hash;
const currentHashAnchor = currentHash
? document.getElementById(currentHash.substring(1))
: undefined;

if (currentHashAnchor) {
isFocusedAnchor.current = true;
setIsNavbarVisible(false);
return;
Expand Down

0 comments on commit a492025

Please sign in to comment.