GithubHelp home page GithubHelp logo

xapgkop4uk / react-media-context Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jxnblk/react-media-context

0.0 1.0 0.0 98 KB

React higher-order component (HOC) to provide context for the currently matched media query.

Home Page: http://jxnblk.com/react-media-context/

JavaScript 100.00%

react-media-context's Introduction

react-media-context

Build Status

React higher-order component (HOC) to provide context for the currently matched media query.

Getting Started

npm i -S react-media-context

Usage

import React from 'react'
import MediaContext from 'react-media-context'

const Title = (props, context) => {
  const { media } = context
  let fontSize = 32
  if (media.indexOf('large') > -1) {
    fontSize = 64
  }

  const style = {
    fontSize
  }

  return (
    <h1 style={style}>
      Responsive Heading
    </h1>
  )
}

Title.contextTypes = {
  media: React.PropTypes.array
}

class App extends React.Component {
  render () {
    <MediaContext>
      <Title />
    </MediaContext>
  }
}

How is this different from x?

Most other responsive React HOCs tend to work on the principle of showing and hiding children based on media queries. With this component, you can alter styling and functionality of components responsively.

This could be handy for:

  • Changing font sizes based on the viewport width
  • Changing margin or padding
  • Grid systems
  • Dramatically altering page layout
  • Using different routing flows
  • Changing image sources based on pixel density (but, srcset is probably a better option)

Props

  • queries (Object) - Media queries to match against on window resize
// Default
{
  'xsmall': 'screen and (max-width: 40em)',
  'small': 'screen and (min-width: 40em)',
  'medium': 'screen and (min-width: 52em)',
  'large': 'screen and (min-width: 64em)'
}

MIT License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.