tiptap: Placeholder not rendering

What’s the bug you are facing?

When the editor is empty, either initially, or after deletion, no placeholder is displayed to the user.

A paragraph node is added with the appropriate classes, and a data-placeholder as set by my configuration, however, there is no displayed placeholder for the user. The Paragraph tag just contains an empty <br/>

image

How can we reproduce the bug on our side?

Set up basic editor, use @tiptap/extension-placeholder as per docs

Can you provide a CodeSandbox?

No response

What did you expect to happen?

The placeholder text gets displayed when there is no content.

Anything to add? (optional)

No response

Did you update your dependencies?

  • Yes, I’ve updated my dependencies to use the latest version of all packages.

Are you sponsoring us?

  • Yes, I’m a sponsor. 💖

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 20 (3 by maintainers)

Most upvoted comments

add some CSS to your component

/* Placeholder (at the top) */
.ProseMirror p.is-editor-empty:first-child::before {
  content: attr(data-placeholder);
  float: left;
  color: #adb5bd;
  pointer-events: none;
  height: 0;
}

/* Placeholder (on every new line) */
/*.ProseMirror p.is-empty::before {
  content: attr(data-placeholder);
  float: left;
  color: #adb5bd;
  pointer-events: none;
  height: 0;
}*/

Any guidance on how to style the placeholder with Tailwind?

I’m using this:

Placeholder.configure({
  placeholder,
  emptyNodeClass:
    'first:before:text-gray-400 first:before:float-left first:before:content-[attr(data-placeholder)] first:before:pointer-events-none',
}),

The placeholder works but the cursor isn’t placed at the beginning of the placeholder text: Screenshot 2022-05-02 at 17 40 42

Any guidance on how to style the placeholder with Tailwind?

I’m using this:

Placeholder.configure({
  placeholder,
  emptyNodeClass:
    'first:before:text-gray-400 first:before:float-left first:before:content-[attr(data-placeholder)] first:before:pointer-events-none',
}),

The placeholder works but the cursor isn’t placed at the beginning of the placeholder text: Screenshot 2022-05-02 at 17 40 42

Did you manage to find a solution? Adding “first:before:h-0” to emptyNodeClass worked for me.

Just sharing my tailwind fix - I’m using absolute positioning :

Placeholder.configure({
  placeholder,
  emptyEditorClass:
    'cursor-text before:content-[attr(data-placeholder)] before:absolute before:top-2 before:left-2 before:text-mauve-11 before:opacity-50 before-pointer-events-none',
})

For anyone using Tailwind, the above solution which uses emptyEditorClass on Placeholder.config won’t work as expected. The placeholder will render initially, but if you type something and remove it, it won’t show up anymore.

Instead you should just use add the styling to your CSS file. You can add tailwind classes with @apply:

@tailwind base;
@tailwind components;
@tailwind utilities;

.tiptap .is-empty::before {
  @apply text-slate-300;
  @apply content-[attr(data-placeholder)];
  @apply float-left;
  @apply pointer-events-none;
  @apply h-0;
}

If you are using Tailwinds CSS Add the the custom css to the global css file for tailwinds.

@tailwind base;
@tailwind components;
@tailwind utilities;



/* Custom CSS Needed for TipTap PlaceHolder */
.ProseMirror p.is-editor-empty:first-child::before {
  color: #adb5bd;
  content: attr(data-placeholder);
  float: left;
  height: 0;
  pointer-events: none;
}

Oh wow, I was trying to hack it via this type of approach, didn’t realize it’s how they want us to actually render it.

Does this seem a little hacky though at first glance? What benefit does this have vs, a default display through just the PlaceHolder.configure.

At the very least I feel this should be made a little more clear in the docs. I completely missed the styles tab in the example myself at 1am last night.

Appreciate the replies everyone. Thanks.

Just sharing my tailwind fix - I’m using absolute positioning :

Placeholder.configure({
  placeholder,
  emptyEditorClass:
    'cursor-text before:content-[attr(data-placeholder)] before:absolute before:top-2 before:left-2 before:text-mauve-11 before:opacity-50 before-pointer-events-none',
})

thanks man, I was confused why my tailwind style (inside tailwind.css) doesn’t work, but applying it to emptyEditorClass works fine

Placeholder text is displayed via a CSS pseudo element, have a look at the demo setup: https://embed.tiptap.dev/preview/Extensions/Placeholder

add some CSS to your component

/* Placeholder (at the top) */
.ProseMirror p.is-editor-empty:first-child::before {
  content: attr(data-placeholder);
  float: left;
  color: #adb5bd;
  pointer-events: none;
  height: 0;
}

/* Placeholder (on every new line) */
/*.ProseMirror p.is-empty::before {
  content: attr(data-placeholder);
  float: left;
  color: #adb5bd;
  pointer-events: none;
  height: 0;
}*/

thank you babe!!! you save my life

@coolcorexix Agreed! Submitted a PR. #3711 👍

Oh wow, I was trying to hack it via this type of approach, didn’t realize it’s how they want us to actually render it.

Does this seem a little hacky though at first glance? What benefit does this have vs, a default display through just the PlaceHolder.configure.

At the very least I feel this should be made a little more clear in the docs. I completely missed the styles tab in the example myself at 1am last night.

Appreciate the replies everyone. Thanks.

this should be mentioned in the doc, the scss file in the example is easy to skip 🤦

I’ve seen a few people with this issue but for me with standard config, cursor shows up at beginning of the input.

image

/* Placeholder (at the top) */
.part3-rich-text p.is-editor-empty:first-child::before {
	color: #adb5bd;
	content: attr(data-placeholder);
	float: left;
	height: 0;
	pointer-events: none;
}

Just sharing my tailwind fix - I’m using absolute positioning :

Placeholder.configure({
  placeholder,

emptyEditorClass: ‘cursor-text before:content-[attr(data-placeholder)] before:absolute before:top-2 before:left-2 before:text-mauve-11 before:opacity-50 before-pointer-events-none’, })

Cursor Positioning looks off… (Tailwind)

Screenshot (72)

Fix by changing before:top-2 before:left-2 to before:top-3 before:left-3

Screenshot (73)

your class should now look like this:

Placeholder.configure({
  placeholder: "add your placeholder text here",
  emptyEditorClass:
     'cursor-text before:content-[attr(data-placeholder)] before:absolute before:top-3 before:left-3 before:text-mauve-11 
     before:opacity-50 before-pointer-events-none',
})

add some CSS to your component

/* Placeholder (at the top) */
.ProseMirror p.is-editor-empty:first-child::before {
  content: attr(data-placeholder);
  float: left;
  color: #adb5bd;
  pointer-events: none;
  height: 0;
}

/* Placeholder (on every new line) */
/*.ProseMirror p.is-empty::before {
  content: attr(data-placeholder);
  float: left;
  color: #adb5bd;
  pointer-events: none;
  height: 0;
}*/

To add to this, if you just wanted to show all available placeholders by default, you could use the following CSS selector:

.ProseMirror [data-placeholder]::before