0.0.23 • Published 9 months ago

radipan v0.0.23

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Radipan

The missing css prop API for 🐼 PandaCSS.

Radipan is an all-in-JS CSS-in-JS engine. With Radipan, you can write all your styles in JavaScript without the need for HTML or CSS.

Features

  • Real Zero-Runtime:
    • Built-in transpiler that transforms css prop into its resulting className prop at build time, and generates transpiled .lite.ts files for you to import and use to completely rid of the dependence on Radipan (or PandaCSS)
  • Flexible API:
    • JSX-compatible API
    • HyperScript-compatible API
      • h(tag, attrs, [text?, Elements?,...])
  • Recipe Shaking Optimization (coming soon...)

How it works

Radipan runs your code during build time and scan the entry component and its child nodes for inline css props and transforms these props into intermediate PandaCSS code (css, cva, etc.), which generates static CSS code.

• Why?

Radipan supports the widely adopted inline css prop syntax that made popular by libraries like Emotion.js, Stitches.js and Mitosis, etc...

Examples

Radipan works with various frameworks and tools, such as React, Preact, Vite, Next.js, etc.

(More coming soon...)

Usage

Radipan comes with HyperScript-compatible API, which you can import and use like h(tag, attrs, [text?, Elements?,...]):

import { h } from 'radipan';

function App() {
  return h('div', { css: { color: 'red' } }, 'whee!')); // Red whee!
}

You can also use Radipan's own withCreate function, which comes with a more verbose create method:

import { withCreate } from 'radipan';

function App() {
  return withCreate('div').create({ css: { color: 'red' } }, 'whee!')); // Red whee!
}

HTML Tags

You can import HTML tags from radipan/tags for convenience:

import { div, form, input, span } from 'radipan/tags';

function App() {
  return form.create({css: { display: 'flex' }}, [
    div.create({ css: { color: 'green' } }, 'green div'));
    span.create({ css: { color: 'red' } }, 'red span'));
    input.create({ css: { color: 'blue' }, placeholder: 'blue input' }));
  ]);
}

Recipes

You can use recipes directly within the css prop. Recipes are a way to define styles for different variants of a component. They offer better performance, developer experience, and composability.

A recipe consists of four properties:

  • base: The base styles for the component
  • variants: The different visual styles for the component
  • compoundVariants: The different combinations of variants for the component
  • defaultVariants: The default variant values for the component

A recipe must have at least variants to be recognized as a recipe. Use a nested object to specify the variant name and the corresponding style in variants. For example:

import { withCreate } from "radipan";

const Badge = ({
  as = "span",
  size = "md", // 'size' is a recipe variant
  variant = "solid", // 'variant' is also a recipe variant
  children = undefined,
  ...props
} = {}) => {
  return withCreate(as).create(
    {
      size,
      variant,
      ...props,
      css: badgeRecipe,
    children
  );
};

const badgeRecipe = {
        base: {
          borderRadius: "xs",
          textTransform: "uppercase",
        },
        variants: {
          size: {
            sm: { fontSize: "xs", padding: "0 2px" },
            md: { fontSize: "sm", padding: "0 var(--spacing-1x)" },
            lg: { fontSize: "md", padding: "1px var(--spacing-1x)" },
          },
          variant: {
            solid: {
              /* solid style CSS code */
            },
            subtle: {
              /* subtle style CSS code */
            },
            outline: {
              /* outline style CSS code */
            },
          },
        },
      },
    };

export default withCreate(Badge);

Setup

Installation

Use your preferred package manager to install Radipan:

npm install --save-dev radipan@latest

Configuration

Create a file named radipan.config.ts (or .js) file in the root of your project and add these configuration options for Radipan:

import { defineConfig } from "radipan/config";

export default defineConfig({
  include: ["src"], // Source paths to include for CSS processing
  exclude: [""], // Source paths to exclude from scanning
  includeNames: ["*.ts", "*.tsx"], // Source files to include for CSS processing
  excludeNames: [
    "main.tsx",
    "*.init.ts",
    "*.init.tsx",
    "*.lite.ts",
    "*.lite.tsx",
  ], // Source files to exclude from scanning
  jsxFramework: "react", // "react" | "solid" | "preact" | "vue" | "qwik"
  preflight: true, // Whether to use css reset
  recipeShaking: true, // Whether to trim unused recipe variants
  sourceTranspile: {
    // Whether to transform `css` prop to corresponding `className`
    enabled: true,
    // File extension for the transpiled output files
    extension: ".lite.tsx",
  },
  outdir: "@design-system", // The output directory in /node_modules
  theme: {
    // Useful for theme customization
    extend: {},
  },
});

Update package.json scripts

Update your package.json scripts to work with Radipan:

  "scripts": {
    "cssgen": "npx radipan cssgen",
    "prepare": "npx radipan prepare",
    "dev": "npx radipan prepare --watch & next dev",
    "build": "npx radipan cssgen && tsc && next build",
  }

The prepare script runs codegen after dependency installation and regenerates the output directory.

Configure the entry CSS with layers

Create an entry CSS file or replace the existing one named index.css (or global.css), usually under the src or src/app folder, with this code:

@layer reset, base, tokens, recipes, utilities;

/* Import generated Radipan static CSS */
@import "radipan/styles.css";

When you run npx radipan cssgen, it scans all css props in your app and statically generates the corresponding CSS code at build time.

JSX Configuration for TypeScript (Optional)

Optionally, you can follow the below steps to use Radipan with JSX in TypeScript if you prefer to use JSX syntax. Feel free to skip this step if you don't use JSX.

Configure the compilerOptions in TSConfig (usually tsconfig.json) with the following settings:

"jsx": "react-jsx",
"jsxImportSource": "radipan"

This requires the new JSX transform and the jsxImportSource option, which are available in TS 4.1 or later.

If you are using a UI framework other than React, you also need to change the jsxFramework setting in your radipan.config.ts file to match your framework. For example, if you are using Solid.js, you should set jsxFramework: 'solid'. This ensures that your JSX code is transpiled correctly for your framework.

With this configuration, you can now use the object syntax to define styles and pass them to your components using the css prop!

function App() {
  return (
    <main
      css={{
        width: "100%",
        height: "100vh",
        color: { base: "black", _osDark: "white" },
        background: { base: "white", _osDark: "black" },
      }}
    >
      <div css={{ fontSize: "2xl", fontWeight: "bold" }}>Hello Radip🐼n!</div>
    </main>
  );
}

With the old JSX transform

If you're on an older version of React / TypeScript and unable to use the newer react-jsx transform, you will need to set the jsxFactory TSConfig option to "radipan.h", or specify the JSX factory at the top of every file:

/** @jsx h */
import { h } from "radipan";

Limitations

  • The transpiler uses Babel to handle the css-to-className transformation. It then runs your code at build time, and turns any dynamic references into static values. If you need real dynamic styles, you might need to move any runtime references (e.g., reference of values from hooks) into styles prop.
const Widget = ({ width: number }) => {
  return (
    <div
      // ⚠️ Static: The value of `width`` is turned into static at build-time as per usage
      // Any change to `width` will not update the width during runtime
      css={{ width, color: "red" }}
    >
      Hello Radip🐼n!
    </div>
  );
};
function Widget() {
  const [color, setColor] = useState("red.300");
  return (
    // ⚠️ Static: This will work statically because `red.300` is the value at build time
    // Any change to `color` will not update the color during runtime
    <div css={{ ...divCss, color }}>Hello Radip🐼n!</div>
  );

  const divCss = { fontSize: "2xl", fontWeight: "bold" };
}
function Widget() {
  const [color, setColor] = useState("red.300");
  return (
    // ✅ Good: `color` is in `styles` because it's dynamically updated after user interactions
    <>
      <div css={divCss} styles={{ color }}>
        Hello Radip🐼n!
      </div>
      <button type="button" onClick={() => setColor("green.600")}>
        Change Color
      </button>
    </>
  );

  const divCss = { fontSize: "2xl", fontWeight: "bold" };
}

