1- import React , { useRef , useState } from "react" ;
1+ import React , { useCallback , useRef , useState } from "react" ;
22import "highlight.js/styles/atom-one-light.css" ;
33import { Algorithm } from "lib/models" ;
44import {
@@ -27,7 +27,9 @@ import {
2727 MoreHoriz ,
2828 OpenInNew ,
2929 PlayArrow ,
30+ FileCopyOutlined ,
3031} from "@material-ui/icons" ;
32+ import { toast } from "react-hot-toast" ;
3133import NextLink from "next/link" ;
3234import { StringParam , useQueryParam , withDefault } from "next-query-params" ;
3335import classes from "./style.module.css" ;
@@ -45,6 +47,12 @@ export default function CodePreview({ algorithm }: { algorithm: Algorithm }) {
4547 const fabRef = useRef ( ) ;
4648 const [ mobileMoreMenuOpen , setMobileMoreMenuOpen ] = useState ( false ) ;
4749
50+ const copyCode = useCallback ( async ( ) => {
51+ const codeText = document . querySelector ( ".style_pre__k555n" ) ?. textContent ;
52+ await navigator . clipboard . writeText ( codeText ) ;
53+ toast . success ( "Code Snippet Copied!" ) ;
54+ } , [ ] ) ;
55+
4856 return (
4957 < div className = { `${ classes . container } ` } >
5058 < div className = { classes . codeBox } >
@@ -86,6 +94,10 @@ export default function CodePreview({ algorithm }: { algorithm: Algorithm }) {
8694 < Fullscreen />
8795 < Typography > { t ( "fullscreen" ) } </ Typography >
8896 </ MenuItem >
97+ < MenuItem onClick = { ( ) => copyCode ( ) } >
98+ < FileCopyOutlined />
99+ < Typography > { t ( "Copy Code" ) } </ Typography >
100+ </ MenuItem >
89101 < Link
90102 href = { implementations [ selectedLanguague ] . url }
91103 target = "_blank"
@@ -101,6 +113,9 @@ export default function CodePreview({ algorithm }: { algorithm: Algorithm }) {
101113 ) : (
102114 < >
103115 < div className = { classes . buttonsTop } >
116+ < IconButton className = { classes . copycode } onClick = { copyCode } >
117+ < FileCopyOutlined />
118+ </ IconButton >
104119 < Button
105120 startIcon = { < OpenInNew /> }
106121 href = { implementations [ selectedLanguague ] . url }
0 commit comments