[Solved] How do I customize themes in monaco editor – Typescript

by
Ali Hasan
customization monaco-editor next.js react-typescript

The Problem:

I am developing a code editor using Monaco and want to customize the theme to highlight specific parts of code differently. I want variables to be highlighted in light blue, types in dark green, and functions in yellow, but the default ‘vs-dark’ theme uses different colors. How can I achieve this customization?

The Solutions:

Solution 1: Use custom theme and rules

Yes, it is partially possible. Here’s a solution:
1. Define a custom theme inheriting from `vs-dark` and add rules to customize specific tokens. Here is an example theme definition:
“`
monaco.editor.defineTheme(‘default’, {
base: ‘vs-dark’,
inherit: true,
rules: [
{ token: ‘identifier’, foreground: ‘#9CDCFE’ },
{ token: ‘type’, foreground: ‘#1AAFB0’ },
],
colors: {}
});
2. Set the custom theme using `monaco.editor.setTheme()`.
“`

Q&A

Can I change syntax highlighting colors in monaco editor?

Yes, you can change the various tokens’ colors in monaco editor

How do I create a custom theme in Monaco editor?

You can create a custom theme by extending an existing theme and modifying its rules and colors.

Can I customize tokens in monaco editor

Yes, you can customize tokens to customize syntax highlighting in monaco editor

Video Explanation:

The following video, titled "Build a Code Editor in React - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... Monaco Editor for React and React Live. We'll walk through setting up a text editor in a React app with Monaco Editor for React, customizing ...