-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support multi version menu in nav (#206)
- Loading branch information
1 parent
6b8f55e
commit 887b0cb
Showing
12 changed files
with
268 additions
and
167 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@rspress/shared': patch | ||
'@rspress/core': patch | ||
--- | ||
|
||
feat: support multi version menu in 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
16 changes: 16 additions & 0 deletions
16
packages/core/src/theme-default/components/Nav/NavTranslations.tsx
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { NavMenuGroup } from './NavMenuGroup'; | ||
import styles from './index.module.scss'; | ||
import { useTranslationMenuData } from './menuDataHooks'; | ||
|
||
export function NavTranslations() { | ||
const translationMenuData = useTranslationMenuData(); | ||
return ( | ||
<div | ||
className={`translation ${styles.menuItem} flex text-sm font-bold items-center px-3 py-2`} | ||
> | ||
<div> | ||
<NavMenuGroup {...translationMenuData} /> | ||
</div> | ||
</div> | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/core/src/theme-default/components/Nav/NavVersions.tsx
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { NavMenuGroup } from './NavMenuGroup'; | ||
import styles from './index.module.scss'; | ||
import { useVersionMenuData } from './menuDataHooks'; | ||
|
||
export function NavVersions() { | ||
const versionsMenuData = useVersionMenuData(); | ||
return ( | ||
<div | ||
className={`translation ${styles.menuItem} flex text-sm font-bold items-center px-3 py-2`} | ||
> | ||
<div> | ||
<NavMenuGroup {...versionsMenuData} /> | ||
</div> | ||
</div> | ||
); | ||
} |
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
75 changes: 75 additions & 0 deletions
75
packages/core/src/theme-default/components/Nav/menuDataHooks.tsx
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { replaceLang, replaceVersion } from '@rspress/shared'; | ||
import Translator from '../../assets/translator.svg'; | ||
import { useLocation, usePageData, useVersion } from '@/runtime'; | ||
|
||
export function useTranslationMenuData() { | ||
const { siteData, page } = usePageData(); | ||
const currentVersion = useVersion(); | ||
const { pathname } = useLocation(); | ||
const defaultLang = siteData.lang || ''; | ||
const defaultVersion = siteData.multiVersion.default || ''; | ||
const localeLanguages = Object.values( | ||
siteData.locales || siteData.themeConfig.locales || {}, | ||
); | ||
const hasMultiLanguage = localeLanguages.length > 1; | ||
const { lang: currentLang } = page; | ||
const { base } = siteData; | ||
|
||
const translationMenuData = hasMultiLanguage | ||
? { | ||
text: ( | ||
<Translator | ||
style={{ | ||
width: '18px', | ||
height: '18px', | ||
}} | ||
/> | ||
), | ||
items: localeLanguages.map(item => ({ | ||
text: item?.label, | ||
link: replaceLang( | ||
pathname, | ||
{ | ||
current: currentLang, | ||
target: item.lang, | ||
default: defaultLang, | ||
}, | ||
{ | ||
current: currentVersion, | ||
default: defaultVersion, | ||
}, | ||
base, | ||
), | ||
})), | ||
activeValue: localeLanguages.find(item => currentLang === item.lang) | ||
?.label, | ||
} | ||
: null; | ||
return translationMenuData; | ||
} | ||
|
||
export function useVersionMenuData() { | ||
const { siteData } = usePageData(); | ||
const currentVersion = useVersion(); | ||
const { pathname } = useLocation(); | ||
const defaultVersion = siteData.multiVersion.default || ''; | ||
const versions = siteData.multiVersion.versions || []; | ||
const { base } = siteData; | ||
const versionsMenuData = { | ||
items: versions.map(version => ({ | ||
text: version, | ||
link: replaceVersion( | ||
pathname, | ||
{ | ||
current: currentVersion, | ||
target: version, | ||
default: defaultVersion, | ||
}, | ||
base, | ||
), | ||
})), | ||
text: currentVersion, | ||
activeValue: currentVersion, | ||
}; | ||
return versionsMenuData; | ||
} |
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
Oops, something went wrong.