Feature Plugins
Feature plugins provide a user-friendly facade for enabling optional features that extend Stately's functionality. They wrap extension points with a simple .enable() API and handle lazy loading of heavy dependencies.
When to Use Feature Plugins
Use feature plugins when you want to:
- Provide optional functionality that users explicitly opt into
- Lazy load heavy dependencies (like CodeMirror, Monaco, etc.)
- Expose a simple API:
myFeature.enable() - Build on top of extension points
Creating a Feature Plugin
Use createFeaturePlugin from @statelyjs/ui/feature-plugin:
Configuration Options
id
Unique identifier for the plugin:
component
Dynamic import for lazy loading. Only loaded when the plugin is enabled and rendered:
defaults
Default options applied when .enable() is called:
setup
Called once when .enable() is invoked. Use this to register extensions:
defaultExtras
Additional properties to expose on the plugin object. Combined with the return value from setup:
User API
Enabling the Plugin
Users call .enable() at application startup:
Checking Status
Accessing the Component
Using FeatureComponent Helper
For convenience, use the FeatureComponent wrapper:
Accessing Options and Extras
Real-World Example: CodeMirror
The codemirror feature plugin in @statelyjs/stately/features provides syntax-highlighted code editing:
Plugin Definition
User Usage
Returning Extras from Setup
The setup function can return an object that gets merged into plugin.extras:
Users access extras via:
Best Practices
- Keep heavy deps lazy: Use the
componentoption to defer loading - Provide good defaults: Users should be able to call
.enable()with no arguments - Document peer dependencies: If your plugin requires packages users must install, document them
- Use idempotent setup:
.enable()can be called multiple times safely (only first call runs setup) - Build on extensions: Feature plugins are most powerful when they extend existing extension points
Relationship to Extensions
Feature plugins and extensions work together:
See Also
- Extensions - Lower-level extension point API
- Plugin Development - Full plugin development guide
- Available Features - User guide to built-in features