File Tree
Preview
<script setup lang="ts">
import { FileTree, FileTreeFile, FileTreeFolder } from 'beui-vue'
import { defineComponent, h, type PropType } from 'vue'
/** Inline Lucide glyphs for the layout demos (generated from lucide-react, ISC). Inner markup of 24x24 stroked icons. */
const ICONS: Record<string, string> = {
"ArrowRight": "<path d=\"M5 12h14\"></path><path d=\"m12 5 7 7-7 7\"></path>",
"ArrowUp": "<path d=\"m5 12 7-7 7 7\"></path><path d=\"M12 19V5\"></path>",
"ArrowUpRight": "<path d=\"M7 7h10v10\"></path><path d=\"M7 17 17 7\"></path>",
"BarChart3": "<path d=\"M3 3v16a2 2 0 0 0 2 2h16\"></path><path d=\"M18 17V9\"></path><path d=\"M13 17V5\"></path><path d=\"M8 17v-3\"></path>",
"Bell": "<path d=\"M10.268 21a2 2 0 0 0 3.464 0\"></path><path d=\"M3.262 15.326A1 1 0 0 0 4 17h16a1 1 0 0 0 .74-1.673C19.41 13.956 18 12.499 18 8A6 6 0 0 0 6 8c0 4.499-1.411 5.956-2.738 7.326\"></path>",
"Braces": "<path d=\"M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1\"></path><path d=\"M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 1-2-2V5a2 2 0 0 0-2-2h-1\"></path>",
"Building2": "<path d=\"M10 12h4\"></path><path d=\"M10 8h4\"></path><path d=\"M14 21v-3a2 2 0 0 0-4 0v3\"></path><path d=\"M6 10H4a2 2 0 0 0-2 2v7a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2h-2\"></path><path d=\"M6 21V5a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v16\"></path>",
"Calendar": "<path d=\"M8 2v3\"></path><path d=\"M16 2v3\"></path><rect x=\"3\" y=\"3\" width=\"18\" height=\"18\" rx=\"2\"></rect><path d=\"M3 9h18\"></path>",
"CalendarClock": "<path d=\"M16 14v2.2l1.6 1\"></path><path d=\"M16 2v3\"></path><path d=\"M21 7.338V5a2 2 0 00-2-2H5a2 2 0 00-2 2v14a2 2 0 002 2h2.338\"></path><path d=\"M3 9h5.859\"></path><path d=\"M8 2v3\"></path><circle cx=\"16\" cy=\"16\" r=\"6\"></circle>",
"Check": "<path d=\"M20 6 9 17l-5-5\"></path>",
"CheckCircle2": "<circle cx=\"12\" cy=\"12\" r=\"10\"></circle><path d=\"m9 12 2 2 4-4\"></path>",
"ChevronDown": "<path d=\"m6 9 6 6 6-6\"></path>",
"ChevronLeft": "<path d=\"m15 18-6-6 6-6\"></path>",
"ChevronRight": "<path d=\"m9 18 6-6-6-6\"></path>",
"ChevronUp": "<path d=\"m18 15-6-6-6 6\"></path>",
"ChevronsUpDown": "<path d=\"m7 15 5 5 5-5\"></path><path d=\"m7 9 5-5 5 5\"></path>",
"Circle": "<circle cx=\"12\" cy=\"12\" r=\"10\"></circle>",
"CircleUserRound": "<path d=\"M17.925 20.056a6 6 0 0 0-11.851.001\"></path><circle cx=\"12\" cy=\"11\" r=\"4\"></circle><circle cx=\"12\" cy=\"12\" r=\"10\"></circle>",
"Command": "<path d=\"M15 6v12a3 3 0 1 0 3-3H6a3 3 0 1 0 3 3V6a3 3 0 1 0-3 3h12a3 3 0 1 0-3-3\"></path>",
"File": "<path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path>",
"FileCode2": "<path d=\"M4 12.15V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.706.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2h-3.35\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"m5 16-3 3 3 3\"></path><path d=\"m9 22 3-3-3-3\"></path>",
"FileJson2": "<path d=\"M14 22h4a2 2 0 0 0 2-2V8a2.4 2.4 0 0 0-.706-1.706l-3.588-3.588A2.4 2.4 0 0 0 14 2H6a2 2 0 0 0-2 2v6\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M5 14a1 1 0 0 0-1 1v2a1 1 0 0 1-1 1 1 1 0 0 1 1 1v2a1 1 0 0 0 1 1\"></path><path d=\"M9 22a1 1 0 0 0 1-1v-2a1 1 0 0 1 1-1 1 1 0 0 1-1-1v-2a1 1 0 0 0-1-1\"></path>",
"FileText": "<path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path>",
"Folder": "<path d=\"M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z\"></path>",
"FolderKanban": "<path d=\"M4 20h16a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.93a2 2 0 0 1-1.66-.9l-.82-1.2A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13c0 1.1.9 2 2 2Z\"></path><path d=\"M8 10v4\"></path><path d=\"M12 10v2\"></path><path d=\"M16 10v6\"></path>",
"FolderOpen": "<path d=\"m6 14 1.5-2.9A2 2 0 0 1 9.24 10H20a2 2 0 0 1 1.94 2.5l-1.54 6a2 2 0 0 1-1.95 1.5H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h3.9a2 2 0 0 1 1.69.9l.81 1.2a2 2 0 0 0 1.67.9H18a2 2 0 0 1 2 2v2\"></path>",
"GitPullRequest": "<circle cx=\"18\" cy=\"18\" r=\"3\"></circle><circle cx=\"6\" cy=\"6\" r=\"3\"></circle><path d=\"M13 6h3a2 2 0 0 1 2 2v7\"></path><line x1=\"6\" x2=\"6\" y1=\"9\" y2=\"21\"></line>",
"GripVertical": "<circle cx=\"9\" cy=\"12\" r=\"1\"></circle><circle cx=\"9\" cy=\"5\" r=\"1\"></circle><circle cx=\"9\" cy=\"19\" r=\"1\"></circle><circle cx=\"15\" cy=\"12\" r=\"1\"></circle><circle cx=\"15\" cy=\"5\" r=\"1\"></circle><circle cx=\"15\" cy=\"19\" r=\"1\"></circle>",
"Heart": "<path d=\"M2 9.5a5.5 5.5 0 0 1 9.591-3.676.56.56 0 0 0 .818 0A5.49 5.49 0 0 1 22 9.5c0 2.29-1.5 4-3 5.5l-5.492 5.313a2 2 0 0 1-3 .019L5 15c-1.5-1.5-3-3.2-3-5.5\"></path>",
"Home": "<path d=\"M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8\"></path><path d=\"M3 10a2 2 0 0 1 .709-1.528l7-6a2 2 0 0 1 2.582 0l7 6A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z\"></path>",
"House": "<path d=\"M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8\"></path><path d=\"M3 10a2 2 0 0 1 .709-1.528l7-6a2 2 0 0 1 2.582 0l7 6A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z\"></path>",
"Inbox": "<polyline points=\"22 12 16 12 14 15 10 15 8 12 2 12\"></polyline><path d=\"M5.45 5.11 2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z\"></path>",
"Layers": "<path d=\"M12.83 2.18a2 2 0 0 0-1.66 0L2.6 6.08a1 1 0 0 0 0 1.83l8.58 3.91a2 2 0 0 0 1.66 0l8.58-3.9a1 1 0 0 0 0-1.83z\"></path><path d=\"M2 12a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 12\"></path><path d=\"M2 17a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 17\"></path>",
"LayoutGrid": "<rect width=\"7\" height=\"7\" x=\"3\" y=\"3\" rx=\"1\"></rect><rect width=\"7\" height=\"7\" x=\"14\" y=\"3\" rx=\"1\"></rect><rect width=\"7\" height=\"7\" x=\"14\" y=\"14\" rx=\"1\"></rect><rect width=\"7\" height=\"7\" x=\"3\" y=\"14\" rx=\"1\"></rect>",
"ListTodo": "<path d=\"M13 5h8\"></path><path d=\"M13 12h8\"></path><path d=\"M13 19h8\"></path><path d=\"m3 17 2 2 4-4\"></path><rect x=\"3\" y=\"4\" width=\"6\" height=\"6\" rx=\"1\"></rect>",
"LoaderCircle": "<path d=\"M21 12a9 9 0 1 1-6.219-8.56\"></path>",
"Mail": "<path d=\"m22 7-8.991 5.727a2 2 0 0 1-2.009 0L2 7\"></path><rect x=\"2\" y=\"4\" width=\"20\" height=\"16\" rx=\"2\"></rect>",
"MessageCircle": "<path d=\"M2.992 16.342a2 2 0 0 1 .094 1.167l-1.065 3.29a1 1 0 0 0 1.236 1.168l3.413-.998a2 2 0 0 1 1.099.092 10 10 0 1 0-4.777-4.719\"></path>",
"Minus": "<path d=\"M5 12h14\"></path>",
"Music": "<path d=\"M9 18V5l12-2v13\"></path><circle cx=\"6\" cy=\"18\" r=\"3\"></circle><circle cx=\"18\" cy=\"16\" r=\"3\"></circle>",
"NotebookTabs": "<path d=\"M2 6h4\"></path><path d=\"M2 10h4\"></path><path d=\"M2 14h4\"></path><path d=\"M2 18h4\"></path><rect width=\"16\" height=\"20\" x=\"4\" y=\"2\" rx=\"2\"></rect><path d=\"M15 2v20\"></path><path d=\"M15 7h5\"></path><path d=\"M15 12h5\"></path><path d=\"M15 17h5\"></path>",
"PackageCheck": "<path d=\"M12 22V12\"></path><path d=\"m16 17 2 2 4-4\"></path><path d=\"M21 11.127V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.729l7 4a2 2 0 0 0 2 .001l1.32-.753\"></path><path d=\"M3.29 7 12 12l8.71-5\"></path><path d=\"m7.5 4.27 8.997 5.148\"></path>",
"Palette": "<path d=\"M12 22a1 1 0 0 1 0-20 10 9 0 0 1 10 9 5 5 0 0 1-5 5h-2.25a1.75 1.75 0 0 0-1.4 2.8l.3.4a1.75 1.75 0 0 1-1.4 2.8z\"></path><circle cx=\"13.5\" cy=\"6.5\" r=\".5\" fill=\"currentColor\"></circle><circle cx=\"17.5\" cy=\"10.5\" r=\".5\" fill=\"currentColor\"></circle><circle cx=\"6.5\" cy=\"12.5\" r=\".5\" fill=\"currentColor\"></circle><circle cx=\"8.5\" cy=\"7.5\" r=\".5\" fill=\"currentColor\"></circle>",
"PanelLeft": "<rect width=\"18\" height=\"18\" x=\"3\" y=\"3\" rx=\"2\"></rect><path d=\"M9 3v18\"></path>",
"Plus": "<path d=\"M5 12h14\"></path><path d=\"M12 5v14\"></path>",
"RadioTower": "<path d=\"M4.9 16.1C1 12.2 1 5.8 4.9 1.9\"></path><path d=\"M7.8 4.7a6.14 6.14 0 0 0-.8 7.5\"></path><circle cx=\"12\" cy=\"9\" r=\"2\"></circle><path d=\"M16.2 4.8c2 2 2.26 5.11.8 7.47\"></path><path d=\"M19.1 1.9a9.96 9.96 0 0 1 0 14.1\"></path><path d=\"M9.5 18h5\"></path><path d=\"m8 22 4-11 4 11\"></path>",
"Rocket": "<path d=\"M12 15v5s3.03-.55 4-2c1.08-1.62 0-5 0-5\"></path><path d=\"M4.5 16.5c-1.5 1.26-2 5-2 5s3.74-.5 5-2c.71-.84.7-2.13-.09-2.91a2.18 2.18 0 0 0-2.91-.09\"></path><path d=\"M9 12a22 22 0 0 1 2-3.95A12.88 12.88 0 0 1 22 2c0 2.72-.78 7.5-6 11a22.4 22.4 0 0 1-4 2z\"></path><path d=\"M9 12H4s.55-3.03 2-4c1.62-1.08 5 .05 5 .05\"></path>",
"Search": "<path d=\"m21 21-4.34-4.34\"></path><circle cx=\"11\" cy=\"11\" r=\"8\"></circle>",
"Settings": "<path d=\"M9.671 4.136a2.34 2.34 0 0 1 4.659 0 2.34 2.34 0 0 0 3.319 1.915 2.34 2.34 0 0 1 2.33 4.033 2.34 2.34 0 0 0 0 3.831 2.34 2.34 0 0 1-2.33 4.033 2.34 2.34 0 0 0-3.319 1.915 2.34 2.34 0 0 1-4.659 0 2.34 2.34 0 0 0-3.32-1.915 2.34 2.34 0 0 1-2.33-4.033 2.34 2.34 0 0 0 0-3.831A2.34 2.34 0 0 1 6.35 6.051a2.34 2.34 0 0 0 3.319-1.915\"></path><circle cx=\"12\" cy=\"12\" r=\"3\"></circle>",
"ShieldCheck": "<path d=\"M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z\"></path><path d=\"m9 12 2 2 4-4\"></path>",
"Sparkles": "<path d=\"M11.017 2.814a1 1 0 0 1 1.966 0l1.051 5.558a2 2 0 0 0 1.594 1.594l5.558 1.051a1 1 0 0 1 0 1.966l-5.558 1.051a2 2 0 0 0-1.594 1.594l-1.051 5.558a1 1 0 0 1-1.966 0l-1.051-5.558a2 2 0 0 0-1.594-1.594l-5.558-1.051a1 1 0 0 1 0-1.966l5.558-1.051a2 2 0 0 0 1.594-1.594z\"></path><path d=\"M20 2v4\"></path><path d=\"M22 4h-4\"></path><circle cx=\"4\" cy=\"20\" r=\"2\"></circle>",
"Target": "<circle cx=\"12\" cy=\"12\" r=\"10\"></circle><circle cx=\"12\" cy=\"12\" r=\"6\"></circle><circle cx=\"12\" cy=\"12\" r=\"2\"></circle>",
"Undo2": "<path d=\"M9 14 4 9l5-5\"></path><path d=\"M4 9h10.5a5.5 5.5 0 0 1 5.5 5.5a5.5 5.5 0 0 1-5.5 5.5H11\"></path>",
"Workflow": "<rect width=\"8\" height=\"8\" x=\"3\" y=\"3\" rx=\"2\"></rect><path d=\"M7 11v4a2 2 0 0 0 2 2h4\"></path><rect width=\"8\" height=\"8\" x=\"13\" y=\"13\" rx=\"2\"></rect>",
"X": "<path d=\"M18 6 6 18\"></path><path d=\"m6 6 12 12\"></path>"
}
export type LucideName = string
export const LucideIcon = defineComponent({
name: 'LucideIcon',
props: { name: { type: String as PropType<LucideName>, required: true }, size: { type: Number, default: 16 }, strokeWidth: { type: Number, default: 2 } },
setup: props => () => h('svg', {
xmlns: 'http://www.w3.org/2000/svg', width: props.size, height: props.size, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor',
'stroke-width': props.strokeWidth, 'stroke-linecap': 'round', 'stroke-linejoin': 'round', 'aria-hidden': 'true', innerHTML: ICONS[props.name] ?? '',
}),
})
</script>
<template>
<div class="stage">
<div class="box">
<FileTree default-value="file-tree" :default-expanded-ids="['app', 'components']" label="Project files">
<FileTreeFolder value="app" name="app">
<FileTreeFolder value="components" name="components">
<FileTreeFile value="file-tree" name="file-tree.tsx">
<template #icon><LucideIcon name="Braces" :size="16" style="color: oklch(0.685 0.169 237.323)" /></template>
</FileTreeFile>
<FileTreeFile value="button" name="button.tsx">
<template #icon><LucideIcon name="Braces" :size="16" style="color: oklch(0.685 0.169 237.323)" /></template>
</FileTreeFile>
</FileTreeFolder>
<FileTreeFile value="page" name="page.tsx">
<template #icon><LucideIcon name="FileCode2" :size="16" style="color: oklch(0.685 0.169 237.323)" /></template>
</FileTreeFile>
<FileTreeFile value="styles" name="globals.css">
<template #icon><LucideIcon name="Palette" :size="16" style="color: oklch(0.606 0.25 292.717)" /></template>
</FileTreeFile>
</FileTreeFolder>
<FileTreeFolder value="public" name="public">
<FileTreeFile value="logo" name="logo.svg" />
<FileTreeFile value="grid" name="grid.svg" />
</FileTreeFolder>
<FileTreeFile value="package" name="package.json">
<template #icon><LucideIcon name="FileJson2" :size="16" style="color: oklch(0.769 0.188 70.08)" /></template>
</FileTreeFile>
<FileTreeFile value="readme" name="README.md">
<template #icon><LucideIcon name="FileText" :size="16" style="color: var(--beui-muted-foreground)" /></template>
</FileTreeFile>
</FileTree>
</div>
</div>
</template>
<style scoped>
.stage { display: flex; min-height: 420px; width: 100%; align-items: flex-start; justify-content: center; padding: 2.5rem 1rem 0; box-sizing: border-box; }
.box { width: 100%; max-width: 20rem; padding: .5rem; box-sizing: border-box; }
</style><script lang="ts">
/** English defaults. Override per instance with `labels`. */
export const fileTreeMessages = { label: 'Files' }
export type FileTreeMessages = typeof fileTreeMessages
export interface FileTreeNode {
id: string
name: string
disabled?: boolean
children?: readonly FileTreeNode[]
}
</script>
<script setup lang="ts">
import { AnimatePresence, motion } from 'motion-v'
import { useReducedMotion } from '../core/reduced-motion'
import { Comment, Fragment, isVNode, normalizeClass, shallowRef, useSlots, type Slot, type VNode } from 'vue'
import { easeOut, layoutSpring, swapSpring } from '../core/motion'
import FileTreeFile from './FileTreeFile.vue'
import FileTreeFolder from './FileTreeFolder.vue'
import SharedLayoutBg from './SharedLayoutBg.vue'
interface Item {
id: string
name: string
type: 'file' | 'folder'
children?: Item[]
icon?: Slot
disabled?: boolean
className?: string
data?: FileTreeNode
}
interface Row { item: Item; depth: number; parentId: string | null; position: number; setSize: number }
const props = withDefaults(defineProps<{
/** Data-driven tree. Omit to compose `FileTreeFolder` and `FileTreeFile` in the default slot. */
items?: readonly FileTreeNode[]
/** Selected id (`v-model`). */
modelValue?: string | null
defaultValue?: string | null
/** Expanded folder ids (`v-model:expanded-ids`). */
expandedIds?: string[]
/** Same as `expandedIds`, bound with `v-model:expanded`. */
expanded?: string[]
defaultExpandedIds?: string[]
/** Accessible name of the tree. */
ariaLabel?: string
/** Same as `ariaLabel`. */
label?: string
labels?: Partial<FileTreeMessages>
/** Indent per level in px. */
indent?: number
className?: string
classNames?: { tree?: string; item?: string; icon?: string; label?: string }
}>(), { indent: 18, defaultValue: null, defaultExpandedIds: () => [], modelValue: undefined, expanded: undefined, expandedIds: undefined })
const emit = defineEmits<{
'update:modelValue': [id: string]
'update:expanded': [ids: string[]]
'update:expandedIds': [ids: string[]]
valueChange: [id: string]
expandedChange: [ids: string[]]
}>()
const slots = useSlots()
defineSlots<{ default?: () => unknown; item?: (props: { item: FileTreeNode }) => unknown }>()
const reduce = useReducedMotion()
const t = (key: keyof FileTreeMessages) => props.labels?.[key] ?? fileTreeMessages[key]
const internalValue = shallowRef<string | null>(props.defaultValue)
const internalExpanded = shallowRef<string[]>([...props.defaultExpandedIds])
const focusedId = shallowRef<string | null>(props.modelValue ?? props.defaultValue)
const selectedId = () => (props.modelValue === undefined ? internalValue.value : props.modelValue)
const currentExpanded = () => props.expandedIds ?? props.expanded ?? internalExpanded.value
const ROW_ENTER = { duration: 0.22, ease: easeOut } as const
const BRANCH_DRAW = { duration: 0.3, ease: easeOut } as const
const instant = { duration: 0 } as const
// --- Collection -----------------------------------------------------------
function fromData(nodes: readonly FileTreeNode[]): Item[] {
return nodes.map(node => ({
id: node.id, name: node.name, type: node.children ? 'folder' : 'file', disabled: node.disabled, data: node,
children: node.children ? fromData(node.children) : undefined,
}))
}
function slotNodes(nodes: VNode[] | undefined): VNode[] {
const out: VNode[] = []
for (const node of nodes ?? []) {
if (!isVNode(node) || node.type === Comment) continue
if (node.type === Fragment && Array.isArray(node.children)) out.push(...slotNodes(node.children as VNode[]))
else out.push(node)
}
return out
}
function fromSlot(nodes: VNode[] | undefined): Item[] {
const items: Item[] = []
for (const node of slotNodes(nodes)) {
const isFolder = node.type === FileTreeFolder
if (!isFolder && node.type !== FileTreeFile) continue
const p = (node.props ?? {}) as Record<string, unknown>
const parts = (node.children && typeof node.children === 'object' ? node.children : {}) as Record<string, Slot | undefined>
items.push({
id: String(p.value), name: String(p.name), type: isFolder ? 'folder' : 'file', icon: parts.icon,
className: p.class ? normalizeClass(p.class) : undefined,
disabled: p.disabled === '' || p.disabled === true,
children: isFolder ? fromSlot(parts.default?.() as VNode[] | undefined) : undefined,
})
}
return items
}
function flatten(items: Item[], open: ReadonlySet<string>, depth = 0, parentId: string | null = null): Row[] {
return items.flatMap((item, index) => {
const row: Row = { item, depth, parentId, position: index + 1, setSize: items.length }
if (item.type !== 'folder' || !open.has(item.id) || !item.children?.length) return [row]
return [row, ...flatten(item.children, open, depth + 1, item.id)]
})
}
// Rows are rebuilt every render: the declarative slot has no reactive source of its own.
function collect() {
const open = new Set(currentExpanded())
const items = props.items ? fromData(props.items) : fromSlot(slots.default?.() as VNode[] | undefined)
return { open, rows: flatten(items, open) }
}
// Keep a real row tabbable in the first commit and right after a collapse removes the focused row.
function tabStop(rows: Row[]) {
const id = focusedId.value
return id !== null && rows.some(row => row.item.id === id) ? id : (rows[0]?.item.id ?? null)
}
// --- Behaviour ------------------------------------------------------------
const rowNodes = new Map<string, HTMLButtonElement>()
function setRowNode(id: string, element: unknown) {
if (element instanceof HTMLButtonElement) rowNodes.set(id, element)
else rowNodes.delete(id)
}
function focusRow(id: string) {
focusedId.value = id
const node = rowNodes.get(id)
if (node) node.focus()
else requestAnimationFrame(() => rowNodes.get(id)?.focus())
}
function selectItem(item: Item) {
if (item.disabled) return
if (props.modelValue === undefined) internalValue.value = item.id
emit('update:modelValue', item.id)
emit('valueChange', item.id)
}
function setExpanded(next: string[]) {
if (props.expandedIds === undefined && props.expanded === undefined) internalExpanded.value = next
emit('update:expanded', next)
emit('update:expandedIds', next)
emit('expandedChange', next)
}
function toggleFolder(id: string) {
const next = new Set(currentExpanded())
if (next.has(id)) next.delete(id)
else next.add(id)
setExpanded(Array.from(next))
}
function activate(row: Row) {
if (row.item.disabled) return
selectItem(row.item)
if (row.item.type === 'folder') toggleFolder(row.item.id)
}
function keydown(event: KeyboardEvent, row: Row) {
const { open, rows } = collect()
const index = rows.findIndex(entry => entry.item.id === row.item.id)
const previous = rows[index - 1]
const next = rows[index + 1]
const isFolder = row.item.type === 'folder'
const isOpen = open.has(row.item.id)
if (event.key === 'ArrowDown' && next) { event.preventDefault(); focusRow(next.item.id) }
else if (event.key === 'ArrowUp' && previous) { event.preventDefault(); focusRow(previous.item.id) }
else if (event.key === 'Home' && rows[0]) { event.preventDefault(); focusRow(rows[0].item.id) }
else if (event.key === 'End' && rows.length) { event.preventDefault(); focusRow(rows[rows.length - 1]!.item.id) }
else if (event.key === 'ArrowRight' && isFolder) {
event.preventDefault()
if (!isOpen && !row.item.disabled) toggleFolder(row.item.id)
else if (next?.parentId === row.item.id) focusRow(next.item.id)
} else if (event.key === 'ArrowLeft') {
event.preventDefault()
if (isFolder && isOpen && !row.item.disabled) toggleFolder(row.item.id)
else if (row.parentId) focusRow(row.parentId)
} else if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault()
activate(row)
}
}
</script>
<template>
<SharedLayoutBg v-bind="{ role: 'tree', 'aria-label': ariaLabel ?? label ?? t('label'), 'aria-multiselectable': 'false' }" :inset="0"
pill-class="be-ft-pill" pill-container-class="be-ft-pill-box" class="be-ft" :class="[className, classNames?.tree]">
<template v-for="ctx in [collect()]" :key="'ctx'">
<motion.div v-for="row in ctx.rows" :key="row.item.id" :layout="reduce ? false : 'position'"
:initial="reduce ? false : { opacity: 0, y: -6 }"
:animate="{ opacity: row.item.disabled ? 0.42 : 1, y: 0, transition: reduce ? instant : { ...ROW_ENTER, delay: Math.min(row.position * 0.025, 0.1) } }"
:transition="reduce ? instant : layoutSpring">
<button :ref="element => setRowNode(row.item.id, element)" type="button" role="treeitem" class="be-ft-row"
:class="[{ 'is-selected': selectedId() === row.item.id }, classNames?.item, row.item.className]"
:aria-level="row.depth + 1" :aria-posinset="row.position" :aria-setsize="row.setSize" :aria-selected="selectedId() === row.item.id"
:aria-expanded="row.item.type === 'folder' ? ctx.open.has(row.item.id) : undefined" :aria-disabled="row.item.disabled || undefined"
:tabindex="tabStop(ctx.rows) === row.item.id ? 0 : -1" :style="{ paddingLeft: `${8 + row.depth * indent}px` }"
@focus="focusedId = row.item.id" @keydown="keydown($event, row)" @click="activate(row)">
<motion.span v-if="row.depth > 0" aria-hidden="true" class="be-ft-branch"
:initial="reduce ? false : { opacity: 0, scaleY: 0 }" :animate="{ opacity: 1, scaleY: 1 }"
:transition="reduce ? instant : BRANCH_DRAW" :style="{ left: `${16 + (row.depth - 1) * indent}px` }" />
<motion.span aria-hidden="true" class="be-ft-chevron" :class="{ 'is-hidden': row.item.type !== 'folder' }"
:animate="{ rotate: ctx.open.has(row.item.id) && row.item.type === 'folder' ? 90 : 0 }" :transition="reduce ? instant : swapSpring">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m9 18 6-6-6-6" /></svg>
</motion.span>
<span aria-hidden="true" class="be-ft-icon" :class="[{ 'is-open': row.item.type === 'folder' && ctx.open.has(row.item.id) }, classNames?.icon]">
<component :is="row.item.icon" v-if="row.item.icon" />
<template v-else-if="row.item.type === 'file'">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z" /><path d="M14 2v5a1 1 0 0 0 1 1h5" /></svg>
</template>
<template v-else>
<svg v-if="reduce && ctx.open.has(row.item.id)" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m6 14 1.5-2.9A2 2 0 0 1 9.24 10H20a2 2 0 0 1 1.94 2.5l-1.54 6a2 2 0 0 1-1.95 1.5H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h3.9a2 2 0 0 1 1.69.9l.81 1.2a2 2 0 0 0 1.67.9H18a2 2 0 0 1 2 2v2" /></svg>
<svg v-else-if="reduce" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z" /></svg>
<AnimatePresence v-else :initial="false" mode="popLayout">
<motion.span :key="ctx.open.has(row.item.id) ? 'open' : 'closed'" class="be-ft-folder"
:initial="{ opacity: 0, scale: 0.75, rotate: ctx.open.has(row.item.id) ? -8 : 8 }" :animate="{ opacity: 1, scale: 1, rotate: 0 }"
:exit="{ opacity: 0, scale: 0.75, rotate: ctx.open.has(row.item.id) ? 8 : -8 }" :transition="swapSpring">
<svg v-if="ctx.open.has(row.item.id)" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m6 14 1.5-2.9A2 2 0 0 1 9.24 10H20a2 2 0 0 1 1.94 2.5l-1.54 6a2 2 0 0 1-1.95 1.5H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h3.9a2 2 0 0 1 1.69.9l.81 1.2a2 2 0 0 0 1.67.9H18a2 2 0 0 1 2 2v2" /></svg>
<svg v-else width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z" /></svg>
</motion.span>
</AnimatePresence>
</template>
</span>
<span class="be-ft-label" :class="classNames?.label">
<slot v-if="row.item.data" name="item" :item="row.item.data">{{ row.item.name }}</slot>
<template v-else>{{ row.item.name }}</template>
</span>
</button>
</motion.div>
</template>
</SharedLayoutBg>
</template>
<style>
.be-ft { min-width: 0; }
.be-shared-bg .be-ft-pill { border-radius: .75rem; background: var(--beui-muted); }
.be-shared-bg .be-ft-pill-box { top: 0; bottom: auto; height: 2.25rem; }
.be-ft-row { position: relative; display: flex; box-sizing: border-box; width: 100%; height: 2.25rem; align-items: center; gap: .5rem; overflow: hidden; margin: 0; padding-block: 0; padding-right: .5rem; border: 0; border-radius: .5rem; background: none; color: var(--beui-muted-foreground); font: inherit; font-size: .875rem; line-height: 1.25rem; text-align: left; outline: none; cursor: pointer; transition: color .15s; }
.be-ft-row:hover { color: var(--beui-foreground); }
.be-ft-row:focus-visible { box-shadow: inset 0 0 0 2px var(--beui-ring); }
.be-ft-row[aria-disabled=true] { cursor: not-allowed; }
.be-ft-row.is-selected { background: var(--beui-muted); font-weight: 500; color: var(--beui-foreground); }
.be-ft-branch { position: absolute; top: 0; bottom: 0; width: 1px; transform-origin: top; background: color-mix(in srgb, var(--beui-border) 70%, transparent); }
.be-ft-chevron, .be-ft-icon { position: relative; z-index: 10; display: grid; width: 1rem; height: 1rem; flex: none; place-items: center; }
.be-ft-chevron.is-hidden { opacity: 0; }
.be-ft-icon { color: var(--beui-muted-foreground); transition: color .15s; }
.be-ft-row:hover .be-ft-icon, .be-ft-icon.is-open { color: var(--beui-foreground); }
.be-ft-folder { position: absolute; inset: 0; display: grid; place-items: center; }
.be-ft-label { position: relative; z-index: 10; min-width: 0; flex: 1 1 0%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
</style>Install
Add it with your package manager, or copy the source manually.
Run it in a project set up with shadcn-vue. The source is copied to components/beui and its dependencies are added to your package.json. Read the shadcn-vue guide
$ bunx --bun shadcn-vue@latest add @beui-vue/file-tree$ bun add beui-vueDownload the release archive to your project root, then install the local file.
$ bun add ./beui-vue-0.1.0.tgzImport the shared styles once in your application.
import FileTree from '@/components/beui/layout/FileTree.vue'
import '@/components/beui/styles.css'API Reference
FileTreeFolder
valuestring—namestring—#icon?slotIcon of the row; replaces the default folder icon.
—disabled?boolean—class?string—#default?slotThe folder's children: `FileTreeFolder` and `FileTreeFile` parts.
—FileTreeFile
valuestring—namestring—#icon?slotIcon of the row; replaces the default file icon.
—disabled?boolean—class?string—FileTree
v-model?string | nullSelected id (`v-model`).
undefineddefaultValue?string | nullnullv-model:expandedIds?string[]Expanded folder ids (`v-model:expanded-ids`).
undefineddefaultExpandedIds?string[][]@update:expanded?(ids: string[]) => void—ariaLabel?stringAccessible name of the tree.
—labels?Partial<FileTreeMessages>Override any built-in string.
—indent?numberIndent per level in px.
18className?string—classNames?{ tree?: string; item?: string; icon?: string; label?: string }—items?readonly FileTreeNode[]Data-driven tree. Omit to compose `FileTreeFolder` and `FileTreeFile` in the default slot.
—v-model:expanded?string[]Same as `expandedIds`, bound with `v-model:expanded`.
undefinedlabel?stringSame as `ariaLabel`.
—@value-change?(id: string) => void—@expanded-change?(ids: string[]) => void—#default?slot—#item?slot—Related components
Animated Context Menu
Composable context-menu primitives with a pointer-origin clip morph, a gliding active row, checkbox and radio choices, keyboard navigation, typeahead, and long-press support.
Image Viewer
A composable Vue image viewer for galleries and attachment previews, with animated lightbox variants, custom thumbnails and controls, swipe and keyboard navigation, zoom and pan, accessible focus management, and controlled or uncontrolled selection.
Animated Color Selector
Composable color swatches with a spring-gliding selection ring, press feedback, native radio keyboard navigation, and controlled or uncontrolled state.
Updated