1.10.0 • Published 4 years ago

react-media v1.10.0

Weekly downloads
139,603
License
MIT
Repository
github
Last release
4 years ago

react-media Travis npm package

react-media is a CSS media query component for React.

A <Media> component listens for matches to a CSS media query and renders stuff based on whether the query matches or not.

Installation

Using npm:

$ npm install --save react-media

Then, use as you would anything else:

// using ES modules
import Media from "react-media";

// using CommonJS modules
var Media = require("react-media");

The UMD build is also available on unpkg:

<script src="https://unpkg.com/react-media/umd/react-media.min.js"></script>

You can find the library on window.ReactMedia.

Usage

Render a <Media> component with a query prop whose value is a valid CSS media query. The children prop should be a function whose only argument will be a boolean flag that indicates whether the media query matches or not.

import React from "react";
import Media from "react-media";

class App extends React.Component {
  render() {
    return (
      <div>
        <Media query="(max-width: 599px)">
          {matches =>
            matches ? (
              <p>The document is less than 600px wide.</p>
            ) : (
              <p>The document is at least 600px wide.</p>
            )
          }
        </Media>
      </div>
    );
  }
}

If you render a <Media> component on the server, it always matches.

If you use a regular React element as children (i.e. <Media><SomethingHere/></Media>) it will be rendered if the query matches. However, you may end up creating a bunch of elements that won't ever actually be rendered to the page (i.e. you'll do a lot of unnecessary createElements on each render). Thus, a children function (i.e. <Media>{matches => ...}</Media>) is the preferred API. Then you can decide in the callback which elements to create based on the result of the query.

For the common case of "only render something when the media query matches", you can use a render prop that is only called if the query matches.

import React from "react";
import Media from "react-media";

class App extends React.Component {
  render() {
    return (
      <div>
        <Media
          query="(max-width: 599px)"
          render={() => <p>The document is less than 600px wide.</p>}
        />
      </div>
    );
  }
}

The render prop is never called if the query does not match.

<Media query> also accepts an object, similar to React's built-in support for inline style objects in e.g. <div style>. These objects are converted to CSS media queries via json2mq.

import React from "react";
import Media from "react-media";

class App extends React.Component {
  render() {
    return (
      <div>
        <Media query={{ maxWidth: 599 }}>
          {matches =>
            matches ? (
              <p>The document is less than 600px wide.</p>
            ) : (
              <p>The document is at least 600px wide.</p>
            )
          }
        </Media>
      </div>
    );
  }
}

Keys of media query objects are camel-cased and numeric values automatically get the px suffix. See the json2mq docs for more examples of queries you can construct using objects.

An optional targetWindow prop can be specified if you want the query to be evaluated against a different window object than the one the code is running in. This can be useful for example if you are rendering part of your component tree to an iframe or a popup window.

If you're curious about how react-media differs from react-responsive, please see this comment.

Enjoy!

About

react-media is developed and maintained by React Training. If you're interested in learning more about what React can do for your company, please get in touch!

dashboardcomponentsperfiosbs-sulaglobalization-idmp-nodejs-applicationpwa-fegardenplacemetropoliant-design-pro-scaffoldant-design-pro-csrofo-sulagntd@identance/sdkcntd2conquista-componentescntd3cntd4cntd5cntd6cntd7elon-npmlink-testcommon-xuc4-libcs-libsimple-antd-management-fast-frameworkcn-libcn-lib-4cn-lib-for-4cn-4cn-2022@infinitebrahmanuniverse/nolb-react-meperfiosuicomponents@everything-registry/sub-chunk-2567oliviaoddonjm-react-component-librarynowtv-myaccount-cancel-journey-widgetsmira-elementsonespot-dashboard-prototypeproject-x-uibistun-sulabiyi-captureblume-home@confluenza/component-navigator-react@confluenza/confluenza@confluenza/gatsby-theme-confluenza@clutch-marketplace/react-mediabstempsulabssula@cultureamp/kaizen-component-librarycapital-components@freepress/theme-default@vitalsource/vstui@uidu/chat-window@uidu/data-manager@uipl/react@uipl/react-components@uidu/responsivr@uidu/form@uidu/guidu-website@visit-x/vxmessenger-mockup@zenlink-dapp/apps@zenlink-dapp/ui-componentswb-ui-kitzvook-ui-kitzero-component-library@deckchairlabs/ui@borrow-ui/uiyui-scaffoldarpsula@arz.digital/components@bloom-housing/ui-components@wesleylhandy/gatsby-theme-author-base@webjet/reactantdsiteantd-site-commonxszz_chatbot@aircamapp/cyclonexcim-codesandboxant-ts-proant-typescript-proant-design-pro-sgwoodant-design-pro-tsapp-tearsheet@allthings/elements@uidu/treenav@sportnet/uivlk-admin@tusharjain994/safe-react-components@sygniacoza/components@tds/core-responsive@gnosis.pm/safe-react-components@gold-jet/sulala-serena-deptosleapstack-commonleapstack_packageivy-modulemdj-react-bigbuttonknosis_componentsjz-cnlzengpsulajh-bigdatanewbron-components
2.0.0-rc.1

4 years ago

2.0.0-rc.0

4 years ago

1.10.0

5 years ago

1.10.0-alpha.1

5 years ago

1.10.0-alpha.0

5 years ago

2.0.1-alpha.0

5 years ago

2.0.0-alpha.1

5 years ago

1.9.2

5 years ago

1.9.1

5 years ago

1.9.0

5 years ago

1.8.0

6 years ago

1.8.0-rc.1

6 years ago

1.7.0

6 years ago

1.6.1

7 years ago

1.6.0

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.2

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.1.0

8 years ago

0.0.1

9 years ago