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

              
                doctype html

// Point of entry for Vue
#app
  select-ex(:items='items' filter='name' :query.sync='query')
    template(slot-scope='{ image, name, email, phone }')
      lazy-image(:src='image')
      .item-body
        h5: strong: highlighted-text(:text='name' :query='query')
        small {{phone}}
        small {{email}}
  button(@click='generateFakeData(1000)' v-cloak) More Data!

// Component Templates
template#selectEx
  .select-ex(:class='[showList ? "focused" : ""]')
    .background(:style='backgroundStyle')
    input(
      v-if='!model'
      ref='input'
      placeholder='Type to search'
      :style='inputStyle'
      @focus='focus'
      @blur='blur'
      v-model='query'
    )
    .item.selected(v-else)
      slot(v-bind='model')
      div(@click='edit'): i.fal.fa-pencil.fa-2x
    .list(
      v-if='showList'
      ref='list'
      :class='[busy ? "busy" : ""]'
      @mousedown.prevent
    )
      slot(name='info' v-bind='filteredItems')
        .info(v-if='busy') Searching...
        .info(v-else-if='query.length < minLength') Enter {{minLength - query.length}} more characters to search
        .info(v-else-if='filteredItems.length') Showing {{shownItems.length}} of {{filteredItems.length}} items
        .info(v-else) No results
      template(v-if='query.length >= minLength')
        template(v-for='(item, index) in shownItems')
          .item(:key='index' @click='select(item, $event)')
            slot(v-bind='item')
        .show-more(v-if='filteredItems.length > shownItems.length' @click='showMore') Show More

template#highlightedText
  .highlighted-text
    template(v-if='this.matches')
      span(v-html='escape(this.matches[1])')
      span.highlight(v-if='this.matches[2]' v-html='escape(this.matches[2])')
      span(v-html='escape(this.matches[3])')
    template(v-else) {{this.text}}

template#lazyImage
  .lazy-image
    img(
      :src='loaded ? src : ""'
      :alt='alt'
      :class='[loaded ? "" : "loading"]'
    )

// Profile link styling for self-shilling
a.shill(href='https://codepen.io/milesmanners' target='_blank' title='Made by Miles Manners')
              
            
!

CSS

              
                @include theme($bg-dark-purple, $deep-purple-a100, $light-blue-300, $grey-200);
//@include theme($bg-dark-red, $red, $orange, $grey-200);

$border-radius: 8px;

#app {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  min-width: 350px;
  
  > * {
    margin-left: auto;
    margin-right: auto;
  }
}

button {
  $color: color-level($primary, 3);
  
  background: $color;
  color: $text;
  padding: 4px 10px;
  border: 0.05em solid darken($color, 20%);
  border-bottom-width: 0.2em;
  border-radius: $border-radius;
  outline: none;
  font-size: 1.5rem;
  cursor: pointer;
  transition: all 100ms;
  
  &:active {
    border-top-width: 0.2em;
    border-bottom-width: 0.05em;
    transition: all 50ms;
  }
}

[v-cloak] {
  display: none;
}

// Component Styling
input {
  background: $bg;
  color: $text;
  border: 2px solid rgba($white, 0.4);
  border-radius: $border-radius;
  outline: none;
  font-family: inherit;
  font-size: 1rem;
  padding: 0.5rem;
  transition:
    border-color $anim-duration,
    box-shadow $anim-duration;
  
  &:focus {
    border-color: color-level($primary, -3);
    box-shadow: 0 0 0 0.25rem rgba($primary, .25),
  }
}

.select-ex {
  position: relative;
  border-radius: $border-radius;
  
  margin-top: 2rem;
  margin-bottom: 2rem;
  
  &.focused {
    .background {
      box-shadow: 0 0 0 0.25rem rgba($primary, .25),
    }
    
    .list {
      border-color: color-level($primary, -3);
    }
  }
  
  .background {
    width: 100%;
    position: absolute;
    pointer-events: none;
    border-radius: inherit;
  }
  
  input {
    width: 100%;
    box-shadow: none;
  }
  
  .list {
    position: absolute;
    display: flex;
    flex-flow: row wrap;
    overflow-x: hidden;
    overflow-y: auto;
    width: 100%;
    background: $bg;
    border: 2px solid rgba($white, 0.4);
    border-top-width: 0;
    border-radius: 0 0 $border-radius $border-radius;
    z-index: 10000;
    
    &.busy .item {
      transition: all 50ms;
      -webkit-filter: blur(3px);
      filter: blur(3px);
      
      &::after {
        transition: all 50ms;
        background: rgba($primary, 0.3);
        pointer-events: all;
      }
    }
  }
  
  .info {
    color: $text-hint;
    width: 100%;
    padding: 0.35rem 0.5rem;
    font-size: 0.9rem;
    text-align: center;
    cursor: default;
  }
  
  .show-more {
    color: $primary;
    width: 100%;
    margin-top: 0.5rem;
    padding: 0.15rem 0 0.5rem 0;
    font-size: 1.1rem;
    text-align: center;
    cursor: pointer;
  }
  
  .item {
    position: relative;
    display: flex;
    width: 100%;
    transition: background $anim-duration;
    
    &:not(.selected):hover {
      background: rgba($primary, 0.6);
      cursor: pointer;
      transition: none;
    }
    
    &.selected {
      border: 2px solid rgba($white, 0.4);
      border-radius: $border-radius;
      
      svg {
        position: absolute;
        margin: auto;
        right: 10px;
        top: 0;
        bottom: 0;
        color: transparent;
        transition: color $anim-duration;
      }
      
      &:hover svg {
        color: $text;
        cursor: pointer;
        
        &:hover {
          color: $primary;
        }
      }
    }

    &::after {
      content: "";
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      transition: all $anim-duration;
      pointer-events: none;
    }
  }
  
  .item-body {
    display: flex;
    flex-direction: column;
  }
}

.highlighted-text {
  display: flex;
}

.highlight {
  background: rgba($primary, 0.6);
}

.lazy-image {
  pointer-events: none;
  background: rgba($primary, 0.2);
  
  img {
    width: 100%;
    height: 100%;
    border-radius: inherit;
    opacity: 1;
    transition: all $anim-duration;
    
    &.loading {
      opacity: 0;
    }
  }
}

// Implementation Styling
.lazy-image {
  width: 40px;
  height: 40px;
  border-radius: $border-radius;
  margin: auto 0.5rem auto 0;
}

.select-ex {
  width: 30%;
  min-width: 300px;
  
  .list {
    max-height: 300px;
  }
  
  .item {
    padding: 0.35rem 0.5rem;
  }

  h5 {
    margin-bottom: 3px;
  }

  strong {
    font-weight: 600;
  }

  small {
    color: $text-hint;
    font-size: 0.75em;
  }
}

/* Multiple items on one line
.select-ex {
  min-width: 600px;

  .item {
    width: 50%;
  }
}
//*/

              
            
!

JS

              
                /* jshint esversion: 6, asi: true, boss: true */

// Component Definitions
Vue.component('select-ex', {
  template: '#selectEx',
  props: {
    items: Array,
    value: Object,
    filter: {
      type: [String, Function],
      required: true
    },
    maxShown: {
      type: Number,
      default: 20
    },
    minLength: {
      type: Number,
      default: 0
    },
    hideEmpty: Boolean
  },
  data () {
    return {
      showList: false,
      busy: false,
      height: 0,
      model: null,
      query: '',
      filteredItems: this.items,
      shownItems: this.items,
      amountShown: 0
    }
  },
  computed: {
    backgroundStyle () {
      // Styling for the background (the box-shadow element)
      return {
        height: `${this.height}px`
      }
    },
    inputStyle () {
      // Styling for the input
      return {
        'border-bottom-left-radius': this.showList ? 0 : '',
        'border-bottom-right-radius': this.showList ? 0 : '',
      }
    }
  },
  methods: {
    filterItems () {
      this.busy = true
      // Filtering proccess to help speed up the selector
      if (this.query.length) {
        console.time('filter')
        // Actually do a search
        if (typeof this.filter === 'string') {
          // Filter is a string, so search the list using it as a property
          let queryLower = this.query.toLowerCase()
          this.filteredItems = this.items.filter(item => item[this.filter].toLowerCase().indexOf(queryLower) > -1)
        } else {
          // Filter is a function, so pass off the filtering to it
          this.filteredItems = this.filter(this.items, this.query)
        }
        console.timeEnd('filter')
      } else {
        this.filteredItems = this.items
      }
      
      // Cut down the total items shown to help performance
      this.showAmount(this.maxShown)
      
      this.busy = false

      // Wait a tick for changes to propagate and then call resize to update the background
      this.$nextTick(this.resize)
    },
    focus () {
      this.showAmount(this.maxShown)
      
      // When the input is focused, show the list
      if (!this.hideEmpty) {
        this.showList = true
      }
    },
    blur () {
      // When the input is unfocused, hide the list
      this.showList = false
    },
    resize () {
      // Find the height of the whole component
      this.height = (this.$refs.input && this.$refs.input.offsetHeight) + (this.$refs.list && this.$refs.list.offsetHeight)
    },
    select (item, event) {
      // Remove focus from the input
      document.activeElement.blur()
      // Clear the search query
      this.query = ''
      
      // Update the model
      this.model = item
    },
    edit () {
      // Clear the model
      this.model = null
      
      // Wait a tick for changes to propagate
      this.$nextTick(() => {
        // Focus the input to make the UX a bit easier
        this.$refs.input.focus()
      })
    },
    showMore () {
      this.showAmount(Math.min(this.amountShown + this.maxShown, this.items.length))
    },
    showAmount (amount) {
      this.amountShown = amount
      this.shownItems = this.filteredItems.slice(0, this.amountShown)
    }
  },
  watch: {
    value: {
      handler (val) {
        this.model = val
      },
      immediate: true
    },
    model (val) {
      this.$emit('input', val)
    },
    items: {
      handler (val) {
        this.filterItems()
      }
    },
    query (val) {
      this.filterItems()
      this.$emit('update:query', val)
      
      if (this.hideEmpty) {
        this.showList = val.length > 0
      }
    },
    showList () {
      this.$nextTick(this.resize)
    }
  },
  mounted () {
    window.addEventListener('resize', this.resize, { passive: true })
    this.resize()
  },
  destroyed () {
    window.removeEventListener('resize', this.resize, { passive: true })
  }
})

// Highlights text to help identify why an item is showing up in a search
Vue.component('highlighted-text', {
  template: '#highlightedText',
  props: {
    text: {
      type: String,
      default: ''
    },
    query: {
      type: String,
      default: ''
    }
  },
  computed: {
    matches () {
      return this.text.match(new RegExp(`^(.*?)(${this.query})(.*?)$`, 'i'))
    }
  },
  methods: {
    escape (val) {
      return val.replace(/ /g, '&nbsp;')
    }
  }
})

// Helps gracefully load images by fading them in after loading instead of letting them pop in
Vue.component('lazy-image', {
  template: '#lazyImage',
  props: {
    src: String,
    alt: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      loaded: false
    }
  },
  created () {
    // Load the image
    let image = new Image()
    
    let imageLoaded = () => {
      this.loaded = true
      image.removeEventListener('load', imageLoaded, { passive: true })
    }
    
    image.addEventListener('load', imageLoaded, { passive: true })
    image.src = this.src
    
    // Check if the image is already cached
    if (image.complete) {
      this.loaded = true
      image.removeEventListener('load', imageLoaded, { passive: true })
    }
  }
})

// Point of entry
new Vue({
  el: '#app',
  data () {
    return {
      items: [],
      query: ''
    }
  },
  methods: {
    generateFakeData (amount = 10) {
      // Create some fake data using faker.js https://github.com/marak/Faker.js/
      this.items.splice(this.items.length, 0, ...Array.from({ length: amount }, x => {
        let firstName = faker.name.firstName()
        let lastName = faker.name.lastName()
        return {
          id: faker.random.uuid(),
          name: `${firstName} ${lastName}`,
          email: `${firstName}.${lastName}@${faker.internet.domainName()}`,
          phone: faker.phone.phoneNumber(),
          image: faker.image.avatar()
        }
      }))
      this.items.sort((a, b) => a.name > b.name)
    }
  },
  mounted () {
    if (!sessionStorage.getItem('data')) {
      console.time('data generation')
      this.generateFakeData(1000)
      console.timeEnd('data generation')
      sessionStorage.setItem('data', JSON.stringify(this.items))
    } else {
      this.items = JSON.parse(sessionStorage.getItem('data'))
    }
  }
})
              
            
!
999px

Console