${data.title}
${data.summary}
| ${item.name} | ${item.value} |
### Quickstart HTML Diff Usage (TypeScript) Source: https://github.com/opral/html-diff/blob/main/docs/guide/index.mdx A quickstart example demonstrating how to use `renderHtmlDiff` with simple HTML strings and includes importing the default CSS for styling diff highlights. It shows rendering the diff in vanilla JavaScript and provides a React component example. ```typescript import { renderHtmlDiff } from "@lix-js/html-diff"; import "@lix-js/html-diff/default.css"; const beforeHtml = `
The quick brown fox
`; const afterHtml = `The fast red fox
`; const diff = renderHtmlDiff({ beforeHtml, afterHtml }); // Vanilla document.getElementById("diff-container")!.innerHTML = diff; // React function Diff({ html }: { html: string }) { return ; } ``` -------------------------------- ### Install HTML Diff using pnpm Source: https://github.com/opral/html-diff/blob/main/README.md This command installs the HTML Diff library using the pnpm package manager. It's the first step to integrating HTML Diff into your project. ```bash pnpm add @lix-js/html-diff ``` -------------------------------- ### Install HTML Diff (Bash) Source: https://github.com/opral/html-diff/blob/main/docs/guide/index.mdx Command to install the HTML Diff library using npm. This is the first step to integrating the library into your project. ```bash npm install @lix-js/html-diff ``` -------------------------------- ### Showcase Component for HTML Diff Example Source: https://github.com/opral/html-diff/blob/main/docs/guide/how-it-works.mdx A React Showcase component demonstrating how HTML diffing works with `data-diff-key` attributes. It displays 'before' and 'after' HTML snippets and associated CSS for styling diffed elements. ```jsx import Showcase from "../components/showcase";| $6 | $45 |
Some text
...
``` -------------------------------- ### Custom CSS Styling for HTML Diff Results Source: https://context7.com/opral/html-diff/llms.txt Provides examples of how to style HTML diff outputs using CSS. You can either import the library's default styles or define your own custom styles by targeting the 'data-diff-status' attribute values (added, modified, removed). ```css /* Import default styles */ @import "@lix-js/html-diff/default.css"; /* Or define custom styles */ [data-diff-status="added"] { color: #1f883d; background-color: #dafbe1; text-decoration: none; } [data-diff-status="modified"] { color: #f59e0b; background-color: #fff3cd; text-decoration: none; } [data-diff-status="removed"] { color: #d1242f; background-color: #ffebe9; text-decoration: line-through; } /* Word-level removed text with strikethrough */ [data-diff-mode="words"] [data-diff-status="removed"] { text-decoration: line-through; } /* Your existing component styles remain intact */ .card { padding: 20px; border: 1px solid #ccc; border-radius: 8px; } /* Diff highlighting layers on top */ .card[data-diff-status="added"] { border-color: #1f883d; } ``` -------------------------------- ### Importing HTML and CSS for Rich Text Document Diff Source: https://github.com/opral/html-diff/blob/main/docs/examples/simple-rich-text-document/index.mdx Imports necessary HTML content (before and after states) and CSS styles for displaying and diffing a rich text document. The '?raw' import ensures the content is loaded as a string. ```javascript import Showcase from "../../components/showcase"; import beforeHtml from "./before.html?raw"; import afterHtml from "./after.html?raw"; import showcaseCSS from "./styles.css?raw"; import defaultCSS from "../../../src/default.css?raw";| $100 | Active |
| $200 | Pending |
| $150 | Active |
| $200 | Completed |
{user.email}
First paragraph
First paragraph
Second paragraph
Hello
`, afterHtml: `Hello world
`, diffAttribute: "data-id" }); ``` -------------------------------- ### Implementing Interactive Features with JavaScript Source: https://github.com/opral/html-diff/blob/main/docs/guide/interactivity.mdx This JavaScript code demonstrates how to generate an HTML diff, render it to the DOM, and then use DOM APIs to query for elements with specific diff statuses and keys. It shows how to attach event listeners for hover, click, and keyboard navigation, and how to store original data for comparison. ```javascript // 1. Generate the diff const diffResult = renderHtmlDiff({ beforeHtml, afterHtml }); // Or, if your HTML already includes stable IDs: // const diffResult = renderHtmlDiff({ beforeHtml, afterHtml, diffAttribute: 'data-id' }); // 2. Render to DOM container.innerHTML = diffResult; // 3. Query for interactive elements const modifiedElements = container.querySelectorAll( '[data-diff-status="modified"][data-diff-key]' ); const addedElements = container.querySelectorAll( '[data-diff-status="added"][data-diff-key]' ); const removedElements = container.querySelectorAll( '[data-diff-status="removed"][data-diff-key]' ); // 4. Add any interactivity you want modifiedElements.forEach((element) => { const diffKey = element.getAttribute("data-diff-key"); // Add hover cards element.addEventListener("mouseenter", () => showHoverCard(diffKey)); // Add click handlers element.addEventListener("click", () => showDetailedDiff(diffKey)); // Add keyboard navigation element.setAttribute("tabindex", "0"); element.addEventListener("keydown", handleKeyNavigation); // Add custom styling element.style.cursor = "pointer"; element.style.position = "relative"; }); // 5. Store original data for comparisons const originalData = extractOriginalData(beforeHtml, afterHtml); function showHoverCard(diffKey) { const data = originalData[diffKey]; // Create and position hover card showing data.before vs data.after } function showDetailedDiff(diffKey) { // Open modal or side panel with detailed change information } function handleKeyNavigation(event) { // Handle arrow keys to navigate between changes if (event.key === "ArrowRight") navigateToNextChange(); if (event.key === "ArrowLeft") navigateToPreviousChange(); } ``` -------------------------------- ### Generate and Render HTML Diff using TypeScript Source: https://github.com/opral/html-diff/blob/main/README.md This TypeScript code snippet demonstrates how to use the `renderHtmlDiff` function to generate a visual diff between two HTML strings. It also shows how to render the diff into a DOM element or a React component. ```typescript import { renderHtmlDiff } from "@lix-js/html-diff"; import "@lix-js/html-diff/default.css"; const tableBefore = renderTable(beforeData); const tableAfter = renderTable(afterData); const diff = renderHtmlDiff({ beforeHtml: tableBefore, afterHtml: tableAfter, // Optional: use a custom attribute to match elements // diffAttribute: 'data-id', }); // Vanilla render document.getElementById("diff-container")!.innerHTML = diff; // React render function DiffView({ html }: { html: string }) { return ; } ``` -------------------------------- ### Importing Default HTML Diff Styles Source: https://github.com/opral/html-diff/blob/main/docs/guide/styling.md Shows two methods for including the default stylesheet provided with the html-diff package. This can be done directly in JavaScript imports or by linking the CSS file in HTML. ```javascript import "@lix-js/html-diff/default.css"; ``` ```html ``` -------------------------------- ### Enable Word-Level HTML Diff Granularity Source: https://github.com/opral/html-diff/blob/main/docs/guide/index.mdx Explains how to enable word-level highlighting for HTML diffs by adding the `data-diff-mode="words"` attribute to elements. This provides more granular diffs compared to element-level highlighting. ```htmlThe quick brown fox
``` -------------------------------- ### CSS for HTML Diff Highlighting Source: https://github.com/opral/html-diff/blob/main/docs/guide/index.mdx Provides CSS selectors to highlight added, modified, and removed elements in an HTML diff. These styles can be added to an existing application's CSS without affecting other elements. ```css /* Your existing app styles */ .my-text { font-size: 24px; font-weight: bold; color: #2d3748; font-family: Georgia, serif; line-height: 1.4; } /* Only these selectors needed for diff highlighting */ [data-diff-status="added"] { color: #22c55e; } [data-diff-status="modified"] { color: #f59e0b; } [data-diff-status="removed"] { color: #ef4444; } ``` -------------------------------- ### React HTML Diff Integration Source: https://context7.com/opral/html-diff/llms.txt Integrates HTML Diff into a React application for comparing document versions. It defines functions to render documents with diff keys and compare HTML content using `renderHtmlDiff`. The output is rendered within a React component using `dangerouslySetInnerHTML`. ```typescript import { renderHtmlDiff } from "@lix-js/html-diff"; import "@lix-js/html-diff/default.css"; // Define a render function that adds diff keys to trackable elements function renderDocument(data: DocumentData): string { return ( `${data.summary}
| ${item.name} | ${item.value} |
| Product A | $10 |
| Product B | $20 |
| Product C | $30 |
| Product A | $10 |
| Product C | $30 |
The quick brown fox
`; const afterHtml = `The fast red fox
The lazy dog sleeps
`; const diff = renderHtmlDiff({ beforeHtml, afterHtml }); // The diff now contains: // - "The fast red fox" with data-diff-status="modified" // - "The lazy dog sleeps" with data-diff-status="added" ``` -------------------------------- ### Word-Level HTML Diffing with TypeScript Source: https://context7.com/opral/html-diff/llms.txt Demonstrates how to perform word-level HTML diffing using the renderHtmlDiff function. This mode highlights individual word changes by wrapping them in span elements with 'data-diff-status'. It's ideal for text-heavy content like paragraphs and headings. ```typescript import { renderHtmlDiff } from "@lix-js/html-diff"; const beforeHtml = `The quick brown fox jumps over the lazy dog.
`; const afterHtml = `The quick red fox leaps over the lazy cat.
`; const result = renderHtmlDiff({ beforeHtml, afterHtml }); // Result: Individual words are highlighted // "brown" wrapped with data-diff-status="removed" // "red" wrapped with data-diff-status="added" // "jumps" wrapped with data-diff-status="removed" // "leaps" wrapped with data-diff-status="added" // "dog" wrapped with data-diff-status="removed" // "cat" wrapped with data-diff-status="added" console.log(result); // Output: //// The quick brown // red fox // jumps // leaps over the lazy // dog. // cat. //
``` -------------------------------- ### Atomic Element Diffing with data-diff-mode="element" Source: https://context7.com/opral/html-diff/llms.txt Illustrates the use of 'data-diff-mode="element"' for atomic element diffing. This mode is suitable for complex components where word-level diffing might disrupt the structure. It displays both the removed and added versions of the element with corresponding 'data-diff-status' attributes. ```typescript import { renderHtmlDiff } from "@lix-js/html-diff"; const beforeHtml = `Price: $99
Price: $79
Price: $99
//Price: $79
//Hello World
``` ```htmlFirst paragraph
First paragraph
Second paragraph
Keep this
Remove this
Keep this
Welcome to our comprehensive updated guide for new usersdevelopers.
``` -------------------------------- ### Interactive HTML Diff Component in React Source: https://github.com/opral/html-diff/blob/main/docs/guide/interactivity.mdx This React component utilizes the renderHtmlDiff function from @lix-js/html-diff to display differences between two HTML strings. It implements interactive hover cards that appear when a user hovers over modified elements, showing the 'before' and 'after' values. The component manages state for the hover card and uses useEffect to perform the diffing and attach event listeners. ```javascript import { useRef, useEffect, useState } from "react"; import { renderHtmlDiff } from "@lix-js/html-diff"; export function InteractiveHtmlDiff() { const ref = useRef(null); const [hoverCard, setHoverCard] = useState(null); // Define your before and after HTML const beforeHtml = `| Monthly Price | $9 | $29 |
| Monthly Price | $12 | $39 |
Some content here
Different content here
Old content here
New content here
Hello there
Hello world
``` ```htmlHello there
Hello world
``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.