• Stars
    star
    673
  • Rank 67,060 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ’  Vue.js directive for masonry blocks layouting βœ…

vue-masonry

DEPENDENCIES status

Current version: 0.16.0

Vue.js directive for masonry blocks layouting. Original masonry library.

Plugin DEMO available πŸŽ‰, JSFiddle DEMO

The plugin can be easily integrated with different CSS Frameworks. Check this Tailwind CSS DEMO.

You can also clone the basic demo repository vue-masonry + vue-cli webpack.

Usage

Install via NPM

  • Get from npm: npm install vue-masonry --save

    or from bower bower install vue-masonry

  • Make sure that the masonry library is included; for example using cdn link: <script async defer src="https://cdnjs.cloudflare.com/ajax/libs/masonry/4.0.0/masonry.pkgd.min.js"></script> or in other convenient way.

Usage with build tools

  • In your Vue app you'll have the following code:

    import Vue from 'vue'
    
    // import ES6 style
    import {VueMasonryPlugin} from 'vue-masonry';
    
    // or using CJS 
    // const VueMasonryPlugin = require('vue-masonry').VueMasonryPlugin
    
    Vue.use(VueMasonryPlugin)
    
    <div v-masonry="containerId" transition-duration="0.3s" item-selector=".item">
        <div v-masonry-tile class="item" v-for="(item, index) in blocks">
           <!-- block item markup -->
        </div>
    </div>
    

Usage with Vue 3

  • Use version >= 0.14.0

  • or of using earlier version: Dependencies: mitt package In your Vue 3 app, you'll have the following code

    import { createApp } from 'vue'
    import mitt from 'mitt'
    
    import { VueMasonryPlugin } from "vue-masonry";
    
    const emitter = mitt()
    let app = createApp(App)
    app.config.globalProperties.emitter = emitter
    app.use(VueMasonryPlugin)
    app.mount('#app')
    
    const containerId = 42 // optional, if you need multiple containers on page
    const blocks = [1,2,3,4,5,6]
    
    <div v-masonry="containerId" transition-duration="0.3s" item-selector=".item">
      <div v-masonry-tile class="item" v-for="(item, index) in blocks">
        <!-- block item markup -->
      </div>
    </div>
    

Usage directly in the browser

Since v 0.11.3 in-browser usage is available using a direct script inclusion on the page like so:

<script src="https://unpkg.com/[email protected]/dist/vue-masonry-plugin-window.js"></script>
var VueMasonryPlugin = window["vue-masonry-plugin"].VueMasonryPlugin
Vue.use(VueMasonryPlugin)

Properties

