Skip to content

Commit a837e8a

Browse files
authored
Feature: ismobile composble (#6)
* Composable to check if device is mobile * Show Floating Cart Button conditionally
1 parent babc035 commit a837e8a

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { throttle } from 'lodash-es'
2+
import { onBeforeUnmount, onMounted, ref } from 'vue'
3+
4+
export const useIsMobile = (breakpoint: number = 768) => {
5+
const isMobile = ref<boolean>(window.innerWidth < breakpoint)
6+
const handleResize = throttle(() => {
7+
isMobile.value = window.innerWidth < breakpoint
8+
}, 200)
9+
10+
onMounted(() => {
11+
window.addEventListener('resize', handleResize)
12+
})
13+
onBeforeUnmount(() => {
14+
window.removeEventListener('resize', handleResize)
15+
})
16+
return { isMobile }
17+
}

apps/web/src/pages/products.page.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@
349349
</div>
350350

351351
<!-- Floating Cart Button (Mobile Only) -->
352-
<button @click="scrollToCart"
352+
<button v-if="!isMobile || (!sidebarOpen && isMobile)" @click="scrollToCart"
353353
class="fixed bottom-6 right-6 xl:hidden w-16 h-16 bg-emerald-600 hover:bg-emerald-700 rounded-full shadow-2xl flex items-center justify-center cursor-pointer transition-all duration-300 hover:scale-110 z-50">
354354
<svg class="w-7 h-7 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
355355
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
@@ -374,10 +374,12 @@ import { useAuthStore } from '@/stores/auth.store'
374374
import { useI18n } from 'vue-i18n'
375375
import { setLocale } from '@/plugins/i18n'
376376
import type { Locale } from '@/locales'
377+
import { useIsMobile } from '@/composables/isMobile.composable'
377378
378379
// Cart store
379380
const cart = useCartStore()
380381
const authStore = useAuthStore()
382+
const { isMobile } = useIsMobile()
381383
const { t } = useI18n()
382384
383385
// Sidebar

0 commit comments

Comments
 (0)