React
Knapsack for React works by scanning your specified React code locations and automatically discovering reusable components, making them instantly available in your design system.
Why This Approach:
- ⚡ Zero Manual Work - Knapsack finds and configures your components automatically
- 🔄 Always Current - Your documentation stays in sync with code changes
- 🎯 Intelligent - Understands different export patterns and component structures
- 🚀 Scale Effortlessly - Handle hundreds of components without individual setup
Requirements
- React 18 or 19
- Knapsack 4.89.4+
Knapsack uses your React, not its own. react and react-dom go in dependencies.
Install
Configure
Minimal Setup The simplest configuration requires just specifying where your React components are located:
Full Configuration Example Here’s a comprehensive example showing all available options:
Code Sources
The codeSrcs array tells Knapsack where to find your React components and how to process them. The renderer will scan these paths, find React component exports, and make them available in the Knapsack UI for documentation and demos.
Local Components
Paths are relative to knapsack.config.js
External Libraries
Ensure that any external library is included in your package dependencies
Smart Filtering
Use filters to control exactly which components are discovered:
Demo Wrapper
A demo wrapper is a React component that wraps around each of your components when they’re rendered in Knapsack. This is essential for providing context like theme providers, CSS resets, or global styles.
Additionally, the props contain useful information beyond just props.children for your custom usage: props.pattern, props.template, props.demo, and props.patternsUsed. See the TypeScript definition for more details.
Setup
1. Create a Wrapper: Create a file (e.g., src/demo-wrapper.tsx):
2. Add your Wrapper Path Reference your wrapper in the renderer configuration:
Common Examples
React Strict Mode
By default, react strict mode is enabled. If you need to disable React Strict mode for your components, you can set this option to true in the configuration:
Webpack Configuration
The React renderer uses Webpack under the hood to process your components. You can provide custom webpack configuration to handle React-specific requirements like babel-loader, JSX transformation, and other build tools.
These webpack configurations are your responsibility to implement correctly. Knapsack provides the integration but you handle the webpack setup.
Common Use Cases
You might need to configure webpack for:
- Path aliases - Create import shortcuts like
@/components/Button - CSS processing - Handle CSS modules, SCSS, styled-components, etc.
- Asset handling - Process images, fonts, and other static files
- TypeScript configuration - Set up TypeScript compilation and path mapping
- Third-party library compatibility - Configure loaders for specific libraries
Common Examples
Path Aliases
CSS Modules
SCSS Support
Asset Handling
TypeScript Path Mapping