svelte-codemirror-search-field
v0.11.0
Published
Svelte search field powered by CodeMirror with tag autocomplete and conceal/pill rendering
Readme
svelte-codemirror-search-field
A Svelte search field component that uses CodeMirror and the conceal mechanism to highlight tags and boolean operators as pills, with autocompletion on #.
Installation
pnpm add svelte-codemirror-search-fieldUsage
<script>
import SearchField from 'svelte-codemirror-search-field';
</script>
<SearchField doc="#linux #git" tags={['linux', 'git', 'nginx']} />Props
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| doc | string | '' | Document content (controlled; external changes replace the doc in place) |
| ph | string | 'Search...' | Placeholder text when empty |
| tags | string[] \| (() => Promise<string[]>) | null | Tag list for autocomplete (null = disabled) |
| matchTags | (query: string, tag: string) => number \| null | null | Custom tag filtering/ranking for autocomplete; score >= 0 = match, higher score ranks first; null keeps the default startsWith behavior |
| threshold | number | 0 | Distance in chars before conceal activates |
| autocompleteMinChars | number | 0 | Min chars after # before autocomplete triggers |
| autocompleteDebounceMs | number | 200 | Debounce delay for autocomplete popup |
| showImplicit | boolean | false | Show implicit AND/OR between consecutive tags |
| implicitOp | 'and' \| 'or' | 'and' | Default implicit operator |
| implicitAutoCloseParents | boolean | false | Show a virtual ) for unclosed parentheses and treat them as closed for SQL generation |
| onchange | (value: string) => void | () => {} | Called on every document change |
Tags with spaces
Tags are written #name. A tag may contain spaces using the quoted form
#"foo bar" — the whole #"foo bar" is rendered as a single pill, and the
underlying tag name is foo bar. Autocomplete quotes a tag automatically when
you pick one that contains a space. Allowed characters inside the quotes:
letters, digits, _ - . ' and spaces.
Custom tag matching with matchTags
By default tags are filtered with a case-insensitive startsWith and listed in
their original order. Pass a matchTags hook to filter and rank tags yourself:
it receives the query text after # and a tag, and returns a score (>= 0) when
the tag matches — higher scores rank first, ties keep the original list order —
or null when it does not match.
The minimal fuzzy matcher below matches any subsequence of the tag:
// Case-insensitive subsequence match: score = number of matched characters.
function subsequenceMatch(query, tag) {
const lowerQuery = query.toLowerCase();
const lowerTag = tag.toLowerCase();
let index = 0;
for (const char of lowerQuery) {
index = lowerTag.indexOf(char, index);
if (index === -1) return null;
index += 1;
}
return lowerQuery.length;
}<script>
import SearchField from 'svelte-codemirror-search-field';
const tags = ['postgresql', 'sqlite', 'mysql', 'nginx'];
</script>
<SearchField doc="#sql" tags={tags} matchTags={subsequenceMatch} />Typing #sql now suggests postgresql and mysql in addition to sqlite.
Example with async tag source
<script>
import SearchField from 'svelte-codemirror-search-field';
const fetchTags = async () => {
const res = await fetch('/api/tags');
return res.json();
};
</script>
<SearchField
doc="#linux or #git"
tags={fetchTags}
threshold={2}
showImplicit
implicitOp="or"
/>Controlled doc
doc is a controlled prop: external changes (e.g. navigation carrying a new
query string) replace the document in place, without recreating the editor. The
replacement is skipped while the field has focus, so debounced typing is never
clobbered by an in-flight navigation — the editor is the source of truth until
the user leaves the field.
Parser sub-export
The framework-agnostic search DSL parser is exposed as a sub-export for server-side reuse (e.g. to build a DNF for SQL predicates):
import { parse } from 'svelte-codemirror-search-field/parser';
const { ast, errors, elements } = parse('#linux and #git');It depends only on chevrotain and has no Svelte or CodeMirror dependency, so
it runs in plain Node.
License
MIT
