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

Lazy load widgets #509

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading