use-css-var
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

use-css-var

React hook that will allow you to control randomly generated CSS variable.

Install

package is already shipped with types for TypeScript

yarn add use-css-var

or

npm i --save use-css-var

Usage

Simple example with styled components:

import React, { useState, useCallback } from 'react';
import styled from 'styled-components';
import { useCssVar } from 'use-css-var';

const Box = styled.div<{ colorVar: string }>`
  width: 20px;
  height: 20px;
  background-color: var(--${({ colorVar }) => colorVar});
`;

const Example = () => {
  const [isRed, setIsRed] = useState(true);
  const colorVar = useCssVar(isRed ? 'red' : 'blue');

  const handleClick = useCallback(() => setIsRed(prev => !prev), []);

  return <Box colorVar={colorVar} onClick={handleClick} />;
}

Example + Demo

Check out another example with lots of state changes in example app: https://github.com/michal-wrzosek/react-component-lib/blob/master/example/src/App.tsx

Live demo: https://michal-wrzosek.github.io/use-css-var/

Notes

This package is still in an experimental phase and contribution is very welcomed.


This package was bootstrapped with react-component-lib

Readme

Keywords

none

Package Sidebar

Install

npm i use-css-var

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

6.53 kB

Total Files

9

Last publish

Collaborators

  • michal.wrzosek