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-viewport-hook

v1.0.7

Published

React hook to return configured device viewport type with help of a context provider

Downloads

22

Readme

yarn add react-viewport-hook

React hook and context provider to give the current defined viewport type based on the current device screen size or when there is a screen resize. This is useful for scenarios like adaptative react pages where we want to render different components for different viewports and CSS is not a good option (like a dropdown for desktop and a modal for phone).

By default this works with the viewport types:

  • phone: from 0px to 479px;
  • tablet: from 480px to 767px;
  • desktop: from 768px;

But you can customize the viewports to use on the provider component.

🕹️ Playground

Components

ViewportProvider

This component updates the viewport type when the screen hits a viewport breakpoint.

Important! Your app should only have one viewport provider on the react tree.

Props

  • initialViewportType: desktop by default, this is useful when using it with SSR (server side rendering) and we want to initially use a viewport type based not on the screen size but on the user agent;
  • customViewportTypes: list of custom viewport types;
  • children: app component;

Usage

When using it with client side or the server always renders the same viewport, you can use as:

  import { ViewportProvider } from 'react-viewport-hook';

  const MainApp = () => (
    <ViewportProvider>
      <App />
    </ViewportProvider>
  );

When using with SSR:

  import { ViewportProvider } from 'react-viewport-hook';

  const MainApp = () => (
    <ViewportProvider initialViewportType={serverViewportBasedOnUserAgent}>
      <App />
    </ViewportProvider>
  );

When customizing the viewport types:

  import { ViewportProvider } from 'react-viewport-hook';

  return (
    <ViewportProvider customViewportTypes={[
      {
        viewportType: 'smallPhone',
        minWith: 0,
        maxWith: 199,
      },
      {
        viewportType: 'others',
        minWith: 200,
      }
    ]>
      <App />
    </ViewportProvider>
  );

Hook

This component reads the current viewport.

Usage

  import useViewportType from 'react-viewport-hook';

  const AdaptativeComponent = () => {
    const { viewportType, isPhone, isTablet, isDesktop } = useViewportType();

    if (isPhone) {
      return (<PhoneComponent />);
    } else if (isTablet) {
      return (<TabletComponent />);
    }

    return (<DesktopComponent />);
  }

Full Example

  import useViewportType, { ViewportProvider } from 'react-viewport-hook';

  const AdaptativeComponent = () => {
    const { isPhone } = useViewportType();

    return isPhone ? <PhoneComponent /> : <DesktopComponent />;
  }

  const Component = () => (
    <ViewportProvider>
      <AdaptativeComponent />
    </ViewportProvider>
  );

Install

yarn install

Build

yarn build

TODO

  • Add unit tests
  • Upgrade to typescript

Author

👤 KennyPT [email protected]

Show your support

Give a ⭐️ if this project helped you!