---
name: goober
description: Use goober, a roughly 1.2 KB css-in-js library with an emotion-style `styled`, `css`, `keyframes` and `extractCss` API. Use when adding or editing styles in a project that depends on goober, when the user asks for a tiny css-in-js solution, or when setting up goober with React, Preact or plain JavaScript, server rendering, theming or TypeScript.
---

# goober

goober is a css-in-js library with the `styled` paradigm in about 1.2 KB gzipped. It is framework agnostic: it never imports React or Preact, so you hand it a pragma once.

Full docs as one file: https://goober.rocks/llms-full.txt
Index with per-topic links: https://goober.rocks/llms.txt

## Do this first

`setup` must run once, in the entry file, before the first `styled()` call. Forgetting it is the most common failure: rendering a styled component throws a TypeError because there is no pragma to call.

```js
import { setup } from 'goober';
import { createElement } from 'react'; // Preact: import { h } from 'preact'

setup(createElement);
```

`setup(pragma, prefixer?, theme?, forwardProps?)`. Only `pragma` is required.

## Core API

```js
import { styled, css, keyframes, extractCss } from 'goober';

// styled: returns a component
const Button = styled('button')`
    background: dodgerblue;
    &:hover {
        background: tomato;
    }
`;

// Styles from props: function, object or array
const Box = styled('div')((p) => ({ opacity: p.dim ? 0.4 : 1 }));
const Tag = styled('span')`
    color: ${(p) => p.color};
`;

// css: returns a class name, works without any framework
const cls = css`
    border-radius: 4px;
`;
el.classList.add(cls);

// keyframes: returns the animation name
const spin = keyframes`from { transform: rotate(0) } to { transform: rotate(360deg) }`;
const Spinner = styled('div')`
    animation: ${spin} 1s linear infinite;
`;
```

-   Extend a component with `styled(Button)` and a template. Reference one component inside another with an interpolation such as `${Icon} { color: red; }`.
-   Change the rendered tag with the `as` prop.
-   Style your own component: it must accept and apply `className`.
-   Refs: pass the framework's forwardRef as the second argument: `styled('h1', React.forwardRef)`.

## Add-ons (separate import paths)

| Need                      | Import                                                                                       |
| ------------------------- | -------------------------------------------------------------------------------------------- |
| Global styles             | `import { createGlobalStyles } from 'goober/global'`                                         |
| Vendor prefixing          | `import { prefix } from 'goober/prefixer'`, then `setup(pragma, prefix)`                     |
| Filter props from the DOM | `import { shouldForwardProp } from 'goober/should-forward-prop'`, pass as 4th arg of `setup` |
| `styled.div` syntax       | `babel-plugin-transform-goober`, or `goober/macro` with babel-plugin-macros                  |
| `css` prop                | `@agney/babel-plugin-goober-css-prop`                                                        |

Transient props (`$size`) are not built in. Add them with `shouldForwardProp((prop) => prop[0] !== '$')`.

## Theming

Pass a hook as the third argument of `setup`. Every styled component then receives `props.theme`.

```js
const ThemeContext = createContext({ primary: 'blue' });
setup(createElement, undefined, () => useContext(ThemeContext));
```

## Server rendering

Render first, then call `extractCss()`. It returns the css string and clears the sheet. Inline it with the id `_goober` so the client reuses it.

```js
const html = renderToString(<App />);
const page = '<style id="_goober">' + extractCss() + '</style>' + html;
```

Call `css` inside a function or use `styled` so the rule is created at render time. A bare top-level `css` call runs at import time and can be missed by `extractCss`.

Render into another node (shadow roots, iframes) by binding a target: `const css = goober.css.bind({ target: node })`. For a CSP nonce set `window.__nonce__` before goober loads.

## TypeScript

Types ship with the package. Props: put the generic after the tag, as in `styled('div')<{ size: number }>` followed by the template. Theme: augment `DefaultTheme` with `declare module 'goober' { export interface DefaultTheme { ... } }`.

## Not supported (do not generate these)

-   `.attrs(...)` and `.withComponent(...)`
-   Labels and emotion's `ClassNames`
-   A default export. Always use named imports: `import { styled } from 'goober'`.
-   `styled.div` without the babel plugin or macro. Use `styled('div')`.

## Common mistakes

1. Missing `setup(...)`, or calling it after a component already rendered.
2. Using `styled.div` with no plugin configured.
3. Leaking custom props such as `size` onto DOM nodes. Filter them with `shouldForwardProp`.
4. Calling `extractCss()` before rendering, or calling it twice and expecting the second call to return css.
5. Reaching for emotion or styled-components APIs from memory. Check the docs link above if unsure.
