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

Add theme support (dark/light/system mode) #51

Closed
wants to merge 1 commit into from
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
6 changes: 4 additions & 2 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"compilerOptions": {
"baseUrl": "src/",
"paths": {
"@/*": ["./*"]
"@/*": [
"./*"
]
}
}
}
}
24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@
"lint:fix": "next lint --fix"
},
"dependencies": {
"@headlessui/react": "^1.4.2",
"axios": "^0.21.1",
"@headlessui/react": "^1.7.19",
"axios": "^0.21.4",
"next": "^14.2.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"next-themes": "^0.3.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.2.1",
"swr": "^1.3.0"
},
"devDependencies": {
"@babel/eslint-parser": "^7.12.1",
"@babel/preset-react": "^7.18.6",
"@babel/eslint-parser": "^7.24.6",
"@babel/preset-react": "^7.24.6",
"@next/eslint-plugin-next": "^14.2.3",
"@tailwindcss/forms": "^0.5.2",
"autoprefixer": "^10.4.2",
"eslint": "^8.27.0",
"eslint-config-next": "^14.0.3",
"@tailwindcss/forms": "^0.5.7",
"autoprefixer": "^10.4.19",
"eslint": "^8.57.0",
"eslint-config-next": "^14.2.3",
MaalTi marked this conversation as resolved.
Show resolved Hide resolved
"eslint-config-prettier": "^7.2.0",
"postcss": "^8.4.6",
"postcss": "^8.4.38",
"prettier": "^3.2.5",
"tailwindcss": "^3.4.3"
}
Expand Down
4 changes: 3 additions & 1 deletion src/app/LoginLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import Link from 'next/link'
import { useAuth } from '@/hooks/auth'
import ThemeSwitch from '@/components/ThemeSwitch'

