Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions cli/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface DocsConfig {
outputDir: string;
propsGlobs: PropsGlobs[];
repoRoot?: string;
scope?: Record<string, any>;
}

export async function getConfig(fileLocation: string): Promise<DocsConfig | undefined> {
Expand Down
7 changes: 7 additions & 0 deletions cli/templates/pf-docs.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ export const config = {
// ],
// },
],
// Add custom scope items for LiveExample component
// These will be available in your example code blocks
// Example:
// scope: {
// MyCustomComponent: () => <div>Custom</div>,
// myUtilFunction: (x) => x * 2,
// },
}
7 changes: 7 additions & 0 deletions pf-docs.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ export const config = {
],
},
],
// Add custom scope items for LiveExample component
// These will be available in your example code blocks
// Example:
// scope: {
// MyCustomComponent: () => <div>Custom</div>,
// myUtilFunction: (x) => x * 2,
// },
}
3 changes: 2 additions & 1 deletion src/components/LiveExample.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
import { LiveExample as LiveExampleBase } from './LiveExample'
import { config } from '../pf-docs.config.mjs'

const { src, html } = Astro.props
---

<LiveExampleBase src={src} html={html} client:only="react" />
<LiveExampleBase src={src} html={html} customScope={config.scope} client:only="react" />
7 changes: 5 additions & 2 deletions src/components/LiveExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface LiveExampleProps {
src?: string
html?: string
isFullscreenPreview?: boolean
customScope?: Record<string, any>
}

function fallbackRender({ error }: any) {
Expand All @@ -30,14 +31,15 @@ function fallbackRender({ error }: any) {
)
}

function getLivePreview(editorCode: string) {
function getLivePreview(editorCode: string, customScope?: Record<string, any>) {
const scope = {
...reactCoreModule,
...reactIconsModule,
...reactDragDropModule,
styles,
...reactTokensModule,
...{ useState, Fragment, useRef, useEffect, createRef, useReducer },
...customScope,
}
const { code: transformedCode } = convertToReactComponent(editorCode)

Expand All @@ -58,6 +60,7 @@ export const LiveExample = ({
src,
html,
isFullscreenPreview,
customScope,
}: LiveExampleProps) => {
const inputCode = src || html || ''
const [code, setCode] = useState(inputCode)
Expand All @@ -73,7 +76,7 @@ export const LiveExample = ({
)
lang = 'html'
} else {
livePreview = getLivePreview(code)
livePreview = getLivePreview(code, customScope)
lang = 'ts'
}

Expand Down
9 changes: 8 additions & 1 deletion src/pf-docs.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@ export const config = {
// },
],
outputDir: "./dist",
navSectionOrder: ["get-started", "design-foundations"]
navSectionOrder: ["get-started", "design-foundations"],
// Add custom scope items for LiveExample component
// These will be available in your example code blocks
// Example:
// scope: {
// MyCustomComponent: () => <div>Custom</div>,
// myUtilFunction: (x) => x * 2,
// },
};