A place to share technical learnings, etc.
aws | jekyll | github | apigateway | serverless | ad | powershell | windows | webdev | nodejs | terraform | consul | nomad | jenkins | traefik | azuread | azure | nextjs |
For some nextjs sites, it’s nice to have dark/light mode support. next-themes makes this relatively easy, although it’s not super intuitive how to set this up.
npx create-next-app@latest
cd <yourprojectname>
npm i next-themes
npm run dev
@tailwind base;
@tailwind components;
@tailwind utilities;
attribute="class"
) and has system theme sync enabled.
'use client'
import { ThemeProvider } from "next-themes"
export const Providers = ({children} : {children: React.ReactNode}) => {
return (
<ThemeProvider attribute="class" enableSystem>
{children}
</ThemeProvider>
)
}
darkMode: "class",
under the Config block. This tells tailwind to enable darkmode based on a class attribute (which will be provided by next-themes):
import type { Config } from "tailwindcss";
const config: Config = {
content: [
"./src/pages/**/*.{js,ts,jsx,tsx,mdx}",
"./src/components/**/*.{js,ts,jsx,tsx,mdx}",
"./src/app/**/*.{js,ts,jsx,tsx,mdx}",
],
theme: {
container: {
center: true,
padding: {
DEFAULT: "1rem",
md: "1.5rem",
lg: "2rem",
},
},
},
darkMode: "class",
plugins: [],
};
export default config;
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";
import { Providers } from './providers'
import Link from 'next/link'
const inter = Inter({ subsets: ["latin"] });
export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en" suppressHydrationWarning>
<body className={inter.className}>
<Providers>
{children}
</Providers>
</body>
</html>
);
}
At this point, you should have dark/light mode with system sync working!
tags: nextjs