Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to add a placeholder to Monaco editor
Adem Kouki
Adem Kouki

Posted on

     

How to add a placeholder to Monaco editor

I was playing around with theMonaco editor and I wanted to add a placeholder to it. Just like the placeholder in VSCode. I couldn't find any documentation on how to do it, so I decided to write this post. I will be using the Monaco editor in React, but the same concept can be applied to any other framework.

The problem

Monaco editor is a great editor, but it doesn't have a placeholder. There is no option to add a placeholder to it.

VSCode is using Monaco, and it has a placeholder. So I decided to look at the source code of VSCode and see how they did it.

I found out how they did it...

It's in the fileuntitledTextEditorHint.ts.

The idea is very simple. They are using adiv element as overlay. When the editor is empty, thediv element is visible. When the editor is not empty, thediv element is hidden.

The solution

I made this sandbox to show how to add a placeholder to Monaco editor.

The component defines two functions, "handleEditorOnChange" and "handleEditorOnMount", that are passed as props to the rendered editor.

The "handleEditorOnChange" function is invoked when the content of the editor changes, and it checks if the editor is empty or not.

If it is empty, it shows the placeholder div by setting its display property to "block", otherwise it hides it by setting the display property to "none". The "handleEditorOnMount" function is invoked when the editor is first rendered and it shows the placeholder and focuses the editor.

If you want to see the full code, you can check it out onStackBlitz.

I hope this post was helpful. If you have any questions, feel free to ask them in the comments.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Greetings 😉, I’m Adem. Full-Stack Engineer @NextGRC | Turning Ideas into Impact
  • Location
    Tunisia
  • Work
    Web Developer | Pentester
  • Joined

More fromAdem Kouki

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp