@ark-ui/svelte 5.2.0 → 5.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/components/collection/index.d.ts +1 -0
  2. package/dist/components/collection/index.js +1 -0
  3. package/dist/components/collection/use-list-selection.svelte.d.ts +92 -0
  4. package/dist/components/collection/use-list-selection.svelte.js +80 -0
  5. package/dist/components/combobox/combobox-context.svelte.d.ts +10 -3
  6. package/dist/components/combobox/combobox-root-provider.svelte.d.ts +10 -3
  7. package/dist/components/combobox/combobox-root.svelte.d.ts +10 -3
  8. package/dist/components/factory/factory.svelte.d.ts +11 -4
  9. package/dist/components/factory/svg-factory.svelte.d.ts +11 -4
  10. package/dist/components/file-upload/file-upload-root.svelte +8 -1
  11. package/dist/components/file-upload/file-upload-root.svelte.d.ts +1 -1
  12. package/dist/components/index.d.ts +1 -0
  13. package/dist/components/index.js +1 -0
  14. package/dist/components/json-tree-view/get-branch-value.d.ts +3 -0
  15. package/dist/components/json-tree-view/get-branch-value.js +13 -0
  16. package/dist/components/json-tree-view/index.d.ts +5 -0
  17. package/dist/components/json-tree-view/index.js +5 -0
  18. package/dist/components/json-tree-view/json-tree-view-key-node.svelte +23 -0
  19. package/dist/components/json-tree-view/json-tree-view-key-node.svelte.d.ts +14 -0
  20. package/dist/components/json-tree-view/json-tree-view-node.svelte +102 -0
  21. package/dist/components/json-tree-view/json-tree-view-node.svelte.d.ts +24 -0
  22. package/dist/components/json-tree-view/json-tree-view-props-context.d.ts +8 -0
  23. package/dist/components/json-tree-view/json-tree-view-props-context.js +6 -0
  24. package/dist/components/json-tree-view/json-tree-view-root-provider.svelte +12 -0
  25. package/dist/components/json-tree-view/json-tree-view-root-provider.svelte.d.ts +7 -0
  26. package/dist/components/json-tree-view/json-tree-view-root.svelte +54 -0
  27. package/dist/components/json-tree-view/json-tree-view-root.svelte.d.ts +18 -0
  28. package/dist/components/json-tree-view/json-tree-view-tree.svelte +23 -0
  29. package/dist/components/json-tree-view/json-tree-view-tree.svelte.d.ts +9 -0
  30. package/dist/components/json-tree-view/json-tree-view-value-node.svelte +31 -0
  31. package/dist/components/json-tree-view/json-tree-view-value-node.svelte.d.ts +10 -0
  32. package/dist/components/json-tree-view/json-tree-view.d.ts +3 -0
  33. package/dist/components/json-tree-view/json-tree-view.js +3 -0
  34. package/dist/components/json-tree-view/use-json-tree-view.svelte.d.ts +10 -0
  35. package/dist/components/json-tree-view/use-json-tree-view.svelte.js +23 -0
  36. package/dist/components/listbox/listbox-context.svelte.d.ts +10 -3
  37. package/dist/components/listbox/listbox-root-provider.svelte.d.ts +10 -3
  38. package/dist/components/listbox/listbox-root.svelte.d.ts +10 -3
  39. package/dist/components/portal/portal.svelte +15 -27
  40. package/dist/components/portal/portal.svelte.d.ts +1 -1
  41. package/dist/components/select/select-context.svelte.d.ts +10 -3
  42. package/dist/components/select/select-item.svelte.d.ts +10 -3
  43. package/dist/components/select/select-root-provider.svelte.d.ts +10 -3
  44. package/dist/components/select/select-root.svelte.d.ts +10 -3
  45. package/dist/components/signature-pad/signature-pad-root.svelte +10 -3
  46. package/dist/components/signature-pad/signature-pad-root.svelte.d.ts +1 -1
  47. package/dist/components/tour/tour.anatomy.d.ts +1 -1
  48. package/dist/components/tree-view/tree-view-node-context.svelte.d.ts +10 -3
  49. package/dist/components/tree-view/tree-view-node-provider.svelte.d.ts +10 -3
  50. package/dist/components/tree-view/tree-view-root-provider.svelte.d.ts +10 -3
  51. package/dist/components/tree-view/tree-view-root.svelte.d.ts +10 -3
  52. package/dist/components/tree-view/tree-view-split-props.d.ts +1 -1
  53. package/package.json +65 -64
  54. package/dist/components/portal/portal-consumer.svelte +0 -9
  55. package/dist/components/portal/portal-consumer.svelte.d.ts +0 -7
