@artsy/fresnel declares breakpoint-based React layouts with CSS media queries so server-rendered pages show the correct layout before JavaScript loads.
Overview
@artsy/fresnel brings CSS media-query thinking into React components for responsive UIs. You define named breakpoints and interaction states, then wrap subtrees in Media components that render only when those queries match, similar to declarative @media rules in stylesheets.
During server-side rendering, markup for all configured breakpoints is emitted and hidden with plain CSS until the browser’s viewport matches. That lets users see the correct mobile or desktop structure while HTML streams in, before React hydrates. After load, matchMedia narrows which branches stay mounted so hidden components do not run lifecycle side effects such as duplicate analytics calls.
The API centers on createMedia to define breakpoints, MediaContextProvider at the app root, Media wrappers with props like at, lessThan, greaterThan, between, and onlyMatch to limit which queries apply. createMediaStyle injects the SSR stylesheet into the document head. The library is used with Next.js, Gatsby, and includes a gatsby-plugin-fresnel for configuration.
Artsy developed it to pair server rendering with responsive design where user-agent sniffing alone is unreliable. Trade-offs include larger initial HTML when all breakpoints ship from the server, offset by client-side cleanup after hydration.
Key features
- Declarative breakpoint Media components
- SSR-safe CSS visibility rules
- createMedia breakpoint definitions
- matchMedia pruning after hydration
- Pointer hover interaction queries
- createMediaStyle for document head
- Next.js and Gatsby examples
- onlyMatch query filtering
Use cases
- Server-rendered responsive React sites
- Separate mobile and desktop layouts
- Next.js hybrid static rendering
- Avoiding duplicate hidden component effects