Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Lazy load widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbo committed Oct 24, 2024
1 parent eb6cda5 commit 0600017
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions domains/grid/components/GridWidget.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computedAsync } from '@vueuse/core'
import { computedAsync, useIntersectionObserver } from '@vueuse/core'
import { z } from 'zod'
import type { LuksoDropdownOnChangeEventDetail } from '@lukso/web-components'
Expand All @@ -20,6 +20,8 @@ const { connect } = useBaseProvider()
const { browserSupportExtension } = useBrowser()
const dropdownId = `dropdown-${generateItemId()}`
const isOpen = ref<boolean | undefined>(undefined)
const targetIsVisible = ref(false)
const target = ref<HTMLElement | null>(null)
const isAllowToEdit = computed(
() => canEditGrid.value && !isAddContentWidget.value
Expand Down Expand Up @@ -146,11 +148,24 @@ const handleDropdownChange = (
onMounted(() => {
widgetComponent.value = loadWidgetComponent(props.widget.type)
setTimeout(() => {
useIntersectionObserver(
target,
([{ isIntersecting }], _observerElement) => {
targetIsVisible.value = targetIsVisible.value || isIntersecting
},
{
rootMargin: '100px', // load margin before target appear in viewport
}
)
}, 1)
})
</script>

<template>
<div
ref="target"
class="group relative flex h-full flex-col rounded-12"
:class="{
'border border-neutral-90 bg-neutral-100 shadow-neutral-drop-shadow-1xl':
Expand Down Expand Up @@ -266,7 +281,7 @@ onMounted(() => {

<!-- Loaded component based on widget type -->
<component
v-if="widgetComponent"
v-if="widgetComponent && targetIsVisible"
:is="widgetComponent"
v-bind="widget.properties"
:widget="widget"
Expand Down

0 comments on commit 0600017

Please sign in to comment.