Ocelot-Social/webapp/mixins/touchDevice.js

24 lines
621 B
JavaScript

export default {
data() {
const pointerQuery =
typeof window !== 'undefined' ? window.matchMedia('(pointer: coarse)') : null
return {
isTouchDevice: pointerQuery ? pointerQuery.matches : false,
pointerQuery,
}
},
mounted() {
if (this.pointerQuery) {
this.onPointerChange = (e) => {
this.isTouchDevice = e.matches
}
this.pointerQuery.addEventListener('change', this.onPointerChange)
}
},
beforeDestroy() {
if (this.pointerQuery && this.onPointerChange) {
this.pointerQuery.removeEventListener('change', this.onPointerChange)
}
},
}