Commit e417da9 1 parent 6d46dbe commit e417da9 Copy full SHA for e417da9
File tree 1 file changed +17
-5
lines changed
1 file changed +17
-5
lines changed Original file line number Diff line number Diff line change @@ -6,12 +6,24 @@ type CodePenProps = {
6
6
height : number ;
7
7
} ;
8
8
9
- export default function CodePen ( { id, title } : CodePenProps ) {
9
+ export default function CodePen ( { id, title, height = 300 } : CodePenProps ) {
10
10
useEffect ( ( ) => {
11
- const script = document . createElement ( 'script' ) ;
12
- script . src = 'https://cpwebassets.codepen.io/assets/embed/ei.js' ;
13
- script . async = true ;
14
- document . body . appendChild ( script ) ;
11
+ globalThis . __codepen_loaded__ ??= 0 ;
12
+ globalThis . __codepen_loaded__ += 1 ;
13
+ const count = globalThis . __codepen_loaded__ ;
14
+ if ( count === 1 ) {
15
+ const script = document . createElement ( 'script' ) ;
16
+ script . className = 'codepen' ;
17
+ script . dataset . id = count ;
18
+ script . src = 'https://cpwebassets.codepen.io/assets/embed/ei.js' ;
19
+ script . async = true ;
20
+ document . body . appendChild ( script ) ;
21
+ }
22
+ return ( ) => {
23
+ globalThis . __codepen_loaded__ -= 1 ;
24
+ const script = document . querySelector ( `script.codepen[data-id="${ count } "]` ) ;
25
+ script ?. remove ( ) ;
26
+ } ;
15
27
} , [ ] ) ;
16
28
return (
17
29
< p className = "codepen"
You can’t perform that action at this time.
0 commit comments