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

inline-style-emitter

v0.0.3

Published

<div style="text-align:center;font-size:1.6em;margin-bottom:1.5em">Inline Style Emitter</div>

Downloads

6

Readme

What it is: A library which helps generate CSS from inline style objects.

Why does it exist: Generating CSS rules can be viewed as part of rendering. There is no reason, on the conceptual level, to have a split between generating the markup and the corresponding style rules. The problem with inline styles though is that they don't offer everything that is possible through external CSS stylesheets: pseudo classes/elements and media queries. You can work around this by re-implementing these browser-native features in your own React code (what Radium does). But that is additional work which should not be necessary. The goal is to take advantage of the already existing styling facilities provided by the browsers.

Current tools (of which there are many) treat generating CSS as part of the compile/build/bundle step. In doing so they require complicated toolchains to extract the CSS rules from the source files.

How it works: The idea is to generate the CSS rules at the same time as React elements, and then insert both into the browser at the same time. Just like ReactDOM.render updates the DOM, a similar function is used to update a CSSStyleSheet with the styles that are needed by the DOM. That way we never generate more CSS rules than necessary for the current DOM.

Similar to how React can render the elements into a browser or into a string (for server-side rendering), we can emit the CSS rules into the browser CSSStyleSheet objects or into an external file. This means we can also generate a static site while defining them with inline style syntax and still have the CSS rules written into an external file!

Example (browser)

import React from "react";
import ReactDOM from "react/dom";
import {Handle, DocumentEmitter} from "inline-style-emitter";


// Some component which uses inline styles.
function Avatar({ url, username }) {
    return (
        <div style={{display:'flex',flexDirection:'row'}}>
            <img style={{display:'block',width:'20rem',height:'20rem'}}
                 src={url}
            />
            <div style={{flex:1}}>{username}</div>
        </div>
    );
}


// Create a Handle which manages all interaction with the DOM.
// Do this during startup and keep the Handle around forever.
let styleEmitterH = new Handle(new DocumentEmitter(document));

// The function which you use to render the React elements into
// your container.
function myRender(root, containerElement) {
    // Process the tree, from its root recursively, and generate
    // CSS rules from 'style' props. This also transforms the
    // elements and replaces style props with className.
    let newRoot = processStyleProperties(styleEmitterH, React, root);

    // Render the new root. At this point all the necessary CSS
    // rules have been inserted into the DOM and are visible by
    // the browser.
    ReactDOM.render(newRoot, containerElement);
}


// Now we render the avatar as our root. After this the document's
// head will contain a <style> element which holds the CSS rules
// which are required by the avatar.
myRender(<Avatar url="..." username="wereHamster" />,
    document.getElementById('root-container'));