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

@drawbotics/use-screen-size

v2.0.0

Published

Hook used to get the current screen size for responsive behaviour

Downloads

12

Readme

Get Screen Size Hook

This hook will use the breakpoints defined in @drawbotics/drylus-style-vars to identify screen size where the app is running.

Installation

$ npm install @drawbotics/use-screen-size @drawbotics/drylus-style-vars

Usage

import { useScreenSize } from '@drawbotics/use-screen-size';


const App = ({ children }) => {
  const { screenSize, ScreenSizes } = useScreenSize();
  return (
    <Page>
      {do {
        if (screenSize >= ScreenSizes.M) {
          <Header />
        }
      }}
      {do {
        if (screenSize < ScreenSizes.S) {
          <Sidebar />
        }
      }}
      <Content>
        {children}
      </Content>
    </Page>
  )
};


export default App;

Api

The hook returns two properties screenSize and ScreenSizes:

  • screenSize is equivalent to the current size of the screen following the current media query matching the size
  • ScreenSizes is a constant with the screen size value matching its size definition:
const ScreenSizes = {
  XS: 1,
  S: 2,
  M: 3,
  L: 4,
  XL: 5,
};

This allows you to mimic the CSS way of writing queries, but in a more verbose way in JavaScript. You can use the comparative operators <,=,> to determine what should be rendered on the screen.

If you want to render something when the screen is smaller or equal to a small size:

  if (screenSize <= ScreenSizes.S) {
    // Content for small screens and lower
  }
  else {
    // Content for screens larger than small
  }

You can also target specific sizes with the === operator:

  if (screenSize === ScreenSizes.L) {
    // Only render this content when the screen is L (smaller than XL and larger than S)
  }

For anything larger than XL you should use the following condition:

  if (screnSize > ScreenSizes.XL) {
    // Content for big screens
  }

### Order of conditions As with CSS @media queries, you have to check the screen size in the same order, i.e. from largest to smallest, otherwise the first one will always apply. This is because a small screen still falls within a large one, but not vice versa.


There's also another utility function called getScreenSize that returns exactly the contents of the value returned by the hook (properties of screenSize) (it's actually used internally in the hook) but that won't update the value based on the resize events.

Example:

import { getScreenSize } from '@drawbotics/use-screen-size';

console.log(getScreenSize());

// on medium screen prints
3