4 Ways to Show Tooltips in React with react-tooltip, Material UI, Bootstrap, or Semantic UI
Tooltips are a great way to provide additional context for elements in your application. In this article we will see how to add tooltips to your React applications using four different libraries: React Tooltip, Material UI, Bootstrap, and Semantic UI.Tooltip is a simple element that can be tricky to implement. You need to consider positioning, responsive layouts, and various browsers support. Fortunately, there are libraries that do that for you. We will look at several popular options for showing tooltips in React applications: React Tooltip , Material UI , Bootstrap , and Semantic UI . For this article, we assume that you're familiar with React components and how to use props and state to control them. All the libraries we're going to review provide great tooltip components. With all options, we can add a tooltip, define content for it, configure its position and styling. So the question is how to select the right one for your project? React Tooltip is a small library that provides only a tooltip component. Other options are large UI libraries with lots of different components. If you only look for the tooltip component for your React application with no need for other elements, React Tooltip would be a great option. In case if you're looking for multiple elements with a unified look, then you can select from Material UI , Bootstrap , and Semantic UI . The UI framework to use would depend on your preferences for a particular project. React Tooltip provides a tooltip component. We start by adding React Tooltip to a project: After that, we can use the ReactTooltip component to display tooltips: In this example, we start by importing the ReactTooltip component. We use ReactTooltip to render the content of the tooltip. We specify the id property for the tooltip and a content. The place and effect properties define the position of the tooltip. Next, we need to mark an element for which we'd like to display a tooltip. This is done with the data-tip and data-for custom attributes. We specify these attributes for the <button> element, but you can display tooltips for any element. The data-tip attribute helps React Tooltip to find an element. The data-for attribute contains the id of the ReactTooltip component that we've declared. There are additional options that allow controlling the position, events, and styling of a tooltip. You can find the API description in the GitHub repository and additional examples on the official site. Material-UI is a collection of React components based on Material Design from Google that you can use in your React applications. It includes the Tooltip component that we can use to show tooltips. Here's how we can show a simple tooltip with Material UI: We start by importing Button and Tooltip components. Next, we wrap the button that we want to show a tooltip for with the Tooltip component. We specify properties for the tooltip: the title property defines the text to display and placement specifies the position. You can find more tooltip examples on the official site. Bootstrap is one of the most popular frameworks for building web applications. There is a set of components that allow displaying overlays, tooltips, and popovers. Here's an example of the Bootstrap tooltip using the React Bootstrap : We start by importing Button , Tooltip , and OverlayTrigger components. We also import the Bootstrap CSS file for the default theme. In Bootstrap the Tooltip component does not position itself, it uses the OverlayTrigger component for that. We wrap the Button with the OverlayTrigger component to define that we'd like to show a tooltip for the button. The placement property of the overlay specified the position of the tooltip, while the overlay property defines the content to render for the tooltip. The renderTooltip function specified for the overlay property returns the Tooltip component with tooltip's content. More tooltip examples are available on the official site. Semantic UI is another popular UI framework that we can use in React applications. It contains its own tooltip component as well. Here's an example of a tooltip using Semantic UI Popup component: We start by importing the Button and Popup components. We also import the Semantic UI semantic.min.css file for default styling. We're using the Popup component to render the tooltip. The trigger property defines the component for which we'll provide a tooltip, the position property defines the position of the tooltip. The content specified inside the Popup component will be used as a tooltip's content. Tooltips are a great way to show additional content for UI elements. In this article, we've seen four ways to add tooltips in React applications. You are free to select the one that better works for your application.