Skip to content

Commit

Permalink
feat: select language wip
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc committed Aug 27, 2023
1 parent 590725a commit f6f6c61
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 45 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
},
"dependencies": {
"@apollo/client": "^3.8.1",
"@chakra-ui/react": "^2.8.0",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@heroicons/react": "^2.0.18",
Expand Down
21 changes: 13 additions & 8 deletions src/app/(public)/layout-client.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
'use client';

import { ThemeProvider } from 'next-themes';
import type { PropsWithChildren } from 'react';
import {ThemeProvider as NextThemeProvider, } from 'next-themes';
import type {PropsWithChildren} from 'react';

import MuiThemeProvider from "@/components/helpers/MuiThemeProvider";
import Footer from '@/components/layout/Footer';
import Header from '@/components/layout/Header';

export default function LayoutClient({ children }: PropsWithChildren) {
export default function LayoutClient({children}: PropsWithChildren) {

return (
<ThemeProvider attribute='class' defaultTheme='light' enableSystem={false}>
<Header />
<NextThemeProvider attribute='class' defaultTheme='light' enableSystem={false}>
<MuiThemeProvider >

<Header/>

<main id='content'>{children}</main>
<main id='content'>{children}</main>

<Footer />
</ThemeProvider>
<Footer/>
</MuiThemeProvider>
</NextThemeProvider>
);
}
113 changes: 77 additions & 36 deletions src/components/atoms/LanguageSwitcher/LanguageSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {InputLabel, MenuItem, SelectChangeEvent} from '@mui/material';
import {InputLabel, MenuItem, Select, SelectChangeEvent} from '@mui/material';
import FormControl from '@mui/material/FormControl';
import {useState} from 'react';
import {useEffect, useState} from 'react';

import i18n from '@/app/(public)/i18n';
import clsx from "clsx";
Expand All @@ -20,9 +20,14 @@ export interface LanguageDef {
}

const LanguageSwitcher = ({ languages } : LanguageSwitcherProps) => {
const { theme } = useTheme();
const { theme, resolvedTheme, setTheme } = useTheme();
const isDarkMode = theme === 'dark';

const [mounted, setMounted] = useState(false);

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

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

const handleChange = (event: SelectChangeEvent) => {
Expand All @@ -47,48 +52,84 @@ const LanguageSwitcher = ({ languages } : LanguageSwitcherProps) => {

return (
<div className="p-2">
<select onChange={changeLanguage}
className="rounded-md border-2 dark:bg-transparent"
>
{languages.map(
(lang: LanguageDef) =>
!lang.disabled && (
<option
style={{fontSize: '14px', fontWeight: 300}}
key={lang.value}
value={lang.value}
>
{lang.label}
</option>
)
)}
</select>

{/*<FormControl sx={{m: 1, minWidth: 80, border: '2px solid white', color: isDarkMode ? 'white' : 'black'}} size='small'>*/}
{/* <InputLabel id='demo-select-small-label'>{label}</InputLabel>*/}
{/* <Select*/}
{/* id='demo-select-small'*/}
{/* labelId='demo-select-small-label'*/}
{/* value={chosenLanguage}*/}
{/* label={label}*/}
{/* onChange={handleChange}*/}
{/* style={{ fontSize: '14px' }}*/}
{/* >*/}
{/* {languages.map(*/}
{/*<Select*/}
{/* labelId="demo-simple-select-helper-label"*/}
{/* id="demo-simple-select-helper"*/}
{/* value={theme}*/}
{/* onChange={handleChange}*/}
{/*>*/}
{/* {languages.map(*/}
{/* (lang: LanguageDef) =>*/}
{/* !lang.disabled && (*/}
{/* <MenuItem*/}
{/* className='font-light'*/}
{/* <option*/}
{/* className="option"*/}
{/* style={{fontSize: '14px', fontWeight: 300}}*/}
{/* key={lang.value}*/}
{/* value={lang.value}*/}
{/* >*/}
{/* {lang.label}*/}
{/* </MenuItem>*/}
{/* </option>*/}
{/* )*/}
{/* )}*/}
{/* </Select>*/}
{/*</FormControl>*/}
{/*</Select>*/}

{/*<select onChange={changeLanguage}*/}
{/* className="select rounded-md border-2 dark:bg-transparent"*/}
{/*>*/}
{/* {languages.map(*/}
{/* (lang: LanguageDef) =>*/}
{/* !lang.disabled && (*/}
{/* <option*/}
{/* className="option"*/}
{/* style={{fontSize: '14px', fontWeight: 300}}*/}
{/* key={lang.value}*/}
{/* value={lang.value}*/}
{/* >*/}
{/* {lang.label}*/}
{/* </option>*/}
{/* )*/}
{/* )}*/}
{/*</select>*/}

<FormControl sx={{m: 1, minWidth: 80, fontSize: '1.5rem' }} size='small'>
<InputLabel id='demo-select-small-label'>{label}</InputLabel>
<Select
id='demo-select-small'
labelId='demo-select-small-label'
value={chosenLanguage}
label={label}
onChange={handleChange}
// style={{ fontSize: '14px' }}
// sx={{
// '& .MuiOutlinedInput-notchedOutline': {
// borderColor: isDarkMode ? 'white' : 'black'
// },
// '& .MuiSelect-iconOutlined': {
// color: isDarkMode ? 'white' : 'black'
// },
// '& .MuiOutlinedInput-input': {
// color: isDarkMode ? 'white' : 'black'
// },
// '& .css-1jy569b-MuiFormLabel-root-MuiInputLabel-root': {
// color: isDarkMode ? 'white' : 'black'
// }
// }}
>
{languages.map(
(lang: LanguageDef) =>
!lang.disabled && (
<MenuItem
className='font-light'
style={{fontSize: '14px', fontWeight: 300}}
key={lang.value}
value={lang.value}
>
{lang.label}
</MenuItem>
)
)}
</Select>
</FormControl>
</div>
);
};
Expand Down
29 changes: 29 additions & 0 deletions src/components/helpers/MuiThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { GlobalStyles } from "@mui/material";
import { CssBaseline, ThemeProvider } from "@mui/material";
import { useTheme } from "next-themes";
import { FC, useEffect, useState } from "react";

import { darkTheme, globalStyles, lightTheme } from "@/theme";

const MUIThemeProvider: FC<{ children: React.ReactNode }> = ({
children,
}) => {
const { resolvedTheme } = useTheme();
const [currentTheme, setCurrentTheme] = useState(lightTheme);

useEffect(() => {
resolvedTheme === "light"
? setCurrentTheme(lightTheme)
: setCurrentTheme(darkTheme);
}, [resolvedTheme]);

return (
<ThemeProvider theme={currentTheme}>
<CssBaseline />
<GlobalStyles styles={globalStyles} />
{children}
</ThemeProvider>
);
};

export default MUIThemeProvider;
7 changes: 7 additions & 0 deletions src/theme/createEmotionCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import createCache from "@emotion/cache";

// prepend: true moves MUI styles to the top of the <head> so they're loaded first.
// It allows developers to easily override MUI styles with other styling solutions, like CSS modules.
export default function createEmotionCache() {
return createCache({ key: "css", prepend: true });
}
37 changes: 37 additions & 0 deletions src/theme/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createTheme, css, PaletteOptions } from "@mui/material/styles";

export type AllowedTheme = NonNullable<PaletteOptions["mode"]>;

export const DEFAULT_THEME: AllowedTheme = "light";

export const lightTheme = createTheme({
palette: {
primary: { main: "#9147FF" },
secondary: { main: "#2a48f3" },
mode: "light",
},
});

export const darkTheme = createTheme({
palette: {
primary: { main: "#9147FF" },
secondary: { main: "#2a48f3" },
mode: "dark",
},
});

export const globalStyles = css`
//:root {
// body {
// background-color: #fff;
// color: #121212;
// }
//}
//
//[data-theme="dark"] {
// body {
// background-color: #121212;
// color: #fff;
// }
//}
`;

0 comments on commit f6f6c61

Please sign in to comment.