Skip to main content

Tooltip Menu Spell

The TooltipMenuSpell 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

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 has spawnsInput: true, it opens a floating input field:

How It Works

  1. User selects text
  2. Tooltip menu appears
  3. User clicks item with spawnsInput: true
  4. Menu disappears, floating input appears
  5. Selected text is automatically added to input
  6. User can modify or add to the query
  7. On submit, message is sent to Cedar store

Example with Floating Input

Positioning Behavior

The tooltip menu intelligently positions itself:
  1. Default Position: Above the selected text, centered
  2. Edge Detection: Adjusts if too close to viewport edges
  3. Padding: Maintains 10px minimum distance from edges
  4. 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

  • 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