Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                
              
            
!

CSS

              
                body {
  font-family: sans-serif;
  color: #444;
}

*, *::before, *::after {
  box-sizing: border-box;
}

ul {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  justify-content: flex-start;
  flex-wrap: wrap;
}

li {
  font-size: 4em;
  margin: 0;
}

.app {
  position: relative;
  width: 800px;
  margin: 2em auto;
  padding: 3em 4em;
  box-shadow: 0 .5em 2.5em #ccc;
  text-align: center;
  border-radius: .25em;
  overflow: hidden;
}

button {
  background: #0D977A;
  color: #fff;
  border: none;
  font-size: 1em;
  padding: 1em;
  margin: 1em 1em 3em;
}

.stats {
  background: #aDfffA;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1em 2em;
  margin: 0 -4em 2em;
  text-align: center;
  box-shadow: inset 0 -0.25em 0.125em -0.25em #555;
  font-size: 1.25em;
}

.stats.too-slow {
  background: #FF8F84;
}

.stats--count, .stats--hyper, .stats--render, .stats--total {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.bold {
  font-weight: bold;
  font-size: 2em;
  margin-left: .125em;
}
              
            
!

JS

              
                const h = (nodeName, attributes, ...children) => {
  return {nodeName, attributes, children};
}

const renderComponent = (component) => {
  const base = component.base;
  let rendered = component.render(component.props, component.state);

  component.base = diff(component.base, rendered);;
}

const diff = (dom, vnode, parent) => {
  if (dom) {
    if (typeof vnode === 'string') {
      dom.nodeValue = vnode;

      return dom;
    }

    if (typeof vnode.nodeName === 'function') {
      const component = new vnode.nodeName(vnode.attributes);
      const rendered = component.render(component.props, component.state)
      diff(dom, rendered);
      return dom;
    }

    if (dom.childNodes.length !== vnode.children.length) {
      dom.appendChild(renderNode(vnode.children[vnode.children.length - 1]));
    }

    for(let i = 0; i < vnode.children.length; i++) {
      diff(dom.childNodes[i], vnode.children[i]);
    }

    return dom;
  } else {
    const newDom = renderNode(vnode);
    parent.appendChild(newDom);
    return newDom;
  }
}

class Component {
  constructor (props) {
    this.props = props;
    this.state = {};
  }

  setState(state) {
    this.state = Object.assign(this.state, state);
    renderComponent(this);
  }
}

class App extends Component {
  constructor (props) {
    super(props);

    this.state.list = ['👩🏽‍🎤', '🐼', '👩🏻‍💻', '👨🏾‍🏫', '👨🏽‍🎓', '🙋🏼‍♀️', '💆🏿', '🤷🏽‍♂️', '🦖', '👩🏼‍🔬'];
    this.timer = setInterval(() => {
      this.setState({
        list : [
          ...this.state.list,
          this.state.list[Math.floor(Math.random() * this.state.list.length)]
        ]
      });
    }, 1000);
  }

  render (props, {list}) {
    return h('div', {class: 'app'},
      h('h1', null,
        '🎉 JSUnconf 2019! 🎉'
      ),
      h(People, {list})
    );
  }
}

class People extends Component {
  render(props, state) {
    return h(
      'ul', null,
      ...props.list.map(item => h('li', null, item))
    )
  }
}

const renderNode = (vnode) => {
  let el;
  const {nodeName, attributes, children} = vnode;

  // Strings just convert to #text Nodes:
  if (vnode.split) return document.createTextNode(vnode);

  if (typeof nodeName === 'string') {
    el = document.createElement(nodeName);

    for (key in attributes) {
      if (key[0]=='o' && key[1]=='n') {
        el.addEventListener(key.toLowerCase().substring(2), attributes[key]);
      } else if (key === 'class') {
        el.className = attributes[key];
      } else {
        el.setAttribute(key, attributes[key]);
      }
    }
  } else if (typeof nodeName === 'function' ) {
    const component = new nodeName(attributes);
    el = renderNode(component.render(component.props, component.state))
    // keep parent for the reference to rerender
    component.base = el;
  }

  // render (build) and then append child nodes:
  (children || []).forEach( c => el.appendChild(renderNode(c)) );

  return el;
}

const render = (vNode, parent) => {
  diff(undefined, vNode, parent);
}

render(
  h(App),
  document.body
);


              
            
!
999px

Console