Skip to content

Commit

Permalink
[Design System] Chore: add tree-view documentation (supabase#26821)
Browse files Browse the repository at this point in the history
* chore: init docs

* remove old stories

* update source panels
  • Loading branch information
MildTomato committed May 28, 2024
1 parent 33abcaa commit 73ff6c1
Show file tree
Hide file tree
Showing 15 changed files with 662 additions and 487 deletions.
44 changes: 44 additions & 0 deletions apps/design-system/__registry__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,50 @@ export const Index: Record<string, any> = {
subcategory: "undefined",
chunks: []
},
"tree-view-demo": {
name: "tree-view-demo",
type: "components:example",
registryDependencies: undefined,
component: React.lazy(() => import("@/registry/default/example/tree-view-demo")),
source: "",
files: ["registry/default/example/tree-view-demo.tsx"],
category: "undefined",
subcategory: "undefined",
chunks: []
},
"tree-view-edit": {
name: "tree-view-edit",
type: "components:example",
registryDependencies: undefined,
component: React.lazy(() => import("@/registry/default/example/tree-view-edit")),
source: "",
files: ["registry/default/example/tree-view-edit.tsx"],
category: "undefined",
subcategory: "undefined",
chunks: []
},
"tree-view-directories": {
name: "tree-view-directories",
type: "components:example",
registryDependencies: undefined,
component: React.lazy(() => import("@/registry/default/example/tree-view-directories")),
source: "",
files: ["registry/default/example/tree-view-directories.tsx"],
category: "undefined",
subcategory: "undefined",
chunks: []
},
"tree-view-multi-select": {
name: "tree-view-multi-select",
type: "components:example",
registryDependencies: undefined,
component: React.lazy(() => import("@/registry/default/example/tree-view-multi-select")),
source: "",
files: ["registry/default/example/tree-view-multi-select.tsx"],
category: "undefined",
subcategory: "undefined",
chunks: []
},
"color-usage-surface-www-and-docs": {
name: "color-usage-surface-www-and-docs",
type: "docs:example",
Expand Down
85 changes: 74 additions & 11 deletions apps/design-system/components/source-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import Image from 'next/image'

const SourcePanel = forwardRef<HTMLDivElement, React.HTMLProps<HTMLDivElement> & { doc: Doc }>(
({ doc, children, ...props }, ref) => {
const label = {
radix: 'Radix UI',
vaul: 'Vaul',
shadcn: 'ui.shadcn',
}

const ShadcnPanel = () => {
if (doc.source?.shadcn) {
return (
Expand Down Expand Up @@ -79,16 +73,84 @@ const SourcePanel = forwardRef<HTMLDivElement, React.HTMLProps<HTMLDivElement> &
alt="Vaul"
className="h-6 w-6 rounded-full"
/>
<span className="hidden font-bold sm:inline-block">vaul</span>
<span className="hidden font-bold text-xs sm:inline-block">vaul</span>
</div>
<span className="text-foreground-light text-sm">
<span className="text-foreground-light text-xs">
This component is based on vaul by emilkowalski
</span>
</div>
)
}
}

const ReactAccesibleTreeViewPanel = () => {
if (doc.source?.reactAccessibleTreeview) {
return (
<div
className={cn(
'bg-surface-75/50 border flex items-center p-3 px-5 gap-6 first:rounded-t-md last:rounded-b-md justify-between',
props.className
)}
{...props}
>
<div className="flex items-center gap-2">
<Image
width={24}
height={24}
src="https://avatars.githubusercontent.com/u/14020024?v=4"
alt="dgreene1"
className="h-6 w-6 rounded-full"
/>
<span className="hidden text-xs font-bold sm:inline-block">
react-accessible-treeview
</span>
<span className="text-foreground-light text-xs">
Component based on react-accessible-treeview by dgreene1
</span>
</div>
{doc.links ? (
<div className="flex items-center gap-2 justify-end">
{doc.links?.doc && (
<Button
type="outline"
className="rounded-full"
icon={<ExternalLink className="text-foreground-muted" strokeWidth={1} />}
>
<Link
href={doc.links.doc}
target="_blank"
rel="noreferrer"

// className={cn(buttonVariants({ variant: 'default' }), 'gap-1')}
>
Docs
</Link>
</Button>
)}
{doc.links?.api && (
<Button
type="outline"
className="rounded-full"
icon={<ExternalLink className="text-foreground-muted" strokeWidth={1} />}
>
<Link
href={doc.links.api}
target="_blank"
rel="noreferrer"

// className={cn(badgeVariants({ variant: 'default' }), 'gap-1')}
>
API Reference
</Link>
</Button>
)}
</div>
) : null}
</div>
)
}
}

const InputOtp = () => {
if (doc.source?.inputOtp) {
return (
Expand All @@ -107,9 +169,9 @@ const SourcePanel = forwardRef<HTMLDivElement, React.HTMLProps<HTMLDivElement> &
alt="inputOtp"
className="h-6 w-6 rounded-full"
/>
<span className="hidden font-bold sm:inline-block">input-otp</span>
<span className="hidden font-bold text-xs sm:inline-block">input-otp</span>
</div>
<span className="text-foreground-light text-sm">
<span className="text-foreground-light text-xs">
This component is based on input-otp by guilhermerodz
</span>
</div>
Expand Down Expand Up @@ -145,7 +207,7 @@ const SourcePanel = forwardRef<HTMLDivElement, React.HTMLProps<HTMLDivElement> &
<path d="M11.5 7C13.433 7 15 5.433 15 3.5C15 1.567 13.433 0 11.5 0C9.56704 0 8 1.567 8 3.5C8 5.433 9.56704 7 11.5 7Z"></path>
</svg>
<div className="flex flex-row items-center justify-between text-sm w-full">
<span className="text-foreground-light text-sm">This component uses Radix UI</span>
<span className="text-foreground-light text-xs">This component uses Radix UI</span>
{doc.links ? (
<div className="flex items-center gap-2 justify-end">
{doc.links?.doc && (
Expand Down Expand Up @@ -195,6 +257,7 @@ const SourcePanel = forwardRef<HTMLDivElement, React.HTMLProps<HTMLDivElement> &
<RadixPanel />
<VaulPanel />
<InputOtp />
<ReactAccesibleTreeViewPanel />
{/* <ShadcnPanel /> */}
</div>
)
Expand Down
5 changes: 5 additions & 0 deletions apps/design-system/config/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ export const docsConfig: DocsConfig = {
href: '/docs/components/tooltip',
items: [],
},
{
title: 'Tree View',
href: '/docs/components/tree-view',
items: [],
},
],
},
],
Expand Down
121 changes: 121 additions & 0 deletions apps/design-system/content/docs/components/tree-view.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
---
title: Tree View
description: A tree view that assembles all the functionalities of the Accordion component to create a tree view.
component: true
links:
doc: https://dgreene1.github.io/react-accessible-treeview/
api: https://dgreene1.github.io/react-accessible-treeview/docs/api
source:
reactAccessibleTreeview: true
---

<ComponentPreview name="tree-view-demo" peekCode wide />

## Usage

```tsx
import { TreeView, TreeViewItem } from 'ui'
import { flattenTree } from 'react-accessible-treeview'
```

```tsx
const data: {
name: ''
children: [
{
name: 'Current batch'
},
{
name: 'Older queries'
},
{
name: 'query all users'
},
{
name: 'users in last day'
},
{
name: 'new users over time'
},
]
}

function App() {
return (
<TreeView
{...args}
data={flattenTree(args.data)}
aria-label="directory tree"
nodeRenderer={({ element, isBranch, isExpanded, getNodeProps, level, isSelected }) => (
<TreeViewItem
isExpanded={isExpanded}
isBranch={isBranch}
isSelected={isSelected}
level={level}
xPadding={16}
name={element.name}
{...getNodeProps()}
/>
)}
/>
)
}
```

## Examples

### With directories

Use a `children` node in your data object to create a tree view with directories.

```tsx {9-16}
const data = {
//..
children: [
{
name: 'Current batch',
},
{
name: 'Older queries',
children: [
{
name: 'all countries',
},
{
name: 'add new countries',
},
],
},
],
}
```

<ComponentPreview name="tree-view-directories" />

### With inline editing

The Tree View can be edited in the app to enable different types of inline editing.

In this example, right click on any item to rename it, and we traverse the data object setting `isEditing` to true.
`TreeViewItem` then accepts a `isEditing` prop to show and focus an inline input to edit.

<ComponentPreview name="tree-view-edit" />

### With multi selection

Use the `multiSelect`, `togglableSelect`, `clickAction` set to "EXCLUSIVE_SELECT" to enable a multi-select tree view.

An example can be seen on the [react-accessible-treeview docs](https://dgreene1.github.io/react-accessible-treeview/docs/examples-MultiSelectDirectoryTree).

```tsx {4-6}
<TreeView
data={flattenTree(args.data)}
aria-label="directory tree"
multiSelect
togglableSelect
clickAction="EXCLUSIVE_SELECT"
// other options..
/>
```

<ComponentPreview name="tree-view-multi-select" />
3 changes: 3 additions & 0 deletions apps/design-system/contentlayer.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const NestedProperties = defineNestedType(() => ({
inputOtp: {
type: 'boolean',
},
reactAccessibleTreeview: {
type: 'boolean',
},
},
}))

Expand Down
44 changes: 44 additions & 0 deletions apps/design-system/registry/default/example/tree-view-demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { TreeView, TreeViewItem } from 'ui'
import { flattenTree } from 'react-accessible-treeview'

export default function TreeViewDemo() {
const data = {
name: '',
children: [
{
name: 'Current batch',
},
{
name: 'Older queries',
},
{
name: 'query all users',
},
{
name: 'users in last day',
},
{
name: 'new users over time',
},
],
}

return (
<TreeView
data={flattenTree(data)}
aria-label="directory tree"
className="w-[420px]"
nodeRenderer={({ element, isBranch, isExpanded, getNodeProps, level, isSelected }) => (
<TreeViewItem
isExpanded={isExpanded}
isBranch={isBranch}
isSelected={isSelected}
level={level}
xPadding={16}
name={element.name}
{...getNodeProps()}
/>
)}
/>
)
}
Loading

0 comments on commit 73ff6c1

Please sign in to comment.