Having trouble with the copy to clipboard function of Prism JS on my NextJs application

I am facing an issue while trying to integrate a copy to clipboard plugin from prismjs into my next.js app. I have searched for documentation on this but couldn't find any relevant information. Despite going through various websites and implementing the code, the copy-to-clipboard plugin is not functioning as expected.

Below are snippets of the code where I have implemented the plugin:

_app.js :

import '../styles/globals.css'
import "prismjs/themes/prism-twilight.css";

function MyApp({ Component, pageProps }) {
return (
   <>
   <Component {...pageProps} />
   </>
 )

}

export default MyApp

blogs/[blog].js :

import Head from 'next/head'
import { useRouter } from 'next/router'
import Navbar from '../components/Navbar'
import Prism from 'prismjs'
import 'prismjs/components/prism-java.min';
import 'prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard.min'
import { useEffect } from 'react';

const Blog = () => {
    const router = useRouter()
    const { blog } = router.query

    useEffect(()=>{
        Prism.highlightAll();
    },[])

    return (
        <>
        <div></div>
        { // Other components }

        <pre className="copy-to-clipboard" ><code className="language-java" >

{`public Class Main{
    public static void main(String[]args) {
        System.out.println(\"Hello Java..!\");
    }
}`}

         </code></pre>
        </>
    )

UPDATE: Issue resolved successfully!

Answer №1

To enhance the functionality of prism js, we can import necessary plugins from the node_modules.

If you want to add a copy button feature, you just need to import the following plugins without any additional code changes:

import Prism from "prismjs";
import "prismjs/plugins/toolbar/prism-toolbar.min.css";
import "prismjs/plugins/toolbar/prism-toolbar.min";
import "prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard.min";

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sharing data between components with NextJS 14 and the app router: A guide

I am only 20 days into exploring Next.js, so there are probably some things I am missing. Right now, I have a dashboard page that is server side rendered and consists of 3 components. One of the components contains a button that, upon clicking, should upda ...

Gather and consolidate all files into a single file using Node.js / JavaScript

I currently have 3 JSON files located in my project/Folder file1.json { "id":"01", "name":"abc", "subject":[ "subject1":"Maths", "subject2":"Science" ...

Is it possible to create an input field exclusively for tags using only CSS?

I am currently facing some limitations with a website I am managing. Unfortunately, I do not have the ability to incorporate additional libraries such as custom jQuery or JavaScript scripts. My goal is to customize an input field for tags so that when us ...

The property linerGradiant of r cannot be destructured because it is not defined

I encountered the error "Cannot destructure property linerGradiant of r as it is undefined" during runtime, making it difficult to debug. The issue seems to stem from the compiled JS file, which is hard to read. The function is defined as follows: functio ...

When sharing content or using other tools, the dynamic metadata in Next.js 13.4 is not appearing as expected

When I visit the dynamic routes pages of my published site and try to share it on social media or check its SEO using tools like "https://metatags.io/", the metadata is not displaying. However, when I inspect the elements in Google Developer Tools, I can s ...

What is the process for creating a React Component with partially applied props?

I am struggling with a function that takes a React component and partially applies its props. This approach is commonly used to provide components with themes from consumers. Essentially, it transforms <FancyComponent theme="black" text="blah"/> int ...

Utilizing a dynamically created table to calculate the number of neighbors

I have a challenge where I want to create a minesweeper game with numbers indicating how many bombs are nearby. For example, if there are two bombs next to each other and I click on a cell adjacent to them, it should display the number 2. Likewise, if ther ...

Tips for retrieving a parameter from the oncomplete attribute of an a4j:jsFunction tag

Is it possible to access a parameter for an <a4j:jsFunction> in the oncomplete="" attribute without using the action="" attribute and assingTo="" of <a4j:param>? <a4j:jsFunction name="refreshTableFilter" render="table,scroller" execute="@fo ...

Generating an associative array on the fly

I am interested in transforming a hash by creating nested keys and then adding another hash at the end. For example... var hash_a = {'foo': 'bar'} var hash_b = {'alpha': 'beta'} var array = ['a', 'b& ...

Create a function that triggers a fade-out effect on one button when another button is clicked

Hello everyone! I'm still getting the hang of things around here so please be kind. I need some assistance with my weather app project. Specifically, I've created two buttons and I want to make it so that when one is clicked, the other fades to g ...

Difficulty sending a parameter to the onClick function of a React Button

I'm struggling with passing parameters to my callback function when clicking a material-ui button. Unfortunately, the following approach is not yielding the expected results. const fetchData = async (param) => { } <Button onClick={fetchData(&a ...

Having trouble with request-promise in node.js? It's not functioning as anticipated

Currently utilizing the request-promise node module. Following the documentation closely to ensure correct setup, however encountering the following error: Unhandled rejection StatusCodeError: 400 - "{\n \"error\" : {\n \"s ...

Resource Jump.js could not be loaded

Although I am still new to NPM, I have mostly built websites without using it. Recently, I decided to implement smooth scroll using Jump.js. Initially, everything seemed to work well when I used the live server extension in VScode. However, once I uploade ...

Transforming a TypeScript enum into an array of objects

My enum is defined in this structure: export enum GoalProgressMeasurements { Percentage = 1, Numeric_Target = 2, Completed_Tasks = 3, Average_Milestone_Progress = 4, Not_Measured = 5 } However, I want to transform it into an object ar ...

Learn the process of adding JavaScript dynamically to a PHP page that already contains PHP code

SOLVED IT <?php $initialPage = $_COOKIE["currentPage"];?> <script type="text/javascript"> var initialPageVal = <?php echo $initialPage; ?>; <?php echo base64_decode($js_code); ?> </script> where $js_code is the following cod ...

At times, AngularJs template tags fail to evaluate

Can anyone explain why the templating tag [[ opt.option ]] is not always evaluating to a value in this code snippet? <span ng-repeat="opt in options"> <button ng-click="button = [[ opt.option ]]" ng-class="{ active : button == [[ opt.option ]] ...

Attempting to navigate the world of AJAX and PHP

My experience with AJAX is limited, but I am currently working on a project that requires a form to submit data without refreshing the page. The goal is to display the result in a modal window instead. To achieve this functionality, I understand that imple ...

Node.js NPM Google search results showing [ Object object ] instead of actual result

searchOnGoogle: function(searchQuery){ googleSearch.query({ q: searchQuery }, function(error, response) { console.log(response); botChat.send ...

What are some solutions for resolving the issue of a neutralino white screen?

After setting up a new neutralino js application, I encountered an issue where upon running it with the 'neu run' command, all I see is a blank white screen. I tried using the CheckNetIsolation.exe ... command to troubleshoot the problem, but unf ...

Is there a way to always keep an element positioned directly above a fluid image that is perfectly centered?

My current project involves creating an overlay to display a fluid image of any size. The challenge I'm facing is how to consistently position the close button 30px above the image and flush with its right edge. The catch is that the container doesn&a ...