Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@theme-ui/match-media

system-ui190.1kMIT0.17.2TypeScript support: included

React hooks for theme-ui breakpoints

readme

@theme-ui/match-media

React hooks for referencing theme-ui breakpoints.

npm i @theme-ui/match-media

Usage

This package provides two React hooks useResponsiveValue and useBreakpointIndex for referencing responsive values outside of the sx prop. Note that this is a client-side-only, JavaScript solution.

import { useResponsiveValue, useBreakpointIndex } from '@theme-ui/match-media'

const MyComponent = () => {
  // Return literal values:
  const color = useResponsiveValue(['red', 'green', 'blue'])
  // Or provide a function to access theme values:
  const themeColor = useResponsiveValue((theme) => [
    theme.colors.red,
    theme.colors.green,
    theme.colors.blue,
  ])
  // `useBreakpointIndex` returns the index of the currently matched media query:
  const index = useBreakpointIndex()

  return (
    <div>
      The current color is: {color}, and the current index is: {index}
    </div>
  )
}

Server side rendering

Each hook also accepts an options object, used to set a default breakpoint index when rendering on the server.

import { useResponsiveValue, useBreakpointIndex } from '@theme-ui/match-media'

const MyComponent = () => {
  const color = useResponsiveValue(['red', 'green', 'blue'], {
    defaultIndex: 1,
  })
  const index = useBreakpointIndex({ defaultIndex: 0 })

  return (
    <div>
      The current color is: {color}, and the current index is: {index}
      // Gatsby will output: // The current color is: green, and the current index
      is: 0
    </div>
  )
}

Each hook uses breakpoints[0] by default, if not specified.

import { useResponsiveValue, useBreakpointIndex } from '@theme-ui/match-media'

const MyComponent = () => {
  const color = useResponsiveValue(['red', 'green', 'blue'])
  const index = useBreakpointIndex()

  return (
    <div>
      The current color is: {color}, and the current index is: {index}
      // Gatsby will output: // The current color is: red, and the current index
      is: 0
    </div>
  )
}

changelog

v0.14.3 (Thu Apr 21 2022)

🐛 Bug Fix

  • Support full media queries in theme.breakpoints in @theme-ui/match-media hooks #2199 (@hasparus)
  • fix(match-media): handle full media queries in breakpoints in useResponsiveValue and useBreakpointIndex hooks (@hasparus)

Authors: 1


v0.12.0 (Thu Oct 28 2021)

:tada: This release contains work from a new contributor! :tada:

Thank you, William Pei (@draekien), for all your work!

🐛 Bug Fix

  • Merge remote-tracking branch 'upstream/develop' into fix/color-scheme-media-query-api-update #981 (@draekien)

Authors: 1


v0.11.0 (Wed Aug 25 2021)

🐛 Bug Fix

Authors: 1


v0.10.0 (Sat Jun 19 2021)

🏠 Internal

Authors: 2


v0.8.3 (Wed May 05 2021)

⚠️ Pushed to stable

  • Merge branch 'stable' into develop (@hasparus)

Authors: 1


v0.8.0 (Wed May 05 2021)

🐛 Bug Fix

⚠️ Pushed to stable

  • Merge branch 'stable' into develop (@hasparus)

Authors: 3


v0.7.5 (Wed Apr 28 2021)

⚠️ Pushed to stable

  • Merge branch 'develop' into stable (@hasparus)

Authors: 1


v0.7.0 (Thu Apr 15 2021)

🚀 Enhancement

🐛 Bug Fix

⚠️ Pushed to stable

  • Merge branch 'develop' into stable (@hasparus)

Authors: 2