JSFiddle - React, Tailwind, and code Playground

by rich_harris

HTML

<script src="http://cdnjs.cloudflare.com/ajax/libs/masonry/3.2.2/masonry.pkgd.min.js"></script>
<main></main>

<script id='template' type='text/ractive'>
    <button on-click='addItem()'>add item</button>
    
    <div class='items'>
        {{#each items :i}}
            <div class='item'
                 style='height: {{height}}px;
                        background-color: {{colour}};'>{{i}}</div>
        {{/each}}
    </div>
</script>

CSS

body {
    font-family: 'Helvetica Neue', arial, sans-serif;
    font-weight: 200;
    color: #353535;
}

h1, h2, h3, h4, h5, h6 {
    font-weight: 200;
}

.error {
    color: #d00;
    font-family: monospace;
}

.items {
    width: 100%;
}

.item {
    width: 25%;
}

button {
    /*width: 100%;*/
    display: block;
}

JavaScript

var ractive = new Ractive({
    el: 'main',
    template: '#template',
    data: {
        items: getItems( 10 )
    },
    addItem: function () {
        var index = ~~( Math.random() * this.get( 'items.length' ) );
    
        this.splice( 'items', index, 0, getItem() );
        
        var node = this.findAll( 'div' )[ index ];
        masonry.appended( node );
        masonry.layout();
    }
});

var masonry = new Masonry( ractive.find( '.items' ) );

function getItems ( i ) {
    var items = [];
    while ( i-- ) {
        items.push( getItem() );
    }           
    return items;
}

function getItem () {
    return {
        width: 30 + rn( 50 ),
        height: 30 + rn( 50 ),
        colour: 'rgb(' + [ rn(255), rn(255), rn(255) ].join( ',' ) + ')'
    };
}
       
function rn ( max ) {
    return ~~( Math.random() * max );
}