Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optionally allow the primary sections of the project sidebar be collapsible #258

Merged
merged 7 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions app/components/DocsLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
useMatches,
useNavigate,
useParams,
useRouterState,
} from '@tanstack/react-router'
import type { AnyOrama, SearchParamsFullText, AnyDocument } from '@orama/orama'
import { SearchBox, SearchButton } from '@orama/searchbox'
import { SearchBox } from '@orama/searchbox'
import { Carbon } from '~/components/Carbon'
import { Select } from '~/components/Select'
import { useLocalStorage } from '~/utils/useLocalStorage'
Expand All @@ -25,7 +24,7 @@
import type { ConfigSchema, MenuItem } from '~/utils/config'
import { create } from 'zustand'
import { searchBoxParams, searchButtonParams } from '~/components/Orama'
import { Framework, getFrameworkOptions, getLibrary } from '~/libraries'
import { Framework, getFrameworkOptions } from '~/libraries'
import { DocsCalloutQueryGG } from '~/components/DocsCalloutQueryGG'
import { DocsCalloutBytes } from '~/components/DocsCalloutBytes'
import { ClientOnlySearchButton } from './ClientOnlySearchButton'
Expand Down Expand Up @@ -179,7 +178,7 @@
config: ConfigSchema
repo: string
frameworks: Framework[]
}) => {
}): MenuItem[] => {
const currentFramework = useCurrentFramework(frameworks)

const localMenu: MenuItem = {
Expand All @@ -206,12 +205,13 @@
to: 'https://tlinz.com/discord',
},
],
collapsible: false,
}

return [
localMenu,
// Merge the two menus together based on their group labels
...config.sections.map((section) => {
...config.sections.map((section): MenuItem | undefined => {
const frameworkDocs = section.frameworks?.find(
(f) => f.label === currentFramework.framework
)
Expand All @@ -232,9 +232,10 @@
return {
label: section.label,
children,
collapsible: section?.collapsible ?? false,
}
}),
].filter(Boolean)
].filter((item) => item !== undefined)
}

const useFrameworkConfig = ({ frameworks }: { frameworks: Framework[] }) => {
Expand Down Expand Up @@ -317,8 +318,7 @@
children,
}: DocsLayoutProps) {
const { libraryId } = useParams({
strict: false,
experimental_returnIntersection: true,
from: '/$libraryId/$version/docs',
})
const frameworkConfig = useFrameworkConfig({ frameworks })
const versionConfig = useVersionConfig({ versions })
Expand Down Expand Up @@ -350,16 +350,25 @@
const [showBytes, setShowBytes] = useLocalStorage('showBytes', true)

const menuItems = menuConfig.map((group, i) => {
const WrapperComp = group.collapsible ? 'details' : 'div'
const LabelComp = group.collapsible ? 'summary' : 'div'

return (
<div key={i}>
<div className="text-[.8em] uppercase font-black">{group?.label}</div>
<WrapperComp
key={i}
className="[&>summary]:before:mr-[0.4rem] [&>summary]:marker:text-[0.8em] [&>summary]:marker:-ml-[0.3rem] [&>summary]:marker:leading-4 [&>div.ts-sidebar-label]:ml-[1rem] relative select-none"
open
>
<LabelComp className="text-[.8em] uppercase font-black leading-4 ts-sidebar-label">
{group?.label}
</LabelComp>
<div className="h-2" />
<div className="ml-2 text-[.85em]">
<ul className="ml-2 text-[.85em] list-none">
{group?.children?.map((child, i) => {
const linkClasses = `cursor-pointer flex gap-2 items-center justify-between group px-2 py-[.1rem] rounded-lg hover:bg-gray-500 hover:bg-opacity-10`

return (
<React.Fragment key={i}>
<li key={i}>
{child.to.startsWith('http') ? (
<a href={child.to} className={linkClasses}>
{child.label}
Expand Down Expand Up @@ -423,11 +432,11 @@
}}
</Link>
)}
</React.Fragment>
</li>
)
})}
</div>
</div>
</ul>
</WrapperComp>
)
})

Expand Down Expand Up @@ -610,7 +619,7 @@
key={partner.name}
className="overflow-hidden hover:bg-gray-500/10 dark:hover:bg-gray-500/10 transition-colors"
>
<a

Check warning on line 622 in app/components/DocsLayout.tsx

View workflow job for this annotation

GitHub Actions / PR

Using target="_blank" without rel="noreferrer" (which implies rel="noopener") is a security risk in older browsers: see https://mathiasbynens.github.io/rel-noopener/#recommendations
href={partner.href}
target="_blank"
className="px-4 flex items-center justify-center cursor-pointer"
Expand Down
2 changes: 2 additions & 0 deletions app/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type MenuItem = {
to: string
badge?: string
}[]
collapsible: boolean
}

const configSchema = z.object({
Expand Down Expand Up @@ -36,6 +37,7 @@ const configSchema = z.object({
})
)
.optional(),
collapsible: z.boolean().optional(),
})
),
users: z.array(z.string()).optional(),
Expand Down
5 changes: 5 additions & 0 deletions tanstack-docs-config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
"required": ["label", "children"],
"additionalProperties": false
}
},
"collapsible": {
"type": "boolean",
"default": false,
"description": "Whether the section should be collapsible."
}
}
}
Expand Down
Loading