cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <header class="site-header">
  <div class="site-title">
    <a id="post-view-all">A Serverless Blog</a>
  </div>
  <div class="site-subtitle">
    All the code that powers this blog is right here.
  </div>
  <a id="sign-in-button" class="button sign-in-button">
    <svg width="24" height="24" viewBox="0 0 24 24">
      <path d="M12,4A4,4 0 0,1 16,8A4,4 0 0,1 12,12A4,4 0 0,1 8,8A4,4 0 0,1 12,4M12,14C16.42,14 20,15.79 20,18V20H4V18C4,15.79 7.58,14 12,14Z"></path>
    </svg> Sign In
  </a>
  <a id="new-post-button" class="button sign-in-button" style="display:none">
    <svg width="24" height="24" viewBox="0 0 24 24">
  <path d="M20.71,4.04C21.1,3.65 21.1,3 20.71,2.63L18.37,0.29C18,-0.1 17.35,-0.1 16.96,0.29L15,2.25L18.75,6M17.75,7L14,3.25L4,13.25V17H7.75L17.75,7Z"></path>
</svg> New Post
  </a>
</header>

<main class="site-wrap">

  <section id="new-post" class="article-form" style="display: none;">
    <h2>New Post</h2>
    <form id="message-form" action="#">
      <h4><label for="new-post-title">Title</label></h4>
      <input type="text" id="new-post-title">
      <br />
      <br />
      <h4><label for="new-post-message">Message</label></h6>
      <textarea rows="3" id="new-post-content"></textarea>
      <br />
      <br />
      <button type="submit">Add post</button>
     </form>
   </section>

  <div id="articles-list">
    <div class="articles-container" />
  </div>
  
  <article id="article-whole" class="article-whole start-hidden" >
    <div></div>
  </article>
  
</main>


<script src="https://www.gstatic.com/firebasejs/4.8.0/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "AIzaSyDEpThYG_vYML09Q5HEY0RtkqzqGtq1XZM",
    authDomain: "duncan-131.firebaseapp.com",
    databaseURL: "https://duncan-131.firebaseio.com",
    projectId: "duncan-131",
    storageBucket: "duncan-131.appspot.com",
    messagingSenderId: "631406093601"
  };
  firebase.initializeApp(config);
</script>
  
            
          
!
            
              * {
  box-sizing: border-box;
}

.site-header {
  border-top: 1rem solid #556CF6;
  background: #26232d;
  padding: 2rem;
  position: relative;
}
.site-title > a {
  border-bottom: none;
  color: white;
  padding: 0;
}
.site-title {
  font-size: 2rem;
  letter-spacing: 3px;
}
.site-subtitle {
  font-style: italic;
  opacity: 0.5;
}
.sign-in-button {
  position: absolute;
  top: 1rem;
  right: 1rem;
}

.site-wrap {
  padding: 2rem;
}

.article-form,
.article-block {
  max-width: 650px;
  margin: 0 auto 2rem;
  background: #403E4F;
  padding: 2rem;
  box-shadow: 0.25rem 0.25rem 1.3rem rgba(0, 0, 0, 0.33);
}
time {
  display: block;
  padding: 0.25rem 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.25rem;
  background: linear-gradient(
    to right,
    #4a4c5a,
    #403E4D
  );
  font-size: 0.75rem;
}
.article-body {
  color: #9e9fa8;
}

.article-whole {
  max-width: 850px;
  margin: 0 auto;
}


body {
  margin: 0;
  background: #323041;
  color: white;
  font-family: 'PT Serif', serif;
  line-height: 1.4;
}
h1, h2, h3, h4, h5, h6,
.site-title {
  font-family: 'Barlow Semi Condensed', sans-serif;
  text-transform: uppercase;
}
h1, h2, h3, h4, h5, h6 {
  margin: 0 0 1rem 0;
  letter-spacing: 0.1rem;
  line-height: 1;
}
h1 {
  font-size: 3.0rem;
  letter-spacing: 0.1rem;
}
a {
  color: #8b9bff;
  font-weight: bold;
  border-bottom: 1px solid;
  text-decoration: none;
  padding: 0.25rem 0.5rem;  
}
a svg {
  fill: currentcolor;
  vertical-align: bottom;
}
a:hover,
a:focus {
  background: rgba(0, 0, 0, 0.33);
}
.start-hidden {
  display: none;
}
input, textarea {
  width: 100%;
}
            
          
!
            
              var blog_api_url = 'https://us-central1-duncan-131.cloudfunctions.net/posts';
var posts_list = document.getElementById('articles-list');
var post_full = document.getElementById('article-whole');
var posts_container = posts_list.querySelector('.articles-container');

// track authenticated user to avoid triggering on refresh
var currentUID;

var loadJsonFromFirebase = function(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", function () {
        callback(JSON.parse(this.response));
    });
    xhr.open("GET", url);
    xhr.send();
};

var getQueryParam = function(param) {
  let params = window.location.search.substr(1);
  params = params.split("&");
  let paramList = {};
  for (let i = 0; i < params.length; i++) {
    let tmp = params[i].split("=");
    paramList[tmp[0]] = decodeURI(tmp[1]);
  }
  return paramList[param];
}

