Git Product home page Git Product logo

next-app-theme's Introduction

next-app-theme

Simple theming for Next.js apps using the App Directory.

  • No flashing on page load
  • Works with tailwind

Installation

npm install next-app-theme

Usage

First add the script to your root layout. Make sure to add it to the <head> of your page so that it runs before your app.

app/layout.js:

import { ThemeScript } from "next-app-theme/theme-script";

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en" suppressHydrationWarning>
      <head>
        <ThemeScript />
      </head>
      <body>{children}</body>
    </html>
  );
}

Then use the theme in your app/global.css:

.light {
  color-scheme: light;
}

.dark {
  color-scheme: dark;
}

Then create a component to toggle the theme:

"use client";

import { useTheme } from "next-app-theme/use-theme";
import { Sun, Moon } from "lucide-react";

function ThemeToggle() {
  const { theme, toggleThem } = useTheme();
  const icon = theme === "dark" ? <Sun /> : <Moon />;

  return <button onClick={toggleThem}>{icon}</button>;
}

Then use the toggle somewhere in your app:

// Don't SSR the toggle since the value on the server will be different than the client
const SetThemeButton = dynamic(() => import("./ui/SetThemeButton"), {
  ssr: false,
  // Make sure to code a placeholder so the UI doesn't jump when the component loads
  loading: () => <div className="w-9 h-9" />,
});

export function Header() {
  return (
    <header className="flex justify-between items-center">
      <h1 className="text-2xl font-bold">My App</h1>
      <SetThemeButton />
    </header>
  );
}

Inspiration

next-app-theme's People

Contributors

hipstersmoothie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.