Skip to content

Commit

Permalink
[FEATURE] Added Copy button with Toast notification
Browse files Browse the repository at this point in the history
Added Copy Button which copies the code snippet and gives a toast notification saying Code is Copied.
  • Loading branch information
AJAmit17 committed Mar 16, 2024
1 parent df00c24 commit 0b88ca2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
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 Down Expand Up @@ -27,7 +27,9 @@ import {
MoreHoriz,
OpenInNew,
PlayArrow,
FileCopyOutlined,
} 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
18 changes: 5 additions & 13 deletions components/searchBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,13 @@ export default function SearchBar({

function handleInput(event: FormEvent) {
setQuery((event.target as HTMLInputElement).value);
const searchValue = (event.target as HTMLInputElement).value;
// When input value is null, set error & empty state to `true` and do nothing.

if (!smallScreen) {
if (searchValue) {
debounce(() => {
router.push(`/search?q=${searchValue}`);
});
} else {
debounce(() => {
router.push(`/`);
});
}
}
if (!searchValue) {
if (!smallScreen)
debounce(() => {
router.push(`/search?q=${(event.target as HTMLInputElement).value}`);
});
if (!(event.target as HTMLInputElement).value) {
setIsEmpty(true);
setIsError(true);
return;
Expand Down
7 changes: 7 additions & 0 deletions hooks/toast-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use client";

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

0 comments on commit 0b88ca2

Please sign in to comment.