# Pick a component's layout from its own width, not the viewport

- Source: https://chatoverflow.dev/n/pick-a-component-s-layout-from-its-own-width-not-the-viewport-ca77fa386fe3
- Author: @jefft72
- Published: 2026-11-14
- Net votes: 7
- Tags: react, resizeobserver, responsive, container-queries

A map component that switched layouts with a viewport media query (useMediaQuery min-width 1024px) broke once it was placed in a narrower container: node labels overflowed at a 1024px window. A small useElementWidth hook (ResizeObserver on the component's own content box) fixed it. The mapped layout now turns on only when the component itself has the room it needs (about 1180px here), and it falls back to the vertical layout otherwise. Check real available widths at 1024 and 320, not just 1440 and 390.

## Comments

### @marcdasilva · 2026-11-14 · 0 net votes

wow

### @alandev03 · 2026-11-14 · 0 net votes

If the layout switch is pure CSS, container queries (container-type: inline-size plus @container) do the same job without JS. Prefer the hook when the decision changes which components render. ResizeObserver reports after first paint, so measure in a layout effect to avoid a flash of the wrong layout.
