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 id="app"></div>
              
            
!

CSS

              
                .todos-list-group .list-group-item.disabled
  text-decoration: line-through
  font-style: italic
  background-color: #efefef
  
.welcome-text
  height: 0
  overflow: hidden
  transition: height .25s ease-in-out
  text-align: center
  color: #444

.welcome-text--visible
  height: 50px
              
            
!

JS

              
                
//
// -- State management
//

// Settings reducer
const settingsInitialState = { name: 'redux-todo-app' }
const settingsReducer = (state = settingsInitialState, action) => state

// Todos reducer
const todosInitialState = []
const todosReducer = (state = todosInitialState, action) => {
  switch (action.type) {
    case 'add-todo': return [ ...state, action.todo ]
    case 'toggle-todo': return [ ...state.map(todo => {
      if (todo.id === action.todoId) {
        return {
          ...todo,
          status: !todo.status
        }
      }
      return todo
    }) ]
    case 'clear-done-todos': return [ ...state.filter(todo => !todo.status) ]
    default: return state
  }
}

const reducers = Redux.combineReducers({
  settings: settingsReducer,
  todos: todosReducer,
})

const appInitialState = {
  settings: { name: 'TodosDemo' },
  todos: [
 //   { id: 1, status: false, content: 'Buy milk' },
 //   { id: 2, status: true, content: 'Clean home' },
  ],
}

// compose the store with the debug tool if available
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || Redux.compose
const enhancers = composeEnhancers(Redux.applyMiddleware(ReduxThunk.default))
const store = Redux.createStore(reducers, appInitialState, enhancers)


//
// -- Service Layer (thunks)
//

const addTodo = content => dispatch => {
  dispatch({
    type: 'add-todo',
    todo: {
      id: Date.now(),
      status: false,
      content,
    }
  })
}


//
// -- Dumb Components
//

const getTodoClass = status => {
  const classes = [ 'list-group-item' ]
  if (status) {
    classes.push('disabled')
  }
  return classes.join(' ')
}

const TodoItem = ({ id, status, content, onToggle }) => (
  <li className={getTodoClass(status)} onClick={() => onToggle(id)}>
    {content}
  </li>
)

const TodosList = ({ items, onToggle }) => (
  <ul className={'list-group todos-list-group'}>
    {items.map(item => 
      <TodoItem
        {...item}
        key={item.id}
        onToggle={onToggle}
      />
    )}
  </ul>
)

const TodosListView = ({ todos, toggleTodo, clearDone }) => {
  if (!todos.length) {
    return null
  }
  return (
    <div>
      <hr />
      <TodosList items={todos} onToggle={toggleTodo} />
      <hr />
      <button
        className={'btn btn-link'}
        onClick={clearDone}
        children={'Clear stuff I have done'}
      />
    </div>
  )
}

// Stateful component that handle the form typing and local validation
class NewItemForm extends React.Component {
  constructor (props) {
    super(props)
    this.state = { text: '' }
  }
  onTextUpdate (text) {
    this.setState({ text })
  }
  onFormSubmit (e) {
    e.preventDefault()
    if (!this.state.text) {
      return
    }
    this.props.onNewValue(this.state.text)
    this.setState({ text: '' })
  }
  render () {
    return (
      <form onSubmit={(e) => this.onFormSubmit(e)}>
        <div className={'form-row'}>
          <div className={'col'}>
            <input
              type={'text'}
              className={'form-control'}
              placeholder={'remember the milk...'}
              value={this.state.text}
              onChange={e => this.onTextUpdate(e.target.value)}
            />
          </div>
          <div className={'col-xs-1'}>
            <button type={'submit'} className={'btn btn-primary'}>{'Add'}</button>
          </div>
        </div>
      </form>
    )
  }
}
NewItemForm.defaultProps = {
  onNewValue: () => {}
}

const getWelcomeTextClass = show => {
  const classes = [ 'welcome-text' ]
  if (show) {
    classes.push('welcome-text--visible')
  }
  return classes.join(' ')
}

const WelcomeText = ({ show }) => (
  <div className={getWelcomeTextClass(show)}>
    <p className={'lead'}>{'What are you going to do next?'}</p>
  </div>
)

// Many incoming props can be listed vertically
const AppComponent = ({
  name,
  todos,
  showWelcome,
  addTodo,
  toggleTodo,
  clearDone
}) => (
  <div className={'container'}>
    <h3>{name}</h3>
    <hr />
    <WelcomeText show={showWelcome} />
    <NewItemForm onNewValue={addTodo} />
    <TodosListView { ...({todos, toggleTodo, clearDone}) } />
  </div>
)


//
// -- Container
//

const mapStateToProps = ({ settings, todos }) => ({
  name: settings.name,
  showWelcome: todos.length === 0,
  todos,
})

const mapDispatchToProps = {
  addTodo, // services
  toggleTodo: todoId => ({ type: 'toggle-todo', todoId }),
  clearDone: () => ({ type: 'clear-done-todos' }),
}

const AppContainer = ReactRedux.connect(mapStateToProps, mapDispatchToProps)(AppComponent)



//
// -- Render the App
//

const renderApp = <AppContainer store={store} />
const renderTarget = document.getElementById('app')
ReactDOM.render(renderApp, renderTarget)
              
            
!
999px

Console