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

              
                <main id="app">

 <search-select
  v-model="selectedItem"
  :options="bands"
 ></search-select>

</main>


<!-- VUE TEMPLATES -->
<script type="text/x-template" id="search-select">
 <div class="search-select" :class="{ 'is-active': isOpen }">

  <input
   @focus="open"
   @keydown.enter.prevent="selectHighlighted"
   @keydown.esc="close"
   @keydown.tab="close"
   @keydown.up="highlightPrev"
   @keydown.down="highlightNext"
   :placeholder="value || 'Please select an option'" 
   v-model="search"
   ref="search"
   type="text" 
   class="search-select-search" 
  >
  
  <svg
   @click="toggle"
   :class="{ open: isOpen }"
   class="search-select-icon"
   width="12px"
   height="8px"
   viewBox="0 0 12 8"
   version="1.1"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:xlink="http://www.w3.org/1999/xlink"
  >
   <g>
    <path d="M5.9403615,4.51983851 L9.61645133,1.12115286 C9.84663298,0.899160775 10.3144958,0.990807274 10.6620138,1.32596106 C11.0095319,1.66111484 11.1045593,2.11233169 10.8743777,2.33432377 L5.91888269,7 L4.61641777,5.74387512 L4.62256756,5.73818941 L1.12562234,2.33432377 C0.895440688,2.11233169 0.990468143,1.66111484 1.33798617,1.32596106 C1.68550421,0.990807274 2.15336702,0.899160775 2.38354867,1.12115286 L5.9403615,4.51983851 Z"></path>
   </g>
  </svg>


  <div v-show="isOpen" class="search-select-dropdown">

    <ul
      v-show="filteredOptions.length > 0"
      @click.prevent="selectClicked"
      @mouseover="highlightOption"
      ref="options"
      class="search-select-options"
    >
     <li
       v-for="(option, index) in filteredOptions"
       :key="option"
       :class="{ 'is-active': index === highlightedIndex}"
       class="search-select-option"
     >{{ option }}</li>
    </ul>

    <div
      v-show="filteredOptions.length === 0"
      class="search-select-empty"
    >No results found for "{{ search }}"</div>

   </div>

 </div>
</script>
              
            
!

CSS

              
                $input-border-thickness: 2px;
$color-input-border: #121e49;
$color-input-background: #fff;
$color-input-copy-disabled: #dedede;
$color-input-hover: #eeedf1;
$color-input-active: #1f1a5e;

* {
 box-sizing: border-box;
}

html {
 margin: 0;
}

body {
 background: hsl(0, 0%, 90%);
 color: hsl(0, 0%, 5%);
 display: flex;
 font-family: Sans-serif;
 margin: 0;
 min-height: 100vh;
}

main {
 background: hsl(0, 0%, 98%);
 box-sizing: border-box;
 box-shadow: 0px 0px 20px hsla(0, 0%, 0%, 0.2);
 display: flex;
 flex-direction: column;
 margin: auto;
 min-width: 50%;
 min-height: 50vh;
 padding: 2rem;
}

.search-select {
 position: relative;
 margin: 0 auto;
 max-width: 300px;
 width: 100%;
}

.search-select-search {
 background-color: $color-input-background;
 border: $input-border-thickness solid $color-input-border;
 border-radius: 1rem;
 box-sizing: border-box;
 cursor: pointer;
 display: block;
 padding: 0.5rem 0.75rem;
 position: relative;
 width: 100%;
 z-index: 1;
}

.search-select-search:focus {
 color: $color-input-border;
 cursor: text;
 outline: 0;
}

.search-select-icon {
 box-sizing: content-box;
 cursor: pointer;
 fill: $color-input-border;
 padding: 0.85rem 0.6rem;
 position: absolute;
 transform: rotate(0);
 transition: transform 0.2s ease-in-out;
 top: 0;
 right: 0;
 stroke: $color-input-border;
 stroke-linecap: round;
 stroke-linejoin: round;
 stroke-width: 1;
 z-index: 1;
}

.search-select-icon.open {
 transform: rotate(180deg);
}

.search-select-dropdown {
 background-color: $color-input-background;
 border: $input-border-thickness solid $color-input-border;
 border-radius: 1rem 1rem 0.25rem 0.25rem;
 box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
 -webkit-box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
 left: 0;
 padding: 2rem 0 0;
 position: absolute;
 right: 0;
 top: 0;
 z-index: 0;
}

.search-select-options {
 list-style: none;
 margin: 0;
 max-height: 14rem;
 padding: 0;
 position: relative;
 overflow-y: auto;
 -webkit-overflow-scrolling: touch;
}

.search-select-option {
 padding: 0.5rem 0.75rem;
 color: $color-input-border;
 cursor: pointer;
 -webkit-user-select: none;
 -moz-user-select: none;
 -ms-user-select: none;
 user-select: none;
}

.search-select-option.is-active,
.search-select-option.is-active:hover {
 background-color: $color-input-hover;
}

.search-select-option.is-active:active {
 background-color: $color-input-active;
 color: $color-input-background;
}

.search-select-empty {
 padding: 0.5rem 0.75rem;
 color: #b8c2cc;
}

              
            
!

JS

              
                Vue.component('search-select', {
	template: '#search-select',
 
 props: {
  value: {
   required: true
  },
  options: {
   required: true,
   type: Array
  },
  filterFunction: {
   type: Function,
   default: function(search, options) {
     return options.filter(option => {
      return option.toLowerCase().startsWith(search.toLowerCase());
     });
   }
  }
 },
	
	data() {
  return {
   isOpen: false,
   search: '',
   highlightedIndex: 0
  }
 },

 computed: {
  filteredOptions() {
   return this.filterFunction(this.search, this.options);
  }
 },
 
 mounted() {
  document.addEventListener('click', this.clickOutside);
 },
 
 beforeDestroy() {
  document.removeEventListener('click', this.clickOutside);
 },

 methods: {
  defaultFilter(search, options) {
   return options.filter(option => {
    return option.toLowerCase().startsWith(search.toLowerCase());
   });
  },
  
  clickOutside( e ) {
   if( e.target === this.$el || this.$el.contains(e.target)) {
    return;
   }
   
   this.close();
  },
  
  open() {
   this.isOpen = true;

   this.$nextTick(() => {
    this.$refs.search.focus();
    this.scrollToHighlighted();    
   })
  },

  close() {
   this.isOpen = false;
  },
  
  toggle() {
   this.isOpen ? this.close() : this.open();
  },

  select( selected ) {
   this.$emit('input', selected);
   this.search = '';
   this.highlightedIndex = 0;
   this.close();
  },
  
  selectClicked( e ) {
   this.select( e.target.innerText );
  },
  
  selectHighlighted( e ) {
   if( this.isOpen ) {
    this.select( this.filteredOptions[this.highlightedIndex] );
   } else {
    this.open();
   }
  },
  
  scrollToHighlighted() {
   this.$refs.options.children[this.highlightedIndex].scrollIntoView({ block: 'nearest' });
  },
  
  highlight(index) {
   this.highlightedIndex = index;
   
   if( this.highlightedIndex < 0 ) {
    this.highlightedIndex = this.filteredOptions.length - 1;
   }
   
   if( this.highlightedIndex > this.filteredOptions.length - 1) {
    this.highlightedIndex = 0;
   }

   this.scrollToHighlighted();
  },
  
  highlightPrev() {
   if( this.isOpen ) this.highlight(this.highlightedIndex -= 1);
  },

  highlightNext() {
   if( this.isOpen )  this.highlight(this.highlightedIndex += 1);
  },
  
  highlightOption( e ) {
   
   // Find the index of the highlighted option
   var node = e.target
   var index = 0;
    while ( (node = node.previousElementSibling) ) {
        index++;
    }
    // return index;
   this.highlightedIndex = index;
   
   // Remove the old active class if one exists
   const old = this.$el.querySelector('.is-active')
   if( old ) old.classList.remove('is-active');
   // Add it to the newly hovered item
   e.target.classList.add('is-active');
  }

 }

} );

const app = new Vue( {
 el: '#app',
 
 data() {
  return {
   selectedItem: null,
   bands: [
    'Anthrax',
    'Dark Angel',
    'Death Angel',
    'Destruction',
    'Exodus',
    'Flotsam and Jetsam',
    'Kreator',
    'Megadeth',
    'Metallica',
    'Overkill',
    'Sepultura',
    'Slayer',
    'Testament'
   ]
  }
 }

} );
              
            
!
999px

Console