Skip to content

Want to skip the docs? Check out pandamastery.com - the best way to learn Panda CSS

Frameworks

Using Preact

Easily use Panda with Preact with our dedicated integration.

This guide shows you how to set up Panda CSS in a Preact project using the @pandacss/vite plugin.

Prefer PostCSS? Run panda init --postcss instead of the plugin step below and follow Using PostCSS. Everything else on this page stays the same.

Start a new project

Create Vite project

To get started, we will need to create a new Preact project using typescript template.

npx preact-cli create typescript test-app
cd test-app

Install Panda

Install panda, the Vite plugin, and create your panda.config.ts file.

npm install -D @pandacss/dev @pandacss/vite
npx panda init

Add the Vite plugin

Add pandacss() to your Vite plugins. It generates the styled-system folder, injects the CSS, and keeps both up to date on save. No separate watcher needed.

vite.config.ts

import { defineConfig } from 'vite'
import pandacss from '@pandacss/vite'
import preact from '@preact/preset-vite'
 
export default defineConfig({
  plugins: [pandacss(), preact()]
})

Update package.json scripts

Open your package.json file and update the scripts section as follows:

package.json

{
  "scripts": {
+    "prepare": "panda codegen",
    "build": "cross-env NODE_OPTIONS=--openssl-legacy-provider preact build",
    "serve": "sirv build --cors --single",
    "dev": "cross-env NODE_OPTIONS=--openssl-legacy-provider preact watch",
    "lint": "eslint src",
    "test": "jest"
  }
}

The plugin only runs with Vite. The "prepare" script covers type checking and your editor, which read styled-system before Vite starts, and lets the folder stay gitignored.

Configure the content

Make sure that all of the paths of your Preact components are included in the include section of the panda.config.ts file.

panda.config.ts

import { defineConfig } from '@pandacss/dev'
 
export default defineConfig({
  preflight: true,
  // Where to look for your css declarations
  include: ['./src/**/*.{js,jsx,ts,tsx}', './pages/**/*.{js,jsx,ts,tsx}'],
  exclude: [],
  outdir: 'styled-system'
})

Configure CSS layers

Add this code to an src/style/index.css file imported in the root component of your project.

src/style/index.css

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

This file only sets the layer order. The plugin injects the generated CSS for you.

Start your build process

Run the following command to start your development server.

npm run dev

Start using Panda

Now you can start using Panda CSS in your project. Here is the snippet of code that you can use in your src/routes/home/index.tsx file.

src/routes/home/index.tsx

import { h } from 'preact'
import { css } from '../../../styled-system/css'
 
const Home = () => {
  return <div class={css({ fontSize: '2xl', fontWeight: 'bold', pt: '56px' })}>Hello 🐼!</div>
}
 
export default Home

Optional: drop the styling runtime

By default css() resolves class strings in the browser with a small runtime. Turn on source transforms and the plugin rewrites static calls to plain class strings at build time, so the runtime never ships:

vite.config.ts

plugins: [pandacss({ transform: true }), preact()]

Dynamic values keep their runtime form. See Source transforms.

Troubleshooting

If you're not getting import autocomplete in your IDE, you may need to include the styled-system directory in your tsconfig.json file:

tsconfig.json

{
  // ...
  "include": ["src", "styled-system"]
}
Edit this page on GitHubView as markdown
Last updated on