styledjsx

package module
v0.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 6 Imported by: 0

README

styledjsx

Go Reference

A native implementation of styled-jsx for Go. Works with both JSX and TSX files.

It's built on:

  • jsx: A JSX parser
  • css: A CSS parser

Features

  • Very fast (native Go!)
  • Supports CSS minification via ESBuild
  • Supports vendor prefixing via ESBuild
  • Supports class and className variants
  • Supports <style scoped> and <style jsx> variants

Usage

Given the following JSX:

export default () => (
  <main class="main">
    <h1>hello</h1>
    <style scoped>{`
      .main {
        background: blue;
      }
      h1 {
        color: red;
      }
    `}</style>
  </main>
)

Rewrite that JSX with the following:

out, _ = styledjsx.Rewrite("input.jsx", jsx)

Resulting in:

import Style from "styled-jsx"

export default () => (
  <main class="jsx-8mUTT main">
    <h1 class="jsx-8mUTT">hello</h1>
    <Style scoped>{`
      .main.jsx-8mUTT{background:#00f}
      h1.jsx-8mUTT{color:red}
    `}</Style>
  </main>
)

Note: you'll need to bring your own <Style/> component. The JS library styled-jsx/style might work, but it's currently untested.

Planned
  • Scope @keyframe names
  • :global() support
  • VSCode support for <style scoped> (fork this repo)
  • Example client library instead of styled-jsx
Unplanned

styled-jsx has a plethora of ways to include CSS. I tend to stick with the very basics. Here are some features that are not planned and what the alternative is:

  • Dynamic styles: Use toggling class names or CSS variables to provide this.
  • <style global>: Use <style> instead.
  • Embedding constants: This would be nice, but it's not worth the effort.
  • Plugins: Write an ESBuild plugin instead.

Why?

Styled JSX is still the most natural way to write CSS in JSX.

You'll probably use this library alongside a bundler like ESBuild.

Install

go get github.com/matthewmueller/styledjsx

Contributors

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Rewrite

func Rewrite(path, code string) (string, error)
Example
package main

import (
	"os"

	"github.com/matthewmueller/styledjsx"
)

func main() {
	const jsx = `
export default () => (
  <main class="main">
    <h1>hello</h1>
    <style scoped>{` + "`" + `
      .main {
        background: blue;
      }
      h1 {
        color: red;
      }
    ` + "`" + `}</style>
  </main>
)
	`
	rewriter := styledjsx.New()
	rewriter.Minify = true
	example, _ := rewriter.Rewrite("example.jsx", jsx)

	os.Stdout.WriteString(example)
}
Output:

import Style from "styled-jsx";

export default () => (
  <main class="jsx-8mUTT main">
    <h1 class="jsx-8mUTT">hello</h1>
    <Style scoped id="jsx-8mUTT">{`.main.jsx-8mUTT{background:#00f}h1.jsx-8mUTT{color:red}`}</Style>
  </main>
)

Types

type Import

type Import struct {
	Name      string
	Path      string
	IsDefault bool
}

func (*Import) String

func (i *Import) String() string

type Rewriter

type Rewriter struct {
	Import Import
	Attr   string
	// Used by ESBuild
	Minify  bool
	Engines []esbuild.Engine
}

func New

func New() *Rewriter

func (*Rewriter) Rewrite

func (r *Rewriter) Rewrite(path, code string) (string, error)

func (*Rewriter) RewriteAST

func (r *Rewriter) RewriteAST(path string, script *ast.Script) error

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL