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

stylekit

v0.2.3

Published

In-browser CSS generation with variable binding and live updates

Downloads

762

Readme

stylekit

stylekit allows style elements to be created in the browser and bind their CSS rules to defined variables. The idea is to make it easy to construct applications with customisable themes that are modifiable at runtime.

Terminology

A StyleSet is a list of StyleBlocks plus a global map of variable names/values. Each StyleBlock manages a DOM style element and its constituent CSS. This architecture allows a themeable application to create one StyleBlock for each module, with the global theme centrally controlled by the StyleSet's variables.

Variables

Each StyleSet has an associated map of named variables that can be referenced from CSS rules using a dollar-prefix (e.g. $MY-VAR). Each StyleBlock will automatically update it's linked style tag when any of its variables' values are altered.

There is no support for LESS/SCSS-like calculation syntax - stylekit is not a general purpose preprocessor.

Usage

Install:

$ npm install stylekit

Instantiate:

var stylekit = require('stylekit');
var myStyles = stylekit();

Example

See demo/index.htm for an example that dynamically binds the rules of a couple of StyleBlocks to form inputs.

API

var styleSet = stylekit([doc])

Create a new StyleSet with the given document doc. If omitted, doc defaults to global.document || document.

styleSet.vars

A wmap containing this StyleSet's variable mappings. In addition to the usual wmap methods, getInt(key) and getFloat(key) are also provided which will return the corresponding variables as ints and floats, respectively.

You can use the vars.watch() method to register your own callbacks to be fired when given variables are changed. This is useful, for example, if your application's layout is not purely CSS-driven and Javascript update routines must be called when the theme changes.

styleSet.block()

Attach a new StyleBlock to this StyleSet and return it.

styleBlock.appendCSS(css) (chainable)

Append a CSS string to this block. Each StyleBlock simply accumulates CSS in a string buffer so it's your responsibility to ensure that it's syntactically correct.

Variables are referenced with a dollar-prefix:

styleSet.vars.set('MAIN_COLOR', 'red');
styleBlock.appendCSS('h1 { color: $MAIN_COLOR }');

styleBlock.rule(selector, css)

Create (possibly nested) CSS rules using an instance of css-builder.

styleBlock.commit()

Create a style element and load this StyleBlock's CSS into the DOM. A StyleBlock is immutable once committed, although it will regenerate whenever any of its variables change.

styleBlock.destroy()

Remove this block's style element from the DOM.

Known Issues

  • Whilst recursive variable replacement is supported, dynamic updates are only triggered when a variable referenced directly by a block's CSS is updated. This will likely be fixed in the future.