--- name: panda-component-impl description: Build React components that properly use Panda CSS patterns, recipes, TypeScript integration, and accessibility best practices --- # Panda CSS Component Implementation ## When to Use This Skill Use this skill when: - Building React components with Panda CSS styling - Implementing recipe-based component variants - Creating polymorphic components (components that can render as different elements) - Integrating TypeScript with Panda CSS types - Implementing accessible components with Panda CSS - Setting up component file structure For creating the recipes themselves, use the **panda-recipe-patterns** skill first. ## Component File Structure ``` src/ components/ Button/ Button.tsx # Component implementation index.tsx # Public exports Button.stories.tsx # Storybook documentation (optional) Icon/ Icon.tsx index.tsx svg/ # SVG source files (for icon systems) CheckBox/ CheckBox.tsx index.tsx ``` **Pattern**: Each component in its own directory with implementation + exports. ## Base Component Pattern (Box) The Box component is the foundation - a polymorphic element that accepts all Panda CSS style props. Create: `src/components/Box/Box.tsx` ```typescript import { createElement } from 'react' import type { ComponentPropsWithoutRef, ElementType, ReactNode } from 'react' import { cx } from '@styled-system/css' import { box } from '@styled-system/patterns' import type { SystemStyleObject } from '@styled-system/types' import { splitProps } from '~/utils/splitProps' // Box can render as any HTML element export type BoxProps = Omit, 'as'> & SystemStyleObject & // Enable all Panda CSS style props { as?: ElementType children?: ReactNode } export const Box = ({ as = 'div', ...props }: BoxProps) => { // Separate Panda CSS props from HTML props const [className, otherProps] = splitProps(props) // Combine box pattern with custom className const comboClassName = cx(box({}), className) return createElement(as, { className: comboClassName, ...otherProps }) } ``` **Key Points**: - `as` prop: Polymorphic rendering (div, button, a, span, etc.) - `SystemStyleObject`: Enables all Panda CSS style props (bg, px, fontSize, etc.) - `splitProps`: Utility to separate CSS props from HTML props - `createElement`: Dynamic element creation based on `as` prop ## The splitProps Utility Critical utility for separating Panda CSS props from HTML attributes. Create: `src/utils/splitProps.ts` ```typescript import { cx, css, splitCssProps } from '@styled-system/css' /** * Splits component props into Panda CSS props and HTML props. * Returns [className, otherProps]. */ export const splitProps = ( props: Record ): [string, Record] => { // Panda's utility: splits CSS props from other props const [cssProps, otherProps] = splitCssProps(props) // Extract css prop separately const { css: cssProp, ...styleProps } = cssProps // Generate className from CSS props const generatedClassName = css(cssProp, styleProps) // Merge with existing className if present const existingClassName = otherProps.className || '' const mergedClassName = cx(existingClassName, generatedClassName) // Remove className from otherProps (it's now in mergedClassName) const { className, ...remainingProps } = otherProps return [mergedClassName, remainingProps] } ``` **Why**: Enables inline style props on components while keeping clean HTML output. **Usage**: ```typescript // Component receives both CSS and HTML props ``` ### Avoid: Over-wrapping ```typescript // BAD: Unnecessary div wrappers Content // GOOD: Minimal, semantic structure Content ``` ## Accessing Official Panda CSS Docs For component implementation patterns: 1. **Resolve library ID**: `mcp__MCP_DOCKER__resolve-library-id` with `libraryName: "panda-css"` 2. **Fetch docs**: `mcp__MCP_DOCKER__get-library-docs` with: - `topic: "recipes"` - Using recipes in components - `topic: "typescript"` - TypeScript patterns - `topic: "patterns"` - Built-in patterns ## Exporting Components Create: `src/components/Button/index.tsx` ```typescript export { Button } from './Button' export type { ButtonProps } from './Button' ``` Create: `src/index.ts` (main library export) ```typescript // Components export { Box } from './components/Box' export { Button, IconButton } from './components/Button' export { Icon } from './components/Icon' export { CheckBox } from './components/CheckBox' // ... more components // Types export type { BoxProps } from './components/Box' export type { ButtonProps, IconButtonProps } from './components/Button' export type { IconProps } from './components/Icon' // ... more types ``` **Pattern**: Export both components and their prop types for consuming projects. ## Working Examples Reference these files in the `examples/` directory for production-tested patterns: **Utility Functions:** - `examples/utils/splitProps.ts` - CSS/HTML prop separation utility ```typescript // Separates Panda CSS props from HTML props const [className, htmlProps] = splitProps(props); // Uses splitCssProps, css(), and cx() to merge classNames ``` - `examples/utils/ThemeContext.tsx` - Theme provider with localStorage persistence ```typescript // Manages light/dark theme with system preference detection // Persists user preference to localStorage // Applies theme class to document root ``` **Token & Configuration Integration:** - `examples/preset.ts` - Shows how to integrate tokens with components via preset - `examples/conditions.ts` - Custom conditions for component states - `examples/textStyles.ts` - Typography presets for text components **For Complete Component Examples:** While this plugin focuses on architecture patterns, you can reference: - The skills themselves (this file, panda-recipe-patterns) contain inline component examples - Use the **panda-architect** agent for full component implementations