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

Agrim/dapi 547/languageswitcher #34

Closed
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
11 changes: 10 additions & 1 deletion docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ const config = {
// to replace "en" with "zh-Hans".
i18n: {
defaultLocale: 'en',
locales: ['en'],
locales: ['en', 'es', 'fr'],
localeConfigs: {
en: {
label: 'English',
},
},
},

plugins: [
Expand Down Expand Up @@ -118,6 +123,10 @@ const config = {
type: 'custom-user-navbar-item',
position: 'right',
},
{
type: 'localeDropdown',
position: 'right',
},
],
},
prism: {
Expand Down
9 changes: 2 additions & 7 deletions src/styles/index.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@use 'src/styles/utility' as *;
@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;700&family=Ubuntu:wght@400;500;700&display=swap');
@import '@deriv/quill-design/dist/quill-design.css';

/**
* Any CSS included here will be global. The classic template
Expand Down Expand Up @@ -41,10 +40,6 @@
--smoke: #414652;
--admin-text: #22bd41;
--admin-border: #33c9517a;
--solid-slate-50: #ffffff;
--solid-slate-75: #f6f7f8;
--opacity-black-100: #00000014;
--opacity-black-75: #0000000a;
}

/* For readability concerns, you should choose a lighter palette in dark mode. */
Expand Down Expand Up @@ -84,7 +79,7 @@ button {
ul li a,
nav a,
nav a div {
font-family: var(--ubuntu-font-family);
font-family: var(--ibm-font-family-base);
}

h1,
Expand Down Expand Up @@ -242,7 +237,7 @@ div[class*='sidebarViewport'] {
.search-overlay {
display: none;
}
&.search-closed + div[class*='searchBox'] {
&.search-closed ~ div[class*='searchBox'] {
display: none;
}
&.search-open {
Expand Down
69 changes: 69 additions & 0 deletions src/theme/Navbar/MobileSidebar/PrimaryMenu/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React from 'react';
import { useThemeConfig, ErrorCauseBoundary } from '@docusaurus/theme-common';
import { splitNavbarItems, useNavbarMobileSidebar } from '@docusaurus/theme-common/internal';
import NavbarItem from '@theme/NavbarItem';
import './primary-menu.scss';
import { Button } from '@deriv/ui';

export function useNavbarItems() {
// TODO temporary casting until ThemeConfig type is improved
return useThemeConfig().navbar.items;
}

export default function CustomMobileSidebar() {
const [languageSidebarVisible, setLanguageSidebarVisible] = React.useState(false);
const mobileSidebar = useNavbarMobileSidebar();
const items = useNavbarItems();
const [leftItems] = splitNavbarItems(items);

React.useEffect(() => {
if (!mobileSidebar?.shown) {
setLanguageSidebarVisible(false);
}
}, [mobileSidebar]);

const toggleLanguageSidebar = () => {
setLanguageSidebarVisible(!languageSidebarVisible);
};

return (
<React.Fragment>
<div>
{leftItems.map((item, i) => (
<ErrorCauseBoundary
key={i}
onError={(error) =>
new Error(
`A theme navbar item failed to render.
Please double-check the following navbar item (themeConfig.navbar.items) of your Docusaurus config:
${JSON.stringify(item, null, 2)}`,
)
}
>
<NavbarItem
{...item}
onClick={() => {
mobileSidebar.toggle();
}}
/>
</ErrorCauseBoundary>
))}
</div>
<div className='navbar__item navbar__link' onClick={toggleLanguageSidebar}>
Language Switcher Placeholder
</div>

{/* Conditionally render the language sidebar */}
<div className={`language_sidebar ${languageSidebarVisible ? 'visible' : ''}`}>
<Button onClick={toggleLanguageSidebar} className='language_sidebar__buttons'>
Back
</Button>
<div className='language_sidebar__items'>
<div>En</div>
<div>Fr</div>
<div>Th</div>
</div>
</div>
</React.Fragment>
);
}
55 changes: 55 additions & 0 deletions src/theme/Navbar/MobileSidebar/PrimaryMenu/primary-menu.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@media (max-width: 996px) {
.navbar-sidebar__item {
.navbar__item {
display: block;
padding: var(--ifm-menu-link-padding-vertical) var(--ifm-menu-link-padding-horizontal);
font-size: 0.875rem;
font-weight: var(--ifm-font-weight-normal);
line-height: 1.25rem;
}

.navbar__link {
&--active {
background-color: var(--ifm-menu-color-background-active);
}
}

/* Style for the language sidebar */
.language_sidebar {
position: fixed;
top: 0;
left: 0;
background-color: var(--ifm-navbar-background-color);
width: var(--ifm-navbar-sidebar-width);
height: 100%;
opacity: 0;
visibility: hidden;
transform: translateX(-100%);
transition-property: opacity, visibility, transform;
transition-duration: var(--ifm-transition-fast);
transition-timing-function: ease-in-out;

&.visible {
opacity: 1;
visibility: visible;
transform: translateX(0);
}

&__buttons {
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid var(--ifm-navbar-border-color);
}

&__items {
margin: 18px;
display: flex;
height: calc(100% - var(--ifm-navbar-height));
transform: translateZ(0);
transition: transform var(--ifm-transition-fast) ease-in-out;
flex-direction: column;
}
}
}
}
60 changes: 60 additions & 0 deletions src/theme/NavbarItem/LocaleDropdownNavbarItem/index.tsx
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>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
.language_switcher {
.dropdown {
position: relative;
}
.dropdown > .navbar__link:after {
display: none;
}

.dropdown__menu {
position: fixed;
display: flex;
top: 80px;
left: 0px;
padding-right: 100px;
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);
}
}
10 changes: 10 additions & 0 deletions static/img/language-switcher.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading