TextCompareForBlazor 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package TextCompareForBlazor --version 1.0.3
                    
NuGet\Install-Package TextCompareForBlazor -Version 1.0.3
                    
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.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TextCompareForBlazor" Version="1.0.3" />
                    
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.3
                    
#r "nuget: TextCompareForBlazor, 1.0.3"
                    
#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.3
                    
#: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.3
                    
Install as a Cake Addin
#tool nuget:?package=TextCompareForBlazor&version=1.0.3
                    
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.

What's New in 1.0.3

  • Unified font family and size applied to both gutter (line numbers) and text content via CSS variables.
  • Toolbar dropdowns for Font and Size.
  • Toolbar order: Search first, then Granularity/Whitespace, then Font; Sync toggle right-aligned.
  • Typical, widely available monospace font stacks offered by default.

Usage: Unified Font Settings

Control the font family and size for the entire compare surface (both gutters and text areas). These parameters can be set directly or via toolbar dropdowns when ShowToolbar is true.

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)

<TextCompareForBlazor.Components.SideBySide UserStyle="width:calc(100vw - 320px); height:calc(100vh - 180px);" LeftText="@OriginalText" RightText="@ModifiedText" />
@code {
    string OriginalText = "...";
    string ModifiedText = "...";
}

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.
ShowToolbar bool true Shows the toolbar containing Search, Granularity/Whitespace, Font controls, and Sync toggle.
ShowSearch bool true Displays the search box, match count, navigation buttons, and clear action.
ShowCaseSensitiveToggle bool true Displays the case-sensitive search toggle in the search group.
CaseSensitiveSearch bool false When true, search uses case-sensitive matching.
FontFamily string e.g., "Consolas, 'Courier New', monospace" or your project default Unified font family applied to both gutter line numbers and text content.
FontSize int 14 Unified font size (pixels) applied to both gutter and text content.
FontFamilies IEnumerable<string>? null Optional list of font stacks to populate the Font dropdown in the toolbar.
FontSizes IEnumerable<int>? null Optional list of font sizes to populate the Size dropdown in the toolbar.

Tip: Font settings are propagated via CSS variables --tcb-font-family and --tcb-font-size to kee

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
SearchHitBackground Background for non-active matches --search-hit-bg
SearchHitColor Text color for non-active matches --search-hit-color
SearchHitBorder Border for non-active matches --search-hit-border
SearchHitActiveBackground Background for the active match --search-hit-active-bg
SearchHitActiveColor Text color for the active match --search-hit-active-color
SearchHitActiveBorder Border for the active match --search-hit-active-border

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

Unified Font CSS Variables

Font settings propagate to both gutter and text content via these variables:

  • --tcb-font-family
  • --tcb-font-size

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.3:
- Unified font family and size settings applied via CSS variables to both gutter and text content (line numbers and text change together).
- Added toolbar dropdowns for Font and Size.
- Reordered toolbar groups: Search first, then Granularity/Whitespace, then Font; Sync toggle remains aligned right.
- Limited AvailableFonts to typical, widely available monospace stacks across Windows/macOS/Linux.
- Minor: preserved scroll sync behavior and search highlight logic.

1.0.2:
- Adjusted scrolling when searching for a word - Scroll only when word is out of view