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 class="container-fluid jumbotron">
  <div class="row">
    <div class="card" style="width: 20rem;">
      <div class="card-body">
        <h4 class="card-title">Stateless Component</h4>
        <hr>
        <div class="col" id="hello"></div> 
        <hr>
        <a href="#" class="card-link">from React Sample #1</a>
      </div>
    </div>
    <div class="card" style="width: 20rem;">
      <div class="card-body">
        <h4 class="card-title">Stateful Component</h4>
        <hr>
        <div class="col" id="timer"></div>
        <hr>
        <a href="#" class="card-link">from React Sample #2</a>
      </div>
    </div>
    <div class="card" style="width: 20rem;">
      <div class="card-body">
        <h4 class="card-title">Composition</h4>
        <hr>
        <div class="col" id="todo"></div>
        <a href="#" class="card-link">from React Sample #3</a>
      </div>
    </div>
    <div class="card" style="width: 20rem;">
      <div class="card-body">
        <h4 class="card-title">Markdown</h4>
        <hr>
        <div class="col" id="markdown"></div>
        <a href="#" class="card-link">from React Sample #4</a>
      </div>
    </div>
  </div>
</div>
            
          
!
            
              ///////////////////////////////////////////////////////////////////////////
// A React Like Functional HTML Library that supports stateless and stateful 
// components as well as component composition in less than 20 lines
//
let render = function (component, initState = {}, mountNode = 'app') {
  initState.render = function( stateRepresentation, options = {} ) {
    const start = (options.focus) ? document.getElementById(options.focus).selectionStart : 0;
    (document.getElementById(mountNode) || {}).innerHTML = stateRepresentation
    if (options.focus) {
      let f = document.getElementById(options.focus)
      f.selectionStart = start
      f.focus()
    }
  }

  let stateRepresentation = component(initState)

  initState.render((typeof stateRepresentation === 'function' ) ? stateRepresentation() : stateRepresentation)
}

let intent = function(i, f) {
  window[i || '_'] = f 
}

let value = function(el) {
  return document.getElementById(el).value
}
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
// Examples (from http://reactjs.org)
//
// HelloMessage: Stateless Component
//
let HelloMessage = ({name}) => `Hello ${name}`

// Timer: Stateful Component (not recommended, use a single-state-tree if possible)
let Timer = function({seconds, render}) {
  let state = {seconds, render}

  let tick = function () {
    state.seconds += 1
    state.render(representation())
  }

  let interval = setInterval(() => tick(), 1000)

  let representation = () => `Seconds: ${state.seconds}`

  return representation
}

// Todo: Stateful Component + Component Composition
let TodoApp = function({render}) {
  let state = { items: [], text:'', render }
  
  intent( "addTodo", function(e) {
    const newItem = {
      text: value("text"),
      id: Date.now()
    }
    state.items.push(newItem)
    state.text = ''
    state.render(representation())
    return false
  })

  let representation = () => `
      <div>
        <h4>TODO</h4>
        ${TodoList({items:state.items})}
        <form onsubmit="addTodo()">
          <input id="text" value=${state.text}>
          <button>
          Add #${state.items.length + 1}
          </button>
        </form>
      </div>`

  return representation
}
// How easy is it to mount a handler on a child element?
// as easy as passing a string! The corresponding intent
// can be declared anywhere, yes you can try this at home!
let TodoList = ({items, onclick}) => `
    <ul>
      ${items.map(item => `<li key="${item.id}" ${(onclick) ? `onclick="${onclick}(${item.id})"` : ``}>${item.text}</li>`)}
    </ul>`

let MarkdownEditor = function({defaultValue, render}) {
  let state = { value: defaultValue || 'Type some *markdown* here!', render }

  let input = "mde"
  intent("handleChange", function () {
    state.value = value(input)
    state.render(representation(), {focus: input})
    return false
  })

  const md = new Remarkable();

  let representation = () => `
      <h3>Input</h3>
      <textarea id="${input}" oninput="handleChange()">${state.value}</textarea>
      <h3>Output</h3>
      <div>
      ${md.render(state.value)}
      </div>
      `
  return representation
}




// Display Components
render(
  HelloMessage, 
  {name: "Jane"}, 
  "hello"
)

render(
  Timer, {seconds: 0},
  "timer"
)

render(
  TodoApp, {},
  "todo"
)

render(
  MarkdownEditor, {},
  "markdown"
)
            
          
!
999px
🕑 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 ..................

Console