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

CSS

              
                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  background-color: #ffff99;
  font-family: 'Kalam', cursive;
}
#wrapper {
  width:600px;
  margin: 20px auto;
}
#app {
  background-color: #3399ff;
  border-radius: 10px;
}
.box {
  position: relative;
}
.logo {
  margin-left: 40px;
  font-size: 40px;
  padding: 10px;
  font-family: 'Tillana', cursive;
  color: #ff3333;
}
.add-btn {
  position: absolute;
  top:15px;
  right:10px;
}
.add-btn button {
  font-family: 'Lato', sans-serif;
  height: 50px;
  width: 180px;
  font-size: 20px;
  line-height: 40px;
  background-color: #ff3333;
  color: #ffff99;
  border: 0;
  outline: 0;
  border-radius: 10px;
}
.add-btn button:hover {
  border: 2px solid #ffff99;
  cursor: pointer;
  outline: 0;
}
.list {
  margin: 0 10px;
}
.recipe-title {
  padding: 5px 5px 5px 15px;
  font-size: 24px;
  background-color: #ffff99;
  margin-bottom: 10px;
  border: 2px solid #ffff99;
  border-radius: 10px;
  overflow-wrap: break-word;
  letter-spacing: 1px;
}
.recipe-title:hover {
  border: 2px solid #ff3333;
  cursor: pointer;
}
.card {
  background-color: #3399ff;
}
.card-buttons {
  padding: 0 10px;
}
.cardBtn {
  font-family: 'Lato', sans-serif;
  font-size: 20px;
  line-height: 36px;
  background-color: #ff3333;
  color: #ffff99;
  border: 2px solid #ff3333;
  outline: 0;
  border-radius: 10px;
  height: 40px;
  width: 80px;
  margin: 0px 10px 10px;
}
.cardBtn:hover {
  border: 2px solid #ffff99;
  cursor: pointer;
}
.card-title {
  text-align: center;
  font-size: 30px;
  font-weight: bold;
  margin: 10px;
  background-color: #ffff99;
  padding: 5px;
  border-radius: 10px;
  letter-spacing: 2px;
  overflow-wrap: break-word;
}
.card-ingredients {
  text-align: left;
  background-color: #ffff99;
  margin:0px 10px 10px 10px;
  font-size: 24px;
  padding: 10px;
  border-radius: 10px;
  letter-spacing: 1px;
  overflow-wrap: break-word;
}
.form-row {
  padding: 10px;
}
.form-input {
  font-size: 30px;
  width: 576px;
  padding:10px;
  font-family: 'Tillana', cursive;
  background-color: #ffff99;
  border: 2px solid #3399ff;
  border-radius: 10px;
  color: black;
  letter-spacing: 2px;
  outline:0;
  overflow-wrap: break-word;
}
.form-textarea {
  letter-spacing: 1px;
  overflow-wrap: break-word;
  font-size: 24px;
  height: auto;
  outline: 0;
  height: 150px;
}
.footer {
  text-align: center;
  font-size: 14px;
  color: #0066cc;
  font-family: 'Lato', sans-serif;
  padding: 0 4px 4px 4px;
}

              
            
!

JS

              
                /* initial values */
var initialRecipeList ='[{"title":"Greek Salad", "ingredients": "4 large tomatoes, 1 cucumber, 1/2 a red onion, 85g feta cheese, 16 olives, 4 tbs olive oil, 1 tbs dried oregano"},{"title":"Apple pie", "ingredients": "1/2 cup unsalted butter, 3 tbs flour, 1/4 cup water, 1/2 cup white sugar, 1/2 cup brown sugar, 8 apples"}]';
if(localStorage.hasOwnProperty('_sebek78_recipebox')===false) {
  localStorage.setItem('_sebek78_recipebox', initialRecipeList);
  console.log("added recipes from localStorage");
}
var closeBtn=null;
var output;   // data from localStorage
var rowList;  // output map

function readLocalStorage() {
  var rows=[];
  if(localStorage.hasOwnProperty('_sebek78_recipebox')===true) {
    output = JSON.parse(localStorage['_sebek78_recipebox']);
    var listLength = output.length;
    for(var i=0;i<listLength;i++) {
      var valueToPush = [];
      valueToPush[0] = i;
      valueToPush[1] = output[i].title;
      valueToPush[2] = output[i].ingredients;
      rows.push(valueToPush);
    }
    rowList = rows.map((rows) =>
      <div className="recipe-title" id={rows[0]} key={rows[0]}>{rows[1]}</div>
    );
  }
}

class ShowList extends React.Component {
  render () {
    return(
        <div className="list">
          {rowList}
        </div>
    );
  }
}

class ShowCard extends React.Component {
  render() {
      var currentRecipe = this.props.cardID;
      var cardTitle = output[currentRecipe].title;
      var cardIngredients = output[currentRecipe].ingredients;
      return (
          <div className="card">
          <div className="card-buttons">
            <button type="button" className="closeBtn cardBtn">Close</button>
            <button type="button" className="deleteBtn cardBtn">Delete</button>
            <button type="button" className="editBtn cardBtn">Edit</button>
          </div>
          <div className="card-title">{cardTitle}</div>
          <div className="card-ingredients">{cardIngredients}</div>
          </div>
        );
      }
}

