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-global-variables

v3.0.2

Published

Manipulate CSS3 Global Variables with Javascript, like regular variables.

Downloads

180

Readme

CSS Variables Manipulation with JS (ES6)

CSSGlobalVariables provides a natural interface for fast manipulation of GLOBAL CSS Variables/Custom Properties (those declared with a :root selector), simplifying templating tasks, and manipulation of general CSS styles in javascript.

import {CSSGlobalVariables} from './css-global-variables.js';

// set the CSS global variable --myColor value to "green"
let cssVar = new CSSGlobalVariables();
cssVar.myColor = "green";

Demo: See it in action

Syntax

 new CSSGlobalVariables( [ configObject ] )

Parameters

A Config Object can be provided to customize internal behaviour. It can set any of the following properties:

  • autoprefix: When set to true allows access to the CSS variables names without specifing the -- prefix on the name. (Boolean. Default:true)

  • filter: Allows to filter which Style Elements should be scanned or ignored through CSS selector strings. By default everything is scanned. (String. Default: '*')

  • normalize: A user-provided transform-function that processes the CSS variable names (before they get autoPrefixed). The function must return a String. This mechanism allows the usage of custom variable name formatting (eg. camelCase, snake_case, CONSTANT_CASE) in your code. (A nice source of transform functions is change-case). (Function. Default: none)

Return Value

The CSSGlobalVariables() Constructor returns a Proxy Object containing a live Collection with the found CSS global variables.

Installation

You can choose between any of the following available distribution channels:

  • GIT: Clone the repository locally using git (or download the latest release here)
$ git clone https://github.com/colxi/css-global-variables.git
  • NPM: Install it using npm and import it.
$ npm install css-global-variables -s
  • CDN: Include the script in your HTML header (window.CSSGlobalVariables will be created).
<script src="https://colxi.info/css-global-variables/src/main.js"></script>

Usage

The Constructor returns a Proxy Object and any regular Object operation can be performed on it (except property deletion). In the following list, you will find examples of the the most common operations and interactions:

Import and Construct:

import {CSSGlobalVariables} from './css-global-variables.js';
let cssVar = new CSSGlobalVariables();

Set a new value to a CSS global variable:

/* The following assigments to '--myVariable' behave equally, and are all valid */
cssVar.myVariable = 'newValue';
cssVar['myVariable'] = 'newValue';
cssVar['--myVariable'] = 'newValue';

Get the value of a CSS global variable:

/* The following value retrievals for '--myVariable' behave equally, and are all valid */
console.log( cssVar.myVariable );
console.log( cssVar['myVariable'] );
console.log( cssVar['--myVariable'] );

Enumeration of all declared CSS global variables, through iteration:

for( let v in cssVar ){
    console.log( v , '=', cssVar[v] );
}

Variable Name Normalization

Normalize functions (implemented by @SebastianDuval ) allow you to perform automatic transformations of the variable names, to make them more suitable for the javascript syntax, or to simply addapt them to your coding style and personal preferences.

In the following example a CSS variable declared using hyphens (--my-css-variable), can be accessed in Javascript using the widelly used camelCase style (myCssVariable), thanks to the camelToHyphens normalize function (and the native autoprefixer):

CSS:

<style>
   :root{
        --my-css-variable: 'red';
    }
</style>

Javascript:

let camelToHyphens = function(name){
    return name.replace(/[A-Z]/g, m => "-" + m.toLowerCase() );
}
let cssVar = new CSSGlobalVariables( { normalize:camelToHyphens });

cssVar.myCssVariable = 'blue';

Automatic DOM Change Tracking

The library uses a DOM Mutation Observer to detect new inclusion in the document. Thanks to this observer, new CSS variables are available automatically when new styles are attached to the document.

CORS Restrictions

CSSGlovalVariables will face limitations when trying to extract the CSS definitions of a remote stylesheet (except for same-origin urls). Restrictions applied by the browser, based in the Cross Origin Policy will block any access attempt.

In such a scenario, a warning will be printed in the console, and the affected style element will be flagged and ignored by the library.

To prevent this restriction, add the crossorigin attribute to the <link> element:

<link rel="stylesheet" crossorigin="anonymous" href="https://www.a-remote-server/styles.css">

If the server is configured to allow CORS (through the Access-Control-Allow-Origin directive) the CORS restrictions should disapear.