const getAnchorParam = function() {
  return (window.location.href.split('#').length > 1) ? window.location.href.split('#')[1] : null;
}

// hide the individual post and show the list of posts
const showListClick = (e) => {
  // hide the single post
  post_full.classList.add('start-hidden');
  // show the full list
  posts_container.classList.remove('start-hidden');
  // adjust the URL back to the full list
  if (!e.skipPushState) {
    history.pushState({ post_id: 'full-list' }, null, window.location.href.split('#')[0]);
  }
}

// handle selecting the links in the list
const showPostClick = (e) => {
  let post_id = e.target.dataset.post;

  if (post_id) {
    // load the post from ajax call
    loadJsonFromFirebase(blog_api_url + '/' + post_id, function(data) {
      let div = document.createElement('div');
      div.innerHTML = renderPost(post_id, data, false);
      post_full.replaceChild(div, post_full.firstChild);

      // hide the full list
      posts_container.classList.add('start-hidden');
      // show the single post
      post_full.classList.remove('start-hidden');
    });

    if (!e.skipPushState) {
      // update the URL 
      history.pushState( { post_id: post_id }, null, '#/' + post_id);
    }
  }
};

// toggle buttons on sign in/out auth changes
const onLogInOutChange = function(user) {

  // Ignore token refresh events
  if (user && currentUID === user.uid) {
    return;
  }

  // If logged in, show the new post button
  if (user) {
    currentUID = user.uid;
    document.getElementById('sign-in-button').style.display = 'none';
    document.getElementById('new-post-button').style.display = 'block';
  } else {
    currentUID = null;
    document.getElementById('sign-in-button').style.display = 'block';
    document.getElementById('new-post-button').style.display = 'none';
  }
}

const renderPost = function(postId, postData, summary = false) {
  let ts = new Date(postData.created);
  if (summary) {
    return `<article class="article-block">
      <h2>${postData.title}</h2>
      <time>${ts.toDateString()}</time>
      <div class="excerpt">
        <p>${postData.content.substr(0, 150)}...</p>
      </div>
      <a data-post="${postId}">Read Post</a>
    </article>`;
  } else {
    return `<article class="article-block">
      <h2>${postData.title}</h2>
      <time>${ts.toDateString()}</time>
      <div class="excerpt">
        <p>${postData.content}</p>
      </div>
    </article>`;
  }
}

// Bindings on load.
document.addEventListener('DOMContentLoaded', function() {

  document.getElementById('sign-in-button').addEventListener('click', function() {
    var provider = new firebase.auth.GoogleAuthProvider();
    firebase.auth().signInWithPopup(provider);
  });

  // Listen for auth state changes
  firebase.auth().onAuthStateChanged(onLogInOutChange);
  
  // show the new post form
  document.getElementById('new-post-button').addEventListener('click', function() {
    document.getElementById('new-post').style.display = '';
  });
  
  // Saves message on form submit.
  let messageForm = document.getElementById('message-form');
  messageForm.onclick = function(e) {
    e.preventDefault();
    let postTitle = document.getElementById('new-post-title');
    let postContent = document.getElementById('new-post-content');
    let title = postTitle.value;
    let content = postContent.value;

    if (content) {
      firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function (idToken) {
        var xhr = new XMLHttpRequest();
        xhr.addEventListener("load", function () {
          // on success, display the post at the top of the list & hide the form
          postTitle.value = '';
          postContent.value = '';
          document.getElementById('new-post').style.display = 'none';

          let postDetails = JSON.parse(this.response);
          let div = document.createElement('div');
          div.innerHTML += renderPost(postDetails.id, postDetails, false);
          posts_container.insertBefore(div, posts_container.firstChild);
        });
        xhr.open("POST", blog_api_url);
        xhr.setRequestHeader("Content-Type", "application/json");
        xhr.send(JSON.stringify({"title": title, "content": content, "token": idToken}));
      }).catch(function (error) {
        // TODO: handle error, not authorized
      });
    } else {
      // TODO: display box around the missing content field
    }
  }
});

document.getElementById('post-view-all').addEventListener('click', showListClick);
posts_list.addEventListener('click', showPostClick);

window.addEventListener('popstate', function(e) {
  let post_id = e.state ? (e.state.post_id ? e.state.post_id : null) : null;
  // when the post_id is null, we don't have any managed history, so do nothing
  e.skipPushState = true;
  if (post_id == null) { 
    return; 
  } else if (post_id == 'full-list') {
    showListClick(e);
  } else {
    e.target.dataset = e.target.dataset ? e.target.dataset : {};
    e.target.dataset.post = post_id;
    showPostClick(e);
  }
});

// and load everything up
let post_id = getAnchorParam();
if (post_id) {
  loadJsonFromFirebase(blog_api_url + post_id, function(postData) {
    let list = document.createElement('div');
    list.innerHTML += renderPost(postData.id, postData, true);
    posts_container.insertBefore(list, posts_container.firstChild);
  });
} else {
  loadJsonFromFirebase(blog_api_url, function(data) {
    let list = document.createElement('div');
    Object.keys(data).forEach(function(key) {
      list.innerHTML += renderPost(key, data[key], true);
    });
    posts_container.insertBefore(list, posts_container.firstChild);
  });
}

            
          
!
999px
Loading ..................

Console