Tooltip Menu Spell
TheTooltipMenuSpell
component creates a contextual menu that automatically appears when text is selected, perfect for text editing, AI-powered transformations, and quick actions on selected content. It can also spawn floating input fields for more complex interactions.
Features
- Automatic Text Selection Detection: Appears when text is selected
- Smart Positioning: Positions above selection, adjusts to stay on screen
- Floating Input Support: Can spawn input fields for complex queries
- AI Integration: Direct access to Cedar store for AI operations
- Selection Preservation: Maintains text selection during interactions
- Flexible Actions: Support for immediate actions or input-based workflows
Installation
Basic Usage
Props
TooltipMenuSpell Props
Prop | Type | Required | Default | Description |
---|---|---|---|---|
spellId | string | Yes | - | Unique identifier for this spell instance |
items | ExtendedTooltipMenuItem[] | Yes | - | Menu items to display |
activationConditions | ActivationConditions | No | Text selection | Custom activation conditions |
stream | boolean | No | true | Whether to use streaming for floating input |
ExtendedTooltipMenuItem Interface
Advanced Examples
AI Writing Assistant
Create a comprehensive writing assistant:Code Editor Actions
Add code-specific actions:Research Assistant
Help with research and fact-checking:Dynamic Menu Items
Generate menu items based on context:Floating Input Integration
When an item hasspawnsInput: true
, it opens a floating input field:
How It Works
- User selects text
- Tooltip menu appears
- User clicks item with
spawnsInput: true
- Menu disappears, floating input appears
- Selected text is automatically added to input
- User can modify or add to the query
- On submit, message is sent to Cedar store
Example with Floating Input
Positioning Behavior
The tooltip menu intelligently positions itself:- Default Position: Above the selected text, centered
- Edge Detection: Adjusts if too close to viewport edges
- Padding: Maintains 10px minimum distance from edges
- Menu Dimensions: Calculates based on item count (48px per item)
Customization
Custom Activation
While text selection is the default, you can customize activation:Disable Streaming
For non-AI actions, disable streaming:Best Practices
1. Logical Item Order
Arrange items by frequency of use:2. Clear Icons
Use recognizable icons that match the action:3. Preserve Selection
Don’t clear selection unnecessarily:4. Handle Edge Cases
Always check for valid selection:Technical Details
Selection Management
The component maintains selection state using refs:Event Handling
- Text Selection: Detected via
SelectionEvent.TEXT_SELECT
- Position Calculation: Based on selection bounding rect
- Menu Dismissal: Click outside or press ESC
- Selection Preservation: Maintained until action completes
Performance
- Debounced Selection: Prevents excessive re-renders
- Conditional Rendering: Only renders when active
- Ref-based State: Avoids unnecessary re-renders
- Lazy Input Creation: Floating input created on demand
Accessibility
Keyboard Support
- Text selection via keyboard works normally
- Menu items should be keyboard navigable
- ESC key closes menu and floating input
Screen Reader Considerations
Troubleshooting
Menu not appearing
- Verify text selection is working
- Check that spell ID is unique
- Ensure component is mounted
- Verify no CSS
user-select: none
on text
Position issues
- Check for CSS transforms on parent elements
- Verify viewport calculations
- Ensure menu has proper z-index
Floating input issues
- Verify
spawnsInput
is set correctly - Check Cedar store is accessible
- Ensure
setOverrideInputContent
is available
Selection lost
- Check for conflicting event handlers
- Verify
preventDefaultEvents
setting - Ensure no other components clear selection
Related Components
- RadialMenuSpell - Circular menu for general actions
- QuestioningSpell - Interactive cursor for exploration
- Creating Custom Spells - Build your own spell components