DEV Community

Cover image for A Colorful Textarea
Daniel Schulz
Daniel Schulz

Posted on • Originally published at iamschulz.com

A Colorful Textarea

Adding syntax highlighting to an input field can be a hard task. <textarea> supports neither styling of individual characters or words, nor HTML tags within itself, there is no fully supported native solution for that. Most editors work with contenteditable to actually render a fully marked up code snippet and let the user edit its content. This requires a lot of work to get it accessible (as in restore all the native functions of a textarea) and still adds a lot of complexity.
If you don't want that and are just looking for a quick, dead-simple solution: Here's how to colorize a textarea.

Solution

The trick is to separate the input element from the displayed one. We can't color the content of a textarea, but we can make it invisible and replace it with marked up content. This works with monospaced fonts and fonts with a uniform width across normal, bold and italic characters. I'm using this for code and markdown, so that's perfectly acceptable for me. We also need to be careful to match the dimensions of the textarea exactly while only using font-relative units like em, to ensure that the highlight element scales well with the invisible textarea. The cursor is still in the textarea's context, while the text itself is rendered in the highlight element. We want to match every character of the textarea to match the highlighted one on a pixel-perfect basis.

A 3D explosion schema of the layout. In the background is a greyed out textarea with a colored cursor after the last character. It's content is a code snipet of an empty html5 page. The foreground is the same text, but syntax-highlighted in bright colors. The cursor of the textarea reaches into the foreground.

I also need to auto-resize my textarea. Since textareas usually scroll vertically, that would mess up the position matching with the highlight element. Auto-resizing seems like a graceful workaround to me.

The highlghting itself would work with every code parser. I'm using highlight.js to convert markdown to syntax-highlighted HTML. I listen for content changes in the textarea and parse new rendered code on every input. To counter the worst performance hits, I'll just use requestAnimationFrame. Debouncing isn't an option here, because the user would only see what they've written after they've finished typing. That'd be very poor UX.

Demo

Note that this example also displays the rendered Markdown in a separate element. I'll use the change listener that I already have to splice in a Markdown renderer: Showdown.

Pros

  • as accessible as a textarea
  • is a progressively enhanced feature
  • can be styled exactly to your needs
  • dead simple solution compared to a rich text editor

Cons

  • has performance issues with large texts (as do textareas in general)
  • works only with monospaced fonts
  • works only with auto-sizing textareas

This article was written in a textarea :)

Top comments (4)

Collapse
 
sfiquet profile image
Sylvie Fiquet

That's an interesting approach but it still needs refining. There is a problem with the text cursor in Safari (but not in Chrome or Firefox). It's not in the right place so it's really difficult to know where you're typing.

screenshot showing the cursor below the text

In the screenshot, the cursor is placed at the end of the text, as if I had just typed the exclamation mark. And of course, the more I type the worse it gets.

Collapse
 
iamschulz profile image
Daniel Schulz

Got it.
Safari was the only browser that didn't set a monospaced font on <textarea> by default. Should be working now.

Collapse
 
sfiquet profile image
Sylvie Fiquet

Yes, it works fine now!

Collapse
 
iamschulz profile image
Daniel Schulz

True. Safari User Agent styles are funky.
Id some some more time with that browser to match everything, but the principle still applies.