Git Product home page Git Product logo

react-custom-properties's Introduction

React Custom Properties

A React component for declaratively applying CSS Variables or CSS Custom Properties as the are officially known. For CSS variable usage see https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables

Install

To get started install via npm

npm install react-custom-properties

You can then import the component into your code using ES5 require

var CustomProperties = require('react-custom-properties');

or ES6 imports

import CustomProperties from 'react-custom-properties';

Usage

This module provides a <CustomProperties /> component. When mounted it will, by default, apply any CSS variables passed to the properties component to its children.

So for example, your stylesheet may contain CSS Variables like this.

.header {
  background: var(--branding-color);
}

And you can apply values to those variables like this.

import React, { Component } from 'react';
import CustomProperties from 'react-custom-properties';

class App extends Component {
  render() {
    return (
      <div>
        <CustomProperties properties={{ '--branding-color': '#FF0000' }}>
          <div className="header">
            this will have the background color #FF0000
          </div>
        </CustomProperties>
      </div>
    );
  }
}

Nesting

The CustomProperties component can be nested so that properties set by parent instances are overridden by ones set by child instances. So for example...

Using the same stylesheet as before

import React, { Component } from 'react';
import CustomProperties from 'react-custom-properties';

class App extends Component {
  render() {
    return (
      <div>
        <CustomProperties properties={{ '--branding-color': '#FF0000' }}>
          <div className="header">
            this will have the background color #FF0000
          </div>
          
          <CustomProperties properties={{ '--branding-color': '#555555' }}>
            <div className="header">
              this will have the background color #555555
            </div>
          </CustomProperties>
        </CustomProperties>
      </div>
    );
  }
}

Global

The CustomProperties component accepts a boolean global prop. By default the CSS Variables will only apply to the component's children. When the global prop is passed the CSS Variables will be set on the document root and will therefor be globally applied to all styles.

Using the same stylesheet as before

import React, { Component } from 'react';
import CustomProperties from 'react-custom-properties';

class App extends Component {
  render() {
    return (
      <div>
        <CustomProperties 
          global
          properties={{ '--branding-color': '#FF0000' }} 
        />

        <div className="header">
          this will have the background color #FF0000
        </div>
      </div>
    );
  }
}

Any properties set by a non-global instance will take precedence over properties set by a global instance

Credit

  • The idea for this component came from working with @carlmw and @Kliriklara
  • This repo was bootstrapped from npm-react-boilerplate

Contribute

  1. Fork this repo
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Make sure the tests pass (npm run test)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request

License

MIT

react-custom-properties's People

Contributors

danbahrami avatar karl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-custom-properties's Issues

Add an example page

To show off the power of this component and to aid with development it would be cool if we could have an example page. Perhaps with some interactivity so you can update the custom properties that are being applied.

Accept property keys that don't start with `--`

At the moment a warning is shown if you try to add a custom property that doesn't begin with --.

For example...

// valid
<CustomProperties properties={{ '--brand-color': '#888' }} />

// invalid
<CustomProperties properties={{ brand-color: '#888' }} />

For these invalid cases, it might be nicer to make them valid by prepending the -- internally.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.