RichText and plain text pasting
RichText
is at the heart of many WordPress blocks where "text has to be introduced".
But not all RichText
s have to have formatting options, like bold, italic, etc.
The documentation says:
If you want to limit the formats allowed, you can specify using
allowedFormats
property in your code.
Since allowedFormats
accepts an array, if you don't want any formatting, you can pass an empty value:
<RichText
allowedFormats={[]}
/>
The problem is that many copy-paste texts from different sources. If they paste a text containing some formatting, it will be preserved by default.
You will end up with elements that are formatted but with no way of removing them!
The solution is to add __unstablePastePlainText
, which is not documented. This will remove all formatting when pasting.
Bonus tip: there is also the __unstableDisableFormats
.
<RichText
__unstablePastePlainText
__unstableDisableFormats
/>
Be aware: this is still "unstable" in WordPress 6.1. It might become part of the API or be removed.