|
1 | | -# kopasin-js |
2 | | -kopasin-js is a simple, lightweight, and customizable JavaScript library for adding copy-paste interactivity to your projects |
| 1 | +# KopasinJS |
| 2 | + |
| 3 | +**kopasin-js** is a lightweight and customizable JavaScript library designed to add copy-paste functionality to your web projects. With `kopasin-js`, you can effortlessly enable users to copy and paste text with minimal setup and maximum flexibility. This library use 0% of JQuery. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +- **Simple Setup:** Easy to integrate with just a few lines of code. |
| 13 | +- **Customizable:** Tailor the library to fit your specific needs. |
| 14 | +- **Lightweight:** Minimal impact on your project's performance. |
| 15 | +- **Cross-Browser Support:** Works seamlessly across modern web browsers. |
| 16 | + |
| 17 | +## Visits |
| 18 | + |
| 19 | +- [Demo Site](https://kopasin-js.galih.me) |
| 20 | +- [Portfolio](https://galih.me) |
| 21 | +- [Galih Blogs](https://masgalih.net) |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +To get started with `kopasin-js`, you can either include it via CDN or install it via npm. |
| 26 | + |
| 27 | +### Using CDN |
| 28 | + |
| 29 | +Add the following script to your HTML: |
| 30 | + |
| 31 | +```html |
| 32 | +# USING CDN |
| 33 | +<script src="https://cdn.jsdelivr.net/npm/kopasin-js@latest/dist/kopasin-js.global.js"></script> |
| 34 | +# OR |
| 35 | +<script src="https://unpkg.com/kopasin-js@1.1.7/dist/kopasin-js.global.js"></script> |
| 36 | + |
| 37 | +# USING DOWNLOADED FILES |
| 38 | +<script src="dist/kopasin-js.global.js"></script> |
| 39 | +``` |
| 40 | + |
| 41 | +## With Import |
| 42 | + |
| 43 | +Install via NPM |
| 44 | + |
| 45 | +```bash |
| 46 | +npm i kopasin-js |
| 47 | +``` |
| 48 | + |
| 49 | +And then import the library |
| 50 | + |
| 51 | +```js |
| 52 | +import KopasinJS from "kopasin-js" |
| 53 | +``` |
| 54 | + |
| 55 | +## Example Usage |
| 56 | + |
| 57 | +### Copying Text from an Input |
| 58 | + |
| 59 | +To copy a value from input element, we need to create 2 element, which is the `<input />` element and `<button></button>` element |
| 60 | + |
| 61 | +```html |
| 62 | +<input type="text" id="copy-this" value="Copy Me" readonly> |
| 63 | +<button type="button" id="copy-button">Copy</button> |
| 64 | +``` |
| 65 | + |
| 66 | +After that, load KopasinJS like this |
| 67 | + |
| 68 | +```js |
| 69 | +new Kopasin({ |
| 70 | + copy: { |
| 71 | + on: 'click', |
| 72 | + trigger: document.getElementById('copy-button'), |
| 73 | + source: document.getElementById('copy-this'), |
| 74 | + success: function () { |
| 75 | + console.log('Copied!') |
| 76 | + }, |
| 77 | + error: function () { |
| 78 | + console.log('Failed to copy') |
| 79 | + } |
| 80 | + }, |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | +The `success` and `error` callback is optional, but you can use the `success` callback for improving the user experience with interactivity after copying is successful. |
| 85 | + |
| 86 | +### Copying Text from an Element |
| 87 | + |
| 88 | +To copy a value from an element (example: `<div>`), we also need to create 2 element, which is the `<div></div>` element and `<button></button>` element. |
| 89 | + |
| 90 | +```html |
| 91 | +<div id="copy-this">Copy Me</div> <!-- You can also use another element like <p>, <span> and anything --> |
| 92 | +<button type="button" id="copy-button">Copy</button> |
| 93 | +``` |
| 94 | + |
| 95 | +For the javascript code is still same. |
| 96 | + |
| 97 | +```js |
| 98 | +new Kopasin({ |
| 99 | + copy: { |
| 100 | + on: 'click', |
| 101 | + trigger: document.getElementById('copy-button'), |
| 102 | + source: document.getElementById('copy-this'), |
| 103 | + success: function () { |
| 104 | + console.log('Copied!') |
| 105 | + }, |
| 106 | + error: function () { |
| 107 | + console.log('Failed to copy') |
| 108 | + } |
| 109 | + }, |
| 110 | +}); |
| 111 | +``` |
| 112 | + |
| 113 | +## Options for `copy` |
| 114 | + |
| 115 | +| Key | Type | Description | Required | Default Value | |
| 116 | +|---------|-------------|------------------------------------------------------------------------------------------------------------------------|----------|---------------| |
| 117 | +| on | String | The event that will be used to trigger the element in the `trigger` option, must be valid event for `addEventListener` | yes | click | |
| 118 | +| trigger | HTMLElement | This element will be used to trigger the copy process | yes | | |
| 119 | +| source | HTMLElement | Element that you want to copy the value/text from | yes | | |
| 120 | +| success | Function | Function that will fired after copying is done | no | () => {} | |
| 121 | +| error | Function | Function that will fired after copying is failed | no | () => {} | |
| 122 | + |
| 123 | +## Options for `paste` |
| 124 | + |
| 125 | +| Key | Type | Description | Required | Default Value | |
| 126 | +|---------|---------------------------------|------------------------------------------------------------------------------------------------------------------------|----------|---------------| |
| 127 | +| on | String | The event that will be used to trigger the element in the `trigger` option, must be valid event for `addEventListener` | yes | click | |
| 128 | +| trigger | HTMLElement | This element will be used to trigger the pasting process | yes | | |
| 129 | +| target | HTMLInputElement \| HTMLElement | Target element that the value/text will replaced with a value from your clipboard | yes | | |
| 130 | +| success | Function | Function that will fired after pasting is done | no | () => {} | |
| 131 | +| error | Function | This function will fired if pasting are failed | no | () => {} | |
| 132 | + |
| 133 | +## Donate |
| 134 | + |
| 135 | +If you have used this library and it's useful for you, please consider to donate: |
| 136 | + |
| 137 | +[Saweria](https://saweria.co/sukristyan) |
| 138 | + |
| 139 | +## License |
| 140 | + |
| 141 | +The KopasinJS is open-sourced software licensed under the [MIT license](/LICENSE.md). |
0 commit comments