Coder Social home page Coder Social logo

barber's Introduction

Barber - Write CSS in JS

Barber lets you write CSS in Javasript. Now you might ask: why would you want to do that?

  1. You swim in Javascript anyway, why not?
  2. Javacript is more programmatic/dynamic. Variables, for example.
  3. Barber automatically handles vendor prefixing of CSS properties for you.
  4. Harness the power of Javascript modules! You can now package up CSS as part of your module to be shared between projects and with the rest of the world.

Install

Install barber via NPM or Bower.

Usage

Simple example

var Barber = require('barber')
var styles = Barber.styleSheet()

styles.add({
    'button': {
        'border-radius': '10px', // vendor prefixes are applied for you
        boxShadow: '3px 2px 3px #888', // can also camelCase
        'background': '#ddd'
    }
})

Barber.install() // apply the styles to the page

Alternatively, you can pass in one single string of CSS to add()

styles.add('button { border-radius: 10px; box-shadow: 3px 2px 3px #888; }')

Selector Nesting

Similarly to most CSS preprocessors, Barber supports selector nesting

styles.add({
    '.view': {
        boxShadow: '3px 2px 3px #888',
        pre: {
          fontFamily: 'Monospace'
        }
    }
})

This will generate the rules:

  • .view { box-shadow: 3px 2px 3px #888; }, and
  • .view pre { font-family: Monospace; }

Namespaces

In the above example, calling Barber.styleSheet() returns the default stylesheet, but you can also namespace your stylesheets

var styles = Barber.styleSheet('components')

Barber.install() still installs all available stylesheets, but you can choose to instead install each stylesheet separately

Barber.styleSheet().install()
Barber.styleSheet('components').install()

This gives you more control over what stylesheets you want applied.

Using Barber with Modules

Barber was design with authoring modules in mind. How that would work is, in your Javascript module, you would have code that creates a stylesheet

// awesome_module.js
var styles = require('barber').styleSheet('awesome_module')

styles.add(...)
styles.add(...)
...

Note that no stylesheet has been installed within the module code.

Next, someone else installs your awesome module, and includes it in their app somewhere

var awesome = require('awesome_module')

Then, in a separate part of their code - where they bootstrap the application - they would install the stylesheets.

Barber.install()

They can also choose to skip the stylesheet from your module by explicitly installing each stylesheet they want included.

Barber.styleSheet().install()
// Barber.stylesheet('awesome_modules').install()
Barber.styleSheet('some_other_module').install()

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.