MapComplete/src/UI/Base/Searchbar.svelte

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
2.2 KiB
Svelte
Raw Normal View History

2024-09-02 03:47:54 +02:00
<script lang="ts">
import { UIEventSource } from "../../Logic/UIEventSource"
import Translations from "../i18n/Translations"
import { createEventDispatcher } from "svelte"
import { placeholder as set_placeholder } from "../../Utils/placeholder"
import { SearchIcon } from "@rgossiaux/svelte-heroicons/solid"
import { ariaLabel } from "../../Utils/ariaLabel"
import { Translation } from "../i18n/Translation"
import Backspace from "@babeard/svelte-heroicons/outline/Backspace"
2024-09-02 03:47:54 +02:00
export let value: UIEventSource<string>
let _value = value.data ?? ""
2024-10-19 14:44:55 +02:00
value.addCallbackD((v) => {
2024-09-02 03:47:54 +02:00
_value = v
})
$: value.set(_value)
const dispatch = createEventDispatcher<{ search }>()
export let placeholder: Translation = Translations.t.general.search.search
export let isFocused: UIEventSource<boolean> = undefined
let inputElement: HTMLInputElement
2024-09-24 18:08:01 +02:00
export let autofocus = false
2024-10-19 14:44:55 +02:00
isFocused?.addCallback((focussed) => {
if (focussed) {
requestAnimationFrame(() => {
if (document.activeElement !== inputElement) {
inputElement?.focus()
inputElement?.select()
}
})
}
})
2024-10-19 14:44:55 +02:00
if (autofocus) {
2024-09-24 18:08:01 +02:00
isFocused.set(true)
}
2024-09-02 03:47:54 +02:00
</script>
2024-10-19 14:44:55 +02:00
<form class="w-full" on:submit|preventDefault={() => dispatch("search")}>
2024-09-02 03:47:54 +02:00
<label
2024-10-19 14:44:55 +02:00
class="neutral-label normal-background box-shadow flex w-full items-center rounded-full border border-black"
2024-09-02 03:47:54 +02:00
>
2024-10-19 14:44:55 +02:00
<SearchIcon aria-hidden="true" class="ml-2 h-8 w-8" />
2024-09-02 03:47:54 +02:00
<input
bind:this={inputElement}
2024-10-19 14:44:55 +02:00
on:focus={() => {
isFocused?.setData(true)
}}
on:blur={() => {
isFocused?.setData(false)
}}
2024-09-02 03:47:54 +02:00
type="search"
style=" --tw-ring-color: rgb(0 0 0 / 0) !important;"
2024-10-19 14:44:55 +02:00
class="ml-1 w-full border-none px-0 outline-none"
2024-09-02 03:47:54 +02:00
on:keypress={(keypr) => {
2024-10-19 14:44:55 +02:00
return keypr.key === "Enter" ? dispatch("search") : undefined
}}
2024-09-02 03:47:54 +02:00
bind:value={_value}
use:set_placeholder={placeholder}
use:ariaLabel={placeholder}
2024-09-02 03:47:54 +02:00
/>
{#if $value.length > 0}
2024-10-19 14:44:55 +02:00
<Backspace
2024-11-28 12:00:23 +01:00
on:click={(e) => {
value.set("")
e.preventDefault()
}}
2024-10-19 14:44:55 +02:00
color="var(--button-background)"
class="mr-3 h-6 w-6 cursor-pointer"
/>
{:else}
2024-10-19 14:44:55 +02:00
<div class="mr-3 w-6" />
{/if}
2024-09-02 03:47:54 +02:00
</label>
</form>