mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-02 16:29:47 +02:00
fix(theme): preserve url ?search#hash on navbar version/locale dropdowns navigations (#8059)
This commit is contained in:
parent
67c692326b
commit
a492025cbf
3 changed files with 16 additions and 3 deletions
|
@ -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';
|
||||
|
@ -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);
|
||||
|
@ -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),
|
||||
};
|
||||
|
|
|
@ -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';
|
||||
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue