Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -11,8 +11,8 @@
* cost of collapse, expand, and view switches; files mount on screen by
* default, or all at once

* @position Fullscreen sandbox tool for evaluating the Lab DiffViewer on large
* change sets
* @position Fullscreen sandbox tool for evaluating DiffViewer on large change
* sets
*/

import {
Expand Down 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
8 changes: 4 additions & 4 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
Loading
Loading