Skip to content

Commit c6c44ff

Browse files
committed
Refactor mdx-client props
1 parent 46662f3 commit c6c44ff

File tree

17 files changed

+393
-228
lines changed

17 files changed

+393
-228
lines changed

packages/mdx/dev/content/rows.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ console.log(7)
6666

6767
</CH.Code>
6868

69-
<CH.Code rows={[2, "focus"]} lineNumbers >
69+
<CH.Code rows={[2, "focus"]} lineNumbers showExpandButton>
7070

7171
```js foo.js
7272
console.log(2)

packages/mdx/dev/content/test.mdx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
<CH.Scrollycoding rows={10} lineNumbers>
2+
3+
<CH.Code rows={3}>
4+
15
```js
2-
// mark(1)
3-
function foobarloremipsumfoobarloremipsumsitametfoobarloremipsumfoobarloremipsumsitamet() {
4-
// box[16:20] red
5-
console.log("hover me")
6-
// mark[9:12]
7-
return 8
8-
}
6+
console.log(1)
97
```
8+
9+
</CH.Code>
10+
11+
Hello 1
12+
13+
</CH.Scrollycoding>

packages/mdx/src/core/types.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Theme } from "@code-hike/lighter"
2+
import type { CodeStep } from "../smooth-code"
23

34
type TriggerPosition = `${number}px` | `${number}%`
45

@@ -25,6 +26,10 @@ export type GlobalConfig = {
2526
showCopyButton?: boolean
2627
staticMediaQuery?: string
2728
triggerPosition?: TriggerPosition
29+
30+
minZoom?: number
31+
maxZoom?: number
32+
horizontalCenter?: boolean
2833
}
2934

3035
export function toGlobalConfig({
@@ -63,3 +68,36 @@ export type CodeSettings = {
6368
rows?: number | "focus" | (number | "focus")[]
6469
debug?: boolean
6570
}
71+
72+
export type ElementProps = {
73+
style?: React.CSSProperties
74+
className?: string
75+
}
76+
77+
export type CodeConfigProps = {
78+
rows?: number | "focus" | (number | "focus")[]
79+
showCopyButton?: boolean
80+
showExpandButton?: boolean
81+
lineNumbers?: boolean
82+
83+
minZoom?: number
84+
maxZoom?: number
85+
horizontalCenter?: boolean
86+
}
87+
88+
type EditorPanel = {
89+
tabs: string[]
90+
active: string
91+
heightRatio: number
92+
}
93+
94+
export type EditorStep = {
95+
files: CodeFile[]
96+
northPanel: EditorPanel
97+
southPanel?: EditorPanel
98+
terminal?: string
99+
}
100+
101+
export type CodeFile = CodeStep & {
102+
name: string
103+
}

packages/mdx/src/mdx-client/code.tsx

Lines changed: 76 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,128 @@
11
import React from "react"
22
import { CodeSpring } from "../smooth-code"
3+
import { EditorSpring, EditorStep } from "../mini-editor"
34
import {
4-
EditorSpring,
5-
EditorProps,
6-
EditorStep,
7-
} from "../mini-editor"
8-
import { CodeSettings, GlobalConfig } from "../core/types"
5+
CodeConfigProps,
6+
ElementProps,
7+
GlobalConfig,
8+
} from "../core/types"
99

10-
export function Code(props: EditorProps & GlobalConfig) {
11-
const [step, setStep] = React.useState(props)
10+
type Props = {
11+
editorStep: EditorStep
12+
globalConfig: GlobalConfig
13+
} & ElementProps &
14+
CodeConfigProps
15+
16+
export function Code(props: Props) {
17+
const { editorStep, globalConfig, ...codeConfigProps } =
18+
props
19+
const [step, setStep] = React.useState(editorStep)
1220

1321
function onTabClick(filename: string) {
14-
const newStep = updateEditorStep(step, filename, null)
22+
const newStep = updateEditorStep(
23+
props.editorStep,
24+
filename,
25+
null
26+
)
1527
setStep({ ...step, ...newStep })
1628
}
1729

18-
return <InnerCode {...step} onTabClick={onTabClick} />
30+
return (
31+
<InnerCode
32+
editorStep={step}
33+
onTabClick={onTabClick}
34+
globalConfig={globalConfig}
35+
codeConfigProps={codeConfigProps}
36+
/>
37+
)
1938
}
2039

21-
// build the CodeConfig from props and props.codeConfig
22-
export function mergeCodeConfig<T>(
23-
props: Partial<CodeSettings> & {
24-
codeConfig: Partial<CodeSettings>
25-
} & T
26-
) {
27-
const {
28-
lineNumbers,
29-
showCopyButton,
30-
showExpandButton,
31-
minZoom,
32-
maxZoom,
33-
...rest
34-
} = props
35-
const codeConfig = {
36-
...props.codeConfig,
37-
maxZoom:
38-
maxZoom == null ? props.codeConfig?.maxZoom : maxZoom,
39-
minZoom:
40-
minZoom == null ? props.codeConfig?.minZoom : minZoom,
41-
horizontalCenter:
42-
props.codeConfig?.horizontalCenter ??
43-
props.horizontalCenter,
44-
lineNumbers:
45-
lineNumbers == null
46-
? props.codeConfig?.lineNumbers
47-
: lineNumbers,
48-
showCopyButton:
49-
showCopyButton == null
50-
? props.codeConfig?.showCopyButton
51-
: showCopyButton,
52-
showExpandButton:
53-
showExpandButton == null
54-
? props.codeConfig?.showExpandButton
55-
: showExpandButton,
56-
rows: props.rows,
57-
debug: props.debug ?? props.codeConfig?.debug,
58-
}
59-
return { ...rest, codeConfig }
40+
type InnerCodeProps = {
41+
onTabClick: (filename: string) => void
42+
globalConfig: GlobalConfig
43+
editorStep: EditorStep
44+
codeConfigProps: CodeConfigProps & ElementProps
6045
}
6146

6247
export function InnerCode({
6348
onTabClick,
64-
...props
65-
}: EditorProps & {
66-
onTabClick?: (filename: string) => void
67-
} & GlobalConfig) {
68-
const { className, style, codeConfig, ...editorProps } =
69-
mergeCodeConfig(props)
49+
globalConfig,
50+
editorStep,
51+
codeConfigProps,
52+
}: InnerCodeProps) {
53+
const { className, style, ...config } = mergeCodeConfig(
54+
globalConfig,
55+
codeConfigProps
56+
)
7057

7158
if (
72-
!props.southPanel &&
73-
props.files.length === 1 &&
74-
!props.files[0].name
59+
!editorStep.southPanel &&
60+
editorStep.files.length === 1 &&
61+
!editorStep.files[0].name
7562
) {
7663
return (
7764
<div
7865
className={`ch-codeblock not-prose ${
7966
className || ""
8067
}`}
81-
data-ch-theme={props.codeConfig?.themeName}
68+
data-ch-theme={globalConfig.themeName}
8269
style={style}
8370
>
8471
<CodeSpring
8572
className="ch-code"
86-
config={codeConfig}
87-
step={editorProps.files[0]}
73+
config={config}
74+
step={editorStep.files[0]}
8875
/>
8976
</div>
9077
)
9178
} else {
9279
const frameProps = {
93-
...editorProps?.frameProps,
80+
// ...editorStep?.frameProps,
9481
onTabClick,
9582
}
9683
return (
9784
<div
9885
className={`ch-codegroup not-prose ${
9986
className || ""
10087
}`}
101-
data-ch-theme={props.codeConfig?.themeName}
88+
data-ch-theme={globalConfig.themeName}
10289
style={style}
10390
>
10491
<EditorSpring
105-
{...editorProps}
92+
{...editorStep}
10693
frameProps={frameProps}
107-
codeConfig={codeConfig}
94+
codeConfig={config}
10895
/>
10996
</div>
11097
)
11198
}
11299
}
113100

101+
export function mergeCodeConfig(
102+
globalConfig: GlobalConfig,
103+
local: CodeConfigProps & ElementProps
104+
) {
105+
const {
106+
// ignore these
107+
staticMediaQuery,
108+
themeName,
109+
triggerPosition,
110+
// keep the rest
111+
...global
112+
} = globalConfig
113+
return {
114+
...global,
115+
...local,
116+
lineNumbers: local.lineNumbers ?? global.lineNumbers,
117+
maxZoom: local.maxZoom ?? global.maxZoom,
118+
minZoom: local.minZoom ?? global.minZoom,
119+
horizontalCenter:
120+
local.horizontalCenter ?? global.horizontalCenter,
121+
showCopyButton:
122+
local.showCopyButton ?? global.showCopyButton,
123+
}
124+
}
125+
114126
export function updateEditorStep(
115127
step: EditorStep,
116128
filename: string | undefined,

packages/mdx/src/mdx-client/inline-code.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ import { Code } from "../utils"
33

44
export function InlineCode({
55
className,
6-
codeConfig,
6+
globalConfig,
77
children,
88
code,
99
...rest
1010
}: {
1111
className: string
1212
code: Code
1313
children?: React.ReactNode
14-
codeConfig: { themeName: string }
14+
globalConfig: { themeName: string }
1515
}) {
1616
const { lines } = code
1717
const allTokens = lines.flatMap(line => line.tokens)
1818

1919
return (
2020
<span
21-
data-ch-theme={codeConfig.themeName}
21+
data-ch-theme={globalConfig.themeName}
2222
className={
2323
"ch-inline-code not-prose" +
2424
(className ? " " + className : "")

packages/mdx/src/mdx-client/preview.tsx

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,31 @@ import {
66
SandboxInfo,
77
} from "@codesandbox/sandpack-client"
88
import { EditorStep } from "../mini-editor"
9-
import { CodeSettings } from "../core/types"
9+
import { ElementProps, GlobalConfig } from "../core/types"
10+
11+
type PreviewProps = {
12+
globalConfig: GlobalConfig
13+
// data
14+
files: EditorStep["files"]
15+
presetConfig?: PresetConfig
16+
children?: React.ReactNode
17+
// local config
18+
show?: string
19+
frameless?: boolean
20+
} & ElementProps
1021

1122
export type PresetConfig = SandboxInfo
1223
export function Preview({
13-
className,
24+
globalConfig,
1425
files,
1526
presetConfig,
1627
show,
1728
children,
18-
style,
1929
frameless,
20-
codeConfig,
30+
className,
31+
style,
2132
...rest
22-
}: {
23-
className: string
24-
frameless?: boolean
25-
files: EditorStep["files"]
26-
presetConfig?: PresetConfig
27-
show?: string
28-
style?: React.CSSProperties
29-
children?: React.ReactNode
30-
codeConfig?: CodeSettings
31-
}) {
33+
}: PreviewProps) {
3234
const kids = presetConfig ? (
3335
<SandpackPreview
3436
files={files}
@@ -43,7 +45,7 @@ export function Preview({
4345
"ch-preview" + (className ? " " + className : "")
4446
}
4547
style={style}
46-
data-ch-theme={codeConfig?.themeName}
48+
data-ch-theme={globalConfig?.themeName}
4749
>
4850
{frameless ? (
4951
kids

0 commit comments

Comments
 (0)