Highlight ranges
Mark text ranges in the open document with useHighlights, and keep them current as the document changes.
Call useHighlights(name, source, options) to mark text as a named set while a component is mounted. Highlights are local view state. They do not change the document and are not saved.
Highlight matching text
The following component marks each whole-word match of a list of terms. Style the glossary-term class with your own CSS, or omit className for the default fill.
import { useCallback } from 'react';
import { useHighlights, type Editor } from '@docx-editor.dev/react';
export function GlossaryMarks({ terms }: { terms: readonly string[] }) {
// A stable function source, so the set refreshes only when `terms` changes.
const findTerms = useCallback(
(editor: Editor) => editor.findMatches(terms, { wholeWord: true }).flat(),
[terms]
);
const { applied } = useHighlights('glossary', findTerms, { className: 'glossary-term' });
return <span>{applied} marked</span>;
}<script setup lang="ts">
import { useHighlights, type Editor } from '@docx-editor.dev/vue';
const props = defineProps<{ terms: readonly string[] }>();
// A source that reads props or refs runs again when they change.
const result = useHighlights(
'glossary',
(editor: Editor) => editor.findMatches(props.terms, { wholeWord: true }).flat(),
{ className: 'glossary-term' }
);
</script>
<template>
<span>{{ result.applied }} marked</span>
</template>A function source runs again after each document change, so the marks follow the text. The set is removed when the component unmounts.
Configure a highlight set
| Argument or field | Description |
|---|---|
name | The set name. Two sets with the same name replace each other's ranges. |
source | The ranges, or a function of the editor that returns them. |
options.className | CSS classes added to every mark of the set. |
options.activeIndex | The range to show in the active color, such as the current result of your own search list. |
applied | The number of ranges that resolve and paint. |
Use highlights outside components
Call watchHighlights(editor, name, source, options) from @docx-editor.dev/core/editor to follow document changes. Call stop() on the returned watch to remove the set and stop watching. For a fixed set that does not refresh, call editor.setHighlights.
See also
Custom cards in the review rail
Render your own review cards inside DocxEditorReview with preset set to false, and keep the comment button and compose box wired.
Accept or reject without scrolling
Open a tracked change from your own list without moving the page, then accept or reject it with useReview.