Skip to content

Easy manipulation of CSS Global variables using Javascript

License

Notifications You must be signed in to change notification settings

kbav/css-global-variables

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Easy manipulation of CSS Global variables using Javascript

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 93.8%
  • CSS 4.4%
  • HTML 1.8%