JSFiddle - React, Tailwind, and code Playground
by rich_harris
HTML
<script src="http://cdn.ractivejs.org/edge/ractive.js"></script>
<main></main>
<script id='template' type='text/ractive'>
<p>This is an <em>inline component</em>:</p>
<widget title='Hi there!'/>
</script>
<script id='widget' type='text/ractive'>
<div>
<h2>{{title}}</h2>
<ul>
<li>An inline component can be as simple or as complex as you like.</li>
<li>It can have encapsulated CSS - <em>notice that the other <em> element isn't orange</em> - and custom behaviours (check the console).</li>
<li>It can also contain inline components of its own – as deeply nested as you like. This <code><widget/></code> component contains a <code><foo/></code> and a <code><bar/></code> - foo is accessible globally (because it's defined as <code>Ractive.components.foo</code>), but bar is only accessible inside the widget (because it's defined as <code>Widget.components.bar</code>).</li>
</ul>
<!-- Nested components -->
<foo/>
<bar/>
</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;
}
code {
background-color: #eee;
padding: 0.2em;
}
li {
line-height: 1.2;
margin: 0 0 0.5em 0;
}
JavaScript
Ractive.components.widget = Ractive.extend({
template: '#widget',
css: 'div { border: 1px solid #666; padding: 1em; } em { color: orange }',
init: function () {
console.log( 'initing widget!' );
},
components: {
bar: Ractive.extend({
template: '<strong>I am a bar</strong>',
css: 'strong { color: green; }'
})
}
});
Ractive.components.foo = Ractive.extend({
template: '<strong>I am a foo</strong>',
css: 'strong { color: red; }'
});
var ractive = new Ractive({
el: 'main',
template: '#template',
data: { name: 'world', v: Ractive.VERSION }
});