Skip to content

Commit

Permalink
UI: Support children prop in custom Folder component
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Jan 20, 2025
1 parent b52f4d0 commit 4f2538a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-cameras-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'fumadocs-ui': patch
---

Support `children` prop in custom `Folder` component
2 changes: 2 additions & 0 deletions apps/docs/source.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export default defineConfig({

return {
rehypeCodeOptions: {
lazy: true,
langs: ['ts', 'js', 'html'],
inline: 'tailing-curly-colon',
themes: {
light: 'catppuccin-latte',
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/layouts/docs/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface SidebarOptions extends SidebarProps {

export interface SidebarComponents {
Item: FC<{ item: PageTree.Item }>;
Folder: FC<{ item: PageTree.Folder; level: number }>;
Folder: FC<{ item: PageTree.Folder; level: number; children: ReactNode }>;
Separator: FC<{ item: PageTree.Separator }>;
}

Expand Down
57 changes: 32 additions & 25 deletions packages/ui/src/layouts/docs/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -396,35 +396,42 @@ export function SidebarPageTree(props: {
): ReactNode[] {
return items.map((item, i) => {
const id = `${item.type}_${i.toString()}`;
if (item.type === 'separator') {
if (Separator) return <Separator key={id} item={item} />;
return (
<SidebarSeparator key={id} className={cn(i !== 0 && 'mt-8')}>
{item.name}
</SidebarSeparator>
);
}

switch (item.type) {
case 'separator':
if (Separator) return <Separator key={id} item={item} />;
return (
<SidebarSeparator key={id} className={cn(i !== 0 && 'mt-8')}>
{item.name}
</SidebarSeparator>
);
case 'folder':
if (Folder) return <Folder key={id} item={item} level={level} />;
return (
<PageTreeFolder key={id} item={item} level={level}>
{renderSidebarList(item.children, level + 1)}
</PageTreeFolder>
);
default:
if (Item) return <Item key={item.url} item={item} />;
if (item.type === 'folder') {
const children = renderSidebarList(item.children, level + 1);

if (Folder)
return (
<SidebarItem
key={item.url}
href={item.url}
external={item.external}
icon={item.icon}
>
{item.name}
</SidebarItem>
<Folder key={id} item={item} level={level}>
{children}
</Folder>
);
return (
<PageTreeFolder key={id} item={item} level={level}>
{children}
</PageTreeFolder>
);
}

if (Item) return <Item key={item.url} item={item} />;
return (
<SidebarItem
key={item.url}
href={item.url}
external={item.external}
icon={item.icon}
>
{item.name}
</SidebarItem>
);
});
}

Expand Down

0 comments on commit 4f2538a

Please sign in to comment.