GithubHelp home page GithubHelp logo

grrowl / class-styles Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jedwatson/classnames

0.0 2.0 0.0 127 KB

A simple utility for folding style objects like css classes

License: MIT License

JavaScript 100.00%
css-in-js

class-styles's Introduction

classStyles

Version

Forked version of classnames to support inline styles generated from a group of objects and "class" names (really just root-level keys on the objects)

Rules

  • classStyles can be bound to an object or an array of objects. These objects should have a { key: styleObject, otherKey: otherStyle } structure.
  • Arguments are applied from left to right, overwriting defined styles as they are applied.
  • String arguments are split by space (" ") and are applied in turn.
  • Object arguments are applied as raw style objects.
  • Arrays are recursed into, applying these rules to Strings, Objects and Arrays as they're encountered.
  • Property values of undefined, null, "", true or false are not applied and will skip overwriting previously encountered properties.
    • { heading: { fontSize: "12pt" } } followed by
    • { heading: { fontSize: null, color: "black" } } will result in
    • { heading: { fontSize: "12pt", color: "black" } }

Usage

import classStyles from 'class-styles'

// Define style objects
const markupStyles = { container: { backgroundColor: 'white', margin: '0px auto' } },
  otherStyles = { container: { color: 'red' }, outer: { margin: '10px' } }

// Bind style objects to classStyles. Can be either an object, or an array of objects.
const cx = classStyles.bind([ markupStyles, otherStyles ])

cx('container outer')
// => { margin: '10px', backgroundColor: 'white', color: 'red' }

cx('container', { paddingBottom: '5px' })
// => { backgroundColor: 'white', margin: '0px auto', paddingBottom: '5px' }

cx({ padding: '10px' }, 'not-exist')
// => { padding: '10px' }

// or, without using .bind
classStyles.call([ markupStyles, otherStyles ], 'outer')
// => { margin: '10px' }

Installation

npm install class-styles --save

Alternatively, you can simply include index.js on your page with a standalone <script> tag and it will export a global classStyles method, or define the module if you are using RequireJS.


class-styles follows the SemVer standard for versioning.

There is also a Changelog.

Polyfills needed to support older browsers

classNames >=2.0.0

Array.isArray: see MDN for details about unsupported older browsers (e.g. <= IE8) and a simple polyfill.

License

MIT. Copyright (c) 2016 Jed Watson, Tom McKenzie.

class-styles's People

Contributors

amilajack avatar avindra avatar barneycarroll avatar bgoscinski avatar brigand avatar chenglou avatar daviferreira avatar dcousens avatar diegolacarta avatar grrowl avatar jedwatson avatar kirill-konshin avatar klimashkin avatar longlho avatar monners avatar nkbt avatar stuartsan avatar wcjordan avatar wwcline avatar zackify avatar zertosh avatar

Watchers

 avatar  avatar

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.