33import React , { useState , useEffect , useRef , useCallback } from "react" ;
44import clsx from "clsx" ;
55import styles from "./code.module.css"
6+ import { cn } from "@/lib/utils" ;
67
78declare global {
89 interface Window {
@@ -227,7 +228,7 @@ export default function Code({
227228 const [ lineNums , setLineNums ] = useState ( lineNumbers ) ;
228229 const [ wrap , setWrap ] = useState ( wordWrap ) ;
229230 const [ copied , setCopied ] = useState ( false ) ;
230- const [ ready , setReady ] = useState ( false ) ;
231+ const [ ready , setReady ] = useState ( false ) ; // loaded?
231232
232233 const codeRef = useRef < HTMLElement > ( null ) ;
233234
@@ -246,26 +247,29 @@ export default function Code({
246247
247248 // Load Prism + plugins + language, then highlight
248249 const highlight = useCallback ( async ( ) => {
249- await loadPrismCore ( ) ;
250+ let prismLoaded = codeRef . current && window . Prism ;
251+ if ( ! prismLoaded ) {
252+ await loadPrismCore ( ) ;
250253
251- // autoloader is needed to load the language dependencies
252- // example: tsx depends on ts
253- await loadPrismPlugin ( "autoloader" , false ) ;
254+ // autoloader is needed to load the language dependencies
255+ // example: tsx depends on ts
256+ await loadPrismPlugin ( "autoloader" , false ) ;
254257
255- if ( lineNums ) await loadPrismPlugin ( "line-numbers" ) ;
256-
257- if ( codeRef . current && window . Prism ) {
258- setReady ( true ) ;
259- // Small tick so DOM is painted with new class
260- requestAnimationFrame ( ( ) => {
261- if ( codeRef . current ) window . Prism . highlightElement ( codeRef . current ) ;
262- } ) ;
258+ if ( lineNums ) await loadPrismPlugin ( "line-numbers" ) ;
263259 }
260+
261+ setReady ( true ) ;
262+
263+ // Small tick so DOM is painted with new class
264+ requestAnimationFrame ( ( ) => {
265+ if ( codeRef . current ) window . Prism . highlightElement ( codeRef . current ) ;
266+ } ) ;
264267 } , [ currentLang , lineNums ] ) ;
265268
269+ let currentHref = typeof window !== "undefined" ? window . location . href : undefined ;
266270 useEffect ( ( ) => {
267- highlight ( ) ;
268- } , [ highlight ] ) ;
271+ highlight ( ) . then ( ( ) => "" ) ;
272+ } , [ highlight , currentHref ] ) ; // should trigger for every new page
269273
270274
271275 let code : string | null = null ;
@@ -298,7 +302,6 @@ export default function Code({
298302
299303 const shell : React . CSSProperties = {
300304 fontFamily : "'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace" ,
301- borderRadius : "12px" ,
302305 overflow : "hidden" ,
303306 border : isLight ? "1px solid #e2e8f0" : "1px solid rgba(255,255,255,0.07)" ,
304307 background : isLight ? "#f8fafc" : "#0d1117" ,
@@ -418,7 +421,7 @@ export default function Code({
418421 letterSpacing : "0.02em" ,
419422 } ;
420423
421-
424+ let roundedValue = "xl"
422425 return (
423426 < >
424427 { /* Google Font for JetBrains Mono */ }
@@ -428,7 +431,7 @@ export default function Code({
428431 />
429432
430433 < div className = {
431- clsx ( "position- relative",
434+ cn ( " relative", `rounded- ${ roundedValue } ` ,
432435 styles [ "shell" ]
433436 ) } style = { shell } { ...rest } >
434437 { /* ── Toolbar ── */ }
@@ -487,29 +490,33 @@ export default function Code({
487490 </ div >
488491 </ >
489492 ) }
490- < div className = {
491- clsx (
492- "position-absolute top-0 end-0 p-2 print:hidden" ,
493- styles [ "showOnHover" ]
494- ) }
495- >
496- < span style = { langBadge } > { currentLang } </ span >
497- < button style = { copyBtnStyle } onClick = { handleCopy } > { copied ? "✓ Copied" : "⎘ Copy" } </ button >
498- </ div >
499493 { /* ── Code Block ── */ }
500494 < pre
501495 className = { clsx (
502496 lineNums ?? "line-numbers" ,
503- styles [ "preStyle" ]
497+ styles [ "preStyle" ] ,
498+ "relative" ,
504499 ) }
505500 >
506- < code
501+ < div className = {
502+ cn (
503+ "absolute top-0 end-0 p-2 print:hidden bg-background" ,
504+ `rounded-tr-${ roundedValue } ` ,
505+ `rounded-bl-${ roundedValue } ` ,
506+ roundedValue ,
507+ styles [ "showOnHover" ] ,
508+ ) }
509+ >
510+ < span style = { langBadge } > { currentLang } </ span >
511+ < button style = { copyBtnStyle } onClick = { handleCopy } > { copied ? "✓ Copied" : "⎘ Copy" } </ button >
512+ </ div >
513+ < code
507514 ref = { codeRef }
508515 className = { `language-${ currentLang } ` }
509516 style = { { opacity : ready ? 1 : 0.4 , transition : "opacity 0.2s" } }
510- >
517+ >
511518 { code }
512- </ code >
519+ </ code >
513520 </ pre >
514521 </ div >
515522 </ >
0 commit comments