@@ -2,3 +2,4 @@ export { createGridCollection, type GridCollection, type GridCollectionOptions }
2
2
  export { createListCollection, type CollectionItem, type CollectionOptions, type ListCollection, } from './list-collection';
3
3
  export { createFileTreeCollection, createTreeCollection, type FilePathTreeNode, type FlatTreeNode, type TreeCollection, type TreeCollectionOptions, type TreeNode, } from './tree-collection';
4
4
  export { useListCollection, type UseListCollectionProps, type UseListCollectionReturn, } from './use-list-collection.svelte';
5
+ export { useListSelection, type UseListSelectionProps, type UseListSelectionReturn } from './use-list-selection.svelte';
@@ -2,3 +2,4 @@ export { createGridCollection } from './grid-collection';
2
2
  export { createListCollection, } from './list-collection';
3
3
  export { createFileTreeCollection, createTreeCollection, } from './tree-collection';
4
4
  export { useListCollection, } from './use-list-collection.svelte';
5
+ export { useListSelection } from './use-list-selection.svelte';
@@ -0,0 +1,92 @@
1
+ import { type SelectionMode } from '@zag-js/collection';
2
+ import { type MaybeFunction } from '@zag-js/utils';
3
+ import type { CollectionItem, ListCollection } from './list-collection';
4
+ export interface UseListSelectionProps<T extends CollectionItem> {
5
+ /**
6
+ * The selection mode.
7
+ */
8
+ selectionMode?: SelectionMode;
9
+ /**
10
+ * Whether the selection is deselectable.
11
+ */
12
+ deselectable?: boolean;
13
+ /**
14
+ * The initial selected values.
15
+ */
16
+ initialSelectedValues?: string[];
17
+ /**
18
+ * Whether to reset the selection when the collection changes.
19
+ */
20
+ resetOnCollectionChange?: boolean;
21
+ /**
22
+ * The collection to use.
23
+ */
24
+ collection: ListCollection<T>;
25
+ }
26
+ export declare function useListSelection<T extends CollectionItem>(props: MaybeFunction<UseListSelectionProps<T>>): UseListSelectionReturn;
27
+ export interface UseListSelectionReturn {
28
+ /**
29
+ * The selected values as an array.
30
+ */
31
+ selectedValues: () => string[];
32
+ /**
33
+ * Whether the selection is empty.
34
+ */
35
+ isEmpty: () => boolean;
36
+ /**
37
+ * The first selected value.
38
+ */
39
+ firstSelectedValue: () => string | null;
40
+ /**
41
+ * The last selected value.
42
+ */
43
+ lastSelectedValue: () => string | null;
44
+ /**
45
+ * Check if a value is selected.
46
+ */
47
+ isSelected: (value: string | null) => boolean;
48
+ /**
49
+ * Check if a value can be selected.
50
+ */
51
+ canSelect: (value: string) => boolean;
52
+ /**
53
+ * Select a value.
54
+ */
55
+ select: (value: string, forceToggle?: boolean) => void;
56
+ /**
57
+ * Deselect a value.
58
+ */
59
+ deselect: (value: string) => void;
60
+ /**
61
+ * Toggle selection of a value.
62
+ */
63
+ toggle: (value: string) => void;
64
+ /**
65
+ * Replace the selection with a single value.
66
+ */
67
+ replace: (value: string | null) => void;
68
+ /**
69
+ * Extend the selection from anchor to target.
70
+ */
71
+ extend: (anchorValue: string, targetValue: string) => void;
72
+ /**
73
+ * Set the selected values.
74
+ */
75
+ setSelectedValues: (values: string[]) => void;
76
+ /**
77
+ * Clear the selection.
78
+ */
79
+ clear: () => void;
80
+ /**
81
+ * Clear all selections.
82
+ */
83
+ resetSelection: () => void;
84
+ /**
85
+ * Returns true if all items from the collection are selected.
86
+ */
87
+ isAllSelected: () => boolean;
88
+ /**
89
+ * Returns true if at least one item from the collection is selected.
90
+ */
91
+ isSomeSelected: () => boolean;
92
+ }
@@ -0,0 +1,80 @@
1
+ import { Selection } from '@zag-js/collection';
2
+ import { runIfFn } from '@zag-js/utils';
3
+ import { untrack } from 'svelte';
4
+ export function useListSelection(props) {
5
+ const localProps = $derived.by(() => {
6
+ const { collection, selectionMode = 'single', deselectable = true, initialSelectedValues = [], resetOnCollectionChange = false, } = runIfFn(props);
7
+ return {
8
+ collection,
9
+ selectionMode,
10
+ deselectable,
11
+ initialSelectedValues,
12
+ resetOnCollectionChange,
13
+ };
14
+ });
15
+ const createSelection = (values = []) => {
16
+ const selection = new Selection(values);
17
+ selection.selectionMode = localProps.selectionMode;
18
+ selection.deselectable = localProps.deselectable;
19
+ return selection;
20
+ };
21
+ const initialSelectedValues = untrack(() => localProps.initialSelectedValues);
22
+ let selection = $state(createSelection(initialSelectedValues));
23
+ // Watch for collection changes and reset selection if needed
24
+ $effect(() => {
25
+ const { collection, resetOnCollectionChange } = localProps;
26
+ // Trigger effect when collection values change
27
+ collection.getValues();
28
+ if (resetOnCollectionChange) {
29
+ selection = createSelection();
30
+ }
31
+ });
32
+ const selectedValues = $derived(Array.from(selection));
33
+ const isEmpty = $derived(selection.isEmpty());
34
+ const firstSelectedValue = $derived(selection.firstSelectedValue(localProps.collection));
35
+ const lastSelectedValue = $derived(selection.lastSelectedValue(localProps.collection));
36
+ return {
37
+ selectedValues: () => selectedValues,
38
+ isEmpty: () => isEmpty,
39
+ firstSelectedValue: () => firstSelectedValue,
40
+ lastSelectedValue: () => lastSelectedValue,
41
+ isSelected: (value) => {
42
+ return selection.isSelected(value);
43
+ },
44
+ isAllSelected: () => {
45
+ const allValues = localProps.collection.getValues();
46
+ return allValues.length > 0 && allValues.every((value) => selection.isSelected(value));
47
+ },
48
+ isSomeSelected: () => {
49
+ const allValues = localProps.collection.getValues();
50
+ return allValues.some((value) => selection.isSelected(value));
51
+ },
52
+ canSelect: (value) => {
53
+ return selection.canSelect(localProps.collection, value);
54
+ },
55
+ select: (value, forceToggle) => {
56
+ selection = selection.select(localProps.collection, value, forceToggle);
57
+ },
58
+ deselect: (value) => {
59
+ selection = selection.deselect(value);
60
+ },
61
+ toggle: (value) => {
62
+ selection = selection.toggleSelection(localProps.collection, value);
63
+ },
64
+ replace: (value) => {
65
+ selection = selection.replaceSelection(localProps.collection, value);
66
+ },
67
+ extend: (anchorValue, targetValue) => {
68
+ selection = selection.extendSelection(localProps.collection, anchorValue, targetValue);
69
+ },
70
+ setSelectedValues: (values) => {
71
+ selection = selection.setSelection(values);
72
+ },
73
+ clear: () => {
74
+ selection = selection.clearSelection();
75
+ },
76
+ resetSelection: () => {
77
+ selection = createSelection();
78
+ },
79
+ };
80
+ }
@@ -4,10 +4,17 @@ import type { UseComboboxContext } from './use-combobox-context';
4
4
  export interface ComboboxContextProps<T extends CollectionItem> {
5
5
  render: Snippet<[UseComboboxContext<T>]>;
6
6
  }
7
+ declare function $$render<T extends CollectionItem>(): {
8
+ props: ComboboxContextProps<T>;
9
+ exports: {};
10
+ bindings: "";
11
+ slots: {};
12
+ events: {};
13
+ };
7
14
  declare class __sveltets_Render<T extends CollectionItem> {
8
- props(): ComboboxContextProps<T>;
9
- events(): {};
10
- slots(): {};
15
+ props(): ReturnType<typeof $$render<T>>['props'];
16
+ events(): ReturnType<typeof $$render<T>>['events'];
17
+ slots(): ReturnType<typeof $$render<T>>['slots'];
11
18
  bindings(): "";
12
19
  exports(): {};
13
20
  }
@@ -11,10 +11,17 @@ export interface ComboboxRootProviderBaseProps<T extends CollectionItem> extends
11
11
  }
12
12
  export interface ComboboxRootProviderProps<T extends CollectionItem> extends Assign<HTMLProps<'div'>, ComboboxRootProviderBaseProps<T>> {
13
13
  }
14
+ declare function $$render<T extends CollectionItem>(): {
15
+ props: ComboboxRootProviderProps<T>;
16
+ exports: {};
17
+ bindings: "ref";
18
+ slots: {};
19
+ events: {};
20
+ };
14
21
  declare class __sveltets_Render<T extends CollectionItem> {
15
- props(): ComboboxRootProviderProps<T>;
16
- events(): {};
17
- slots(): {};
22
+ props(): ReturnType<typeof $$render<T>>['props'];
23
+ events(): ReturnType<typeof $$render<T>>['events'];
24
+ slots(): ReturnType<typeof $$render<T>>['slots'];
18
25
  bindings(): "ref";
19
26
  exports(): {};
20
27
  }
