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

css-template

v0.2.3

Published

convert css string to style object. Useful for inline styling in React

Downloads

417

Readme

css-template

Reduce context-switching when defining style in React Component.

To use inline styles in React, you often find yourself writing this type of code:

const styles = {
  title: {
    borderBottomLeftRadius: '10px',
    //    ^     ^   ^       ^    ^^
    //    |     |   |       |    ||___ annoying trailing comma
    //    |     |   |       |____|____ annoying JS quotes
    //    |_____|___|_________________ annoying camel case
    //
    //    (╯°□°)╯︵ ┻━┻ I WANT CSS BACK!!!
  }
}

With css-template, those times are gone! Instead of writing this:

// THIS IS BAD FOR YOUR EYES
const styles = {
  title: {
    marginTop: '10px',
    fontSize: '120%',
    lineHeight: '1.5',
    textAlign: 'center',
    backgroundColor: 'rgba(100, 255, 100, 0.7)',
  },
  footer: {
    width: 'calc(100% - 16px)',
    textAlign: 'right',
    marginTop: '20px',
  }
};

you can write something like this

// THIS IS BETTER
const styles = {
  title: css`{
    margin-top: 10px;
    font-size: 120%;
    line-height: 1.5;
    text-align: center;
    background-color: rgba(100, 255, 100, 0.7);
  }`,
  footer: css`{
    width: calc(100% - 16px);
    text-align: right;
    margin-top: 20px;
  }`,
};

Supported Features

  • translate snake-case to camelCase

      css`margin-top: 20px` // will be translated to { marginTop: '20px' }
  • multiple lines with optional brackets for better visuals

      css`{
        padding: 10px;
        margin: 10px;
      }`
    
      // is equivalent to
      css`
        padding: 10px;
        margin: 10px;
      `
  • optional final semicolon

      css`padding: 10px`
    
      // is equivalent to
      css`padding: 10px;`
  • multiple rules in one line

      css`padding: 10px; margin: 10px`
  • compose other style objects

      const bigFont = css`font-size: 200%`
      const underlined = css`text-decoration: underline`
    
      const myStyle = css`{
        composes: ${bigFont};
        composes: ${underlined};
        padding: 10px;
        margin: 10px;
      }`
    

Installation

npm i -S css-template

Usages

import css from 'css-template';

const COLOR_MAIN = 'white';
const BACKGROUND_MAIN = '#336699';
const awesomeStyles = css`font-size: 200%`;

const styles = {
  header: css`{
    padding: 10px 0 20px 10px;
    text-align: center;
  }`,
  main: css`{
    composes: ${awesomeStyles};
    color: ${COLOR_MAIN};
    background-color: ${BACKGROUND_MAIN};
  }`
};

const MyComponent = (props) => (
  <div>
    <div style={styles.header}>
      {props.header}
    </div>
    <div style={styles.main}>
      {props.main}
    </div>
  </div>
);

ROADMAP

  • [x] composes: ${otherStyles}; just like postcss composes feature
  • [ ] optional auto-prefixer
  • [ ] spread numeric values like padding: ${[10, 20, 0, 10]}px;