Skip to content
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

docs: removed show more from code example #3373

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ interface CodeDemoProps extends UseCodeDemoProps, WindowResizerProps {
displayMode?: "always" | "visible";
isGradientBox?: boolean;
gradientColor?: GradientBoxProps["color"];
defaultExpanded?: boolean;
previewHeight?: string | number;
overflow?: "auto" | "visible" | "hidden";
className?: string;
Expand All @@ -61,7 +60,6 @@ export const CodeDemo: React.FC<CodeDemoProps> = ({
typescriptStrict = false,
showOpenInCodeSandbox,
isGradientBox = false,
defaultExpanded = false,
previewHeight = "auto",
overflow = "visible",
displayMode = "always",
Expand Down Expand Up @@ -138,7 +136,6 @@ export const CodeDemo: React.FC<CodeDemoProps> = ({

const content = (
<DynamicSandpack
defaultExpanded={defaultExpanded}
files={files}
highlightedLines={highlightedLines}
showEditor={showEditor}
Expand All @@ -155,7 +152,6 @@ export const CodeDemo: React.FC<CodeDemoProps> = ({
isInView,
files,
highlightedLines,
defaultExpanded,
showPreview,
showSandpackPreview,
showOpenInCodeSandbox,
Expand Down
78 changes: 10 additions & 68 deletions apps/docs/components/sandpack/code-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import type {SandpackInitMode} from "@codesandbox/sandpack-react";

import * as React from "react";
import {FileTabs, useSandpack, useActiveCode, SandpackStack} from "@codesandbox/sandpack-react";
import {Button} from "@nextui-org/react";
import scrollIntoView from "scroll-into-view-if-needed";
import {clsx} from "@nextui-org/shared-utils";
import {Language} from "prism-react-renderer";

import {HighlightedLines} from "./types";
Expand Down Expand Up @@ -33,83 +30,47 @@ export interface CodeViewerProps {
containerRef?: React.RefObject<HTMLDivElement>;
}

const INITIAL_HEIGHT = "200px";

export const SandpackCodeViewer = React.forwardRef<any, CodeViewerProps>(
({showTabs, code: propCode, defaultExpanded = false, highlightedLines, containerRef}, ref) => {
({showTabs, code: propCode, highlightedLines, containerRef}, ref) => {
const {sandpack} = useSandpack();
const {code} = useActiveCode();

const {activeFile} = sandpack;

const [isExpanded, setIsExpanded] = React.useState(defaultExpanded);

// const id = React.useId();
// hack to make sure we re-render the code editor and change current file
// TODO: open an issue on sandpack-react
winchesHe marked this conversation as resolved.
Show resolved Hide resolved
// const [internalKey, setInternalKey] = React.useState(() => id);
const lineCountRef = React.useRef<{[key: string]: number}>({});
// const lineCountRef = React.useRef<{[key: string]: number}>({});

if (!lineCountRef.current[activeFile]) {
lineCountRef.current[activeFile] = code.split("\n").length;
}
// if (!lineCountRef.current[activeFile]) {
// lineCountRef.current[activeFile] = code.split("\n").length;
// }

const shouldShowTabs = showTabs ?? sandpack.visibleFilesFromProps.length > 1;

const lineCount = lineCountRef.current[activeFile];
const isExpandable = lineCount > 7 || isExpanded;
const fileExt = activeFile.split(".").pop() as Language;

// const isAppFile = activeFile.includes("App");

React.useEffect(() => {
if (containerRef && containerRef?.current !== null && isExpandable) {
containerRef.current.style.height = INITIAL_HEIGHT;
}
}, [containerRef]);

// React.useEffect(() => {
winchesHe marked this conversation as resolved.
Show resolved Hide resolved
// setInternalKey(getId());
// }, [propCode, code]);

React.useEffect(() => {
if (defaultExpanded && containerRef && containerRef?.current !== null) {
const container = containerRef?.current;
if (containerRef && containerRef.current !== null) {
const container = containerRef.current;

container.style.height = "auto";
}
}, [defaultExpanded]);

const handleExpand = () => {
const nextIsExpanded = !isExpanded;

setIsExpanded(nextIsExpanded);
if (containerRef && containerRef?.current !== null) {
const container = containerRef?.current;

if (nextIsExpanded) {
container.style.height = "auto";
} else {
container.style.height = INITIAL_HEIGHT;
scrollIntoView(container, {
behavior: "smooth",
scrollMode: "if-needed",
block: "center",
});
}
}
};
}, []);

return (
<>
<div className="h-full">
<SandpackStack>
{shouldShowTabs ? <FileTabs /> : null}
<div
className={clsx("sp-code-viewer max-h-[600px] overflow-y-scroll", {
"is-expanded": isExpanded,
})}
>
<div className="sp-code-viewer max-h-[600px] overflow-y-scroll">
{/*
* Disabled in favor of Codeblock due to performance issues & font size on ios
*
Expand All @@ -127,33 +88,14 @@ export const SandpackCodeViewer = React.forwardRef<any, CodeViewerProps>(
/> */}
<Codeblock
ref={ref}
className={isExpandable ? "pb-16" : "pb-2"}
className="pb-2"
codeString={propCode || code}
language={fileExt}
metastring={highlightedLines && `{${highlightedLines}}`}
/>
</div>
</SandpackStack>
</div>
{isExpandable && (
<div
className={clsx(
"w-full absolute z-10 py-1 px-4 flex items-center justify-center bg-gradient-to-t from-code-background to-code-background/10 dark:to-code-background/50",
{"h-10 bottom-0 pb-2": isExpanded},
{"h-full inset-0": !isExpanded},
)}
>
<Button
className="bg-[#2a2838] shadow-md font-sans dark:bg-zinc-800 text-zinc-300 dark:text-zinc-400 hover:!text-zinc-200"
radius="full"
size="sm"
variant="flat"
onClick={handleExpand}
>
{isExpanded ? "Show less" : "Show more"}
</Button>
</div>
)}
</>
);
},
Expand Down
3 changes: 0 additions & 3 deletions apps/docs/components/sandpack/sandpack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface SandpackProps extends UseSandpackProps {
showEditor?: boolean;
showCopyCode?: boolean;
showReportBug?: boolean;
defaultExpanded?: boolean;
showOpenInCodeSandbox?: boolean;
children?: React.ReactNode;
}
Expand All @@ -29,7 +28,6 @@ export const Sandpack: FC<SandpackProps> = ({
typescriptStrict = false,
showPreview = false,
showEditor = true,
defaultExpanded = false,
showOpenInCodeSandbox = true,
showReportBug = true,
showCopyCode = true,
Expand Down Expand Up @@ -66,7 +64,6 @@ export const Sandpack: FC<SandpackProps> = ({
<SandpackCodeViewer
containerRef={editorContainerRef}
decorators={decorators}
defaultExpanded={defaultExpanded}
highlightedLines={highlightedLines}
showTabs={showTabs}
/>
Expand Down
7 changes: 1 addition & 6 deletions apps/docs/styles/sandpack.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
max-height: 600px;
}

.sp-code-viewer.is-expanded .cm-scroller {
overflow: auto;
padding-bottom: 50px;
}

.cm-scroller::-webkit-scrollbar {
width: 0px
}
Expand Down Expand Up @@ -73,4 +68,4 @@

.sp-highlight {
@apply relative z-10 before:content-[''] before:w-full before:h-full before:absolute before:z-[-1] before:left-0 before:bg-gradient-to-r before:from-white/10 before:to-code-background before:border-l-2 border-l-white/80 dark:before:border-l-white/50;
}
}