-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Agrim Jain
authored and
Agrim Jain
committed
May 9, 2024
1 parent
a8cf15c
commit 9645617
Showing
5 changed files
with
162 additions
and
8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React from 'react'; | ||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
import { useAlternatePageUtils } from '@docusaurus/theme-common/internal'; | ||
import { useLocation } from '@docusaurus/router'; | ||
import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem'; | ||
import type { LinkLikeNavbarItemProps } from '@theme/NavbarItem'; | ||
import type { Props } from '@theme/NavbarItem/LocaleDropdownNavbarItem'; | ||
import classnames from 'classnames'; | ||
import './locale-dropdown-navbar-item.scss'; | ||
|
||
export default function LocaleDropdownNavbarItem({ | ||
dropdownItemsBefore, | ||
dropdownItemsAfter, | ||
...props | ||
}: Props): JSX.Element { | ||
const { | ||
i18n: { currentLocale, locales, localeConfigs }, | ||
} = useDocusaurusContext(); | ||
const alternatePageUtils = useAlternatePageUtils(); | ||
const { search, hash } = useLocation(); | ||
|
||
const localeItems = locales.map((locale): LinkLikeNavbarItemProps => { | ||
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, | ||
to, | ||
target: '_self', | ||
autoAddBaseUrl: false, | ||
className: classnames({ 'dropdown__link--active': locale === currentLocale }), | ||
}; | ||
}); | ||
|
||
const getShortNames = (locale) => { | ||
switch (locale) { | ||
case 'en': | ||
return 'EN'; | ||
case 'es': | ||
return 'ES'; | ||
case 'fr': | ||
return 'FR'; | ||
default: | ||
return 'EN'; | ||
} | ||
}; | ||
|
||
const items = [...dropdownItemsBefore, ...localeItems, ...dropdownItemsAfter]; | ||
const dropdownLabel = getShortNames(currentLocale); | ||
|
||
return ( | ||
<div className='language_switcher'> | ||
<DropdownNavbarItem {...props} label={<>{dropdownLabel}</>} items={items} /> | ||
</div> | ||
); | ||
} |
80 changes: 80 additions & 0 deletions
80
src/theme/NavbarItem/LocaleDropdownNavbarItem/locale-dropdown-navbar-item.scss
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,80 @@ | ||
.language_switcher { | ||
position: relative; | ||
.dropdown { | ||
position: relative; | ||
} | ||
.dropdown > .navbar__link:after { | ||
display: none; | ||
} | ||
|
||
.dropdown__menu { | ||
position: absolute; | ||
display: flex; | ||
width: 100vw; | ||
min-width: 1280px; | ||
padding-bottom: 32px; | ||
flex-direction: column; | ||
align-items: center; | ||
gap: 24px; | ||
background-color: var(--ifm-navbar-background-color); | ||
box-shadow: var(--ifm-navbar-shadow); | ||
overscroll-behavior-y: contain; | ||
overflow-y: auto; | ||
align-items: flex-end; | ||
z-index: 999; | ||
} | ||
|
||
.navbar__items--right .dropdown__link { | ||
font-weight: normal; | ||
font-style: normal; | ||
line-height: 24px; | ||
font-size: 18px; | ||
@media (max-width: 1201px) { | ||
font-size: rem(1.4); | ||
} | ||
} | ||
|
||
.navbar__items--right a { | ||
@media (max-width: 1201px) { | ||
font-size: rem(1.4); | ||
} | ||
} | ||
|
||
.dropdown { | ||
background-image: url('/img/language-switcher.svg'); | ||
background-repeat: no-repeat; | ||
display: flex; | ||
padding: 2px 22px; | ||
align-items: center; | ||
gap: 4px; | ||
justify-content: flex-end; | ||
padding-top: rem(2.5); | ||
@media screen { | ||
display: grid; | ||
} | ||
} | ||
|
||
.navbar__link { | ||
font-weight: normal; | ||
} | ||
.dropdown__link { | ||
font-size: 16px; | ||
font-weight: normal; | ||
margin-top: 15px; | ||
} | ||
|
||
.dropdown:hover { | ||
.navbar__link { | ||
color: var(--colors-coral500); | ||
} | ||
} | ||
|
||
.dropdown__link--active { | ||
color: var(--colors-coral500); | ||
background-color: var(--colors-greyLight100); | ||
} | ||
|
||
.dropdown__link--active:hover { | ||
color: var(--colors-coral500); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.