Last active
February 20, 2019 23:13
-
-
Save burningTyger/07ff8b15b72ae609c723d529c29180d5 to your computer and use it in GitHub Desktop.
Revisions
-
burningTyger revised this gist
Feb 20, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ This is an example svelte autocomplete component. There is not much to do with it unless you want to query the farhang database. But feel free to copy and paste things into your projects. This repo is public domain. -
burningTyger revised this gist
Feb 20, 2019 . 1 changed file with 2 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,8 +27,8 @@ function fetch_options (term) { term.length > 0 ? fetch(`https://farhang.im/search/autocomplete.json?term=${term}`) .then(async res => {options = await res.json(); pos = 1}) .catch(e => options = []) : options = [] } const keydown = (e) => { @@ -72,9 +72,7 @@ padding: 0; margin: 0; border: 1px solid #dbdbdb; overflow: auto; background-color: white; box-shadow: 2px 2px 24px rgba(0, 0, 0, 0.1); position: absolute; -
burningTyger revised this gist
Feb 20, 2019 . 1 changed file with 6 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ <h1>{selected.link || 'No selection'}</h1> <div class="search"> <input bind:value={term} on:keydown={keydown} class="input" on:blur={() => options = []}> <ul class="list"> {#each options as t,i} <li @@ -27,8 +27,7 @@ function fetch_options (term) { term.length > 0 ? fetch(`https://farhang.im/search/autocomplete.json?term=${term}`) .then(async res => {options = await res.json(); pos = 1}) .catch(e => options = []) : options = [] } @@ -50,6 +49,10 @@ e.preventDefault(); options.length > 0 && (term = options[pos-1].value) break; case 'Escape': e.preventDefault(); options = [] break } } </script> -
burningTyger created this gist
Feb 20, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,93 @@ <h1>{selected.link || 'No selection'}</h1> <div class="search"> <input bind:value={term} on:keydown={keydown} class="input"> <ul class="list"> {#each options as t,i} <li class={`item ${i+1 === pos ? 'active':''}`} value={t.link} on:click={() => select(t)} >{t.value}</li> {/each} </ul> </div> <script> let term = 'a' let options = [] let pos let selected = {} $: fetch_options(term) function select (t) { selected = t term = '' pos = 1 } function fetch_options (term) { term.length > 0 ? fetch(`https://farhang.im/search/autocomplete.json?term=${term}`) .then(res => res.text()) .then(text => {options = JSON.parse(text); pos = 1}) .catch(e => options = []) : options = [] } const keydown = (e) => { switch (e.key) { case 'ArrowDown': e.preventDefault(); options.length > pos && (pos += 1) break; case 'ArrowUp': e.preventDefault(); pos > 1 && (pos -=1) break; case 'Enter': e.preventDefault(); select(options[pos-1]) break; case 'Tab': e.preventDefault(); options.length > 0 && (term = options[pos-1].value) break; } } </script> <style> * { box-sizing: border-box; } .input { height: 2rem; font-size: 1rem; padding: 0.25rem 0.5rem; } .search { position: relative; } .list { padding: 0; margin: 0; border: 1px solid #dbdbdb; /* height: 6rem; */ overflow: auto; /* width: 100%; */ background-color: white; box-shadow: 2px 2px 24px rgba(0, 0, 0, 0.1); position: absolute; z-index: 100; } .item { /* color: #7a7a7a; */ list-style: none; text-align: left; height: 2rem; padding: 0.25rem 0.5rem; cursor: pointer; } .item.active, .item:hover { background-color: #dbdbdb; } </style> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ Created with [svelte.technology/repl](https://svelte.technology/repl) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ { } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ { "svelte": true }