Tooltips & Dropdown Menus with Portals
Use portals to render tooltips and dropdowns that escape overflow-hidden containers.
Tooltips & Dropdown Menus with Portals is a free React Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Welcome
The Overflow Problem
Positioning Strategy
const rect = triggerRef.current.getBoundingClientRect();
const top = rect.bottom + window.scrollY + 8;
const left = rect.left + window.scrollX;Tooltip Component
function Tooltip({ content, children }) {
const [visible, setVisible] = useState(false);
const [pos, setPos] = useState({ top: 0, left: 0 });
const ref = useRef();
const show = () => {
const r = ref.current.getBoundingClientRect();
setPos({ top: r.bottom + window.scrollY + 4,
left: r.left + window.scrollX });
setVisible(true);
};
return (
<span ref={ref} onMouseEnter={show} onMouseLeave={() => setVisible(false)}>
{children}
{visible && createPortal(
<div style={{ position:'absolute', top: pos.top, left: pos.left }}
className="tooltip">{content}</div>,
document.body
)}
</span>
);
}Updating Position on Scroll & Resize
useEffect(() => {
if (!visible) return;
const update = () => setVisible(false);
window.addEventListener('scroll', update, true);
window.addEventListener('resize', update);
return () => {
window.removeEventListener('scroll', update, true);
window.removeEventListener('resize', update);
};
}, [visible]);Dropdown Menu Architecture
Click-Outside Handler
useEffect(() => {
if (!open) return;
const handler = (e) => {
if (!menuRef.current?.contains(e.target) &&
!triggerRef.current?.contains(e.target)) {
setOpen(false);
}
};
document.addEventListener('mousedown', handler);
return () => document.removeEventListener('mousedown', handler);
}, [open]);Keyboard Navigation in Dropdowns
Using a Positioning Library
import { useFloating, flip, shift } from '@floating-ui/react';
const { refs, floatingStyles } = useFloating({
middleware: [flip(), shift()],
});Accessibility: ARIA for Tooltips
<button aria-describedby="tip1">Hover me</button>
{visible && createPortal(
<div id="tip1" role="tooltip">Helpful text</div>,
document.body
)}Quick Check
Recap
Course Complete
Frequently asked questions
Is the “Tooltips & Dropdown Menus with Portals” lesson free?
Yes — the full text of “Tooltips & Dropdown Menus with Portals” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “Tooltips & Dropdown Menus with Portals”?
Use portals to render tooltips and dropdowns that escape overflow-hidden containers. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Tooltips & Dropdown Menus with Portals” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Are React Portals?
- Building a Modal Component with Portals
- Handling Focus & Keyboard Traps in Modals
- Tooltips & Dropdown Menus with Portals