-
-
Notifications
You must be signed in to change notification settings - Fork 358
Feature/add tree view #3585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dev-viinz
wants to merge
29
commits into
skeletonlabs:main
Choose a base branch
from
dev-viinz:feature/add-tree-view
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Feature/add tree view #3585
Changes from 13 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
03167cc
feat: add @zag-js/tree-view to catalog
dev-viinz 8aa1c45
feat: implemented pretty much the example from zag.js
dev-viinz 1563008
fix: types
dev-viinz db170ea
chore: update zag...
dev-viinz 4bd9f47
feat: working prototype
dev-viinz ed9ccb0
feat: re-order things for draft
dev-viinz b97c340
Merge branch 'main' into feature/add-tree-view
dev-viinz 2fa4e9f
feat: consolidate treeview into single component + improve playground
dev-viinz 5650a38
chore: cleanup types + lint
dev-viinz e910470
fix: mock for resize observer to keep zagjs from throwing during testing
dev-viinz 39ae970
fix: remove unused context
dev-viinz 6850646
test: add first tests to svelte treeview
dev-viinz dab9c6e
test: add prop tests
dev-viinz ec4cd52
chore: move resizeobserver mock to vitest config
dev-viinz 02e116e
fix: remove style tag and make custom utility
dev-viinz e81837b
chore: move chevron placeholder from script to template
dev-viinz 01b0aec
chore: apply suggestions to props
dev-viinz 23e237d
fix: add button type, to prevent form submission
dev-viinz f2b5076
fix: forgot to adjust test after renaming props
dev-viinz 4956014
chore: update zag
dev-viinz 4c62218
feat: very WIP implementation of template-driven TreeView
dev-viinz 4424d47
feat: first working template driven approach
dev-viinz 16966fe
chore: cleanup debugging mess
dev-viinz 1fd80e1
fix: refactor tests into the new structure
dev-viinz 7fc0a66
chore: lint
dev-viinz 62ab259
chore: multiple support
dev-viinz 0a42145
chore: improve example
dev-viinz 77487e7
feat: add prop for styling selected item
dev-viinz 5c19677
fix: decided against additional prop
dev-viinz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 13 additions & 1 deletion
14
packages/skeleton-react/src/components/Segment/Segment.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
packages/skeleton-svelte/src/components/TreeView/TreeView.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
<script lang="ts" module> | ||
import { createRawSnippet } from 'svelte'; | ||
|
||
const chevron = createRawSnippet(() => { | ||
dev-viinz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { | ||
render: () => /* html */ ` | ||
<svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"> | ||
<path d="m9 18 6-6-6-6" /> | ||
</svg> | ||
` | ||
}; | ||
}); | ||
</script> | ||
|
||
<script lang="ts"> | ||
import * as tree from '@zag-js/tree-view'; | ||
import { useMachine, normalizeProps } from '@zag-js/svelte'; | ||
import type { CollectionNode, TreeViewProps } from './types.js'; | ||
import { slide } from 'svelte/transition'; | ||
|
||
const { | ||
// Animation | ||
animationConfig, | ||
// Data | ||
collection, | ||
// Root | ||
base = 'flex flex-col w-fit', | ||
bg = '', | ||
dev-viinz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
spaceY = 'space-y-4', | ||
border = ' rounded-base', | ||
padding = 'p-4', | ||
shadow = '', | ||
classes = '', | ||
// Control | ||
controlBase = 'flex gap-2', | ||
controlBg = '', | ||
controlSpaceY = '', | ||
controlHover = 'hover:preset-tonal-primary', | ||
controlBorder = 'rounded-base', | ||
controlPadding = 'p-2', | ||
controlShadow = '', | ||
controlClasses = '', | ||
// Content | ||
contentBase = 'flex gap-1', | ||
contentBg = '', | ||
contentSpaceY = '', | ||
contentBorder = controlBorder, | ||
contentPadding = '', | ||
contentShadow = '', | ||
contentClasses = '', | ||
// Item | ||
itemBase = 'flex gap-2', | ||
itemBg = '', | ||
itemSpaceY = '', | ||
itemHover = controlHover, | ||
itemBorder = contentBorder, | ||
itemPadding = controlPadding, | ||
itemShadow = '', | ||
itemClasses = '', | ||
// Indent | ||
indentAmount = '1.2rem', | ||
dev-viinz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Indicator | ||
indicatorOpenRotation = '90deg', | ||
indicatorTransition = 'transition-transform', | ||
// Snippets | ||
branchIcon, | ||
itemIcon, | ||
branchIndicator, | ||
...zagProps | ||
dev-viinz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}: TreeViewProps = $props(); | ||
|
||
// Zag | ||
const treeCollection = tree.collection<CollectionNode>({ | ||
nodeToValue: (node) => node.id, | ||
nodeToString: (node) => node.value, | ||
rootNode: { | ||
id: 'ROOT_NODE', | ||
value: '', | ||
children: collection | ||
} | ||
}); | ||
const id = $props.id(); | ||
const service = useMachine(tree.machine as tree.Machine<CollectionNode>, () => ({ | ||
id: id, | ||
collection: treeCollection, | ||
...zagProps | ||
})); | ||
const api = $derived(tree.connect(service, normalizeProps)); | ||
</script> | ||
|
||
<!-- @component A collapsible TreeView. --> | ||
|
||
<!-- Tree --> | ||
<div class="{base} {bg} {spaceY} {border} {padding} {shadow} {classes}" {...api.getRootProps()} data-testid="tree"> | ||
<div {...api.getTreeProps()}> | ||
{#if treeCollection.rootNode.children} | ||
{#each treeCollection.rootNode.children as node, index} | ||
{@render treeNode({ node, indexPath: [index] })} | ||
{/each} | ||
{/if} | ||
</div> | ||
</div> | ||
|
||
<!-- Node --> | ||
{#snippet treeNode(nodeProps: tree.NodeProps)} | ||
{#if api.getNodeState(nodeProps).isBranch} | ||
<!-- Branch --> | ||
<div {...api.getBranchProps(nodeProps)} data-testid="tree-branch"> | ||
<!-- Control --> | ||
<button | ||
class="{controlBase} {controlBg} {controlSpaceY} {controlHover} {controlBorder} {controlPadding} {controlShadow} {controlClasses}" | ||
{...api.getBranchControlProps(nodeProps)} | ||
data-testid="tree-control" | ||
> | ||
<span | ||
dev-viinz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class="flex items-center {indicatorTransition}" | ||
style="--indicator-rotation:{indicatorOpenRotation};" | ||
{...api.getBranchIndicatorProps(nodeProps)} | ||
data-testid="tree-indicator" | ||
> | ||
{#if branchIndicator} | ||
{@render branchIndicator()} | ||
{:else} | ||
{@render chevron()} | ||
{/if} | ||
</span> | ||
{#if branchIcon} | ||
<div data-testid="tree-branch-icon"> | ||
{@render branchIcon()} | ||
</div> | ||
{/if} | ||
<span {...api.getBranchTextProps(nodeProps)} data-testid="tree-branch-text"> | ||
{nodeProps.node.value} | ||
</span> | ||
</button> | ||
|
||
<!-- Content --> | ||
{#if api.expandedValue.includes(nodeProps.node.id)} | ||
<div | ||
class="{contentBase} {contentBg} {contentSpaceY} {contentBorder} {contentPadding} {contentShadow} {contentClasses}" | ||
transition:slide={animationConfig} | ||
dev-viinz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{...api.getBranchContentProps(nodeProps)} | ||
data-testid="tree-content" | ||
> | ||
<div {...api.getBranchIndentGuideProps(nodeProps)} style="--indent-factor:{indentAmount}"></div> | ||
<div class="flex flex-col"> | ||
dev-viinz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{#if nodeProps.node.children} | ||
{#each nodeProps.node.children as childNode, index} | ||
{@render treeNode({ node: childNode, indexPath: [...nodeProps.indexPath, index] })} | ||
{/each} | ||
{/if} | ||
</div> | ||
</div> | ||
{/if} | ||
</div> | ||
{:else} | ||
<button | ||
dev-viinz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class="{itemBase} {itemBg} {itemSpaceY} {itemHover} {itemBorder} {itemPadding} {itemShadow} {itemClasses}" | ||
{...api.getItemProps(nodeProps)} | ||
data-testid="tree-item" | ||
> | ||
{#if itemIcon} | ||
<div data-testid="tree-item-icon"> | ||
{@render itemIcon()} | ||
</div> | ||
{/if} | ||
<span {...api.getItemTextProps(nodeProps)} data-testid="tree-item-text"> | ||
{nodeProps.node.value} | ||
</span> | ||
</button> | ||
{/if} | ||
{/snippet} | ||
|
||
<style> | ||
[data-scope='tree-view'][data-part='branch-indent-guide'] { | ||
dev-viinz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
width: calc(var(--indent-factor)); | ||
} | ||
[data-scope='tree-view'][data-part='branch-indicator'] { | ||
&[data-state='open'] { | ||
transform-box: fill-box; | ||
transform-origin: center; | ||
transform: rotate(var(--indicator-rotation)); | ||
} | ||
} | ||
</style> |
99 changes: 99 additions & 0 deletions
99
packages/skeleton-svelte/src/components/TreeView/TreeView.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
dev-viinz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { render, screen } from '@testing-library/svelte'; | ||
|
||
import TreeView from './TreeView.svelte'; | ||
import type { TreeViewProps } from './types.js'; | ||
import { mockSnippet } from '../../internal/test-utils.js'; | ||
|
||
describe('TreeView', () => { | ||
const testIds = { | ||
root: 'tree', | ||
branch: 'tree-branch', | ||
indicator: 'tree-indicator', | ||
branchIcon: 'tree-branch-icon', | ||
itemIcon: 'tree-item-icon', | ||
branchText: 'tree-branch-text', | ||
itemText: 'tree-item-text', | ||
control: 'tree-control', | ||
content: 'tree-content', | ||
item: 'tree-item' | ||
} as const; | ||
const commonProps: TreeViewProps = { | ||
collection: [ | ||
{ | ||
id: 'LEVEL_1', | ||
value: 'node_modules', | ||
children: [{ id: 'LEVEL_2.1', value: 'zag-js' }] | ||
} | ||
] | ||
} as const; | ||
|
||
it('Renders the component', () => { | ||
render(TreeView, { ...commonProps }); | ||
const component = screen.getByTestId(testIds.root); | ||
expect(component).toBeInTheDocument(); | ||
}); | ||
|
||
it(`should render with the branchIndicator snippet`, () => { | ||
const testValue = 'testIndicator'; | ||
render(TreeView, { ...commonProps, branchIndicator: mockSnippet(testValue) }); | ||
const input = screen.getByTestId(testIds.indicator); | ||
expect(input).toHaveTextContent(testValue); | ||
}); | ||
|
||
it(`should render with the branchIcon snippet`, () => { | ||
const testValue = 'testIcon'; | ||
render(TreeView, { ...commonProps, branchIcon: mockSnippet(testValue) }); | ||
const input = screen.getByTestId(testIds.branchIcon); | ||
expect(input).toHaveTextContent(testValue); | ||
}); | ||
|
||
it(`should render with the value/text of a branch`, () => { | ||
const valueToTest = commonProps.collection[0].value; | ||
render(TreeView, { ...commonProps }); | ||
const input = screen.getByTestId(testIds.branchText); | ||
expect(input).toHaveTextContent(valueToTest); | ||
}); | ||
|
||
it(`should render expanded with the itemIcon snippet`, () => { | ||
const testValue = 'testIcon'; | ||
render(TreeView, { ...commonProps, defaultExpandedValue: ['LEVEL_1'], itemIcon: mockSnippet(testValue) }); | ||
const input = screen.getByTestId(testIds.itemIcon); | ||
expect(input).toHaveTextContent(testValue); | ||
}); | ||
|
||
it(`should render expanded with the value/text of an item`, () => { | ||
const nodeToTest = commonProps.collection[0].children?.at(0); | ||
render(TreeView, { ...commonProps, defaultExpandedValue: ['LEVEL_1'] }); | ||
const input = screen.getByTestId(testIds.itemText); | ||
expect(nodeToTest).toBeDefined(); | ||
expect(input).toHaveTextContent(nodeToTest?.value ?? ''); | ||
}); | ||
|
||
const propMap = [ | ||
{ testId: testIds.root, props: ['base', 'bg', 'spaceY', 'border', 'padding', 'shadow', 'classes'] }, | ||
{ | ||
testId: testIds.control, | ||
props: ['controlBase', 'controlBg', 'controlSpaceY', 'controlBorder', 'controlPadding', 'controlShadow', 'controlClasses'] | ||
}, | ||
{ | ||
testId: testIds.content, | ||
props: ['contentBase', 'contentBg', 'contentSpaceY', 'contentBorder', 'contentPadding', 'contentShadow', 'contentClasses'] | ||
}, | ||
{ | ||
testId: testIds.item, | ||
props: ['itemBase', 'itemBg', 'itemSpaceY', 'itemBorder', 'itemPadding', 'itemShadow', 'itemClasses'] | ||
} | ||
]; | ||
|
||
for (const item of propMap) { | ||
for (const prop of item.props) { | ||
it(`Correctly applies the ${prop} prop`, () => { | ||
const value = 'bg-green-500'; | ||
render(TreeView, { ...commonProps, defaultExpandedValue: ['LEVEL_1'], [prop]: value }); | ||
const component = screen.getByTestId(item.testId); | ||
expect(component).toHaveClass(value); | ||
}); | ||
} | ||
} | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.