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

              
                <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>
              
            
!

CSS

              
                
              
            
!

JS

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

Console