next-themes

Perfect dark mode for React and Next.js apps in two lines of code, with zero flash on load.

Library
npm
v0.4.6
6,336 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
45 /100 Fair
Development Activity 4
Maintenance 20
Community 56
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
86 /100 Excellent
Architecture 78
Code Quality 84
Innovation 90
Learning Curve 92

next-themes is a lightweight React library that adds complete theme support—including dark mode, light mode, and any number of custom themes—to Next.js and other React applications. It solves the two hardest parts of theme switching in an SSR/SSG environment: the flash of incorrect theme on page load and keeping theme state synchronized across browser tabs.

At its core is a ThemeProvider component and a useTheme hook that expose the active theme, the resolved system theme, and a setTheme function, while a synchronously-injected inline script updates the data-theme or class attribute on the <html> element before React hydrates, eliminating any visible flicker.

What You Get

  • ThemeProvider component that manages theme state and injects a pre-hydration script to avoid flash-of-incorrect-theme
  • useTheme hook exposing theme, resolvedTheme, systemTheme, themes, and setTheme
  • Automatic prefers-color-scheme detection with live updates when the OS theme changes
  • Cross-tab theme synchronization via the storage event
  • Support for unlimited custom theme names, not just light/dark, via the themes prop
  • Forced-theme support for locking individual pages/routes to a specific theme

Common Use Cases

  • Adding a dark mode toggle to a Next.js app/ or pages/ router app
  • Building multi-theme products (e.g. brand-selectable themes) beyond light/dark
  • Locking marketing or landing pages to a single forced theme regardless of user preference
  • Persisting and syncing a user’s theme choice across multiple open tabs

Under The Hood

Architecture The library exposes a React Context-based ThemeProvider around a single component (src/index.tsx, 263 lines) that manages theme state via useState, syncing it to localStorage and to a data-theme/class attribute on document.documentElement. To avoid a flash of the wrong theme on first paint, it injects a synchronously-executed inline <script> tag (ThemeScript, memoized) whose body is the stringified script function from src/script.ts — this runs before React hydrates, reading localStorage and applying the DOM attribute/class immediately. useEffect hooks wire up a matchMedia('(prefers-color-scheme: dark)') listener for system-theme changes and a window.addEventListener('storage', ...) listener to sync theme across tabs. src/types.ts defines the public ThemeProviderProps/UseThemeProps contracts. The package is a single flat module with no internal composition or external state library — just React Context, two effects, and one inlined bootstrap script.

Tech Stack TypeScript with only react/react-dom as peer dependencies (16.8 through 19), built via tsup into ESM and CJS bundles plus .d.ts types (minified, tree-shaken, bundle: true). Tested with Vitest (__tests__/index.test.tsx, 502 lines) plus a focused root-level test/ suite (forced-theme, storage-event, system-theme, switch-theme) and Playwright end-to-end tests (playwright.config.ts, .github/workflows/e2e.yml). The repo is a pnpm + Turborepo monorepo (pnpm-workspace.yaml, turbo.json) with the publishable package isolated under next-themes/, and GitHub Actions runs both the unit and e2e workflows on every push.

Code Quality Test coverage is strong relative to the codebase’s size — 373 lines of source paired with 500+ lines of unit tests and a 5-file Playwright e2e suite covering the trickiest edge cases (hydration timing, forced themes, storage sync, system-theme changes). The code favors a terse, framework-free style, guards localStorage access with try/catch for disabled-storage and SSR environments (isServer check), and keeps its public surface intentionally minimal. Minor rough edges: tsconfig.json sets strict: false, and a couple of empty catch (e) { // Unsupported } blocks silently swallow storage errors rather than logging — acceptable for a browser-storage guard, but worth noting.

API Design The public API is just two exports — <ThemeProvider> and useTheme() — with sensible defaults (defaultTheme: 'system', attribute: 'data-theme') that make a working dark-mode integration genuinely “two lines of code,” matching the README’s own framing. Props are documented inline via TSDoc comments in types.ts, and the README (18KB) devotes a section to nearly every configuration knob — class vs. data-attribute output, forced themes, differing DOM/theme names, CSS-in-JS integration, Cloudflare Rocket Loader compatibility — plus explicit hydration-mismatch guidance. There’s no CLI and no required build step for consumers; it’s a pure drop-in React component.

Used by 97 apps in this directory

TypeScript
94%
Apache 2.0

Openship

Devops · Hosting Control Panel

12,979

Openship is an open-source, self-hostable deployment platform that points at a repo and builds, ships, routes, and TLS-terminates the app — driven from a desktop app, web dashboard, or CLI.

View details
84
Repo Health
85
Technical
65
Dependency
Built with
TypeScript 94%
Updated 1 weeks ago
TypeScript
91%
AGPL 3.0

OpenStock

Analytics

19,402

Free, open-source stock market tracker with real-time data, watchlists, price alerts, and AI-powered market summaries.

View details
72
Repo Health
70
Technical
74
Dependency
Built with
TypeScript 91%
Updated 1 weeks ago
Python
75%
AGPL 3.0

OpenViking

AI Development · AI Memory · Databases

38,819

An open-source context database that gives AI agents a unified filesystem for memory, resources, and skills with hierarchical tiered retrieval.

View details
85
Repo Health
75
Technical
64
Dependency
Built with
Python 75%
Rust 12%
Updated 1 weeks ago
TypeScript
83%
MIT

page-agent

AI Agents · Automation

29,242

Control any web interface with natural language — no browser extension, no headless browser, just a JavaScript script tag.

View details
84
Repo Health
83
Technical
90
Dependency
Built with
TypeScript 83%
JavaScript 12%
Updated 2 weeks ago
TypeScript
82%
Apache 2.0

Palmr.

File Storage · Security

2,396

Self-hosted, privacy-focused file sharing without limits

View details
56
Repo Health
62
Technical
69
Dependency
Built with
TypeScript 82%
MDX 13%
Updated 1 weeks ago
TypeScript
99%
AGPL 3.0

Pangolin

Networking

22,937

An open-source, identity-based zero-trust remote access platform built on WireGuard — a self-hostable alternative to Cloudflare Tunnel and Twingate with SSO, OIDC, and tunneled reverse proxying.

View details
87
Repo Health
68
Technical
68
Dependency
Built with
TypeScript 99%
Updated 1 weeks ago
TypeScript
99%
Other

Papermark

Digital Signiture · File Storage

9,218

Open-source DocSend alternative with page-by-page analytics, secure data rooms, and custom domains for document sharing.

View details
82
Repo Health
63
Technical
67
Dependency
Built with
TypeScript 99%
Updated 1 months ago
TypeScript
96%
MIT

Payload CMS

Blogging · CMS · Developer Tools

44,995

The open-source, Next.js-native headless CMS that lives inside your /app folder and gives you a full TypeScript backend instantly.

View details
91
Repo Health
88
Technical
65
Dependency
Built with
TypeScript 96%
Updated 1 weeks ago
TypeScript
69%
AGPL 3.0

Plane

Collaboration · Productivity · Project Management

59,964

Open-source project management platform to replace Jira, Linear, and ClickUp — with built-in cycles, real-time collaborative docs, and full self-hosting.

View details
90
Repo Health
82
Technical
77
Dependency
Built with
TypeScript 69%
Python 27%
Updated 1 weeks ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers