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

postcss-minicloud-theme-colors

v1.0.4

Published

A postcss plugin that could generate light and dark mode css

Downloads

9

Readme

postcss-minicloud-theme-colors

A PostCSS plugin that automatically transforms color groups for light and dark themes, allowing you to define colors once and apply them across your project with ease. This plugin also supports CSS custom properties (variables) for more flexible theming.

Features

  • Automatic Theme Switching: Easily define and switch between light and dark theme colors.
  • Support for Custom Properties: Use CSS variables to define theme colors.
  • Nesting Support: Automatically handles nested CSS selectors when used with postcss-nested or postcss-nesting.

Installation

Install via npm:

npm install postcss-minicloud-theme-colors --save-dev

Usage

// postcss.config.js
module.exports = {
  plugins: [
    require('postcss-nested'), // If you use nested CSS
    require('postcss-minicloud-theme-colors').default({
      function: 'transformColor',
      groups: {
        primary: ['#ffffff', '#000000'],
        secondary: ['#333333', '#cccccc'],
      },
      useCustomProperties: false,
      darkThemeSelector: 'html[data-theme="dark"]',
      nestingPlugin: 'nested', // Set to 'nesting' if using postcss-nesting
    }),
    require('autoprefixer'),
  ]
};

If your project supports ES model, use the following config:

// postcss.config.mjs
import postcssNested from 'postcss-nested';
import minicloudThemeColors from 'postcss-minicloud-theme-colors';
import autoprefixer from 'autoprefixer';

export default {
  plugins: [
    postcssNested,
    minicloudThemeColors.default({
      function: 'transformColor',
      groups: {
        primary: ['#ffffff', '#000000'],
        secondary: ['#f0f0f0', '#333333']
      },
      colors: {
        white: '#ffffff',
        black: '#000000'
      },
      useCustomProperties: true,
      darkThemeSelector: 'html[data-theme="dark"]',
      nestingPlugin: 'nested',
    }),
    autoprefixer,
  ]
};

Example

Given the following CSS:

.header {
  background-color: transformColor(primary);
  color: transformColor(secondary);
}

The plugin will generate:

.header {
  background-color: #ffffff;
  color: #333333;
}

html[data-theme="dark"] .header {
  background-color: #000000;
  color: #cccccc;
}

You can also use CSS custom properties (variables) to define your theme colors:

// postcss.config.js
module.exports = {
  plugins: [
    require('postcss-minicloud-theme-colors').default({
      function: 'transformColor',
      groups: {
        primary: ['--primary-color', '--primary-dark-color'],
        secondary: ['--secondary-color', '--secondary-dark-color'],
      },
      useCustomProperties: true,
      darkThemeSelector: 'html[data-theme="dark"]',
    }),
    require('autoprefixer'),
  ]
};

With the following CSS:

.header {
  background-color: transformColor(primary);
  color: transformColor(secondary);
}

The plugin will generate:

.header {
  background-color: var(--primary-color);
  color: var(--secondary-color);
}

html[data-theme="dark"] .header {
  background-color: var(--primary-dark-color);
  color: var(--secondary-dark-color);
}

Options

  • function (string): The function name used in your CSS to trigger color transformation. Default is 'transformColor'.

  • groups (object): An object where keys are group names and values are arrays of two colors: the first for light theme, and the second for dark theme.

  • colors (object): An optional object that maps color names to actual color values or CSS variables.

  • useCustomProperties (boolean): If set to true, the plugin will treat the group values as CSS custom properties (variables). Default is false.

  • darkThemeSelector (string): The selector for dark theme styles. Default is 'html[data-theme="dark"]'.

  • nestingPlugin (string or null): If you're using a nesting plugin like postcss-nesting or postcss-nested, specify its name here. This allows the plugin to correctly generate nested styles.

Error Handling

If the plugin encounters an invalid color value or group, it will throw an error and stop processing. Ensure that all color groups are properly defined and that any defaultValue passed to the transformColor function is a valid CSS color.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you encounter any issues or have questions, feel free to open an issue on the GitHub repository.