Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Added Copy Code button with Toast Notification. #282

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion components/codePreview/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState } from "react";
import React, { useCallback, useRef, useState } from "react";
import "highlight.js/styles/atom-one-light.css";
import { Algorithm } from "lib/models";
import {
Expand All @@ -23,11 +23,13 @@ import { getLanguageName, Language } from "lib/repositories";
import LanguageIcon from "components/icon";
import {
Close,
FileCopyOutlined,
Fullscreen,
MoreHoriz,
OpenInNew,
PlayArrow,
} from "@material-ui/icons";
import { toast } from "react-hot-toast";
import NextLink from "next/link";
import { StringParam, useQueryParam, withDefault } from "next-query-params";
import classes from "./style.module.css";
Expand All @@ -45,6 +47,12 @@ export default function CodePreview({ algorithm }: { algorithm: Algorithm }) {
const fabRef = useRef();
const [mobileMoreMenuOpen, setMobileMoreMenuOpen] = useState(false);

const copyCode = useCallback(async () => {
const codeText = document.querySelector(".style_pre__k555n")?.textContent;
await navigator.clipboard.writeText(codeText);
toast.success("Code Snippet Copied!");
}, []);

return (
<div className={`${classes.container}`}>
<div className={classes.codeBox}>
Expand Down Expand Up @@ -86,6 +94,10 @@ export default function CodePreview({ algorithm }: { algorithm: Algorithm }) {
<Fullscreen />
<Typography>{t("fullscreen")}</Typography>
</MenuItem>
<MenuItem onClick={() => copyCode()}>
<FileCopyOutlined />
<Typography>{t("Copy Code")}</Typography>
</MenuItem>
<Link
href={implementations[selectedLanguague].url}
target="_blank"
Expand All @@ -101,6 +113,9 @@ export default function CodePreview({ algorithm }: { algorithm: Algorithm }) {
) : (
<>
<div className={classes.buttonsTop}>
<IconButton className={classes.copycode} onClick={copyCode}>
<FileCopyOutlined />
</IconButton>
<Button
startIcon={<OpenInNew />}
href={implementations[selectedLanguague].url}
Expand Down
5 changes: 5 additions & 0 deletions hooks/toast-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Toaster } from "react-hot-toast";

export default function ToastProvider() {
return <Toaster />;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"react": "^17.0.2",
"react-devicons": "^2.11.0-rev.2",
"react-dom": "^17.0.2",
"react-hot-toast": "^2.4.1",
"rehype-highlight": "<5.0.0",
"rehype-stringify": "<9.0.0",
"remark-gfm": "<2.0.0",
Expand Down
2 changes: 2 additions & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { QueryProvider } from "hooks/query";
import { DarkThemeProvider } from "hooks/darkTheme";
import DefaultLayout from "layouts/default";
import { NextQueryParamProvider } from "next-query-params";
import ToastProvider from "hooks/toast-provider";

function MyApp({ Component, pageProps }: AppProps) {
const [isDarkTheme, setIsDarkTheme] = useState(false);
Expand All @@ -32,6 +33,7 @@ function MyApp({ Component, pageProps }: AppProps) {
<ThemeProvider theme={isDarkTheme ? darkTheme : lightTheme}>
<QueryProvider>
<NextQueryParamProvider>
<ToastProvider />
<Head />
<CssBaseline />
<NextNprogress
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2317,6 +2317,11 @@ globby@^11.1.0:
merge2 "^1.4.1"
slash "^3.0.0"

goober@^2.1.10:
version "2.1.14"
resolved "https://registry.yarnpkg.com/goober/-/goober-2.1.14.tgz#4a5c94fc34dc086a8e6035360ae1800005135acd"
integrity sha512-4UpC0NdGyAFqLNPnhCT2iHpza2q+RAY3GV85a/mRPdzyPQMsj0KmMMuetdIkzWRbJ+Hgau1EZztq8ImmiMGhsg==

has-bigints@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/has-bigints/-/has-bigints-1.0.1.tgz#64fe6acb020673e3b78db035a5af69aa9d07b113"
Expand Down Expand Up @@ -3802,6 +3807,13 @@ react-dom@^17.0.2:
object-assign "^4.1.1"
scheduler "^0.20.2"

react-hot-toast@^2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/react-hot-toast/-/react-hot-toast-2.4.1.tgz#df04295eda8a7b12c4f968e54a61c8d36f4c0994"
integrity sha512-j8z+cQbWIM5LY37pR6uZR6D4LfseplqnuAO4co4u8917hBUvXlEqyP1ZzqVLcqoyUesZZv/ImreoCeHVDpE5pQ==
dependencies:
goober "^2.1.10"

react-i18next@^11.8.13:
version "11.13.0"
resolved "https://registry.yarnpkg.com/react-i18next/-/react-i18next-11.13.0.tgz#7b6f23f717154a44cf3f70d62a9368152bf8a73f"
Expand Down