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-device-pixel-ratio

v1.1.2

Published

React hook for finding device pixel ratio (DPR), optionally capping/rounding

Downloads

496,275

Readme

use-device-pixel-ratio

github status checks bundlephobia

useDevicePixelRatio() is a React hook (and utility) that will tell you what the current device has as its Device Pixel Ratio (DPR). The hook is reactive - if the browser window moves to a different display with a different DPR, it will update automatically. If you only need to get the DPR statically, there is a function (getDevicePixelRatio()) equivalent.

Installing

npm i use-device-pixel-ratio

Server rendering

When rendering on the server or in browsers that do not support the devicePixelRatio property, it will default to 1 unless overriden by using the defaultDpr option.

Rounding/limiting

The hook (by default) both rounds and limits the DPR - it will round down by default, and cap the number at 3. In other words, you should by default only have three values returned: 1, 2 or 3. To allow larger DPRs, pass a higher number to the maxDpr option. To prevent rounding, pass round: false.

Usage

Default usage

import {useDevicePixelRatio} from 'use-device-pixel-ratio'

async function MyComponent() {
  const dpr = useDevicePixelRatio()
  return <img src={`https://my.image.host/file.jpg?dpr=${dpr}`}>
}

Setting higher limit

import {useDevicePixelRatio} from 'use-device-pixel-ratio'

async function MyComponent() {
  const dpr = useDevicePixelRatio({maxDpr: 50})
  return <div>DPR is {dpr}</div>
}

Getting the "raw" DPR

import {useDevicePixelRatio} from 'use-device-pixel-ratio'

async function MyComponent() {
  const dpr = useDevicePixelRatio({maxDpr: +Infinity, round: false})
  return <div>DPR is {dpr}</div>
}

Setting the default DPR

import {useDevicePixelRatio} from 'use-device-pixel-ratio'

async function MyComponent() {
  const dpr = useDevicePixelRatio({defaultDpr: 2})

  // Actual device DPR if available, 2 otherwise
  return <div>DPR is {dpr}</div>
}

Function usage

import {getDevicePixelRatio} from 'use-device-pixel-ratio'

console.log('Device pixel ratio is ', getDevicePixelRatio())

// Or, with options (same options as hook):
console.log('Device pixel ratio is ', getDevicePixelRatio({maxDpr: 5}))

License

MIT © Espen Hovlandsdal