Skip to content

Commit

Permalink
Merge pull request #627 from Adamant-im/feat/hook-is-mobile-reactive
Browse files Browse the repository at this point in the history
feat: hook isMobile
  • Loading branch information
bludnic authored Jun 4, 2024
2 parents 8c9e7c9 + a8cc9da commit a5b6d80
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/components/EmojiPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useTheme } from '@/hooks/useTheme'
import axios from 'axios'
import { defineComponent, onMounted, PropType, ref } from 'vue'
import { Picker } from 'emoji-mart'
import { isMobile } from '@/lib/display-mobile'
import { useIsMobile } from '@/hooks/useIsMobile'
const className = 'emoji-picker'
const classes = {
Expand All @@ -34,6 +34,7 @@ export default defineComponent({
},
emits: ['emoji:select'],
setup(props, { emit }) {
const isMobile = useIsMobile()
const { isDarkTheme } = useTheme()
const container = ref<HTMLElement>()
const picker = ref<Picker>()
Expand All @@ -43,7 +44,7 @@ export default defineComponent({
picker.value = new Picker({
data,
autoFocus: !isMobile(), // disable autofocus on mobile devices
autoFocus: !isMobile.value, // disable autofocus on mobile devices
dynamicWidth: true,
navPosition: 'none',
previewPosition: 'none',
Expand Down
30 changes: 30 additions & 0 deletions src/hooks/useIsMobile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { detect } from 'detect-browser'
import { ref, onMounted, onUnmounted } from 'vue'

export function useIsMobile() {
const isMobile = ref(false)

const checkIsMobile = () => {
const browser = detect()
const isMobileDevice =
browser && browser.os
? ['android os', 'ios', 'blackberry os'].includes(browser.os.toLowerCase())
: false
return isMobileDevice || window.innerWidth < 450
}

const handleResize = () => {
isMobile.value = checkIsMobile()
}

onMounted(() => {
isMobile.value = checkIsMobile()
window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
window.removeEventListener('resize', handleResize)
})

return isMobile
}

0 comments on commit a5b6d80

Please sign in to comment.