Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <button>Click</button>

              
            
!

CSS

              
                
              
            
!

JS

              
                // Library functions:
function pipe(arg, ...fns) {
  return fns.reduce((v, fn) => fn(v), arg);
}

function fromCallback(emitter) {
  let values;
  let resolve;
  const init = r => [values, resolve] = [[], r];  
  let valuesAvailable = new Promise(init);
  emitter(value => {
    values.push(value);
    resolve(values);
  });
  return {
    [Symbol.asyncIterator]: async function*() {
      for (;;) {
        const vs = await valuesAvailable;
        valuesAvailable = new Promise(init);
        yield* vs;
      }
    }
  };
}

function fromEvent(element, event, options) {
  if (typeof element === 'string') {
    element = document.querySelector(element);
  }
  return element ? fromCallback(listener => {
    element.addEventListener(event, listener, options);
  }) : [];
}
  
function dup(count = 2) {
  return function (src) {
    const dups = [];

    async function pull() {
      if (Symbol.asyncIterator in src) {
        const iter = src[Symbol.asyncIterator]();
        for (;;) {
          const promise = iter.next();
          dups.forEach(d => d.promises.push(promise));
          const r = await promise;
          if (r.done) {
            return;
          }
        }
      }
      if (Symbol.iterator in src) {
        for (const value of src) {
          const promise = Promise.resolve({value});
          dups.forEach(d => d.promises.push(promise));
        }
      }
      const promise = Promise.resolve({done: true});
      dups.forEach(d => d.promises.push(promise));   
    }

    for (let i = 0; i < count; ++i) {
      const dup = {
        promises: [],
        [Symbol.asyncIterator]: () => ({
          next: () => dup.promises.shift()
        })
      };
      dups.push(dup);
    }

    pull();
    return dups;
  };
}

function reduce(op, acc) {
  return async function* (src) {
    let index = 0;
    for await (const value of src) {
      acc = op(acc, value, index++);
      yield acc;
    }
  };
}

function flat(depth = 1) {
  return async function* (src) {
    for await (const value of src) {
      if (depth > 0 && (value[Symbol.asyncIterator] || value[Symbol.iterator]))  {
        yield* flat(depth - 1)(value);
      } else {
        yield value;      
      }
    }
  };
}
    
function forEach(op) {
  return async function forEach(src) {
    let index = 0;
    for await (const value of src) {
      if (op(value, index++) === false) {
        break;
      }
    }
  };
}

function filter(op) {
  return async function* (src) {
    let index = 0;
    for await (const value of src) {
      if (op(value, index++)) {
        yield value;
      }
    }
  };
}

function map(op) {
  return async function* map(src) {
    let index = 0;
    for await (const value of src) {
      yield op(value, index++);
    }
  };
}

async function head(src) {
  for await (const value of src) {
    return value;
  }  
}

function find(op) {
  return async function (src) {
    return pipe(src, filter(op), head);
  };
}

// Test the library
async function demo() {
  const [ticker1, ticker2] = pipe(fromCallback(emitTicks), flat(), dup());
  pipe(ticker1, filter((v, i) => i > 12), forEach(endTicks));
  pipe(ticker2, forEach(v => console.log('Ticks', v)));
  
  pipe(fromEvent('button', 'click'), reduce(acc => acc + 1, 0), forEach(v => console.log('Click', v)));
}
demo();

// Helper functions for testing
function emitValues(cb) {
  const vs = [1, 2, 42, 3];
  vs.forEach(cb);
}

var intervalId;
function emitTicks(cb) {
  let i = 0;
  intervalId = setInterval(() => {
    cb([i++, i++, i++]); // Date.now());
  }, 1000);
}
function endTicks() {
  clearInterval(intervalId);
  return false;
}


              
            
!
999px

Console