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

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

CSS

              
                body {
  background: #e5e5e5;
  font-family: "Roboto Slab", sans-serif;
  .container {
    box-sizing: border-box;
    width: 66.66666666%;
    margin: 2em auto;
    padding: 2em;
    position: relative;
    background: #f5f5f5;
    h1 {
      display: table;
      margin: auto;
      padding-bottom: 0.25em;
      border-bottom: 0.1em solid black;
    }
  }
  h2 {
    cursor: pointer;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }
  div.form-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.6);
  }
  form {
    padding: 2em;
    border: 0.1em solid black;
    position: fixed;
    top: 10%;
    left: 25%;
    width: 50%;
    z-index: 1;
    background: #f5f5f5;
    label, input {
      display: block;
    }
    input, textarea {
      margin: auto auto 2em;
      line-height: 2;
      padding: auto 0.5em;
      width: 100%;
      font-size: 0.8em;
    }
  }
  button {
    background: #e5e5e5;
    border: 2px solid black;
    width: auto;
    font-size: 1em;
    margin-right: 1em;
    &:hover {
      border: 2px solid blue;
      cursor: pointer;
      background: blue;
      color: #e5e5e5;
    }
  }
  button.delete:hover {
    border: 2px solid red;
    background: red;
  }
  button.new {
    margin-top: 1em;
  }
}

@media screen and (max-width: 48em) {
  body {
    padding: 1em;
    .container {
      width: 95%;
    }
    form {
      left: 2.5%;
      width: 80%;
    }
  }
}
              
            
!

JS

              
                var RecipesContainer = React.createClass({
  getInitialState: function() {
    var availableData = storage ?
        JSON.parse(storage.getItem('_0damian_data')) :
    seedData;
    return {
      data: availableData,
      formVisibility: 0
    };
  },
  handleRecipeSubmit: function(data) { 
    var newId = this.state.data.length + 1;
    var newRecipe = {
      id: newId,
      active: 1,
      title: data.title,
      ingredients: data.ingredients,
      instructions: data.instructions
    };
    var newData = this.state.data;
    newData.push(newRecipe);
    if (storage)
      storage.setItem('_0damian_data',JSON.stringify(newData));
    this.setState({
      data: newData,
      formVisibility: 0
    });
  },
  handleRecipeTransfer: function(newData) {
    var unwrappedData = newData.data.data;
    for (var i = 0;i < this.state.data.length;i++) {
      if (this.state.data[i].id == unwrappedData.id.replace(/f/,'')) {
        var editData = this.state.data;
        editData[i].title = unwrappedData.title;
        editData[i].ingredients = unwrappedData.ingredients;
        editData[i].instructions = unwrappedData.instructions;
        if (storage)
          storage.setItem('_0damian_data',JSON.stringify(editData));
        this.setState({
          data: editData
        });
      }
    }
  },
  handleRecipeCancel: function(data) {
    this.setState({
      formVisibility: 0
    });
  },
  handleRecipeDelete: function(data) {
    for (var j = 0;j < this.state.data.length;j++) {
      if (this.state.data[j].id == data.data.id) {
        var newData = this.state.data;
        newData[j].active = 0;
        if (storage)
          storage.setItem('_0damian_data',JSON.stringify(newData));
        this.setState({
          data: newData
        });
      }
    }
  },
  toggleForm: function(e) {
    e.preventDefault();
    if (this.state.formVisibility) {
      this.setState({
        formVisibility: 0
      });
    } else {
      this.setState({
        formVisibility: 1
      });
    }
  },
  render: function() {
    return (
      <main className="container">
        <h1>RecipePad</h1>
        
        <RecipeList
          data={this.state.data}
          onRecipeTransfer={this.handleRecipeTransfer}
          onRecipeDelete={this.handleRecipeDelete}
        />
          
        <button
          className="new"
          onClick={this.toggleForm}
        >
          {String.fromCharCode(43) + ' New Recipe'}
        </button>
          
        <RecipeForm
          title="Add your recipe title"
          ingredients="Ingredients, separated, by, commas"
          instructions="Add your preparation instructions here"
          display={this.state.formVisibility ?
            {display:'block'} :
            {display:'none'}}
          onRecipeSubmit={this.handleRecipeSubmit}
          onRecipeCancel={this.handleRecipeCancel}
        />
      </main>
    )
  }
});