const LoginLinks = () => {
const { user } = useAuth({ middleware: 'guest' })

return (
<div className="hidden fixed top-0 right-0 px-6 py-4 sm:block">
<div className="fixed top-0 right-0 hidden px-6 py-4 sm:block">
<span className="pr-2"><ThemeSwitch/></span>
{user ? (
<Link
href="/dashboard"
Expand Down
2 changes: 1 addition & 1 deletion src/app/global.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import url("https://fonts.bunny.net/css2?family=Nunito:wght@400;600;700&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Nunito:ital,wght@0,200..1000;1,200..1000&display=swap");

@tailwind base;
@tailwind components;
Expand Down
5 changes: 4 additions & 1 deletion src/app/layout.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import '@/app/global.css'
import ThemeWrapper from '@/providers/ThemeWrapper'

export const metadata = {
title: 'Laravel',
}
const RootLayout = ({ children }) => {
return (
<html lang="en">
<body className="antialiased">{children}</body>
<body className="antialiased bg-slate-100 dark:bg-slate-900">
<ThemeWrapper attribute="class" defaultTheme="system">{children}</ThemeWrapper>
</body>
</html>
)
}
Expand Down
50 changes: 50 additions & 0 deletions src/components/ThemeSwitch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use client"

import { FiSun, FiMoon, FiSettings } from "react-icons/fi"
import { useState, useEffect } from "react"
import { useTheme } from "next-themes"
import Image from "next/image"

export default function ThemeSwitch() {
const [mounted, setMounted] = useState(false)
const { setTheme, theme } = useTheme()

useEffect(() => setMounted(true), [])

if (!mounted)
return (
<Image
src="data:image/svg+xml;base64,PHN2ZyBzdHJva2U9IiNGRkZGRkYiIGZpbGw9IiNGRkZGRkYiIHN0cm9rZS13aWR0aD0iMCIgdmlld0JveD0iMCAwIDI0IDI0IiBoZWlnaHQ9IjIwMHB4IiB3aWR0aD0iMjAwcHgiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHJlY3Qgd2lkdGg9IjIwIiBoZWlnaHQ9IjIwIiB4PSIyIiB5PSIyIiBmaWxsPSJub25lIiBzdHJva2Utd2lkdGg9IjIiIHJ4PSIyIj48L3JlY3Q+PC9zdmc+Cg=="
width={20}
height={20}
alt=""
priority={false}
/>
)

return (
<button
className="text-amber-400 border-amber-400 bg-yellow-100 dark:text-sky-400 dark:bg-cyan-100 dark:border-sky-400 rounded-lg p-1.5 border"
title={
theme === "system"
? "system"
: theme === "dark"
? "dark theme"
: "light theme"
}
onClick={() =>
theme === "system"
? setTheme("dark")
: theme === "dark"
? setTheme("light")
: setTheme("system")
}
>
{theme === "system" ? (
<FiSettings />
) : theme === "dark" ? (
<FiMoon />
) : <FiSun />}
</button>
)
}
22 changes: 22 additions & 0 deletions src/providers/ThemeWrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use client"

import { ThemeProvider } from "next-themes"
import { useState, useEffect } from "react"

export default function ThemeWrapper({ children, ...props }) {
const [mounted, setMounted] = useState(false)

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

if (!mounted) {
return <>{children}</>
}

return (
<ThemeProvider {...props}>
{children}
</ThemeProvider>
)
}
53 changes: 52 additions & 1 deletion tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,63 @@ const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
content: ['./src/**/*.js'],
darkMode: 'media',
darkMode: 'class',
theme: {
extend: {
fontFamily: {
sans: ['Nunito', ...defaultTheme.fontFamily.sans],
},
typography: (theme) => ({
DEFAULT: {
css: {
'--tw-prose-body': theme('colors.neutral.DEFAULT'),
'--tw-prose-headings': theme('colors.neutral.DEFAULT'),
'--tw-prose-lead': theme('colors.neutral.DEFAULT'),
'--tw-prose-links': theme('colors.neutral.DEFAULT'),
'--tw-prose-bold': theme('colors.neutral.DEFAULT'),
'--tw-prose-counters': theme('colors.neutral.DEFAULT'),
'--tw-prose-bullets': theme('colors.neutral.DEFAULT'),
'--tw-prose-hr': theme('colors.neutral.DEFAULT'),
'--tw-prose-quotes': theme('colors.neutral.DEFAULT'),
'--tw-prose-quote-borders': theme('colors.primary.DEFAULT'),
'--tw-prose-captions': theme('colors.neutral.DEFAULT'),
'--tw-prose-code': theme('colors.neutral.DEFAULT'),
'--tw-prose-pre-code': theme('colors.white'),
'--tw-prose-pre-bg': theme('colors.neutral.DEFAULT'),
'--tw-prose-th-borders': theme('colors.neutral.DEFAULT'),
'--tw-prose-td-borders': theme('colors.neutral.DEFAULT'),
'--tw-prose-invert-body': theme('colors.white'),
'--tw-prose-invert-headings': theme('colors.white'),
'--tw-prose-invert-lead': theme('colors.white'),
'--tw-prose-invert-links': theme('colors.white'),
'--tw-prose-invert-bold': theme('colors.white'),
'--tw-prose-invert-counters': theme('colors.white'),
'--tw-prose-invert-bullets': theme('colors.white'),
'--tw-prose-invert-hr': theme('colors.white'),
'--tw-prose-invert-quotes': theme('colors.white'),
'--tw-prose-invert-quote-borders': theme('colors.white'),
'--tw-prose-invert-captions': theme('colors.white'),
'--tw-prose-invert-code': theme('colors.white'),
'--tw-prose-invert-pre-code': theme('colors.neutral.DEFAULT'),
'--tw-prose-invert-pre-bg': theme('colors.white'),
'--tw-prose-invert-th-borders': theme('colors.white'),
'--tw-prose-invert-td-borders': theme('colors.white'),
'ul > li > p, ol > li > p': {
marginTop: '0em !important',
marginBottom: '0em !important',
},
':where(.prose > div > :first-child)': {
marginTop: '0 !important',
},
':where(.prose > div > :last-child)': {
marginBottom: '0 !important',
},
'.not-prose': {
margin: '2rem 0 !important',
}
}
}
}),
},
},
variants: {
Expand Down