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

css-vars-manager

v1.1.0

Published

CSS Variables Management with JS

Downloads

57

Readme

CSS Vars Manager

Manage your CSS variables dynamically with Javascript

CSS Vars Manager is a ready to use CSS Variable's Manager using javascript. You can set variable dynamically and update them without write one thousand line of code. The libary offers a css variables importer, useful for a dark mode theme for example.

🚀 Install

npm i css-vars-manager

🔦 Usage

The library contains two object that you can import on your project, cssVars to manage you're CSS variables and darkMode for working with browser dark mode feature. Let's see what you can make with this package.

Working

all cssVars's method work with a classic object's interface :

 interface varsCollection {
    tag: string
    color?: string
    value?: string
    shadeCount?: number
}

Objects need to contain tag's key, this is you're css tag's name. If you want working with color, you need to use color's key, with that we can generate shade of your color. Per default we generate zero shade, you need to specify how many shade you want with using shadeCount's key. For other type of css variable, you can simply use value's key.


Methods


Allows you to push a variable.

Example

import { cssVars } from "css-vars-manager";

cssVars.set({
    tag: "--interface-size",
    value: "1200px"
});
cssVars.set({
    tag: "--main-color",
    color: "#000",
    shadeCount: 1
});

Result

html {
    --interface-size: 1200px;
    --main-color: #000;
    --main-color-light-1: #222;
    --main-color-dark-1: #000;
}

Get a variable's value.

Example

html {
    --interface-size: "1200px";
    --main-color: #000;
}
import { cssVars } from "css-vars-manager";

console.log(
    cssVars.get("--interface-size"),
    cssVars.get("--main-color")
);

Result

console > 1200px #000

Allows you to update a variable.

If your variable have been add with color's tag, you don't need to specify shadeCount's tag, we use the shadeCount you used for initialize this variable.

Example

import { cssVars } from "css-vars-manager";

cssVars.update({
    tag: "--interface-size",
    value: "1200px"
});
cssVars.update({
    tag: "--main-color",
    color: "#FFF",
});

Result

html {
    --interface-size: 1200px;
    --main-color: #FFF;
    --main-color-light-1: #FFF;
    --main-color-dark-1: #DDD;
}

Allows to set multiple css variables from an array.

Example

import { cssVars } from "css-vars-manager";

cssVars.setCollection(
    [
        {
            tag: "--logo",
            value: "url('/static/logo.svg')",
        },
        {
          tag: "--main-color",  // # Variable's name
          color: "#000",        // # Variable's color
          shadeCount: 3,        // # Number of shades (light and darkness)
        },
        {
          tag: "--sub-color",
          color: "#000",
          shadeCount: 1,
        },
        {
          tag: "--neutral-color",
          color: "#d5514b",
        },
    ],
)

Result

html {
    --logo: url('/static/logo.svg');
    --main-color: #fff;
    --main-color-light-1: #FFF;
    --main-color-dark-1: #DDD;
    --main-color-light-2: #FFF;
    --main-color-dark-2: #BBB;
    --main-color-light-3: #FFF;
    --main-color-dark-3: #999;
    --sub-color: #000;
    --sub-color-light-1: #222;
    --sub-color-dark-1: #000;
    --neutral-color: #d5514b;
}

Allows to import a variables configuration from JSON file.

Example

dark.json
[
  {
    "tag": "--logo",
    "value": "url('/static/logo_dark.svg')"
  },
  {
    "tag": "--main-color",
    "color": "#000",
    "shadeCountolor": 3
  },
  {
    "tag": "--text-color",
    "color": "#fff",
    "shadeCountolor": 1
  },
  {
    "tag": "--neutral-color",
    "color": "#d5514b"
  }
]
import { cssVars } from "css-vars-manager";

cssVars.importCollection("/static/theme/dark.json");

Result

html {
    --logo: url('/static/logo_dark.svg');
    --main-color: #000;
    --main-color-light-1: #FFF;
    --main-color-dark-1: #DDD;
    --main-color-light-2: #FFF;
    --main-color-dark-2: #BBB;
    --main-color-light-3: #FFF;
    --main-color-dark-3: #999;
    --text-color: #fff;
    --text-color-light-1: #222;
    --text-color-dark-1: #000;
    --neutral-color: #d5514b;
}

Allows you to create shade from a hex color.

Example

import { cssVars } from "css-vars-manager";

console.log(
    "#000 lighter :",
    cssVars.getShadeFromHex("#000", 2)
);

console.log(
    "#000 darkness :",
    cssVars.getShadeFromHex("#000", -2)
);

Result

console > #000 lighter : #222
console > #000 darkness : #000

Allows you to create shade from a variable's color (in hexadecimal).

Example

html {
    --main-color: "#FFF";
}
import { cssVars } from "css-vars-manager";

console.log(
    "--main-color lighter :",
    cssVars.getShadeFromVar("--main-color", 2)
);

console.log(
    "--main-color darkness :",
    cssVars.getShadeFromVar("--main-color", -2)
);

Result

console > --main-color lighter : #FFF
console > --main-color darkness : #DDD

Methods


Initialize dark mode module

You need to specify two JSON files, one for dark theme and one for light theme, files need to use varCollection's object structure.

Example

import { darkMode } from "css-vars-manager";

darkMode.init(
    "/static/template/light.json",
    "/static/template/dark.json"
)

Trigger dark mode change from user computer

Example

import { darkMode } from "css-vars-manager";

darkMode.watcher()