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

dark-toggle

v2.0.0

Published

correct dark theme switching logic

Downloads

44

Readme

dark-toggle

Reliable dark theme toggling logic with no theme flicker and comprehensive testing.

Theme toggling should not be entirely controlled by JavaScript. When the theme is set to "system," it should automatically match the prefers-color-scheme media query.

Theme Flickering

If the default page theme is light and has already rendered, when the script identifies the need for a dark theme and switches styles, users can noticeably perceive the flicker, diminishing the user experience.

install

pnpm i dark-toggle

Usage

This library can be used vanilla or with React.

Vanilla Usage

import { createDarkToggle } from 'dark-toggle'

const darkToggle = createDarkToggle({
  key: 'theme',
})

darkToggle.subscribe((dark, theme) => {
  if (dark) {
    document.documentElement.classList.add('dark')
  } else {
    document.documentElement.classList.remove('dark')
  }
  document.documentElement.setAttribute('data-theme', theme ?? '')
})

const btn = document.querySelector('button')

btn.onclick = darkToggle.toggle

API

The following describes the API based on the interface provided:

const createDarkToggle = (params: Params): Return

export interface Params {
  storage?: Storage; // Specifies the storage used, default SessionStorage
  key: string;
}

export interface Return {
  // Indicates if the dark theme is active
  isDark: boolean;
  // Current theme
  theme: Theme | null;
  // Function to set the theme
  setTheme: (theme: Theme) => void;
  // Function to toggle light/dark themes
  toggle: VoidFunction;
  // Subscribe to changes, returns unsubscribe function
  subscribe: (cb: SubscribeFunction) => () => void;
}

Using in React

Here's an example of usage in a Next.js application:

// layout.ts
import { DarkToggleScript, DarkToggleProvider } from 'dark-toggle/react';

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html suppressHydrationWarning lang="en">
      <body>
        <DarkToggleProvider>{children}</DarkToggleProvider>
        <DarkToggleScript />
      </body>
    </html>
  );
}

Component Usage

import { useDarkToggle } from 'dark-toggle/react'

export default function Toggle() {
  const { isDark, theme, toggle } = useDarkToggle()
  return (
    <main>
      <p>isDark: {`${isDark}`}</p>
      <p>theme: {theme}</p>
      <button onClick={toggle}>Toggle</button>
    </main>
  )
}

API

<DarkToggleScript/> Initialization script.

<DarkToggleProvider/> Provider.

useDarkToggle(): Return

useDarkToggle returns the same values as createDarkToggle used vanilla. The only difference is that isDark and theme are states, ensuring that hook values change when properties change.

Note

The DarkToggleScript injects the script into the page at a different timing than React, avoiding theme flickering.

It communicates with React through the window.darkToggle object. It is recommended to use useDarkToggle; however, you can directly access it in your code if needed.

Example Usage

zhangyu.dev

Contributions

Contributions are welcome.

License

MIT