Urgent.News

One page, thousands of outlets. See who else covered it.

Editions

Tech

React Portals: How They Work and When to Use Them for Modals and Tooltips

You’ve probably built a modal or tooltip in React and hit a weird snag: the overlay appears visually outside your usual component tree, but events don’t behave as you expect. Maybe clicks inside the modal don’t bubble the way you thought, or keyboard focus seems lost when you open it. That’s React portals messing with your head , in a good way. They let you render children into a DOM node outside…

React portals are a powerful tool for rendering components outside their normal position in the React DOM tree. This can prevent clipping issues, improve stacking order, and make focus management easier for modals, tooltips and other overlay components.

Under the hood, a portal lets React render its children into a separate DOM node that lives outside the root React container. This means the component tree and the actual DOM tree can diverge - the modal or tooltip will be rendered as a real DOM node at the specified container location like document.body, while the React tree still sees it as a child of the root component.

This separation affects how React handles events and focus. Events bubble and propagate through the real DOM, but the portal nodes are separate from the React tree. So clicking inside a portalized modal may not trigger event handlers as expected if they are listening at the React root level rather than the portal container. Focus management also breaks down, because keyboard navigation works on the real DOM, not the React tree.

The key to using portals correctly is understanding these differences. Keep a ref to the portal container DOM node and check event targets against that container when handling clicks or keyboard events. Attach event listeners to the portal container instead of the React root. Explicitly manage focus by focusing the first focusable element inside the portal and trapping tab navigation inside the portal container.

And watch out for CSS stacking context and overflow issues that can affect portalized elements. Portals are ideal when you need UI overlays that break out of their parent containers' clipping, need to be siblings of the main app node, or require independent focus management. Modals, tooltips, dropdowns and similar UI elements are common candidates for using portals to get them positioned and focused correctly.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Spotify App Adds a List of New Features

Spotify has added a whole bunch of new features for its app, including Playlist Notes, Running Mode, and more. Playlist … Read More The post Spotify App Adds a List of New Features appeared first on…

I Built a Drag-and-Drop Kanban Board with Vanilla JavaScript

If you've ever wanted to understand how drag-and-drop actually works under the hood — no libraries, no frameworks, just the browser — building a Kanban board is a great way to find out.

  • Three-column Kanban board created with HTML, CSS, JavaScript
  • Drag-and-drop functionality implemented using HTML5 Drag and Drop API
  • Event listeners handle card movement between To Do, In Progress, and Done columns

Get better color accuracy on your TV with these simple steps

Even if it has impressive out-of-the-box color, your new (or old) TV can always benefit from a few simple tweaks to get the best color accuracy possible.

  • Adjust color temperature to achieve desired warmth or coolness.
  • Modify color saturation for balance between vibrancy and clarity.
  • Fine-tune red, green, and blue levels for precise color accuracy.

Building a Change & Alert Engine That Webhooks Any Diff on Any URL

Most data jobs re-pull the whole source every run and waste time and money on data that didn't move. A change-detection engine wakes up, checks what actually changed, and pushes only the diff.

  • Engine detects changes on any URL, sends only differences
  • Polls source once per schedule, auto-sniffs content format
  • Supports JSON, RSS/Atom, HTML; emits changes with before/after

More from Wednesday 19 August →