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.

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

            
              <header>
  <h1>Autocomplete</h1>
  <p>Autocomplete demonstration with vanilla JavaScript and <a href='http://fusejs.io/' target='_blank'>Fuse.js</a>.</p>
</header>
<main>
  <form class='search-form'>
    <div class='form-row'>
      <div class='search-input-container'>
        <input class='search-input' name='search-input' placeholder='Search'>
        <div class='search-auto-complete'></div>
      </div>
      <button type='submit' class='search-submit'>
        <svg viewBox='0 0 24 24'>
          <path d='M9.5,3A6.5,6.5 0 0,1 16,9.5C16,11.11 15.41,12.59 14.44,13.73L14.71,14H15.5L20.5,19L19,20.5L14,15.5V14.71L13.73,14.44C12.59,15.41 11.11,16 9.5,16A6.5,6.5 0 0,1 3,9.5A6.5,6.5 0 0,1 9.5,3M9.5,5C7,5 5,7 5,9.5C5,12 7,14 9.5,14C12,14 14,12 14,9.5C14,7 12,5 9.5,5Z' />
        </svg>
      </button>
    </div>
  </form>
  <div class='result'>
</main>
            
          
!
            
              $color-primary: #1c5b72;

main {
  margin-top: 64px;
}

.search-form {
  margin: 0 auto;
  padding: 0 16px;
  width: 100%;
  max-width: 600px;
}

.form-row {
  width: 100%;
  display: flex;
  align-items: flex-start;
}

.search-input-container {
  flex: 1;
}

input,
button {
  font-family: inherit;
}

.search-input {
  border: 1px solid rgba(0, 0, 0, 0.54);
  width: 100%;
  padding: 8px;
  font-size: 16px;
  line-height: 1.5;
}

