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

@geekie/css

v1.0.0

Published

A simple CSS-in-JS solution that extracts to CSS files

Downloads

14

Readme

@geekie/css

npm License Travis Codecov Prettier

A simple CSS-in-JS solution that extracts CSS files.

About

This library was created at Geekie when we started porting parts of our RN app to a web version. We evaluated most existing libraries and none would accomplish all our requirements:

  • extract final CSS to a static file
  • an API very similar to RN styling
  • support for dynamic styles

Usage

A simple example of the API:

import { StyleSheet, css } from "@geekie/css";

const styles = StyleSheet.create({
  header: {
    fontSize: 18,
    fontWeight: "bold"
  },
  bordered: {
    border: "1px solid #000"
  }
});

function Header({ children }) {
  return (
    <span className={css(styles.header, styles.bordered)}>{children}</span>
  );
}

When used with our Webpack plugin, the StyleSheet.create() call will be replaced by a simple object, where the keys are kept and each value is a string of [atomic classnames] separated by an space. Each classname correspond to a single style defined in the object passed to StyleSheet.create. See the following output for an illustration:

import { css } from "@geekie/css";

// these classnames are just an example
const styles = {
  header: "gkcss0_a4bcdef gkcss1_b9cdk4l",
  bordered: "gkcss2_a84ifj"
};

function Header({ children }) {
  return (
    <span className={css(styles.header, styles.bordered)}>{children}</span>
  );
}

And the css() function will select one classname from each "type", to prevent non-deterministic behavior caused by the order of CSS rules order.

In the cases where the styles can't be computed statically, the StyleSheet.create() will simply return the first argument, and when the css() function receives an object as argument, it will fallback to add class dynamically, very similar to the [glamor] (and others) behavior. An example:

css(
  // assume these are the output of a `StyleSheet.create()` call
  "gkcss0_a4bcdef gkcss1_b9cdk4l",
  "gkcss0_g83nvn",

  // these are dynamic styles
  { color: "red" },
  { fontFamily: "Verdana", color: "black" }
);

The call above will return:

"gkcss0_g83nvn gkcss1_b9cdk4l css-839fn39"

And a corresponding CSS rule will be created:

.css-839fn39 {
  font-family: Verdana;
  color: black;
}

Webpack integration

In order to benefit from the build time CSS compilation, you need to use the companion loader and plugin. Example:

// webpack.config.js

const GkCssPlugin = require("@geekie/css/webpack");

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          // we recommend adding the loader as the last loader
          {
            loader: GkCssPlugin.loader,
            options: {
              globals: {
                // include any globals that may affect the CSS styles
                // so they can be evaluated statically
                __DEV__: process.env.NODE_ENV !== "production"
              }
            }
          }
        ]
      }
    ]
  },
  plugins: [
    // define the filename of the compiled CSS
    new GkCssPlugin("style.css")
  ]
};

TODO

  • Manage "conflicts" between general vs specific styles (e.g. border vs border-left)