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

              
                 <main id = "app"></main>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}
body {
  background: #f1f1f1;
}
.todo-component {
  background: linear-gradient(to left, #6e0235, #910346);
  color: white;
  width: 80%;
  @media (min-width: 769px) {
    width: 30%;
  }
  min-height: 100px;
  margin: 50px auto;
  padding: 0;
  .todo-box {
    padding: 1rem 2rem;
  }
  ul {
    width: 100%;
    list-style-type: none;
    padding: 0;
    display: inline-block;
  }

  li {
    width: 100%;
    background: white;
    border-radius: 2px;
    height: 30px;
    font-size: 16px;
    padding: 5px 5px 6px 15px;
    margin-bottom: 10px;
    color: #303030;
  }

  button {
    margin-left: 5px;
    float: right;
    background: transparent;
    border: 0;
  }

  input {
    display: block;
    width: 100%;
    margin: 0 auto;
    height: 40px;
    padding-left: 10px;
    font-size: 16px;
  }
  p {
    font-size: 16px;
    line-height: 20px;
    color: red;
    
    span {
      color: white
    }
  }
  h2 {
    padding : {
      top: 15px;
      bottom: 15px;
    }
    margin: 0 0 1rem 0;

    text-align: center;
    color: white;
  }
}

              
            
!

JS

              
                class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
       hasError : false,
       error    : null,
       info     : null
    };
  }

  componentDidCatch(error, info) {
    this.setState({ 
      hasError : true, 
      error    : error,
      info     : info
    });
  }

  render() {
    if (this.state.hasError) {
      return (
        <div>
          <h1>Oops!!! Something went wrong</h1>
          <p><span>The error: </span> {this.state.error.toString()}</p>
          <p><span>Where it occured:</span> {this.state.info.componentStack}</p>
         </div> 
          );
    } else {
      return this.props.children;
    }
  }
}

class Input extends React.Component {
  constructor(props) {
    super(props);
    this.state = { value: "" };
    this.handleInput = this.handleInput.bind(this);
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(event) {
    this.setState({ value: event.target.value });
  }

  handleInput() {
    event.preventDefault();
    const input = this.state.value;
    if (input == "") {
      return;
    }
    this.setState({ value: "" });
    this.props.onChange(input);
  }

  render() {
    return (
      <form onSubmit={this.handleInput}>
        <input
          value={this.state.value}
          onChange={this.handleChange}
          placeholder="Add an Item"
        />
      </form>
    );
  }
}

class ToDo extends React.Component {
  constructor(prps) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
  }
  //handles when the cancel button is clicked
  handleClick() {
    const todoName = this.props.name;
    this.props.onClick(todoName);
  }

  render() {
    return (
      <li>
        {this.props.name}
        <button onClick={this.handleClick}>X</button>
      </li>
    );
  }
}

class ToDoList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {list: this.props.value };
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick(todo) {
    let list = this.state.list;
    for (let i = 0; i < list.length; i++) {
      if (list[i] == todo) {
        list.splice(i, 1);
      }
    }
    const newList = list;
    this.setState({ list: newList });
  }

  render() {
    const displayList = this.state.list;
    let tagList = displayList.map((todos, i) => (
      <ToDo key={"item" + i} name={todos} onClick={this.handleClick} />
    ));
    return <ul>{tagList}</ul>;
  }
}

class ToDoApp extends React.Component {
  constructor(props) {
    super(props);
    this.state = { display: [], error: "" };
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(newInput) {
    const isTag = (array, tag) => {
      for (let a = 0; a < array.length; a++) {
        if (array[a] == tag) {
          return true;
        }
      }
    };

    const todoArray = this.state.display;
    if (!isTag(todoArray, newInput)) {
      todoArray.push(newInput);
      this.setState({
        display: todoArray,
        error: ""
      });
    } else {
      this.setState({ error: "You've already added that item" });
    }
  }

  render() {
    return (
      <div className="todo-component">
        <h2>ToDo</h2>
        <div className="todo-box">
            <Input onChange={this.handleChange} />
          <ErrorBoundary>
            <ToDoList value={this.state.display} />
          </ErrorBoundary>
        </div>
      </div>
    );
  }
}

ReactDOM.render(<ToDoApp />, document.getElementById("app"));

              
            
!
999px

Console