Loading...

A simple React Tooltip component that uses TailwindCSS only (no libraries)

question react tailwindcss
Ram Patra Published on September 19, 2024

Here’s a React component that allows you to attach the tooltip to any element by passing it as a child. The component will render the tooltip and an arrow pointing downward.

Tooltip Component

import React from 'react';

const Tooltip = ({ children, text }) => {
  return (
    <div className="relative group inline-block">
      {children}

      <div className="absolute left-1/2 -translate-x-1/2 bottom-full mb-2 hidden group-hover:block w-max bg-black text-white text-xs rounded px-2 py-1">
        {text}
        <div className="absolute left-1/2 -translate-x-1/2 top-full h-0 w-0 border-l-4 border-r-4 border-t-4 border-l-transparent border-r-transparent border-t-black"></div>
      </div>
    </div>
  );
};

export default Tooltip;

How to Use the Component

You can now wrap any element with the Tooltip component to display the tooltip when hovering over that element.

Example Usage:

import React from 'react';
import Tooltip from './Tooltip';

const App = () => {
  return (
    <div className="p-10">
      {/* Example with a button */}
      <Tooltip text="This is a tooltip for a button">
        <button className="px-4 py-2 bg-blue-600 text-white rounded">Hover me</button>
      </Tooltip>

      {/* Example with an image */}
      <Tooltip text="This is a tooltip for an image">
        <img src="https://via.placeholder.com/150" alt="Example" className="mt-5" />
      </Tooltip>

      {/* Example with text */}
      <Tooltip text="This is a tooltip for text">
        <p className="mt-5">Hover over this text</p>
      </Tooltip>
    </div>
  );
};

export default App;

Explanation:

  • Tooltip Component: Accepts two props, children and text.
    • children: This is the element you want to attach the tooltip to (e.g., button, image, text, etc.).
    • text: This is the text that will appear inside the tooltip.
  • Positioning: The tooltip will appear above the element and will be centered horizontally. It will only be visible on hover (group-hover:block).

Key Points:

  • You can wrap any element with the Tooltip component.
  • The tooltip will work for any element since it’s designed to be generic.

You can also implement this tooltip with just HTML and CSS without React.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on September 19, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question react front-end August 21, 2024 How to disable minification in a React app?

To disable minification in a React app created with create-react-app (CRA) using npm, you need to modify the build process. However, CRA does not directly expose Webpack configuration without ejecting, but you can still achieve this without ejecting by using the GENERATE_SOURCEMAP environment variable and a custom build script.

question nextjs react July 18, 2024 How to programmatically navigate to a page in Next.js?

In Next.js, you can programmatically navigate to different pages using the useRouter hook from the next/navigation module. This hook provides a convenient way to navigate between pages within your Next.js application. Below is an example demonstrating how to use the useRouter hook to programmatically navigate to another page.

question nextjs react July 9, 2024 How to define an enum in Typescript and use it in a Next.js app?

Creating an enum in TypeScript within a Next.js project is straightforward. Enums in TypeScript allow you to define a set of named constants that can be used to represent a collection of related values. Here’s how you can create and use an enum in a Next.js application: