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

stylor

v1.0.0

Published

Create virtual stylesheets scoped to a component.

Downloads

4

Readme

stylor

This is an ES6 module that lets you create a virtual stylesheet for a component. It takes two arguments: base and styles. base is a valid CSS selector for the element to which the stylesheet will be attached. This should be unique in the document to prevent style leaks. So, best to use an id for base. styles is an object defining the stylesheet you want to create.

Installation

Install from npm:

npm i -g stylor

Then import it into your project. Stylor is an ES6 module, so use standard ES6 import syntax:

import {createStylesheet} from 'stylor'

After importing stylor, you need to use it in a lifecycle event. For React, Preact or Inferno: componentDidMount, for Vue: created, Angular: ngOnInit and Composi: componentWasCreated. If you are using it with a library that does not have lifecycle events, use it during a DOM ready event.

Creating a Styles Object

When creating the object to define the stylesheet there are several rules to be aware of. Because this is a JavaScript object, it must follow JavaScript naming conventions. That means any CSS properties with hyphens must be converted to camel case or quoted. Any properties with non-alphabetic characters, such as : must be quoted. An all values must be quoted.

Semicolons

Because this is a JavaScript object, you can't put semicolons at the end of a definition. Instead use a comma:

// Correct usage:
createStylesheet('#list', {
  // Use comma to separate from next property:
  border: 'solid 1px #ccc',
  margin: '20px'
})

// Incorrect:
createStylesheet('#list', {
  // Do not put semicolons at end!
  // This will generate an error:
  border: 'solid 1px #ccc';
  margin: '20px';
})

Camel Case or Quoted

Below are some examples of camel case and quoted properties:

// Camel case:
createStylesheet('#list', {
  backgroundColor: '#ff0000',
  fontFamily: 'sans-serif',
  textAlign: 'center'
})

// Or quoted:
createStylesheet('#list', {
  'background-color': '#ff0000',
  'font-family': 'sans-serif',
  'text-align': 'center'
})

Pixel Values

If you're using a pixel value for a property, you can leave off the length identifier and provide just a raw number:

createStylesheet('#list', {
  // Define margin of 20px:
  margin: 20,
  // Define width of 300px:
  width: 300
})

Nested Elements

Like LESS and SASS, you can nest child elements to define their relationship to parents. This also works for pseudo-elements.

createStylesheet('#main', {
  ul: {
    margin: 10,
    width: 350,
    border: 'solid 1px #ccc',
    li: {
      padding: 10,
      borderBottom: 'solid 1px #ccc',
      transition: 'all .25s ease-out',
      ':hover': {
        cursor: 'pointer',
        backgroundColor: '#333',
        color: '#fff'
      },
      ':last-of-type': {
        border: 'none'
      }
    }
  }
})

This will produce the following stylesheet:

#main ul {
  margin: 10px;
  width: 350px;
  border: solid 1px #ccc;
}
#main ul li {
  padding: 10px;
  border-bottom: solid 1px #ccc;
  transition: all .25s ease-out;
}
#main ul li:hover {
  cursor: pointer;
  background-color: #333;
  color: #fff;
}
#main ul li:last-of-type {
  border: none;
}

BEM

This will also work with BEM. When doing so, best to just use the generic body tag as the base for the stylesheet:

<ul class="list">
  <li class="list__item">
    <h3 class="item__title">Joe Bodoni</h3>
  </li>
  <li class="list__item">
    <h3 class="item__title-selected">Ellen Vanderbilt</h3>
  </li>
  <li class="list__item">
    <h3 class="item__title">Sam Anderson</h3>
  </li>
</ul>

Define BEM CSS for above markup:

createStylesheet('body', {
  '.list': {
    margin: '20px 0',
    listStyle: 'none',
    border: 'solid 1px #ccc'
  },
  '.list__item': {
    padding: 0,
    borderBottom: 'solid 1px #ccc',
    ':last-of-type': {
      border: 'none'
    }
  },
  '.item__title': {
    margin: 0,
    padding: 10,
    ':hover': {
      backgroundColor: '#333',
      color: '#fff',
      cursor: 'pointer'
    }
  },
  'item__title-selected': {
    backgroundColor: '#333',
    color: '#fff'
  }
})