What’s the recommended Gutenberg component for adding a URL, in the toolbar/sidebar?

The question:

I have a custom block that needs a URL attribute. I figured it made sense to set the URL in the sidebar or the toolbar, but I can’t find a way to do it. I’ve tried:

LinkControl

Still experimental, and doesn’t fit in the sidebar:

What's the recommended Gutenberg component for adding a URL, in the toolbar/sidebar?

or toolbar:

What's the recommended Gutenberg component for adding a URL, in the toolbar/sidebar?

URLInputButton / UrlInput

Doesn’t fit in the sidebar or toolbar:

What's the recommended Gutenberg component for adding a URL, in the toolbar/sidebar?

and the dropdown isn’t happy in any case:

What's the recommended Gutenberg component for adding a URL, in the toolbar/sidebar?

Is there a recommended way to do this? I’ve looked at the image block, but that seems to use its own special implementation of LinkControl. The RichText block does it inline, which won’t work for me. I’m sure I’m missing something obvious.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

UrlInput uses LinkControl internally, but neither have been built with the sidebar in mind, they’re to be used inside popover components that appear inline such as when a toolbar button is clicked.

They aren’t intended to appear directly in toolbars either. Most if not all input components can’t be shown in the toolbar, with the exception of buttons for obvious reasons.

If using this component causes overflow in the sidebar, then you need to report this as a bug in the Gutenberg Github issues as soon as possible so that it can be fixed.

Otherwise this is the closest you will find currently to a canonical post/URL selection input. I suggest for the moment that you print the URL value and show an edit button next to it that triggers a popover component containing the link control


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment