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

@rosux/darkmode

v1.0.0

Published

This package provides a simple way to allow you to easily implement a dark mode toggle feature on your website.

Downloads

4

Readme

License

DarkMode

Example

A simple JavaScript class for enabling dark mode on a website.

Made by Rosux.

Installation

To use this class on your website, include the DarkMode.js file in your project and instantiate a new DarkMode object with the required parameters.

<script src="DarkMode.js"></script> <!-- no defer needed -->
<script>
    const dark = "--background-color: black;";
    const light = "--background-color: white;";
    const darkMode = new DarkMode(dark, light);
</script>

Usage

To toggle dark mode on and off, simply call the toggle() method on the DarkMode object.

darkMode.toggle();

You can also set dark mode to be on or off by calling the on() or off() methods, respectively.

darkMode.on(); // sets dark mode to on
darkMode.off(); // sets dark mode to off

You can set the theme to the user's preferred color scheme by calling the defaultColorScheme() method.

darkMode.defaultColorScheme();

You can check the current state of dark mode by accessing the getMode() method, which returns a Boolean (true if dark, false if light).

if (darkMode.getMode()) {
  console.log('Dark mode is on');
} else {
  console.log('Dark mode is off');
}

Customization

You can customize the colors used in dark mode by passing an strings of css variables to the DarkMode constructor.

const darkMode = new DarkMode(
  'css variables for dark theme',
  'css variables for light theme'
);

You can also add event listeners to elements with darkmode-button and darkmode-selector attributes to control the dark mode state.

<button darkmode-button="switch">switch</button>
<button darkmode-button="on">make it dark</button>
<button darkmode-button="off">make it light</button>
<button darkmode-button="default">default to prefered color scheme</button>
<input type="checkbox" darkmode-selector="true"><!-- if its checked its dark (defaults to true) -->
<input type="checkbox" darkmode-selector="false"><!-- if its checked its light (defaults to true) -->

You can add eventListeners to new elements with darkmode-button and darkmode-selector attributes. This also automatically updates the buttons state.

darkMode.applyListener();

Contributing

We welcome contributions to this project! If you have an idea for a new feature or a bug to report, please open an issue. If you want to submit a fix or a new feature, please create a pull request.

Before submitting a pull request, please make sure to:

Write tests for any new code (simple examples on a page to prove it works)
Follow the existing code style
Document any changes in a log file

Thank you for considering contributing to DarkMode!

Let's make dark the new standard.