Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add a toolbar button in toolbar but not in dropdown menu #14881

Closed
QiShaoXuan opened this issue Apr 9, 2019 · 17 comments · Fixed by #43581
Closed

How to add a toolbar button in toolbar but not in dropdown menu #14881

QiShaoXuan opened this issue Apr 9, 2019 · 17 comments · Fixed by #43581
Assignees
Labels
[Package] Block editor /packages/block-editor [Type] Developer Documentation Documentation for developers

Comments

@QiShaoXuan
Copy link

I try to add a toolbar button in toolbar like this:

( function( wp ) {
    var MyCustomButton = function( props ) {
        return wp.element.createElement(
            wp.editor.RichTextToolbarButton, {
                icon: 'editor-code',
                title: 'Sample output',
                onClick: function() {
                    console.log( 'toggle format' );
                },
            }
        );
    }
    wp.richText.registerFormatType(
        'my-custom-format/sample-output', {
            title: 'Sample output',
            tagName: 'samp',
            className: null,
            edit: MyCustomButton,
        }
    );
} )( window.wp );

This is copied in gutenberg document

C603AE87-396F-4934-8612-44967837C895
My question is why the button is always in the dropdown menu, how can I add it in toolbar.

And it seems like it is not allow to add in toolbar after I view the code in format-toolbar and rich-text

It is dependent on the props but none be passed.

Wait online, it's urgent

@technote-space
Copy link
Contributor

@QiShaoXuan
Copy link
Author

I use BlockFormatControls to add buttons in toolbar.
BlockFormatControls

https://github.com/WordPress/gutenberg/blob/master/packages/block-editor/src/components/block-toolbar/index.js#L36

Thank you so much, could you tell me where did you get your screenshot of the plugin?

@mtias mtias added [Type] Developer Documentation Documentation for developers [Type] Help Request Help with setup, implementation, or "How do I?" questions. labels Apr 10, 2019
@andpor
Copy link

andpor commented Jul 30, 2019

Have the same problem...none of the examples around work...toolbar buttons always added to the end of More Rich Text Controls dropdown menu...even sample code from WordPress does not work and the screenshot in documentation is dated because I do not see strikethrough in main toolbar either - only dropdown

so annoying...!!!!!

@ghostmaster
Copy link

@QiShaoXuan: can you please share example, how to do that ?

@QiShaoXuan
Copy link
Author

@QiShaoXuan: can you please share example, how to do that ?

use BlockControls like this

<BlockControls>
  <Toolbar>
    <IconButton
      icon="edit"
    />
  </Toolbar>
</BlockControls>

@screeper
Copy link

Or in ES2015:

return wp.element.createElement(
    wp.components.IconButton, {
       icon: 'editor-code',
       label: 'Sample output',
       onClick: function() {
           console.log( 'toggle format' );
       },
   }
);

@mahnunchik
Copy link

What are the differences between RichTextToolbarButton and BlockFormatControls? Why there are only RichTextToolbarButton in the examples?

@fabienlege
Copy link

Same issue for me. Any news ?

@mahnunchik
Copy link

Hi @nickhamze how did you solve it?

@nickhamze
Copy link

Hi @nickhamze how did you solve it?

Look here at George's commit:
sortabrilliant/emojiconbini@d45573d

@chrisvanpatten
Copy link
Member

It would be great to see the RichTextToolbarButton component support controlling placement. Something like an isPrimary attribute, or some other way to "promote" to the main toolbar.

Long term a similar pinning approach to plugin sidebars would be pretty neat, or like the block visibility modal.

@t-hamano
Copy link
Contributor

It has been quite some time since the assignment was submitted, but I have been thinking about this issue again.

The answer to this issue is to use BlockControls, but there is no specific tutorial or sample code even in the latest Gutenberg.
I think it is a good idea to add a new step at the end of Formatting Toolbar API document. I would like to create a sample code and submit a PR.

@t-hamano
Copy link
Contributor

By updating the documentation, #29181 will be resolved at the same time.
Also related to this issue are #22663 and #30069 regarding the option to move the core format from the drop-down to the toolbar.

@t-hamano t-hamano self-assigned this Aug 24, 2022
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Aug 24, 2022
@t-hamano t-hamano removed the [Status] In Progress Tracking issues with work in progress label Aug 28, 2022
@mjjwatson
Copy link

mjjwatson commented Feb 21, 2023

Reigniting this with a potential issue—when adding the icon using <BlockControls> as described, if you have a RichText component using inlineToolbar, it renders to the parent block toolbar rather than your inline toolbar. Any work around or suggestions there?

@t-hamano
Copy link
Contributor

@mjjwatson
could you please attach a screenshot or specific code?

@mjjwatson
Copy link

mjjwatson commented Mar 11, 2023

Took a while as I've been away, sorry.
Using the example from the documentation:

const MyCustomButton = ( { isActive, onChange, value } ) => {
	return (
		<BlockControls>
			<ToolbarGroup>
				<ToolbarButton
					icon="editor-code"
					title="Sample output"
					onClick={ () => {
						onChange(
							toggleFormat( value, {
								type: 'my-custom-format/sample-output',
							} )
						);
					} }
					isActive={ isActive }
				/>
			</ToolbarGroup>
		</BlockControls>
	);
};
registerFormatType( 'my-custom-format/sample-output', {
	title: 'Sample output',
	tagName: 'samp',
	className: null,
	edit: MyCustomButton,
} );

And then creating a RichText component with inlineToolbar:

<RichText
	tagName={'h2'}
	value={ value }
	onChange={ value => {
              if(onChange)
                  onChange(value) 
          }}
         inlineToolbar
/>

This renders the new block control within the RichText's parent block, in the top left, as seen on this screenshot:

Screenshot 2023-03-11 at 15 33 52

@t-hamano
Copy link
Contributor

@mjjwatson
It is possible to render the RichTextToolbarButton component into an inline toolbar dropdown. However, as far as I can tell, there does not seem to be a slot provided to insert the component directly into the inline toolbar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Block editor /packages/block-editor [Type] Developer Documentation Documentation for developers
Projects
None yet
Development

Successfully merging a pull request may close this issue.