How to Use the Tooltip Component in Gutenberg

While working on my Gutenberg Development Course I realized that default toolbar buttons have a nice Tooltip that appears over them on hover.

Gutenberg Link Tooltip.png
Example of the tooltip component in action with the Bold button

I also remembered seeing a Tooltip component in Gutenberg when walking through the source code.  It turns out it is pretty easy to add these in our own block as well.

Setting Up the Tooltip Component

In order for the Tooltip component to work you simply have to get it from the wp.components library.  It is found at wp.componentns

Then you simply wrap <Tooltip></Tooltip> around whatever you want to trigger the tooltip.

The Tooltip takes two parameters:

  • text [required] = What text should appear in the tooltip
  • position [default “top”] = Determines where the tooltip will appear in relation to the child content

Here is an example of Tooltip in action:


const {
Button,
Dashicon,
Tooltip,
} = wp.components;
// Wrap the Tooltip component around whatever you want to activate the tooltip on hover
<Tooltip
text={ __( 'Add Tooltip Text Here' ) }
>
<Button>
// Change icon to desired dashicon icon or replace component with SVG
<Dashicon icon="edit" />
</Button>
</Tooltip>

NOTE: This example also includes the Button and Dashicon components.  This is not necessary.  Tooltip can be wrapped around other components, but I have only been able to get it working in the editor, not on the frontend.

Here is a look at what the code above can create:

High Contast View.png
Example of tooltip applied to custom block button

Want to Learn More About Gutenberg?

If you would like to learn more about working with JavaScript in WordPress, check out my Gutenberg Development Course.

Leave a comment