Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                script#result(type="text/template")
	.card
		a(class="result {{color}}", onclick="profile('{{name}}', true)")
			b {{name}}

script#profile(type="text/template")
	.card
		div(class="result")
			b {{name}}
			br
			span
				b Location
				span {{location}}
			br
			span
				b Established
				span {{location}}

h1 Autocomplete Search
input(onkeyup='filter(event)', autofocus="autofocus")
.cards

              
            
!

CSS

              
                $blue: #358bf1
$green: #86de74

body
	width: 80%
	margin: 0 auto
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif
	background: #fdfdfd
	overflow-y: scroll

h1
	margin-top: .5em
	text-align: center
	font-size: 1.5em

input
	width: calc(100% - 38px)
	// margin-top: 1.5em
	padding: .5em
	border-radius: 24px
	border: 2px solid #eee
	height: 28px
	padding-left: 1.5em
	font-size: 1em
	background: #fff !important
	box-shadow: 0 .25em 1em #ddd
	font-family: helvetica, arial, sans-serif
	outline: none
	&:hover
		border-color: #ddd
		box-shadow: 0 .25em 2em #ccc
	&:focus
		border-color: $blue
		box-shadow: 0 .25em 2em #bbb

.result
	display: block
	color: #111
	background: #fefefe
	border: solid #fff 2px
	border-radius: .3em
	padding: 1em
	margin-top: 2em
	box-shadow: 0 .25em .5em #ddd

a.result
	cursor: pointer
	&:hover
		box-shadow: 0 .25em 1em #ccc
		background: #fff

div.result
	box-shadow: 0 .25em 1em #ccc
	background: #fff

.white
	background: white

.green
	border-color: $green

.blue
	border-color: $blue

.tan
	border-color: tan

              
            
!

JS

              
                // Autocomplete Search v0
// Max Terry
// 2019-09-30

let data = MaineLighthouses

function result(q, data, color, clear=true) {
  let card = document.getElementById('result').innerHTML
  let cards = document.getElementsByClassName('cards')[0]
  clear && (cards.innerHTML = '')
  let temp = JSON.parse(JSON.stringify(data))
  for (let i in temp) {
    temp[i].color = color
    // if (q) {
    //  temp[i].name = '<b>' + q + '</b>' + temp[i].name.split(q)
    // }
    cards.innerHTML += Mustache.to_html(card, temp[i])
  }
}

// TODO factor out to profile <- card
function profile(name, clear=true) {
  let card = document.getElementById('profile').innerHTML
  let cards = document.getElementsByClassName('cards')[0]
  clear && (cards.innerHTML = '')
  for (let i in data) {
    // data[i].color = color
    if (data[i].name === name) {
      // Render profile card
      cards.innerHTML += Mustache.to_html(card, data[i])
      // Put name in input
      document.getElementsByTagName('input')[0].value = data[i].name
      // Select input text
      document.getElementsByTagName('input')[0].select()
    }
  }
}

function isSubstringMatch(q, candidate) {
  q = q.toLowerCase()
  candidate = candidate.toLowerCase()
  return candidate.indexOf(q) > -1
}

function isLevenshteinMatch(q, candidate) {
  return levenshtein(q, candidate) < 5
}

function isLocationMatch(q, candidate) {
  q = q.toLowerCase()
  candidate = candidate.toLowerCase()
  return candidate.indexOf(q) > -1
}

function filter(it) {
  
  let q = it.target.value
  if (!q) {
    result('', data, 'white')
    return
  }
  let substringResults = data.filter(it => {
    return isSubstringMatch(q, it.name)
  })
  let levenshteinResults = data.filter(it => {
    return !isSubstringMatch(q, it.name) && isLevenshteinMatch(q, it.name)
  })
  let locationResults = data.filter(it => {
    return !isSubstringMatch(q, it.name) && !isLevenshteinMatch(q, it.name) && isLocationMatch(q, it.location)
  })
  result(q, substringResults, 'blue')
  result(q, levenshteinResults, 'green', false)
  result(q, locationResults, 'tan', false)
}

result('', data)

              
            
!
999px

Console