npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

native-css

v2.0.0

Published

Convert pure CSS to javascript literal objects or React Style

Downloads

217

Readme

native-css

Convert pure CSS to javascript literal objects or React Style.

NPM Version Build Status

Before Anything!

This tool believes in this semantic for CSS: HTML semantics and front-end architecture

'Cause of this, all ids and inheritance relationships are converted to classes.

Install

Verify if you have node and npm installed.

As CLI
$ npm install -g native-css
As Node Module
$ npm install native-css

CLI Usage

Params: input (required), output (optional)

$ native-css <input> <output>

Example

Input CSS Example:

.taxi {
  background-color: #F8F8F8;
  color: #999;
}

#car {
  color: blue;
}

Convert CSS to React Format

Using:

$ native-css <input> <output> --react

Generate this as output (JS format):

var styles = StyleSheet.create({
  taxi: {
    color: '#999',
    backgroundColor: '#F8F8F8'
  },
  car: {
	color: 'blue';
  }
});

Convert CSS to Literal JS object

Using, but without react flag:

$ native-css <input> <output>

Generate this as output (JS format):

styles: {
  taxi: {
    color: '#999',
    backgroundColor: '#F8F8F8'
  },
  car: {
	color: 'blue';
  }
}

Inherent and Parents

As stated, this tool believes and follows a specific semantic for CSS. Therefore in these cases, exists a change in nomenclature. Example:

Input File (style.css)

.any.element {
  color: red;
}

.parent .child {
  color: orange;
}

.parent  .child2 {
  color: blue;
}

Output File (style.js)

any_element: { 
  color: 'red' 
},
parent__child: { 
  color: 'orange' 
},
parent__child2: { 
  color: 'blue' 
}

Media Queries

Input File (style.css):

@media screen and (min-width: 1020px){
  body .container { 
    width: 1020px !important
  }
}

@media (min-width: 768px){
  body .container {
    width: 748px !important;
    box-sizing: 'all'
  }
}

Output File (style.js):

'@media screen and (min-width:1020px)': { 
  __expression__: 'screen and (min-width:1020px)',
  body__container: { 
    width: '1020px !important' 
  } 
},
'@media (min-width:768px)': { 
  __expression__: '(min-width:768px)',
  body__container: { 
    width: '748px !important',
    boxSizing: 'all'
  } 
}

Module Usage

supported input types: Path, Url, String, Buffer

var nativeCSS = require('native-css'),
	pathToCssFile = 'somePath/file.css';

// Generate JavaScript Object
var cssObject = nativeCSS.convert(pathToCssFile);

url support with async version:

var nativeCSS = require('native-css'),
  URL = 'http://raw.githubusercontent.com/raphamorim/native-css/master/test/fixtures/sample.css';

// Generate JavaScript Object
nativeCSS.convertAsync(URL || Path) // returns Promise
  .then(function(result) {
    // convert/validate data
    // return or throw
  })
  .catch(function(err){
    handleError(err);
  })
  .finally(function(cssObject) {
    handleValue(cssObject);
  });

webpack usage:

cssobjects-loader

Not supported CSS features

React Style does not support CSS selectors, pseudo-classes and CSS animation. Mostly because we try to avoid implicit behaviour and want the user to make layout decisions inside the render() function.

CSS selectors introduce implicit behaviour by not having a direct link with the elements on which they're applied. Therefore there is no way of knowing what the consequences are, and this easily leads to refactoring issues. Instead you should be using plain JavaScript variables.

Classes with pseudo-classes have a higher precedence then classes with no pseudo-classes, which results in issues if you want to override styling in "higher-level" components. In some cases(:before, after, etc.) a component is easily added, in others (active, focus, hover, etc) plain JavaScript will do the trick. In all, you don't need CSS for this. In some cases though you might want to use pseudo-classes (like styling a scrollbar) - which we do support.

Animations inside CSS also introduce implicit behaviour, as CSS animations are decoupled from logic. By being decoupled, the state of the component is split between the component and the CSS animation. We however believe state should be contained within a component. An example of solving this using JS is React Magician.

Contributing

Don't be shy, send a Pull Request! Here is how:

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

About

License: MIT ® Raphael Amorim