useTouchDevice.ts 766 B

1234567891011121314151617181920212223242526272829
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { tryOnScopeDispose } from '@vueuse/shared'
  3. import { ref } from 'vue'
  4. export const useTouchDevice = () => {
  5. const isTouchDevice = ref(false)
  6. if (window) {
  7. let mediaQuery: MediaQueryList
  8. const observeCallback = () => {
  9. isTouchDevice.value = window.matchMedia('(pointer: coarse)').matches
  10. mediaQuery?.removeEventListener('change', observeCallback)
  11. mediaQuery = window.matchMedia('(pointer: coarse)')
  12. mediaQuery.addEventListener('change', observeCallback, { once: true })
  13. }
  14. observeCallback()
  15. tryOnScopeDispose(() => {
  16. mediaQuery?.removeEventListener('change', observeCallback)
  17. })
  18. }
  19. return {
  20. isTouchDevice,
  21. }
  22. }