File Diff
Preview
<script setup lang="ts">
import { FileDiff, type FileDiffLine } from 'beui-vue'
import { computed, onBeforeUnmount, onMounted, shallowRef } from 'vue'
const DIFF_LINES: FileDiffLine[] = [
{ id: '1', oldLine: 18, newLine: 18, content: 'export async function runTask() {' },
{ id: '2', type: 'removed', oldLine: 19, content: ' return execute(task);' },
{ id: '3', type: 'added', newLine: 19, content: ' const result = await execute(task);' },
{ id: '4', type: 'added', newLine: 20, content: ' return normalize(result);' },
{ id: '5', oldLine: 20, newLine: 21, content: '}' },
]
const INTERVAL = 360
const visible = shallowRef(0)
const done = shallowRef(false)
const run = shallowRef(0)
let timers: ReturnType<typeof setTimeout>[] = []
const lines = computed(() => DIFF_LINES.slice(0, visible.value))
const copyText = DIFF_LINES.map(line => line.content).join('\n')
function start() {
timers.forEach(clearTimeout)
visible.value = 0
done.value = false
timers = DIFF_LINES.map((_, index) => setTimeout(() => { visible.value = index + 1 }, index * INTERVAL + 180))
timers.push(setTimeout(() => { done.value = true }, DIFF_LINES.length * INTERVAL + 480))
}
function replay() { run.value += 1; start() }
onMounted(start)
onBeforeUnmount(() => timers.forEach(clearTimeout))
</script>
<template>
<div class="demo">
<FileDiff :key="run" file="src/runner.ts" :lines="lines" :status="done ? 'complete' : 'streaming'" :copy-text="copyText" :max-height="150" />
<button type="button" class="replay" @click="replay"><svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" /><path d="M3 3v5h5" /></svg>Replay</button>
</div>
</template>
<style scoped>
.demo { position: relative; width: 100%; max-width: 32rem; height: 300px; }
.replay { position: absolute; bottom: 0; left: 0; display: inline-flex; align-items: center; gap: 0.375rem; padding: 0.25rem 0.5rem; border: 0; border-radius: 0.375rem; background: transparent; color: var(--beui-muted-foreground); font: inherit; font-size: 0.75rem; line-height: 1rem; font-weight: 500; cursor: pointer; outline: none; transition: color 0.15s, background-color 0.15s; }
.replay:hover { background: var(--beui-muted); color: var(--beui-foreground); }
.replay:focus-visible { box-shadow: 0 0 0 2px var(--beui-ring); }
</style><script lang="ts">
export const fileDiffMessages = { applying: 'Applying changes', applied: 'Changes applied', changes: 'File changes', copy: 'Copy diff', copied: 'Copied' }
export type FileDiffLine = { id: string, type?: 'added' | 'removed' | 'context', oldLine?: number, newLine?: number, content: string }
</script>
<script setup lang="ts">
import { motion } from 'motion-v'
import { useReducedMotion } from '../core/reduced-motion'
import { computed, onBeforeUnmount, useId, useTemplateRef, watch } from 'vue'
import { pressSpring, swapSpring } from '../core/motion'
import { useOpenState } from '../primitives/state'
import AgentCodeLine from './AgentCodeLine.vue'
import AgentIcon from './AgentIcon.vue'
import AgentReveal from './AgentReveal.vue'
import './agent-tokens.css'
import { useCopy, type Labels } from './shared'
const props = withDefaults(defineProps<{
file: string, lines: FileDiffLine[], status?: 'streaming' | 'complete', open?: boolean, defaultOpen?: boolean, collapseOnComplete?: boolean,
maxHeight?: number, language?: string, copyText?: string, onCopy?: () => void | Promise<void>, labels?: Labels
}>(), { status: 'streaming', open: undefined, defaultOpen: true, collapseOnComplete: true, maxHeight: 220, language: 'typescript' })
const emit = defineEmits<{ 'update:open': [value: boolean], openChange: [value: boolean] }>()
const reduce = useReducedMotion()
const { open, setOpen } = useOpenState(() => props.open, props.defaultOpen, next => { emit('update:open', next); emit('openChange', next) })
const { copied, copy } = useCopy()
const uid = useId()
const viewport = useTemplateRef<HTMLElement>('viewport')
const streaming = computed(() => props.status === 'streaming')
const additions = computed(() => props.lines.filter(line => line.type === 'added').length)
const deletions = computed(() => props.lines.filter(line => line.type === 'removed').length)
const canCopy = computed(() => Boolean(props.copyText || props.onCopy))
const t = (key: keyof typeof fileDiffMessages) => props.labels?.[key] ?? fileDiffMessages[key]
const format = (value: number) => new Intl.NumberFormat('en').format(value)
watch(() => props.status, (next, previous) => {
if (previous !== 'streaming' && next === 'streaming') setOpen(true)
if (previous === 'streaming' && next === 'complete' && props.collapseOnComplete) setOpen(false)
})
let frame = 0
watch(() => [props.lines, props.lines.length, props.status, open.value] as const, () => {
cancelAnimationFrame(frame)
if (!open.value || !streaming.value) return
frame = requestAnimationFrame(() => {
const el = viewport.value
if (!el || el.scrollHeight <= el.clientHeight) return
el.scrollTo({ top: el.scrollHeight, behavior: reduce.value ? 'auto' : 'smooth' })
})
}, { flush: 'post' })
onBeforeUnmount(() => cancelAnimationFrame(frame))
</script>
<template>
<div class="be-ag be-fd" :data-state="status" :aria-busy="streaming">
<button :id="`${uid}-trigger`" type="button" class="be-fd-trigger" :aria-expanded="open" :aria-controls="`${uid}-content`" @click="setOpen(!open)">
<AgentIcon name="FileCode2" :size="16" class="be-fd-file" />
<span class="be-fd-name"><slot name="file">{{ file }}</slot></span>
<span class="be-fd-counts">
<span v-if="additions" class="be-fd-count" data-type="added">+{{ format(additions) }}</span>
<span v-if="deletions" class="be-fd-count" data-type="removed">−{{ format(deletions) }}</span>
</span>
<span class="be-fd-state"><span v-if="streaming" role="img" :aria-label="t('applying')" class="be-fd-icon"><AgentIcon name="LoaderCircle" :size="14" :class="{ 'be-ag-spin': !reduce }" /></span><span v-else role="img" :aria-label="t('applied')" class="be-fd-icon"><AgentIcon name="Check" :size="14" /></span></span>
<motion.span aria-hidden="true" class="be-fd-chevron" :animate="{ rotate: open ? 180 : 0 }" :transition="reduce ? { duration: 0 } : swapSpring"><AgentIcon name="ChevronDown" :size="14" /></motion.span>
</button>
<AgentReveal :id="`${uid}-content`" role="region" :aria-labelledby="`${uid}-trigger`" :open="open">
<div class="be-fd-indent">
<div class="be-fd-card">
<div ref="viewport" data-slot="file-diff-viewport" aria-live="polite" class="be-fd-viewport be-ag-hide-scroll" :style="{ maxHeight: `${maxHeight}px` }">
<div class="be-fd-lines">
<span class="be-ag-sr">{{ t('changes') }}</span>
<div v-for="line in lines" :key="line.id" class="be-fd-line" :data-type="line.type ?? 'context'">
<span class="be-fd-num">{{ line.oldLine }}</span>
<span class="be-fd-num">{{ line.newLine }}</span>
<span class="be-fd-sign">{{ line.type === 'added' ? '+' : line.type === 'removed' ? '−' : '' }}</span>
<AgentCodeLine :code="line.content" :language="language" class="be-fd-code" />
</div>
</div>
</div>
<div v-if="canCopy" class="be-fd-foot"><motion.button type="button" class="be-fd-copy" :aria-label="copied ? t('copied') : t('copy')" :title="copied ? t('copied') : t('copy')" :while-press="reduce ? undefined : { scale: 0.9 }" :transition="pressSpring" @click="copy(copyText ?? '', onCopy)"><AgentIcon :name="copied ? 'Check' : 'Copy'" :size="14" /></motion.button></div>
</div>
</div>
</AgentReveal>
</div>
</template>
<style scoped>
.be-fd { box-sizing: border-box; width: 100%; font-size: 0.875rem; line-height: 1.25rem; color: var(--beui-foreground); }
.be-fd-trigger { display: flex; box-sizing: border-box; min-height: 2.25rem; width: 100%; align-items: center; gap: 0.5rem; padding: 0.25rem 0; border: 0; border-radius: 0.375rem; background: transparent; color: inherit; font: inherit; text-align: left; cursor: pointer; outline: none; }
.be-fd-trigger:focus-visible { box-shadow: 0 0 0 2px var(--beui-background), 0 0 0 4px var(--beui-ring); }
.be-fd-file { color: var(--beui-muted-foreground); }
.be-fd-name { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--font-mono, ui-monospace, monospace); font-size: 0.75rem; line-height: 1rem; color: color-mix(in srgb, var(--beui-foreground) 80%, transparent); }
.be-fd-counts { display: flex; flex: none; align-items: center; gap: 0.5rem; }
.be-fd-count { font-family: var(--font-mono, ui-monospace, monospace); font-size: 0.75rem; line-height: 1rem; font-variant-numeric: tabular-nums; }
.be-fd-count[data-type=added] { color: var(--ag-green); }
.be-fd-count[data-type=removed] { color: var(--ag-rose); }
.be-fd-state { display: grid; flex: none; width: 1rem; height: 1rem; place-items: center; color: color-mix(in srgb, var(--beui-muted-foreground) 60%, transparent); }
.be-fd-icon { display: grid; place-items: center; }
.be-fd-chevron { display: inline-flex; flex: none; color: color-mix(in srgb, var(--beui-muted-foreground) 45%, transparent); transition: color 0.15s; }
.be-fd-trigger:hover .be-fd-chevron { color: var(--beui-muted-foreground); }
.be-fd-indent { padding: 0.375rem 0 0 1.5rem; }
.be-fd-card { overflow: hidden; border-radius: 0.75rem; background: color-mix(in srgb, var(--beui-muted) 80%, transparent); }
.be-fd-viewport { overflow: auto; }
.be-fd-lines { font-family: var(--font-mono, ui-monospace, monospace); font-size: 0.75rem; line-height: 1.25rem; }
.be-fd-line { display: grid; grid-template-columns: 2.25rem 2.25rem 1rem minmax(0, 1fr); }
.be-fd-line[data-type=added] { background: oklch(69.6% 0.17 162.48 / 0.07); }
.be-fd-line[data-type=removed] { background: oklch(64.5% 0.246 16.439 / 0.07); }
.be-fd-num { user-select: none; padding-right: 0.5rem; text-align: right; font-variant-numeric: tabular-nums; color: color-mix(in srgb, var(--beui-muted-foreground) 40%, transparent); }
.be-fd-sign { user-select: none; text-align: center; color: color-mix(in srgb, var(--beui-muted-foreground) 45%, transparent); }
.be-fd-line[data-type=added] .be-fd-sign { color: var(--ag-green); }
.be-fd-line[data-type=removed] .be-fd-sign { color: var(--ag-rose); }
.be-fd-code { min-width: 0; white-space: pre; padding-inline: 0.375rem; }
.be-fd-foot { display: flex; justify-content: flex-end; padding: 0.25rem 0.5rem 0.375rem; }
.be-fd-copy { display: grid; width: 1.75rem; height: 1.75rem; place-items: center; padding: 0; border: 0; border-radius: 0.375rem; background: transparent; color: var(--beui-muted-foreground); cursor: pointer; outline: none; transition: color 0.15s, background-color 0.15s; }
.be-fd-copy:hover { background: color-mix(in srgb, var(--beui-background) 70%, transparent); color: var(--beui-foreground); }
.be-fd-copy:focus-visible { box-shadow: 0 0 0 2px var(--beui-ring); }
</style>Install
Add it with your package manager, or copy the source manually.
Run it in a project set up with shadcn-vue. The source is copied to components/beui and its dependencies are added to your package.json. Read the shadcn-vue guide
$ bunx --bun shadcn-vue@latest add @beui-vue/file-diff$ bun add beui-vueDownload the release archive to your project root, then install the local file.
$ bun add ./beui-vue-0.1.0.tgzImport the shared styles once in your application.
import FileDiff from '@/components/beui/agents/FileDiff.vue'
import '@/components/beui/styles.css'API Reference
filestring—linesFileDiffLine[]—status?"streaming" | "complete"streamingv-model:open?booleanundefineddefaultOpen?booleantruecollapseOnComplete?booleantruemaxHeight?number220language?stringtypescriptcopyText?string—onCopy?() => void | Promise<void>—labels?LabelsOverride any built-in string.
—class?string—@open-change?(value: boolean) => void—Composition
Place the patch inside the tool output that produced the file change.
ToolResult
└── ToolResultOutput
└── FileDiffNote: Code Block provides the shared syntax presentation for source lines. Tool Result summarizes the command or edit that produced the patch. Agent Activity records the file edit among surrounding agent events.
How it works
A file diff explains a proposed or completed edit. It should preserve the relationship between old and new lines while making the scale and status of the change visible at a glance.
Related components
Code Block
A syntax-highlighted code surface with stable streaming updates, line numbers, focused lines, smooth following, and copy feedback.
Tool Result
A lightweight execution disclosure for syntax-highlighted terminal output and request responses that collapses into a compact completed state.
Message
Composable conversation primitives for message rows, grouped bubbles, avatars, metadata, live markers, and a mount-only trailing-edge pop-up for newly sent rows.
Updated