123

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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!--Navbar-->
<nav class="navbar navbar-dark bg-dark" id='navbar-group'>
  <div class="container">
    <div class="nav-left d-flex">
      <a href="#banner" class="navbar-brand pr-2">
        <i class='fas fa-handshake text-success'></i>
      </a>
      <ul class="navbar-nav flex-row">
        <li class="nav-item d-flex align-items-center">
          <a class="nav-link p-0" href="#banner">
            <span class="d-none d-md-block favorite-btn">Favorite</span>
            <i class='fas fa-heart pr-1 d-block d-md-none favorite-btn'></i>
          </a>
        </li>
        <li class="nav-item pl-4 d-flex align-items-center">
          <a class="nav-link p-0" href="#">
            <span class="d-none d-md-block filter-btn">Filters</span>
            <i class="fas fa-filter d-block d-md-none filter-btn"></i>
          </a>
        </li>
      </ul>
    </div>
    <div class="nav-right">
      <form class="form-inline" id='nav-search'>
        <div class="input-group justify-content-end">
          <div class="input-group-prepend">
            <span class="input-group-text" id="inputGroupPrepend"><i class="fas fa-search"></i></span>
          </div>
          <input type="text" class="form-control" placeholder="Search for a friend" aria-describedby="inputGroupPrepend" required>
        </div>
      </form>
    </div>
  </div>
</nav>

<!--Filter Section-->
<form action="" id="filter">
  <fieldset class="form-group" id='gender'>
    <legend class="col-form-label col-sm-2 mb-2 p-0 filter-title">Gender</legend>
    <div class="d-flex" id='filter-gender'>
      <div class="form-check form-check-inline">
        <input class="form-check-input" type="checkbox" id="male" value="male" checked>
        <label class="form-check-label" for="male">Male</label>
      </div>
      <div class="form-check form-check-inline">
        <input class="form-check-input" type="checkbox" id="female" value="female" checked>
        <label class="form-check-label" for="female">Female</label>
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group" id='age'>
    <legend class="col-form-label col-sm-2 mb-2 p-0 filter-title">Age</legend>
    <div class="d-flex flex-wrap" id='filter-age'>
      <!--Display Age Options-->
    </div>
  </fieldset>
  <fieldset class="form-group" id='region'>
    <legend class="col-form-label col-sm-2 mb-2 p-0 filter-title">Region</legend>
    <select class="form-control form-control-sm" id='filter-region'>
      <option>All</option>
    </select>
  </fieldset>
</form>

<!--Banner-->
<section class='container-fluid p-0 position-relative' id='banner'>
  <div class='cover-photo'></div>
  <div class='overlay'></div>
  <div class='container banner-title position-relative'>
    <h2 class='text-white'>Your Connection<br>Starts Right Here</h2>
  </div>
</section>

<!--Data Panel-->
<div class='container mt-5'>
  <div id='data-panel' class='row'>
    <!--Display Profile Card-->
  </div>
</div>

<!-- Modal-->
<div class='modal fade' id='profileDetail' tabindex='-1' role='dialog' aria-labelledby='exampleModalLongTitle' aria-hidden='true'>
  <div class='modal-dialog modal-dialog-centered modal-lg' role='document'>
    <div class='modal-content'>
      <div class='modal-body'>
        <div class='container-fluid'>
          <div class='row'>
            <!--Avatar-->
            <div class='col-12 col-lg-4'>
              <img id='profile-detail-image' class='w-100 h-auto' src='' alt='profile photo' class='img-fluid'>
            </div>
            <!--Profile Detail Info-->
            <div class='col-12 col-lg-8 py-4 text-secondary position-relative' id='profile-detail-info'>
              <h1 class='mb-4 text-dark' id='profile-detail-name'></h1>
              <p id='profile-detail-age'></p>
              <p id='profile-detail-birth'></p>
              <p id='profile-detail-region'></p>
              <ul class='text-center'>
                <li><img src='' alt='country flag' id='profile-detail-flag'></li>
                <li class='mt-3'><span class='badge' id='profile-detail-gender'></span></li>
              </ul>
            </div>
          </div>
        </div>
      </div>
      <div class='modal-footer text-white'>
        <a type='button' class='btn btn-primary' id='profile-detail-connect' href=''><i class='far fa-envelope pr-2'></i>Connect
          Now</a>
        <a type='button' class='btn btn-secondary' data-dismiss='modal'>Close</a>
      </div>
    </div>
  </div>
