-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1488 from PrefectHQ/virtual-scroller-scroll-item-…
…into-view Add the ability to scroll an item into view in the virtual scroller
- Loading branch information
Showing
5 changed files
with
186 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,56 @@ | ||
<template> | ||
<ComponentPage title="Virtual Scroller" :demos="[{ title: 'Virtual Scroller' }]"> | ||
<template #virtual-scroller> | ||
<p-number-input v-model="itemCount" /> | ||
|
||
<div class="virtual-scroller_demo"> | ||
<p-virtual-scroller :items="items"> | ||
<template #default="{ item }"> | ||
<div> | ||
{{ item }} | ||
</div> | ||
</template> | ||
</p-virtual-scroller> | ||
</div> | ||
<p-content> | ||
<p-number-input v-model="itemCount" /> | ||
|
||
<div class="virtual-scroller_demo"> | ||
<p-virtual-scroller :items name="demo-scroller" :item-estimate-height="24"> | ||
<template #default="{ item, id }"> | ||
<div :id> | ||
{{ item }} | ||
</div> | ||
</template> | ||
</p-virtual-scroller> | ||
</div> | ||
|
||
<div class="flex gap-2"> | ||
<p-number-input v-model="itemToScrollTo" /> | ||
<p-button @click="scrollToItem"> | ||
Scroll to item | ||
</p-button> | ||
<p-checkbox v-model="smooth" label="smooth" /> | ||
</div> | ||
</p-content> | ||
</template> | ||
</ComponentPage> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { getVirtualScroller } from '@/components/VirtualScroller' | ||
import { computed, ref } from 'vue' | ||
import ComponentPage from '@/demo/components/ComponentPage.vue' | ||
const itemCount = ref(100) | ||
const itemCount = ref(1000) | ||
const items = computed(() => new Array(itemCount.value).fill(null).map((item, index) => ({ | ||
id: index, | ||
label: `item #${index}`, | ||
}))) | ||
const itemToScrollTo = ref<number>(800) | ||
const smooth = ref(true) | ||
function scrollToItem(): void { | ||
const scroller = getVirtualScroller('demo-scroller') | ||
scroller.scrollItemIntoView(itemToScrollTo.value, { behavior: smooth.value ? 'smooth' : 'auto' }) | ||
} | ||
</script> | ||
|
||
<style> | ||
.virtual-scroller_demo { @apply | ||
border | ||
overflow-auto | ||
max-h-52 | ||
mt-2 | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const scrollers = new Map<string, UseVirtualScroller>() | ||
|
||
type UseVirtualScroller = { | ||
makeItemVisible: (itemKey: unknown) => void, | ||
scrollItemIntoView: (itemKey: unknown, options?: ScrollIntoViewOptions) => void, | ||
} | ||
|
||
export function registerVirtualScroller(name: string, scroller: UseVirtualScroller): void { | ||
scrollers.set(name, scroller) | ||
} | ||
|
||
export function unregisterVirtualScroller(name: string): void { | ||
scrollers.delete(name) | ||
} | ||
|
||
export function getVirtualScroller(name: string): UseVirtualScroller { | ||
|
||
const makeItemVisible: UseVirtualScroller['makeItemVisible'] = (itemKey) => { | ||
const scroller = scrollers.get(name) | ||
|
||
return scroller?.makeItemVisible(itemKey) | ||
} | ||
|
||
const scrollItemIntoView: UseVirtualScroller['scrollItemIntoView'] = (itemKey, options) => { | ||
const scroller = scrollers.get(name) | ||
|
||
return scroller?.scrollItemIntoView(itemKey, options) | ||
} | ||
|
||
return { | ||
makeItemVisible, | ||
scrollItemIntoView, | ||
} | ||
} | ||
|
||
export function scrollIntoView(target: Element, options?: ScrollIntoViewOptions): Promise<void> { | ||
const { promise, resolve } = Promise.withResolvers<void>() | ||
|
||
const observer = new IntersectionObserver((entries) => { | ||
const [entry] = entries | ||
|
||
if (entry.isIntersecting) { | ||
observer.unobserve(target) | ||
observer.disconnect() | ||
|
||
setTimeout(() => { | ||
resolve() | ||
}, 100) | ||
} | ||
}) | ||
|
||
observer.observe(target) | ||
|
||
target.scrollIntoView(options) | ||
|
||
return promise | ||
} |