Adding syntax highlighting to an input field can be a hard task. 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
contenteditableto 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.
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.
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.
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.
- 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
- 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 :)
Less time debugging, more time building. Scout APM allows you to find and fix performance issues with no hassle. Now with error monitoring and external services monitoring, Scout is a developer's best friend when it comes to application development.
Markdown component for ReactProject mention: Markdown component without external library? | reddit.com/r/reactjs | 2022-01-10
Maybe look through the source to figure out how they do it?
> we can't take some well-tested, used-by-millions library
Here is an example of Sciter application that uses RemarkableJS library (https://github.com/jonschlinkert/remarkable) as it is:
the smallest commonmark (optionally gfm) compliant markdown parser that exists; new basis for @unifiedjs (hundreds of projects w/ billions of downloads for dealing w/ content)Project mention: How to Safely Render Markdown From a React Component | dev.to | 2021-07-29
React markdown vs remark react-markdown is a library that provides the React component to render the Markdown markup while remark is a markdown preprocessor built on micromark. It inspects, parses and transforms markdowns.
remark plugin to support GFM (autolink literals, footnotes, strikethrough, tables, tasklists)Project mention: Easy Comments | dev.to | 2021-08-31
Here React Markdown, React Syntax Highlighter and the Remark GFM plugin is used. React Markdown helps with parsing markdown and Remark GFM adds support for GitHub Flavoured Markdown. React Syntax Highlighter does syntax highlighting for code snippets in a comment.
Markdown component without external library?
1 project | reddit.com/r/reactjs | 10 Jan 2022
How to Safely Render Markdown From a React Component
7 projects | dev.to | 29 Jul 2021
1 project | reddit.com/r/tutobase | 6 Jun 2021
Best way to store blog post content (React/MongoDB)?
5 projects | reddit.com/r/reactjs | 26 May 2021
Paginating your Contentful blog posts in Next.js with the GraphQL API
2 projects | dev.to | 26 Apr 2021
Rendering Markdown with React Components embedded? I'm feeling stuck.
1 project | reddit.com/r/reactjs | 23 Mar 2021
Libraries to simplify styling/formatting of user "posts" retrieved from the database as a gigantic string of doom and ugliness?
1 project | reddit.com/r/reactjs | 30 Jan 2021
Are you hiring? Post a new remote job listing for free.