Search input transitions on items, styling items on search and hover, exporting search from component.

This commit is contained in:
Bradley Shellnut 2021-07-27 13:28:56 -07:00
parent 61e9a92311
commit 1ce29699f6
2 changed files with 56 additions and 13 deletions

View file

@ -1,18 +1,61 @@
<script>
import { fade } from 'svelte/transition';
export let items;
let search = '';
export let search = '';
let isFocused = false;
$: filteredSearch = items.filter((item) => {
return search === '' || item.toLowerCase().indexOf(search.toLowerCase()) !== -1;
});
</script>
<label>
Search Names: <br />
<input bind:value={search} type="text" placeholder="Search" />
</label>
<ul>
{#each filteredSearch as item}
<li>{item}</li>
{/each}
</ul>
<div class="wrapper">
<label>
Search Names: <br />
<input
on:focus={() => (isFocused = true)}
bind:value={search}
type="text"
placeholder="Search"
/>
</label>
{#if isFocused}
<ul transition:fade={{ duration: 200 }}>
{#each filteredSearch as item}
<li
transition:fade={{ duration: 200 }}
on:click={() => {
search = item;
isFocused = false;
}}
>
{item}
</li>
{/each}
</ul>
{/if}
</div>
<style>
.wrapper {
position: relative;
display: inline-block;
}
ul {
position: absolute;
width: 100%;
border: 1px solid black;
list-style: none;
padding: 0;
margin: 0;
}
li {
padding: 10px;
cursor: pointer;
transition: 0.3s background ease;
}
li:hover {
background: var(--listItemBackground, #eee);
}
</style>

View file

@ -3,13 +3,13 @@
// import BetterAccordion from '$lib/BetterAccordion.svelte';
// import Toggle from '$lib/Toggle.svelte';
let isToggled = false;
let search = '';
let items = ['scott', 'wes', 'landon', 'courtney', 'lucie', 'brooklyn', 'Samson'];
</script>
<h1>Welcome to Level Up UI</h1>
<h1>Welcome to Level Up UI {search}</h1>
<SearchFilter {items} />
<SearchFilter {items} bind:search />
<!-- <BetterAccordion isOpen={false} buttonText="Do I need a credit card??">
<div>