cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

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;
  width: 20px; 
}

            
          
!
            
              // The Counter component
function Counter(sources) {
  // INTENT: 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);
  
  /*
    MODEL: The count$ stream represents the current count of the counter.
    
    The state is kept by merging the increment$ and decrement$ streams,
    and then 'scanning' them on the current count.
    
    Let's say the user clicks decrement, then increment, and then
    the decrement again. The marble graph of the streams 
    to visually see what is happening:
    
    increment$: === -1 ======= -1 ===
    decrement$: ========= +1 ========
    ---------------------------------
    merged:     ====-1 == +1 = -1 ===
    ---------------------------------
    scan:       ==== -1 == 0 = -1 ===
    
    The 'scan' operator takes the current count, which starts at
    0 thanks to 'startsWith' and each time a new 'merged' value
    is available runs the function. In this case the function simply
    takes the current count and adds the modifier on top of it. The
    value that is returend from 'scan' is the next count.
    
    In ascii form:

    =============================
    | count | modifier | result |
    =============================
    |   0   |    -1    |   -1   |
    -----------------------------
    |   -1  |    +1    |   0    |
    -----------------------------
    |   0   |    -1    |   -1   |
    =============================
  */
  const count$ = Rx.Observable
    .merge(increment$, decrement$)                // Merge the clicks from both buttons into one stream
    .startWith(0)                                 // Start the count at zero.
    .scan((count, modifier) => count + modifier); // Take the current count and perform either -1 or + 1 on it.
  
  /* 
    VIEW: Create a stream which represents the UI of the counter.
    Each time the count$ produces a new value the UI should be
    re-created.
    
    vtree$ stands for 'virtual DOM tree' a Virtual DOM is a structure
    representing a DOM structure but is not the actual DOM. Cycle.js
    uses a Virtual DOM to check what the difference is with the actual
    real DOM. When things differ only the differences are patched back
    to the actual DOM minimizing the DOM operations needed.
    
    A video about what a Virtual DOM is: https://www.youtube.com/watch?v=a21b-KDHG-Q
  */
  const vtree$ = count$.map(counter =>
    CycleDOM.div([
      CycleDOM.h1("Counter"),
      CycleDOM.button('.decrement', '-'),
      CycleDOM.span(String(counter)),
      CycleDOM.button('.increment', '+')
    ]));
  
  const sinks = {
    DOM: vtree$
  };
  
  return sinks;
};

/*
  Here Cycle.js will actually make sure our Counter app
  is run. Cycle.run takes the Counter function so it can
  and executes it so it can take the 'sinks' which it passes
  to the CycleDOM driver. A driver takes a 'sink' and performs
  an operation with side effects. The DOM driver will take a
  stream of DOM (vtree$) and renders it into an actual DOM element,
  in this case the #content <div>.
*/
Cycle.run(Counter, {
  DOM: CycleDOM.makeDOMDriver('#content')
});

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

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

Console