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

@jsee_dev/react-mediaqueries

v1.7.0

Published

Ultra light library for React to render components depending on media queries.

Downloads

6

Readme

React Mediaqueries

What is it about ?

React Mediaqueries helps you enforce media rules to render your components in React. It basically provides prebuilt media rules, a method to customize them, and a wrapper component to be used in your app. I also lately added a useMediaQuery hook which I'll tell how to use.

Getting started

You need to be using typescript for your project.

Let's first install the library.

yarn add @jsee_dev/react-mediaqueries

or

npm install @jsee_dev/react-mediaqueries

The ReactMediaQueries class

  1. To be starting with, I would recommend importing it in a dedicated file in your project. Once it is done, you will have to instanciate the ReactMediaQueries class that has been imported, and get the wrapper component named MediaContext. Export it eventually so that it can be used anywhere in your application.
import ReactMediaQueries from "@jsee_dev/react-mediaqueries";

const { MediaContext } = new ReactMediaQueries();

export default MediaContext;
  1. Use it where your need it. Note that it can take two props : orientation, and type. For now I'm not using any other query type so I didn't find useful to go further.
const MyApp: React.FC = () => (
  <MediaContext type="desktop-large">
    <p>Hello World !</p>
  </MediaContext>
);
  1. (optional) You can customize the media queries. When you instanciate the library, just pass a configuration object as constructor. There you can specify which type you need to be tweaked, and then set min/max width for landscape and/or portrait orientation. Note that as soon as you make a modification, for instance on a min property, the associated max will be reset to null unless you give it a value as well.
import ReactMediaQueries from "@jsee_dev/react-mediaqueries";

const { MediaContext } = new ReactMediaQueries({
  smartphone_large: {
    portrait: {
      max: 414,
    },
  },
  desktop_small: {
    landscape: {
      min: 1024,
    },
  },
});

export default MediaContext;

And that's pretty much it !!

The useMediaQuery hook

  1. This hook has been designed to make precise queries more convenient to use.
    Just import it in your file, call it at the higher level of your function component, and it will return 4 methods. Each one targets a specific query, and returns a boolean if the queries matches with updates on navigator resize : max-width for isWidthSmaller, min-width for isWidthLarger, max-height for isHeightSmaller, and min-height for isHeightLarger.

  2. Then it can be used straight in your component TSX to condition the rendering of elements according to specific queries :-)

import { useMediaQuery } from "@jsee_dev/react-mediaqueries";

const MyComponent: React.FC = () => {
  const { isWidthLarger, isWidthSmaller, isHeightLarger, isHeightSmaller } = useMediaQuery();
  return (
    <div>
      <p>Hello World !</p>
      {isWidthLarger(720) && <p>Your device is probably not a smartphone !</p>}
    </div>
  );
};
  1. You can also use the hook to check custom queries. Just set your query as an argument of the hook, and it will return true or false if the query has matched or not.
const isHoverNotSupported = useMediaQuery("(hover: none)");

Have fun and feel free to bring any improvements as you like !