css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div id="content"></div>
              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')

🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................