From 4f3227a31ddde457192cc374dd5c0d9c5376292e Mon Sep 17 00:00:00 2001 From: Xylight Date: Tue, 1 Oct 2024 16:54:46 -0700 Subject: [PATCH] feat: improve search experience/autofocus --- src/lib/i18n/en.json | 5 +- src/lib/mono/forms/TextInput.svelte | 2 + src/routes/communities/+page.svelte | 76 ++++++++++++++++++++--------- src/routes/search/+error.svelte | 8 --- src/routes/search/+page.svelte | 15 ++++-- 5 files changed, 71 insertions(+), 35 deletions(-) delete mode 100644 src/routes/search/+error.svelte diff --git a/src/lib/i18n/en.json b/src/lib/i18n/en.json index 0ee3d5b7..417189b9 100644 --- a/src/lib/i18n/en.json +++ b/src/lib/i18n/en.json @@ -231,7 +231,10 @@ "accounts": "Accounts", "communities": { "title": "Communities", - "trending": "Trending" + "trending": "Trending", + "search": { + "placeholder": "Search for a new community" + } }, "search": { "title": "Search", diff --git a/src/lib/mono/forms/TextInput.svelte b/src/lib/mono/forms/TextInput.svelte index 9b97a64f..2b723b71 100644 --- a/src/lib/mono/forms/TextInput.svelte +++ b/src/lib/mono/forms/TextInput.svelte @@ -27,6 +27,7 @@ export let id: string = generateID() export let inlineAffixes: boolean = false export let shadow: Shadow = 'none' + export let element: HTMLInputElement | undefined = undefined const borderClass = ` border border-slate-200 border-b-slate-300 dark:border-zinc-900 dark:border-t-zinc-800 @@ -68,6 +69,7 @@ {placeholder} {disabled} bind:value + bind:this={element} on:input on:change on:focus diff --git a/src/routes/communities/+page.svelte b/src/routes/communities/+page.svelte index 950759ca..a8b0ff70 100644 --- a/src/routes/communities/+page.svelte +++ b/src/routes/communities/+page.svelte @@ -1,5 +1,5 @@ - + { + if (e.target == document.body) searchElement?.focus() + }} +/> {$t('routes.communities.title')} @@ -83,19 +94,22 @@ > @@ -105,7 +119,7 @@
    {#if data.communities.length == 0} {/if} - {#if (data.query ?? '' != '') && data.communities.length > 0} + {#if showTop} {$t('routes.search.top')}
    - {#each data.communities.slice(0, 3) as community} - - - + + + +
    {/each} {/if} - {#if data.communities.slice((data.query ?? '' != '') ? 3 : 0).length > 0} + {#if data.communities.slice(showTop ? 3 : 0).length > 0} {$t('routes.search.other')} {/if} {#if data.communities} - {@const sliced = data.communities.slice((data.query ?? '' != '') ? 3 : 0)} + {@const sliced = data.communities.slice(showTop ? 3 : 0)}
    - {#each sliced as community} + {#each sliced as community, index (community.community.id)}
    diff --git a/src/routes/search/+error.svelte b/src/routes/search/+error.svelte deleted file mode 100644 index ea837df0..00000000 --- a/src/routes/search/+error.svelte +++ /dev/null @@ -1,8 +0,0 @@ - - -
    -

    {$page.status}

    - The search timed out. This is an issue on the server's side. -
    diff --git a/src/routes/search/+page.svelte b/src/routes/search/+page.svelte index 43b95b8a..acabdcc2 100644 --- a/src/routes/search/+page.svelte +++ b/src/routes/search/+page.svelte @@ -1,5 +1,5 @@ + { + if (e.target == document.body) searchElement?.focus() + }} +/> + {$t('routes.search.title')} @@ -68,19 +75,21 @@ >