TextCompareForBlazor 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package TextCompareForBlazor --version 1.0.1
                    
NuGet\Install-Package TextCompareForBlazor -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TextCompareForBlazor" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TextCompareForBlazor" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="TextCompareForBlazor" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TextCompareForBlazor --version 1.0.1
                    
#r "nuget: TextCompareForBlazor, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TextCompareForBlazor@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TextCompareForBlazor&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=TextCompareForBlazor&version=1.0.1
                    
Install as a Cake Tool

TextCompareForBlazor

TextCompareForBlazor is a lightweight, client-side Blazor (WebAssembly or Server) component for visually comparing two text blobs side-by-side with selectable diff granularity (Character / Word / Line), optional whitespace normalization, synchronized scrolling, line alignment (with gap placeholders), and customizable theming.

Features

  • Side-by-side synchronized scroll viewing (toggleable)
  • Granularity selector: Character, Word, or Line
  • Word diff uses token mapping to preserve original spacing
  • Intelligent line alignment (modified / inserted / deleted handling)
  • Optional whitespace collapsing (IgnoreWhitespace)
  • Optional NBSP width preservation for deleted/inserted tokens (UseTokenPadding)
  • Line numbers (toggleable)
  • Footer metrics: lines, words, characters, differences
  • Fully styleable via parameters + CSS variable hooks
  • Accessible structure: ARIA labels, focusable line items

Getting Started

Add a project/package reference to TextCompareForBlazor (project reference or NuGet, if published). Then:

Set originalText / modifiedText in your page/component C# code.

Minimal Example (.razor)

<CompareText Text="@textA" Text2="@textB" Width="800px" Height="600px" />
@code {
    string textA = "...";
    string textB = "...";
}

Component Parameters

Name Type Default Description
LeftCaption string Original Caption above left panel.
RightCaption string Modified Caption above right panel.
LeftText string "" Source/original content.
RightText string "" Target/modified content.
ShowGranularitySelector bool true Shows the Character/Word/Line dropdown.
ShowIgnoreWhitespace bool true Toggle to collapse whitespace runs.
ShowSyncToggle bool true Toggle for scroll sync.
ShowLineNumbers bool true Displays gutter line numbers.
SyncScrolling bool true Initial sync state (can be toggled).
IgnoreWhitespace bool false Collapses all whitespace to single spaces for comparison.
UseTokenPadding bool false Pads deleted/inserted word tokens with NBSPs to retain horizontal alignment.
ShowFooter bool true Shows metrics footer.
Granularity DiffGranularity Character Current diff level: Character, Word, or Line.
UserStyle string? width:auto; height:auto; Inline style appended to root container.
Styling params string (various) Colors / theming (caption, gutter, diff highlights).

Styling Parameters

Param Purpose CSS Variable
CaptionBackground Caption background --caption-bg
CaptionTextColor Caption text color --caption-text
FooterBackground Footer background --footer-bg
FooterTextColor Footer text --footer-text
GutterBackground Line number gutter --gutter-bg
GutterNumberColor Line number color --gutter-number-color
LeftDiffColor Highlight for left diffs --left-diff
RightDiffColor Highlight for right diffs --right-diff
EmptyLineBackground Placeholder line background --empty-line-bg
EmptyLineBorder Placeholder line border --empty-line-border

Override by passing parameter values or extend with a wrapping CSS class via UserStyle.

Granularity Behavior

  • Character: Fine-grained semantic diff (uses DiffMatchPatch) on each line.
  • Word: Tokenizes into whitespace + word runs; preserves spacing; optional NBSP padding.
  • Line: Each aligned line treated as a single segment (modified vs equal).

Alignment Logic

Lines are normalized (optionally whitespace collapsed), then encoded into tokens for diffing. Deletes followed immediately by inserts are paired as modified lines; pure inserts/deletes create blank counterpart placeholders for visual alignment.

Accessibility

  • Panels have aria-label regions.
  • Lines are focusable (tabindex="0"), enabling keyboard navigation.
  • Placeholder lines include a title for context.

JavaScript Interop

Scroll synchronization uses a tiny JS module (tsCompareSync) injected inline. No external script file needed. If you bundle scripts separately, you may extract that <script> block.

Performance Notes

  • DiffMatchPatch is efficient for typical text sizes; extremely large files (> several MB) may benefit from pre-truncation.
  • Word granularity token mapping uses in-memory dictionaries; memory footprint proportional to distinct tokens.
  • Re-render triggers recomputation when IgnoreWhitespace or Granularity changes�cache externally if needed.

Extensibility Ideas

  • Add export of diff summary (counts)
  • Provide virtualized rendering for very large files (wrap lines list)
  • Add unified diff view mode (single panel) or inline change markers

Example With Custom Theme


<SideBySide Text="@textA" Text2="@textB" 
    LeftCaption="Before" RightCaption="After"
    IgnoreWhitespace="true" Granularity="Word"
    UserStyle="--caption-bg:#f0f0f0; --footer-bg:#f8f8f8;"
    Width="800px" Height="600px" />
@code {
    string textA = "...";
    string textB = "...";
}

  • Lines = aligned line pair count
  • Words / characters refer to right side counts (you can extend to show both)
  • Differences = number of non-equal segments under current granularity

Namespace

Use @using TextCompareForBlazor.Components (or add in _Imports.razor).

License / Attribution

Uses DiffMatchPatch via NuGet (DiffMatchPatch 3.0.0). Ensure compliance with its license (Apache 2.0).


Questions or improvements? Extend SideBySideTextCompareBase in your own partial class for additional behaviors.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.2 366 12/10/2025
1.2.1 425 12/9/2025
1.2.0 185 12/5/2025
1.1.0 185 12/4/2025
1.0.3 653 12/3/2025
1.0.2 1,133 12/1/2025
1.0.1 182 11/26/2025
1.0.0 169 11/24/2025

1.0.1:
- Toolbar UX: added word Search and next/previous match navigation.
- Search UX: active match highlight color updated (configurable via CSS variables).
- Case sensitivity: new parameter to toggle case sensitivity in diffs.
- Dynamic layout: panels auto-expand when toolbar is hidden; height subtracts/adds toolbar size via CSS variable.