CSSFUN CSSFUN v0.4.0

CSSFUN CSSFUN

Near-zero runtime CSS-in-JS library

Write modular CSS within your JavaScript code with built-in themes and SSR support.

const { classes } = css({
    button : {
        backgroundColor : 'blue',
        color : 'white',
        padding : '10px',
        borderRadius : '5px'
    }
});

const Button = () => <button className={classes.button}>Click me</button>;
npm i cssfun

Key Features

Near-zero runtime

Styles are generated once, when the module is initialized, rather than during component rendering. Components only read class names, which are plain strings, so rendering carries no styling cost. With a linked CSS file, the browser only computes the class names: no CSS is built or injected.

Component-scoped styles

Styles live in the same file as the component that uses them. Each class gets a unique generated name, so styles never leak between components.

Stable class names

Class names are hashed from the content of the styles, not from a counter or load order. Identical styles produce identical names on the server and in the browser, across hot reloads and repeated calls, so server-rendered markup always matches the client.

Server-side rendering (SSR) support

StyleSheet.toString() renders every stylesheet as <style> tags for the server response, or StyleSheet.toCSS() writes them to a CSS file the page links. In the browser, stylesheets already in the page, inline or linked, are not injected again.

Built-in theme management

createTheme turns light and dark values into CSS variables that follow the user's color scheme, or a fixed one set with data-color-scheme. Switching schemes needs no re-render.

Framework-agnostic, no build tools

classes maps your style keys to plain class name strings, usable with any UI library or with the DOM directly. Styles are plain JavaScript objects processed at runtime: no compiler, no bundler plugin, no CSS extraction step.

TypeScript support

Ships with type definitions. The classes object is typed from your styles, so you get autocomplete for class names and a type error on typos. CSS properties autocomplete via csstype.

Getting Started

Installing via npm

$ npm install cssfun
import { css } from 'cssfun';

Using native ES modules

No bundler needed: an import map points cssfun to the ES module build, so imports read the same as with npm.

<script type="importmap">
    {
        "imports": {
            "cssfun": "https://cdn.jsdelivr.net/npm/cssfun/dist/cssfun.esm.min.js"
        }
    }
</script>

<script type="module">
    import { css } from 'cssfun';
</script>

The import map can also point to a copy of the file served from your own site, or to https://esm.run/cssfun. During development, use cssfun.esm.js for readable class names.

Using the UMD build

For a classic <script> tag, the UMD build exposes the CSSFUN global object. It also loads as an AMD module, with RequireJS for example.

<script src="https://cdn.jsdelivr.net/npm/cssfun/dist/cssfun.umd.min.js"></script>
const { css } = CSSFUN;

The development build is cssfun.umd.js.

Create your styles

css() injects the styles and returns a StyleSheet instance. Its classes property maps class name selectors to their generated unique class name:

const { classes } = css({
    button : {
        backgroundColor : 'blue',
        color : 'white',
        padding : '10px',
        borderRadius : '5px'
    }
});

Apply the styles to your components:

const Button = () => <button className={classes.button}>Click me</button>;

See Class name generation for how the names are built.

Documentation

  • Writing styles — class name generation and the style syntax: nesting, class references, global styles and at-rules.
  • Themes — creating a theme, color schemes and using its CSS variables.
  • Server-side rendering — rendering styles on the server, hydration, code splitting and Content Security Policy nonces.
  • Extending — custom renderers, subclassing StyleSheet and separate registries.
  • TypeScript — class name inference, CSS property autocomplete and exported types.
  • API documentation — every CSSFUN function, class, method and option.
  • Agent skill — the API, style syntax, themes and best practices for coding agents. Install it with npx skills add 8tentaculos/cssfun, or copy node_modules/cssfun/skills/cssfun into your agent's skills folder.

TypeScript

CSSFUN ships with TypeScript declarations out of the box. The types are bundled in the package and resolved automatically. How classes is inferred from your styles is in the TypeScript guide.

Examples

The example folder contains various sample projects demonstrating how to use CSSFUN in different environments and frameworks. Each example is a standalone project that you can run locally to see CSSFUN in action.

Available Examples

  • React Example: A basic React application demonstrating the use of CSSFUN for styling React components. Try it.
  • Rasti Example: A simple Rasti application illustrating how to apply CSSFUN to style Rasti components. Try it.
  • Vanilla JS Example: A straightforward JavaScript example showing how to use CSSFUN for styling HTML components. Try it.

How this website is built

This website is built with Rasti and CSSFUN, using the Static scaffold provided by create-rasti.

The Static template writes each public route as HTML that can be hosted on services such as GitHub Pages. This combines fast static delivery and SEO-friendly pages with fluid SPA navigation once Rasti hydrates the markup in the browser.

The home page and documentation are generated directly from the Markdown in the CSSFUN repository, keeping the published website aligned with the project source.