JSFiddle - React, Tailwind, and code Playground

by rich_harris

HTML

<p>Mousing over one of the list items should highlight it, but this fails with all but the last item</p>

<main></main>

<script id='template' type='text/ractive'>
    <p>highlightedId: {{highlightedId}}</p>
    
    {{#each items}}
        <list-item highlighted='{{highlightedId === id}}'/>
    {{/each}}
</script>

<script id='list-item' type='text/ractive'>
    <div on-mouseover='highlight:{{id}}' class='{{highlighted ? "highlighted" : "" }}'>{{id}}: {{description}}</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;
}

div {
    padding: 0.5em;
    border: 1px solid #eee;
    margin: 0 0 0.5em 0;
}

JavaScript

Ractive.components['list-item'] = Ractive.extend({
    template: '#list-item',
    css: 'div { padding: 0.5em; border: 1px solid #eee; margin: 0 0 0.5em 0;} .highlighted { background-color: #333; color: white }'
});

var ractive = new Ractive({
    el: 'main',
    template: '#template',
    data: {
        items: [
            { id: 1, description: 'foo' },
            { id: 2, description: 'bar' },
            { id: 3, description: 'baz' }
        ]
    },
    oninit: function () {
        this.on( '*.highlight', function ( event, id ) {
            this.set( 'highlightedId', id );
        });
    }
});