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

use-window-orientation

v1.0.3

Published

A React hook for using window orientation.

Downloads

1,474

Readme

useWindowOrientation

npm version npm peer dependency version npm bundle size License: Apache-2.0 Contributor Covenant

Sometimes, just knowing the window width isn't enough. Sometimes you want to know if the window's orientation is portrait or landscape. Good thing you found this React hook.

npm install use-window-orientation
# OR
yarn add use-window-orientation

After importing the hook...

import useWindowOrientation from "use-window-orientation";

...call it from the top level of your React function.

const { orientation, portrait, landscape } = useWindowOrientation();

This hook returns an object with three properties, each describing the current orientation of the window in a different way.

  • orientation will be either "portrait" or "landscape"
  • portrait will be either true or false
  • landscape will be either false or true

The easiest way to access these properties is by using object destructuring, as in the above example. One advantage of this method is that you only have to declare variables for the properties you actually want to use. Only plan on using the portrait boolean in your code? Then just call the hook like this:

const { portrait } = useWindowOrientation();

What's this hook good for? Say you have two components, Chart and Explanation. You want Explanation to come first if the window is portrait, but you want Chart to come first if the window is landscape. Then arrange them in your JSX like this:

{portrait && <Explanation />}
<Chart />;
{landscape && <Explanation />}

Or say you want to creep out your users by divining the orientation of their window:

<p>Well, your window is {orientation} right now, so you leave me no choice.</p>

The possibilities are endless.

This hook has one optional parameter: an options object. There is currently only one option, defaultOrientation, the default orientation you'd like to return if no window exists (such as if a search engine is crawling your page). Valid defaultOrientations are "portrait" or "landscape", and if you omit the option, it will default to "portrait".

const { orientation, portrait, landscape } = useWindowOrientation({
  defaultOrientation: "portrait",
});

This hook only deals with the window orientation, not the device orientation. It calculates this orientation using window.innerWidth and window.innerHeight. It does not consult window.orientation at all because that feature has been deprecated.

Also, in the rare case that the window's width and height are equal, useWindowOrientation will just report the orientation as portrait.

If you'd like to contribute to this project (which would be awesome), the easiest way to set it up would be to install the GitHub CLI and then run the following:

gh repo fork tywmick/use-window-orientation --clone=true
cd use-window-orientation
npm install

Now, you can build the package with npm run build, build and watch for changes with npm run dev (automatically rebuilding on each change in the source), run the test suite with npm run test, and create pull requests with gh pr create.

After building the package, you can test it in another project on your machine by adding the local path as a dependency (e.g., by running npm install /path/to/local/use-window-orientation in that other project).