packages/react-aria-components/docs/examples/file-system.mdx
{/* Copyright 2023 Adobe. All rights reserved. This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */}
import {ExampleLayout} from '@react-spectrum/docs'; export default ExampleLayout;
import docs from 'docs:react-aria-components'; import {TypeLink} from '@react-spectrum/docs'; import styles from '@react-spectrum/docs/src/docs.css'; import Tree from '@react-spectrum/docs/pages/assets/component-illustrations/Tree.svg'; import {ExampleCard} from '@react-spectrum/docs/src/ExampleCard'; import ChevronRight from '@spectrum-icons/workflow/ChevronRight';
A file system Tree featuring multiple selection and styled with Tailwind CSS.
import './tailwind.global.css';
const filesystem = [
// mock up a file system with 50 items total and nested children up to 4 levels deep
{'id': 'documents', name: 'Documents', children: [
{'id': 'photos', name: 'Photos', children: [
{'id': 'summer', name: 'Summer', children: [
{'id': 'beach', name: 'Beach'},
{'id': 'mountains', name: 'Mountains'},
{'id': 'forest', name: 'Forest'},
{'id': 'desert', name: 'Desert'}
]},
{'id': 'winter', name: 'Winter', children: [
{'id': 'skiing', name: 'Skiing'},
{'id': 'snowboarding', name: 'Snowboarding'},
{'id': 'snowmobiling', name: 'Snowmobiling'},
{'id': 'snowshoeing', name: 'Snowshoeing'}
]}
]},
{'id': 'videos', name: 'Videos', children: [
{'id': 'family', name: 'Family'},
{'id': 'friends', name: 'Friends'},
{'id': 'pets', name: 'Pets'},
{'id': 'vacations', name: 'Vacations'}
]},
{'id': 'music', name: 'Music', children: [
{'id': 'rock', name: 'Rock', children: [
{'id': 'classic', name: 'Classic'},
{'id': 'alternative', name: 'Alternative'},
{'id': 'punk', name: 'Punk'},
{'id': 'metal', name: 'Metal'}
]},
{'id': 'pop', name: 'Pop', children: [
{'id': 'dance', name: 'Dance'},
{'id': 'hip-hop', name: 'Hip Hop'},
{'id': 'r&b', name: 'R&B'},
{'id': 'soul', name: 'Soul'}
]}
]},
{'id': 'movies', name: 'Movies', children: [
{'id': 'action', name: 'Action'},
{'id': 'comedy', name: 'Comedy'},
{'id': 'drama', name: 'Drama'},
{'id': 'horror', name: 'Horror'}
]}
]}
];
import {Button, Collection, Tree, TreeItem, TreeItemContent} from 'react-aria-components';
import {ChevronRight} from 'lucide-react';
function FileSystemExample() {
return (
<div className="bg-linear-to-r from-indigo-500 to-violet-500 p-8 rounded-lg flex items-center justify-center">
<Tree aria-label="File system" selectionMode="multiple" selectionBehavior="replace" items={filesystem} defaultExpandedKeys={['documents']} className={`
border-separate border-spacing-0 w-60 h-100 bg-slate-900
overflow-auto rounded-lg shadow-lg`}>
{function renderItem(item) {
return (
<TreeItem
textValue={item.name}
className={`selected:bg-slate-500 text-white
cursor-default group outline-hidden focus-visible:outline focus-visible:outline-2
focus-visible:outline-slate-600 focus-visible:-outline-offset-4
selected:focus-visible:outline-white`}>
<TreeItemContent>
{({hasChildItems}) => (
<div className="flex items-center space-x-2 py-2 ps-[calc(calc(var(--tree-item-level)_-_1)_*_calc(var(--spacing)_*_3))]">
{hasChildItems ? <Button slot="chevron" className={`shrink-0 w-8 h-8
group-data-[expanded=true]:rotate-90 transition-rotate duration-200
inline-flex items-center justify-center bg-transparent border-0 me-0
cursor-default outline-hidden text-white`}><ChevronRight className="w-4 h-4" /></Button> : <div className="shrink-0 w-8 h-8" />}
<div>{item.name}</div>
</div>
)}
</TreeItemContent>
<Collection items={item.children}>
{renderItem}
</Collection>
</TreeItem>
)
}}
</Tree>
</div>
);
}
This example uses the tailwindcss-react-aria-components plugin. When using Tailwind v4, add it to your CSS:
@import "tailwindcss";
@plugin "tailwindcss-react-aria-components";
When using Tailwind v3, add the plugin to your tailwind.config.js instead:
module.exports = {
// ...
plugins: [
require('tailwindcss-react-aria-components')
]
};
Note: When using Tailwind v3, install tailwindcss-react-aria-components version 1.x instead of 2.x.
<ExampleCard url="../Tree.html" title="Tree" description="A tree provides users with a way to navigate nested hierarchical information, with support for keyboard navigation and selection."> <Tree /> </ExampleCard>
</section>