Vue CSS Masonry Component
Vue-masonry-css is a Vue.js component that provides a masonry layout powered by CSS, prioritizing fast rendering and optimal utilization of Vue's Virtual DOM. It is dependency-free, specifically avoiding external libraries like jQuery or traditional JavaScript-based masonry solutions (e.g., DeSandro Masonry) which often introduce performance overhead through double-rendering. The current stable version is 1.0.3, and it appears to follow a slow, stable release cadence given its nature as a lightweight, dependency-free UI component. Key differentiators include its responsive-by-default behavior, compatibility with IE10+ (including IE9, though this might be an outdated claim), and seamless integration with existing CSS animations. It achieves its layout using plain divs and a touch of flexbox, offering a simple interface to define columns and gutter sizes based on responsive breakpoints. Notable limitations include its inability to handle elements with varying widths within a single column and its intentional omission of height-based sorting to maintain high rendering performance.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'use') at Function.Vue.use
cause The `Vue` global object or imported `Vue` instance was not properly initialized or available before calling `Vue.use(VueMasonry)`.fixEnsure `import Vue from 'vue'` is present at the top of your main application file and that Vue is correctly initialized before `Vue.use()` is called, especially when using module bundlers. -
[Vue warn]: Unknown custom element: <masonry> - did you register the component correctly?
cause The `<masonry>` component was not globally registered via `Vue.use(VueMasonry)` or was not imported and registered locally in the component where it's being used.fixVerify that `import VueMasonry from 'vue-masonry-css'` and `Vue.use(VueMasonry)` are executed in your application's entry point (e.g., `main.js` or `app.js`). -
[Vue warn]: Invalid prop: type check failed for prop "cols". Expected Number, got Object
cause The `cols` or `gutter` prop was provided with an object (for responsive breakpoints) but was not bound using `v-bind` or its shorthand `:`.fixChange `cols="{ default: 4, 700: 2 }"` to `:cols="{ default: 4, 700: 2 }"` in your template. Similarly, apply this for the `gutter` prop when using an object.
Warnings
- breaking This package is designed for Vue 2 applications and is not compatible with Vue 3's composition API or instance creation API (`createApp`).
- gotcha The component does not support items with varying widths within a single column. All items are expected to occupy the full width of their respective columns.
- gotcha This library intentionally omits height-based sorting of items to maintain rendering performance. Items are ordered based on their input sequence within each column.
- gotcha When defining responsive breakpoints for `cols` or `gutter` using an object, the attributes must be bound using `v-bind` or its shorthand `:`.
Install
-
npm install vue-masonry-css -
yarn add vue-masonry-css -
pnpm add vue-masonry-css
Imports
- VueMasonry
import { VueMasonry } from 'vue-masonry-css'import VueMasonry from 'vue-masonry-css'
- Vue.use(VueMasonry)
app.use(VueMasonry)
Vue.use(VueMasonry)
Quickstart
import Vue from 'vue';
import VueMasonry from 'vue-masonry-css';
Vue.use(VueMasonry);
new Vue({
el: '#app',
data: {
items: Array.from({ length: 15 }, (_, i) => ({ id: i + 1, height: `${Math.floor(Math.random() * 100) + 100}px` }))
},
template: `
<div id="app">
<h1>My Masonry Layout</h1>
<masonry
:cols="{default: 4, 1000: 3, 700: 2, 400: 1}"
:gutter="{default: '30px', 700: '15px'}"
>
<div
v-for="(item, index) in items"
:key="item.id"
:style="{ height: item.height, background: '#f0f0f0', padding: '15px', border: '1px solid #ddd', marginBottom: '30px' }"
>
Item: {{ index + 1 }}
</div>
</masonry>
</div>
`
});