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

stylin

v5.1.3

Published

Javascript to CSS: A convenience wrapper around free-style

Downloads

314

Readme

stylin Build Status

Javascript to CSS: Convert a style object into css and insert it into the DOM at runtime.

This library is a convenience wrapper around free-style that provides auto-prefixing and an easier API.

Total size is less than 6KB gzipped.

npm install --save stylin

Example

var css = require('stylin')
var h = require('virtual-dom/h')

function render (color) {
  var style = {
    color: color,
    '&:hover': {
      backgroundColor: color
    }
  }
  return h('div', {className: css(style)})
}

render('red') // generates new stylesheet and className
render('red') // re-uses stylesheet and className
render('blue') // generates new stylesheet and className

API

All styles are set to !important by default, to avoid the pains of style-priority.

stylin will mutate passed in style objects to add browser prefixes and !important the first time they are passed in. We mutate objects for two performance increases:

  • We only need to add prefixes and !important once per style object
  • We don't thrash the garbage collector with a deluge of new objects

stylin(style, [styleOptions]) -> className

Returns a string className for the given style object. All style values will be marked as !important. Use stylin.unimportant when this is not wanted.

style

Type: Object (required)

An object of style key/value pairs, which will be prefixed and passed to FreeStyle#registerStyle.

styleOptions

Type: Object

styleOptions.styleId

If passed in, use a FreeStyle instance and stylesheet matching styleId for these styles. By default, all styles are shared between one FreeStyle instance and <style> tag using stylin.STYLE_ID.

stylin.unimportant(style, [styleOptions]) -> className`

Returns a string className for the given style object. This the same as the the main stylin method above, except it does not mark style values as !important.

This is good to use with elements who will have styles added to them by third-party libraries, so as not to override those styles.

stylin.rule(key, style, [styleOptions]) -> undefined

Creates a global rule from the given styles extended together. Use it for font-faces and other global rules.

Passes a prefixed rule object to FreeStyle#registerRule.

See stylin() method above for documentation of styleOptions.

Example:

var css = require('stylin')
css.rule('@font-face', {
  fontFamily: '"Bitstream Vera Serif Bold"',
  src: 'url(https://mdn.mozillademos.org/files/2468/VeraSeBd.ttf)'
})

stylin.keyframes(style, [styleOptions]) -> animationName

Returns a string animationName for the given keyframe definition.

Passes a prefixed keyframe object to FreeStyle#registerKeyframes.

See stylin() method above for documentation of styleOptions.

Example:

var css = require('stylin')
var animationName = css.keyframes({
  from: {opacity: 0},
  to: {opacity: 1}
})

stylin.getCss([styleId]) -> cssString

styleId defaults to stylin.STYLE_ID.

Returns a string of all styles generated. Intended for use in server-side rendering.

stylin.reset()

Removes all saved styles. Good for "clearing" styles before each server-side render.

stylin.STYLE_ID

This is the string ID of the <style> element that styles will be placed into by default.

For server side rendering, send the following to the client, and the client will find the existing styles in the style tag and merge them:

var stylin = require('stylin')
function serverRenderApp () {
  var appHtml = renderMyApp()
  return `
    <html>
    <head>
      <style id="${stylin.STYLE_ID}">${stylin.getCss()}</style>
    </head>
    ... everything else ...
  `
}

License

MIT © Andrew Joslin

MIT © Blake Embrey, free-style