Skip to content

Commit 7f6cee2

Browse files
authored
feat: active navigation item (#125)
1 parent 2dffa74 commit 7f6cee2

File tree

6 files changed

+1511
-167
lines changed

6 files changed

+1511
-167
lines changed

apps/storefront-telegram/app/components/catalog/CategoriesSliderMenu.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<template>
2-
<div class="z-40 sticky top-0 h-fit tg-bg-secondary">
2+
<div class="z-40 sticky top-0 h-fit tg-content-safe-area-top tg-bg-secondary">
33
<div class="max-w-full overflow-x-scroll snap-x">
4-
<div class="w-max flex flex-row flex-wrap gap-5">
4+
<div class="py-1 w-max flex flex-row flex-wrap gap-1">
55
<div
66
v-for="category in menuStore.menu?.categories"
7+
:id="`to-${category.slug}`"
78
:key="category.id"
8-
class="scroll-ml-6 snap-start"
9+
class="scroll-ml-24 snap-start text-muted rounded-md"
10+
:class="{ 'tg-text-button bg-primary': visibleCategory === category.slug }"
911
>
10-
<button class="py-2" @click="scrollToCategory(category.slug)">
12+
<button class="px-3 py-1" @click="scrollToCategory(category.slug)">
1113
{{ category.name }}
1214
</button>
1315
</div>
@@ -18,6 +20,7 @@
1820

1921
<script setup lang="ts">
2022
const { vibrate } = useFeedback()
23+
const { visibleCategory } = useCatalog()
2124
2225
const menuStore = useMenuStore()
2326

apps/storefront-telegram/app/components/catalog/CategoryBlock.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<template>
2-
<div class="flex flex-col gap-3 mb-10">
3-
<h2 :id="category?.slug" class="scroll-mt-14 text-2xl/5 font-semibold tracking-tight">
2+
<div
3+
v-element-visibility="[
4+
(isVisible) => (isVisible && category?.slug) ? visibleCategory = category.slug : null,
5+
observerOptions,
6+
]"
7+
class="flex flex-col gap-3 mb-10"
8+
>
9+
<h2 :id="category?.slug" class="scroll-mt-22 text-2xl/5 font-semibold tracking-tight">
410
{{ category?.name }}
511
</h2>
612

@@ -16,10 +22,14 @@
1622
</template>
1723

1824
<script setup lang="ts">
25+
import { vElementVisibility } from '@vueuse/components'
26+
1927
const { categoryId } = defineProps<{
2028
categoryId: string
2129
}>()
2230
31+
const { visibleCategory, observerOptions } = useCatalog()
32+
2333
const menuStore = useMenuStore()
2434
const category = menuStore.menu?.categories.find((c) => c.id === categoryId)
2535
const products = category?.products.filter((p) => p.isAvailableForPurchase && p.variants.length)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function _useCatalog() {
2+
const visibleCategory = ref<string | null>(null)
3+
4+
const observerOptions = { rootMargin: '0px 0px -400px 0px' }
5+
6+
const scrollToCategory = useDebounceFn(() => {
7+
const category = window.document.getElementById(`to-${visibleCategory.value}`)
8+
category?.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'start' })
9+
}, 1000)
10+
11+
watch(visibleCategory, () => scrollToCategory())
12+
13+
return {
14+
visibleCategory,
15+
observerOptions,
16+
}
17+
}
18+
19+
export const useCatalog = createSharedComposable(_useCatalog)

packages/ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@nuxtjs/i18n": "catalog:",
1414
"@unovis/ts": "catalog:",
1515
"@unovis/vue": "catalog:",
16+
"@vueuse/components": "catalog:",
1617
"@vueuse/core": "catalog:",
1718
"@vueuse/integrations": "catalog:",
1819
"@vueuse/nuxt": "catalog:",

0 commit comments

Comments
 (0)