Customise your web projects with an easy-to-use and responsive Tailwind CSS Tooltip!
A Tooltip is a small pop-up element that appears while the user moves the mouse pointer over an element. Use it when you want to specify extra information about something when the user moves the mouse pointer over an element.
See below our example that will help you create a simple Tooltip for your React project.
You can change the position of the Tooltip component using the placement prop.
You can modify the open/close state animation for Tooltip component using the animate prop.
This tooltip example is perfect if you want to avoid cluttering the screen. It provides additional info about a specific element on the website.
You can use the className prop to add custom styles to the Tooltip component.
The following props are available for tooltip component. These are the custom props that we've added for the tooltip component and you can use all the other native props as well.
| Attribute | Type | Description | Default |
|---|---|---|---|
open | boolean | Makes the tooltip open, when tooltip is controlled | undefined |
handler | function | Change open state for controlled tooltip | undefined |
content | node | Add content for tooltip | undefined |
interactive | boolean | Make tooltip interactive | false |
placement | Placement | Change tooltip placement | top |
offset | Offset | Change tooltip offset from it's handler | 5 |
dismiss | Dismiss | Change dismiss listeners when clicking outside | undefined |
animate | Animate | Change tooltip animation | undefined |
className | string | Add custom className for tooltip | '' |
children | node | The element that tooltip will reference to | No default value it's a required prop. |
import type { TooltipProps } from "@material-tailwind/react";type placement =
| "top"
| "top-start"
| "top-end"
| "right"
| "right-start"
| "right-end"
| "bottom"
| "bottom-start"
| "bottom-end"
| "left"
| "left-start"
| "left-end";type offset =
| number
| {
mainAxis?: number;
crossAxis?: number;
alignmentAxis?: number | null;
};type dismissType = {
enabled?: boolean;
escapeKey?: boolean;
referencePress?: boolean;
referencePressEvent?: 'pointerdown' | 'mousedown' | 'click';
outsidePress?: boolean | ((event: MouseEvent) => boolean);
outsidePressEvent?: 'pointerdown' | 'mousedown' | 'click';
ancestorScroll?: boolean;
bubbles?: boolean | {
escapeKey?: boolean;
outsidePress?: boolean;
};
};type animate = {
mount?: object;
unmount?: object;
};Learn how to customize the theme and styles for tooltip components, the theme object for tooltip components has two main objects:
A. The defaultProps object for setting up the default value for props of tooltip component.
B. The styles object for customizing the theme and styles of tooltip component.
You can customize the theme and styles of tooltip components by adding Tailwind CSS classes as key paired values for objects.
interface TooltipStylesType {
defaultProps: {
interactive: string;
placement: string;
offset:
| number
| {
mainAxis: number;
crossAxis: number;
alignmentAxis: number;
};
dismiss: {
enabled: boolean;
escapeKey: boolean;
referencePress: boolean;
referencePressEvent: 'pointerdown' | 'mousedown' | 'click';
outsidePress: boolean | ((event: MouseEvent) => boolean);
outsidePressEvent: 'pointerdown' | 'mousedown' | 'click';
ancestorScroll: boolean;
bubbles: boolean | {
escapeKey: boolean;
outsidePress: boolean;
};
};
animate: {
mount: object;
unmount: object;
};
className: string;
};
styles: {
base: object;
};
}import type { TooltipStylesType } from "@material-tailwind/react";const theme = {
tooltip: {
defaultProps: {
interactive: false,
placement: "top",
offset: 5,
dismiss: {},
animate: {
unmount: {},
mount: {},
},
className: "",
},
styles: {
base: {
bg: "bg-black",
py: "py-1.5",
px: "px-3",
borderRadius: "rounded-lg",
fontFamily: "font-sans",
fontSize: "text-sm",
fontWeight: "font-normal",
color: "text-white",
outline: "focus:outline-none",
overflowWrap: "break-words",
zIndex: "z-[999]",
whiteSpace: "whitespace-normal",
},
},
},
};