Dev Helpers
← Back

Issue text highlight

Place this file at app/issues/page.tsx in an App Router project.

Open this example in the interactive library

Source code

// app/issues/page.tsx

export default async function IssueTextHighlight({
  searchParams,
}: {
  searchParams: Promise<Record<string, string | string[] | undefined>>;
}) {
  const issues = [
    { id: 12, title: "Header spacing", owner: "Kim", severity: 1, open: true },
    { id: 14, title: "Invoice rounding", owner: "Sam", severity: 3, open: true },
    { id: 21, title: "Alt text", owner: "Kim", severity: 2, open: false },
  ];
  const { q } = await searchParams;
  const needle = typeof q === "string" ? q.toLowerCase() : "";
  return (
    <ul>
      {issues.map((i) => {
        const at = i.title.toLowerCase().indexOf(needle);
        return (
          <li key={i.id}>
            {at >= 0 && needle ? (
              <>
                {i.title.slice(0, at)}
                <mark>{i.title.slice(at, at + needle.length)}</mark>
                {i.title.slice(at + needle.length)}
              </>
            ) : (
              i.title
            )}
          </li>
        );
      })}
    </ul>
  );
}