class RecipeForm extends React.Component {
  render() {
    var content = this.props.content;
    var formTitle;
    var formIgredients;
    var formButton;
    var formEditCardID;
    if (content==="empty") {
      formTitle = null;
      formIgredients = null;
      formEditCardID = <input id="cardID" type="hidden" value="" />;
      formButton = <button type="submit" className="addRecipe cardBtn">Add</button>;
    }
    else {
      formTitle = output[content].title;
      formIgredients = output[content].ingredients;
      formButton = <button type="submit" className="saveRecipe cardBtn">Save</button>;
      formEditCardID = <input id="cardID" type="hidden" value={content} />;
    }
    return (
      <form className="card recipeForm">
      <div className="card-buttons">
        <button type="button" className="closeBtn cardBtn">Close</button>
        {formButton}
      </div>
      <div className="form-row">
      {/*<label htmlFor="title" className="form-label">Title</label>*/}
      <input id="title" className="form-input" type="text" defaultValue={formTitle} placeholder="Title" />
      </div>
      <div className="form-row">
        {/*<label htmlFor="ingredients" className="form-label">Ingredients</label>*/}
        <textarea id="ingredients" className="form-input form-textarea" defaultValue={formIgredients} placeholder="Ingredients"/>
      </div>
        {formEditCardID}
      </form>
    );
  }
}

class RecipeBox extends React.Component {
  render () {
    var action = this.props.action;
    console.log("action: ",action);
    var cardID = this.props.cardID;
    var showPage;
    var addRecipeBtn=null;

    if (action === "list" ) {
      showPage = <ShowList />;
      addRecipeBtn = <div className="add-btn"><button type="button">Add a new recipe</button></div>;

    } else if (action==="add") {
      showPage = <RecipeForm content="empty"/>;

    } else if (action==="edit") {
      showPage = <RecipeForm content={cardID}/>;

    } else {
      var actionNumber = parseInt(action);
    if (Number.isInteger(actionNumber)) showPage = <ShowCard cardID={action} />;
    }
    return (
        <div className="box">
          <div className="logo">The Recipe Box</div>
          {addRecipeBtn}
          {showPage}
          <div className="footer">Created by Sebastian Sporek</div>
        </div>
      );
    }
}

function showRecipeList () {
  ReactDOM.render(<RecipeBox action="list" />, document.querySelector('#app'));
  var chosenRecipe =  document.querySelector('.list');
  var addBtn = document.querySelector('.add-btn');

  addBtn.addEventListener("click", function() {
    ReactDOM.render(<RecipeBox action="add" />, document.querySelector('#app'));
    closeCard();
    sendToLocalStorage("add");
  },false);

  chosenRecipe.addEventListener("click", function(e) {
    var recipeID = e.target.id;
    if (recipeID !=="") {
      ReactDOM.render(<RecipeBox action={recipeID}/>, document.querySelector('#app'));
      closeCard();
      editCard(recipeID);
      deleteCard(recipeID);
    }
  },false);
}

function closeCard() {
  closeBtn = document.querySelector('.closeBtn');
  if (closeBtn!==null) {
      closeBtn.addEventListener("click", function() {
        console.log("action: close card");
        showRecipeList();
        closeBtn=null;
      },false);
  }
}

function editCard(editCardID) {
  var editBtn = document.querySelector('.editBtn');
  editBtn.addEventListener("click", function() {
    ReactDOM.render(<RecipeBox action="edit" cardID={editCardID}/>, document.querySelector('#app'));
    var saveBtn = document.querySelector('.saveRecipe');
    closeCard();
    saveBtn.addEventListener("click", function() {
      sendToLocalStorage("edit");
    },false);

  },false);
}

function deleteCard(deleteCardID) {
  var deleteBtn = document.querySelector('.deleteBtn');
  deleteBtn.addEventListener("click", function() {
    deleteFromLocalStorage(deleteCardID);
  },false);
}

function sendToLocalStorage(update) {
  var recipeForm = document.querySelector('.recipeForm');
  recipeForm.addEventListener("submit", function(e){
    e.preventDefault();
    var titleToSend = e.target.elements.title.value;
    var ingredientsToSend = e.target.elements.ingredients.value;
    var cardID = e.target.elements.cardID.value;

    if (update === "add") {
      var objToSend = {title: titleToSend, ingredients: ingredientsToSend};
      output.push(objToSend);
      console.log("Added to LS");
    }
    if (update==="edit") {
      output[cardID].title = titleToSend;
      output[cardID].ingredients = ingredientsToSend;
      console.log("Edited Card in LS");
    }
    var stringToSend = JSON.stringify(output);
    localStorage.setItem('_sebek78_recipebox', stringToSend);
    readLocalStorage();
    showRecipeList();
  },false);
}

function deleteFromLocalStorage(deleteCardID) {
        output.splice(deleteCardID,1);
        var stringToSend = JSON.stringify(output);
        localStorage.setItem('_sebek78_recipebox', stringToSend);
        console.log("action: deleted card");
        readLocalStorage();
        showRecipeList();
}

readLocalStorage();
showRecipeList();

              
            
!
999px

Console