Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@theme-ui/match-media

Package Overview
Dependencies
Maintainers
0
Versions
437
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@theme-ui/match-media

React hooks for theme-ui breakpoints

  • 0.17.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
23K
increased by49.7%
Maintainers
0
Weekly downloads
 
Created
Source

@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>
  )
}

FAQs

Package last updated on 24 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc