Skip to content
πŸ₯œ goober, a less than 1KB πŸŽ‰css-in-js alternative with a familiar API
Branch: master
Clone or download
michael-klein and cristianbote Extract css any target (#43)
* extractCSS now works with any target

* Update README.md

* Update README.md
Latest commit 072f335 Mar 14, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode Extract css any target (#43) Mar 14, 2019
src Extract css any target (#43) Mar 14, 2019
tests Parser full rewrite Feb 22, 2019
.babelrc Parser full rewrite Feb 22, 2019
.gitignore
.travis.yml Fixing the travis ci Mar 7, 2019
LICENSE Create LICENSE Feb 6, 2019
README.md
goober.d.ts bugfix/object hashing (#41) Mar 9, 2019
package-lock.json Added tests for hash issue Mar 9, 2019
package.json bugfix/object hashing (#41) Mar 9, 2019
typings.json Fix the d.ts path for typings.json Mar 7, 2019

README.md

goober

πŸ₯œ goober, a less than 1KB css-in-js solution.

npm version Build Status gzip size coverage Gitter

Motivation

I always wondered, if you can get a working solution for css-in-js with a smaller footprint. I started a project and wanted a to use styled-components. Looking at their sizes, it seems that I would rather not include ~16kB(styled-components) or ~11kB(emotion) just so I can use the styled paradigm. So, I embarked in a mission to create a smaller alternative for these well established apis.

Usage

The API is inspired by emotion, styled function. Meaning, you call it with your tagName and returns a vDOM component for that tag. Note, setPragma is needed to be run before the styled function is used.

import { h } from "preact";
import { styled, setPragma } from "goober";

// Should be called here, and just once
setPragma(h);

const Icon = styled("i")`
  display: flex;
  flex: 1;
  color: red;
`;

const Button = styled("button")`
  background: dodgerblue;
  color: white;
  border: ${Math.random()}px solid white;

  &:focus,
  &:hover {
    padding: 1em;
  }

  .otherClass {
    margin: 0;
  }

  ${Icon} {
    color: black;
  }
`;

Examples

SSR

You can get the critical CSS for SSR, via extractCss. Take a look at this example: CodeSandbox: SSR with Preact and goober and read the full explanation for extractCSS and targets below.

API

As you can see it supports most of the syntaxes of CSS. If you find any issues, please submit a ticket or even a PR with a fix.

styled(tagName)

  • @param {String} tagName The name of the dom element you'd like the styled to be applied to
  • @returns {Function} Returns the tag template function.
import { styled } from "goober";

const Btn = styled("button")`
  border-radius: 4px;
`;

setPragma(pragma: Function)

Given the fact that react uses createElement for the transformed elements and preact uses h, setPragma should be called with the proper pragma function. This was added to reduce the bundled size and being able to bundle esmodule version. At the moment I think it's the best tradeoff we can have.

import React from "react";
import { setPragma } from "goober";

setPragma(React.createElement);

css(taggedTemplate)

  • @returns {Function} Returns the tag template function.

Same as styled but without the tagName and vNode generation. In the end the output will be a className.

import { css } from "goober";

const BtnClassName = css`
  border-radius: 4px;
`();
// (!) Note the empty param at the end. If you wanna use `props` throughout the syntax this is the place to put them

const btn = document.querySelector("#btn");
btn.classList.add(BtnClassName);

targets

By default, goober will append a style tag to the <head> of a document. You might want to target a different node, for instance, when you want to use goober with web components (so you'd want it to append style tags to individual shadowRoots). For this purpose, you can .bind a new target to the styled and css methods:

import * as goober from "goober";
const target = document.getElementById('target');
const css = goober.css.bind({target: target});
const styled = goober.styled.bind({target: target});

If you don't provide a target, goober always defaults to <head> and in environments without a DOM (think certain SSR solutions), it will just use a plain string cache to store generated styles which you can extract with extractCSS(see below).

extractCss(target?)

  • @returns {String}

Returns the <style> tag that is rendered in a target and clears the style sheet. Defaults to <head>.

const { extractCss } = require("goober");

// After your app has rendered, just call it:
const styleTag  = extractCss();

glob

To create a global style, you need to call glob with your global tagged template. Usually here's a good idea to place document wide styles.

import { glob } from "goober";

glob`
  html,
  body {
    background: light;
  }

  * {
    box-sizing: border-box;
  }
`;

Features

  • Basic CSS parsing
  • Nested rules with pseudo selectors
  • Nested styled components
  • Media queries (@media)
  • Keyframes (@keyframes)
  • Smart(lazy) client-side hydration
  • Vanilla(via css function)
  • globalStyle(via glob) so one would be able to create global styles
  • target/extract from elements other than <head>
  • Vendor prefixing

Contributing

Feel free to try it out and checkout the examples. If you wanna fix something feel free to open a issue or a PR.

You can’t perform that action at this time.