variable-picker-plugin.tsx


Overview

variable-picker-plugin.tsx is a React component plugin designed for the Lexical text editor framework. It provides a typeahead menu that allows users to insert variable nodes into the editor content by typing a trigger character (/). This plugin supports searching and selecting variables from a structured list and replaces matched text patterns with special variable nodes inside the editor.

Key features:


Classes

VariableInnerOption (extends MenuOption)

Represents an individual variable option within a grouped menu.

Properties

Constructor

constructor(label: string, value: string)

Creates an option representing a selectable variable.


VariableOption (extends MenuOption)

Represents a grouped menu option containing multiple VariableInnerOption entries.

Properties

Constructor

constructor(
  label: ReactElement | string,
  title: string,
  options: VariableInnerOption[],
)

Components & Functions

VariablePickerMenuItem

A React component that renders a single grouped variable menu item with its inner options.

Props

{
  index: number;
  option: VariableOption;
  selectOptionAndCleanUp: (option: VariableOption | VariableInnerOption) => void;
}

Usage Example

Renders a group label and a list of clickable variable items:

<VariablePickerMenuItem
  index={0}
  option={someVariableOption}
  selectOptionAndCleanUp={handleSelect}
/>

VariablePickerMenuPlugin

The main exported React component that integrates with Lexical's LexicalTypeaheadMenuPlugin to provide the variable picker functionality.

Props

{
  value?: string;
}

Functionality

Usage Example

<VariablePickerMenuPlugin value="{userName} logged in" />

This would initialize the editor content by replacing {userName} with the corresponding variable node if found.


Important Implementation Details

Trigger and Query Matching

Option Filtering

Variable Node Insertion

Parsing Text to Variable Nodes

React Portals & Menu Rendering


Interaction with Other System Parts


Mermaid Component Diagram

componentDiagram
    component VariablePickerMenuPlugin {
        +value?: string
        +onQueryChange(query: string)
        +onSelectOption(option, node, closeMenu)
        +parseTextToVariableNodes(text)
    }
    component VariablePickerMenuItem {
        +index: number
        +option: VariableOption
        +selectOptionAndCleanUp(option)
    }
    component VariableOption {
        +label: ReactElement | string
        +title: string
        +options: VariableInnerOption[]
    }
    component VariableInnerOption {
        +label: string
        +value: string
    }
    VariablePickerMenuPlugin --> VariableOption : uses
    VariableOption *-- VariableInnerOption : contains
    VariablePickerMenuPlugin --> VariablePickerMenuItem : renders
    VariablePickerMenuItem --> VariableInnerOption : renders items

Summary

The variable-picker-plugin.tsx file implements a powerful and flexible variable insertion UI for the Lexical text editor using React. It leverages Lexical’s plugin infrastructure to provide an intuitive slash-triggered typeahead menu for selecting variables, supports dynamic option lists, and ensures seamless integration of variable nodes into rich text content with custom parsing capabilities. This makes it a key component for any application that requires embedding dynamic variables or tokens into user-editable text flows.