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

react-cosmos-dark-mode

v0.1.3

Published

React Cosmos plugin for toggling dark mode

Downloads

587

Readme

React Cosmos Dark Mode

Adds a toolbar icon for toggling the dark mode.

Who should use this?

If you are working with components that have a dark mode that is controlled by a class on an ancestor (e.g. Tailwind's dark class) then you can use this to conveniently toggle between states.

By default it will also track your system dark mode settings.

If your components only respond to the system dark mode setting you don't need this plugin. Instead you can toggle dark mode through your browser's DevTools or your OS's settings.

Installation

Install the package:

npm install -D react-cosmos-dark-mode

Add the plugin to your cosmos.config.json, e.g.:

 {
   "$schema": "http://json.schemastore.org/cosmos-config",
-  "plugins": ["react-cosmos-plugin-webpack"],
+  "plugins": ["react-cosmos-dark-mode", "react-cosmos-plugin-webpack"],
   "dom": {
     "containerQuerySelector": "#container"
   }
 }

React Cosmos doesn't yet provide a way for plugins to install fixtures so you need to add a global decorator that wraps everything in the DarkMode fixture.

You can do that by adding a file named cosmos.decorator.tsx to your root src folder that looks something like this:

import type { RenderableProps } from 'preact';
import { DarkMode } from 'react-cosmos-dark-mode/DarkMode';

type EmptyProps = { __tag?: never };

export default function GlobalDecorator({
  children
}: RenderableProps<EmptyProps>) {
  return <DarkMode>{children}</DarkMode>;
}

(Can someone please help me update this for React?)

Configuration

The following configuration options can be set in the ui.darkMode section of cosmos.config.json:

| Key | Type | Default | Description | | --------- | ------------------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------- | | dark | string \| null | "dark" | The class set on the root element when in dark mode or null to set no class. | | light | string \| null | null | The class set on the root element when in light mode or null to set no class. | | default | "dark" \| "light" \| "system" | "system" | The default value to use. If "system" is used the system-defined (i.e. browser or OS) preference is used when the setting is cleared. |

Example

To use a class light instead and disable the behavior where the plugin matches the system dark mode setting you would specify:

 {
   "$schema": "http://json.schemastore.org/cosmos-config",
   "plugins": ["react-cosmos-dark-mode", "react-cosmos-plugin-webpack"],
+  "ui": {
+    "darkMode": {
+      "light": "light",
+      "dark": null,
+      "default": "light"
+    }
+  }
 }

Future extensions

This plugin is very basic. The following are some configuration settings it probably should support. Please file an issue if you need any of them:

  • Specifying the target element. For example, we could add a targetSelector setting to allow specifying which element should have its class toggled, e.g. targetSelector: "#container".

  • Specifying the target attribute. For example, we could add a targetAttr setting to allow specifying which attribute to toggle. e.g. targetAttr: "data-dark-mode".

Developing

The easiest way to develop is using yalc.

Install it globally and then from this repo do:

yarn build
yalc publish

Then in your project that is using React Cosmos run:

yalc add react-cosmos-dark-mode