cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Cycle.js demo

A Pen By Captain Anonymous

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

              div#content { 
  margin-left: auto; 
  margin-right: auto; 
  width: 100px;

button {
  margin: 0 10px;
  border: none;
  color: white;
  font-weight: bold;
  background-color: #6CA2C8;

              // The Counter component
function Counter(sources) {
  // Listen to increment and decrement clicks.
  const increment$ = sources.DOM.select('.increment').events('click').map(() => +1);
  const decrement$ = sources.DOM.select('.decrement').events('click').map(() => -1);
  const count$ = Rx.Observable.merge(increment$, decrement$)
    .shareReplay() // Keeps track of a counters 'count' even when re-created when 'Create Counter' is clicked.
    .scan((count, modifier) => count + modifier);  // Take the current count and perform either -1 or + 1 on it.
  const vtree$ = count$.map(counter =>
      CycleDOM.button('.decrement', '-'),
      CycleDOM.button('.increment', '+')
  // Expose the count$ so external Components can observe it.
  const sinks = {
    DOM: vtree$,
    count$: count$ 
  return sinks;

function CounterList(sources) {
  // Whenever the user clicks the 'Create Counter' button add a new IsolatedCounter.
  const addCounter$ = sources.DOM.select('.create-counter').events('click').map(() => IsolatedCounter(sources));
  // Whenever the user clicks on the 'remove' button remove the counter.
  const removeCounter$ = sources.DOM.select('.remove').events('click').map(() => event.target.index);
  // MODEL
    Creates the counters$ observable by merging two other streams:
    the addCounter$ and removeCounter$. Whenever one or two fires
    an event it will manipulate the list of counters.

    The list initially starts of as an empty array via the 'startWith'

    When either addCounter$ or removeCounter$ fires they respective 
    reducer functions are put on the stream. This causes 'scan' to
    trigger applying either the addCounter or removeCounter function 
    on the current list. The result will be the next value of the 
  const counters$ = Rx.Observable.merge(
    addCounter$.map(counter => addCounter(counter)), 
    removeCounter$.map(index => removeCounter(index)) // Add a new removeCounter reducer function on the stream.
  .scan((counters, operation) => operation(counters))  
  .share(); // share so the totalCount$ and vtree$ each get their own fork.
    Calculates the totalCount and makes it available as totalCount$.

    The counters$ produces an array of Counter components. The counter
    objects have a count$ property we want each of those counts and
    add them together.

    It does this by combining each Counter component's count$ into
    another stream which will produce an array via 'combineLatest'.
    The array combineLatest produces is then summed up via a 'reduce'.

    The reason we use 'flatMapLatest' on the counters$ is that
    'combineLatest' also produces an observable. We do not want to
    get that 'observable' we want whatever it is producing!

    The 'flatMap' takes the observable that 'combineLatest' produces, 
    observes it and whenever combineLatest produces something, 'flatMap' 
    simply produces it to. See this video which explains it on egghead: 
  const totalCount$ = counters$.flatMapLatest(counters => {
    return Rx.Observable.combineLatest(counters.map(counter => counter.count$))
                        .map(arr => arr.reduce((total, count) => total + count, 0));
    Combine the counters$ and the totalCount$ whenever one of them
    produces a value, take the latest value of both of them produced
    and render a Virtual DOM tree with them.
  const vtree$ = counters$.combineLatest(totalCount$, 
    (counters, totalCount) =>
        CycleDOM.div(counters.map((counter, index) => 
            CycleDOM.button('.remove', { index: index }, 'remove')
        CycleDOM.button('.create-counter', 'Create Counter'),
        CycleDOM.h2('Total: ' + totalCount)
  const sinks = {
    DOM: vtree$
  return sinks;

// Takes a counter and adds it to the counter list.
function addCounter(counter) {  
  // Inner reducer function, is given the list of counters.
  return function(counters) {
     return counters.concat(counter); // Add the counter.

// Takes an index to remove a counter from the counter list.
function removeCounter(index) {
  // Inner reducer function, is given the list of counters.
  return function(counters) {
    return counters.filter((_, i) => i !== index);

  Creates an Isolate version of the Counter component via CycleIsolate.
  This way two Counter components do not get in each others way.
  For fun you can alter the body of the function to:
  'return Counter({ DOM: sources.DOM });'.
  To see the effects of not having isolate Components.

  Cycle.js will behind the scenes alter the Virtual DOM of each
  Counter to make each counter identifiable. You can inspect via
  the browser to see what it does.
  For fun you should remove CycleIsolate and return: 
  Counter({ DOM: sources.DOM })l
function IsolatedCounter(sources) {
  return CycleIsolate(Counter)({ DOM: sources.DOM });

Cycle.run(CounterList, {
  DOM: CycleDOM.makeDOMDriver('#content')

Loading ..................