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-ui-breakpoints

v1.0.1

Published

Easily add media breakpoints to your react project

Downloads

17

Readme

react-ui-breakpoints (RUB) npm

Easily add media breakpoints to your react components with the useScreen hook!

Installation

npm i react-ui-breakpoints

Introduction

React is a powerful, component-based, tool that streamlines the process of developing clean readable website code! And through the use of React Hooks, developers can easily add and create additional functionalities which aren't already in React! React UI Breakpoints (RUB) is one of these functionalities! It allows a clean and simple system for managing media breakpoints in your React project, allowing you to deliver readable, well-organized code which dynamically changes as users resize various aspects of a website.

Getting Started

To get started, install RUB in your project.

npm i react-ui-breakpoints

RUB contains two hooks you can use to build screen breakpoints; useScreen and useView. Attach them to the return of a component to make it dynamic! You can also define if you want your breakpoints to operate in Desktop first or Mobile first mode using the QueryMode enum! (Hint: Modern website development should always use Mobile first.)

const ExampleComponent = () => {
    const MobileView = () => (
        <div className='text-left text-neutral-100 text-6xl'>
            <span>Some text</span>
        </div>
    );
    const TabletView = () => (
        <div className='text-left text-neutral-100 text-6xl'>
            <span>Some text</span>
        </div>
    );
    const DesktopView = () => (
        <div className='text-center text-neutral-100 text-lg'>
            <span>Some text</span>
        </div>
    );

    return useScreen(
        QueryMode.MOBILE_FIRST,
        useView('1000px', DesktopView());
        useView('700px', TabletView());
        useView("default", MobileView());
    );
}

The component above is what we call a dynamic component. Instead of directly returning JSX, we hold multiple views and then return them as the breakpoints are met!

Let's also review a version that has state:

const ExampleComponent = () => {
    const [ something, setSomething ]: [ string, Function ] = useState('');

    useEffect(() => setSomething('Hello world!'),[]);

    const MobileView = () => (
        <div className='text-left text-neutral-100 text-6xl'>
            <span>{something}</span>
        </div>
    );
    const TabletView = () => (
        <div className='text-left text-neutral-100 text-6xl'>
            <span>{something}</span>
        </div>
    );
    const DesktopView = () => (
        <div className='text-center text-neutral-100 text-lg'>
            <span>{something}</span>
        </div>
    );

    return useScreen(
        QueryMode.MOBILE_FIRST,
        useView('1000px', DesktopView());
        useView('700px', TabletView());
        useView("default", MobileView());
    );
}

As you can see, we don't save state inside of the views, instead, we hold state in the parent component and then reference that state inside of our views!

Auto-resizing

RUB will only ever update when the component itself updates. If you want the components to update automatically on screen-resizing. You can attach the useHookOntoScreen React hook! This hook will cause your component to re-render anytime the viewport is resized. If you already have viewport specific state in your component. Adding this hook is unnecessary.

const ExampleComponent = () => {
    const autoResize = useHookOntoScreen();

    const MobileView = () => (
        <div className='text-left text-neutral-100 text-6xl'>
            <span>Some text</span>
        </div>
    );
    const TabletView = () => (
        <div className='text-left text-neutral-100 text-6xl'>
            <span>Some text</span>
        </div>
    );
    const DesktopView = () => (
        <div className='text-center text-neutral-100 text-lg'>
            <span>Some text</span>
        </div>
    );

    return useScreen(
        QueryMode.MOBILE_FIRST,
        useView('1000px', DesktopView());
        useView('700px', TabletView());
        useView("default", MobileView());
    );
}

Disclaimer

It can be easy to misuse the features that RUB gives you. Making hundreds of breakpoints instead of properly designing your webpage for resizing is not suggested. RUB Breakpoints should only be considered when you have major page elements that you want to add or remove between different viewports.