nextui/apps/docs/components/sandpack/code-viewer.tsx
WK 0d217e466f
refactor: optimization (#5362)
* chore(deps): bump RA versions

* chore(deps): bump RA versions

* chore(deps): bump RA versions

* chore: changeset

* chore(deps): remove unnecessary dependencies

* fix(calendar): typing issue

* refactor(system): remove unused SupportedCalendars

* refactor(system): move I18nProviderProps to type

* refactor: use `spectrumCalendarProps<DateValue>["createCalendar"]`

* feat: add consistent-type-imports

* fix: eslint

* chore: add changeset

* refactor: remove unused deps
2025-06-09 14:17:44 +08:00

101 lines
3.2 KiB
TypeScript

import type {SandpackInitMode} from "@codesandbox/sandpack-react";
import type {Language} from "prism-react-renderer";
import type {HighlightedLines} from "./types";
import type {Decorators} from "./types";
import {FileTabs, useSandpack, useActiveCode, SandpackStack} from "@codesandbox/sandpack-react";
import * as React from "react";
import {Codeblock} from "@/components/docs/components";
export interface CodeViewerProps {
showTabs?: boolean;
showLineNumbers?: boolean;
/**
* Provides a way to draw or style a piece of the content.
*/
decorators?: Decorators;
code?: string;
highlightedLines?: HighlightedLines;
wrapContent?: boolean;
defaultExpanded?: boolean;
/**
* This provides a way to control how some components are going to
* be initialized on the page. The CodeEditor and the Preview components
* are quite expensive and might overload the memory usage, so this gives
* a certain control of when to initialize them.
*/
initMode?: SandpackInitMode;
containerRef?: React.RefObject<HTMLDivElement>;
}
export const SandpackCodeViewer = React.forwardRef<any, CodeViewerProps>(
({showTabs, code: propCode, highlightedLines, containerRef}, ref) => {
const {sandpack} = useSandpack();
const {code} = useActiveCode();
const {activeFile} = sandpack;
// 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
// const [internalKey, setInternalKey] = React.useState(() => id);
const shouldShowTabs = showTabs ?? sandpack.visibleFilesFromProps.length > 1;
const fileExt = activeFile.split(".").pop() as Language;
// const isAppFile = activeFile.includes("App");
// React.useEffect(() => {
// setInternalKey(getId());
// }, [propCode, code]);
React.useEffect(() => {
if (containerRef && containerRef.current !== null) {
const container = containerRef.current;
container.style.height = "auto";
}
}, []);
return (
<>
<div className="h-full">
<SandpackStack>
{shouldShowTabs ? <FileTabs /> : null}
<div className="sp-code-viewer max-h-[600px] overflow-y-scroll">
{/*
* Disabled in favor of Codeblock due to performance issues & font size on ios
*
<CodeEditor
key={internalKey}
ref={ref}
readOnly
code={propCode || code}
decorators={isAppFile ? decorators : []}
filePath={activeFile}
initMode={initMode || sandpack.initMode}
showLineNumbers={showLineNumbers}
showReadOnly={false}
wrapContent={wrapContent}
/> */}
<Codeblock
ref={ref}
className="pb-2"
codeString={propCode || code}
language={fileExt}
metastring={highlightedLines && `{${highlightedLines}}`}
/>
</div>
</SandpackStack>
</div>
</>
);
},
);
SandpackCodeViewer.displayName = "SandpackCodeViewer";
export default SandpackCodeViewer;