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 Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

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
Loading ..................

Console