Photo by Casper Nichols on Unsplash

Look at Me!!! Ensure Your Users See Your Tooltips With This Fun React / Material UI Snippet

Don Mallory

--

After deploying a recent project and tracking user activity using Google Analytics, I realized some of my users weren’t able to divine how to navigate through the application. Implementing a legend or big shiny buttons might be options on larger viewports, but with maintaining responsiveness for mobile devices one of my top priorities, finding a solution when screen real estate is at a premium was leaving me scratching my head for a solution.

Not bad for displaying a ton of data on such a small screen size, but what does that star even represent?

Tooltips came to mind, but that came with its own set of issues as some iconography used to inform content is attached to say, a Menu Item. For a desktop user, hovering would display a tooltip, but on a mobile device there isn’t an effective way to display a tooltip unless a user somehow decides to do a long press. I could disable the click from propagating from the icon to the menu, but that didn’t feel like a great solution either.

Thankfully, the wonderful creators of the @material-ui react library expose the open property for a Tooltip and we can wire the tooltips to display on component load.

Material UI allows programmatic manipulation of tooltip behavior. Sweet!

Staring at that tooltip for a long time might make the behavior feel unintended, though, so let’s set it to disappear after a timeout. React Hooks to the rescue!

Even though I traditionally use MobX for global state management, there’s plenty of room for useState when certain values can stay localized. A simple boolean state value and a setTimeout are all we need here to toggle the tooltips off after a few seconds. Voila!

Tooltips for this component will now display on component load, and then disappear after a set interval

After using functional components and React Hooks exclusively for the last few months, I can’t imagine I ever dealt with classes and lifecycle methods. If you haven’t made the transition, now’s the time!

--

--