File Tree
svelte-animations/file-treeFreeBlock
An interactive file tree component with expandable folders, selectable files, and smooth animations.
Install it
npx shadcn@latest add https://sv-animations.vercel.app/r/file-tree.jsonSource
1<script lang="ts" module>2 export type TreeViewElement = {3 id: string;4 name: string;5 isSelectable?: boolean;6 children?: TreeViewElement[];7 };8</script>910<script lang="ts">11 import { setContext, getContext } from "svelte";12 import { cn } from "$UTILS$";13 import type { Snippet } from "svelte";14 import type { HTMLAttributes } from "svelte/elements";1516 interface TreeProps extends HTMLAttributes<HTMLDivElement> {17 /**18 * Initial selected item ID19 */20 initialSelectedId?: string;21 /**22 * Initial expanded items23 */24 initialExpandedItems?: string[];25 /**26 * Tree elements data27 */28 elements?: TreeViewElement[];29 /**30 * Show indicator line31 */32 indicator?: boolean;33 /**34 * Open folder icon35 */36 openIcon?: Snippet;37 /**38 * Close folder icon39 */40 closeIcon?: Snippet;41 /**42 * Text direction43 */44 dir?: "rtl" | "ltr";45 /**46 * Children content47 */48 children: Snippet;49 /**50 * Additional CSS classes51 */52 class?: string;53 }5455 let {56 initialSelectedId,57 initialExpandedItems = [],58 elements,59 indicator = true,60 openIcon,61 closeIcon,62 dir = "ltr",63 children,64 class: className,65 ...props66 }: TreeProps = $props();6768 let selectedId = $state(initialSelectedId);69 let expandedItems = $state<string[]>(initialExpandedItems);7071 const selectItem = (id: string) => {72 selectedId = id;73 };7475 const handleExpand = (id: string) => {76 if (expandedItems.includes(id)) {77 expandedItems = expandedItems.filter((item) => item !== id);78 } else {79 expandedItems = [...expandedItems, id];80 }81 };8283 const setExpandedItems = (items: string[]) => {84 expandedItems = items;85 };8687 const direction = dir === "rtl" ? "rtl" : "ltr";8889 setContext("tree", {90 selectedId: () => selectedId,91 expandedItems: () => expandedItems,92 handleExpand,93 selectItem,94 setExpandedItems,95 indicator,96 openIcon,97 closeIcon,98 direction,99 });100</script>101102<div class={cn("size-full", className)} {...props}>103 <div class="relative h-full overflow-auto px-2" dir={direction}>104 <div class="flex flex-col gap-1">105 {@render children()}106 </div>107 </div>108</div>
What it pulls in
npm packages
Files it writes
More from Svelte Animations
All 66 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Animated Beamanimated-beam | Svelte Animations | Blocks | Free | 1 dep · 6 files | |
| Animated Circular Progress Baranimated-circular-progress-bar | Svelte Animations | Blocks | Free | no deps · 2 files | |
| Animated Gradient Textanimated-gradient-text | Svelte Animations | Blocks | Free | no deps · 2 files | |
| Animated Grid Patternanimated-grid-pattern | Svelte Animations | Blocks | Free | 1 dep · 2 files | |
| Animated Listanimated-list | Svelte Animations | Blocks | Free | 1 dep · 2 files | |
| Animated Shiny Textanimated-shiny-text | Svelte Animations | Blocks | Free | no deps | |
| Animated Theme Toggleranimated-theme-toggler | Svelte Animations | Blocks | Free | no deps · 2 files | |
| Arc Timelinearc-timeline | Svelte Animations | Blocks | Free | no deps · 3 files |
