← Каталог
React — компоненты-рецепты — Tooltip (подсказка)
Фрагмент из «React — компоненты-рецепты»: Tooltip (подсказка).
import { useId, useState } from 'react';
export function Tooltip({ label, children }) {
const [open, setOpen] = useState(false);
const tipId = useId();
return (
<span
className="tooltip-wrap"
onMouseEnter={() => setOpen(true)}
onMouseLeave={() => setOpen(false)}
onFocus={() => setOpen(true)}
onBlur={() => setOpen(false)}
>
<span aria-describedby={open ? tipId : undefined}>{children}</span>
{open && (
<span id={tipId} role="tooltip" className="tooltip-bubble">
{label}
</span>
)}
</span>
);
} import { useId, useState } from 'react';
export function Tooltip({ label, children }) {
const [open, setOpen] = useState(false);
const tipId = useId();
return (
<span
className="tooltip-wrap"
onMouseEnter={() => setOpen(true)}
onMouseLeave={() => setOpen(false)}
onFocus={() => setOpen(true)}
onBlur={() => setOpen(false)}
>
<span aria-describedby={open ? tipId : undefined}>{children}</span>
{open && (
<span id={tipId} role="tooltip" className="tooltip-bubble">
{label}
</span>
)}
</span>
);
} .tooltip-wrap { position: relative; display: inline-block; }
.tooltip-bubble {
position: absolute;
left: 50%;
bottom: calc(100% + 6px);
transform: translateX(-50%);
padding: 0.35rem 0.6rem;
font-size: 0.8125rem;
white-space: nowrap;
background: #212529;
color: #fff;
border-radius: 6px;
z-index: 10;
} .tooltip-wrap { position: relative; display: inline-block; }
.tooltip-bubble {
position: absolute;
left: 50%;
bottom: calc(100% + 6px);
transform: translateX(-50%);
padding: 0.35rem 0.6rem;
font-size: 0.8125rem;
white-space: nowrap;
background: #212529;
color: #fff;
border-radius: 6px;
z-index: 10;
}