Creating Custom Spells
Spells in Cedar-OS are created using theuseSpell
hook, which provides a declarative API for binding gestures to behaviors. This guide will walk you through creating custom spells from simple to advanced.
Basic Spell Structure
Every spell requires three core elements:The useSpell Hook
TheuseSpell
hook is your primary interface for creating spells:
Parameters
Return Values
Activation Conditions
Activation conditions define how users trigger your spell:Events
Spells can respond to multiple event types:Activation Modes
Control how your spell’s lifecycle works:Complete Examples
Example 1: Command Palette Spell
A command palette that appears withCmd+K
:
Example 2: Context Menu Spell
A context menu that appears on right-click:Example 3: AI Assistant Spell
A spell that integrates with Cedar’s AI capabilities:Advanced Patterns
Combining Multiple Spells
You can compose multiple spells for complex interactions:Dynamic Activation Conditions
Activation conditions can be changed dynamically:Accessing Cedar Store
Spells can interact with the Cedar store for AI operations:Best Practices
1. Use Descriptive IDs
2. Handle Cleanup
3. Prevent Conflicts
4. Provide Visual Feedback
5. Consider Accessibility
API Reference
Available Hotkeys
All single keys (A-Z, 0-9) plus:- Function keys:
F1
-F12
- Special keys:
ESCAPE
,ENTER
,SPACE
,TAB
,DELETE
,BACKSPACE
- Arrow keys:
ARROW_UP
,ARROW_DOWN
,ARROW_LEFT
,ARROW_RIGHT
- Modifiers:
CTRL
,CMD
,META
,ALT
,SHIFT
Mouse Events
RIGHT_CLICK
- Right mouse buttonDOUBLE_CLICK
- Double left clickMIDDLE_CLICK
- Middle mouse buttonSHIFT_CLICK
- Shift + left clickCTRL_CLICK
- Ctrl + left clickCMD_CLICK
- Cmd + left click (Mac)ALT_CLICK
- Alt + left click
Selection Events
TEXT_SELECT
- Text selection in document
Next Steps
- Explore the Spell Architecture to understand the internals
- Check out the Radial Menu implementation for a complex example
- Visit the Cedar Playground to see spells in action