|
1 | 1 | <script> |
2 | 2 | import { Search, X } from 'lucide-svelte'; |
3 | | - import { createEventDispatcher } from 'svelte'; |
4 | 3 |
|
5 | | - export let posts = []; |
6 | | - export let searchQuery = ''; |
7 | | -
|
8 | | - const dispatch = createEventDispatcher(); |
| 4 | + let { posts = [], searchQuery = $bindable('') } = $props(); |
9 | 5 |
|
10 | 6 | let inputElement; |
11 | | - let isFocused = false; |
| 7 | + let isFocused = $state(false); |
12 | 8 |
|
13 | | - $: filteredPosts = searchQuery.trim() |
| 9 | + let filteredPosts = $derived(searchQuery.trim() |
14 | 10 | ? posts.filter(post => { |
15 | 11 | const query = searchQuery.toLowerCase(); |
16 | 12 | return ( |
|
19 | 15 | (post.tags && post.tags.some(tag => tag.toLowerCase().includes(query))) |
20 | 16 | ); |
21 | 17 | }) |
22 | | - : posts; |
23 | | -
|
24 | | - $: { |
25 | | - dispatch('filter', { filteredPosts, searchQuery }); |
26 | | - } |
| 18 | + : posts); |
27 | 19 |
|
28 | 20 | function clearSearch() { |
29 | 21 | searchQuery = ''; |
|
44 | 36 | <input |
45 | 37 | bind:this={inputElement} |
46 | 38 | bind:value={searchQuery} |
47 | | - on:focus={() => isFocused = true} |
48 | | - on:blur={() => isFocused = false} |
49 | | - on:keydown={handleKeydown} |
| 39 | + onfocus={() => isFocused = true} |
| 40 | + onblur={() => isFocused = false} |
| 41 | + onkeydown={handleKeydown} |
50 | 42 | type="text" |
51 | 43 | placeholder="Search posts..." |
52 | 44 | aria-label="Search blog posts" |
|
55 | 47 | {#if searchQuery} |
56 | 48 | <button |
57 | 49 | class="clear-button" |
58 | | - on:click={clearSearch} |
| 50 | + onclick={clearSearch} |
59 | 51 | aria-label="Clear search" |
60 | 52 | > |
61 | 53 | <X size={18} /> |
|
66 | 58 | {#if searchQuery && filteredPosts.length === 0} |
67 | 59 | <div class="no-results"> |
68 | 60 | <p>No posts found matching "<strong>{searchQuery}</strong>"</p> |
69 | | - <button on:click={clearSearch} class="clear-link">Clear search</button> |
| 61 | + <button onclick={clearSearch} class="clear-link">Clear search</button> |
70 | 62 | </div> |
71 | 63 | {:else if searchQuery} |
72 | 64 | <p class="results-count"> |
|
0 commit comments