RiotJS Example


RiotJS Example


When I look at any of the myriad JavaScript UI libraries out there, I now ask two questions:

  • Does data flow in one direction?
  • Do updates to the UI happen intelligently?

I am a fan of Facebook’s ReactJS library because of its DOM diffing (via the “virtual DOM”) and one-way data binding. React is a tool I use every day and have come to enjoy (sans-JSX), but I am always on the lookout for way to do things simpler.

A colleague of mine recently shared the second iteration of RiotJS with me. Of course, I was sucked in because it compared itself with React (a bold statement). You can view the comparisons between Riot and React for yourself.

One unfortunate fact about fledgling JS libraries is that they lack examples of how to accomplish common goals for the web. This is an attempt to help out with that.



While I am not a fan of Facebook’s JSX (and don’t use it–more to come in future blog posts on this), Riot’s pre-compiled syntax is much easier to swallow. Given you have a package.json set up, you can easily install the Riot compiler as a development dependency

and then set up Riot to run as an NPM script and watch for any changes

which is then run by a simple

Alternatively, you can download the riot.js library via any of their recommended methods.

Global Installation (alternative)

If you don’t have a package.json and want to install Riot globally:

Tab Example

I decided to start small and make a tabbing example where clicking a tab shows content related to it underneath. Here is the final product:

Step 1: Start With Markup

Starting with a blank HTML document, add the <riot-tabs></riot-tabs> tag to your document:

As mentioned previously, we know we need the (very tiny) RiotJS library, so don’t forget to include it:

Easy enough, right? Given Riot doesn’t write our applications for us, we will need to tell Riot to mount some component, which in this case is “tabs.”

When we run this code through the browser, we’re going to receive an error telling us that 'tabs' is not a thing. Congrats! Time for Step 2.

Step 2: Creating Your Custom Tag

Riot’s NPM package, as mentioned earlier, allows us to write and compile pseudo-markup mixed with a little JS. To get started, create a src folder and add a tabs.tag file to it, then run

if you have an NPM script set up or

to compile and watch for more changes to the file/folder.

Back in the tabs.tag file, add this:

That looks almost exactly like vanilla HTML, save for the conditional class(es), which we will use later with is-active classes. Also, they are way better than concatenating className strings yourself.

Refreshing your browser will show you that you now have content that is nested within a <riot-tabs></riot-tabs> tag.

Next up, we can add in the different tabs’ contents:

Okay, this is no big deal, so far.

Being software developers, we hate writing things over and over, so let’s start with the tabs.

Riot has a nice each={ item, i in array } attribute, similar to JavaScript’s for … in …

While we’re at it, why not iterate over the content items, as well?

Next, we need to set a default “active tab” and “active content.”

Step 3: Conditional Classes

We want to be able to specify a default tab and tab content. This is accomplished via a conditional is-active class on both the .tabItem as well as the corresponding .tabContent__item. To keep track of what tab/content is active, we can

  • add a property to the this.tabs array objects
  • set an activeTab property and
  • create a function to check if the currently iterated tab is the activeTab

Since these are conditional classes, they will either be evaluated as true or false (I believe that anything that is not falsy is considered true; for example, new Date() is considered true). Here, we create a function called isActiveTab and call it from the item itself, but because the function is not scoped to the item, we need to access the parent scope and call the function on that.

Finally, we need a way to react to events.

Step 4: Toggling the Tabs

When we click on a tab, we want that tab to now be active, and we want the corresponding tab content to be displayed. This can be done via an onclick handler that calls a function on the parent called toggleTab

The onclick event handler receives an event object that is packed with information. What we want is the current tab that we are clicking on, and this is accessed through, which is just the ref property on the tab object of the currently iterated item.

According to the Riot docs, when an event handler is called, Riot will automatically call this.update() and re-render the component. However, I found that after I altered my data, I had to return true.

Once this event handler is completed and the component is re-rendered, the correct tab and content will be displayed, and you will be happy.

Wrap up

In sum, playing with Riot was a mostly enjoyable experience, and I am thankful to the Muut folks for releasing it.

While there are quirks (single quote vs double quote issues, among others) and opinions (neglecting the use of semi-colons, as well as returns), this is a promising UI library that I am definitely going to consider vs. React in my future projects.