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

theme-handler

v0.0.1

Published

## A React Theme Handler

Downloads

1,494

Readme

Theme Handler

A React Theme Handler

A theme switcher for Next.js or React applications.

Installation

bun install theme-handler
yarn add theme-handler
pnpm install theme-handler
npm install theme-handler

Usage

Out of the box the ThemeProvider tries to match the theme based on the users system preferences.

Passing in any value to the theme prop wil result in a class name added to the html tag of your application to manage your theme. Handles any theme name or value and as many theme options you may want to provide.

Import the ThemeProvider component as a parent to the application you want to wrap:

<ThemeProvider>{children}</ThemeProvider>

Storing a Theme

By default the Theme Provider will set a cookie for you out of the box. All that is required is that the application provides the correct theme value to load to support SSR. The application will have to manage the cookie and provide it.

Next.js example below:

export default function RootLayout({ children }: { children: ReactNode }) {
  const cookieStore = cookies()
  const theme = cookieStore.get('theme')

  return (
    <html lang="en" className="h-full" suppressHydrationWarning>
      <body>
        <ThemeProvider theme={theme?.value ?? 'system'}>
          {children}
        </ThemeProvider>
      </body>
    </html>
  )
}

Optional Theme Toggle

You can create a theme toggle to allow the user to toggle between theme settings.

export function ThemeSwitcher() {
  const { theme, setTheme } = useTheme()

  return (
    <div className="flex items-center gap-1">
      <IconButton
        disabled={theme === 'system'}
        onClick={() => setTheme('system')}
      >
        <DesktopIcon />
      </IconButton>
      <IconButton
        disabled={theme === 'light'}
        onClick={() => {
          setTheme('light')
        }}
      >
        <SunIcon />
      </IconButton>
      <IconButton disabled={theme === 'dark'} onClick={() => setTheme('dark')}>
        <MoonIcon />
      </IconButton>
    </div>
  )
}

Prop options

Out of the box Theme Handler defaults to the users preference. Internally it is referenced as system.

component props:

  • children - ReactNode - Required The child nodes that are wrapped by the ThemeProvider.
  • theme - string - Optional Default is system if you want user preferences to decide between a class named light or dark provide system.
  • setStoredTheme - (storageKey: string, theme: string) => void - Optional The callback function that set updates the theme value if changed on the client.
  • storedKey - string - Optional The key name of the stored theme value. Defaults to theme.

Future Support

  • Multiple themes on a page
  • Data attribute option instead of class name