Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useDevicePixelRatio): more efficient mechanism #2044

Merged
merged 3 commits into from Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/useDevicePixelRatio/demo.vue
Expand Up @@ -10,4 +10,5 @@ const code = stringify(pixelRatio)
<template>
<div>Device Pixel Ratio:</div>
<pre>{{ code }}</pre>
<span class="opacity-50">Zoom in and out (or move the window to a screen with a different scaling factor) to see the value changes</span>
</template>
2 changes: 1 addition & 1 deletion packages/core/useDevicePixelRatio/index.md
Expand Up @@ -6,7 +6,7 @@ category: Sensors

Reactively track [`window.devicePixelRatio`](https://developer.mozilla.org/ru/docs/Web/API/Window/devicePixelRatio)
>
> NOTE: there is no event listener for `window.devicePixelRatio` change. So this function uses [`Testing media queries programmatically (window.matchMedia)`](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Testing_media_queries) as described in [this example](https://stackoverflow.com/questions/28905420/window-devicepixelratio-change-listener/29653772#29653772), but unlike the example this function subscribes to **several** pixelRatio scales (taken from [mydevice.io](https://www.mydevice.io/)) to detect any `window.devicePixelRatio` change.
> NOTE: there is no event listener for `window.devicePixelRatio` change. So this function uses [`Testing media queries programmatically (window.matchMedia)`](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Testing_media_queries) applying the same mechanism as described in [this example](https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio#monitoring_screen_resolution_or_zoom_level_changes).

## Usage

Expand Down
45 changes: 12 additions & 33 deletions packages/core/useDevicePixelRatio/index.ts
@@ -1,24 +1,7 @@
import { ref, watch } from 'vue-demi'
import { useEventListener } from '../useEventListener'
import { useMediaQuery } from '../useMediaQuery'
import type { ConfigurableWindow } from '../_configurable'
import { defaultWindow } from '../_configurable'
import { ref } from 'vue-demi'
import { type Fn, tryOnScopeDispose } from '@vueuse/shared'
import { type ConfigurableWindow, defaultWindow } from '../_configurable'

// device pixel ratio statistics from https://www.mydevice.io/
const DEVICE_PIXEL_RATIO_SCALES = [
1,
1.325,
1.4,
1.5,
1.8,
2,
2.4,
2.5,
2.75,
3,
3.5,
4,
]
/**
* Reactively track `window.devicePixelRatio`.
*
Expand All @@ -34,21 +17,17 @@ export function useDevicePixelRatio({
}
}

const pixelRatio = ref(window.devicePixelRatio)
const pixelRatio = ref(1)

const handleDevicePixelRatio = () => {
let mqResolution: MediaQueryList
tryOnScopeDispose((function MqResolutionObserve(): Fn {
pixelRatio.value = window.devicePixelRatio
}

useEventListener(window, 'resize', handleDevicePixelRatio, { passive: true })

DEVICE_PIXEL_RATIO_SCALES.forEach((dppx) => {
// listen mql events in both sides
const mqlMin = useMediaQuery(`screen and (min-resolution: ${dppx}dppx)`)
const mqlMax = useMediaQuery(`screen and (max-resolution: ${dppx}dppx)`)

watch([mqlMin, mqlMax], handleDevicePixelRatio)
})
mqResolution = window.matchMedia(`(resolution: ${pixelRatio.value}dppx)`)
mqResolution.addEventListener('change', MqResolutionObserve, { once: true })
return () => {
mqResolution.removeEventListener('change', MqResolutionObserve)
}
})())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for wraping the function like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a short way to immediately invoke the function and passing removeEventListener to tryOnScopeDispose


return { pixelRatio }
}
Expand Down