CodeDiff
Qué cambió entre dos versiones de un texto: un archivo en un pull request, una política, una migración. Las líneas quitadas con −, las agregadas con + y los tramos largos sin cambios plegados.
import { CodeDiff, CodeDiffSkeleton } from '@sypnia/ui/react'
Una política que cambió
| create policy "read own orders" |
| on orders for select |
| quitada: using (customer_id = auth.uid()); |
| agregada: using (customer_id = auth.uid() or auth.role() = 'staff'); |
| create policy "insert own orders" |
| on orders for insert |
| quitada: with check (customer_id = auth.uid()); |
| agregada: with check (customer_id = auth.uid() and total > 0); |
Código
import { CodeDiff } from '@sypnia/ui/react';
// A change to a policy: removed lines with −, added lines with +, both said in words to screen readers.
const before = `create policy "read own orders"
on orders for select
using (customer_id = auth.uid());
create policy "insert own orders"
on orders for insert
with check (customer_id = auth.uid());`;
const after = `create policy "read own orders"
on orders for select
using (customer_id = auth.uid() or auth.role() = 'staff');
create policy "insert own orders"
on orders for insert
with check (customer_id = auth.uid() and total > 0);`;
export default function Policy() {
return <CodeDiff title="db/policies.sql" lang="sql" before={before} after={after} />;
} Un archivo largo con un cambio
| { id: 19, name: 'Product 19', price: 38 }, | |||
| { id: 20, name: 'Product 20', price: 40 }, | |||
| quitada: { id: 21, name: 'Product 21', price: 42 }, | |||
| agregada: { id: 21, name: 'Rye loaf', price: 7 }, | |||
| { id: 22, name: 'Product 22', price: 44 }, | |||
| { id: 23, name: 'Product 23', price: 46 }, | |||
Código
import { CodeDiff } from '@sypnia/ui/react';
// A long file with one change: the unchanged lines fold away. Press a fold to open it.
const lines = Array.from({ length: 40 }, (_, i) => ` { id: ${i + 1}, name: 'Product ${i + 1}', price: ${(i + 1) * 2} },`);
const before = ['export const products = [', ...lines, '];'].join('\n');
const after = before.replace("name: 'Product 21', price: 42", "name: 'Rye loaf', price: 7");
export default function Folded() {
return <CodeDiff title="src/products.ts" lang="ts" before={before} after={after} context={2} />;
} Skeleton
Código
import { CodeDiffSkeleton } from '@sypnia/ui/react';
// While the two versions load.
export default function Skeleton() {
return <CodeDiffSkeleton lines={5} />;
} Cuándo usarlo
Úsalo cuando
- Revisar un cambio antes de aplicarlo.
- Mostrar qué cambió el agente en un arreglo.
No lo uses cuando
- Texto que no va por líneas, como un párrafo: di qué cambió en palabras.
Para todos
- Cada línea cambiada empieza con «agregada» o «quitada» para lectores de pantalla; el color nunca es la única señal.
- Los números de la barra dicen cuántas líneas se agregaron y cuántas se quitaron.
Props
| CodeDiff | Tipo | Por defecto |
|---|---|---|
afterobligatorio | string | |
beforeobligatorio | string | |
contextUnchanged lines kept around each change; longer runs fold. | number | 3 |
lang | string | text |
titleThe file: «db/policies.sql». | string |
| CodeDiffSkeleton | Tipo | Por defecto |
|---|---|---|
lines | number | 6 |