Replace Preview

Previews every find-and-replace match in context before applying, so "cat" → "dog" doesn't turn "category" into "dogegory".

StopsReplace-all mangles other words

Installation

pnpm dlx shadcn@latest add https://bollard-ui.vercel.app/r/replace-preview.json

The files land in your project. They're yours to edit.

Usage

replace-preview-usage.tsx
import { ReplacePreview } from "@/components/ui/replace-preview"

<ReplacePreview value={text} onValueChange={setText} defaultFind="cat" defaultReplace="dog" />

What it catches

  • Matches that sit inside longer words, with a whole-word fix
  • Total match count before applying
  • Individual matches you want to skip
  • Case of each match, preserved in the replacement (CAT → DOG)

Props

ReplacePreview props
PropTypeDefault
value

Controlled text being edited.

string—
defaultValue

Initial text when uncontrolled.

string""
onValueChange

Called with the new text after Replace or Undo.

(value: string) => void—
defaultFind

Initial search term.

string""
defaultReplace

Initial replacement.

string""
defaultWholeWord

Start with whole-word matching on.

booleanfalse
findLabel

Label of the find input.

string"Find"
replaceLabel

Label of the replace input.

string"Replace with"
partialMessage

Templates: `{count}` and `{words}` are filled in.

string"{count} inside other words ({words}), skipped."
wholeWordFixLabel

Label of the one-tap whole-word fix.

string"Whole words only"
noMatchesText

Shown when nothing matches.

string"No matches."
applyLabel

`{count}` and `{total}` are filled in.

string"Replace {count} of {total}"
doneText

`{count}` is filled in.

string"Replaced {count}."
undoLabel

Label of the Undo button after replacing.

string"Undo"

* required. Also accepts the native props of <div> (className, aria-*, data-*…), passed through to it.

Accessibility

  • Find and replace inputs have labels
  • Match-case, whole-word and preserve-case are aria-pressed toggles in a labelled group
  • Match list has an aria-label with the count; each include/skip control has its own label
  • Warnings and the result render in polite live regions

Only what the source does. Test with your own screen reader and content before shipping.

Limitations

  • Plain-text matching, not regex
  • Word boundaries follow simple letter rules and may differ for some scripts
  • Undo covers the last replace only

What it stops

Replace-all mangles other words

Also known as

find and replace · replace all · search replace · bulk rename · editor · whole word · preview changes · text tool

Field type

Dependencies

Pairs well with