New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

cssvarenums

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cssvarenums

A template tag function which extracts CSS Custom Properties as JavaScript enums

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

cssvarenums

npm version

A template tag function which extracts CSS Custom Properties as JavaScript enums

Install

npm i -D cssvarenums

Usage

The expected usage for this project is when constructing frontend components which have separate .css and .js files. The expectation is you import the .css file into the .js file, run the imported string through the template tag function and return enums to be used within the JavaScript that relate back to the CSS Custom Properties found within the imported CSS.

Observe the following simple web component .css contents.

:host {
  --toggle-handle-size: 1rem;
}

.toggle-handle {
  height: var(--toggle-handle-size);
  width: var(--toggle-handle-size);
}

Note: the following .js file is not possible without a build step to import .html and .css files as text. Additionally, this is prepared as a CommonJS export. Tooling is required to complete the ESM export shown below. I recommend rollup for both requirements but there are other options.

import html from './template.html';
import css from './styles.css';
import cssvarenums from 'cssvarenums';

// Returns an object with an overloaded .toString() method
const { 
  TOGGLE_HANDLE_SIZE, 
  ...innerHTML
} = cssvarenums`<style>${css}</style>${html}`; 

class MyToggle extends HTMLElement {
  constructor() {
    super();  
    // This will call .toString(), returning the expected <style>...</style> string from above.
    this.attachShadow({ mode: 'open' }).innerHTML = innerHTML;
  }

  set size(value) {
    // Parsing the string from before will result in SCREAMING_SNAKE_CASE enums of CSS Custom Properties
    this.style.setProperty(TOGGLE_HANDLE_SIZE, value);
  }
}

Keywords

utility

FAQs

Package last updated on 04 Feb 2022

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