Content editable element in React.
Tags: webdev, javascript, react, beginners
Published: 2024-04-25 09:21:46

In recent time, I was tasked with implementing a feature that allows users to edit content seamlessly, such as modifying paragraphs or headings. I explored various approaches to tackle this challenge, ranging from toggling text elements to input elements upon clicking or onBlur events, to leveraging the 'editable' prop in the Ant Design library.

All the above suggestions worked but not giving me the result I was looking for. Then I learnt something new. I figured you can also make text elements (paragraph tags, header tags etc.) editable seamlessly with an inbuilt attribute called contentEditable. 

So contentEditable prop when passed, would make the element editable by just clicking on it, it can be a lot helpful when building rich editors so basically what this is doing is giving user the ability to inject html directly on the DOM(Document Object Model) which if we are being honest can be risky due to browsers inconsistency so it should be handled properly.

**How it works**

contentEditable  is a property In the html element that help with rendering rich texts in the browser, and editing it on the fly, making an element editable is quite straight forward. 

Let’s see an example:

![Example of how to make an editable content](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwyf1ir14ou3wnamxvdc.png)




From the example above we noticed the props being passed into the element

_**contentEdtable**_: This prop is set to true, which signifies to the browser that this element is editable 

**_suppressEditableContentWarning_**: This prop is also added to help remove warning that’s arise when using the contentEditable prop, this is a warning you can possibly get “A component is contentEditable and contains children managed by react” if using it in a react project.
 
Now, let’s save the value that is being typed in our editable element.


![Example of how to save the editable data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28yy6rki5xg2ivj6qr04.png)

Now in this example, we can clearly see we how we catch values typed into our editable content and save it in a state. And if we go over to our console and check after the onBlur function is fired you should see your value rendered. In our case "Hello there"

![result of out console log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcatmf9w8xo6ees00jxz.png)



There are other methods you can use to save your data, I decided to use onBlur by discretion, but you can choose to use other functions like onInput, onMouseOut, onKeydown etc.

NB. Editable elements do not have onChange function like form elements(input tags, textarea tags etc).

Let's do one last trick. We now have our editable content up and working, but just like inputs have placeholders when it is empty, we too can add placeholders to our elements when empty.

Here is how: 

![html image for showing placeholder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9rg9dqjdyu7x399jn36.png)

In the image above you can see we added two new attributes

_**data-placeholder**_: This attribute helps us add the appropriate placeholder to our element. This is where we would add our placeholder value

**_className_**: This is us adding a className to our editable element so we can target it in our css.

Then, our final strike as we would have known is going to be in our css file. 

![Image of the css file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tm5zyv0sq1vnpd5bxqna.png)

With this css properties added to our element class our element should have the placeholder specified by the data-placeholder in the html.

And that's it guys!

We have successfully created an editable element, saved it's state locally and also added placeholder when it is empty.


Best wishes on your development journey.