Cursor Selection Handling

1.0.1Last update Dec 8, 2024
by@krishn404

This codemod shows how to migrate cursor and selection handling from Slate v0.88 to v0.104, including the Preventing runtime errors from null selections.

Code Changes

Version 0.88

const selection = editor.selection;
const start = Range.start(selection);

Version 0.104

const selection = editor.selection as Range | null;
if (selection) {
const start = Range.start(selection);
// Selection is now safely typed and null-checked
}

Key Changes

  • Added explicit type casting to Range | null
  • Implemented null safety check using conditional block
  • Ensures selection exists before accessing its properties

Migration Benefits

  • Prevents runtime errors from null selections
  • Provides better TypeScript type safety
  • Follows current best practices for Slate selection handling

Build custom codemods

Use AI-powered codemod studio and automate undifferentiated tasks for yourself, colleagues or the community

background illustrationGet Started Now