// Copyright (c) Meta Platforms, Inc. and affiliates. 'use client'; import {useState} from 'react'; import {useTreeFocus} from '@astryxdesign/core/hooks'; import {Icon} from '@astryxdesign/core/Icon'; import {HStack, VStack} from '@astryxdesign/core/Layout'; import {Text} from '@astryxdesign/core/Text'; import {rtlStyles} from '@astryxdesign/core/utils'; import {ChevronDownIcon, ChevronRightIcon} from '@heroicons/react/24/outline'; type FileNode = {name: string; children?: FileNode[]}; const FILES: FileNode[] = [ {name: 'src', children: [{name: 'index.ts'}, {name: 'theme.ts'}]}, {name: 'docs', children: [{name: 'intro.md'}, {name: 'usage.md'}]}, {name: 'package.json'}, {name: 'README.md'}, ]; const FOLDERS = new Set( FILES.filter(node => node.children != null).map(node => node.name), ); export default function UseTreeFocusHookUsage() { const [expanded, setExpanded] = useState>( () => new Set(['src']), ); const [openFile, setOpenFile] = useState(null); const toggle = (name: string) => setExpanded(current => { const next = new Set(current); if (!next.delete(name)) { next.add(name); } return next; }); const {treeRef, handleKeyDown, handleFocus} = useTreeFocus({ hasRovingTabIndex: true, onToggleExpand: toggle, onActivate: (_item, name) => { if (name == null || FOLDERS.has(name)) { return false; } setOpenFile(name); return true; }, }); const renderRow = (node: FileNode, level: 1 | 2) => { const isFolder = node.children != null; const isExpanded = expanded.has(node.name); return ( (isFolder ? toggle(node.name) : setOpenFile(node.name))}> {isFolder && (isExpanded ? ( ) : ( // The collapsed chevron points toward the inline end, where // ArrowRight (ArrowLeft under RTL) opens the folder. ))} {node.name} ); }; return ( {FILES.flatMap(node => [ renderRow(node, 1), ...(expanded.has(node.name) ? (node.children ?? []).map(child => renderRow(child, 2)) : []), ])} Open file: {openFile ?? 'none'} ); }