</div>

<!--Pagination Section-->
<div class="container">
  <nav aria-label="Page navigation">
    <ul class="pagination justify-content-center" id='pagination'>
      <!--Display page item-->
    </ul>
  </nav>
</div>

<!--To top button-->
<a href="#banner" class="btn btn-danger btn-lg position-fixed" role="button" id='to-top-btn'>
  <h5 class='m-0'><i class="far fa-caret-square-up mr-2 d-none d-lg-inline"></i>Top</h5>
</a>
            
          
!
            
              /*Initiallization*/
* {
  box-sizing: border-box;
}

body {
  background: #f0f0f0;
}

li {
  list-style-type: none;
}

a,
a:hover {
  text-decoration: none;
}

/*Filter Section*/
#filter {
  font-size: 16px;
  display: none;
  background: RGBA(144,144,144,0.75);
  position: fixed;
  top: 68px;
  left: 18%;
  z-index: 999;
  padding: 1em;
  width: 13em;
}

#filter .filter-title{
  color: white;
  font-size: 1.1em;
  font-weight: 600;
}

#filter .form-control {
  width: 100%;
  color: #505050;
  font-weight: 500;
}

#filter label {
  font-weight: 500;
}

/*Navbar Section*/
#navbar-group {
  font-size: 16px;
  position: fixed;
  z-index: 999;
  width: 100%;
}

i.fa-handshake {
  font-size: 2em;
}


#nav-search .form-control {
  width: 32%;
  flex: 0 1 auto;
  background: RGBA(52,58,64,0.45);
}

#nav-search .form-control:focus {
  flex: 1;
  color: white;
  -moz-transition: all .5s linear;
  -o-transition: all .5s linear;
  -webkit-transition: all .5s linear;
  transition: all .5s linear;
  border-color: #e9ecef;
  box-shadow: none;
}

/*Banner Section*/
.cover-photo {
  width: 100%;
  height: 310px;
  background-image: url("https://images.pexels.com/photos/296881/pexels-photo-296881.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
}

.banner-title {
  font-size: 16px;
}

.banner-title h2 {
  position: absolute;
  bottom: 0;
  right: 0;
  text-shadow: 2px 2px #505050;
  font-size: 3em;
}

/*Data Panel Section: General*/
#data-panel figure {
  overflow: hidden;
}

#data-panel img.card-img-top {
  cursor: pointer;
  -webkit-transform: scale(1);
  transform: scale(1);
}

#data-panel img.card-img-top:hover {
  opacity: 0.7;
  -webkit-transition: all 0.3s linear;
  -moz-transition: all 0.3s linear;
  -o-transition: all 0.3s linear;
  transition: all 0.3s linear;
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}

#data-panel i.fa-heart {
  cursor: pointer;
  -webkit-transition: all 0.1s linear;
  -moz-transition: all 0.1s linear;
  -o-transition: all 0.1s linear;
  transition: all 0.1s linear;
}

.gray-heart {
  color: #c0c0c0;
}

/*Data Panel Section: Favorite*/
#data-panel i.fa-heart:hover,
.red-heart {
  color: #c80000;
  text-shadow: 2px 2px #e0e0e0;
}

#empty-message {
  margin: 3em auto 0 auto;
  font-size: 16px;
  color: #b0b0b0;
  text-shadow: 1px 1px #888888;
}

#empty-message h1 {
  font-size: 4em;
}

#empty-message h3 {
  font-size: 1.8em;
}

#empty-message .fa-handshake {
  font-size: 1.2em;
}

/*Modal Section*/
#profile-detail-info {
  font-size: 16px;
}

#profile-detail-info h1 {
  font-size: 1.9em;
}

#profile-detail-info p {
  margin: 0;
  font-size: 1.1em;
  line-height: 1.8em;
}

#profile-detail-info ul {
  position: absolute;
  top: 28px;
  right: 25px;
}

