Vue 3 Masonry Layout Component
vue-next-masonry is a UI component library offering a responsive masonry layout for Vue 3 applications. It functions as a direct port of the well-established `vue-masonry-css` library, specifically adapted for the Vue 3 composition API and ecosystem. The current stable version is 1.1.3, indicating ongoing maintenance and compatibility with recent Vue 3 releases. Its release cadence appears to be driven by bug fixes, minor enhancements, and alignment with Vue 3's evolution rather than a strict schedule. A key differentiator is its seamless integration into Vue 3 projects for developers familiar with the Vue 2 `vue-masonry-css` component, providing a familiar API while leveraging Vue 3's performance improvements. It aims to provide an easy-to-use `<masonry>` component that automatically arranges child elements into a grid with variable column heights, optimizing space and improving visual presentation for galleries, portfolios, and lists.
Common errors
-
[Vue warn]: Failed to resolve component: masonry
cause The `<masonry>` component has not been globally registered with the Vue application instance.fixEnsure you call `app.use(masonry)` in your main application entry point (`main.ts` or `main.js`) after creating your Vue app instance. -
Items within <masonry> are not displaying in a proper grid layout; instead, they appear stacked vertically or improperly positioned when using slots.
cause The component is failing to correctly detect and arrange child elements passed through a default slot, often due to how Vue renders slot content.fixAdd the prop `:resolve-slot="true"` to your `<masonry>` component if its children are being rendered via a `<slot />`. -
TypeError: app.use is not a function
cause This error typically indicates that `app` is not a Vue 3 application instance, or you are attempting to use Vue 3's `use` method in a Vue 2 project.fixVerify that you are importing `createApp` from 'vue' and using it to initialize your application, and that your project dependencies correctly specify Vue 3.
Warnings
- breaking This package is exclusively built for Vue 3. It is not backward compatible with Vue 2 applications and will lead to errors if used in a Vue 2 project.
- gotcha When dynamically rendering child items through a `<slot />`, the `:resolve-slot="true"` prop is required on the `<masonry>` component for the layout to correctly detect and arrange these items.
- gotcha The component's configuration props (e.g., `cols`, `gutter`) and their behavior are largely inherited from `vue-masonry-css`. Developers should consult the documentation for the original `vue-masonry-css` project for detailed prop usage and examples.
Install
-
npm install vue-next-masonry -
yarn add vue-next-masonry -
pnpm add vue-next-masonry
Imports
- masonry
const masonry = require('vue-next-masonry');import masonry from 'vue-next-masonry';
Quickstart
import { createApp } from 'vue';
import masonry from 'vue-next-masonry';
// Assume your root component is App.vue
import App from './App.vue';
const app = createApp(App);
app.use(masonry);
app.mount('#app');
// In your App.vue or any other component's template:
// <template>
// <masonry :cols="{ default: 3, 700: 2, 500: 1 }" :gutter="20">
// <div v-for="(item, index) in 10" :key="index" class="masonry-item" :style="{ height: `${50 + Math.random() * 150}px` }">
// Item {{ item }}
// </div>
// </masonry>
// </template>
//
// <style>
// .masonry-item {
// background-color: #f0f0f0;
// border: 1px solid #ccc;
// padding: 10px;
// margin-bottom: 20px; /* Gutter is handled by the component, this is just for visual clarity */
// display: flex;
// align-items: center;
// justify-content: center;
// }
// </style>