Troubleshooting

To enable debug logging, run the Radipan CLI with DEBUG=true.

0.0.23

9 months ago

0.0.22

9 months ago

0.0.21

9 months ago

0.0.20

9 months ago

0.0.19-th.3

9 months ago

0.0.19-th.2

9 months ago

0.0.19-th.1

9 months ago

0.0.19-refactor.1

9 months ago

0.0.19

10 months ago

0.0.18-fix.6

10 months ago

0.0.18-fix.5

10 months ago

0.0.18-fix.4

10 months ago

0.0.18-fix.3

10 months ago

0.0.18-fix.2

10 months ago

0.0.18-fix.1

10 months ago

0.0.18

10 months ago

0.0.17

10 months ago

0.0.16-transpiler.1

10 months ago

0.0.16-bpild.1

10 months ago

0.0.16

10 months ago

0.0.15-fix.1

10 months ago

0.0.15

10 months ago

0.0.14-compiler.3

10 months ago

0.0.14-compiler.2

10 months ago

0.0.14-compiler.1

10 months ago

0.0.14-deps.2

10 months ago

0.0.14-deps.1

10 months ago

0.0.14

10 months ago

0.0.13-fix.1

10 months ago

0.0.13

10 months ago

0.0.12-fix.9

10 months ago

0.0.12-fix.8

10 months ago

0.0.12-fix.7

10 months ago

0.0.12-fix.6

10 months ago

0.0.12-fix.5

10 months ago

0.0.12-fix.4

10 months ago

0.0.12-fix.3

10 months ago

0.0.12-fix.2

10 months ago

0.0.12-fix.1

10 months ago

0.0.12

10 months ago

0.0.11-fix.5

10 months ago

0.0.11-fix.4

10 months ago

0.0.11-fix.3

10 months ago

0.0.11-fix.2

10 months ago

0.0.11-fix.1

10 months ago

0.0.11-test.8

10 months ago

0.0.11-test.7

10 months ago

0.0.11-test.6

10 months ago

0.0.11-test.5

10 months ago

0.0.11-test.4

10 months ago

0.0.11-test.3

10 months ago

0.0.11-test.2

10 months ago

0.0.11-test.1

10 months ago

0.0.11

10 months ago

0.0.10-jsx.1

10 months ago

0.0.10-svelte.1

10 months ago

0.0.10

10 months ago

0.0.9-jsx.7

10 months ago

0.0.9-jsx.6

10 months ago

0.0.9-rm.2

10 months ago

0.0.9-rm.1

10 months ago

0.0.9-jsx.5

10 months ago

0.0.9-jsx.4

10 months ago

0.0.9-jsx.3

10 months ago

0.0.9-jsx.2

10 months ago

0.0.9-jsx.1

10 months ago

0.0.9-fix.4

10 months ago

0.0.9-fix.3

10 months ago

0.0.9-fix.2

10 months ago

0.0.9-fix.1

10 months ago

0.0.9

10 months ago

0.0.8-fix.1

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6-fixtypes.3

10 months ago

0.0.6-fixtypes.2

10 months ago

0.0.6-fixtypes.1

10 months ago

0.0.6-jsx.3

10 months ago

0.0.6-jsx.2

10 months ago

0.0.6-jsx.1

10 months ago

0.0.6-next.6

10 months ago

0.0.6-next.5

10 months ago

0.0.6-next.4

10 months ago

0.0.6-next.3

10 months ago

0.0.6-next.1

10 months ago

0.0.6

10 months ago

0.0.5-bin.4

10 months ago

0.0.5-bin.3

10 months ago

0.0.5-bin.2

10 months ago

0.0.5-bin.1

10 months ago

0.0.5-dfoutdir.1

10 months ago

0.0.5-config.2

10 months ago

0.0.5-config.1

10 months ago

0.0.5-extractor.8

10 months ago

0.0.5-extractor.7

10 months ago

0.0.5-extractor.6

10 months ago

0.0.5-extractor.5

10 months ago

