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 🙏

© 2024 – Pkg Stats / Ryan Hefner

remix-themes

v1.5.1

Published

An abstraction for themes in your Remix_run app.

Downloads

28,447

Readme

Remix Themes

An abstraction for themes in your Remix app.

Features

  • ✅ Perfect dark mode in a few lines of code
  • ✅ System setting with prefers-color-scheme
  • ✅ No flash on load
  • ✅ Disable flashing when changing themes
  • ✅ Class or data attribute selector
  • ✅ Sync theme across tabs and windows

Check out the Example to see it in action.

Install

$ npm install remix-themes
# or
$ yarn add remix-themes

Getting Started

Create your session storage and create a themeSessionResolver

// sessions.server.tsx

import {createThemeSessionResolver} from 'remix-themes'
import { createCookieSessionStorage } from "@remix-run/node"

const sessionStorage = createCookieSessionStorage({
  cookie: {
    name: '__remix-themes',
    // domain: 'remix.run',
    path: '/',
    httpOnly: true,
    sameSite: 'lax',
    secrets: ['s3cr3t'],
    // secure: true,
  },
})

export const themeSessionResolver = createThemeSessionResolver(sessionStorage)

Note: make sure you have domain and secure parameters set only for your production environment. Otherwise, Safari won't store the cookie if you set these parameters on localhost.

Setup Remix Themes

// root.tsx

import {ThemeProvider, useTheme, PreventFlashOnWrongTheme} from 'remix-themes'
import {themeSessionResolver} from './sessions.server'

// Return the theme from the session storage using the loader
export const loader: LoaderFunction = async ({request}) => {
  const {getTheme} = await themeSessionResolver(request)
  return {
    theme: getTheme(),
  }
}

// Wrap your app with ThemeProvider.
// `specifiedTheme` is the stored theme in the session storage.
// `themeAction` is the action name that's used to change the theme in the session storage.
export default function AppWithProviders() {
  const data = useLoaderData()
  return (
    <ThemeProvider specifiedTheme={data.theme} themeAction="/action/set-theme">
      <App />
    </ThemeProvider>
  )
}

// Use the theme in your app.
// If the theme is missing in session storage, PreventFlashOnWrongTheme will get
// the browser theme before hydration and will prevent a flash in browser.
// The client code runs conditionally, it won't be rendered if we have a theme in session storage.
function App() {
  const data = useLoaderData()
  const [theme] = useTheme()
  return (
    <html lang="en" data-theme={theme ?? ''}>
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width,initial-scale=1" />
        <Meta />
        <PreventFlashOnWrongTheme ssrTheme={Boolean(data.theme)} />
        <Links />
      </head>
      <body>
        <Outlet />
        <ScrollRestoration />
        <Scripts />
        {process.env.NODE_ENV === 'development' && <LiveReload />}
      </body>
    </html>
  )
}

Add the action route

Create a file in /routes/action/set-theme.ts or /routes/action.set-theme.ts when using Route File Naming v2 with the content below. Ensure that you pass the filename to the ThemeProvider component.

Note: You can name the action route whatever you want. Just make sure you pass the correct action name to the ThemeProvider component. Make sure to use absolute path when using nested routing.

This route is used to store the preferred theme in the session storage when the user changes it.

import {createThemeAction} from 'remix-themes'
import {themeSessionResolver} from './sessions.server'

export const action = createThemeAction(themeSessionResolver)

API

Let's dig into the details.

ThemeProvider

  • specifiedTheme: The theme from the session storage.
  • themeAction: The action name used to change the theme in the session storage.
  • disableTransitionOnThemeChange: Disable CSS transitions on theme change to prevent the flashing effect.

useTheme

useTheme takes no parameters but returns:

  • theme: Active theme name
  • setTheme: Function to set the theme. If the theme is set to null, the system theme will be used and definedBy property in the metadata object will be set to SYSTEM.
  • metadata: An object which contains the following properties:
    • definedBy: The theme source. It can be USER or SYSTEM. Useful to detect if the theme was set by the user or by the system.

createThemeSessionResolver

createThemeSessionResolver function takes a cookie session storage and returns

  • resolver: A function that takes a request and returns an object with the following properties:
    • getTheme: A function that returns the theme from the session storage.
    • setTheme: A function that takes a theme name and sets it in the session storage.
    • commit: A function that commits the session storage (Stores all data in the session and returns the Set-Cookie header to use in the HTTP response.)

PreventFlashOnWrongTheme

On the server, "theme" might be null so PreventFlashOnWrongTheme ensures that this is correct before hydration. If the theme is null on the server, this component will set the browser theme on the html element in a data-theme attribute if exists, otherwise it will be set to a class attribute. If both data-theme and class are set, the data-theme will be used.

  • ssrTheme: boolean value that indicates if we have a theme in the session storage.