Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

json-tree.tsx

76 lines1.9 KB

1import { JsonTreeView } from '@ark-ui/solid'
2import ChevronRightIcon from 'lucide-solid/icons/chevron-right'
3import { Show, splitProps } from 'solid-js'
4import { ScrollArea } from './scroll-area'
5import { CopyButton } from './copy-button'
6import type { ComponentProps } from 'solid-js'
7import { cn } from '@/utils'
8​
9interface JsonTreeProps extends ComponentProps<'div'> {
10 /**
11 * The value to render in the JSON tree.
12 */
13 value: unknown
14 /**
15 * The default expansion depth that the JSON viewer should have.
16 */
17 defaultExpandedDepth?: number
18 /**
19 * Whether or not the JSON tree should be copyable. If true,
20 * adds a copy button on the top right of the tree.
21 *
22 * @default false
23 */
24 copyable?: boolean
25 /**
26 * Whether the property keys should have quotes or not.
27 *
28 * @default false
29 */
30 quotesOnKeys?: boolean
32​
33/**
34 * A scrollable JSON inspector with optional key quotes and copy support.
35 *
36 * @example
37 * ```tsx
38 * <JsonTree value={{ name: "Ada" }} defaultExpandedDepth={1} />
39 * ```
40 */
41export function JsonTree(props: JsonTreeProps) {
42 const [local, others] = splitProps(props, [
43 'value',
44 'class',
45 'quotesOnKeys',
46 'copyable',
47 'defaultExpandedDepth',
48 ])
49​
50 return (
51 <JsonTreeView.Root
52 defaultExpandedDepth={local.defaultExpandedDepth}
53 data={local.value}
54 quotesOnKeys={local.quotesOnKeys}
55 asChild={(innerProps) => (
56 <ScrollArea
57 class={cn(
58 'ark-json-root w-full rounded-lg font-mono text-sm/6',
59 local.class,
60 )}
61 {...others}
62 {...innerProps()}
63 />
64 )}
65 >
66 <Show when={props.copyable}>
67 <CopyButton value={local.value} class="absolute top-1 right-1 z-10" />
68 </Show>
69 <JsonTreeView.Tree
70 arrow={<ChevronRightIcon class="h-4 w-4 text-muted-foreground" />}
71 indentGuide
72 class="text-1/2 flex flex-col rounded-lg"
73 />
74 </JsonTreeView.Root>
75 )