angular – copy to clipboard feature

We all came across this kind of feature – copy to clipboard.

Basically, the aim of this feature is to help the end user to quickly copy some text to clipboard. Instead of selecting particular text and clicking keyboard shortcut for copying text, some websites will provide us a icon to simplify the task.

Once we click that icon or button, the required text will get copy into the clipboard.

I used copy to clipboard option most of the time in Github,

And that’s the base story.

Why?

Few months back, I was supposed to implement the same feature in one of my angular application.

I’m aware that how to do this feature in plain JavaScript. The typical copy function will look like the below snippet

But I hesitated to follow the same structure.

Analyzed existing npm packages

I have gone through various npm packages for copy clipboard with following features expectation in mind

  • Copy icon should get automatically created
  • Selection & Copy function should be generic
  • The code should reused and light weight
  • Minimal effort to implement

Most of the npm plugins available didn’t fulfilled my expectation.

The popular plugin expected the developer to create and pass the text which needs to copied to the copy to clipboard function.

For me, this looks weird!

If a paragraph is having 100 lines, sending those entire text to a plugin doesn’t make sense to me. So I decided to create a angular directive based on my expectation.

Later, I published it as a separate npm package. And the package name is

angular-clipboard-auto

How it works?

Installation

To install this library, run:

$ npm install angular-clipboard-auto --save

Consuming your library

Once you have published your library to npm, you can import your library in any Angular application by running:

$ npm install angular-clipboard-auto

and then from your Angular AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
 
import { AppComponent } from './app.component';
 
// Import your library
import { ClipboardModule } from 'angular-clipboard-auto';
 
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
 
    // Specify your library as an import
    ClipboardModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Once your library is imported, you can use its components, directives and pipes in your Angular application:

<!-- You can now use your library component in app.component.html --&gt;
  
<!-- Add attribute appCopy in your html code --&gt;
  
<input appCopy type="text" #text1 value="hello world"&gt;
  
<!-- And refer font awesome in your index.html to view copy symbol --&gt;
  
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" crossorigin="anonymous"&gt;

That’s it!

TLTR;

All we have to do is

  • Add appCopy as attribute in text field HTML
  • Import fontawesome css

The copy icon will automatically get created next to your input field and you can use the feature right away.

The npm package will take care of all the background task seamlessly.

And also you can find the working copy of angular application in following path

https://stackblitz.com/edit/angular-clipboard

Sample snippet of HTML block

If you feel the implementation looks like, feel free to star ⭐️ the repository in GitHub and share with your friends.

Angular Clipboard Auto - Create a copy icon to your text element with one attribute. | Product Hunt Embed

Happy Coding!

Advertisements

Create custom WhatsApp stickers with your photo – See How?

WhatsApp has recently introduced stickers option. To get this feature, make sure you are using the latest version of WhatsApp.

So, how to create custom WhatsApp stickers?

Go to google play store, install the following app

Sticker maker for WhatsApp

Click here for the download link
‘Sticker maker for WhatsApp’ is one of the easiest ways to use stickers on WhatsApp and here is how to use:
  1. Make Sticker packs for a pet, your girlfriend, your family, your friends in 4 easy steps.
  2. And add a name for your pack
  3. Add the stickers to the pack, cut them with your finger.
  4. Publish the Sticker pack.

 

Now, a new page that lets you add up to 30 custom stickers of your own will open. However, the icon on top will be the tray icon or the icon which will appear as an identifier for you sticker pack in WhatsApp. Do note that the tray icon will not appear as a sticker.

Steps to create the sticker pack

Install the Sticker maker for WhatsApp app

Screenshot_20181111-173621

 

After installing, open the app and create sticker pack

Screenshot_20181111-173730

 

Add the images you wish to add in the pack, you need to add a minimum 3 stickers to save the pack

Screenshot_20181111-174219

 

Once added, you can publish and add to your WhatsApp. Make sure you are using images with no background or less background.

To clear the background, you can use the app “Background Eraser”

Screenshot Image

If you are just experimenting with the app, you can download a sample picture from the internet. Search in google for images with png format.
Screenshot_20181111-173821

 

 

Once you have published the sticker and added to Whatsapp. You can start sending to your friends.

 

I hope you have a funny picture of your friends. So give a try and leave your feedback in the comment section.

Have fun.