Properties that are currently available reproduce most of those on the original masonry plugin:

  • item-selector=".item" - list element DOM item selector;
  • transition-duration="0.3s - duration of transitions;
  • column-width="#test" - element selector for column width. Can be a selector string or a number;
  • origin-left="false" - set to group elements to the right instead of left by default;
  • origin-top="false" - set to group elements to the bottom instead of top by default;
  • stamp=".stamp" - specifies which elements are stamped within the layout;
  • gutter=".gutter-block-selector" - specifies [horizontal space between item elements]. Can be a selector string or a number. (https://masonry.desandro.com/options.html#gutter). Set gutter to an Element or Selector String to use the outer width of the element;
  • fit-width="true" - sets the width of the container to fit the available number of columns;
  • horizontal-order="true" - lays out items to (mostly) maintain horizontal left-to-right order;
  • stagger="0.03s" - Staggers item transitions, so items transition incrementally after one another. Set as a CSS time format, '0.03s', or as a number in milliseconds, 30.
  • destroy-delay="0" - Amount of time (in milliseconds) to wait before unloading masonry via masonry.destroy() when the container is destroyed. This is useful during page/route transitions to ensure the layout is consistent while the transition takes place.

If you need to manually trigger masonry layout redraw (for example in case if your tile elements amount or content has changed) you can now use this.$redrawVueMasonry('containerId') method. As of 0.11.8 your can pass id of the block where you want to trigger the redraw.

(If you use old version < 0.10.11 it can still be Vue.redrawVueMasonry(), but please consider to upgrade)

NUXT ssr implementation

The best way to implement this is to use the vue-client-only plugin. This project is previously known as vue-no-ssr.

  1. Create a file in your plugins folder called vue-masonry.js with the following contents:
import Vue from 'vue'
import {VueMasonryPlugin} from 'vue-masonry'

Vue.use(VueMasonryPlugin)
  1. Add this plugin to your nuxt.config.js
  plugins: [
    { src: '~/plugins/vue-masonry', ssr: false }
  ]

(NB make sure ssr is set to false)

  1. Add client-only wrapper and the markup for your vue-masonry to a component:

HTML:

    <client-only>
      <div v-masonry transition-duration="3s" item-selector=".item" class="masonry-container">
        <div v-masonry-tile class="item" :key="index" v-for="(item, index) in blocks">
          <p>{{item}} - {{index}}</p>
        </div>
      </div>
    </client-only>

JS:

  import ClientOnly from 'vue-client-only'

  export default {
    components: {
      ClientOnly
    },
    mounted () {
      if (typeof this.$redrawVueMasonry === 'function') {
        this.$redrawVueMasonry()
      }
    }
  }

An example implementation of vue-no-ssr old version + vue-masonry with nuxt can be found here - https://github.com/richlloydmiles/example-vue-masonry-ssr

Contributing

Thanks to all the contributors for making the plugin better!

Questions, bugs

Known issues

  • Minor API change (JAN 2018). If you suddenly see error: Uncaught TypeError: _vue2.default.redrawVueMasonry is not a function - please upgrade your usage of the plugin's method redrawVueMasonry in component methods from Vue.redrawVueMasonry() to this.$redrawVueMasonry();. See more details in #31 issue

License

MIT

More Repositories

1

vuejs-sound-player

▢️ 🎹 🎡 HTML5 <audio> tag sound player UI for Vue.js - supports single, loop, pause/stop modes etc
JavaScript
178
star
2

emoji-vue

Add emoji keyboard to your vuejs project
JavaScript
137
star
3

vue-styleguide-generator

React inspired style guide generator for Vue.js
JavaScript
122
star
4

vue-terminal-ui

πŸ–₯Terminal UI emulator VueJS component
JavaScript
55
star
5

vue.js-v2-crud-application

Vue.js demo app
HTML
54
star
6

tailor-vue-demo

Using frontend microservices with zalando/tailor and Vue.js as frontend fw
JavaScript
39
star
7

vuejs-content-scroll-progress

Top bar indicating amount of content scrolled
HTML
25
star
8

vue-lorem-ipsum

Lorem ipsum text generator component for vue.js
Vue
15
star
9

vue-preload

Plugin SSR Vue adding <link rel="preload" capabilities
JavaScript
15
star
10

svelte-pwa-chat

SvelteJS + websockets app
HTML
8
star
11

svelte-pwa-chat-backend

Java
6
star
12

ng2-web-cryptography

Module implementing Web Cryptography API for Angular 2
TypeScript
6
star
13

vue-ya-metrica

Yandex.Metrica component for Vue.js
JavaScript
6
star
14

vue-image-colorpicker

Image colours picker (dropper) - using Vue.js
JavaScript
3
star
15

sveltejsnews

[ARCHIVED]
HTML
3
star
16

nest-vs-next-vs-nuxt-vs-sapper

JavaScript
2
star
17

otus-chrome-extension

JavaScript
2
star
18

single-spa-hyperapp

Plugin to include your Hyperapp into single-spa based webpage
JavaScript
2
star
19

vue2-pdfjs-viewer

PDF preview component for Vue.js based on PDF.js
JavaScript
2
star
20

node-express-vite-eureka-kibana

JavaScript
2
star
21

vue3-workshop-demo-2021

Demo files for 'Vue3 practical development' workshop. Find PDF with slides via link below:
JavaScript
2
star
22

vue-starters-directory

List of available starter kits for Vue projects
Vue
2
star
23

svelte-chat-live

HTML
1
star
24

qr-app

JavaScript
1
star
25

quasar-vite-tailwind-storybook

Vue
1
star
26

commonjs-es6-modules-webpack-jest

demo repo to showcase commonjs vs es6 modules; jest; webpack; babel
JavaScript
1
star
27

textarea-moderated

JavaScript
1
star
28

otus-docker-demo

Vue
1
star
29

conf-talks

🎦 πŸ“ˆ Slides, videos and textual content for my meetup and conferences presentations
1
star
30

vue-masonry-plugin-demo

JavaScript
1
star
31

vue-material-url-shortener-example

vue-material demo
JavaScript
1
star
32

cards-game-app

Svelte --> Azure
JavaScript
1
star
33

first-image-search-load

Simple module that loads the first image from Google image search on query
JavaScript
1
star
34

vuejs-3-workshop

1
star
35

otus-vue3-demo-composition-api

CSS
1
star