Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dynamic-css-properties

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dynamic-css-properties

CSS custom properties made dynamic.

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

dynamic-css-properties

This tiny library exposes a single function in order to dynamically set custom CSS properties (CSS variables).

Features

  • Simple API - takes an object of properties and values to update and automatically converts camelCased property names to --kebab-case for you.
  • Defaults to document.documentElement, i.e. the root node, but allows you to specify a subnode to update instead (custom CSS properties follow the standard cascading rules).
  • It doesn't do much, but it still feels like magic!

Install

yarn add dynamic-css-properties

Usage

Importing the library

ES6 modules:

import setCustomProperties from 'dynamic-css-properties';

CommonJS:

const setCustomProperties = require('dynamic-css-properties');

Script tag (adds a setCustomProperties global):

<script src="https://unpkg.com/dynamic-css-properties@0.1.1/dist/dynamic-css-properties.min.js"></script>
Using the library

A minimal example:

setCustomProperties({
  primaryTextColor: 'hotpink',
  backgroundColor: 'papayawhip',
});

Now simply use the variables:

body {
  color: var(--primary-text-color, #1e1e1e);
  background-color: var(--background-color, #fff);
}

As soon as the code is executed, the custom properties will be added to the document. Should you wish to only update part of the document you may specify a particular node as the second argument, and only it and its children will be affected.

Caveat

Using this for serious business is not a good idea, unless you know that all your users are running browsers or environments (such as Electron) that support custom CSS properties. See here.

FAQs

Package last updated on 21 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc