Skip to content

Commit aee5a4f

Browse files
committed
fix: Search component
1 parent e197502 commit aee5a4f

File tree

9 files changed

+46
-41
lines changed

9 files changed

+46
-41
lines changed

.changeset/silent-pants-pump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte-5-ui-lib': patch
3+
---
4+
5+
fix: Search component

src/lib/forms/search/Search.svelte

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
11
<script lang="ts">
2-
import { Input } from '$lib';
32
import { type SearchProps as Props, search } from '.';
43
5-
let { children, right, size = 'lg', placeholder = 'Search', value = $bindable(), show = true, use = () => {}, class: className, ...restProps }: Props<unknown> = $props();
4+
let { children, right, inputClass, size = 'md', placeholder = 'Search', value = $bindable(), show = true, use = () => {}, class: className, ...restProps }: Props<unknown> = $props();
65
7-
const { base, content, icon } = $derived(search());
6+
const { base, content, icon, input: inputCls, leftDiv } = $derived(search({ size }));
87
</script>
98

10-
{#if show}
11-
<div class={base()} use:use>
12-
<Input bind:value type="search" {placeholder} {size} {...restProps} class={className}>
13-
{#snippet left()}
14-
<svg class={icon()} fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
15-
<path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd" />
16-
</svg>
17-
{/snippet}
18-
{#if right}
19-
{@render right()}
20-
{/if}
21-
</Input>
22-
{#if children}
23-
<div class={content()}>
24-
{@render children()}
25-
</div>
26-
{/if}
9+
<div class={base()}>
10+
<div class={leftDiv()}>
11+
<svg class={icon()} aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 20">
12+
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m19 19-4-4m0-7A7 7 0 1 1 1 8a7 7 0 0 1 14 0Z"/>
13+
</svg>
2714
</div>
28-
{:else if children}
29-
{@render children()}
30-
{/if}
15+
<input type="search" class={inputCls({ class: inputClass})} {placeholder} required {...restProps}/>
16+
{#if children}
17+
<div class={content()}>
18+
{@render children()}
19+
</div>
20+
{/if}
21+
</div>

src/lib/forms/search/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import Search from './Search.svelte';
22
import type { Action } from 'svelte/action';
33
import type { Snippet } from 'svelte';
44
import { search } from './theme';
5+
import type { HTMLInputAttributes } from 'svelte/elements';
56

6-
interface SearchProps<T> {
7+
interface SearchProps<T> extends Omit<HTMLInputAttributes, 'size'> {
78
children?: Snippet;
89
right?: Snippet;
10+
inputClass?: string;
911
size?: 'sm' | 'md' | 'lg';
1012
placeholder?: string;
1113
value?: T;

src/lib/forms/search/theme.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,31 @@ import { tv } from 'tailwind-variants';
33
export const search = tv({
44
slots: {
55
base: 'relative w-full',
6-
icon: '',
7-
content: 'absolute inset-y-0 end-0 flex items-center text-gray-500 dark:text-gray-400'
6+
leftDiv: 'absolute inset-y-0 start-0 flex items-center ps-3 pointer-events-none',
7+
icon: 'text-gray-500 dark:text-gray-400',
8+
content: 'absolute inset-y-0 end-0 flex items-center text-gray-500 dark:text-gray-400',
9+
input: 'block w-full text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500'
810
},
911
variants: {
1012
size: {
1113
sm: {
12-
icon: 'w-3.5 h-3.5'
14+
input: 'text-xs p-2 ps-9 pe-9 ',
15+
icon: 'w-3 h-3',
16+
// leftDiv: 'ps-2.5',
1317
},
1418
md: {
15-
icon: 'w-5 h-5'
19+
input: 'text-sm p-2.5 ps-10 pe-10',
20+
icon: 'w-4 h-4',
21+
// leftDiv: 'ps-10',
1622
},
1723
lg: {
18-
icon: 'w-6 h-6'
24+
input: 'sm:text-base p-3 ps-11 pe-11',
25+
icon: 'w-6 h-6',
26+
// leftDiv: 'ps-11',
1927
}
2028
}
2129
},
2230
defaultVariants: {
23-
size: 'lg'
31+
size: 'md'
2432
}
2533
});

src/routes/forms/search-input/+page.svelte

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
});
2121
2222
const exampleArr = [
23-
{ name: 'Default search', component: ExampleComponents.DefaultSearch },
24-
{ name: 'Simple search', component: ExampleComponents.SimpleSearch },
23+
{ name: 'Size large', component: ExampleComponents.SizeLarge },{ name: 'Simple search', component: ExampleComponents.SimpleSearch },
2524
{ name: 'Search with dropdown', component: ExampleComponents.SearchWithDropdown }
2625
];
2726
let selectedExample: string | number = $state(exampleArr[0].name);
@@ -60,9 +59,7 @@
6059
<Radio labelClass="w-44 my-1" onclick={() => (exampleExpand = false)} name="block_style" bind:group={selectedExample} value={style.name}>{style.name}</Radio>
6160
{/each}
6261
</div>
63-
<div class="h-24">
64-
<SelectedComponent />
65-
</div>
62+
<SelectedComponent />
6663
{#snippet codeblock()}
6764
<DynamicCodeBlockHighlight replaceLib {handleExpandClick} expand={exampleExpand} {showExpandButton} code={exampleModules[`./examples/${svelteCode}`] as string} />
6865
{/snippet}

src/routes/forms/search-input/examples/SearchWithDropdown.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
});
2323
</script>
2424

25+
<div class="h-48">
2526
<form class="flex">
2627
<Button class="whitespace-nowrap rounded-e-none border border-e-0 border-primary-700" onclick={dropdown.toggle}>
2728
{selectCategory}
@@ -38,8 +39,9 @@
3839
</DropdownUl>
3940
</Dropdown>
4041
</div>
41-
<Search size="md" class="rounded-none py-2.5 pl-8" placeholder="Searching {selectCategory}" />
42+
<Search inputClass="rounded-none py-3 pl-8" placeholder="Searching {selectCategory}" />
4243
<Button class="rounded-s-none !p-2.5">
4344
<SearchOutline class="h-6 w-6" />
4445
</Button>
4546
</form>
47+
</div>

src/routes/forms/search-input/examples/SimpleSearch.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
</script>
55

66
<form class="flex gap-2">
7-
<Search size="md" class="pl-10" />
8-
<Button class="!p-2.5">
7+
<Search />
8+
<Button class="p-2.5">
99
<SearchOutline class="h-5 w-5" />
1010
</Button>
1111
</form>

src/routes/forms/search-input/examples/DefaultSearch.svelte renamed to src/routes/forms/search-input/examples/SizeLarge.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
import { Search, Button } from '$lib';
33
</script>
44

5-
<Search class="pl-10">
6-
<Button class="mr-1">Search</Button>
5+
<Search size="lg">
6+
<Button class="mr-2">Search</Button>
77
</Search>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { default as DefaultSearch } from './DefaultSearch.svelte';
1+
export { default as SizeLarge } from './SizeLarge.svelte';
22
export { default as SearchWithDropdown } from './SearchWithDropdown.svelte';
33
export { default as SimpleSearch } from './SimpleSearch.svelte';

0 commit comments

Comments
 (0)