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

v1.2.1

Published

Use breakpoints in JS when dealing with React based applications

Downloads

724

Readme

react-use-breakpoint

Rational

A React hook to get the current breakpoint via the MatchMedia API. Tiny, fast, and easy to use. I created this as I found myself copying this bad boy around every project I did... so I figured I'd make it a package. Other plugins out there are not exactly what I needed, and although I think getting breakpoints in JS should be a last resort. There is undoubtedly a time and place for it. Especially as designers design more creative layouts that are not easily achievable with CSS alone.

Getting Started

Install the package

npm install react-use-breakpoint
yarn add react-use-breakpoint

Wrap your apps root component with the BreakpointProvider component. This will provide the useBreakpoint hook to all child components.

import {BreakpointProvider} from 'react-use-breakpoint';

const App = () => (
    <BreakpointProvider>
        <MyComponent/>
    </BreakpointProvider>
);

The default breakpoint size values are:

| Breakpoint | Size (px) | |------------|-----------| | xs | 360 | | sm | 640 | | md | 960 | | lg | 1280 | | xl | 1440 |

You can customise these values xs, sm, md, lg, xl to fit your needs like so (strings or numbers are accepted):

import {BreakpointProvider} from 'react-use-breakpoint';

const App = () => (
    <BreakpointProvider
        breakpointOverrides={{
            xs: 360,
            sm: '400'
        }}
    >
        <MyComponent/>
    </BreakpointProvider>
);

Hook Usage

Within all child components you can then leverage the useBreakpoint() hook to get the current breakpoints.

Specifying no arguments will return an object with min and max breakpoints { min: {...}, max: {...} }. The function overload handles the types for you 😉.

import {useBreakpoint} from 'react-use-breakpoint';

const MyComponent = () => {
    const {min} = useBreakpoint();
    const {isXs} = min;

    return <div>Current breakpoint: {breakpoint}</div>;
};

If you specify a breakpoint "direction" (min or max) you will get an object back with all the breakpoint sizes and their assigned boolean value.

import {useBreakpoint} from 'react-use-breakpoint';

const MyComponent = () => {
    const {isXs, isSm, isMd} = useBreakpoint('min');
    // isXs = true
    // isSm = false
    // isMd = false
...
    return <div>Current breakpoint: {breakpoint}</div>;
};

Feature Requests...

Sure... open up an issue, and I'll see what I can do.

Todo

  • [ ] Add tests for the hook