Skip to content

Commit

Permalink
feat: add LanguageSwitcherMobile
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc committed Aug 27, 2023
1 parent 95972ab commit c2d0ac9
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
53 changes: 53 additions & 0 deletions src/components/atoms/LanguageSwitcher/LanguageSwitcherMobile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import Image from 'next/image';
import * as React from 'react';
import { useEffect, useState } from 'react';

import i18n from '@/app/(public)/i18n';

export interface LanguageSwitcherProps {
languages: LanguageDef[];
}

export interface LanguageDef {
label: string;
value: string;
flag: string;
disabled?: boolean;
}

const LanguageSwitcherMobile = ({ languages }: LanguageSwitcherProps) => {
const [_, setMounted] = useState(false);

// When mounted on client, now we can show the UI
useEffect(() => setMounted(true), []);

const [__, setChosenLanguage] = useState('');

const handleChange = (newLang: string) => {
setChosenLanguage(newLang);
i18n.changeLanguage(newLang);
};

const flagSize = 20;

return (
<div className='flex flex-row'>
{languages.map(
(lang) =>
!lang.disabled && (
<Image
key={lang.value}
className='mx-2 hover:drop-shadow-md'
src={lang.flag}
alt={lang.value}
height={flagSize}
width={flagSize * 1.5}
onClick={() => handleChange(lang.value)}
/>
),
)}
</div>
);
};

export default LanguageSwitcherMobile;
12 changes: 12 additions & 0 deletions src/components/molecules/MobileMenu/MobileMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { AnimatePresence, motion } from 'framer-motion';
import * as React from 'react';
import { useTranslation } from 'react-i18next';

import LanguageSwitcherMobile from '@/components/atoms/LanguageSwitcher/LanguageSwitcherMobile';
import { NavigationItem } from '@/components/atoms/NavigationItem';
import ModeToggleButton from '@/components/buttons/ModeToggleButton';
import { links } from '@/components/layout/Header';

import languages from '@/locales/languages.json';

export interface MobileMenuProps {
isOpen: boolean;
}
Expand Down Expand Up @@ -66,6 +69,15 @@ export const MobileMenu = ({ isOpen }: MobileMenuProps) => {
>
<ModeToggleButton />
</motion.li>
<motion.li
className='flex justify-center'
variants={navigationVariants}
initial='hidden'
animate='visible'
custom={0.5 + (links.length + 2) * 0.1}
>
<LanguageSwitcherMobile languages={languages} />
</motion.li>
</ul>
</FocusTrap>
</motion.div>
Expand Down
4 changes: 2 additions & 2 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"headerMenu": {
"about": "About",
"aboutWork": "Work & Career",
"aboutWorkMobile": "About - Work & Career",
"aboutWorkMobile": "About ~ Work & Career",
"aboutFreeTime": "Free Time",
"aboutFreeTimeMobile": "About - Free Time",
"aboutFreeTimeMobile": "About ~ Free Time",
"projects": "Projects",
"cv": "CV",
"uses": "Uses",
Expand Down
10 changes: 5 additions & 5 deletions src/locales/it.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"headerMenu": {
"about": "Su di me",
"aboutWork": "Carriera Lavorativa",
"aboutWorkMobile": "Su di me - Carriera Lavorativa",
"about": "About",
"aboutWork": "Lavoro",
"aboutWorkMobile": "About ~ Lavoro",
"aboutFreeTime": "Tempo Libero",
"aboutFreeTimeMobile": "Su di me - Tempo Libero",
"aboutFreeTimeMobile": "About ~ Tempo Libero",
"projects": "Progetti",
"cv": "CV",
"uses": "Uses",
"uses": "Cose che uso",
"contact": "Contatti"
},
"footerMenu": {
Expand Down
3 changes: 3 additions & 0 deletions src/locales/languages.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
[
{
"label": "🇬🇧 EN",
"flag": "https://res.cloudinary.com/dwrurydlt/image/upload/v1692716604/uk_657deb6132.svg",
"value": "en"
},
{
"label": "🇮🇹 IT",
"flag": "https://res.cloudinary.com/dwrurydlt/image/upload/v1692716604/it_00c57db1fb.svg",
"value": "it"
},
{
"label": "🇩🇪 DE",
"flag": "https://res.cloudinary.com/dwrurydlt/image/upload/v1692716604/de_9c5221adfa.svg",
"value": "de",
"disabled": true
}
Expand Down

0 comments on commit c2d0ac9

Please sign in to comment.