Skip to content

Commit

Permalink
fix: useDevicePixelRatio - implements useMediaQuery (vueuse#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
anteriovieira committed Nov 4, 2020
1 parent 7e7c3e5 commit cb4ef58
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions packages/core/useDevicePixelRatio/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ref, onUnmounted } from 'vue-demi'
import { ref, watch } from 'vue-demi'
import { useEventListener } from '../useEventListener'
import { useMediaQuery } from '../useMediaQuery'
import { isClient, tryOnMounted } from '@vueuse/shared'

// device pixel ratio statistics from https://www.mydevice.io/
Expand Down Expand Up @@ -37,15 +38,11 @@ export function useDevicePixelRatio() {

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

mqlMin.addEventListener('change', handleDevicePixelRatio)
mqlMax.addEventListener('change', handleDevicePixelRatio)

onUnmounted(() => {
mqlMin.removeEventListener('change', handleDevicePixelRatio)
mqlMax.removeEventListener('change', handleDevicePixelRatio)
watch([mqlMin, mqlMax], () => {
handleDevicePixelRatio()
})
})

Expand Down

0 comments on commit cb4ef58

Please sign in to comment.