@alexsasharegan/react-match-media
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

react-match-media

npm npm downloads GitHub issues GitHub license

A render prop component that provides idiomatic react component access to the match media API.

Type Documentation

Install

NPM

npm install @alexsasharegan/react-match-media

Yarn

yarn add @alexsasharegan/react-match-media

Usage

As Render Prop

import { MatchMedia } from "@alexsasharegan/react-match-media";

function BootstrapBreakpoints(props) {
  return (
    <MatchMedia
      queries={{
        xs: "(max-width: 575.98px)",
        sm: "(min-width: 576px) and (max-width: 767.98px)",
        md: "(min-width: 768px) and (max-width: 991.98px)",
        lg: "(min-width: 992px) and (max-width: 1199.98px)",
        xl: "(min-width: 1200px)",
      }}
    >
      {({ xs, sm, md, lg, xl }) => (
        <main>
          <h1>Match Media</h1>
          <pre>{prettyPrint({ xs, sm, md, lg, xl })}</pre>
        </main>
      )}
    </MatchMedia>
  );
}

function prettyPrint(value) {
  return JSON.stringify(value, null, 2);
}

As HOC

import { MatchMedia } from "@alexsasharegan/react-match-media";

const ExampleHOC = withBootstrapBreakpoints(function Example({ matches }) {
  return (
    <main>
      <h1>Match Media</h1>
      <pre>{prettyPrint(matches)}</pre>
    </main>
  );
});

const withBootstrapBreakpoints = Component => props => (
  <MatchMedia
    queries={{
      xs: "(max-width: 575.98px)",
      sm: "(min-width: 576px) and (max-width: 767.98px)",
      md: "(min-width: 768px) and (max-width: 991.98px)",
      lg: "(min-width: 992px) and (max-width: 1199.98px)",
      xl: "(min-width: 1200px)",
    }}
  >
    {matches => <Component matches={matches} {...props} />}
  </MatchMedia>
);

function prettyPrint(value) {
  return JSON.stringify(value, null, 2);
}

Readme

Keywords

none

Package Sidebar

Install

npm i @alexsasharegan/react-match-media

Weekly Downloads

8

Version

1.1.2

License

MIT

Unpacked Size

24 kB

Total Files

24

Last publish

Collaborators

  • alexsasharegan