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-breakpoint-utils

v1.0.4

Published

TS library to help out with handling window sizes in React projects

Downloads

11

Readme

react-breakpoint-utils

The react-breakpoint-utils library is aiming to help with declaring, and using breakpoints easily for responsive UI development in React.
 

BreakPointsProvider

First, you will need BreakPointsProvider, to declare the breakpoints for your application. Super easy to use, just wrap your application with this component, passing the breakpoints as shown below. It is a good idea to do this on the highest possible level you can, i.e. your index or App file.

You can define values optionally to the following breakpoint keys as you like:

  • XS, SM, MD, LG, XL, XXL, XXXL

Example #1:

import { createRoot } from 'react-dom/client';
import { BreakPoints, BreakPointsProvider } from 'react-breakpoint-utils';
import { App } from './App';

const container = document.getElementById("root");
const root = createRoot(container!);

const breakPoints: BreakPoints = {
  XS: 425,
  SM: 640,
  MD: 768,
  LG: 1024,
  XL: 1280,
  XXL: 1536,
  XXXL: 2000,
};

root.render(
  <BreakPointsProvider value={breakPoints}>
    <App />
  </BreakPointsProvider>
);

Example #2:

You might want to use less breakpoints for your app. In that case, assign values only to those keys you want to use. Based on your declaration, the hooks of this library will dynamically provide data for you. Easy as that.

import { createRoot } from 'react-dom/client';
import { BreakPoints, BreakPointsProvider } from 'react-breakpoint-utils';
import { App } from './App';

const container = document.getElementById("root");
const root = createRoot(container!);

const breakPoints: BreakPoints = {
  SM: 640,
  MD: 768,
  LG: 1024,
  XL: 1280,
};

root.render(
  <BreakPointsProvider value={breakPoints}>
    <App />
  </BreakPointsProvider>
);


 

MatchMedia

MatchMedia is a super simple to use component, designed to easily hide or display your components. Props are optional, so you can provide either only one of the min/max breakpoints value. It is recommended to pass actual breakpoints, using the useBreakPoints hook, avoiding hard coded or duplicate breakpoint values in your code.

Example:

import { FC } from "react";
import { MatchMedia, useBreakPoints } from "react-breakpoint-utils";

export const MyTestPage: FC = () => {
  const { SM, LG } = useBreakPoints();

  return (
    <>
      <MatchMedia min={SM} max={LG}>
        <p>{"You can only see me between 640px and 1024px"}</p>
      </MatchMedia>
      <MatchMedia min={SM}>
        <p>{"You can only see me above 640px"}</p>
      </MatchMedia>
      <MatchMedia max={SM}>
        <p>{"You can only see me below 640px"}</p>
      </MatchMedia>
    </>
  );
};


 

useWindowSize()

You can access a series of properties with the help of this hook, that you can use just as you like. This is recommended for more advanced / complex solutions compared to MatchMedia usage.

Example:

import { FC } from "react";
import { useWindowSize } from "react-breakpoint-utils";

export const MyTestPage: FC = () => {
  const {
    width,
    height,
    isXs,
    isSm,
    isMd,
    isLg,
    isXl,
    is2Xl,
    is3Xl,
    underXs,
    underSm,
    underMd,
    underLg,
    underXl,
    under2Xl,
    under3Xl,
  } = useWindowSize();

  return null;
};


 

useBreakPoints()

You can access the globally declared breakpoint values, in case you need them. A common use case for this hook, is being combined with the MatchMedia component.

Example:

import { FC } from "react";
import { useBreakPoints } from "react-breakpoint-utils";

export const MyTestPage: FC = () => {
  const { XS, SM, MD, LG, XL, XXL, XXXL } = useBreakPoints();

  return null;
};


 

withWindowSize()

There is also a HOC version for useWindowSize, you can use it together with the WithWindowSizeInjectedProps interface in TypeScript. It will provide the same values as the useWindowSize hook, but as props.
 

withBreakPoints()

There is also a HOC version for useBreakPoints, you can use it together with the WithBreakPointsInjectedProps interface in TypeScript. It will provide the same values as the useBreakPoints hook, but as props.