Skip to the content

useMediaQuery

useMediaQuery returns which viewport band is active, from the theme breakpoints. Use it when a component must change props or structure per viewport; for styling, prefer the responsive props or CSS media queries.
Hooks

Signature

const { isMobile, isTablet, isLaptop, isDesktop, isPortrait } = useMediaQuery();
Name
Type
Description
isMobile
boolean
Width below md (default 767px).
isTablet
boolean
md ≤ width < lg (default 990px).
isLaptop
boolean
lg ≤ width < xl (default 1200px).
isDesktop
boolean
Width ≥ xl.
isPortrait
boolean
(orientation: portrait) matches.
Scrolls sideways inside the table. The page does not.

Exactly one of the four size flags is true at any width. Change the bands with createTheme({ breakpoints: { values: { md, lg, xl } } }).

Example

import { useMediaQuery } from '@apollion-dsi/core/hooks';

function Actions() {
  const { isMobile } = useMediaQuery();

  return (
    <Flex gap="medium" flexDirection={isMobile ? 'column-reverse' : 'row'}>
      <Button variant="outlined" text="Cancel" fullWidth={isMobile} />
      <Button text="Confirm" fullWidth={isMobile} />
    </Flex>
  );
}

Pitfalls

  • The first render — on the server and on the client — is always the mobile band. Wider flags arrive after hydration, so a desktop layout chosen in JS appears one frame late. Prefer CSS when you can.
  • It throws outside BreakpointsProvider. ApollionProvider mounts one for you.
  • Branching on these flags for pure styling duplicates what the sm_/md_/lg_ responsive props already do.

See also