Skip to content

zero dependencies, ~1kb render prop wrapper of window.matchMedia

Notifications You must be signed in to change notification settings

schiehll/react-micro-match-media

Folders and files

NameName
Last commit message
Last commit date

Latest commit

52b92e1 · Apr 18, 2018

History

8 Commits
Apr 18, 2018
Apr 18, 2018
Apr 17, 2018
Apr 18, 2018
Apr 17, 2018
Apr 18, 2018
Apr 18, 2018
Apr 18, 2018
Apr 17, 2018
Apr 18, 2018
Apr 18, 2018
Apr 18, 2018
Apr 18, 2018
Apr 18, 2018

Repository files navigation

react-micro-match-media

zero dependencies, ~1kb render prop wrapper of window.matchMedia

travis build version

Instalation

$ npm install --save react-micro-match-media

Peer dependencies

This package expect react@^16.3.0 as a peer dependency.

Usage

You just pass in the query that you want to be listening and render your components accordingly.

import React, { Component } from 'react'
import MatchMedia from 'react-micro-match-media'

const App = () => (
  <MatchMedia query={`(max-width: 600px)`}>
    {matches => {
      if (matches) return 'will be rendered if viewport is <= 600'
      return 'will be rendered if viewport is > 600'
    }}
  </MatchMedia>
)

Smart usage

You can also do things like:

import React, { Component } from 'react'
import MatchMedia from 'react-micro-match-media'

const Portrait = ({ children, ...props }) => (
  <MatchMedia {...props} query={`(orientation: portrait)`}>
    {isPortrait => (isPortrait ? children : null)}
  </MatchMedia>
)

const App = () => (
  <Portrait>will render only if orientation is portrait</Portrait>
)

About

zero dependencies, ~1kb render prop wrapper of window.matchMedia

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published