1.11.0 • Published 6 months ago

vue-virtual-scroll-grid v1.11.0

Weekly downloads
221
License
MIT
Repository
github
Last release
6 months ago

Virtual Scroll Grid for Vue 3

This is a reusable component for Vue 3 that renders a list with a huge number of items (e.g. 1000+ items) as a grid in a performant way.

Features

  • Use virtual-scrolling / windowing to render the items, so the number of DOM nodes is kept low.
  • Just use CSS grid to style your grid. Minimum styling opinions form the library.
  • Support using a paginated API to load the items in the background.
  • Support rendering placeholders for unloaded items.
  • Support both vertical and horizontal scroll.
  • Loaded items are cached for better performance.

Code Examples

Install

npm install vue-virtual-scroll-grid

Available Props

NameDescriptionTypeValidation
lengthThe number of items in the listnumberRequired, an integer greater than or equal to 0
pageProviderThe callback that returns a page of items as a promise. pageNumber start with 0(pageNumber: number, pageSize: number) => Promise<unknown[]>Required
pageSizeThe number of items in a page from the item provider (e.g. a backend API)numberRequired, an integer greater than or equal to 1
pageProviderDebounceTimeDebounce window in milliseconds on the calls to pageProvidernumberOptional, an integer greater than or equal to 0, defaults to 0
probeTagThe HTML tag used as probe element. Default value is divstringOptional, any valid HTML tag, defaults to div
respectScrollToOnResizeSnap to the position set by scrollTo when the grid container is resizedbooleanOptional, defaults to false
scrollBehaviorThe behavior of scrollTo. Default value is smoothsmooth | autoOptional, a string to be smooth or auto, defaults to smooth
scrollToScroll to a specific item by indexnumberOptional, an integer from 0 to the length prop - 1, defaults to 0
tagThe HTML tag used as container element. Default value is divstringOptional, any valid HTML tag, defaults to div
getKeyThe :key used on each grid item. Auto-generated, but overwritable via function(internalItem: InternalItem) => number \| string 1Optional, any valid Function that returns a string or number

Example:

<Grid
  :length="1000"
  :pageProvider="async (pageNumber, pageSize) => Array(pageSize).fill('x')"
  :pageSize="40"
  :scrollTo="10"
>
<!-- ...slots -->
</Grid>

Available Slots

There are 3 scoped slots: default, placeholder and probe.

The default slot

The default slot is used to render a loaded item.

Props:

  • item: the loaded item that is used for rendering your item element/component.
  • index: the index of current item within the list.
  • style: the style object provided by the library that need to be set on the item element/component.

Example:

<template v-slot:default="{ item, style, index }">
  <div :style="style">{{ item }} {{ index }}</div>
</template>

Theplaceholder slot

When an item is not loaded, the component/element in the placeholder slot will be used for rendering. The placeholder slot is optional. If missing, the space of unloaded items will be blank until they are loaded.

Props:

  • index: the index of current item within the list.
  • style: the style object provided by the library that need to be set on the item element/component.

Example:

<template v-slot:placeholder="{ index, style }">
  <div :style="style">Placeholder {{ index }}</div>
</template>

The probe slot

The probe slot is used to measure the visual size of grid item. It has no prop. You can pass the same element/component for the placeholder slot. If not provided, you must set a fixed height to grid-template-rows on your CSS grid, e.g. 200px. If provided, make sure it is styled with the same dimensions as rendered items in the default or placeholder slot. Otherwise, the view wouldn't be rendered properly, or the rendering could be very slow.

Example:

<template v-slot:probe>
  <div class="item">Probe</div>
</template>

Exposed Public Properties

  • allItems: All items memoized by the grid

Scroll Mode

The library uses grid-auto-flow CSS property to infer scroll mode. Set it to column value if you want to enable horizontal scroll.

Caveats

The library does not require items have foreknown width and height, but do require them to be styled with the same width and height under a view. E.g. the items can be 200px x 200px when the view is under 768px and 300px x 500px above 768px.

Development

Required environment variables:

  • VITE_APP_ID: An Algolia app ID
  • VITE_SEARCH_ONLY_API_KEY: The search API key for the Algolia app above
  • Setup: npm install
  • Run dev server: npm run dev
  • Lint (type check): npm run lint
  • Build the library: npm run build
  • Build the demo: npm run build -- --mode=demo
  • Preview the locally built demo: npm run serve

How to Release a New Version

We use semantic-release to release the library on npm automatically.

1.11.0

6 months ago

1.10.2

1 year ago

1.10.1

1 year ago

1.10.0

2 years ago

1.9.0

2 years ago

1.7.2

2 years ago

1.8.0

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.2.4

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.9.0

3 years ago

0.8.0

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago