Skip to content

Commit

Permalink
⚡️ keep scroll position on ownerInsights tab change
Browse files Browse the repository at this point in the history
  • Loading branch information
daiagi committed Apr 12, 2023
1 parent 1025862 commit d021350
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
26 changes: 23 additions & 3 deletions components/collection/activity/OwnerInsights.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
<div
class="mr-4 is-clickable"
:class="{ 'has-text-weight-bold': activeTab === Tabs.holders }"
@click="activeTab = Tabs.holders">
@click="changeTab(Tabs.holders)">
{{ $t('holders') }}
</div>
<div
class="is-clickable"
:class="{ 'has-text-weight-bold': activeTab === Tabs.flippers }"
@click="activeTab = Tabs.flippers">
@click="changeTab(Tabs.flippers)">
{{ $t('flippers') }}
</div>
</div>
<div class="py-4 limit-height is-scrollable">
<div ref="container" class="py-4 limit-height is-scrollable">
<HoldersTab v-if="activeTab === Tabs.holders" :owners="owners" />
<FlippersTab v-if="activeTab === Tabs.flippers" :flippers="flippers" />
</div>
Expand All @@ -36,6 +36,26 @@ defineProps<{
flippers?: Flippers
}>()
const activeTab = ref(Tabs.holders)
const container = ref<HTMLDivElement | null>(null)
const scrollPositions = {
[Tabs.holders]: 0,
[Tabs.flippers]: 0,
}
const changeTab = (tab: Tabs) => {
// Store the current scroll position
if (container.value) {
scrollPositions[activeTab.value] = container.value.scrollTop
}
// Set the new active tab and restore the previous scroll position
// this is done in order to optimize performance and prevent unnecessary loading
// since HolderTab and FlippersTab are lazy loaded
activeTab.value = tab
if (container.value) {
container.value.scrollTop = scrollPositions[tab]
}
}
</script>

<style lang="scss" scoped>
Expand Down
15 changes: 7 additions & 8 deletions components/collection/activity/ownerInsightsTabs/FlipperTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ import { format } from '@/components/collection/activity/utils'
import NFTsDetaislDropdown from './NFTsDetaislDropdown.vue'
import { timeAgo } from '@/components/collection/utils/timeAgo'
import { Flippers } from '@/composables/collectionActivity/types'
const props = defineProps<{
flippers?: Flippers
}>()
const target = ref<HTMLElement | null>(null)
const offset = ref(4)
const flippers = computed(() => Object.entries(props.flippers || {}))
const toggleNFTDetails = (flipperId: string) => {
const isOpen = isNFTDetailsOpen.value[flipperId]
Expand All @@ -87,10 +94,6 @@ const toggleNFTDetails = (flipperId: string) => {
[flipperId]: !isOpen,
}
}
const target = ref<HTMLElement | null>(null)
const offset = ref(4)
const flippers = computed(() => Object.entries(props.flippers || {}))
useIntersectionObserver(target, ([{ isIntersecting }]) => {
if (isIntersecting) {
Expand All @@ -109,10 +112,6 @@ const isFlipperMoreNFTSectionOpen = flippers.value.reduce(
{}
)
const isNFTDetailsOpen = ref(isFlipperMoreNFTSectionOpen)
const props = defineProps<{
flippers?: Flippers
}>()
</script>

<style lang="scss" scoped>
Expand Down

0 comments on commit d021350

Please sign in to comment.