var RecipeList = React.createClass({
  handleRecipeTransfer: function(data) {
    this.props.onRecipeTransfer({
      data
    });
  },
  handleRecipeCancel: function(data) {
    this.props.onRecipeCancel({
      data: data
    });
  },
  handleRecipeDelete: function(data) {
    this.props.onRecipeDelete({
      data: data
    });
  },
  render: function() {
    var recipeNodes = this.props.data.map(function(recipe) {
      if (!recipe.active)
        return;
      return (
        <div className="recipe">
          <Recipe
            title={recipe.title}
            ingredients={recipe.ingredients}
            instructions={recipe.instructions}
            key={recipe.id}
            id={recipe.id}
            onRecipeTransfer={this.handleRecipeTransfer}
            onRecipeCancel={this.handleRecipeCancel}
            onRecipeDelete={this.handleRecipeDelete}
          />
        </div>
      );   
    }, this);
    return (
      <div className="list">
        {recipeNodes}
      </div>
    );
  }
});

var Recipe = React.createClass({
  getInitialState: function() {
    return {
      formVisibility: 0,
      ingredientsVisibility: 0,
      active: 1
    };
  },
  toggleIngredients: function(e) {
    e.preventDefault();
    if (this.state.ingredientsVisibility) {
      this.setState({
        ingredientsVisibility: 0
      });
    } else {
      this.setState({
        ingredientsVisibility: 1
      });
    } 
  },
  toggleForm: function(e) {
    e.preventDefault();
    if (this.state.formVisibility) {
      this.setState({
        formVisibility: 0
      });
    } else {
      this.setState({
        formVisibility: 1
      });
    }
  },
  handleRecipeTransfer: function(data) {
    this.setState({
      formVisibility: 0
    }),
    this.props.onRecipeTransfer({
      data
    })
  },
  handleRecipeCancel(data) {
    if (data.cancel && data.source != 'f0') {
      this.setState({
        formVisibility: 0
      }); 
    } else {
      this.props.onCancelTransfer({
        data: data
      })
    }
  },
  handleRecipeDelete: function(e) {
    e.preventDefault();
    this.props.onRecipeDelete({
      id: this.props.id
    });
  },
  render: function() {
    var ingredientNodes = this.props.ingredients.map(function(ingredient) {
      return (
        <li>{ingredient}</li>
      );
    });
    var ingredientsList = String(this.props.ingredients).replace(/\,/g,', ');
    return (
      <div className="recipe">
        <h2
          onClick={this.toggleIngredients}
        >
          {this.props.title}
        </h2>
        <div
          className="recipe-body"
          style={this.state.ingredientsVisibility ?
              {display: 'block'} :
              {display: 'none'}}
        >
          <ul>
            {ingredientNodes}
          </ul>

          <p>{this.props.instructions}</p>
        </div>
        
        <button
          onClick={this.toggleForm}
        >
          Edit
        </button>
        
        <button
          className="delete"
          onClick={this.handleRecipeDelete}
        >
          Delete
        </button>
        
        <RecipeForm
          title={this.props.title}
          ingredients={ingredientsList}
          instructions={this.props.instructions}
          key={'f' + this.props.id}
          id={'f' + this.props.id}
          display={this.state.formVisibility ?
            {display:'block'} :
            {display:'none'}}
          onRecipeSubmit={this.handleRecipeTransfer}
          onRecipeCancel={this.handleRecipeCancel}
        />
      </div>
    );
  }
});

