SearchBar
Preview for Vue Web and NativeScript on iOS and Android.
<MSearchBar
v-model="query"
label="Find a photo"
showCancel
@submit="search"
@cancel="leaveSearch"
/>Type to update the controlled query. Return or the mobile Search key emits submit(query). Clear requests an empty value and restores focus. Cancel blurs the editor and emits cancel, giving the application control of navigation and the stored query. Translate both actions with clearLabel and cancelLabel.
| Prop | Type | Default | Description |
|---|---|---|---|
modelValue / v-model | string | '' | Controlled query. |
label | string | required | Visible label and editor name. |
showCancel | boolean | false | Shows the Cancel action. |
searching | boolean | false | The app reports a search in progress: the search icon pulses and the Web bar sets aria-busy. |
clearLabel / cancelLabel | string | Clear search / Cancel | Localizable action names. |
Motion
The search icon takes a quick look around as the text changes and, while searching is set, pulses calmly until the app clears it. The Clear button pops in with the first character and shrinks away once the field is empty; Cancel arrives when showCancel turns on and leaves the same way. While they leave, neither acts. The editor itself never moves or resizes: it keeps focus, the caret and the typed text in place, so the field does not grow on focus. Reduced motion settles the pulse instead of repeating it.
<script setup lang="ts">
import { useMavisTheme } from '@getcortexlabs/mavis-core/vue'
import { mountains, coast, lake } from '../atlas/photos'
import { computed, onBeforeUnmount, ref, watch } from 'vue'
import { useCatalogLocale } from '../locale'
const { theme } = useMavisTheme()
const props = defineProps<{ lang?: 'en' | 'pt' }>()
const { locale } = useCatalogLocale(() => props.lang)
const t = (en: string, pt: string) => ((props.lang ?? locale.value) === 'pt' ? pt : en)
const query = ref('')
const submitted = ref('')
// Stands in for a request: the archive answers a moment after the last keystroke.
const searching = ref(false)
const answered = ref('')
let timer: ReturnType<typeof setTimeout> | undefined
watch(query, (value) => {
clearTimeout(timer)
searching.value = Boolean(value)
if (!value) answered.value = ''
else
timer = setTimeout(() => {
searching.value = false
answered.value = value
}, 700)
})
onBeforeUnmount(() => clearTimeout(timer))
function cancel() {
query.value = ''
submitted.value = ''
}
const archive = computed(() => [
{
id: 'shore',
photo: coast,
title: t('Atlantic tides', 'Marés do Atlântico'),
place: t('Atlantic coast', 'Costa atlântica'),
},
{
id: 'ridge',
photo: mountains,
title: t('First light on the ridge', 'Primeira luz na serra'),
place: t('Alpine peaks', 'Picos alpinos'),
},
{
id: 'lake',
photo: lake,
title: t('The still lake', 'O lago sereno'),
place: t('Alpine lakes', 'Lagos alpinos'),
},
])
const normalize = (v: string) =>
v
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.toLowerCase()
.trim()
const matches = computed(() =>
archive.value.filter((row) =>
normalize(row.title + ' ' + row.place).includes(normalize(answered.value)),
),
)
</script>
<template>
<MColumn id="search-bar-preview" :backgroundColor="theme.colors.background" gap="lg">
<MText variant="title" :text="t('Photo archive', 'Arquivo fotográfico')" />
<MSearchBar
v-model="query"
:label="t('Find a photo', 'Buscar uma foto')"
:placeholder="t('Title or location', 'Título ou lugar')"
:showCancel="Boolean(query)"
:searching="searching"
:clearLabel="t('Clear search', 'Limpar busca')"
:cancelLabel="t('Cancel', 'Cancelar')"
@submit="submitted = $event"
@clear="submitted = ''"
@cancel="cancel"
/>
<MText
v-if="submitted && submitted === query"
variant="caption"
tone="muted"
:text="t('Results for ', 'Resultados para ') + '“' + submitted + '”'"
/>
<MColumn gap="md">
<MRow v-for="entry in matches" :key="entry.id" align="center" gap="md">
<MImage :src="entry.photo" :height="76" :width="100" fit="cover" radius="md" alt="" />
<MColumn :width="0" :grow="1" gap="xs"
><MText variant="heading" :text="entry.title" /><MText
variant="caption"
tone="muted"
:text="entry.place"
/></MColumn>
</MRow>
<MText
v-if="!matches.length"
:text="t('No photographs match this search.', 'Nenhuma foto corresponde à busca.')"
/>
</MColumn>
</MColumn>
</template>On Web and NativeScript, searching keeps the icon pulsing in one playback. Clearing it settles the icon while the input retains the caret and its text.
Desktop
On the desktop Return submits the search and Escape empties a field that has text, as the search fields of the system do. While searching is true the system progress ring takes the place of the search icon; see Desktop components.