Panda CSS

repository·main·Indexed 11 days ago

https://github.com/chakra-ui/panda

A universal, build-time, type-safe CSS-in-JS styling solution for the modern web. It provides scalable CSS output using cascade layers and variables, featuring a developer experience with recipes and variants. Includes a CLI (@pandacss/dev) for build management, a Model Context Protocol (MCP) server for AI assistants, and a generator for design system artifacts.

Tokens
203.6K
Snippets
760
Records
894
Agent score
88%

What's inside Panda CSS

  1. Get started with Panda CSS

    main

    Panda is a styling engine that generates styling primitives to author atomic CSS and recipes in a type-safe and readable manner. It combines the developer experience of CSS-in-JS with the performance of atomic CSS by using static analysis to scan JavaScript and TypeScript files for JSX style props and function calls, generating styles on-demand (Just-in-Time).

    TLDR: Panda is a CSS-in-JS engine that generates atomic CSS at build time via the CLI or PostCSS.

  2. Understand Panda CSS browser support requirements

    main

    Panda CSS is designed for modern browsers and relies on native support for the following CSS features:

    • CSS Variables
    • CSS Cascade Layers
    • Modern selectors (e.g., :where() and :is())

    Panda targets the latest stable releases of major browsers. The default supported browser range is defined by the following browserslist criteria:

    >= 1%
    last 1 major version
    not dead
    Chrome >= 99
    Edge >= 99
    Firefox >= 97
    iOS >= 15.4
    Safari >= 15.4
    Android >= 115
    Opera >= 73
  3. Predefined Layout Patterns

    main

    Panda CSS provides several built-in patterns for common layout tasks. These include:

    • Stacking: stack, hstack (horizontal stack), vstack (vertical stack).
    • Wrapping: wrap.
    • Layout Primitives: box, container, aspectRatio, flex, center, linkOverlay, float, and grid.

    For instructions on how to build your own, refer to the customization documentation.

  4. What is Panda CSS and its core philosophy?

    main

    Panda CSS is a universal, build-time, type-safe CSS-in-JS solution. Unlike runtime CSS-in-JS libraries, Panda extracts styles during the build process to generate optimized CSS and TypeScript utilities, resulting in zero runtime overhead in the browser.

    Core Principles:

    • Build-time extraction: Styles are analyzed at compile time.
    • Type safety: Provides full TypeScript support with auto-generated types based on your configuration.
    • Zero runtime: Minimal JavaScript is shipped to the browser as CSS is pre-generated.
    • Framework agnostic: Compatible with React, Vue, Svelte, Solid, Preact, Qwik, and more.
    • Modern CSS: Uses CSS custom properties and cascade layers (@layer).
  5. What is Panda Studio

    main

    Panda Studio is a visual, read-only interface designed for exploring and documenting your design system. It automatically visualizes your design system elements, including:

    • Tokens
    • Semantic tokens
    • Recipes
    • Patterns
    • Conditions

    It serves as an alternative to manually generating spec files or building a custom documentation website. Unlike manual documentation workflows, Panda Studio does not require you to run pnpm panda spec and acts as a fully-featured documentation tool without requiring additional code.

  6. What is the styled-system and how does it work?

    main

    The styled-system folder is a generated, lightweight runtime that transforms CSS-in-JS syntax (such as css() calls or template literals) into class name strings.

    Because Panda performs static extraction at build-time rather than using a bundler plugin (like Vite or Webpack) to transform code during compilation, this runtime is necessary to map your style objects to the actual CSS classes generated by Panda.

    Key characteristics:

    • Generated Artifact: It is created in the directory specified by your config.outdir when running panda or panda codegen.
    • Tailored Runtime: It is customized based on your specific configuration (tokens, patterns, recipes, etc.), containing only the code and types necessary for your app.
    • Do Not Edit: The folder is a build artifact and should not be edited manually. It should typically be excluded from your version control (git).
    • Zero Runtime for Users: In pre-rendering environments (like Astro or React Server Components), the styled-system functions are replaced by static class names at build-time, ensuring the runtime is not shipped to the client.
    // Example of the runtime transforming a style object to a class name
    css({ color: 'blue.300' }) // => "text_blue_300"
  7. Implement responsive and conditional variants

    main

    You can pass breakpoint objects to variant props (e.g., size: { base: 'sm', md: 'lg' }) to create responsive styles. This uses ConditionalValue types generated by Panda.

    Requirements for Responsive Variants:

    1. The recipe must be defined using defineRecipe in the config (not cva).
    2. The recipe must not have compoundVariants defined.
    import { button } from '../styled-system/recipes'
    
    // ✅ This works if no compoundVariants are present
    <button className={button({ size: { base: 'sm', md: 'lg' } })}>Click me</button>
  8. How Panda manages global styles with layers

    main

    Panda organizes global styles into two distinct CSS cascade layers to ensure predictable defaults and safe overrides:

    1. @layer reset: Contains preflight/reset styles. This layer is enabled via the preflight configuration option.
    2. @layer base: Contains your custom global styles defined via the globalCss configuration option.

    By using these layers, Panda ensures that global styles have lower specificity than utility classes or component-specific styles, preventing unexpected overrides.

  9. Use tokens in composite values and at-rules

    main

    For CSS properties that allow multiple values (like border or box-shadow) or within at-rules (like @media queries), Panda provides two syntaxes to reference tokens:

    1. token() function syntax: Useful when you need to provide a fallback value. Syntax: token(path, fallback).
    2. Reference syntax: A more concise syntax for when a fallback is not required. Syntax: {path}.

    Examples

    Composite values (e.g., border):

    • Using token(): border: '1px solid token(colors.red.400)' (or with fallback: token(colors.red.400, red))
    • Using reference syntax: border: '1px solid {colors.red.400}'

    At-rules (e.g., media queries):

    • Using token(): '@media screen and (min-width: token(sizes.4xl))'
    • Using reference syntax: '@media screen and (min-width: {sizes.4xl})'
    // token() syntax with fallback
    const className = css({ border: '1px solid token(colors.red.400, red)' })
    
    // Reference syntax
    const className = css({ border: '1px solid {colors.red.400}' })
    
    // Reference syntax in media queries
    const className = css({
      '@media screen and (min-width: {sizes.4xl})': {
        color: 'green.400'
      }
    })
  10. Understand the styled-system directory structure

    main

    The styled-system folder contains the entrypoints for the Panda runtime and the generated CSS. The structure typically includes:

    • css/: Functions for generating class names from style objects.
    • jsx/: JSX components for styled elements.
    • recipes/: Runtime support for recipes.
    • patterns/: Runtime support for patterns.
    • tokens/: Generated token definitions.
    • types/: TypeScript definitions for your specific design system.
    • styles.css: The actual extracted CSS used by your application.
  11. Create flex containers with Flex

    main

    The Flex pattern provides shortcuts for flexbox properties.

    Properties:

    • direction: row, column, row-reverse, or column-reverse.
    • wrap: Boolean to enable wrapping.
    • align: Alias for align-items.
    • justify: Alias for justify-content.
    • basis: Alias for flex-basis.
    • grow: Alias for flex-grow.
    • shrink: Alias for flex-shrink.
    import { Flex } from '../styled-system/jsx'
    
    function App() {
      return (
        <Flex direction="row" align="center">
          <div>First</div>
          <div>Second</div>
        </Flex>
      )
    }