var RecipeForm = React.createClass({
  getInitialState: function() {
    return {
      title: '',
      ingredients: '',
      instructions: ''
    };
  },
  handleTitleChange: function(e) {
    e.preventDefault();
    this.setState({
      title: e.target.value
    });
  },
  handleIngredientsChange: function(e) {
    e.preventDefault();
    this.setState({
      ingredients: e.target.value
    });
  },
  handleInstructionsChange: function(e) {
    e.preventDefault();
    this.setState({
      instructions: e.target.value
    });
  },
  handleRecipeSubmit: function(e) {
    e.preventDefault();
    var title = this.state.title.trim();
    var ingredients = this.state.ingredients.trim().split(',');
    var instructions = this.state.instructions.trim().split(',');
    this.setState({
      title: '',
      ingredients: '',
      instructions: ''
    });
    this.refs.titleInput.value = '';
    this.refs.ingredientsInput.value = '';
    this.refs.instructionsInput.value = '';
    this.props.onRecipeSubmit({
      id: this.props.id,
      title: title,
      ingredients: ingredients,
      instructions: instructions
    });
  },
  handleRecipeCancel: function(e) {
    e.preventDefault();
    this.setState({
      title: '',
      ingredients: '',
      instructions: ''
    });
    this.refs.titleInput.value = '';
    this.refs.ingredientsInput.value = '';
    this.refs.instructionsInput.value = '';
    this.props.onRecipeCancel({
      source: this.props.id,
      cancel: 1
    });
  },
  render: function() {
    return (
      <div
        className="form-container"
        style={this.props.display}
      >
        <form style={this.props.display}>
          <label for="title">Title</label>
          <input
            type="text"
            name="title"
            ref="titleInput"
            placeholder={this.props.title}
            onChange={this.handleTitleChange}
          />

          <label for="ingredients">
            Ingredients
          </label>
          <input
            type="text"
            name="ingredients"
            ref="ingredientsInput"
            placeholder={this.props.ingredients}
            onChange={this.handleIngredientsChange}
          />
          
          <label for="instructions">
            Instructions
          </label>
          
          <textarea
            name="instructions"
            rows="3"
            ref="instructionsInput"
            placeholder={this.props.instructions}
            onChange={this.handleInstructionsChange}
          />

          <button
            type="submit"
            onClick={this.handleRecipeSubmit}
            disabled={!this.state.title.length || !this.state.ingredients.length || !this.state.instructions.length}
          >
            Save
          </button>
          
          <button
            type="submit"
            onClick={this.handleRecipeCancel}
          >
            Cancel
          </button>
        </form>
      </div>
    );
  }
});

var seedData = [
  {id: 1,
   active: 1,
   title:"Four Cheese Rigatoni",
   ingredients:['Rigatoni','Double Cream', 'Mozzarella','Cheddar','Parmesan','Roquefort','Pepper'],
   instructions: "Boil the pasta to taste. Meanwhile, chop / grate the cheese. Drain the pasta, add the cream, then the cheese and finally some pepper."
  },
  {id: 2,
   active: 1,
   title:"Mushroom Omelette",
   ingredients:['Eggs','Milk','Mixed Herbs','Mushrooms','Cheese','Pepper'],
   instructions: "Slice the mushrooms and fry. Meanwhile, beat the eggs in a cup then add milk and stir. Add the mix to the mushrooms and season with herbs, cook gently until fluffy."},
  {id: 3,
   active: 1,
   title:"Pesto & Avocado Sandwich",
   ingredients:['Toast','Avocado','Pesto','Tomato',"Goat's Cheese",'Pepper','Rocket'],
  instructions: "Put the toast under the grill. Chop the vegetables and cheese. Add a thin coat of pesto to the ungrilled side of the toast, grill for one minute then put the vegetables  and cheese on top."},
];

if (localStorage  && !localStorage.getItem('_0damian_data')) {
  var storage = localStorage;
  storage.setItem('_0damian_data', JSON.stringify(seedData));
} else if (localStorage && !!localStorage.getItem('_0damian_data')) {
  storage = localStorage;
} else
  storage = 0;

ReactDOM.render(
  <RecipesContainer />,
  document.getElementById('app')
);
              
            
!
999px

Console