New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

use-device-pixel-ratio

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-device-pixel-ratio

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

  • 1.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

Keywords

FAQs

Package last updated on 13 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc