@@ -2,9 +2,6 @@ import type {SandpackInitMode} from "@codesandbox/sandpack-react";
2
2
3
3
import * as React from "react" ;
4
4
import { FileTabs , useSandpack , useActiveCode , SandpackStack } from "@codesandbox/sandpack-react" ;
5
- import { Button } from "@nextui-org/react" ;
6
- import scrollIntoView from "scroll-into-view-if-needed" ;
7
- import { clsx } from "@nextui-org/shared-utils" ;
8
5
import { Language } from "prism-react-renderer" ;
9
6
10
7
import { HighlightedLines } from "./types" ;
@@ -33,83 +30,42 @@ export interface CodeViewerProps {
33
30
containerRef ?: React . RefObject < HTMLDivElement > ;
34
31
}
35
32
36
- const INITIAL_HEIGHT = "200px" ;
37
-
38
33
export const SandpackCodeViewer = React . forwardRef < any , CodeViewerProps > (
39
- ( { showTabs, code : propCode , defaultExpanded = false , highlightedLines, containerRef} , ref ) => {
34
+ ( { showTabs, code : propCode , highlightedLines, containerRef} , ref ) => {
40
35
const { sandpack} = useSandpack ( ) ;
41
36
const { code} = useActiveCode ( ) ;
42
37
43
38
const { activeFile} = sandpack ;
44
39
45
- const [ isExpanded , setIsExpanded ] = React . useState ( defaultExpanded ) ;
46
-
47
40
// const id = React.useId();
48
41
// hack to make sure we re-render the code editor and change current file
49
42
// TODO: open an issue on sandpack-react
50
43
// const [internalKey, setInternalKey] = React.useState(() => id);
51
- const lineCountRef = React . useRef < { [ key : string ] : number } > ( { } ) ;
52
-
53
- if ( ! lineCountRef . current [ activeFile ] ) {
54
- lineCountRef . current [ activeFile ] = code . split ( "\n" ) . length ;
55
- }
56
44
57
45
const shouldShowTabs = showTabs ?? sandpack . visibleFilesFromProps . length > 1 ;
58
46
59
- const lineCount = lineCountRef . current [ activeFile ] ;
60
- const isExpandable = lineCount > 7 || isExpanded ;
61
47
const fileExt = activeFile . split ( "." ) . pop ( ) as Language ;
62
48
63
49
// const isAppFile = activeFile.includes("App");
64
50
65
- React . useEffect ( ( ) => {
66
- if ( containerRef && containerRef ?. current !== null && isExpandable ) {
67
- containerRef . current . style . height = INITIAL_HEIGHT ;
68
- }
69
- } , [ containerRef ] ) ;
70
-
71
51
// React.useEffect(() => {
72
52
// setInternalKey(getId());
73
53
// }, [propCode, code]);
74
54
75
55
React . useEffect ( ( ) => {
76
- if ( defaultExpanded && containerRef && containerRef ? .current !== null ) {
77
- const container = containerRef ? .current ;
56
+ if ( containerRef && containerRef . current !== null ) {
57
+ const container = containerRef . current ;
78
58
79
59
container . style . height = "auto" ;
80
60
}
81
- } , [ defaultExpanded ] ) ;
82
-
83
- const handleExpand = ( ) => {
84
- const nextIsExpanded = ! isExpanded ;
85
-
86
- setIsExpanded ( nextIsExpanded ) ;
87
- if ( containerRef && containerRef ?. current !== null ) {
88
- const container = containerRef ?. current ;
89
-
90
- if ( nextIsExpanded ) {
91
- container . style . height = "auto" ;
92
- } else {
93
- container . style . height = INITIAL_HEIGHT ;
94
- scrollIntoView ( container , {
95
- behavior : "smooth" ,
96
- scrollMode : "if-needed" ,
97
- block : "center" ,
98
- } ) ;
99
- }
100
- }
101
- } ;
61
+ } , [ ] ) ;
102
62
103
63
return (
104
64
< >
105
65
< div className = "h-full" >
106
66
< SandpackStack >
107
67
{ shouldShowTabs ? < FileTabs /> : null }
108
- < div
109
- className = { clsx ( "sp-code-viewer max-h-[600px] overflow-y-scroll" , {
110
- "is-expanded" : isExpanded ,
111
- } ) }
112
- >
68
+ < div className = "sp-code-viewer max-h-[600px] overflow-y-scroll" >
113
69
{ /*
114
70
* Disabled in favor of Codeblock due to performance issues & font size on ios
115
71
*
@@ -127,33 +83,14 @@ export const SandpackCodeViewer = React.forwardRef<any, CodeViewerProps>(
127
83
/> */ }
128
84
< Codeblock
129
85
ref = { ref }
130
- className = { isExpandable ? "pb-16" : "pb-2" }
86
+ className = "pb-2"
131
87
codeString = { propCode || code }
132
88
language = { fileExt }
133
89
metastring = { highlightedLines && `{${ highlightedLines } }` }
134
90
/>
135
91
</ div >
136
92
</ SandpackStack >
137
93
</ div >
138
- { isExpandable && (
139
- < div
140
- className = { clsx (
141
- "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" ,
142
- { "h-10 bottom-0 pb-2" : isExpanded } ,
143
- { "h-full inset-0" : ! isExpanded } ,
144
- ) }
145
- >
146
- < Button
147
- className = "bg-[#2a2838] shadow-md font-sans dark:bg-zinc-800 text-zinc-300 dark:text-zinc-400 hover:!text-zinc-200"
148
- radius = "full"
149
- size = "sm"
150
- variant = "flat"
151
- onClick = { handleExpand }
152
- >
153
- { isExpanded ? "Show less" : "Show more" }
154
- </ Button >
155
- </ div >
156
- ) }
157
94
</ >
158
95
) ;
159
96
} ,
0 commit comments