0.0.5-extractor.4

10 months ago

0.0.5-extractor.3

10 months ago

0.0.5-extractor.2

10 months ago

0.0.5-extractor.1

10 months ago

0.0.5

10 months ago

0.0.4-mtags.5

10 months ago

0.0.4-mtags.4

10 months ago

0.0.4-mtags.3

10 months ago

0.0.4-mtags.2

10 months ago

0.0.4-debug.1

10 months ago

0.0.4-mtags.1

10 months ago

0.0.4-fix.1

10 months ago

0.0.4

10 months ago

0.0.3-outdir.6

10 months ago

0.0.3-outdir.5

10 months ago

0.0.3-outdir.4

10 months ago

0.0.3-outdir.2

10 months ago

0.0.3-outdir.1

10 months ago

0.0.3

10 months ago

0.0.3-test.moving.2

10 months ago

0.0.3-test.moving.1

10 months ago

0.0.3-minified.1

10 months ago

0.0.3-alpha.3

10 months ago

0.0.3-alpha.2

10 months ago

0.0.3-alpha.1

10 months ago

0.0.2

10 months ago

0.0.1-phy.3

10 months ago

0.0.1-phy.2

10 months ago

0.0.1-phy.1

10 months ago

0.0.1

10 months ago

0.0.1-rc.3

10 months ago

0.0.1-rc.2.test.5

10 months ago

0.0.1-rc.2.test.4

10 months ago

0.0.1-rc.2.test.3

10 months ago

0.0.1-rc.2.test.2

10 months ago

0.0.1-rc.2.test.1

10 months ago

0.0.1-rc.2

10 months ago

0.0.1-rc.1.test

10 months ago

0.0.1-rc.1

10 months ago

0.0.1-beta.36

10 months ago

0.0.1-beta.35

10 months ago

0.0.1-beta.34

10 months ago

0.0.1-beta.33

10 months ago

0.0.1-beta.32

10 months ago

0.0.1-beta.31

10 months ago

0.0.1-beta.30

10 months ago

0.0.1-beta.29

10 months ago

0.0.1-beta.28

10 months ago

0.0.1-beta.27

10 months ago

0.0.1-beta.26

10 months ago

0.0.1-beta.25

10 months ago

0.0.1-beta.24

10 months ago

0.0.1-beta.23

10 months ago

0.0.1-beta.22

10 months ago

0.0.1-beta.21

10 months ago

0.0.1-beta.20

10 months ago

0.0.1-beta.19

10 months ago

0.0.1-beta.18

10 months ago

0.0.1-beta.17

10 months ago

0.0.1-beta.16

10 months ago

0.0.1-beta.15

10 months ago

0.0.1-beta.13

10 months ago

0.0.1-beta.12

10 months ago

0.0.1-beta.11

10 months ago

0.0.1-beta.10

10 months ago

0.0.1-beta.9

10 months ago

0.0.1-beta.8

10 months ago

0.0.1-beta.7

10 months ago

0.0.1-beta.6

10 months ago

0.0.1-beta.5

10 months ago

0.0.1-beta.4

10 months ago

0.0.1-beta.3

10 months ago

0.0.1-beta.2

10 months ago

0.0.1-beta.1

10 months ago

0.0.1-alpha.17

10 months ago

0.0.1-alpha.16

10 months ago

0.0.1-alpha.15

10 months ago

0.0.1-alpha.14

10 months ago

0.0.1-alpha.13

10 months ago

0.0.1-alpha.12

10 months ago

0.0.1-alpha.11

10 months ago

0.0.1-alpha.10

10 months ago

0.0.1-alpha.9

10 months ago

0.0.1-alpha.8

10 months ago

0.0.1-alpha.7

10 months ago

0.0.1-alpha.6

10 months ago

0.0.1-alpha.5

10 months ago

0.0.1-alpha.4

10 months ago

0.0.1-alpha.3

10 months ago

0.0.1-alpha.2

10 months ago

0.0.1-alpha.1

10 months ago

0.0.1-alpha

10 months ago