Skip to main content

How to Create Copy to Clipboard or How to Create Clipboard in Lightning Component

Hello friends today post,, How to Create Copy Clipboard in Lightning Component
so let us start......

Step=>1.

       goto developer Console......
                                    File=>New=>Lightning Component
                             
                                          copyToClipboard.cmp

 <aura:component implements="flexipage:availableForAllPageTypes" access="global" >
    <div>
        <p aura:id="pId">quick brown fox jumps over the lazy dog</p>
        <lightning:button iconName="utility:copy_to_clipboard"
                          onclick="{!c.copyHardcoreText}"
                          label="Copy Text to clipboard"
                          aura:id="btn1"/>
    </div>
 
    <div>
        <lightning:textarea value="hello i am textarea value" aura:id="inputF"/>
        <lightning:button iconName="utility:copy_to_clipboard"
                          onclick="{!c.copyInputFieldValue}"
                          label="Copy Text to clipboard"
                          aura:id="btn2"/>
    </div>
 
    <div>
        <lightning:textarea value="" label="Paste Here (for testing)"/>
    </div>
</aura:component>

Step=>2.
     
             copyToClipboardController.js

({
    copyHardcoreText : function(component, event, helper) {
        // get HTML hardcore value using aura:id
        var textForCopy = component.find('pId').getElement().innerHTML;
        // calling common helper class to copy selected text value
        helper.copyTextHelper(component,event,textForCopy);
    },
 
    copyInputFieldValue : function(component, event, helper) {
        // get lightning:textarea field value using aura:id
        var textForCopy = component.find('inputF').get("v.value");
        // calling common helper class to copy selected text value
        helper.copyTextHelper(component,event,textForCopy);
    },
 
})


Step=>3.

             copyToClipboardHelper.js

({
    copyTextHelper : function(component,event,text) {
        // Create an hidden input
        var hiddenInput = document.createElement("input");
        // passed text into the input
        hiddenInput.setAttribute("value", text);
        // Append the hiddenInput input to the body
        document.body.appendChild(hiddenInput);
        // select the content
        hiddenInput.select();
        // Execute the copy command
        document.execCommand("copy");
        // Remove the input from the body after copy text
        document.body.removeChild(hiddenInput);
        // store target button label value
        var orignalLabel = event.getSource().get("v.label");
        // change button icon after copy text
        event.getSource().set("v.iconName" , 'utility:check');
        // change button label with 'copied' after copy text
        event.getSource().set("v.label" , 'copied');
     
        // set timeout to reset icon and label value after 700 milliseconds
        setTimeout(function(){
            event.getSource().set("v.iconName" , 'utility:copy_to_clipboard');
            event.getSource().set("v.label" , orignalLabel);
        }, 700);
     
    }
})



                                                       Output:




Thanks.!

Buy me a coffeeBuy me a coffee

Comments

  1. Can you do this but with the value of an output field as the thing to copy? For example, I want to display the record's ID (which is an output field) on the Equipment's record page.

    Then, I want a 'copy to clipboard' button that allows the user to copy that record id quickly.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to used slds-hide/show class for div hide and show in lightning component

Hello friends today post ,how to create a lightning component to use slds-hide/show class, very easy to use class and aplly.. so let us start ... firstly create a lightning component and there are two or three div or anything when you want to hide/show ... so see given bellow example and udes slds-hide/show class Step=>1.                   goto developer console=>File=>New=>Lightning Component                           sldshideshow.cmp <aura:component >     <aura:attribute name="First" type="string" default="slds-show"/>     <aura:attribute name="Second" type="string" default="slds-hide"/>         <center>     <div class="slds-box" style="width:300px;height:300px;margin-top:15px;">         <div class="{!v.First}">         ...

Create Overlays with the New Modal Component

Hello friends, Now modal tag is available in #LWC, we can create a modal using the #LWC Standard tag. snapshot given below with code. Step-1 Create LWC component Step-2 Add below code Step-3 I am showing how my component is look like. Thanks for reading

How to create Dynamic Generic Tree grid using LWC

Hello frnds todat i am talking about , how to create tree-grid using lwc Step-1:- First create Custom metadata, Click setup and In Quich Search box type " Custom metadata " Step-2:- Click on Custom metadata and click on "New custom metadata type" type custom metadata name and save after that create custom feilds, list given below image after that click on "manage" enter field api name according to your requirement Example given below Step-3:- Create Apex class Name is "SLMT_GenericHierarchiesController" global class SLMT_GenericHierarchiesController { @AuraEnabled(cacheable=true) global static List getFullHierarchies(Id recordId,String parentRelationship){ Id topLevelParentId = SLMT_GenericHierarchiesController.getUltimateParent(recordId,parentRelationship); List treeColumns = SLMT_GenericHierarchiesController.describeHierarchyMappings(recordId); List additionalFields =new List (); ...