.search-submit {
  border: 1px solid $color-primary;
  padding: 8px 16px;
  background: $color-primary;
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-submit svg {
  width: 24px;
  height: 24px;
  fill: #fff;
}

.search-auto-complete {
  display: none;
  position: absolute;
  margin-top: -1px;
  border: 1px solid rgba(0, 0, 0, 0.54);
  padding: 4px 0;
  width: 100%;
  z-index: 1;
  background: #fff;
  transition: none;
}

.search-auto-complete.show {
  display: block;
}

.search-auto-complete ul {
  margin: 0;
  padding: 0;
  list-style: none;
  transition: none;
}

.auto-complete-item {
  padding: 4px 8px;
}

.auto-complete-item:hover {
  cursor: pointer;
  background: rgba(0, 0, 0, 0.12);
}

.auto-complete-item.selected {
  background: rgba(0, 0, 0, 0.12);
}

/* General layout and typography stuff */
@import url("https://fonts.googleapis.com/css?family=Open+Sans:300,400");

* {
  box-sizing: border-box;
  position: relative;
  transition: all .3s ease
}

html {
  font-size: 16px
}

body {
  font-family: Open Sans, Verdana, sans-serif;
  color: rgba(0, 0, 0, .87);
  font-weight: 400;
  line-height: 1.45
}

body,
header {
  background: #fafafa
}

header {
  padding: 40px;
  min-height: 200px;
  text-align: center;
  color: rgba(0, 0, 0, .87)
}

header > * {
  max-width: 800px;
  margin-left: auto;
  margin-right: auto
}

header>:last-child {
  margin-bottom: 0
}

h1 {
  margin-bottom: 0.5em;
  font-weight: inherit;
  line-height: 1.2;
  color: #1c5b72;
  font-size: 2.618em
}

p {
  margin-bottom: 1.3em;
  line-height: 1.618
}

@media (min-width:800px) {
  h1 {
    font-size: 4.236em;
    font-weight: 300
  }
  
  p {
    font-size: 1.3em
  }
}

a {
  color: #e03616;
  text-decoration: none
}

            
          
!
            
              const searchData = [
  {
    title: "Old Man's War",
    author: {
      firstName: "John",
      lastName: "Scalzi"
    }
  },
  {
    title: "The Lock Artist",
    author: {
      firstName: "Steve",
      lastName: "Hamilton"
    }
  },
  {
    title: "HTML5",
    author: {
      firstName: "Remy",
      lastName: "Sharp"
    }
  },
  {
    title: "Right Ho Jeeves",
    author: {
      firstName: "P.D",
      lastName: "Woodhouse"
    }
  },
  {
    title: "The Code of the Wooster",
    author: {
      firstName: "P.D",
      lastName: "Woodhouse"
    }
  },
  {
    title: "Thank You Jeeves",
    author: {
      firstName: "P.D",
      lastName: "Woodhouse"
    }
  },
  {
    title: "The DaVinci Code",
    author: {
      firstName: "Dan",
      lastName: "Brown"
    }
  },
  {
    title: "Angels & Demons",
    author: {
      firstName: "Dan",
      lastName: "Brown"
    }
  },
  {
    title: "The Silmarillion",
    author: {
      firstName: "J.R.R",
      lastName: "Tolkien"
    }
  },
  {
    title: "Syrup",
    author: {
      firstName: "Max",
      lastName: "Barry"
    }
  },
  {
    title: "The Lost Symbol",
    author: {
      firstName: "Dan",
      lastName: "Brown"
    }
  },
  {
    title: "The Book of Lies",
    author: {
      firstName: "Brad",
      lastName: "Meltzer"
    }
  },
  {
    title: "Lamb",
    author: {
      firstName: "Christopher",
      lastName: "Moore"
    }
  },
  {
    title: "Fool",
    author: {
      firstName: "Christopher",
      lastName: "Moore"
    }
  },
  {
    title: "Incompetence",
    author: {
      firstName: "Rob",
      lastName: "Grant"
    }
  },
  {
    title: "Fat",
    author: {
      firstName: "Rob",
      lastName: "Grant"
    }
  },
  {
    title: "Colony",
    author: {
      firstName: "Rob",
      lastName: "Grant"
    }
  },
  {
    title: "Backwards, Red Dwarf",
    author: {
      firstName: "Rob",
      lastName: "Grant"
    }
  },
  {
    title: "The Grand Design",
    author: {
      firstName: "Stephen",
      lastName: "Hawking"
    }
  },
  {
    title: "The Book of Samson",
    author: {
      firstName: "David",
      lastName: "Maine"
    }
  },
  {
    title: "The Preservationist",
    author: {
      firstName: "David",
      lastName: "Maine"
    }
  },
  {
    title: "Fallen",
    author: {
      firstName: "David",
      lastName: "Maine"
    }
  },
  {
    title: "Monster 1959",
    author: {
      firstName: "David",
      lastName: "Maine"
    }
  }
]

const searchFormNode = document.querySelector('.search-form')
const searchInputNode = document.querySelector('.search-input')
const autoCompleteNode = document.querySelector('.search-auto-complete')
const resultNode = document.querySelector('.result')

let input = ''
let results = []
let selected = -1
let showAutoComplete = false

const fuse = new Fuse(
  searchData,
  {
    keys: ['title'],
    shouldSort: true,
    threshold: 0.5,
    maxPatternLength: 50
  }
)

function renderAutoComplete () {
  if (!showAutoComplete || input.length < 3 || results.length === 0) {
    autoCompleteNode.classList.remove('show')
    return ''
  } else {
    autoCompleteNode.classList.add('show')
  }

  return `
    <ul>
      ${results.map((result, index) => `
        <li class='auto-complete-item${index === selected ? ' selected': ''}'>
          ${result.title}
        </li>
      `).join('')}
    </ul>
  `
}

function handleSearchSubmit (event) {
  if (event) {
    event.preventDefault()  
  }
  
  input = searchInputNode.value.trim()
  selected = -1
  
  document.activeElement.blur()
  autoCompleteNode.innerHTML = renderAutoComplete()
  resultNode.innerHTML = `Searched for: ${input}`
}

function handleSearchInput (event) {
  input = event.target.value
  results = []
  if (input.length >= 3) {
    results = fuse.search(input).slice(0, 7)
  }
  showAutoComplete = true
  autoCompleteNode.innerHTML = renderAutoComplete()
}

function updateSearchInput () {
  if (selected === -1) {
    searchInputNode.value = input
  } else {
    searchInputNode.value = results[selected].title
  }
  autoCompleteNode.innerHTML = renderAutoComplete()
}

function handleSearchKeyDown (event) {
  switch (event.which) {
    case 38: // Arrow up
      selected = Math.max(--selected, -1)
      updateSearchInput()
      break
    case 40: // Arrow down
      selected = Math.min(++selected, results.length - 1)
      showAutoComplete = true
      updateSearchInput()
      break
    case 9: // Tab
      showAutoComplete = false
      updateSearchInput()
      break
    case 27: // Escape
      selected = -1
      showAutoComplete = false
      updateSearchInput()
      break
  }
}

function handleAutoCompleteClick (event) {
  event.stopPropagation() // Prevent click from bubbling to window click handler
  searchInputNode.value = event.target.textContent.trim()
  showAutoComplete = false
  handleSearchSubmit()
}

function handleWindowClick (event) {
  showAutoComplete = false
  autoCompleteNode.innerHTML = renderAutoComplete()
}

searchFormNode.addEventListener('submit', handleSearchSubmit)
document.querySelector('.search-input-container').addEventListener('keydown', handleSearchKeyDown)
searchInputNode.addEventListener('input', handleSearchInput)
autoCompleteNode.addEventListener('click', handleAutoCompleteClick)
window.addEventListener('click', handleWindowClick)

            
          
!
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.
Loading ..................

Console