@@ -217,6 +217,19 @@ export const CodeBlock = forwardRef<HTMLDivElement, CodeBlockProps>(
217217 const [ isModalOpen , setIsModalOpen ] = useState ( false ) ;
218218 const [ isWrapped , setIsWrapped ] = useState ( false ) ;
219219
220+ const handleCodeMouseDown = useCallback ( ( e : React . MouseEvent ) => {
221+ if ( e . button !== 0 ) return ;
222+ const el = e . currentTarget as HTMLElement ;
223+ document . documentElement . style . userSelect = "none" ;
224+ el . style . userSelect = "text" ;
225+ const restore = ( ) => {
226+ document . documentElement . style . userSelect = "" ;
227+ el . style . userSelect = "" ;
228+ document . removeEventListener ( "mouseup" , restore ) ;
229+ } ;
230+ document . addEventListener ( "mouseup" , restore ) ;
231+ } , [ ] ) ;
232+
220233 const onCopied = useCallback (
221234 ( event : React . MouseEvent < HTMLButtonElement > ) => {
222235 event . preventDefault ( ) ;
@@ -334,42 +347,44 @@ export const CodeBlock = forwardRef<HTMLDivElement, CodeBlockProps>(
334347 ) }
335348 </ div >
336349
337- { shouldHighlight ? (
338- < HighlightCode
339- theme = { theme }
340- code = { code }
341- language = { language }
342- showLineNumbers = { showLineNumbers }
343- highlightLines = { highlightLines }
344- maxLineWidth = { maxLineWidth }
345- className = "px-2 py-3"
346- preClassName = "text-xs"
347- isWrapped = { isWrapped }
348- searchTerm = { searchTerm }
349- />
350- ) : (
351- < div
352- dir = "ltr"
353- className = { cn (
354- "px-2 py-3 scrollbar-thin scrollbar-track-transparent scrollbar-thumb-charcoal-600" ,
355- ! isWrapped && "overflow-x-auto" ,
356- isWrapped && "overflow-y-auto"
357- ) }
358- style = { {
359- maxHeight,
360- } }
361- >
362- < pre
350+ < div onMouseDown = { handleCodeMouseDown } >
351+ { shouldHighlight ? (
352+ < HighlightCode
353+ theme = { theme }
354+ code = { code }
355+ language = { language }
356+ showLineNumbers = { showLineNumbers }
357+ highlightLines = { highlightLines }
358+ maxLineWidth = { maxLineWidth }
359+ className = "px-2 py-3"
360+ preClassName = "text-xs"
361+ isWrapped = { isWrapped }
362+ searchTerm = { searchTerm }
363+ />
364+ ) : (
365+ < div
366+ dir = "ltr"
363367 className = { cn (
364- "relative mr-2 p-2 font-mono text-xs leading-relaxed" ,
365- isWrapped && "[&_span]:whitespace-pre-wrap [&_span]:break-words"
368+ "px-2 py-3 scrollbar-thin scrollbar-track-transparent scrollbar-thumb-charcoal-600" ,
369+ ! isWrapped && "overflow-x-auto" ,
370+ isWrapped && "overflow-y-auto"
366371 ) }
367- dir = "ltr"
372+ style = { {
373+ maxHeight,
374+ } }
368375 >
369- { highlightSearchText ( code , searchTerm ) }
370- </ pre >
371- </ div >
372- ) }
376+ < pre
377+ className = { cn (
378+ "relative mr-2 p-2 font-mono text-xs leading-relaxed" ,
379+ isWrapped && "[&_span]:whitespace-pre-wrap [&_span]:break-words"
380+ ) }
381+ dir = "ltr"
382+ >
383+ { highlightSearchText ( code , searchTerm ) }
384+ </ pre >
385+ </ div >
386+ ) }
387+ </ div >
373388 </ div >
374389
375390 < Dialog open = { isModalOpen } onOpenChange = { setIsModalOpen } >
0 commit comments