#profile-detail-flag {
  width: 2.5em;
  height: auto;
}

#profile-detail-gender {
  background: #e8e8e8;
}

/*To top button*/
#to-top-btn {
  bottom: 3%;
  right: 2%;
}

/*Large Devices*/
@media screen and (min-width: 992px) {
  /*Filter Section*/
  #filter {
    font-size: 18px;
  }
  
  /*Banner Section*/
  .cover-photo {
    height: 400px;
  }
  
  .banner-title {
    font-size: 20px;
  }
  
  .banner-title h2 {
    bottom: 1em;
  }

  /*Modal Section*/
  #profile-detail-info {
    font-size: 19px;
  }

/*Extra Large Devices*/
@media screen and (min-width: 1200px) {
  /*Filter Section*/
  #filter {
    font-size: 19px;
    width: 15em;
  }
  
  /*Banner Section*/
  .cover-photo {
    height: 550px;
  }
  
  .banner-title h2 {
    bottom: 2em;
  }
  
  .banner-title {
    font-size: 25px;
  }
}

            
          
!
            
              (function(){  
  const BASE_URL = 'https://lighthouse-user-api.herokuapp.com'
  const INDEX_URL = BASE_URL + '/api/v1/users/'
  const data = []
  const navbarGroup = document.querySelector('#navbar-group')
  const searchForm = document.getElementById('nav-search')
  const searchFormInput = document.querySelector('#nav-search input')
  const filterForm = document.getElementById('filter')
  let filteredData = [] 
  const dataPanel = document.querySelector('#data-panel')
  const myFavorite = document.querySelector('#my-favorite')
  const pagination = document.getElementById('pagination')
  let paginationData = []
  const ITEM_PER_PAGE = 12
  let currentPage = 1
  let onFavoritePage = false
  
  function displayAgeOptions(data) {
    const oldest = data.reduce((accumulator, currentValue) => Math.max(accumulator, currentValue.age), 0)
    const youngest = data.reduce((accumulator, currentValue) => Math.min(accumulator, currentValue.age), 99)
    
    const lastTier = Math.ceil(oldest / 5)
    const firstTier = Math.floor(youngest / 5)
    
    for(let i = firstTier; i < lastTier; i++) {
      document.getElementById('filter-age').innerHTML += `
        <div class="form-check form-check-inline">
          <input class="form-check-input" type="checkbox" id="tier${i}" value="${5 * i}" checked>
          <label class="form-check-label" for="tier${i}">${5 * i} - ${5 * i + 4}</label>
        </div>
      `
    }
  }
 
  function displayRegionOptions(data) {
    const regionData = []
    
    //find all unique country names
    data.forEach(item => {
      if (regionData.indexOf(item.region) < 0) regionData.push(item.region)
    })
    
    //place all unique country names into filter pannel
    regionData.sort().forEach(item => {
      document.getElementById('filter-region').innerHTML += `
        <option>${item}</option>
      `
    })
  }
  
  function updatePaginationStatus(pageNumber) {
    pagination.children[pageNumber].classList.toggle('active')
  }
  
  function getTotalPages(data) {
    let totalPages =  Math.ceil(data.length / ITEM_PER_PAGE) || 1
    
    //add previous page link
    pagination.innerHTML = `
      <li class="page-item">
        <a class="page-link" href="#" aria-label="Previous" data-page='previous'>&laquo;</a>
      </li>
    `
  
    //add page number link
    for (let i = 0; i < totalPages; i++) {
      pagination.innerHTML += `
        <li class="page-item"><a class="page-link" href="#" data-page=${i + 1}>${i + 1}</a></li>
      `
    }
    
    //add next page link
    pagination.innerHTML += `
      <li class="page-item">
        <a class="page-link" href="#" aria-label="Next" data-page='next'>&raquo;</a>
      </li>
    `
    
    //add active status to first page link
    updatePaginationStatus(1)
    
    //upsate current page number
    currentPage = 1
  }
  
  function displayProfile(data) {
    dataPanel.innerHTML = data.map(person => {
      //if the profile has been selected as favorite add red-heart class
      redHeartClass = person.favorite === true ? 'red-heart' : ''
      //return display content
      return `
        <div class='col-md-6 col-xl-4 mb-4' id='profile'>
          <div class='card h-100 shadow'>
            <figure>
              <img src=${person.avatar} class='card-img-top' alt='profile photo' data-toggle='modal' data-target='#profileDetail' data-id='${person.id}'>
            </figure>
            <div class='card-body text-capitalize row'>
              <div class='col-10'>
                <h5 class='card-title'>${person.name} ${person.surname}</h5>
                <p class='card-text text-secondary'>
                  <i class='fas fa-map-marker-alt mr-2'></i>${person.region}
                </p>
              </div>
              <div class='col-2'>
                <h2><i class='fas fa-heart gray-heart ${redHeartClass}' data-id='${person.id}'></i></h2>
              </div>
            </div>
            <div class='card-footer text-muted text-center'>
              <a href='mailto:${person.email}' class='card-link'><i class='far fa-envelope pr-2'></i>Connect Now</a>
            </div>
          </div>
        </div>
      `
    }).join('')
  }

  function displayEmptyMessage() {
    dataPanel.innerHTML = `
      <div class='text-center mb-5' id='empty-message'>
        <h1><i class='far fa-grin-beam-sweat'></i></h1>
        <h3>No one is here</h3>
        <p class='my-3'>(Click <i class='fas fa-handshake'></i> on top to home page)</p>
      </div>
    `
  }
  
  function getPageData(pageNumber, data) {
    paginationData = data || paginationData
    
    if (paginationData.length === 0) return displayEmptyMessage()
    
    //if data is not empty, find page for the page
    let firstDisplayDataIndex = (pageNumber - 1) * ITEM_PER_PAGE
    let displayData = paginationData.slice(firstDisplayDataIndex, firstDisplayDataIndex + ITEM_PER_PAGE)
    
    //display the page
    displayProfile(displayData)
  }
  
  function updateFilterStatus() {
    //update gender and age sections
    const allInput = [...document.querySelectorAll('#filter input')]
    allInput.forEach(item => item.checked = true)
    
    //update region section
    document.querySelector('#filter-region').value = 'All'
  }  
  
  function filterGender() {
    //select elements
    const genderChoices = [...document.querySelectorAll('#filter-gender input')]
    const maleCheck = genderChoices[0].checked
    const femaleCheck = genderChoices[1].checked
    
    if (maleCheck && femaleCheck) return filteredData
    if (maleCheck) return filteredData = filteredData.filter(item => item.gender === 'male')
    if (femaleCheck) return filteredData = filteredData.filter(item => item.gender === 'female')
    
    filteredData = []
  }

  function filterAge() {
    const ageChoices = [...document.querySelectorAll('#filter-age input')]
    let tempResult = []
    
    //find all age tierd that are checked
    const genderChecks = ageChoices.filter(item => item.checked === true)      
    
    //find filtered data
    genderChecks.forEach(item => {
      const value = Number(item.value)
      const result = filteredData.filter(profile => Number(profile.age) >= value && Number(profile.age) < value + 5)
      tempResult = [...tempResult, ...result]
    })
    filteredData = tempResult
  }
  
  function filterRegion() {
    const regionChoice = document.querySelector('#filter-region').value
    if (regionChoice === 'All') return filteredData
    
    //find filtered data based on country selected
    filteredData = filteredData.filter(item => item.region === regionChoice)
  }
  
  function createFlag(region) {
    //modify country name for special regions
    if (region === 'England') region = 'United Kingdom'
    
    const modalFlag = document.getElementById('profile-detail-flag')
    const BASE_URL = 'https://restcountries.eu'
    const FLAG_URL = BASE_URL + '/rest/v2/name/' + region + '?fields=flag'
   
    //send request to REST Countries API
    axios
      .get(FLAG_URL)
      .then(response => modalFlag.src = response.data[0].flag)
      .catch(error => {     
          modalFlag.src = ''
          console.log(error)
      })
  }
  
  function showProfileDetail(id) {
    //get elements
    const modalImage = document.getElementById('profile-detail-image')
    const modalName = document.getElementById('profile-detail-name')
    const modalAge = document.getElementById('profile-detail-age')
    const modalBirth = document.getElementById('profile-detail-birth')
    const modalRegion = document.getElementById('profile-detail-region')
    const modalGender = document.getElementById('profile-detail-gender')
    const modalEmail = document.getElementById('profile-detail-connect')
    
    //set request url
    const SHOW_API = INDEX_URL + id
    
    //send request to SHOW API
    axios
      .get(SHOW_API)
      .then(response => {
        const data = response.data
        modalImage.src = data.avatar
        modalName.textContent = `${data.name} ${data.surname}`
        modalAge.textContent = `Age: ${data.age}`
        modalBirth.textContent = `Born in: ${data.birthday}`
        modalRegion.textContent = `Nationality: ${data.region}`
        modalEmail.href = `mailto:${data.email}`
        modalGender.textContent = (data.gender === 'male') ? 'Male' : 'Female'
        createFlag(data.region)
    })
  }
  
  function removeFavoriteProfile(profile) {
    profile.parentElement.removeChild(profile)
    if (dataPanel.children.length === 0) displayEmptyMessage()
  }
  
  //Send request to Index API
  axios
    .get(INDEX_URL)
    .then(response => {
      response.data.results.forEach(profileData => {
        data.push({...profileData, favorite: false})
      })
      displayAgeOptions(data)
      displayRegionOptions(data)
      getTotalPages(data)
      getPageData(currentPage, data)
    })
    .catch(error => console.log(error))
  
  //add submit event to search form
  searchForm.addEventListener('submit', event => event.preventDefault())
  
  //add click event listener to navbar
  navbarGroup.addEventListener('click', event => {
    
    if (event.target.matches('.fa-handshake')) {
      getTotalPages(data)
      getPageData(currentPage, data)
      onFavoritePage = false
      updateFilterStatus()
      searchFormInput.value = ''
    } else if (event.target.matches('.favorite-btn')) {
      const favoriteData = data.filter(person => person.favorite )
      getTotalPages(favoriteData)
      getPageData(currentPage, favoriteData)
      onFavoritePage = true
    } else if (event.target.matches('.filter-btn')) {
      filterForm.classList.toggle('d-block')
    }
  })
  
  //add input event listener to form input
  searchFormInput.addEventListener('input', () => {
    onFavoritePage = false
    
    //create a regular expression
    const regex = new RegExp(searchFormInput.value, 'i')
    
    //find matching results
    const results = data.filter(item => `${item.name} ${item.surname}`.match(regex))
    
    //update pagination
    getTotalPages(results)
    
    //display results
    getPageData(currentPage, results)
  })
  
  //Add Event listener to filter Form
  filterForm.addEventListener('input', event => {
    onFavoritePage = false
    filteredData = data
    filterGender()
    filterAge()
    filterRegion()
    getTotalPages(filteredData)
    getPageData(currentPage, filteredData)
  })
  
  //Add event listener to dataPanel
  dataPanel.addEventListener('click', event => {
    if (event.target.matches('.card-img-top')) {
      showProfileDetail(event.target.dataset.id)
    } else if (event.target.matches('.fa-heart')) {
      event.target.classList.toggle('red-heart')
      let id = event.target.dataset.id
      
      //update profile data's favorite status 
      data[id-1].favorite = data[id-1].favorite ? false : true
      
      //if the user is on the favorite page remove that profile
      if (!onFavoritePage) { return }
      let profile = event.target.closest('#profile')
      removeFavoriteProfile(profile)
    }
  })
  
  //Add event listener to pagination
  pagination.addEventListener('click', event => {
    //remove pagination link active status
    updatePaginationStatus(currentPage)
    
    switch (event.target.dataset.page) {
      case 'previous': 
        currentPage = currentPage - 1 || 1
        break
      case 'next':
        const totalPage = pagination.children.length - 2
        currentPage = Math.min(currentPage + 1, totalPage)
        break
      default: 
        currentPage = Number(event.target.dataset.page)
    }
    //display page content
    getPageData(currentPage)
    
    //remove pagination link active status
    updatePaginationStatus(currentPage)
  })
  
})()
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console