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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div id='app'></div>
              
            
!

CSS

              
                // VARIABLES
// --> colors
// --> grids
// --> typography
// 
// BASE
// --> page
// --> typography
// 
// COMPONENTS
// --> header
// --> form
// --> list
// --> footer
// 
// --> main

@import "breakpoint";


// VARIABLES

// colors
$borderColor: #111;
$fontColor: #222;
$buttonColor: #ddd;
$activeButtonColor: #f7f7f7;
$inputColor: #f7f7f7;

// grids
$mobileBP: 27.5rem;
$maxWidth: 40rem;

// typography
$fontFamily: Helvetica, Arial, sans-serif;

$rootFontSize: 16px;
$titleFontSize: 2.5rem;
$todoFontSize: 1.5rem;
$auxFontSize: 1rem;


// BASE

// page
* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

// typography
html {
  font-size: $rootFontSize;
}

body {
  font-family: $fontFamily;
  color: $fontColor;
}

a {
  text-decoration: none;
  color: $fontColor;
}


// COMPONENTS

// header
.title {
  font-size: $titleFontSize;
}

.desc {
  font-size: $auxFontSize;
}

// form
.add-todo-form {
  width: 80%;
  margin: 0 auto;
  display: flex;
}

.add-todo-input, .add-todo-button {
  font-size: $todoFontSize;
  margin: 0.125rem;
  border: 1px solid $borderColor;
}

.add-todo-input {
  flex: 1 1 auto;
  width: 100%;
  padding: 0.125rem 0.375rem;
  background: $inputColor;
}

.add-todo-button {
  flex: 0 0 auto;
  padding: 0.125rem 0.5rem;
  background: $buttonColor;
}

// list
.todo-list {
  list-style: none;
  font-size: $todoFontSize;
  text-align: left;
  width: 70%;
  margin: 1.5rem auto;
  padding: 0;
}

.todo {
  margin: 0.5rem 0;
  display: flex;
  align-content: center;
}

.todo-text {
  flex: 1 1 auto;
  margin: 0;
  word-wrap: break-word;
  cursor: pointer;
}

.todo-done {
  text-decoration: line-through;
}

.todo-delete {
  flex: 0 0 auto;
  display: block;
  margin: auto 0;
  padding: 0 0.25rem;
}

// footer
.footer {
  font-size: $auxFontSize;
  display: flex;
  flex-wrap: wrap;
  width: 80%;
  margin: 0 auto;
}

.footer-link {
  flex: 1 1 100%;
  width: 100%;
  border: 1px solid $borderColor;
  margin: 0.5rem;
  padding: 0.5rem 0.25rem;
  cursor: pointer;
  background: $buttonColor;
}

.active {
  background: $activeButtonColor;
}

@include breakpoint($mobileBP) {
  .footer {
    flex-wrap: nowrap;
  }

  .footer-link {
    flex: 1 1 0;
  }
}

// main
.todo-app {
  text-align: center;
  width: 100%;
  max-width: $maxWidth;
  margin: 0 auto;
  padding: 1rem;
}

              
            
!

JS

              
                // ACTIONS
// --------------------
// --> addTodo
// --> toggleTodo
// --> deleteTodo
// --> setVisibilityFilter
// 
// REDUCERS
// --------------------
// ------> todo
// ----> todos
// ----> visibilityFilter
// --> reducers
// 
// COMPONENTS
// --------------------
// ----> Header
// ------> AddTodoForm
// ----> AddTodo
// --------> Todo
// ------> TodoList
// ----> FilteredTodoList
// --------> Link
// ------> FilterLink
// ----> Footer
// --> App

const { PropTypes } = React;
const { connect, Provider } = ReactRedux;


// ACTIONS
let nextTodoId = 0;

// addTodo
const addTodo = (text) => {
  return {
    type: 'ADD_TODO',
    id: nextTodoId++,
    text,
  };
};

// toggleTodo
const toggleTodo = (id) => {
  return {
    type: 'TOGGLE_TODO',
    id,
  };
};

// deleteTodo
const deleteTodo = (id) => {
  return {
    type: 'DELETE_TODO',
    id
  };
};

// setVisibilityFilter
const setVisibilityFilter = (filter) => {
  return {
    type: 'SET_VISIBILITY_FILTER',
    filter,
  };
};


// REDUCERS
const initialState = [
  {
    id: -3,
    text: 'A todo list demo made with React',
    completed: false
  }, {
    id: -2,
    text: 'Click on the text to complete a todo',
    completed: false
  }, {
    id: -1,
    text: 'Click on the x to delete a todo',
    completed: false
  }
];

// todo
const todo = (state, action) => {
  switch (action.type) {
    case 'ADD_TODO':
      return {
        id: action.id,
        text: action.text,
        completed: false
      };
    case 'TOGGLE_TODO':
      if (state.id !== action.id) {
        return state;
      }

      return {
        ...state,
        completed: !state.completed
      };
    case 'DELETE_TODO':
      return state.id !== action.id;
    default:
      return state;
  }
};

// todos
const todos = (state = initialState, action) => {
  switch (action.type) {
    case 'ADD_TODO':
      return [
        ...state,
        todo(undefined, action)
      ];
    case 'TOGGLE_TODO':
      return state.map(t => todo(t, action));
    case 'DELETE_TODO':
      return state.filter(t => todo(t, action));
    default:
      return state;
  }
};

