diff --git a/packages/react/src/App.tsx b/packages/react/src/App.tsx index 78fd287..8f82b37 100644 --- a/packages/react/src/App.tsx +++ b/packages/react/src/App.tsx @@ -11,46 +11,8 @@ import DocsLayout from "./DocsLayout"; import ErrorBoundary from "./ErrorHandler"; import DynamicLayout from "./DynamicLayout"; -import DocsIntroduction, { - tableOfContents as docsIntroductionToc, -} from "./docs/docs/introduction.mdx"; -import DocsInstallation, { - tableOfContents as docsInstallationToc, -} from "./docs/docs/installation.mdx"; - -import ComponentsButton, { - tableOfContents as componentsButtonToc, -} from "./docs/components/Button.mdx"; -import ComponentsCheckbox, { - tableOfContents as componentsCheckboxToc, -} from "./docs/components/Checkbox.mdx"; -import ComponentsDialog, { - tableOfContents as componentsDialogToc, -} from "./docs/components/Dialog.mdx"; -import ComponentsDrawer, { - tableOfContents as componentsDrawerToc, -} from "./docs/components/Drawer.mdx"; -import ComponentsInput, { - tableOfContents as componentsInputToc, -} from "./docs/components/Input.mdx"; -import ComponentsLabel, { - tableOfContents as componentsLabelToc, -} from "./docs/components/Label.mdx"; -import ComponentsSwitch, { - tableOfContents as componentsSwitchToc, -} from "./docs/components/Switch.mdx"; -import ComponentsTabs, { - tableOfContents as componentsTabsToc, -} from "./docs/components/Tabs.mdx"; -import ComponentsToast, { - tableOfContents as componentsToastToc, -} from "./docs/components/Toast.mdx"; -import ComponentsTooltip, { - tableOfContents as componentsTooltipToc, -} from "./docs/components/Tooltip.mdx"; - -import { ForwardedRef, forwardRef, useContext, useEffect, useRef } from "react"; import { HeadingContext } from "./HeadingContext"; +import { ForwardedRef, forwardRef, useContext, useEffect, useRef } from "react"; function buildThresholdList() { let thresholds = []; @@ -134,121 +96,48 @@ const overrideComponents = { h6: forwardRef(HashedHeaders("h6")), }; +const docsModules = import.meta.glob("./docs/**/*.mdx"); + +const routes = Object.keys(docsModules).map((path) => { + const sfPath = path.replace("./docs", "").replace(".mdx", ""); + + return ( + { + const { default: C, tableOfContents } = await import( + `./docs${sfPath}.mdx` + ); + return { + Component: () => ( + + + + ), + }; + }} + /> + ); +}); + const router = createBrowserRouter( createRoutesFromElements( } errorElement={}> } /> }> - - - } + path="components" + loader={() => redirect("/docs/components/button")} /> - - - - } - /> - - redirect("/docs/components/button")} /> - - - - } - /> - - - - } - /> - - - - } - /> - - - - } - /> - - - - } - /> - - - - } - /> - - - - } - /> - - - - } - /> - - - - } - /> - - - - } - /> - + {routes} ) ); function App() { - return ( - <> - - - ); + return ; } export default App; diff --git a/packages/react/src/RouteObject.ts b/packages/react/src/RouteObject.ts index f65a58b..d4c3556 100644 --- a/packages/react/src/RouteObject.ts +++ b/packages/react/src/RouteObject.ts @@ -1,86 +1,39 @@ -export default { - mainNav: [ - { - path: "/docs", - name: "Docs", - eq: (pathname: string) => pathname.startsWith("/docs") && !pathname.startsWith("/docs/components") - }, - { - path: "/docs/components", - name: "Components", - eq: (pathname: string) => pathname.startsWith("/docs/components") - }, - { - path: "https://github.com/p-sw/ui", - name: "Github", - eq: () => false - } - ], - sideNav: { - "Documents": [ - { - path: "/docs", - name: "Introduction", - eq: (pathname: string) => pathname === "/docs" - }, - { - path: "/docs/installation", - name: "Installation", - eq: (pathname: string) => pathname === "/docs/installation" - } - ], - "Components": [ - { - path: "/docs/components/button", - name: "Button", - eq: (pathname: string) => pathname === "/docs/components/button" - }, - { - path: "/docs/components/checkbox", - name: "Checkbox", - eq: (pathname: string) => pathname === "/docs/components/checkbox" - }, - { - path: "/docs/components/dialog", - name: "Dialog", - eq: (pathname: string) => pathname === "/docs/components/dialog" - }, - { - path: "/docs/components/drawer", - name: "Drawer", - eq: (pathname: string) => pathname === "/docs/components/drawer" - }, - { - path: "/docs/components/input", - name: "Input", - eq: (pathname: string) => pathname === "/docs/components/input" - }, - { - path: "/docs/components/label", - name: "Label", - eq: (pathname: string) => pathname === "/docs/components/label" - }, - { - path: "/docs/components/switch", - name: "Switch", - eq: (pathname: string) => pathname === "/docs/components/switch" - }, - { - path: "/docs/components/tabs", - name: "Tabs", - eq: (pathname: string) => pathname === "/docs/components/tabs" - }, - { - path: "/docs/components/toast", - name: "Toast", - eq: (pathname: string) => pathname === "/docs/components/toast" - }, - { - path: "/docs/components/tooltip", - name: "Tooltip", - eq: (pathname: string) => pathname === "/docs/components/tooltip" - } - ] - } -} +const docsModules = import.meta.glob('./docs/**/*.mdx'); +const mainNav = [ + { + path: "/docs", + name: "Docs", + eq: (pathname: string) => pathname.startsWith("/docs") && !pathname.startsWith("/docs/components") + }, + { + path: "/docs/components", + name: "Components", + eq: (pathname: string) => pathname.startsWith("/docs/components") + }, + { + path: "https://github.com/p-sw/ui", + name: "Github", + eq: () => false + } +]; + +const sideNav: Record boolean })[]> = { + "Documents": [], + "Components": [] +}; + +Object.keys(docsModules).forEach((path) => { + const name = (path.split('/').pop() ?? '').replace('.mdx', ''); + const section = path.includes('/components/') ? "Components" : "Documents"; + sideNav[section].push({ + path: path.replace('./docs', '/docs').replace('.mdx', ''), + name: name.charAt(0).toUpperCase() + name.slice(1), + eq: (pathname: string) => pathname === path.replace('./docs', '/docs').replace('.mdx', '') + }); +}); + +export default { + mainNav, + sideNav +};