Skip to content

Commit 434c3b4

Browse files
committed
docs: svelte usage example
1 parent 96112c3 commit 434c3b4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

docs/svelte.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# How to render in svelte
2+
3+
Here is an example of a reusable component for svelte 5:
4+
5+
```svelte
6+
<script lang="ts">
7+
import { create } from 'jsondiffpatch';
8+
import { format as formatHtml } from 'jsondiffpatch/formatters/html';
9+
import 'jsondiffpatch/formatters/styles/html.css';
10+
11+
let {
12+
left,
13+
right,
14+
diffOptions = undefined,
15+
hideUnchangedValues = false,
16+
}: {
17+
left: unknown;
18+
right: unknown;
19+
diffOptions?: Parameters<typeof create>[0] | undefined;
20+
hideUnchangedValues?: boolean;
21+
} = $props();
22+
23+
let htmlDiff = $derived.by(() => {
24+
const jdp = create(diffOptions ?? {});
25+
const delta = jdp.diff(left, right);
26+
return formatHtml(delta, left);
27+
});
28+
</script>
29+
30+
<div class={`json-diff-container ${hideUnchangedValues ? 'jsondiffpatch-unchanged-hidden' : ''}`}>
31+
<div class="jsondiffpatch-result" aria-label="Visual JSON diff">
32+
{@html htmlDiff}
33+
</div>
34+
</div>
35+
```

0 commit comments

Comments
 (0)