|
| 1 | +--- |
| 2 | +title: Panda CSS |
| 3 | +category: Guides / Styling Components |
| 4 | +order: 5 |
| 5 | +mainNavExclude: true |
| 6 | +use_cases: >- |
| 7 | + styling components, css-in-js, build-time styles, design tokens, type-safe |
| 8 | + styles |
| 9 | +tags: |
| 10 | + - styling |
| 11 | + - css |
| 12 | + - panda |
| 13 | + - css-in-js |
| 14 | + - postcss |
| 15 | + - tokens |
| 16 | +version: "1.0" |
| 17 | +description: >- |
| 18 | + Set up Panda CSS in your Solid app for build-time CSS-in-JS. Configure |
| 19 | + PostCSS, define design tokens, and write type-safe styles. |
| 20 | +--- |
| 21 | + |
| 22 | +[Panda CSS](https://panda-css.com/) is a build-time CSS-in-JS library that combines the developer experience of CSS-in-JS with the performance of atomic CSS. |
| 23 | +Styles are extracted at build time through PostCSS, so no runtime styling code ships to the browser. |
| 24 | + |
| 25 | +## Installation |
| 26 | + |
| 27 | +1. Install Panda CSS as a development dependency: |
| 28 | + |
| 29 | +```package-install-dev |
| 30 | +@pandacss/dev |
| 31 | +``` |
| 32 | + |
| 33 | +2. Initialize Panda CSS in your project. |
| 34 | + This creates a `panda.config.ts` file and a `postcss.config.cjs` file: |
| 35 | + |
| 36 | +```package-exec |
| 37 | +panda init --postcss |
| 38 | +``` |
| 39 | + |
| 40 | +3. Add the `prepare` script to your `package.json`, which generates the `styled-system` directory after installs: |
| 41 | + |
| 42 | +```json title="package.json" |
| 43 | +{ |
| 44 | + "scripts": { |
| 45 | + "prepare": "panda codegen", |
| 46 | + "dev": "vite", |
| 47 | + "build": "vite build" |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## Configure Panda CSS |
| 53 | + |
| 54 | +Open `panda.config.ts` and make sure the `include` patterns match your source files. |
| 55 | +Setting `jsxFramework` to `"solid"` enables Solid-specific JSX style props support: |
| 56 | + |
| 57 | +```ts title="panda.config.ts" |
| 58 | +import { defineConfig } from "@pandacss/dev"; |
| 59 | + |
| 60 | +export default defineConfig({ |
| 61 | + preflight: true, |
| 62 | + include: ["./src/**/*.{js,jsx,ts,tsx}"], |
| 63 | + exclude: [], |
| 64 | + jsxFramework: "solid", |
| 65 | + outdir: "styled-system", |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +## Import the cascade layers |
| 70 | + |
| 71 | +Add the Panda CSS cascade layers to your `src/index.css` file: |
| 72 | + |
| 73 | +```css title="src/index.css" |
| 74 | +@layer reset, base, tokens, recipes, utilities; |
| 75 | +``` |
| 76 | + |
| 77 | +Then import your `index.css` file into the root `index.jsx` or `index.tsx` file: |
| 78 | + |
| 79 | +```jsx |
| 80 | +/* @refresh reload */ |
| 81 | +import { render } from "solid-js/web" |
| 82 | +import "./index.css" |
| 83 | +import App from "./App" |
| 84 | + |
| 85 | +render(() => <App />, document.getElementById('root') as HTMLElement); |
| 86 | +``` |
| 87 | + |
| 88 | +## Usage |
| 89 | + |
| 90 | +With Panda CSS set up, use the `css` function from the generated `styled-system` directory to style your components: |
| 91 | + |
| 92 | +```jsx |
| 93 | +/* src/App.jsx */ |
| 94 | +import { css } from "../styled-system/css"; |
| 95 | + |
| 96 | +function App() { |
| 97 | + return ( |
| 98 | + <div class={css({ fontSize: "2xl", fontWeight: "bold" })}> |
| 99 | + Hello from Panda! 🐼 |
| 100 | + </div> |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +export default App; |
| 105 | +``` |
| 106 | + |
| 107 | +## Support |
| 108 | + |
| 109 | +For additional assistance, refer to the [Panda CSS installation guide](https://panda-css.com/docs/installation/vite). |
0 commit comments