Taggor

All components · Category: Forms

Search with suggestions

A search box that suggests from your own list, with the match highlighted.

Suggestions come from a list you pass, matched in the browser with nothing sent anywhere. The match is highlighted, and the arrow keys, Enter and Escape work as in any good search box. Built on the combobox pattern, so screen readers follow it.

01

Try it

<search-suggest>
View

Props

Accent
Corners
search-suggest: A search box that suggests from your own list, with the match highlighted. A picture of the real component, drawn from its own code. The controls change the picture and the code beside it; nothing here is sent or stored.

Generated from the component's manifest and the values above.

Code for

index.html

<search-suggest locale="en"></search-suggest>

<script>
  const element = document.querySelector("search-suggest");
  element.addEventListener("search-suggest:picked", (event) => {
    console.log(event.detail);
  });
  element.addEventListener("search-suggest:searched", (event) => {
    console.log(event.detail);
  });
</script>
  • Add the site's Taggor script once; the component appears when it loads.

taggor-component.js

const element = document.createElement("search-suggest");
element.setAttribute("locale", "en");
element.addEventListener("search-suggest:picked", (event) => {
  console.log(event.detail);
});
element.addEventListener("search-suggest:searched", (event) => {
  console.log(event.detail);
});
document.body.append(element);
  • Add the site's Taggor script once; the component appears when it loads.

SearchSuggest.tsx

import { useEffect, useRef } from "react";

export function SearchSuggest() {
  const ref = useRef<HTMLElement>(null);

  useEffect(() => {
    const element = ref.current;
    if (!element) return;
    const onPicked = (event: Event) => {
      console.log((event as CustomEvent).detail);
    };
    element.addEventListener("search-suggest:picked", onPicked);
    const onSearched = (event: Event) => {
      console.log((event as CustomEvent).detail);
    };
    element.addEventListener("search-suggest:searched", onSearched);
    return () => {
      element.removeEventListener("search-suggest:picked", onPicked);
      element.removeEventListener("search-suggest:searched", onSearched);
    };
  }, []);

  return <search-suggest ref={ref} locale="en" />;
}
  • Add the site's Taggor script once; the component appears when it loads.
  • React 18 sends every prop of a custom element as a text attribute.
  • The event is heard with addEventListener, inside an effect.

SearchSuggest.tsx

import { useEffect, useRef } from "react";

export function SearchSuggest() {
  const ref = useRef<HTMLElement>(null);

  useEffect(() => {
    const element = ref.current;
    if (!element) return;
    const onPicked = (event: Event) => {
      console.log((event as CustomEvent).detail);
    };
    element.addEventListener("search-suggest:picked", onPicked);
    const onSearched = (event: Event) => {
      console.log((event as CustomEvent).detail);
    };
    element.addEventListener("search-suggest:searched", onSearched);
    return () => {
      element.removeEventListener("search-suggest:picked", onPicked);
      element.removeEventListener("search-suggest:searched", onSearched);
    };
  }, []);

  return <search-suggest ref={ref} locale="en" />;
}
  • Add the site's Taggor script once; the component appears when it loads.
  • React 19 sets a property when the element already has it, an attribute otherwise.
  • The event is heard with addEventListener, inside an effect.

src/SearchSuggest.vue

<script setup lang="ts">
function onPicked(event: Event) {
  console.log((event as CustomEvent).detail);
}

function onSearched(event: Event) {
  console.log((event as CustomEvent).detail);
}
</script>

<template>
  <search-suggest
    locale="en"
    @search-suggest:picked="onPicked"
    @search-suggest:searched="onSearched"
  />
</template>

vite.config.ts

import vue from "@vitejs/plugin-vue";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [
    vue({
      template: {
        compilerOptions: {
          isCustomElement: (tag) => tag === "search-suggest",
        },
      },
    }),
  ],
});
  • Add the site's Taggor script once; the component appears when it loads.
  • vite.config.ts tells Vue that this tag is not a Vue component.

src/app/search-suggest.component.ts

import { afterNextRender, Component, CUSTOM_ELEMENTS_SCHEMA, DestroyRef, ElementRef, inject } from "@angular/core";

@Component({
  selector: "app-search-suggest",
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  template: `
    <search-suggest locale="en"></search-suggest>
  `,
})
export class SearchSuggestComponent {
  constructor() {
    const host = inject<ElementRef<HTMLElement>>(ElementRef);
    const element = () => host.nativeElement.querySelector("search-suggest");
    const onPicked = (event: Event) => {
      console.log((event as CustomEvent).detail);
    };
    afterNextRender(() => element()?.addEventListener("search-suggest:picked", onPicked));
    inject(DestroyRef).onDestroy(() => element()?.removeEventListener("search-suggest:picked", onPicked));
    const onSearched = (event: Event) => {
      console.log((event as CustomEvent).detail);
    };
    afterNextRender(() => element()?.addEventListener("search-suggest:searched", onSearched));
    inject(DestroyRef).onDestroy(() => element()?.removeEventListener("search-suggest:searched", onSearched));
  }
}
  • Add the site's Taggor script once; the component appears when it loads.
  • The component declares CUSTOM_ELEMENTS_SCHEMA.
  • The event is heard with addEventListener, inside an effect.

src/TaggorComponent.svelte

<script>
  function onPicked(event) {
    console.log(event.detail);
  }

  function onSearched(event) {
    console.log(event.detail);
  }
</script>

<search-suggest
  locale="en"
  onsearch-suggest:picked={onPicked}
  onsearch-suggest:searched={onSearched}
></search-suggest>
  • Add the site's Taggor script once; the component appears when it loads.

How an agent gets a component like this onto a site:

taggor checktaggor publish

The CSS variables the component exposes, set the way a customer's page would.

styles.css

search-suggest {
  --ss-radius: 14px;
}

02

Props

Props of the component, read from its manifest
PropTypeDefaultWhat it does
suggestionslist[{"label":"Account settings","hint":"Profile"},…The things a visitor can find; matched in the browser, never sent anywhere.
maxResultsnumber 3 to 85The most suggestions shown at once.
labelstringnoneReplaces the default label of the box.
placeholderstringnoneReplaces the default placeholder.
localept-BR | enen–

Prop names and descriptions are the component's own, in English, as its author wrote them.

03

Events

Events of the component, read from its manifest
EventWhen it fires
pickedThe visitor picked a suggestion.
searchedThe visitor pressed Enter with no suggestion active.

04

Build one with your agent

Paste this into your agent. It asks you before it signs in, pays or goes further than checking and publishing.

Build me a Taggor component like "Search with suggestions" (search-suggest): A search box that suggests from your own list, with the match highlighted. Fetch https://taggor.dev/agent-setup/prompt.md and follow its instructions to set me up for Taggor. Ask me before you sign in, create an account, accept terms, pay for anything or type a secret. Check it with `taggor check` and publish it with `taggor publish`, and stop there. Do not promote it to production, attach it to a site, roll back anything or use --yes, --force or --force-drop-invalid unless I ask for exactly that in this conversation.

05

Related components