question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an `editor` prop to the `Editor` react component.

See original GitHub issue

Do you want to request a feature or report a bug?

Feature

What’s the expected behavior?

Currently, you can only get the editor controller instance by using refs. I think being able to instantiate your Editor controller above your component in the tree, and pass it as a prop to the editor would improve the ergonomics a bit when you need to interact with your editor outside of plugins. This can also sort of be accomplished with renderEditor, but at the expense of a bit of clarity.

Example:

function App() {
  const editor = new SlateEditor({plugins})
 
  return (
    <Editor
         editor={editor} // could be `controller` maybe?
    />
  )
}

If this were implemented, we could remove ref support and cleanup the editor component code a bit. You could still technically do the same thing, you’d just have grab the editor from the props of your ref vs acting on your ref directly.

Thoughts?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
brendancarneycommented, Jun 4, 2019

I’ve been thinking about this more and doing some experimenting in our code base. I do think we should work away from the ref approach for a couple reasons:

  • Function components can’t have refs, and while react will support classes for a long time, it’s clear the future is all components being functions, and there are at least a few bugs that I know could be solved cleanly with hooks in the editor component.
  • It’s not very ergonomic. I think we can come up with a better way that will be less confusing for new users.

I’m not sure if this is best, but here’s what I’m thinking so far:

Basic Usage

import { useSlate, Editor } from "slate-react"

function App() {
  const editor = useSlate({ plugins: [], ...otherOptions}) 

  return <Editor editor={editor} />
}

This way, editor always exists vs in the ref approach where Editor has to render first.

2reactions
brendancarneycommented, Jun 10, 2019

A few thoughts in response:

View-layer-specific props I think there are two options here. One is that these are defined in the hook, and the hook also gives you a component:

const [Editor, controller] = useSlate({ plugins, spellCheck: false})

The other is that anything that gets passed directly to the DOM node is passed to the component. I think it should behave this way either way, so it makes sense to support it.

Value Yes, I think you’d pass value to the hook also. For default value, we could still support that with state inside the hook.

I’d like to introduce a useEditor() hook later for accessing the editor

I do exactly this in our application. I love it. I’d recommend useSlate as the main hook.


I’m down to put some time into working out a proof of concept whether we go this way or not. The biggest thing for me is getting ConvertKit’s editors up to date with the latest versions first, so it might be a little bit before I get started.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Editor Demo: Add an Editor Component the Easy Way
In this article, you'll learn how to easily add a React Rich Text Editor with the help of the KendoReact component library and...
Read more >
How To Customize React Components with Props
To start, open src/App.js in a text editor. This is the root component that is injected into the page. All components will start...
Read more >
How to Build a Code Editor with React that Compiles and ...
The editor receives the props onChange , language , code , and theme from its parent component, which is Landing.js .
Read more >
Build rich text editors in React using Draft.js and react-draft ...
The react-draft-wysiwyg library offers a rich text editor ... Add the className props to the Editor component and the relevant styles to ...
Read more >
React component - CKEditor 5 Documentation
The easiest way to use CKEditor 5 in your React application is by choosing one of the rich text editor builds. Additionally, it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found