BACK TO PORTFOLIO

nthemes

A minimal command-line utility to scaffold next-themes setups pre-configured with CSS view transition effects.

npx nthemes
GitHub

Transitions

Select Style

Live Preview sandboxACTIVE: Circular Ripple
Click toggle to run view transition
Circles expand dynamically from click position
ThemeToggle.tsx
Scaffold Output Code
"use client"

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

export function ThemeToggle() {
    const { theme, systemTheme, setTheme } = useTheme();
    const [mounted, setMounted] = useState(false);

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

    if (!mounted) return <div className="size-8 rounded-md bg-accent/20 animate-pulse" />;

    const toggleTheme = () => {
        if (theme === 'system') {
            setTheme(systemTheme === 'dark' ? 'light' : 'dark');
        } else {
            setTheme(theme === 'dark' ? 'light' : 'dark');
        }
    }

    const handleThemeToggle = (e: React.MouseEvent<HTMLButtonElement>) => {
            if (!document?.startViewTransition) {
                toggleTheme();
                return;
            }
    
            // Set click coordinates as CSS variables for cursor-relative animation
            document.documentElement.style.setProperty("--x", `${e.clientX}px`);
            document.documentElement.style.setProperty("--y", `${e.clientY}px`);
    
            document.startViewTransition(() => {
                toggleTheme();
            });
        };

    const isDark = theme === 'dark' || (theme === 'system' && systemTheme === 'dark');

    return (
        <button
            aria-label="Toggle theme"
            onClick={handleThemeToggle}
            className="rounded-md size-8 flex justify-center items-center aspect-square relative overflow-hidden bg-accent hover:bg-accent/80 active:translate-y-px transition-all duration-75 ease-out"
            type="button"
        >
            {isDark ? (
                <Moon className="size-4" />
            ) : (
                <Sun className="size-4" />
            )}
        </button>
    )
}

const Sun = (props: SVGProps<SVGSVGElement>) => (
    <svg
        xmlns="http://www.w3.org/2000/svg"
        width="24"
        height="24"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        strokeWidth="2"
        strokeLinecap="round"
        strokeLinejoin="round"
        {...props}
    >
        <circle cx="12" cy="12" r="4" />
        <path d="M12 3v1" />
        <path d="m18.364 5.636-.707.707" />
        <path d="M20 12h1" />
        <path d="m17.657 17.657.707.707" />
        <path d="M12 20v1" />
        <path d="m6.343 17.657-.707.707" />
        <path d="M3 12h1" />
        <path d="m5.636 5.636.707.707" />
    </svg>
)

const Moon = (props: SVGProps<SVGSVGElement>) => (
    <svg
        xmlns="http://www.w3.org/2000/svg"
        width="24"
        height="24"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        strokeWidth="2"
        strokeLinecap="round"
        strokeLinejoin="round"
        {...props}
    >
        <path d="M20.985 12.486a9 9 0 1 1-9.473-9.472c.405-.022.617.46.402.803a6 6 0 0 0 8.268 8.268c.344-.215.825-.004.803.401" />
    </svg>
)

CLI Simulator

Interactive Setup flow

cli interactive simulator
$ npx nthemes
Initializing scaffolder...
?Use TypeScript?(Yes)
Scaffolding Tree

The generated file directory matches options configured in the CLI Simulator on the left.

root/
src/
app/
globals.css
Provider.tsx
components/
ThemeProvider.tsx
ThemeToggle.tsx
Files are scaffolding ready. Check React client component hooks inside the generated code.

Scaffold Guide

Manual Instructions

1

Execute Utility

Run the quick scaffold utility inside your local project directory:

npx nthemes
2

Import Provider

Wrap your main root layout wrapper inside the generated ThemeProvider:

Layout theme provider block
3

Embed Component

Embed the generated toggle component anywhere inside your site headers or page panels:

<ThemeToggle />