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

react-match-width

v0.1.0

Published

A react utility (in the form of a higher order component) that makes it easy use the width media query allowing a higher degree of flexibility than CSS Media Queries

Downloads

1

Readme

react-match-width

A react utility (in the form of a higher order component) that makes it easy use the width media query allowing a higher degree of flexibility than CSS Media Queries

Installation

yarn add react-match-width

or

npm install react-match-width --save

Features

Basic usage

import { matchWidth } from 'react-match-width';

class MyComp extends Component {
  render() {
    return (
      <div>
        isBig: { this.props.isBig ? 'yes' : 'no' }
        <br />
        isMedium: { this.props.isMedium ? 'yes' : 'no' }
        <br />
        isSmall: { this.props.isSmall ? 'yes' : 'no' }
        <br />
      </div>
    );
  }
}

export default matchWidth({
  isBig: { minWidth: '700px' },
  isMedium: { minWidth: '420px', maxWidth: '700px' },
  isSmall: { maxWidth: '420px' },
})(MyComp);

Setting global defaults

When rendering on the server, there is no window to query so we need to set some defaults in order to have an answer. This can be achieved by wrapping this component

// helpers/match-width-with-defaults.js
import { withDefaults } from 'react-match-width';

// produce a default enabled version of the module
const { matchWidth } = withDefaults({
  width: '1200px', // default is: 1280px
});


// app.js
import { matchWidth } from 'helpers/match-width-with-defaults.js'
// ... use it as usual

Setting local defaults

Each use of the higher order component can specify the defaults for that use, just add a second parameter to the HOC call

//... define your component MyComp then wrap it:
export default matchWidth({
  isBig: { minWidth: '700px' },
  isMedium: { minWidth: '420px', maxWidth: '700px' },
  isSmall: { maxWidth: '420px' },
}, {
  width: '500px',
})(MyComp);

The matched API

import { matched } from 'react-match-width';


function SomeComponent(/* props */) {
  return <div>Some comp</div>;
}

const ForNotSmall = matched(SomeComponent, {minWidth: '420px'});

class TestMatched extends Component {
  render() {
    return (
      <div>
        Render for not-small:
        <ForNotSmall />
      </div>
    );
  }
}

The renderIf API

import { renderIf } from 'react-match-width';

const IfSmall = renderIf({maxWidth: '420px'});

class TestRenderIf extends Component {
  render() {
    return (
      <div>
        Will render if small:
        <IfSmall>
          This is small
        </IfSmall>
      </div>
    );
  }
}

Future

Because this is a higher order component, it's config --the queries themselves-- is defined statically, maybe find a way of defining them dynamically

Credits

It uses matchmedia, a wrapper over matchMedia API that also supports server rendering

Related projects

  • https://github.com/viruschidai/react-match-media - supports only the matched API style
  • https://github.com/contra/react-responsive - similar project but does not expose a HoC
  • https://github.com/paulirish/matchMedia.js/ - polifill
  • https://github.com/weblinc/media-match - another polifill

TODOs:

  • pass in the defaults at runtime (dev purposes, no dynamic)
  • maybe add a defaults provider
  • support local default also for non hoc apis
  • tests
  • types
  • CI
  • document node and browser compatibility