useViewportBreakpoint
Description
useViewportBreakpoint — a React hook for tracking responsive breakpoints using window.matchMedia.
- Converts a breakpoint map into a sorted list and creates a
MediaQueryListfor each one (with caching). - Automatically updates the active breakpoint when the viewport width changes.
- Returns
matches— an object with all breakpoints and their state (true/false), andactiveBreakpoint— the currently active breakpoint. - Supports
defaultBreakpointas a fallback, especially useful in SSR to provide a predictable value before hydration. - The returned structure is hybrid: available both as a tuple
[matches, activeBreakpoint]and as an object{ matches, activeBreakpoint }.
Signature
ts
function useViewportBreakpoint<BreakpointKey extends string | symbol>(
breakpointMap: Record<BreakpointKey, number>,
options?: UseViewportBreakpointOptions<BreakpointKey>,
): UseViewportBreakpointReturn<BreakpointKey>;Parameters
breakpointMap: Record<BreakpointKey, number>— map of breakpoint keys to their minimum width in pixels.options?: UseViewportBreakpointOptions— optional settings:defaultBreakpoint?: BreakpointKey— fallback key when no breakpoint matches.
Returns:
UseViewportBreakpointReturn— a hybrid structure with both tuple and object forms:matches: Record<BreakpointKey, boolean>— map of all breakpoints and whether they currently match.activeBreakpoint: BreakpointKey | null— the currently active breakpoint key, ornullif none matches.- Tuple access:
[matches, activeBreakpoint].
Examples
1) Basic usage (tuple)
tsx
import { useViewportBreakpoint } from '@webeach/react-hooks';
export function ResponsiveLayout() {
const [matches, active] = useViewportBreakpoint({
mobile: 0,
tablet: 768,
desktop: 1200,
});
return (
<div>
<p>Active breakpoint: {String(active)}</p>
{matches.mobile && <MobileMenu />}
{matches.desktop && <DesktopNav />}
</div>
);
}2) Named access (object)
tsx
import { useViewportBreakpoint } from '@webeach/react-hooks';
export function Sidebar() {
const { matches, activeBreakpoint } = useViewportBreakpoint({
narrow: 0,
wide: 1000,
});
return (
<aside>
<h2>Breakpoint: {String(activeBreakpoint)}</h2>
{matches.narrow && <CollapsedSidebar />}
{matches.wide && <ExpandedSidebar />}
</aside>
);
}3) With defaultBreakpoint
tsx
const { matches, activeBreakpoint } = useViewportBreakpoint(
{ sm: 0, md: 600, lg: 1200 },
{ defaultBreakpoint: 'sm' },
);
// If no media query matches, 'sm' will be returned as active (relevant in SSR).Behavior
- Sorted breakpoints
- Breakpoints are sorted by their numeric width values (from smallest to largest).
- Single active breakpoint
- Only one breakpoint can be active (
activeBreakpoint) at any given time.
- Only one breakpoint can be active (
- SSR safety
- On the server, empty values are returned, and the hook activates properly in the browser.
- Media query caching
- Each
min-widthmedia query is cached to avoid creating duplicateMediaQueryListinstances.
- Each
- Stable return structure
- The returned object/tuple is stable thanks to
useDemandStructure.
- The returned object/tuple is stable thanks to
- Fallback breakpoint
- If no media query matches and a
defaultBreakpointis provided, it is used as the active breakpoint. - Especially useful in SSR to have a predictable value before hydration in the browser.
- If no media query matches and a
When to use
- Responsive layouts where you need to know the active breakpoint in React.
- Conditional rendering of UI elements depending on viewport width.
- Managing adaptive components (menus, sidebars, navigation, grids).
When not to use
- If you only need CSS-based breakpoints without JS awareness — prefer pure CSS.
- For extremely performance-sensitive contexts with dozens of listeners — consider optimizing.
Common mistakes
- Expecting multiple breakpoints to be active
- Only one breakpoint can be
trueat a time.
- Only one breakpoint can be
- Forgetting to provide a default breakpoint
- Without it,
activeBreakpointcan benullwhen none matches.
- Without it,
- Overlapping or unsorted values
- Always provide consistent ascending values for breakpoints.
Typing
Exported types
UseViewportBreakpointMatches<BreakpointKey>- Record of breakpoints and their boolean state.
UseViewportBreakpointOptions<BreakpointKey>- Options object with
defaultBreakpoint.
- Options object with
UseViewportBreakpointReturn<BreakpointKey>- Hybrid: tuple
[matches, activeBreakpoint]and object{ matches, activeBreakpoint }.
- Hybrid: tuple