Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

css-stylo

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

css-stylo

'stylo takes any css string and converts it to a style object'

  • 0.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

css-stylo

Preface

Built with react in mind, this is a naive implementation of converting a string of css to a style object (css-stylo). This lib was created as more of a thought experiment and a chance to get my head around recursion. Never the less, it made for a pretty cool REPL, and if you did want to use it in production, I've whipped up some docs for you below.

Installation

npm install --save css-stylo

Demo & Example

Live demo: cameronbourke.github.io/css-stylo

To build the example locally, clone this repo then run:

npm install
npm start
Then open localhost:8080 in a browser.

Usage

Simply pass a string to stylo. Using ES6 template strings makes creating a string of css trivial. The function will look for certain css selectors in the string, at the moment only .classes and #ids are supported. It will then go on and create a corresponding object of each selector.

stylo(cssString, parsePixels)
// using babel and a module loader
import stylo from 'css-stylo';

const style = stylo(`
	.example {
		color: #34e233;
		line-height: 1.4;
		padding: 10px 30px 20px 0px;
		border-bottom: 2px solid red;
	}
`);

The returned value looks like:

{
	"example": {
	  "color": "#34e233",
	  "lineHeight": 1.4,
	  "padding": "10 30 20 0",
	  "borderBottom": "2 solid red"
	}
}

You can then use this object to apply inline styles to your components.

<div style={style.example}>I'm an example</div>

This can be particularly handy when working with React Native because you are able to define your styles using css's syntax. Note when using px values, stylo will remove it so that both React and React Native are able to interpret the value, as React handles the number 10 to equal 10px. Remember, you can pass false as the second argument to stop stylo from doing so.

The best part is that the styles will cascade just like they do in css, so something like this will work as expected.

// css string representation
const style = stylo(`
	.first {
		color: red;
		font-size: 20px;
	}

	.first {
		color: blue
	}
`);

// returned value
{
  "first": {
		"color": "blue",
		"fontSize": 20
  }
}

Bonus

We can even do nesting! Just like in Sass and Less.

// css string representation
const style = stylo(`
	.initial {
		color: orange;

		.nested {
			border: '1px solid grey';

			.nested-again {
				width: 500px
			}

		}
	}
`);

// returned value
{
	"initial": {
	  "color": "orange",
	  "nested": {
	    "border": "'1 solid grey'",
	    "nested-again": {
	      "width": 500
	    }
	  }
	}
}

Todo

  • Add unit tests
  • Create style objects without a selector
  • Throw helpful errors if the css is not valid

License

MIT Licensed Copyright (c) Cameron Bourke 2016

Keywords

FAQs

Package last updated on 21 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc