Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/diffviewer-core-blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/cli': patch
---

[docs] `astryx template` lists DiffViewer's blocks among Core's component blocks: the "Unified diff from a patch" showcase, and a new "DiffViewer — Viewed files" example, the file list of a review page, where a "Viewed" checkbox folds each file through `collapsible`.

@ernestt
7 changes: 7 additions & 0 deletions .changeset/promote-diffviewer-to-core.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[component] Promote `DiffViewer` to Core, imported from `@astryxdesign/core/DiffViewer` or the package root together with `parseUnifiedDiff`, `createDiffFile`, and the diff model types. DiffViewer shows a read-only, syntax-highlighted diff of one file in unified or split view: a file header with the path, status, and line counts over the hunks, review annotation rows under any line, optional line-number buttons, and folds for a change too large to show line by line. Every changed line reads as added or removed without colour, also under forced colors. The file header is a disclosure that starts open; `collapsible` takes the same `CollapsibleConfig` as Banner and Collapsible (`{defaultIsOpen: false}` starts it collapsed, `{isOpen, onOpenChange}` controls it, and `false` turns it off).

@ernestt
2 changes: 2 additions & 0 deletions apps/sandbox/scripts/next-export-routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@
"/templates/DialogScrollingContent/",
"/templates/DialogShowcase/",
"/templates/DialogWithSubtitle/",
"/templates/DiffViewerShowcase/",
"/templates/DiffViewerViewedFiles/",
"/templates/DividerFullBleed/",
"/templates/DividerShowcase/",
"/templates/DividerVariants/",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

import {describe, expect, it} from 'vitest';
import {createDiffFile} from '../../../../../../../packages/lab/src/DiffViewer/createDiffFile';
import {createDiffFile} from '../../../../../../../packages/core/src/DiffViewer/createDiffFile';
import {
countLines,
createMultiFileWorkload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
DiffViewer,
type DiffViewerFile,
type DiffViewerViewMode,
} from '@astryxdesign/lab';
} from '@astryxdesign/core/DiffViewer';
import {
createMultiFileWorkload,
createSingleFileWorkload,
Expand Down Expand Up @@ -396,12 +396,10 @@ export default function DiffViewerPerfPage() {
// Looks for a rendered diff now and then once per frame, so a file
// that renders late is caught at the frame it appears.
const watchFirstFile = () => {
// Unified view renders a <table>; split view, a role="table" grid;
// a file folded for its size, only its fold row.
// A DiffViewer renders its root and its rows (a table, a
// role="table" grid, or a fold row) in one commit.
if (
surfaceRef.current?.querySelector(
'table, [role="table"], [data-diff-fold]',
) != null
surfaceRef.current?.querySelector('.astryx-diff-viewer') != null
) {
afterNextPaint(renderStart, ms => {
firstFileMs = ms;
Expand Down
2 changes: 1 addition & 1 deletion apps/storybook/rtl-audit/targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@
},
{
"component": "DiffViewer",
"storyId": "lab-diffviewer--character-highlight",
"storyId": "core-diffviewer--character-highlight",
"dims": ["D2"],
"selectors": {
"prev": ".astryx-diff-viewer-path",
Expand Down
6 changes: 3 additions & 3 deletions apps/storybook/stories/DiffViewer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
createDiffFile,
parseUnifiedDiff,
type DiffViewerLineRef,
} from '@astryxdesign/lab';
} from '@astryxdesign/core/DiffViewer';
import {Button} from '@astryxdesign/core/Button';
import {Card} from '@astryxdesign/core/Card';
import {CheckboxInput} from '@astryxdesign/core/CheckboxInput';
Expand Down Expand Up @@ -76,7 +76,7 @@ const FILES = parseUnifiedDiff(PATCH);
const [TOTAL_FILE] = FILES;

const meta: Meta<typeof DiffViewer> = {
title: 'Lab/DiffViewer',
title: 'Core/DiffViewer',
component: DiffViewer,
tags: ['autodocs'],
parameters: {
Expand Down Expand Up @@ -290,7 +290,7 @@ export const ViewedToggle: Story = {
<DiffViewer
{...args}
collapsible={{isOpen, onOpenChange: setIsOpen}}
headerEnd={
endContent={
<CheckboxInput
label="Viewed"
value={isViewed}
Expand Down
4 changes: 2 additions & 2 deletions docs/specs/components/CodeBlock/CodeBlock.spec.md

Large diffs are not rendered by default.

305 changes: 305 additions & 0 deletions docs/specs/components/DiffViewer/DiffViewer.spec.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2306,7 +2306,6 @@
"INV2",
"INV3"
],
"packages/lab/src/DiffViewer/DiffViewer.tsx": ["FR13", "FR3", "FR3"],
"packages/lab/src/LogStream/LogStream.tsx": ["FR3"],
"packages/lab/src/Sankey/SankeyLabel.tsx": ["#0", "#1"],
"packages/lab/src/Schedule/MonthlyView.tsx": [
Expand Down
17 changes: 12 additions & 5 deletions internal/shadcn-registry/routes.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@
"path": "components/dialog-header",
"aliases": []
},
{
"name": "component-diff-viewer",
"path": "components/diff-viewer",
"aliases": [
"components/lab/diff-viewer"
]
},
{
"name": "component-divider",
"path": "components/divider",
Expand Down Expand Up @@ -617,11 +624,6 @@
"path": "components/lab/dialog-hero-header",
"aliases": []
},
{
"name": "component-lab-diff-viewer",
"path": "components/lab/diff-viewer",
"aliases": []
},
{
"name": "component-lab-emoji-picker",
"path": "components/lab/emoji-picker",
Expand Down Expand Up @@ -2183,6 +2185,11 @@
"path": "examples/dialog/scrollable",
"aliases": []
},
{
"name": "example-diff-viewer-viewed-files",
"path": "examples/diff-viewer/viewed-files",
"aliases": []
},
{
"name": "example-divider-full-bleed",
"path": "examples/divider/full-bleed",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */
export default {
export const doc = {
type: 'block',
name: 'Unified diff from a patch',
displayName: 'Unified diff from a patch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
/**
* @input A unified diff string parsed with parseUnifiedDiff
* @output One DiffViewer with a unified/split toggle and click-to-comment line numbers
* @position Lab DiffViewer's docsite showcase and copyable CLI block
* @position DiffViewer's docsite showcase and copyable CLI block
*/

import {useState} from 'react';
import {
DiffViewer,
parseUnifiedDiff,
type DiffViewerLineRef,
} from '@astryxdesign/lab';
} from '@astryxdesign/core/DiffViewer';
import {Button} from '@astryxdesign/core/Button';
import {HStack, StackItem, VStack} from '@astryxdesign/core/Stack';
import {Text} from '@astryxdesign/core/Text';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */
export const doc = {
type: 'block',
exampleFor: 'DiffViewer',
name: 'DiffViewer — Viewed files',
displayName: 'Diff Viewer — Viewed files',
description:
'The file list of a review page: one DiffViewer per file in a patch, each with a "Viewed" checkbox in endContent. Checking it folds the file through a controlled collapsible, and the file header still opens and closes the file on its own.',
isReady: true,
aspectRatio: 4 / 3,
componentsUsed: ['DiffViewer', 'CheckboxInput', 'VStack'],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

'use client';

/**
* @input A two-file patch parsed with parseUnifiedDiff
* @output One DiffViewer per file, each with a "Viewed" checkbox that folds it
* @position DiffViewer example block: the file list of a review page
*/

import {useState} from 'react';
import {
DiffViewer,
parseUnifiedDiff,
type DiffViewerFile,
} from '@astryxdesign/core/DiffViewer';
import {CheckboxInput} from '@astryxdesign/core/CheckboxInput';
import {VStack} from '@astryxdesign/core/Stack';

const PATCH = `diff --git a/src/cart/total.ts b/src/cart/total.ts
--- a/src/cart/total.ts
+++ b/src/cart/total.ts
@@ -1,6 +1,7 @@
import type {CartItem} from './types';
+import {roundCurrency} from '../money';

export function cartTotal(items: CartItem[], discount = 0) {
- return items.reduce((sum, item) => sum + item.price, 0) * (1 - discount);
+ const subtotal = items.reduce((sum, item) => sum + item.price, 0);
+ return roundCurrency(subtotal * (1 - discount));
}
diff --git a/src/money.ts b/src/money.ts
new file mode 100644
--- /dev/null
+++ b/src/money.ts
@@ -0,0 +1,4 @@
+/** Round to whole cents. */
+export function roundCurrency(value: number): number {
+ return Math.round(value * 100) / 100;
+}
`;

const FILES = parseUnifiedDiff(PATCH);

function ReviewFile({file}: {file: DiffViewerFile}) {
const [isViewed, setIsViewed] = useState(false);
const [isOpen, setIsOpen] = useState(true);
return (
<DiffViewer
file={file}
collapsible={{isOpen, onOpenChange: setIsOpen}}
endContent={
<CheckboxInput
label="Viewed"
value={isViewed}
onChange={checked => {
setIsViewed(checked);
setIsOpen(!checked);
}}
/>
}
/>
);
}

export default function DiffViewerViewedFiles() {
return (
<VStack gap={3}>
{FILES.map(file => (
<ReviewFile key={file.newPath ?? file.oldPath} file={file} />
))}
</VStack>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "search",
"data": {
"query": "close",
"matchCount": 54,
"matchCount": 55,
"results": [
{
"domain": "component",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Results for "close" (20 of 54)
Results for "close" (20 of 55)
name: AlertDialog
domain: component
package: @astryxdesign/core
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/swizzle-proof/samples.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ export const samples = {
onSubmit: noop,
}),
),
lab__DiffViewer: ({C, React, mod, Pkg}) => {
core__DiffViewer: ({C, React, mod, Pkg}) => {
const parse = mod.parseUnifiedDiff ?? Pkg.parseUnifiedDiff;
const patch = [
'diff --git a/a.ts b/a.ts',
Expand Down
44 changes: 22 additions & 22 deletions packages/core/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -741,91 +741,91 @@
},
"@astryx.diffViewer.label": {
"defaultMessage": "Diff",
"description": "Accessible name of a lab DiffViewer's scrollable code area when the file has no path to name it."
"description": "Accessible name of a DiffViewer's scrollable code area when the file has no path to name it."
},
"@astryx.diffViewer.renamedPath": {
"defaultMessage": "{oldPath} → {newPath}",
"description": "File path shown in a lab DiffViewer header when the file was renamed. `{oldPath}` and `{newPath}` are file paths and must stay untranslated; keep the arrow pointing from the old path to the new one."
"description": "File path shown in a DiffViewer header when the file was renamed. `{oldPath}` and `{newPath}` are file paths and must stay untranslated; keep the arrow pointing from the old path to the new one."
},
"@astryx.diffViewer.statusAdded": {
"defaultMessage": "Added",
"description": "Badge in a lab DiffViewer file header marking a file that the change creates."
"description": "Badge in a DiffViewer file header marking a file that the change creates."
},
"@astryx.diffViewer.statusDeleted": {
"defaultMessage": "Deleted",
"description": "Badge in a lab DiffViewer file header marking a file that the change removes."
"description": "Badge in a DiffViewer file header marking a file that the change removes."
},
"@astryx.diffViewer.statusRenamed": {
"defaultMessage": "Renamed",
"description": "Badge in a lab DiffViewer file header marking a file that the change moves to a new path."
"description": "Badge in a DiffViewer file header marking a file that the change moves to a new path."
},
"@astryx.diffViewer.addedCount": {
"defaultMessage": "+{count, number}",
"description": "Compact count of added lines shown in a lab DiffViewer file header, e.g. \"+12\". Hidden from screen readers, which hear `@astryx.diffViewer.stats` instead."
"description": "Compact count of added lines shown in a DiffViewer file header, e.g. \"+12\". Hidden from screen readers, which hear `@astryx.diffViewer.stats` instead."
},
"@astryx.diffViewer.removedCount": {
"defaultMessage": "−{count, number}",
"description": "Compact count of removed lines shown in a lab DiffViewer file header, e.g. \"−3\" (a minus sign). Hidden from screen readers, which hear `@astryx.diffViewer.stats` instead."
"description": "Compact count of removed lines shown in a DiffViewer file header, e.g. \"−3\" (a minus sign). Hidden from screen readers, which hear `@astryx.diffViewer.stats` instead."
},
"@astryx.diffViewer.stats": {
"defaultMessage": "{added, plural, one {# line added} other {# lines added}}, {removed, plural, one {# line removed} other {# lines removed}}",
"description": "Screen-reader text for the line counts in a lab DiffViewer file header, e.g. \"12 lines added, 3 lines removed\"."
"description": "Screen-reader text for the line counts in a DiffViewer file header, e.g. \"12 lines added, 3 lines removed\"."
},
"@astryx.diffViewer.added": {
"defaultMessage": "Added",
"description": "Screen-reader text marking one line of a lab DiffViewer as added by the change; sighted readers see a + sign and a tinted row."
"description": "Screen-reader text marking one line of a DiffViewer as added by the change; sighted readers see a + sign and a tinted row."
},
"@astryx.diffViewer.removed": {
"defaultMessage": "Removed",
"description": "Screen-reader text marking one line of a lab DiffViewer as removed by the change; sighted readers see a - sign and a tinted row."
"description": "Screen-reader text marking one line of a DiffViewer as removed by the change; sighted readers see a - sign and a tinted row."
},
"@astryx.diffViewer.lineNumberButton": {
"defaultMessage": "{side, select, old {Original line {line, number}} other {Changed line {line, number}}}",
"description": "Accessible label of a clickable line number in a lab DiffViewer (callers use it, for example, to start a comment on that line). `side` is `old` for the file before the change and `new` for the file after it; `line` is the line number in that version."
"description": "Accessible label of a clickable line number in a DiffViewer (callers use it, for example, to start a comment on that line). `side` is `old` for the file before the change and `new` for the file after it; `line` is the line number in that version."
},
"@astryx.diffViewer.columnOldLine": {
"defaultMessage": "Original line number",
"description": "Screen-reader-only header of a lab DiffViewer column holding line numbers in the file before the change. Pairs with `@astryx.diffViewer.lineNumberButton` (\"Original line …\")."
"description": "Screen-reader-only header of a DiffViewer column holding line numbers in the file before the change. Pairs with `@astryx.diffViewer.lineNumberButton` (\"Original line …\")."
},
"@astryx.diffViewer.columnNewLine": {
"defaultMessage": "Changed line number",
"description": "Screen-reader-only header of a lab DiffViewer column holding line numbers in the file after the change. Pairs with `@astryx.diffViewer.lineNumberButton` (\"Changed line …\")."
"description": "Screen-reader-only header of a DiffViewer column holding line numbers in the file after the change. Pairs with `@astryx.diffViewer.lineNumberButton` (\"Changed line …\")."
},
"@astryx.diffViewer.columnChange": {
"defaultMessage": "Change",
"description": "Screen-reader-only header of a lab DiffViewer column that marks each line as added or removed (noun: the kind of change)."
"description": "Screen-reader-only header of a DiffViewer column that marks each line as added or removed (noun: the kind of change)."
},
"@astryx.diffViewer.columnCode": {
"defaultMessage": "Code",
"description": "Screen-reader-only header of the source-code column in a lab DiffViewer's single-column (unified) view."
"description": "Screen-reader-only header of the source-code column in a DiffViewer's single-column (unified) view."
},
"@astryx.diffViewer.columnOldCode": {
"defaultMessage": "Original code",
"description": "Screen-reader-only header of the source-code column for the file before the change, in a lab DiffViewer's side-by-side (split) view."
"description": "Screen-reader-only header of the source-code column for the file before the change, in a DiffViewer's side-by-side (split) view."
},
"@astryx.diffViewer.columnNewCode": {
"defaultMessage": "Changed code",
"description": "Screen-reader-only header of the source-code column for the file after the change, in a lab DiffViewer's side-by-side (split) view."
"description": "Screen-reader-only header of the source-code column for the file after the change, in a DiffViewer's side-by-side (split) view."
},
"@astryx.diffViewer.binary": {
"defaultMessage": "Binary file not shown",
"description": "Message in place of line changes in a lab DiffViewer when the file is binary and has no text diff."
"description": "Message in place of line changes in a DiffViewer when the file is binary and has no text diff."
},
"@astryx.diffViewer.noChanges": {
"defaultMessage": "No content changes",
"description": "Message in a lab DiffViewer when a file changed only its path or mode, so there are no line changes to show."
"description": "Message in a DiffViewer when a file changed only its path or mode, so there are no line changes to show."
},
"@astryx.diffViewer.largeChange": {
"defaultMessage": "Large change: {count, plural, one {# line} other {# lines}}",
"description": "Label of a folded block in a lab DiffViewer. The change was too large to compare line by line, so the block shows its old lines as removed and its new lines as added, folded until the reader opens it. `{count}` is the number of removed plus added lines in the block; some can be the same on both sides, because the block was not compared. Keep it short: it shares one row with the Show full diff button."
"description": "Label of a folded block in a DiffViewer. The change was too large to compare line by line, so the block shows its old lines as removed and its new lines as added, folded until the reader opens it. `{count}` is the number of removed plus added lines in the block; some can be the same on both sides, because the block was not compared. Keep it short: it shares one row with the Show full diff button."
},
"@astryx.diffViewer.showFullDiff": {
"defaultMessage": "Show full diff",
"description": "Button beside `@astryx.diffViewer.largeChange` in a lab DiffViewer that shows the folded lines of a large change. Pairs with `@astryx.diffViewer.hideFullDiff`."
"description": "Button beside `@astryx.diffViewer.largeChange` in a DiffViewer that shows the folded lines of a large change. Pairs with `@astryx.diffViewer.hideFullDiff`."
},
"@astryx.diffViewer.hideFullDiff": {
"defaultMessage": "Hide full diff",
"description": "Button beside `@astryx.diffViewer.largeChange` in a lab DiffViewer that folds the lines of a large change again after `@astryx.diffViewer.showFullDiff` showed them."
"description": "Button beside `@astryx.diffViewer.largeChange` in a DiffViewer that folds the lines of a large change again after `@astryx.diffViewer.showFullDiff` showed them."
},
"@astryx.markdownHeadingLinks.copied": {
"defaultMessage": "Link copied",
Expand Down
Loading
Loading