Skip to content

Instantly share code, notes, and snippets.

@segunadebayo
Last active October 2, 2021 13:00
Show Gist options
  • Save segunadebayo/631907b87bb40122441657120bcd760d to your computer and use it in GitHub Desktop.
Save segunadebayo/631907b87bb40122441657120bcd760d to your computer and use it in GitHub Desktop.
Ideas for Static CSS Generation of JSX Style Props
// Button.js => Button.css
import { chakra } from "@chakra-ui/magic";
import "@chakra-ui/css/Button.css";

<chakra.button mb="2" mt="2" _hover={{ bg: "red.200", _focus: {} }} />;

<button css={{mb: "2", mt="2", _hover: { bg: "red.200", _focus: {} }} />

const result = interpret(
  `<chakra.button mb="2" mt="2" _hover={{bg: "red.200", _focus: {}}} />`
);

function Button({ className, style }) {
  return (
    <chakra.button mb="2" mt="2" className={className}>
      {children}
    </chakra.button>
  );
}

// <button className={cx("css-hash-12", className)}>

const dict ={
    ".css-hash-12": {
        mb: 2,
        mt: 2,
    },
    ".css-hash-Button": {
        mt: 5
    }
}

function Page() {
  return <Button mt="5">Hello</Button>;
}

// <Button className="css-hash-Button" />

// Store the styles as object

result.element; // button

result.styleProps - // ...
  result.static - // ...
  result.dynamic; // ...

result.attributes; // ...

const result = compile(
  `<chakra.button mb="2" mt="2" _hover={{bg: "red.200", _focus: {}}} />`
);

result; // => <button class="css-hash" />
result; // => <chakra.button class="css-hash" mb={Math.random() ? "3": "4"} />
<template>
  <chakra.button mb="2" mt="2" :hover="{bg: "red.200", _focus: {}}" />
</template>
<script lang="ts">
const variants = createVariants({
  true: {},
  false: {},
});
</script>
import { chakra, variants, cx } from "magic"
import { compile } from "magic/compiler"

module.export = esbuild({
    fileProcessor: (file, content)=>{
      compile(file, {output: ".chakra"})
    }
})

import "button-fileHash.css"

const sizeVariants = variants({
    "sm": {},
    "md": {}
})

<chakra.div mt="3" bg={Math.random() ? "red" : "green"} _hover={isDragging: styles.dragging: styles.default} />

const style = getStyle(isDragging)

<div
  className={`static-css ${style}`}
  style={{"--bg-hash": Math.random() ? "red" : "green" }}
>
.css-hash{
    margin-top: 3px
    bg: var(--bg-hash)
}

Process

  • If an opening element is chakra element
  • Take all the static attributes and add them to a __style attribute
  • Take all the attributes with expressions (primitive), write to __style and add css variable to style atribute
  • For object styles defined in _hover, _active, etc. write static parts to __style and css variable to style attribute
  • For any component definition, take all the __style attributes and convert them to css strings
  • For every __style css string, render them to a single <style jsx>

Plugin Helpers

  • getJSXStyleProps
  • mergeClassName
  • isJSXElement
  • addInlineStyle
  • writeCSSVariable
  • mergeObjects
  • removeJSXAttribute
  • removeStyleProps

Inspiration

Tools

Example

const input = <chakra.div mt="2" mb="4" _hover={{ mt: "2" }} />;

const output = (
  <>
    <div className="__hash" />
    <style jsx>
      {`
          .hash {
              margin-top: 8px
              margin-bottom: 8px
          }

          .hash:hover{
              margin-top: 8px
          }
        `}
    </style>
  </>
);

Babel Plugin Setup

// babel plugin options
{
  "plugins": ["chakra-jsx", "styled-jsx"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment