Skip to content

Commit c3e90a8

Browse files
authoredJul 4, 2023
Merge pull request #396 from code-hike/code-expand
Add expand button to codeblocks
2 parents 111f44a + 08be59d commit c3e90a8

File tree

7 files changed

+78
-19
lines changed

7 files changed

+78
-19
lines changed
 

‎packages/mdx/dev/content/scrollycoding-demo.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor i
44

55
Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Praesent elementum facilisis leo vel fringilla est ullamcorper eget. At imperdiet dui accumsan sit amet nulla facilities morbi tempus. Praesent elementum facilisis leo vel fringilla. Congue mauris rhoncus aenean vel. Egestas sed tempus urna et pharetra pharetra massa massa ultricies.
66

7-
<CH.Scrollycoding showCopyButton={false} rows="focus" showExpandButton={true}>
7+
<CH.Scrollycoding showCopyButton={true} rows="focus" showExpandButton={true}>
88

99
## Step 1
1010

‎packages/mdx/src/mini-editor/code-browser.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ function Sidebar({
3535
activeFile: CodeFile
3636
setActiveFile: (file: CodeFile) => void
3737
}) {
38+
const noFiles = files.length === 0 || !files[0].name
3839
const tree = React.useMemo(
3940
() => toFileTree(files),
4041
[files]
4142
)
42-
return (
43+
return noFiles ? null : (
4344
<div className="ch-code-browser-sidebar">
4445
<SidebarNodes
4546
tree={tree}

‎packages/mdx/src/mini-editor/dialog.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
.ch-expand-dialog {
88
height: 100vh;
99
width: 100vw;
10-
max-width: 900px;
10+
max-width: min(900px, calc((100% - 6px) - 2em));
1111
border: 0;
1212
background-color: transparent;
1313
}

‎packages/mdx/src/mini-editor/editor-tween.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useTransition, EditorStep } from "./editor-shift"
88
import { CodeConfig } from "../smooth-code"
99
import { useLayoutEffect } from "../utils"
1010
import { CopyButton } from "smooth-code/copy-button"
11-
import { ExpandButton } from "mini-editor/expand-button"
11+
import { EditorExpandButton } from "mini-editor/expand-button"
1212

1313
export { EditorTransition, EditorTween }
1414
export type { EditorTransitionProps, EditorTweenProps }
@@ -105,7 +105,7 @@ function EditorTween({
105105
/>
106106
) : undefined}
107107
{showExpandButton ? (
108-
<ExpandButton
108+
<EditorExpandButton
109109
className="ch-editor-button"
110110
step={next || prev}
111111
/>

‎packages/mdx/src/mini-editor/expand-button.tsx

+47-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,63 @@
11
import React from "react"
22
import { CodeBrowser } from "./code-browser"
33
import { EditorStep } from "./editor-shift"
4+
import { CodeStep } from "../smooth-code/code-tween"
45

5-
export function ExpandButton({
6-
style,
6+
export function EditorExpandButton({
77
step,
8-
className,
8+
...props
99
}: {
1010
style?: React.CSSProperties
1111
step: EditorStep
1212
className?: string
13+
}) {
14+
const files = step.files
15+
const activeFileName = step.northPanel.active
16+
17+
return (
18+
<ExpandButton
19+
{...props}
20+
files={step.files}
21+
activeFileName={step.northPanel?.active}
22+
/>
23+
)
24+
}
25+
26+
export function CodeExpandButton({
27+
step,
28+
...props
29+
}: {
30+
style?: React.CSSProperties
31+
step: CodeStep
32+
className?: string
33+
}) {
34+
const file = { ...step, name: "" }
35+
const activeFileName = ""
36+
37+
return (
38+
<ExpandButton
39+
{...props}
40+
files={[file]}
41+
activeFileName={activeFileName}
42+
/>
43+
)
44+
}
45+
46+
function ExpandButton({
47+
style,
48+
className,
49+
files,
50+
activeFileName,
51+
}: {
52+
style?: React.CSSProperties
53+
files: EditorStep["files"]
54+
activeFileName: string
55+
className?: string
1356
}) {
1457
const [expanded, setExpanded] = React.useState(false)
1558
const [dialogSupported, setDialogSupported] =
1659
React.useState(true)
1760
const ref = React.useRef<any>(null)
18-
const files = step.files
1961

2062
// check if <dialog /> is supported
2163
React.useEffect(() => {
@@ -62,7 +104,7 @@ export function ExpandButton({
62104
<div className="ch-expand-dialog-content">
63105
<CodeBrowser
64106
files={files}
65-
startingFileName={step.northPanel.active}
107+
startingFileName={activeFileName}
66108
/>
67109
</div>
68110
) : undefined}

‎packages/mdx/src/smooth-code/code-tween.tsx

+18-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from "./partial-step-parser"
1515
import { SmoothLines } from "./smooth-lines"
1616
import { CopyButton } from "./copy-button"
17+
import { CodeExpandButton } from "mini-editor/expand-button"
1718

1819
type HTMLProps = React.DetailedHTMLProps<
1920
React.HTMLAttributes<HTMLDivElement>,
@@ -95,6 +96,7 @@ export function CodeTween({
9596
config={config}
9697
progress={progress}
9798
htmlProps={preProps}
99+
tween={tween}
98100
/>
99101
)
100102
}
@@ -126,12 +128,14 @@ function AfterDimensions({
126128
progress,
127129
htmlProps,
128130
config,
131+
tween,
129132
}: {
130133
dimensions: NonNullable<Dimensions>
131134
stepInfo: CodeShift
132135
config: CodeConfig
133136
progress: number
134137
htmlProps: HTMLProps
138+
tween: FullTween<CodeStep>
135139
}) {
136140
return (
137141
<Wrapper htmlProps={htmlProps} measured={true}>
@@ -144,12 +148,20 @@ function AfterDimensions({
144148
maxZoom={maxZoom}
145149
center={horizontalCenter}
146150
/>
147-
{config.showCopyButton ? (
148-
<CopyButton
149-
className="ch-code-button"
150-
content={stepInfo?.code?.prev}
151-
/>
152-
) : undefined}
151+
<div className="ch-code-buttons">
152+
{config.showCopyButton ? (
153+
<CopyButton
154+
className="ch-code-button"
155+
content={stepInfo?.code?.prev}
156+
/>
157+
) : undefined}
158+
{config.showExpandButton ? (
159+
<CodeExpandButton
160+
className="ch-code-button"
161+
step={tween.prev}
162+
/>
163+
) : undefined}
164+
</div>
153165
</Wrapper>
154166
)
155167
}

‎packages/mdx/src/smooth-code/index.scss

+7-3
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@
3030
color: inherit;
3131
}
3232

33-
.ch-code-button {
34-
@include button-reset;
35-
33+
.ch-code-buttons {
3634
position: absolute;
3735
top: 10px;
3836
right: 10px;
37+
}
38+
39+
.ch-code-button {
40+
@include button-reset;
41+
3942
width: 1.1em;
4043
height: 1.1em;
44+
margin-left: 0.5em;
4145
}
4246

4347
.ch-code-wrapper {

0 commit comments

Comments
 (0)