Skip to content

Add expand button to codeblocks #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2023
Merged
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
2 changes: 1 addition & 1 deletion packages/mdx/dev/content/scrollycoding-demo.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor i

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.

<CH.Scrollycoding showCopyButton={false} rows="focus" showExpandButton={true}>
<CH.Scrollycoding showCopyButton={true} rows="focus" showExpandButton={true}>

## Step 1

Expand Down
3 changes: 2 additions & 1 deletion packages/mdx/src/mini-editor/code-browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ function Sidebar({
activeFile: CodeFile
setActiveFile: (file: CodeFile) => void
}) {
const noFiles = files.length === 0 || !files[0].name
const tree = React.useMemo(
() => toFileTree(files),
[files]
)
return (
return noFiles ? null : (
<div className="ch-code-browser-sidebar">
<SidebarNodes
tree={tree}
Expand Down
2 changes: 1 addition & 1 deletion packages/mdx/src/mini-editor/dialog.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
.ch-expand-dialog {
height: 100vh;
width: 100vw;
max-width: 900px;
max-width: min(900px, calc((100% - 6px) - 2em));
border: 0;
background-color: transparent;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/mdx/src/mini-editor/editor-tween.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useTransition, EditorStep } from "./editor-shift"
import { CodeConfig } from "../smooth-code"
import { useLayoutEffect } from "../utils"
import { CopyButton } from "smooth-code/copy-button"
import { ExpandButton } from "mini-editor/expand-button"
import { EditorExpandButton } from "mini-editor/expand-button"

export { EditorTransition, EditorTween }
export type { EditorTransitionProps, EditorTweenProps }
Expand Down Expand Up @@ -105,7 +105,7 @@ function EditorTween({
/>
) : undefined}
{showExpandButton ? (
<ExpandButton
<EditorExpandButton
className="ch-editor-button"
step={next || prev}
/>
Expand Down
52 changes: 47 additions & 5 deletions packages/mdx/src/mini-editor/expand-button.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,63 @@
import React from "react"
import { CodeBrowser } from "./code-browser"
import { EditorStep } from "./editor-shift"
import { CodeStep } from "../smooth-code/code-tween"

export function ExpandButton({
style,
export function EditorExpandButton({
step,
className,
...props
}: {
style?: React.CSSProperties
step: EditorStep
className?: string
}) {
const files = step.files
const activeFileName = step.northPanel.active

return (
<ExpandButton
{...props}
files={step.files}
activeFileName={step.northPanel?.active}
/>
)
}

export function CodeExpandButton({
step,
...props
}: {
style?: React.CSSProperties
step: CodeStep
className?: string
}) {
const file = { ...step, name: "" }
const activeFileName = ""

return (
<ExpandButton
{...props}
files={[file]}
activeFileName={activeFileName}
/>
)
}

function ExpandButton({
style,
className,
files,
activeFileName,
}: {
style?: React.CSSProperties
files: EditorStep["files"]
activeFileName: string
className?: string
}) {
const [expanded, setExpanded] = React.useState(false)
const [dialogSupported, setDialogSupported] =
React.useState(true)
const ref = React.useRef<any>(null)
const files = step.files

// check if <dialog /> is supported
React.useEffect(() => {
Expand Down Expand Up @@ -62,7 +104,7 @@ export function ExpandButton({
<div className="ch-expand-dialog-content">
<CodeBrowser
files={files}
startingFileName={step.northPanel.active}
startingFileName={activeFileName}
/>
</div>
) : undefined}
Expand Down
24 changes: 18 additions & 6 deletions packages/mdx/src/smooth-code/code-tween.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "./partial-step-parser"
import { SmoothLines } from "./smooth-lines"
import { CopyButton } from "./copy-button"
import { CodeExpandButton } from "mini-editor/expand-button"

type HTMLProps = React.DetailedHTMLProps<
React.HTMLAttributes<HTMLDivElement>,
Expand Down Expand Up @@ -95,6 +96,7 @@ export function CodeTween({
config={config}
progress={progress}
htmlProps={preProps}
tween={tween}
/>
)
}
Expand Down Expand Up @@ -126,12 +128,14 @@ function AfterDimensions({
progress,
htmlProps,
config,
tween,
}: {
dimensions: NonNullable<Dimensions>
stepInfo: CodeShift
config: CodeConfig
progress: number
htmlProps: HTMLProps
tween: FullTween<CodeStep>
}) {
return (
<Wrapper htmlProps={htmlProps} measured={true}>
Expand All @@ -144,12 +148,20 @@ function AfterDimensions({
maxZoom={maxZoom}
center={horizontalCenter}
/>
{config.showCopyButton ? (
<CopyButton
className="ch-code-button"
content={stepInfo?.code?.prev}
/>
) : undefined}
<div className="ch-code-buttons">
{config.showCopyButton ? (
<CopyButton
className="ch-code-button"
content={stepInfo?.code?.prev}
/>
) : undefined}
{config.showExpandButton ? (
<CodeExpandButton
className="ch-code-button"
step={tween.prev}
/>
) : undefined}
</div>
</Wrapper>
)
}
Expand Down
10 changes: 7 additions & 3 deletions packages/mdx/src/smooth-code/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@
color: inherit;
}

.ch-code-button {
@include button-reset;

.ch-code-buttons {
position: absolute;
top: 10px;
right: 10px;
}

.ch-code-button {
@include button-reset;

width: 1.1em;
height: 1.1em;
margin-left: 0.5em;
}

.ch-code-wrapper {
Expand Down