Skip to content

Commit 16fc9aa

Browse files
Merge pull request #175 from edewit/scope-override
add scope to config to update live example
2 parents e8cabb8 + cfae887 commit 16fc9aa

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

cli/getConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface DocsConfig {
1717
outputDir: string;
1818
propsGlobs: PropsGlobs[];
1919
repoRoot?: string;
20+
scope?: Record<string, any>;
2021
}
2122

2223
export async function getConfig(fileLocation: string): Promise<DocsConfig | undefined> {

cli/templates/pf-docs.config.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ export const config = {
2929
// ],
3030
// },
3131
],
32+
// Add custom scope items for LiveExample component
33+
// These will be available in your example code blocks
34+
// Example:
35+
// scope: {
36+
// MyCustomComponent: () => <div>Custom</div>,
37+
// myUtilFunction: (x) => x * 2,
38+
// },
3239
}

pf-docs.config.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ export const config = {
3030
],
3131
},
3232
],
33+
// Add custom scope items for LiveExample component
34+
// These will be available in your example code blocks
35+
// Example:
36+
// scope: {
37+
// MyCustomComponent: () => <div>Custom</div>,
38+
// myUtilFunction: (x) => x * 2,
39+
// },
3340
}

src/components/LiveExample.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
import { LiveExample as LiveExampleBase } from './LiveExample'
3+
import { config } from '../pf-docs.config.mjs'
34
45
const { src, html } = Astro.props
56
---
67

7-
<LiveExampleBase src={src} html={html} client:only="react" />
8+
<LiveExampleBase src={src} html={html} customScope={config.scope} client:only="react" />

src/components/LiveExample.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface LiveExampleProps {
1919
src?: string
2020
html?: string
2121
isFullscreenPreview?: boolean
22+
customScope?: Record<string, any>
2223
}
2324

2425
function fallbackRender({ error }: any) {
@@ -30,14 +31,15 @@ function fallbackRender({ error }: any) {
3031
)
3132
}
3233

33-
function getLivePreview(editorCode: string) {
34+
function getLivePreview(editorCode: string, customScope?: Record<string, any>) {
3435
const scope = {
3536
...reactCoreModule,
3637
...reactIconsModule,
3738
...reactDragDropModule,
3839
styles,
3940
...reactTokensModule,
4041
...{ useState, Fragment, useRef, useEffect, createRef, useReducer },
42+
...customScope,
4143
}
4244
const { code: transformedCode } = convertToReactComponent(editorCode)
4345

@@ -58,6 +60,7 @@ export const LiveExample = ({
5860
src,
5961
html,
6062
isFullscreenPreview,
63+
customScope,
6164
}: LiveExampleProps) => {
6265
const inputCode = src || html || ''
6366
const [code, setCode] = useState(inputCode)
@@ -73,7 +76,7 @@ export const LiveExample = ({
7376
)
7477
lang = 'html'
7578
} else {
76-
livePreview = getLivePreview(code)
79+
livePreview = getLivePreview(code, customScope)
7780
lang = 'ts'
7881
}
7982

src/pf-docs.config.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@ export const config = {
1717
// },
1818
],
1919
outputDir: "./dist",
20-
navSectionOrder: ["get-started", "design-foundations"]
20+
navSectionOrder: ["get-started", "design-foundations"],
21+
// Add custom scope items for LiveExample component
22+
// These will be available in your example code blocks
23+
// Example:
24+
// scope: {
25+
// MyCustomComponent: () => <div>Custom</div>,
26+
// myUtilFunction: (x) => x * 2,
27+
// },
2128
};

0 commit comments

Comments
 (0)