Skip to content

Commit

Permalink
feat: adds theming
Browse files Browse the repository at this point in the history
  • Loading branch information
OnlyNico43 committed Jul 6, 2024
1 parent 99bb419 commit 3dcaf41
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 6 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"next": "14.2.4",
"next-connect": "1.0.0",
"next-i18n-router": "5.5.0",
"next-themes": "^0.3.0",
"react": "18.3.1",
"react-dom": "18.3.1",
"react-i18next": "14.1.2",
Expand Down
3 changes: 3 additions & 0 deletions public/locales/de/login.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"login": "Anmelden"
}
3 changes: 3 additions & 0 deletions public/locales/en/login.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"login": "Login"
}
23 changes: 23 additions & 0 deletions src/app/[locale]/login/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import TranslationsProvider from '@/src/components/translation-provider/TranslationsProvider';
import initTranslations from '@i18n';
import { FunctionComponent } from 'react';

interface HomeProps {
params: {
locale: string;
};
}

const i18nNamespaces = ['login'];

const Home: FunctionComponent<HomeProps> = async ({ params: { locale } }) => {
const { t, resources } = await initTranslations(locale, i18nNamespaces);

return (
<TranslationsProvider resources={resources} locale={locale} namespaces={i18nNamespaces}>
<h1>{t('login:login')}</h1>
</TranslationsProvider>
);
};

export default Home;
124 changes: 122 additions & 2 deletions src/app/[locale]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ThemeSwitcher } from '@/src/components/theme-switcher/ThemeSwitcher';
import TranslationsProvider from '@/src/components/translation-provider/TranslationsProvider';
import { Button } from '@nextui-org/react';
import { FunctionComponent } from 'react';
Expand All @@ -15,8 +16,127 @@ const Home: FunctionComponent<HomeProps> = async ({ params: { locale } }) => {
const { t, resources } = await initTranslations(locale, i18nNamespaces);

return (
<TranslationsProvider resources={resources} locale={locale} namespaces={i18nNamespaces}>
<Button color="primary">{t('home:button')}</Button>
<TranslationsProvider resources={resources} namespaces={i18nNamespaces} locale={locale}>
<ThemeSwitcher />
<div className="m-2">
<Button className="m-1" color="primary">
{t('home:button')}
</Button>
<Button className="m-1" color="secondary">
{t('home:button')}
</Button>
<Button className="m-1" color="success">
{t('home:button')}
</Button>
<Button className="m-1" color="warning">
{t('home:button')}
</Button>
<Button className="m-1" color="danger">
{t('home:button')}
</Button>
</div>
<div className="m-2">
<Button className="m-1" color="primary" variant="faded">
{t('home:button')}
</Button>
<Button className="m-1" color="secondary" variant="faded">
{t('home:button')}
</Button>
<Button className="m-1" color="success" variant="faded">
{t('home:button')}
</Button>
<Button className="m-1" color="warning" variant="faded">
{t('home:button')}
</Button>
<Button className="m-1" color="danger" variant="faded">
{t('home:button')}
</Button>
</div>
<div className="m-2">
<Button className="m-1" color="primary" variant="bordered">
{t('home:button')}
</Button>
<Button className="m-1" color="secondary" variant="bordered">
{t('home:button')}
</Button>
<Button className="m-1" color="success" variant="bordered">
{t('home:button')}
</Button>
<Button className="m-1" color="warning" variant="bordered">
{t('home:button')}
</Button>
<Button className="m-1" color="danger" variant="bordered">
{t('home:button')}
</Button>
</div>
<div className="m-2">
<Button className="m-1" color="primary" variant="light">
{t('home:button')}
</Button>
<Button className="m-1" color="secondary" variant="light">
{t('home:button')}
</Button>
<Button className="m-1" color="success" variant="light">
{t('home:button')}
</Button>
<Button className="m-1" color="warning" variant="light">
{t('home:button')}
</Button>
<Button className="m-1" color="danger" variant="light">
{t('home:button')}
</Button>
</div>
<div className="m-2">
<Button className="m-1" color="primary" variant="flat">
{t('home:button')}
</Button>
<Button className="m-1" color="secondary" variant="flat">
{t('home:button')}
</Button>
<Button className="m-1" color="success" variant="flat">
{t('home:button')}
</Button>
<Button className="m-1" color="warning" variant="flat">
{t('home:button')}
</Button>
<Button className="m-1" color="danger" variant="flat">
{t('home:button')}
</Button>
</div>
<div className="m-2">
<Button className="m-1" color="primary" variant="ghost">
{t('home:button')}
</Button>
<Button className="m-1" color="secondary" variant="ghost">
{t('home:button')}
</Button>
<Button className="m-1" color="success" variant="ghost">
{t('home:button')}
</Button>
<Button className="m-1" color="warning" variant="ghost">
{t('home:button')}
</Button>
<Button className="m-1" color="danger" variant="ghost">
{t('home:button')}
</Button>
</div>
<div className="m-2">
<Button className="m-1" color="primary" variant="shadow">
{t('home:button')}
</Button>
<Button className="m-1" color="secondary" variant="shadow">
{t('home:button')}
</Button>
<Button className="m-1" color="success" variant="shadow">
{t('home:button')}
</Button>
<Button className="m-1" color="warning" variant="shadow">
{t('home:button')}
</Button>
<Button className="m-1" color="danger" variant="shadow">
{t('home:button')}
</Button>
</div>
</TranslationsProvider>
);
};
Expand Down
9 changes: 8 additions & 1 deletion src/app/providers.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { NextUIProvider } from '@nextui-org/react';
import { ThemeProvider } from 'next-themes';
import { FunctionComponent, PropsWithChildren } from 'react';

