Tooltip Component
The <Tooltip> component renders floating content next to its children. It can be opened on hover or on click, stays inside the screen and can be used as a simple tooltip, a popover or a menu.
Basic Usage
The component requires a content prop. The children are the element the tooltip is attached to.
- Basic
- With Arrow
- Click Trigger
- Menu
import { Tooltip, Button } from "react-better-html";
function App() {
return (
<Tooltip content="Deletes the item permanently">
<Button.destructive text="Delete" />
</Tooltip>
);
}
import { Tooltip, Icon } from "react-better-html";
function App() {
return (
<Tooltip
content="More information"
position="right"
withArrow
isSmall
>
<Icon name="infoI" />
</Tooltip>
);
}
import { Tooltip, Button, Div, Text } from "react-better-html";
function App() {
return (
<Tooltip
trigger="click"
content={
<Div.column gap={8}>
<Text fontWeight={700}>Share</Text>
<Text>Copy the link to share this page.</Text>
</Div.column>
}
contentWidth={240}
>
<Button.secondary text="Share" />
</Tooltip>
);
}
Opens on click and closes when clicking outside.
import { Tooltip, Button } from "react-better-html";
function App() {
return (
<Tooltip
trigger="click"
align="right"
asContextMenu
content={
<>
<Tooltip.sectionTitle text="Account" />
<Tooltip.item icon="eye" text="View profile" onClick={() => {}} />
<Tooltip.item icon="filter" text="Preferences" description="Theme, language" onClick={() => {}} />
<Tooltip.divider />
<Tooltip.item icon="trash" text="Delete account" textColor="#dc3545" iconColor="#dc3545" />
</>
}
contentMinWidth={220}
>
<Button.icon icon="chevronDown" />
</Tooltip>
);
}
asContextMenu removes the inline padding of the content so the items fill the whole width.
Common Props
content- the content of the tooltip. Can be a string or any elementposition-top,bottom(default),leftorright. The tooltip is shifted to stay inside the screenalign- the alignment along the side -left,center(default) orrightfortop/bottompositions andtop,centerorbottomforleft/rightpositionstrigger-hover(default) orclickwithArrow- renders an arrow pointing to the childrenisSmall- smaller padding of the contentbackgroundColor- the background color of the contentasContextMenu- styles the content as a menudisabled- the tooltip is not openedisTabAccessed- the children can be focused with theTabkeycontentWidth/contentMinWidth- the size of the contentcontentPaddingBlock/contentPaddingInline- the padding of the contentcontentPointerEvents- thepointer-eventsof the content (passnonefor tooltips that should not be hovered)childrenWrapperWidth/childrenWrapperHeight- the size of the element that wraps the children (width defaults tofit-content)onOpen/onClose- callbacks when the tooltip is opened or closed
Ref API
The tooltip can be controlled using a ref:
type TooltipRef = {
isOpen: boolean;
open: () => void;
close: () => void;
};
import { useRef } from "react";
import { Tooltip, TooltipRef, Button } from "react-better-html";
function App() {
const tooltipRef = useRef<TooltipRef>(null);
return (
<Tooltip
trigger="click"
asContextMenu
content={
<Tooltip.item
text="Duplicate"
onClick={() => {
duplicateItem();
tooltipRef.current?.close();
}}
/>
}
ref={tooltipRef}
>
<Button.secondary text="Actions" />
</Tooltip>
);
}
Subcomponents
The subcomponents are building blocks for the content of the tooltip.
Tooltip.item
A clickable row with an optional icon and description.
text/textColor- the text of the itemdescription- a smaller text underneathicon/iconColor- an icon before the textisActive- renders the text in bolddisabled- the item can not be clickedonClick/onClickWithValue- click handlers.onClickWithValueis used together with thevalueprop
Tooltip.sectionTitle
A small uppercase title for a group of items. Accepts all <Text> props.
Tooltip.divider
A horizontal line between items. Accepts all <Divider.horizontal> props.