Infinite Scroll

Doomscrolling, now as a component. Loads more content as you scroll near an edge, in any of four directions.

Infinite scroll

Item 1

Item 2

Item 3

Item 4

Item 5

Item 6

Item 7

Item 8

Item 9

Item 10

Item 11

Item 12

Item 13

Item 14

Item 15

Item 16

Item 17

Item 18

Item 19

Item 20

Item 21

Item 22

Item 23

Item 24

Item 25

Item 26

Item 27

Item 28

Item 29

Item 30

Installation

Comes with the scroll area.

Usage

<script setup lang="ts">
import { InfiniteScroll, ScrollArea } from '@/components/ui/scroll-area'

const load = async ({ index }: { index: number }) => {
  const page = await fetchPage(index)
  items.value = [...items.value, ...page.items]
  if (page.last) return 'stop'
}
</script>

<template>
  <ScrollArea class="h-72">
    <InfiniteScroll :on-load="load">
      <p v-for="item in items" :key="item.id">{{ item.text }}</p>
    </InfiniteScroll>
  </ScrollArea>
</template>

Scroll within offset pixels of the edge, and onLoad gets called with { direction, index }. index starts at 1, so it doubles as a page number. It won't fire again until the promise settles.

Out of content? Return 'stop'. To start again, call resume() or reset().

If the promise throws, the next scroll retries the same page, and the error goes to app.config.errorHandler.

Examples

Reverse

directions="['top']" loads older content at the top, like a chat. Your reading position stays put while new content appears above.

Scroll the area to the bottom before turning loading on, or it hits the top edge right on mount.

Infinite scroll, reverse

Message 1

Message 2

Message 3

Message 4

Message 5

Message 6

Message 7

Message 8

Message 9

Message 10

Message 11

Message 12

Message 13

Message 14

Message 15

Message 16

Message 17

Message 18

Message 19

Message 20

Message 21

Message 22

Message 23

Message 24

Message 25

Message 26

Message 27

Message 28

Message 29

Message 30

Horizontal

start and end load sideways. In right-to-left pages, they flip.

Infinite scroll, horizontal
1
2
3
4
5
6
7
8
9
10
11
12

Both directions

Each direction lives its own life: its own page count, loading state and stop. One loading doesn't block the other.

Infinite scroll, both directions

line 1000

line 1001

line 1002

line 1003

line 1004

line 1005

line 1006

line 1007

line 1008

line 1009

line 1010

line 1011

line 1012

line 1013

line 1014

line 1015

line 1016

line 1017

line 1018

line 1019

line 1020

line 1021

line 1022

line 1023

line 1024

line 1025

line 1026

line 1027

line 1028

line 1029

line 1030

line 1031

line 1032

line 1033

line 1034

line 1035

line 1036

line 1037

line 1038

line 1039

The composable

useInfiniteScroll() is the same thing without markup, for lists that draw their own loading rows. It returns the same things InfiniteScroll exposes.

const rootRef = useTemplateRef<HTMLElement>('root')
const { state, reset } = useInfiniteScroll({
  anchor: rootRef,
  directions: ['bottom'],
  onLoad: async ({ index }) => {
    /* … */
  },
})
  • target: what to watch, an element, window or a ScrollArea API. By default, the nearest scrolling parent of anchor, or the window. With a virtualized ScrollArea set to anchorTo: 'end', pass its API: the anchor then holds the rows loaded at the top in place, and the composable doesn't shift them a second time.
  • shouldLoad(direction): your own "time to load?" check instead of the pixel offset. Handy for virtualized lists that count rows. InfiniteScroll takes it as a prop too.

API Reference

Data attributes

data-slotAttributesDescription
infinite-scrolldata-orientation, aria-busyThe root. A flex row when a horizontal direction is listed.
infinite-scroll-loadingdata-direction, data-stateThe loading part at one edge, present until that direction stops. Invisible but keeping its room while idle.
infinite-scroll-enddata-directionThe end slot at one edge, rendered once that direction is stopped and the slot is given.

Accessibility

The root gets aria-busy while loading. Loading indicators are hidden from screen readers, so if new content matters to your users, announce it yourself.