const Providers: FunctionComponent<PropsWithChildren> = ({ children }) => {
return <NextUIProvider>{children}</NextUIProvider>;
return (
<NextUIProvider>
<ThemeProvider attribute="class" defaultTheme="dark">
{children}
</ThemeProvider>
</NextUIProvider>
);
};

export default Providers;
24 changes: 24 additions & 0 deletions src/components/theme-switcher/ThemeSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use client';

import { Button } from '@nextui-org/react';
import { useTheme } from 'next-themes';
import { useEffect, useState } from 'react';

export function ThemeSwitcher(): React.ReactNode {
const [mounted, setMounted] = useState(false);
const { theme, setTheme } = useTheme();

useEffect(() => {
setMounted(true);
}, []);

if (!mounted) return null;

return (
<div>
The current theme is: {theme}
<Button onClick={() => setTheme('light')}>Light Mode</Button>
<Button onClick={() => setTheme('dark')}>Dark Mode</Button>
</div>
);
}
31 changes: 30 additions & 1 deletion tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,35 @@ const config: Config = {
extend: {},
},
darkMode: 'class',
plugins: [nextui()],
plugins: [
nextui({
prefix: 'nextui', // prefix for themes variables
themes: {
light: {
colors: {
background: '#f6fefe',
foreground: '#021d1d',
primary: {
DEFAULT: '#0b7779',
foreground: '#021d1d',
},
secondary: '#f269c0',
}, // light theme colors
},
dark: {
colors: {
background: '#010909',
foreground: '#e2fdfd',
primary: {
DEFAULT: '#86f2f4',
foreground: '#021d1d',
},
secondary: '#960d64',
}, // dark theme colors
},
// ... custom themes
},
}),
],
};
export default config;
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
],
"paths": {
"@/*": ["./*"],
"@components/*": ["./src/components/*"]
"@components/*": ["./src/components/*"],
"@i18n": ["./src/app/i18n"]
}
},
"exclude": ["node_modules"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"]
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "i18n.config.ts"]
}

0 comments on commit 3dcaf41

Please sign in to comment.