Package Exports
- @yeger/vue-masonry-wall
Readme
@yeger/vue-masonry-wall
Responsive masonry layout with SSR support and zero dependencies for Vue 3.
Features
- 📱 Responsive: Responsive with configurable column width and gaps. Based on
ResizeObserver. - 🔁 Reactive: Reacts to property changes.
- 🪶 Lightweight: Zero dependencies. Less than 1.3 kB.
- ⬅️ RTL: Supports LTR and RTL layouts.
Links
Installation
# yarn
$ yarn add @yeger/vue-masonry-wall
# npm
$ npm install @yeger/vue-masonry-wallUsage
import { createApp } from 'vue'
import MasonryWall from '@yeger/vue-masonry-wall'
const app = createApp()
app.use(MasonryWall)Props:
items: Array of items. Required.column-width: Minimal width of columns inpx.gap: Spacing between items inpx. Defaults to0.rtl: Toggles between LTR (false) and RTL (true) layouts. Defaults tofalse.ssr-columns: Number of server-side-rendered columns. Optional.scroll-container: ScrollingHTMLElementparent element that will be used for restoring scroll position. If omitted,windowis used.
<script setup lang="ts">
const items = [
{
title: 'First',
description: 'The first item.',
},
{
title: 'Second',
description: 'The second item.',
},
]
</script>
<template>
<masonry-wall :items="items" :ssr-columns="1" :column-width="300" :gap="16">
<template #default="{ item, index }">
<div :style="{ height: `${index * 100}px` }">
<h1>{{ item.title }}</h1>
<span>{{ item.description }}</span>
</div>
</template>
</masonry-wall>
</template>Adding items
To add new items, assign a new value to the items property, e.g., items.value = [...items.value, newItem].
DO NOT push items to the array (e.g., items.value.push(newItem)), as such mutations will not be detected by the reactivity.
Limitations
This library intentionally doesn't handle elements with dynamically changing height, as this would cause constant changes of the column distribution. As a consequence, the initial height of items is used. For images, specyfing aspect ratios can prevent unbalanced distributions.
All columns have the same width, specified by the column-width property.
In addition, the elements of items should not set a specific width and instead be full-width, e.g., use width: 100%.
Nuxt 3
Create a plugin (e.g., plugins/vue-masonry-wall.ts) with the following code:
import MasonryWall from '@yeger/vue-masonry-wall'
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.use(MasonryWall)
})Also, see https://github.com/DerYeger/vue-masonry-wall/issues/43.
Development
To serve or build the demo, the library has to be built first using pnpm dev or pnpm build.
# install dependencies
$ pnpm install
# build in watch mode
$ pnpm dev
# build for production
$ pnpm turbo build
# lint project files
$ pnpm turbo lint
# serve demo
$ pnpm turbo dev:demo
# build demo for production
$ pnpm turbo build:demoDisclaimer
This component originated as a modified version of vue-masonry-wall by Fuxing Loh.
License
MIT - Copyright © Fuxing Loh, Jan Müller