npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kaweb/react-mql

v1.3.0

Published

CSS Media Queries HOC and MediaContext for React

Downloads

10

Readme

react-mql

CircleCI Known Vulnerabilities npm npm MIT Licence

CSS Media Queries HOC and MediaContext for React

Demo

Introduction

react-mql is a React based utility component to match media as per CSS Media Queries using browser-native matchMedia. react-mql provide two components namely Media and MediaContext, both of which can have just one functional component as child.

Use Media as the top most component which accept list of media queries as prop and it passes on the matches to its immediate single functional component. In case we need to have statefull component as child of Media then it need to be enclosed inside functional component wrapper.

MediaContext is a helper component which can be used anywhere and any number of times in the component sub-tree inside Media. It is more helpfull when we do not want to keep passing media query matches as props down the component sub-tree. Anywhere in the sub-tree when we need to access matches we can get using MediaContext.

Demo

https://kumarabhishek.github.io/react-mql/

Edit on codepen

Edit vyklk855w7

Features

  • It has ZERO dependencies and is ~ 75 LOC.
  • It is purely based on browser native HTML5 feature MediaQueryList.
  • Uses latest React 16.x Context API

Install

NPM

npm i @kaweb/react-mql

UMD

Add script tag as below:

<script src="https://unpkg.com/@kaweb/react-mql/lib/react-mql.js"></script>

Then use following global object:

const Media = ReactMql.default;
const MediaContext = ReactMql.MediaContext;

Usage

react-mql can be imported in your application as below:

import Media, {MediaContext} from '@kaweb/react-mql';
  • Media component

Media accept following props:

Name | type | Description | Example -----|------|---------------------|--------- list |Object| Object with key as media-query name and value as CSS media queries. If list is not passed, <Media> simply render its children. | {landscape: '(orientation: landscape)'}, where landscape and '(orientation: landscape)' are respectively name and value of media-query. When there is a match for this media-query, matches object provided to functional component will be {landscape: true/false}

import React from 'react';
import Media from '@kaweb/react-mql';

const CompStateless = (props) => {
  return <div style={{background: props.bigScreen ? '#edeeed' : '#66ee66', padding: '1rem'}}>
    <h3>Stateless Component</h3>
    <h4>CSS Media Query Matches:<br/>{JSON.stringify(props, 4)}</h4>
  </div>
};

class CompMatchesAsProps extends React.Component {
	render() {
		return <div style={{ background: this.props.bigScreen ? '#ffccff' : '#66cc66', padding: '0.5rem' }}>
			<h3>Component with matches coming from passed props</h3>
			<h4>CSS Media Query Matches:<br/>{JSON.stringify(this.props, 4)}</h4>
		</div>;
	}
}

const list = {
  bigScreen: "(min-width: 1080px) and (max-width: 1920px)",
  landscape: "(orientation: landscape)"
};
export default class App extends React.Component {
  render() {
    return (
      <React.Fragment>
        <h2>
          Resize browser / rotate your mobile and observe changes in values as
          below:
        </h2>
        <Media list={list}>
          {CompStateless}
        </Media>
        <Media list={list}>
          {v => <CompMatchesAsProps {...v} />}
        </Media>
      </React.Fragment>
    );
  }
}

render(<App />, document.getElementById("root"));
  • MediaContext component

We use MediaContext to access mediaquery matches anywhere inside the component hierarchy of Media as shown below:

import React from 'react';
import Media, {MediaContext} from '@kaweb/react-mql';

class CompMatchesUsingMediaContext extends React.Component {
  render() {
    return (
      <MediaContext>
        {v => (
          <div
            style={{
              background: v.bigScreen ? "#aaccee" : "#66ccaa",
              padding: "0.5rem"
            }}
          >
            <h3>Component with matches using MediaContext</h3>
            <h4>
              CSS Media Query Matches:<br />
              {JSON.stringify(v, 4)}
            </h4>
          </div>
        )}
      </MediaContext>
    );
  }
}

const list = {
  bigScreen: "(min-width: 1080px) and (max-width: 1920px)",
  landscape: "(orientation: landscape)"
};

export default class App extends React.Component {
  render() {
    return (
      <React.Fragment>
        <h2>
          Resize browser / rotate your mobile and observe changes in values as
          below:
        </h2>
        <Media list={list}>
          {() => <CompMatchesUsingMediaContext />}
        </Media>
      </React.Fragment>
    );
  }
}

render(<App />, document.getElementById("root"));

Example

To try out example provided as part of react-mql follow following steps:

Clone repo

git clone https://github.com/kumarabhishek/react-mql.git

Install npm modules

npm i

Run example

npm start

Now open http://localhost:3000.

Tests

Jest is used for unit testing with coverage ans eslint is used as linter. To run the test suite, first install the dependencies, then run npm test inside root folder:

npm test

Contributing

Your contributions are welcome!

Refer contributing guide for more details.