// visibilityFilter
const visibilityFilter = (state = 'SHOW_ALL', action) => {
  switch (action.type) {
    case 'SET_VISIBILITY_FILTER':
      return action.filter;
    default:
      return state;
  }
};

// reducers
const reducers = Redux.combineReducers({
  todos,
  visibilityFilter
});


// COMPONENTS

// Header
const Header = () => (
  <div className="header">
    <h1 className="title">React Todo Demo</h1>
    <p className="desc">
      Enter a new todo to add to the todo list.
      Click on a todo to toggle its completed state.
    </p>
  </div>
);

// AddTodoForm
const AddTodoForm = ({ onSubmit }) => {
  let input;

  return (
    <div className="add-todo">
      <form
        className="add-todo-form"
        onSubmit={e => onSubmit(e, input)}
      >
        <input
          className="add-todo-input"
          placeholder="Add todo here..."
          ref={node => { input = node; }}
        />
        <button
          className="add-todo-button"
          type="submit"
        >
          +
        </button>
      </form>
    </div>
  );
};

AddTodoForm.PropTypes = {
  onSubmit: PropTypes.func.isRequired
};

// AddTodo
const AddTodo = connect(
  null,
  (dispatch) => {
    return {
      onSubmit: (e, input) => {
        e.preventDefault();
        if (!input.value.trim()) {
          return;
        }
        dispatch(addTodo(input.value));
        input.value = '';
      }
    }
  }
)(AddTodoForm);

// Todo
const Todo = ({ onTodoClick, onDeleteClick, completed, text, id }) => {
  let todoClass = "todo-text todo-text-" + id;

  if (completed) {
    todoClass += " todo-done";
  }

  return (
    <li
      className={"todo todo-" + id}
    >
      <p
        className={todoClass}
        onClick={onTodoClick}
      >
        {text}
      </p>
      <a
        href="#"
        className={"todo-delete todo-delete-" + id}
        onClick={(e) => {
          e.preventDefault();
          onDeleteClick();
        }}
      >
        ×
      </a>
    </li>
  );
};

Todo.PropTypes = {
  onTodoClick: PropTypes.func.isRequired,
  onDeleteClick: PropTypes.func.isRequired,
  completed: PropTypes.bool.isRequired,
  text: PropTypes.string.isRequired
};

// TodoList
const TodoList = ({ todos, onTodoClick, onDeleteClick }) => {
  if (!todos.length) {
    return <p className="no-todo">No todos</p>;
  }

  return (
    <ul className="todo-list">
      {todos.map(todo =>
        <Todo
          key={todo.id}
          {...todo}
          onTodoClick={() => onTodoClick(todo.id)}
          onDeleteClick={() => onDeleteClick(todo.id)}
        />
      )}
    </ul>
  );
};

TodoList.PropTypes = {
  todos: PropTypes.arrayOf(PropTypes.shape({
    id: PropTypes.number.isRequired,
    text: PropTypes.string.isRequired,
    completed: PropTypes.bool.isRequired
  }).isRequired).isRequired,
  onTodoClick: PropTypes.func.isRequired,
  onDeleteClick: PropTypes.func.isRequired
};

const getFilteredTodos = (todos, filter) => {
  switch (filter) {
    case 'SHOW_ALL':
      return todos;
    case 'SHOW_COMPLETED':
      return todos.filter(t => t.completed);
    case 'SHOW_ACTIVE':
      return todos.filter(t => !t.completed);
    default:
      throw new Error(`Unknown filter: ${filter}.`);
  }
};

// FilteredTodoList
const FilteredTodoList = connect(
  (state) => {
    return {
      todos: getFilteredTodos(state.todos, state.visibilityFilter),
    };
  },
  (dispatch) => {
    return {
      onTodoClick: (id) => {
        dispatch(toggleTodo(id));
      },
      onDeleteClick: (id) => {
        dispatch(deleteTodo(id));
      },
    };
  }
)(TodoList);

// Link
const Link = ({ active, children, onClick }) => {
  const footerLinkClass = "footer-link footer-link-" + children.toLowerCase();

  if (active) {
    return <span className={footerLinkClass + " active"}>{children}</span>;
  }

  return (
    <a
      className={footerLinkClass}
      href="#"
      onClick={e => {
        e.preventDefault();
        onClick();
      }}
    >
      {children}
    </a>
  );
};

Link.PropTypes = {
  active: PropTypes.bool.isRequired,
  children: PropTypes.node.isRequired,
  onClick: PropTypes.func.isRequired
};

// FilterLink
const FilterLink = connect(
  (state, ownProps) => {
    return {
      active: ownProps.filter === state.visibilityFilter,
    };
  },
  (dispatch, ownProps) => {
    return {
      onClick: () => {
        dispatch(setVisibilityFilter(ownProps.filter));
      },
    };
  }
)(Link);

// Footer
const Footer = () => (
  <div className="footer">
    <FilterLink filter="SHOW_ALL">
      All
    </FilterLink>
    <FilterLink filter="SHOW_ACTIVE">
      Active
    </FilterLink>
    <FilterLink filter="SHOW_COMPLETED">
      Completed
    </FilterLink>
  </div>
);

// App
const App = () => (
  <div className="todo-app">
    <Header />
    <AddTodo />
    <FilteredTodoList />
    <Footer />
  </div>
);

const store = Redux.createStore(reducers);

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('app')
);

              
            
!
999px

Console