-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add component render to custom pages (#272)
- Loading branch information
Showing
6 changed files
with
56 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export type { ZudokuConfig } from "./config/config.js"; | ||
export type { ConfigSidebar as Sidebar } from "./config/validators/InputSidebarSchema.js"; | ||
export type { SlotletComponentProps } from "./lib/components/SlotletProvider.js"; | ||
export type { ExposedComponentProps } from "./lib/components/SlotletProvider.js"; | ||
export type { MDXImport } from "./lib/plugins/markdown/index.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/zudoku/src/lib/plugins/custom-pages/CustomPage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from "react"; | ||
import { cn } from "../../util/cn.js"; | ||
import { useExposedProps } from "../../util/useExposedProps.js"; | ||
import type { CustomPageConfig } from "./index.js"; | ||
|
||
export const CustomPage = ({ | ||
element, | ||
render, | ||
prose = true, | ||
}: Omit<CustomPageConfig, "path">) => { | ||
const slotletProps = useExposedProps(); | ||
const content = render ? React.createElement(render, slotletProps) : element; | ||
|
||
return <div className={cn(prose && "prose max-w-full")}>{content}</div>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
import type { ReactNode } from "react"; | ||
import { type ComponentType, type ReactNode } from "react"; | ||
import type { RouteObject } from "react-router-dom"; | ||
import { ProseClasses } from "../../components/Markdown.js"; | ||
import { type ExposedComponentProps } from "../../components/SlotletProvider.js"; | ||
import type { DevPortalPlugin, NavigationPlugin } from "../../core/plugins.js"; | ||
import { CustomPage } from "./CustomPage.js"; | ||
|
||
type CustomPagesConfig = Array<{ | ||
export type CustomPageConfig = { | ||
path: string; | ||
element: ReactNode; | ||
}>; | ||
prose?: boolean; | ||
element?: ReactNode; | ||
render?: ComponentType<ExposedComponentProps>; | ||
}; | ||
|
||
export const customPagesPlugin = ( | ||
config: CustomPagesConfig, | ||
config: CustomPageConfig[], | ||
): DevPortalPlugin & NavigationPlugin => { | ||
return { | ||
getRoutes: (): RouteObject[] => | ||
config.map(({ path, element }) => ({ | ||
config.map(({ path, ...props }) => ({ | ||
path, | ||
// TODO: we should componentize prose pages | ||
element: <div className={ProseClasses + " max-w-full"}>{element}</div>, | ||
element: <CustomPage {...props} />, | ||
})), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useLocation, useNavigate, useSearchParams } from "react-router-dom"; | ||
import type { ExposedComponentProps } from "../components/SlotletProvider.js"; | ||
|
||
export const useExposedProps = (): ExposedComponentProps => { | ||
const location = useLocation(); | ||
const navigate = useNavigate(); | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
|
||
return { location, navigate, searchParams, setSearchParams }; | ||
}; |