npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-router-theme

v1.2.1

Published

Theme support for React Router and Remix

Readme

react-router-theme

What

  • ✅ SSR themes without flashing
  • ✅ Immediate updates across windows and tabs
  • ✅ Perfect for daisyUI themes
  • ✅ Perfect for Tailwind CSS data attribute themes
  • ✅ Compatible with system-preference dark mode for themes supporting it
  • ✅ Made for React Router (v7) and Remix

How

This package provides a useTheme-hook, along with a pre-defined loader and action, to simplify support for themes in server-side-rendered Remix/React Router apps. Utility functions for custom loaders and actions are also provided.

The hook assures that the user's preferred theme is stored in cookies and local storage when set, loaded by the server when rendering server-side, and immediately updated across all tabs and windows when updated.

Installation

npm install react-router-theme@latest

Usage

[!NOTE] Whilst you can apply your themes however you'd like, this package was designed and tested with data attribute themes. This works out of the box with daisyUI, but if you'd like a custom Tailwind approach, you can have a look at react-router-theme-example.

  1. Export { loader, action } from this package in the route rendering your <html>-tag
  2. Call the useTheme-hook in the component, and use the return values to set and update the theme
import { useFetcher, useLoaderData } from "react-router";
import { useTheme } from "react-router-theme";
export { loader, action } from "react-router-theme";

export default function Layout() {
  const loaderData = useLoaderData() as { theme: string };
  const fetcher = useFetcher();

  const [theme, setTheme] = useTheme(loaderData, fetcher);

  return (
    <html data-theme={theme}>
      ...
      <MyThemeSelector theme={theme} setTheme={setTheme}>
    </html>
  );
}

Custom loader and action

If you need to customize the loader or action on the given route (or simply don't want to use the provided ones) just make sure to:

  • include theme: getThemeFromCookie(request) in your loader response
export const loader = (args) => {
  const otherData = ...;

  return {
    theme: getThemeFromCookie(args.request),
    otherData: otherData
  };
};
  • return await themeCookieResponse(formData) from your action if the form data matches action: 'themeChange'
export const action = async (args) => {
  const formData = await args.request.formData();

  if (formData.get("action") === "themeChange") return themeCookieResponse(formData);

  // Other actions...
};

(custom responses are also fine, just include Set-Cookie-header using createThemeCookie(formData))

if (formData.get("action") === "themeChange") {
  const otherData = ...;

  return new Response(otherData, {
    headers: { "Set-Cookie": createThemeCookie(formData) },
  });
}

Default theme

If the user has no theme cookie set, the returned theme will be "default". To override this, pass your desired default value as a third parameter to the hook

const [theme, setTheme] = useTheme(loaderData, fetcher, "myDefaultTheme");

Context and provider

If you need access to the theme or setter in a different route/component from where you call the useTheme-hook (for instance in a custom theme selector in your sidebar/footer/etc. ) you can use a React context and provider.

// Create a context (do this in a separate file).
export const ThemeContext = createContext({
  theme: "",
  setTheme: (theme: string) => {},
});
// Same as before...
export default function Layout() {
  const loaderData = useLoaderData() as { theme: string };
  const fetcher = useFetcher();

  const [theme, setTheme] = useTheme(loaderData, fetcher);

  return (
    // ...but wrapped with a provider
    <ThemeContext.Provider value={{ theme, setTheme }}>
      <html data-theme={theme}>...</html>
    </ThemeContext.Provider>
  );
}
// Use the context to retrieve the theme and setter, as opposed to params
export default function ThemeSelector() {
  const { theme, setTheme } = useContext(ThemeContext);

  return <>...</>;
}

Repository https://github.com/franzvrolijk/react-router-theme

Bugs https://github.com/franzvrolijk/react-router-theme/issues