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-adapter

v0.2.6

Published

<p> <a aria-label="NPM version" href="https://www.npmjs.com/package/css-vars-adapter"> <img alt="" src="https://badgen.net/npm/v/css-vars-adapter"> </a> <a aria-label="Package size" href="https://bundlephobia.com/result?p=css-vars-adapter">

Downloads

9

Readme

Css-Vars-Adapter

The library will help you manage the css variables through javascript. This is useful if you are developing a microfront-end on different frameworks, and the theme may come from above.

Menu

Usage

Inside your project directory, run in terminal:

yarn add css-vars-adapter

Or with npm:

npm install css-vars-adapter

API

setVariables

Provides the ability to add a theme globally.

Takes 1 generic(TVariables) and 2 arguments.

type TVariables = {
  [key: string]: TVariables | string | number;
};

type TOptions = {
  replace?: boolean;
};

type TSetVariales = <T extends TVariables = {}>(values: T, options: TOptions = {}) => void

Values are your theme. You can transfer an object of any nesting, the library will parse it into a kebab-case.

Options specifies for adding a theme

  1. replace - Checks globally for the presence of variables, and if a variable is present anywhere in the styles, skips it.

This will put the following styles object globally

import { setVariables } from 'css-vars-adapter';

const theme = {
  colors: {
    primary: {
      '100': '#000000',
      '200': {
        '0': '#020202',
        '1': '#030303',
      }
    },
    secondary: '#FFFFFF'
  },
  borderRadius: '8px'
}

type TTheme = typeof theme;

setVariables<TTheme>(theme, { replace: true });

getVariables

Allows you to get absolutely all the variables that the current page processes (not only which you have set).

From the previous example, you get the following:

import { getVariables } from 'css-vars-adapter';

type TValues = 
   | '--colors-primary-100'
   | '--colors-primary-200-0'
   | '--colors-primary-200-1'
   | '--colors-secondary'
   | '--borderRadius';

const globalViriables = getVariables<Record<TValues, string>>();
//   {
//      '--colors-primary-100': '#000000',
//      '--colors-primary-200-0': '#020202', 
//      '--colors-primary-200-1': '#030303', 
//      '--colors-secondary': '#FFFFFF',
//      '--borderRadius': '8px'
//    }

Microfrontend

Use the recommended setting single-spa-systemjs

  1. Inside importmap your index.html describe the path to the dependency
...
<head>
   <script type="systemjs-importmap">
      {
        "imports": {
          "css-vars-adapter": "https://unpkg.com/css-vars-adapter/dist/system/css-vars-adapter.production.js",
        }
      }
   </script>
</head>
...
  1. Inside your webpack.config.js
module.exports = {
   externals: ['css-vars-adapter'],
   //...
}
  1. Inside your application, it is advisable to make changes to variables before the first mount.

React:

We give preference to emotion or styled-components, then you can use the ThemeProvider with this approach and do without our library.

import React from 'react';
import { ThemeProvider } from '@emotion/react'

export default function ({ theme }) {
  return (
    <ThemeProvider {...{ theme }}>
       {/* ... */}
    </ThemeProvider>
  )
}

But you can also use our library if you work with css...

import { useLayoutEffect } from 'react';
import { setVariables } from 'css-vars-adapter';

type TTheme = {
   colors: {}
}

export const useGlobalVariables = (theme: TTheme) => {
   useLayoutEffect(() => {
      setVariables(theme, { replace: false });
   })
}

Vue:

<script lang='ts'>
import { onBeforeMount, PropType } from 'vue';
import { setVariables } from 'css-vars-adapter';

type TTheme = {
  colors: {
    primary: '#000000'
  }
}

export default {
  props: {
    theme: Object as PropType<TTheme>,
  },
  setup({ theme }) {
    onBeforeMount(() => {
       setVariables(theme, { replace: false });
    });
  },
};
</script>

<style>
  .main {
     background-color: var(--colors-primary);
  }
</style>