Web Components
Knapsack for Web Components works by scanning your specified Web Components 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
- Custom Elements - Components must use the Web Components API
- Asset Sets - Required for loading web components in Knapsack
- Custom Element Manifest - Optional, but enables automatic spec inference (props, slots, events)
The Web Components renderer supports components created using Custom Elements and the Web Components API. You must install the underlying Web Components packages separately.
Install
Configure
Minimal Setup
The simplest configuration requires specifying where your web components are located:
Full Configuration Example
Here’s a comprehensive example showing all available options:
To enable spec inference for web components, you must first generate a custom element manifest for your web components and then add the path to the KnapsackWebComponentRenderer config. This can be a package reference or a file path relative to the config file.
Code Sources
The codeSrcs array tells Knapsack where to find your web components. The renderer will scan these paths and make them available in the Knapsack UI for documentation and demos.
Local Components
External Libraries
Custom Element Manifest
To enable enhanced spec inference for web components, you can provide a custom element manifest. This is optional but provides better prop and slot information.
Generating the Manifest
Knapsack uses @custom-elements-manifest/analyzer to generate custom element manifests. See the @custom-elements-manifest/analyzer documentation for installation and usage instructions.
You can also add this to your package.json scripts for automated builds:
The custom element manifest file generation can be set up as part of your build process so that it runs automatically when you build your components or manually after adding or removing new props, slots, or other component features.
Learn more about custom element manifests.
Asset Sets
Web components require asset sets to work properly in Knapsack. Asset sets tell Knapsack how to load and render your web components.
Configure Asset Sets
Update the asset set configuration in knapsack/data/knapsack.asset-sets.json to include your custom elements:
Rebuild
After configuring your renderer and asset sets, rebuild your Knapsack configuration to ensure the web components are properly rendered.