lovasoa/react-contenteditable

React component for a div with editable contents

TypeScript

1,673

171 commits

updated Mar 15, 2023

See the code

README

react-contenteditable

React component for a div with editable contents

Build Status download count bundle size license

Install

npm install react-contenteditable

Usage

import React from 'react'
import ContentEditable from 'react-contenteditable'

class MyComponent extends React.Component {
  constructor() {
    super()
    this.contentEditable = React.createRef();
    this.state = {html: "<b>Hello <i>World</i></b>"};
  };

  handleChange = evt => {
    this.setState({html: evt.target.value});
  };

  render = () => {
    return <ContentEditable
              innerRef={this.contentEditable}
              html={this.state.html} // innerHTML of the editable div
              disabled={false}       // use true to disable editing
              onChange={this.handleChange} // handle innerHTML change
              tagName='article' // Use a custom HTML tag (uses a div by default)
            />
  };
};

Available props

propdescriptiontype
innerRefelement's ref attributeObject | Function
htmlrequired: innerHTML of the editable elementString
disableduse true to disable editingBoolean
onChangecalled whenever innerHTML changesFunction
onBlurcalled whenever the html element is blurredFunction
onFocusevent fires when an element has received focusFunction
onKeyUpcalled whenever a key is releasedFunction
onKeyDowncalled whenever a key is pressedFunction
classNamethe element's CSS classString
stylea collection of CSS properties to apply to the elementObject

Known Issues

If you are using hooks, please see this issue. Using this component with useState doesn't work, but you can still use useRef :

const text = useRef('');

const handleChange = evt => {
    text.current = evt.target.value;
};

const handleBlur = () => {
    console.log(text.current);
};

return <ContentEditable html={text.current} onBlur={handleBlur} onChange={handleChange} />

Examples

You can try react-contenteditable right from your browser to see if it fits your project's needs:

contenteditable
editing
html
react
react-component
text
wysiwyg

lovasoa/react-contenteditable

React component for a div with editable contents

TypeScript

1,673

171 commits

updated Mar 15, 2023

See the code

README

react-contenteditable

React component for a div with editable contents

Build Status download count bundle size license

Install

npm install react-contenteditable

Usage

import React from 'react'
import ContentEditable from 'react-contenteditable'

class MyComponent extends React.Component {
  constructor() {
    super()
    this.contentEditable = React.createRef();
    this.state = {html: "<b>Hello <i>World</i></b>"};
  };

  handleChange = evt => {
    this.setState({html: evt.target.value});
  };

  render = () => {
    return <ContentEditable
              innerRef={this.contentEditable}
              html={this.state.html} // innerHTML of the editable div
              disabled={false}       // use true to disable editing
              onChange={this.handleChange} // handle innerHTML change
              tagName='article' // Use a custom HTML tag (uses a div by default)
            />
  };
};

Available props

propdescriptiontype
innerRefelement's ref attributeObject | Function
htmlrequired: innerHTML of the editable elementString
disableduse true to disable editingBoolean
onChangecalled whenever innerHTML changesFunction
onBlurcalled whenever the html element is blurredFunction
onFocusevent fires when an element has received focusFunction
onKeyUpcalled whenever a key is releasedFunction
onKeyDowncalled whenever a key is pressedFunction
classNamethe element's CSS classString
stylea collection of CSS properties to apply to the elementObject

Known Issues

If you are using hooks, please see this issue. Using this component with useState doesn't work, but you can still use useRef :

const text = useRef('');

const handleChange = evt => {
    text.current = evt.target.value;
};

const handleBlur = () => {
    console.log(text.current);
};

return <ContentEditable html={text.current} onBlur={handleBlur} onChange={handleChange} />

Examples

You can try react-contenteditable right from your browser to see if it fits your project's needs:

contenteditable
editing
html
react
react-component
text
wysiwyg

Project health

Overall

3.2/10

Maintained

0/10

Code review

5/10
OpenSSF Scorecard

Latest release

3.3.7 · Feb 28, 2023

Releases this year

0

Significant stargazers

Jeswin

74 followers · starred Sep 2020

yongyu

47 followers · starred Sep 2019

Lorenzo Lewis

71 followers · starred Oct 2025

Michele Bertoli

1,232 followers · starred Aug 2018