Guttenberg block that add class for selected element.
-
Hi, I am creating a gutenberg block that gives the ability to assign a class to selected text in Block. Here is my code. This is my code in edit.js
const { __ } = wp.i18n; const { toggleFormat } = wp.richText; const { RichTextToolbarButton, RichTextShortcut } = wp.editor; const { registerFormatType } = wp.richText; const name = 'block-fields/add-class'; const add_class = { name, title: __('Add Class'), tagName: 'span', className: 'wp-block-custom-red', edit({ isActive, value, onChange }) { const onToggle = () => { onChange(toggleFormat(value, { type: name }), { tagName: 'span', className: isActive ? 'wp-block-custom-red' : null }); }; return ( <> <RichTextShortcut type="primary" character="u" onUse={onToggle} /> <RichTextToolbarButton icon="editor-paste-word" title={__('Add Class')} onClick={onToggle} isActive={isActive} /> </> ); }, }; function registerFormats() { registerFormatType(name, add_class); } registerFormats(); export default add_class;
This code works, but I’m seeing errors in the developer console. Can you tell me what causes them and how to get rid of them ?
Also, I would be grateful if you could explain how to move this functionality to quick buttons ?
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Guttenberg block that add class for selected element.’ is closed to new replies.