Skip to content

Commit acc0e8a

Browse files
committed
Pass theme to mini-browser
1 parent 21b32dd commit acc0e8a

File tree

13 files changed

+79
-14
lines changed

13 files changed

+79
-14
lines changed

packages/mdx/src/client/preview.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
SandboxInfo,
77
} from "@codesandbox/sandpack-client"
88
import { EditorStep } from "@code-hike/mini-editor"
9+
import { EditorTheme } from "@code-hike/utils"
910

1011
export type PresetConfig = SandboxInfo
1112
export function Preview({
@@ -14,13 +15,15 @@ export function Preview({
1415
presetConfig,
1516
show,
1617
children,
18+
codeConfig,
1719
...rest
1820
}: {
1921
className: string
2022
files: EditorStep["files"]
2123
presetConfig?: PresetConfig
2224
show?: string
2325
children?: React.ReactNode
26+
codeConfig: { theme: EditorTheme }
2427
}) {
2528
return (
2629
<div
@@ -30,11 +33,16 @@ export function Preview({
3033
{...rest}
3134
>
3235
{!presetConfig ? (
33-
<MiniBrowser loadUrl={show} children={children} />
36+
<MiniBrowser
37+
loadUrl={show}
38+
children={children}
39+
theme={codeConfig.theme}
40+
/>
3441
) : (
3542
<SandpackPreview
3643
files={files}
3744
presetConfig={presetConfig}
45+
codeConfig={codeConfig}
3846
/>
3947
)}
4048
</div>
@@ -44,9 +52,11 @@ export function Preview({
4452
function SandpackPreview({
4553
files,
4654
presetConfig,
55+
codeConfig,
4756
}: {
4857
files: EditorStep["files"]
4958
presetConfig: PresetConfig
59+
codeConfig: { theme: EditorTheme }
5060
}) {
5161
const iframeRef = React.useRef<HTMLIFrameElement>(null!)
5262
const clientRef = React.useRef<SandpackClient>(null!)
@@ -76,7 +86,8 @@ function SandpackPreview({
7686
}, [files])
7787

7888
return (
79-
<MiniBrowser>
89+
// TODO only render iframe here, leave MiniBrowser for the parent component
90+
<MiniBrowser theme={codeConfig.theme}>
8091
<iframe ref={iframeRef} />
8192
</MiniBrowser>
8293
)

packages/mdx/src/client/scrollycoding.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export function Scrollycoding({
8080
className="ch-scrollycoding-preview"
8181
files={tab.files}
8282
presetConfig={presetConfig}
83+
codeConfig={codeConfig}
8384
/>
8485
)}
8586
</div>

packages/mdx/src/client/slideshow.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export function Slideshow({
6060
className="ch-slideshow-preview"
6161
files={tab.files}
6262
presetConfig={presetConfig}
63+
codeConfig={codeConfig}
6364
/>
6465
)}
6566
</div>

packages/mdx/src/client/spotlight.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export function Spotlight({
7979
className="ch-spotlight-preview"
8080
files={tab.files}
8181
presetConfig={presetConfig}
82+
codeConfig={codeConfig}
8283
/>
8384
)}
8485
</div>

packages/mdx/src/plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import visit from "unist-util-visit"
88
import { transformSlideshows } from "./plugin/slideshow"
99
import { valueToEstree } from "./plugin/to-estree"
1010
import { CH_CODE_CONFIG_VAR_NAME } from "./plugin/unist-utils"
11+
import { transformPreviews } from "./plugin/preview"
1112

1213
type CodeHikeConfig = {
1314
theme: any
@@ -35,6 +36,7 @@ export function remarkCodeHike(config: CodeHikeConfig) {
3536
}
3637

3738
try {
39+
await transformPreviews(tree)
3840
await transformScrollycodings(tree, config)
3941
await transformSpotlights(tree, config)
4042
await transformSlideshows(tree, config)

packages/mdx/src/plugin/preview.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import fetch from "node-fetch"
2+
import {
3+
visitAsync,
4+
toJSX,
5+
CH_CODE_CONFIG_PLACEHOLDER,
6+
} from "./unist-utils"
7+
import { Node } from "unist"
28

39
export async function getPresetConfig(
410
attributes?: {
@@ -18,3 +24,24 @@ export async function getPresetConfig(
1824
const res = await fetch(configUrl)
1925
return await res.json()
2026
}
27+
28+
export async function transformPreviews(tree: Node) {
29+
await visitAsync(
30+
tree,
31+
"mdxJsxFlowElement",
32+
async node => {
33+
if (node.name === "CH.Preview") {
34+
await transformPreview(node)
35+
}
36+
}
37+
)
38+
}
39+
40+
async function transformPreview(node: Node) {
41+
toJSX(node, {
42+
props: {
43+
codeConfig: CH_CODE_CONFIG_PLACEHOLDER,
44+
},
45+
appendProps: true,
46+
})
47+
}

packages/mini-browser/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"dependencies": {
2525
"@code-hike/classer": "^0.3.0-next.0",
2626
"@code-hike/mini-frame": "^0.3.0-next.0",
27+
"@code-hike/utils": "^0.3.0-next.0",
2728
"use-spring": "^0.2.3"
2829
},
2930
"peerDependencies": {

packages/mini-browser/src/mini-browser-hike.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { MiniFrame } from "@code-hike/mini-frame"
33
import { TitleBar } from "./title-bar"
44
import { MiniBrowserStep, useSteps } from "./use-steps"
55
import { useClasser, Classes } from "@code-hike/classer"
6+
import { EditorTheme } from "@code-hike/utils"
67

78
type Transition = "none" | "slide"
89

@@ -12,6 +13,7 @@ export type MiniBrowserHikeProps = {
1213
classes?: Classes
1314
steps?: MiniBrowserStep[]
1415
transition?: Transition
16+
theme: EditorTheme
1517
} & React.PropsWithoutRef<JSX.IntrinsicElements["div"]>
1618

1719
const MiniBrowserHike = React.forwardRef<
@@ -26,6 +28,7 @@ function MiniBrowserWithRef(
2628
steps: ogSteps,
2729
transition = "none",
2830
classes,
31+
theme,
2932
...props
3033
}: MiniBrowserHikeProps,
3134
ref: React.Ref<HTMLIFrameElement>
@@ -50,8 +53,13 @@ function MiniBrowserWithRef(
5053
}}
5154
classes={classes}
5255
titleBar={
53-
<TitleBar url={displayUrl!} linkUrl={loadUrl!} />
56+
<TitleBar
57+
url={displayUrl!}
58+
linkUrl={loadUrl!}
59+
theme={theme}
60+
/>
5461
}
62+
theme={theme}
5563
>
5664
{children || <iframe ref={ref} src={loadUrl} />}
5765
</MiniFrame>

packages/mini-browser/src/title-bar.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import React from "react"
22
import { Back, Forward, Open } from "./buttons"
33
import { FrameButtons } from "@code-hike/mini-frame"
4+
import { EditorTheme } from "@code-hike/utils"
45

56
export { TitleBar }
67

78
function TitleBar({
89
url,
910
linkUrl,
11+
theme,
1012
}: {
1113
url: string
1214
linkUrl: string
15+
theme: EditorTheme
1316
}) {
1417
return (
1518
<>

packages/mini-frame/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"url": "https://opencollective.com/codehike"
3535
},
3636
"dependencies": {
37-
"@code-hike/classer": "^0.3.0-next.0"
37+
"@code-hike/classer": "^0.3.0-next.0",
38+
"@code-hike/utils": "^0.3.0-next.0"
3839
}
3940
}

packages/mini-frame/src/mini-frame.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ import {
44
useClasser,
55
Classes,
66
} from "@code-hike/classer"
7+
import {
8+
EditorTheme,
9+
getColor,
10+
ColorName,
11+
} from "@code-hike/utils"
712

813
type MiniFrameProps = {
914
title?: string
1015
titleBar?: React.ReactNode
1116
zoom?: number
1217
classes?: Classes
1318
overflow?: string
19+
theme: EditorTheme
1420
} & React.PropsWithoutRef<JSX.IntrinsicElements["div"]>
1521

1622
export const MiniFrame = React.forwardRef<
@@ -22,6 +28,7 @@ export const MiniFrame = React.forwardRef<
2228
children,
2329
titleBar,
2430
classes,
31+
theme,
2532
zoom = 1,
2633
overflow,
2734
...props
@@ -40,7 +47,17 @@ export const MiniFrame = React.forwardRef<
4047
<ClasserProvider classes={classes}>
4148
<div {...props} ref={ref}>
4249
<div className={c("")}>
43-
<div className={c("title-bar")}>{bar}</div>
50+
<div
51+
className={c("title-bar")}
52+
style={{
53+
background: getColor(
54+
theme,
55+
ColorName.EditorGroupHeaderBackground
56+
),
57+
}}
58+
>
59+
{bar}
60+
</div>
4461
<div className={c("content")}>
4562
<div className={c("zoom")} style={zoomStyle}>
4663
{children}

packages/playground/content/simple-code.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,3 @@ function lorem(ipsum, dolor = 1) {
8484
: []
8585
}
8686
```
87-
88-
<CH.Preview show="/" style={{ height: 200 }} />

packages/playground/content/test.mdx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
# Hello
22

3-
export const pi = { foo: 1 }
4-
5-
<pre>{JSON.stringify(chCodeConfig, null, 2)}</pre>
6-
7-
```js
8-
console.log(1)
9-
```
3+
<CH.Preview show="/" style={{ height: 200 }} />

0 commit comments

Comments
 (0)