Giter Site home page Giter Site logo

react-media-query-hoc's Introduction

react-media-query-hoc

A dead simple React Higher Order Component (HOC) that uses context for matching media queries

Why use this?

  • A simple API which doesnt require you to put MediaQuery components all over your code base
  • More performant (you only need 1 parent MediaQueryProvider that listens to media events you wish to configure)
  • Easier to test than other react media query libraries
  • Uses matchmedia for media queries for client and server
  • Abstracted away React context which is experimental (and subject to change)

Install

Via NPM:

npm install react-media-query-hoc --save

Via Yarn:

yarn add react-media-query-hoc

Usage

This library is designed so that you have 1 MediaQueryProvider parent and 1-many child components wrapped with withMedia HOC

MediaQueryProvider

This component will listen to media events you want to configure, it should be used once as a parent component

Usage:

import { MediaQueryProvider } from 'react-media-query-hoc';

const App = (props) => {
  return (
    <MediaQueryProvider>
      <TheRestOfMyApp />
    </MediaQueryProvider>
  );
};

export default App;

By providing no queries prop to the MediaQueryProvider component, it will default to these media queries

But you can provide different media queries for your use case using the queries prop, eg:

const App = (props) => {
  const customQueries = {
    verySmall: 'screen and (max-width: 300px)',
    someOtherMediaQuery: 'screen and (min-width: 301px)',
  };

  return (
    <MediaQueryProvider queries={customQueries}>
      <TheRestOfMyApp />
    </MediaQueryProvider>
  );
};

withMedia

This is a HOC to provide media match props to your component. This abstracts away context so that if there is any changes to the API in the future its easier to upgrade (see: React Context)

Usage:

import { withMedia } from 'react-media-query-hoc';

const MyComponent = ({ media, ...props}) => (
  if(media.tablet || media.mobile) {
    ..
    return (
      <div>
        Mobile and Tablet View
      </div>
    )
  }

  return (
    <div>
      Other View
    </div>
  )
);

export const BaseMyComponent = MyComponent;
export default withMedia(MyComponent);

Server Side Rendering

You can pass in media features from your server, all supported values can be found here: https://www.w3.org/TR/css3-mediaqueries/#media1

Usage (matches mobile screen during SSR):

const App = (props) => {
  const values = {
    width: 300,
    type: 'screen',
  };

  return (
    <MediaQueryProvider values={values}>
      <TheRestOfMyApp />
    </MediaQueryProvider>
  );
};

Testing Components

Because the media queries and context are abstracted out you can easily test components with or without the withMedia HOC, just ensure you export your component base without the HOC as well, eg:

export const BaseMyComponent = MyComponent;
export default withMedia(MyComponent);

Then in your React tests you can import like:

import { BaseMyComponent } from 'location_of_my_component';

And unit test the component without having to worry about context

Thanks

Big thanks to the maintainers of these repos

Both libraries are a bit similar, but my original use case required the extra advantages listed in Why use this?

react-media-query-hoc's People

Contributors

jooj123 avatar

Stargazers

Roman avatar

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.