@@ -6,10 +6,17 @@ export interface ComboboxRootBaseProps<T extends CollectionItem> extends UseComb
6
6
  }
7
7
  export interface ComboboxRootProps<T extends CollectionItem> extends Assign<HTMLProps<'div'>, ComboboxRootBaseProps<T>> {
8
8
  }
9
+ declare function $$render<T extends CollectionItem>(): {
10
+ props: ComboboxRootProps<T>;
11
+ exports: {};
12
+ bindings: "ref" | "value" | "open" | "inputValue";
13
+ slots: {};
14
+ events: {};
15
+ };
9
16
  declare class __sveltets_Render<T extends CollectionItem> {
10
- props(): ComboboxRootProps<T>;
11
- events(): {};
12
- slots(): {};
17
+ props(): ReturnType<typeof $$render<T>>['props'];
18
+ events(): ReturnType<typeof $$render<T>>['events'];
19
+ slots(): ReturnType<typeof $$render<T>>['slots'];
13
20
  bindings(): "ref" | "value" | "open" | "inputValue";
14
21
  exports(): {};
15
22
  }
@@ -1,7 +1,7 @@
1
1
  import type { HTMLProps, PolymorphicProps } from '../../types';
2
2
  import type { SvelteHTMLElements } from 'svelte/elements';
3
- declare class __sveltets_Render<T extends keyof SvelteHTMLElements> {
4
- props(): HTMLProps<T> & PolymorphicProps<T> & {
3
+ declare function $$render<T extends keyof SvelteHTMLElements>(): {
4
+ props: HTMLProps<T> & PolymorphicProps<T> & {
5
5
  /**
6
6
  * The HTML tag of the component.
7
7
  */
@@ -11,8 +11,15 @@ declare class __sveltets_Render<T extends keyof SvelteHTMLElements> {
11
11
  */
12
12
  ref?: Element | null;
13
13
  };
14
- events(): {};
15
- slots(): {};
14
+ exports: {};
15
+ bindings: "ref";
16
+ slots: {};
17
+ events: {};
18
+ };
19
+ declare class __sveltets_Render<T extends keyof SvelteHTMLElements> {
20
+ props(): ReturnType<typeof $$render<T>>['props'];
21
+ events(): ReturnType<typeof $$render<T>>['events'];
22
+ slots(): ReturnType<typeof $$render<T>>['slots'];
16
23
  bindings(): "ref";
17
24
  exports(): {};
18
25
  }
@@ -1,12 +1,19 @@
1
1
  import type { HTMLProps, PolymorphicProps } from '../../types';
2
2
  import type { SvelteHTMLElements } from 'svelte/elements';
3
- declare class __sveltets_Render<T extends keyof SvelteHTMLElements> {
4
- props(): HTMLProps<T> & PolymorphicProps<T> & {
3
+ declare function $$render<T extends keyof SvelteHTMLElements>(): {
4
+ props: HTMLProps<T> & PolymorphicProps<T> & {
5
5
  as: T;
6
6
  ref?: Element | null;
7
7
  };
8
- events(): {};
9
- slots(): {};
8
+ exports: {};
9
+ bindings: "ref";
10
+ slots: {};
11
+ events: {};
12
+ };
13
+ declare class __sveltets_Render<T extends keyof SvelteHTMLElements> {
14
+ props(): ReturnType<typeof $$render<T>>['props'];
15
+ events(): ReturnType<typeof $$render<T>>['events'];
16
+ slots(): ReturnType<typeof $$render<T>>['slots'];
10
17
  bindings(): "ref";
11
18
  exports(): {};
12
19
  }
@@ -13,14 +13,16 @@
13
13
  import { FileUploadProvider } from './use-file-upload-context'
14
14
  import { useFileUpload } from './use-file-upload.svelte'
15
15
 
16
- let { ref = $bindable(null), ...props }: FileUploadRootProps = $props()
16
+ let { ref = $bindable(null), acceptedFiles = $bindable(), ...props }: FileUploadRootProps = $props()
17
17
  const providedId = $props.id()
18
18
 
19
19
  const [useFileUploadProps, localProps] = $derived(
20
20
  createSplitProps<UseFileUploadProps>()(props, [
21
21
  'accept',
22
+ 'acceptedFiles',
22
23
  'allowDrop',
23
24
  'capture',
25
+ 'defaultAcceptedFiles',
24
26
  'directory',
25
27
  'disabled',
26
28
  'id',
@@ -45,6 +47,11 @@
45
47
  const resolvedProps = $derived<UseFileUploadProps>({
46
48
  ...useFileUploadProps,
47
49
  id: useFileUploadProps.id ?? providedId,
50
+ acceptedFiles,
51
+ onFileChange: (details) => {
52
+ useFileUploadProps.onFileChange?.(details)
53
+ if (acceptedFiles !== undefined) acceptedFiles = details.acceptedFiles
54
+ },
48
55
  })
49
56
 
50
57
  const fileUpload = useFileUpload(() => resolvedProps)
@@ -4,6 +4,6 @@ export interface FileUploadRootBaseProps extends UseFileUploadProps, Polymorphic
4
4
  }
5
5
  export interface FileUploadRootProps extends Assign<HTMLProps<'div'>, FileUploadRootBaseProps> {
6
6
  }
7
- declare const FileUploadRoot: import("svelte").Component<FileUploadRootProps, {}, "ref">;
7
+ declare const FileUploadRoot: import("svelte").Component<FileUploadRootProps, {}, "ref" | "acceptedFiles">;
8
8
  type FileUploadRoot = ReturnType<typeof FileUploadRoot>;
9
9
  export default FileUploadRoot;
@@ -23,6 +23,7 @@ export * from './format';
23
23
  export * from './frame';
24
24
  export * from './highlight';
25
25
  export * from './hover-card';
26
+ export * from './json-tree-view';
26
27
  export * from './listbox';
27
28
  export * from './menu';
28
29
  export * from './number-input';
@@ -23,6 +23,7 @@ export * from './format';
23
23
  export * from './frame';
24
24
  export * from './highlight';
25
25
  export * from './hover-card';
26
+ export * from './json-tree-view';
26
27
  export * from './listbox';
27
28
  export * from './menu';
28
29
  export * from './number-input';
@@ -0,0 +1,3 @@
1
+ import type { TreeCollection } from '@zag-js/collection';
2
+ import type { JsonNode } from '@zag-js/json-tree-utils';
3
+ export declare function getBranchValues(tree: TreeCollection<JsonNode>, depth: number): string[];
@@ -0,0 +1,13 @@
1
+ export function getBranchValues(tree, depth) {
2
+ let values = [];
3
+ tree.visit({
4
+ onEnter: (node, indexPath) => {
5
+ if (indexPath.length === 0)
6
+ return;
7
+ if (tree.isBranchNode(node) && indexPath.length <= depth) {
8
+ values.push(tree.getNodeValue(node));
9
+ }
10
+ },
11
+ });
12
+ return values;
13
+ }
@@ -0,0 +1,5 @@
1
+ export { default as JsonTreeViewRoot, type JsonTreeViewRootBaseProps, type JsonTreeViewRootProps, } from './json-tree-view-root.svelte';
2
+ export { default as JsonTreeViewRootProvider, type JsonTreeViewRootProviderProps, } from './json-tree-view-root-provider.svelte';
3
+ export { default as JsonTreeViewTree, type JsonTreeViewTreeBaseProps, type JsonTreeViewTreeProps, } from './json-tree-view-tree.svelte';
4
+ export { useJsonTreeView, type UseJsonTreeViewProps, type UseJsonTreeViewReturn } from './use-json-tree-view.svelte';
5
+ export * as JsonTreeView from './json-tree-view';
@@ -0,0 +1,5 @@
1
+ export { default as JsonTreeViewRoot, } from './json-tree-view-root.svelte';
2
+ export { default as JsonTreeViewRootProvider, } from './json-tree-view-root-provider.svelte';
3
+ export { default as JsonTreeViewTree, } from './json-tree-view-tree.svelte';
4
+ export { useJsonTreeView } from './use-json-tree-view.svelte';
5
+ export * as JsonTreeView from './json-tree-view';
@@ -0,0 +1,23 @@
1
+ <script lang="ts">
2
+ import { type JsonNode, keyPathToKey } from '@zag-js/json-tree-utils'
3
+
4
+ interface JsonTreeViewKeyNodeProps {
5
+ /**
6
+ * The node to render.
7
+ */
8
+ node: JsonNode
9
+ /**
10
+ * Whether to show quotes on the key.
11
+ */
12
+ showQuotes?: boolean
13
+ }
14
+
15
+ let { node, showQuotes }: JsonTreeViewKeyNodeProps = $props()
16
+
17
+ const key = $derived(keyPathToKey(node.keyPath))
18
+ </script>
19
+
20
+ <span data-kind="key" data-non-enumerable={node.isNonEnumerable ? '' : undefined}>
21
+ {showQuotes ? `"${key}"` : key}
22
+ </span>
23
+ <span data-kind="colon">:</span>
@@ -0,0 +1,14 @@
1
+ import { type JsonNode } from '@zag-js/json-tree-utils';
2
+ interface JsonTreeViewKeyNodeProps {
3
+ /**
4
+ * The node to render.
5
+ */
6
+ node: JsonNode;
7
+ /**
8
+ * Whether to show quotes on the key.
9
+ */
10
+ showQuotes?: boolean;
11
+ }
12
+ declare const JsonTreeViewKeyNode: import("svelte").Component<JsonTreeViewKeyNodeProps, {}, "">;
13
+ type JsonTreeViewKeyNode = ReturnType<typeof JsonTreeViewKeyNode>;
14
+ export default JsonTreeViewKeyNode;
@@ -0,0 +1,102 @@
1
+ <script lang="ts">
2
+ import {
3
+ type JsonNode,
4
+ type JsonNodeHastElement,
5
+ getAccessibleDescription,
6
+ jsonNodeToElement,
7
+ keyPathToKey,
8
+ } from '@zag-js/json-tree-utils'
9
+ import type { Snippet } from 'svelte'
10
+ import { TreeView, useTreeViewContext } from '../tree-view'
11
+ import JsonTreeViewKeyNode from './json-tree-view-key-node.svelte'
12
+ import JsonTreeViewNode from './json-tree-view-node.svelte'
13
+ import { useJsonTreeViewPropsContext } from './json-tree-view-props-context'
14
+ import JsonTreeViewValueNode from './json-tree-view-value-node.svelte'
15
+
16
+ export interface JsonTreeViewNodeBaseProps {
17
+ /**
18
+ * The icon to use for the arrow.
19
+ */
20
+ arrow?: Snippet<[]>
21
+ /**
22
+ * The indent guide to use for the tree.
23
+ */
24
+ indentGuide?: boolean | Snippet<[]>
25
+ /**
26
+ * The function to render the value of the node.
27
+ */
28
+ renderValue?: Snippet<[JsonNodeHastElement]>
29
+ }
30
+
31
+ export interface JsonTreeViewNodeProps extends JsonTreeViewNodeBaseProps {
32
+ node: JsonNode
33
+ indexPath: number[]
34
+ }
35
+
36
+ const props: JsonTreeViewNodeProps = $props()
37
+
38
+ const { node, indexPath, arrow, indentGuide, renderValue } = props
39
+
40
+ const options = useJsonTreeViewPropsContext()
41
+
42
+ const tree = useTreeViewContext()
43
+ const nodeState = $derived(tree().getNodeState({ node, indexPath }))
44
+
45
+ const key = $derived(keyPathToKey(node.keyPath, { excludeRoot: true }))
46
+ const valueNode = $derived(jsonNodeToElement(node, options()))
47
+
48
+ const nodeProps = $derived.by(() => {
49
+ const desc = getAccessibleDescription(node)
50
+ const line = indexPath.reduce((acc, curr) => acc + curr, 1)
51
+ const lineLength = indexPath.length - 1
52
+
53
+ return {
54
+ 'aria-label': desc,
55
+ 'data-line': line,
56
+ style: `--line-length: ${lineLength}`,
57
+ }
58
+ })
59
+
60
+ const scopeProps = {
61
+ 'data-scope': 'json-tree-view',
62
+ }
63
+ </script>
64
+
65
+ <TreeView.NodeProvider {node} {indexPath}>
66
+ {#if nodeState.isBranch}
67
+ <TreeView.Branch {...scopeProps}>
68
+ <TreeView.BranchControl {...nodeProps} {...scopeProps}>
69
+ {#if arrow}
70
+ <TreeView.BranchIndicator {...scopeProps}>
71
+ {@render arrow()}
72
+ </TreeView.BranchIndicator>
73
+ {/if}
74
+ <TreeView.BranchText {...scopeProps}>
75
+ {#if key}
76
+ <JsonTreeViewKeyNode {node} showQuotes={options().quotesOnKeys} />
77
+ {/if}
78
+ <JsonTreeViewValueNode node={valueNode} {renderValue} />
79
+ </TreeView.BranchText>
80
+ </TreeView.BranchControl>
81
+ <TreeView.BranchContent {...scopeProps}>
82
+ {#if typeof indentGuide === 'boolean'}
83
+ <TreeView.BranchIndentGuide />
84
+ {:else if indentGuide}
85
+ {@render indentGuide()}
86
+ {/if}
87
+ {#each node.children ?? [] as child, index (index)}
88
+ <JsonTreeViewNode {...props} node={child} indexPath={[...indexPath, index]} />
89
+ {/each}
90
+ </TreeView.BranchContent>
91
+ </TreeView.Branch>
92
+ {:else}
93
+ <TreeView.Item {...nodeProps} {...scopeProps}>
94
+ <TreeView.ItemText {...scopeProps}>
95
+ {#if key}
96
+ <JsonTreeViewKeyNode {node} showQuotes={options().quotesOnKeys} />
97
+ {/if}
98
+ <JsonTreeViewValueNode node={valueNode} {renderValue} />
99
+ </TreeView.ItemText>
100
+ </TreeView.Item>
101
+ {/if}
102
+ </TreeView.NodeProvider>
@@ -0,0 +1,24 @@
1
+ import { type JsonNode, type JsonNodeHastElement } from '@zag-js/json-tree-utils';
2
+ import type { Snippet } from 'svelte';
3
+ import JsonTreeViewNode from './json-tree-view-node.svelte';
4
+ export interface JsonTreeViewNodeBaseProps {
5
+ /**
6
+ * The icon to use for the arrow.
7
+ */
8
+ arrow?: Snippet<[]>;
9
+ /**
10
+ * The indent guide to use for the tree.
11
+ */
12
+ indentGuide?: boolean | Snippet<[]>;
13
+ /**
14
+ * The function to render the value of the node.
15
+ */
16
+ renderValue?: Snippet<[JsonNodeHastElement]>;
17
+ }
18
+ export interface JsonTreeViewNodeProps extends JsonTreeViewNodeBaseProps {
19
+ node: JsonNode;
20
+ indexPath: number[];
21
+ }
22
+ declare const JsonTreeViewNode: import("svelte").Component<JsonTreeViewNodeProps, {}, "">;
23
+ type JsonTreeViewNode = ReturnType<typeof JsonTreeViewNode>;
24
+ export default JsonTreeViewNode;
@@ -0,0 +1,8 @@
1
+ import type { JsonNodePreviewOptions } from '@zag-js/json-tree-utils';
2
+ export interface JsonTreeViewOptions extends Partial<JsonNodePreviewOptions> {
3
+ /**
4
+ * Whether to show quotes on the keys.
5
+ */
6
+ quotesOnKeys?: boolean;
7
+ }
8
+ export declare const JsonTreeViewPropsProvider: (opts: () => JsonTreeViewOptions) => void, useJsonTreeViewPropsContext: (fallback?: (() => JsonTreeViewOptions) | undefined) => () => JsonTreeViewOptions;
@@ -0,0 +1,6 @@
1
+ import { createContext } from '../../utils/create-context';
2
+ export const [JsonTreeViewPropsProvider, useJsonTreeViewPropsContext] = createContext({
3
+ name: 'JsonTreeViewPropsContext',
4
+ hookName: 'useJsonTreeViewPropsContext',
5
+ providerName: '<JsonTreeViewPropsProvider />',
6
+ });
@@ -0,0 +1,12 @@
1
+ <script lang="ts">
2
+ import type { JsonNode } from '@zag-js/json-tree-utils'
3
+ import { TreeViewRootProvider, type TreeViewRootProviderProps } from '../tree-view'
4
+
5
+ export interface JsonTreeViewRootProviderProps extends TreeViewRootProviderProps<JsonNode> {}
6
+
7
+ const props: JsonTreeViewRootProviderProps = $props()
8
+ </script>
9
+
10
+ <TreeViewRootProvider data-scope="json-tree-view" {...props}>
11
+ {@render props.children?.()}
12
+ </TreeViewRootProvider>
@@ -0,0 +1,7 @@
1
+ import type { JsonNode } from '@zag-js/json-tree-utils';
2
+ import { type TreeViewRootProviderProps } from '../tree-view';
3
+ export interface JsonTreeViewRootProviderProps extends TreeViewRootProviderProps<JsonNode> {
4
+ }
5
+ declare const JsonTreeViewRootProvider: import("svelte").Component<JsonTreeViewRootProviderProps, {}, "">;
6
+ type JsonTreeViewRootProvider = ReturnType<typeof JsonTreeViewRootProvider>;
7
+ export default JsonTreeViewRootProvider;
@@ -0,0 +1,54 @@
1
+ <script lang="ts">
2
+ import { type JsonNode, getRootNode, nodeToString, nodeToValue } from '@zag-js/json-tree-utils'
3
+ import { createSplitProps } from '../../utils/create-split-props'
4
+ import { TreeView, createTreeCollection, type TreeViewRootProps } from '../tree-view'
5
+ import { getBranchValues } from './get-branch-value'
6
+ import { type JsonTreeViewOptions, JsonTreeViewPropsProvider } from './json-tree-view-props-context'
7
+
8
+ export interface JsonTreeViewRootBaseProps extends JsonTreeViewOptions {
9
+ /**
10
+ * The data to display in the tree.
11
+ */
12
+ data: unknown
13
+ /**
14
+ * The default expand level.
15
+ */
16
+ defaultExpandedDepth?: number
17
+ }
18
+
19
+ export interface JsonTreeViewRootProps
20
+ extends Omit<TreeViewRootProps<JsonNode>, 'collection'>,
21
+ JsonTreeViewRootBaseProps {}
22
+
23
+ const { data, defaultExpandedDepth, ...props }: JsonTreeViewRootProps = $props()
24
+
25
+ const splitJsonTreeViewProps = createSplitProps<JsonTreeViewOptions>()
26
+
27
+ const [jsonTreeProps, localProps] = $derived(
28
+ splitJsonTreeViewProps(props, [
29
+ 'maxPreviewItems',
30
+ 'collapseStringsAfterLength',
31
+ 'quotesOnKeys',
32
+ 'groupArraysAfterLength',
33
+ 'showNonenumerable',
34
+ ]),
35
+ )
36
+
37
+ const collection = $derived(
38
+ createTreeCollection<JsonNode>({
39
+ nodeToValue,
40
+ nodeToString,
41
+ rootNode: getRootNode(data),
42
+ }),
43
+ )
44
+
45
+ const defaultExpandedValue = $derived(
46
+ defaultExpandedDepth != null ? getBranchValues(collection, defaultExpandedDepth) : undefined,
47
+ )
48
+
49
+ JsonTreeViewPropsProvider(() => jsonTreeProps)
50
+ </script>
51
+
52
+ <TreeView.Root data-scope="json-tree-view" {collection} {defaultExpandedValue} {...localProps}>
53
+ {@render props.children?.()}
54
+ </TreeView.Root>