Why Custom Components?
Custom components are useful when you need to:- Create specialized visualizations not available in the built-in library
- Integrate third-party JavaScript libraries
- Build reusable UI patterns specific to your organization
- Implement custom business logic or calculations
- Add advanced interactivity beyond standard inputs
Creating a Custom Component
Custom components are Svelte files (.svelte) placed in your project’s components directory.
Project Structure
Basic Component Example
Createcomponents/MetricCard.svelte:
Using the Component
In your markdown pages:Usage
Using the Library
Interactive Components
Create components with internal state and interactions:Using Slots
Slots allow you to pass content into components:Usage with Slots
Component Props Validation
Validate and provide defaults for props:Accessing Evidence Context
Evidence provides context that components can access:Exporting Components as a Package
Create reusable component libraries:Package Structure
package.json
src/index.js
Best Practices
- Props over hardcoding - Make components flexible with props
- Provide defaults - Set sensible default values for optional props
- Validate inputs - Check for required props and valid values
- Use TypeScript - Add type safety with TypeScript
- Document components - Include JSDoc comments for props
- Keep it simple - Start simple, add complexity as needed
- Responsive design - Make components work on all screen sizes
- Accessibility - Follow ARIA guidelines for interactive components
TypeScript Support
Use TypeScript for type-safe components:Debugging Components
Use Svelte’s reactive debugging:Example: Custom Chart Component
Complete example of a custom chart component:Next Steps
Svelte Documentation
Learn more about Svelte
Component Examples
Browse built-in components