Skip to content
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

TreeList Example #107

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/lib/components/TreeList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
import { cls } from '$lib/utils/styles';
import { getComponentTheme } from './theme';


type Node = { id: number; name: string; level: number; children: Node[] };

/**
* An array of nodes containing custom metadata and an array of child nodes
* @param {Array} nodes - An array of Node Objects each containing a children: Node[] attribute.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be @type instead since it's not a function parameter (I'm not a jsdoc expert though).

/** @type {{ name: string; children: Node[] }[]} */

Doing so improves the docs (at least how Sveld parses them). See the orange unknown

Before After
image image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ment to link to the jsdoc @type docs - https://jsdoc.app/tags-type.html

Also, if we needed to do per-property comments, I think we would need to use this syntax...

/**
 * @typedef PropertiesHash
 * @type {object}
 * @property {string} id - an ID.
 * @property {string} name - your name.
 * @property {number} age - your age.
 */

/** @type {PropertiesHash} */
var props;

but I'm not sure it's worth it over just...

/** @type {{ id: string; name: string; age: number }} */

as I don't think it's handled by Sveld (or at least we have a way to show it in the docs). It could improve VSCode developer experience though (but haven't tested).

Thanks again for tackling this.

* Example: type Node = { name: string; children: Node[] }
*/
export let nodes: Node[];

/**
* Allows for conditional classes to be applied to each tag <ul> and <li>
* @param {Object} classes - An object containing optional classes for <ul> and <li> tags.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarily, should this be...

/**
 * @type {{ul?: string|Function, li?: string|Function}}
**/

It's what I've used for Button, at least as a test to improve how Sveld parses them since it doesn't appear to read the typescript types in this case :(.

See also this comment in LayerChart (which applies to Svelte UX) as well with improving how classes are shown in the docs.

techniq/layerchart#39 (comment)

Basically what you're doing here, we need to do everywhere (unless we can find a way to have Sveld handle this, or another "automated" way to parse the typescript defs.

I also wrote up a little bit in the discord

* @param {string|Function} classes.ul - A string or function that returns a string of classes to be applied to each <ul> tag.
* @param {string|Function} classes.li - A string or function that returns a string of classes to be applied to each <li> tag.
*/
export let classes: {
ul?: string | ((nodes: Node[]) => string);
li?: string | ((node: Node) => string);
Expand Down
73 changes: 71 additions & 2 deletions src/routes/docs/components/TreeList/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,77 @@
import TreeList from '$lib/components/TreeList.svelte';
import Preview from '$lib/components/Preview.svelte';
import Blockquote from '$docs/Blockquote.svelte';
</script>
import Icon from '$lib/components/Icon.svelte';
import { mdiCircleSmall } from '@mdi/js';
import { cls } from '$lib';

type Node = { name: string; level: number; children: Node[] };


let nodes: Node[] = [
{
name: 'Node 1',
level: 1,
children: [
{
name: 'Node 1.1',
level: 2,
children: [
{
name: 'Node 1.1.1',
level: 3,
children: []
},
{
name: 'Node 1.1.2',
level: 3,
children: []
}
]
},
{
name: 'Node 1.2',
level: 2,
children: []
}
]
},
{
name: 'Node 2',
level: 1,
children: [
{
name: 'Node 2.1',
level: 2,
children: []
}
]
}
];
</script>
<h1>Examples</h1>

<Blockquote>TODO</Blockquote>
Wer're going to use a TreeList to render the following tree using &lt;ul&gt; and &lt;li&gt;:
<br><br>

<Preview language="typescript" code="{"type Node = { name: string; level: number; children: Node[] };\n\nconst nodes =" + JSON.stringify(nodes, null, 2)}">
<pre>
type Node = &lbrace; name: string; level: number; children: Node[] &rbrace;;
const nodes = {JSON.stringify(nodes, null, 2).slice(0, 300)}...
</pre>
</Preview>
Comment on lines +58 to +63
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it might be nicer to add "svelte-json-tree as a devDependency like I did for LayerChart recently.

CleanShot.2023-11-03.at.10.01.03.mp4

I would think we would show it inline instead of via a dialog, but just a nice JSON preview. What do you think?


<br><br>

<Preview>
<TreeList nodes="{nodes}" let:node
classes={{ li: (node) => cls(node.level === 1 ? 'mb-2' : node.level > 2 ? 'ml-3' : '') }}
>
<slot {node}>
{#if node.level > 1}
<Icon path={mdiCircleSmall} class="-mx-1 text-black/30" />
{/if}
{@html node.name}
</slot>
</TreeList>
</Preview>