Skip to content

Commit 73e253d

Browse files
committed
docs: svelte usage example
1 parent 96112c3 commit 73e253d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

docs/svelte.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
showAnnotated?: boolean;
22+
} = $props();
23+
24+
let htmlDiff = $derived.by(() => {
25+
const jdp = create(diffOptions ?? {});
26+
const delta = jdp.diff(left, right);
27+
return formatHtml(delta, left);
28+
});
29+
</script>
30+
31+
<div class={`json-diff-container ${hideUnchangedValues ? 'jsondiffpatch-unchanged-hidden' : ''}`}>
32+
<div class="jsondiffpatch-result" aria-label="Visual JSON diff">
33+
{@html htmlDiff}
34+
</div>
35+
</div>
36+
```

0 commit comments

Comments
 (0)