mnstr-vue
v1.0.1
Published
Vue 3 component library wrapping mnstr
Readme
mnstr-vue
Vue 3 wrapper for mnstr — a high-performance
virtualized list & tree renderer that can display very large data sets by only
rendering the cells currently in view.
MNSTR itself is framework-agnostic and manages its own scroll node and cells. This package bridges MNSTR's native cell renderer to a Vue slot, so you write your cell template as normal Vue markup.
Install
npm i mnstr-vuemnstr comes along as a regular dependency. vue is a peer dependency — it
uses the Vue already installed in your project (a component library must never
bundle its own copy of Vue, or the app ends up with two Vue instances).
Quick start
The default slot is your cell template. It receives the element's own properties
plus $index and $expanded as slot props. (For primitive elements — strings,
numbers — the value is exposed as $value instead.)
Styles are injected automatically; a single import is all you need.
<script setup>
import { ref } from 'vue'
import { MnstrVue } from 'mnstr-vue'
const elements = ref(
Array.from({ length: 100_000 }, (_, i) => ({ id: i, name: `Item ${i}` }))
)
</script>
<template>
<!-- The wrapper MUST have a fixed height (see below). -->
<div style="height: 400px">
<MnstrVue :elements="elements">
<template #default="{ name, $index }">
<div class="row">{{ $index }} — {{ name }}</div>
</template>
</MnstrVue>
</div>
</template>⚠️ The container needs a fixed
height/max-height. Without it MNSTR has no viewport to virtualize against and will render every element, defeating the purpose. The component fills its parent (height: 100%), so give the parent a height.
Global registration (plugin)
Instead of importing the component everywhere, register it once:
import { createApp } from 'vue'
import MnstrVue from 'mnstr-vue'
import App from './App.vue'
createApp(App).use(MnstrVue).mount('#app')
// <MnstrVue> is now available in every template.Props
| Prop | Type | Default | Description |
| ---------------- | ---------- | ------- | ------------------------------------------------------------------------------------ |
| elements | Array | — | Required. The data (getData). Object identity must stay stable — MNSTR compares with ===. |
| getChildren | Function | — | Optional. Maps to getElementChildren() for tree data. |
| retainPosition | Boolean | true | On a new elements reference, keep the current scroll position. |
| options | Object | {} | Any additional MNSTR options (className, updateThresholdRatio, observeCellBounds, …). See the mnstr docs. |
Events
All map directly to the corresponding MNSTR callbacks:
| Event | Payload |
| --------------------------------- | --------- |
| did-render-first-element | element |
| did-render-last-element | element |
| first-in-bounds-element-changed | element |
| last-in-bounds-element-changed | element |
| cells-updated | cells |
Methods (via template ref)
<script setup>
import { ref } from 'vue'
const list = ref(null)
// e.g. list.value.scrollToElement(myElement)
</script>
<template>
<MnstrVue ref="list" :elements="elements"> … </MnstrVue>
</template>| Method | Description |
| ------------------------------------- | --------------------------------------------------------------------- |
| update(force, retainPosition) | Re-render. Call after in-place mutation of the data (see below). |
| needUpdate() | Whether MNSTR thinks an update is pending. |
| cellBoundsUpdated() | Call when cell heights changed due to reactive slot updates. |
| scrollToElement(el, opts) | Scroll a given element into view. |
| expandElement(el) / collapseElement(el) / toggleExpandElement(el) | Tree expansion control. |
| isElementExpanded(el) | Expansion state of an element. |
| exportRestoreState() | Export scroll/expansion state for later restore. |
| getInstance() | Escape hatch — the raw MNSTR instance. |
Updating data
The component watches elements by reference only (a deep watch would be
prohibitively expensive for large lists):
- Replacing the array (
elements.value = newArray) → updates automatically. - Mutating in place (
elements.value.push(...)) → callupdate()on the ref yourself, since the reference didn't change.
How it works
Each cell is rendered with Vue's low-level render() into a DOM node that MNSTR
owns. render() patches synchronously, so the DOM exists by the time MNSTR
measures cell height — an async approach (e.g. Teleport + nextTick) would report
0-height cells. Detached cells are garbage-collected on every cells-updated, and
all mounts are torn down on unmount, so slot content doesn't leak.
For everything about the underlying engine — data shape, tree support, all options and callbacks — see the mnstr documentation.
Develop
npm install
npm run build # builds ESM + CSS into dist/License
MIT © Thomas Winkler
