clipboard.js/index.html
2021-01-19 05:04:12 -08:00

259 lines
14 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>clipboard.js &mdash; Copy to clipboard without Flash</title>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
<meta name="description" content="Copy text to the clipboard shouldn't be hard. It shouldn't require dozens of steps to configure or hundreds of KBs to load. But most of all, it shouldn't depend on Flash or any bloated framework. That's why clipboard.js exists.">
<meta property="og:title" content="clipboard.js">
<meta property="og:description" content="A modern approach to copy text to clipboard. No Flash. No frameworks. Just 3kb gzipped">
<meta property="og:url" content="https://zenorocha.github.io/clipboard.js/">
<meta property="og:image" content="https://zenorocha.github.io/clipboard.js/assets/images/facebook.png">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="bower_components/primer-css/css/primer.css">
<link rel="stylesheet" href="bower_components/highlightjs/styles/github.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,400,700,900">
<link rel="stylesheet" href="assets/styles/main.css">
</head>
<body>
<header class="header gradient text-center">
<h1 class="title">clipboard.js</h1>
<h2 class="subtitle">A modern approach to copy text to clipboard</h2>
<h2 class="subtitle">No Flash. No frameworks. Just 3kb gzipped</h2>
<p class="gh-btns">
<iframe src="https://ghbtns.com/github-btn.html?user=zenorocha&amp;repo=clipboard.js&amp;type=watch&amp;count=true&amp;size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="160" height="30"></iframe>
<iframe src="https://ghbtns.com/github-btn.html?user=zenorocha&amp;repo=clipboard.js&amp;type=fork&amp;count=true&amp;size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="160" height="30"></iframe>
</p>
</header>
<main class="wrap">
<h1 id="why">Why</h1>
<p>Copying text to the clipboard shouldn't be hard. It shouldn't require dozens of steps to configure or hundreds of KBs to load. But most of all, it shouldn't depend on Flash or any bloated framework.</p>
<p>That's why clipboard.js exists.</p>
<h1 id="install">Install</h1>
<p>You can get it on npm.</p>
<pre class="snippet"><code class="js">npm install clipboard --save</code></pre>
<p>Or if you're not into package management, just <a href="https://github.com/zenorocha/clipboard.js/archive/master.zip">download a ZIP</a> file.</p>
<h1 id="setup">Setup</h1>
<p>First, include the script located on the <code>dist</code> folder or load it from <a href="https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers">a third-party CDN provider</a>.</p>
<pre class="snippet"><code class="html">&lt;script src="dist/clipboard.min.js"&gt;&lt;/script&gt;</code></pre>
<p>Now, you need to instantiate it by <a href="https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-selector.html#L18">passing a DOM selector</a>, <a href="https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-node.html#L16-L17">HTML element</a>, or <a href="https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-nodelist.html#L18-L19">list of HTML elements</a>.</p>
<pre class="snippet"><code class="js">new ClipboardJS('.btn');</code></pre>
<p>Internally, we need to fetch all elements that matches with your selector and attach event listeners for each one. But guess what? If you have hundreds of matches, this operation can consume a lot of memory.</p>
<p>For this reason we use <a href="https://stackoverflow.com/questions/1687296/what-is-dom-event-delegation">event delegation</a> which replaces multiple event listeners with just a single listener. After all, <a href="https://twitter.com/hashtag/perfmatters">#perfmatters</a>.</p>
<h1 id="usage">Usage</h1>
<p>We're living a <em>declarative renaissance</em>, that's why we decided to take advantage of <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes">HTML5 data attributes</a> for better usability.</p>
<h3>Copy text from another element</h3>
<p>A pretty common use case is to copy content from another element. You can do that by adding a <code>data-clipboard-target</code> attribute in your trigger element.</p>
<p>The value you include on this attribute needs to match another's element selector.</p>
<div id="example-target" class="example">
<div class="input-group">
<input id="foo" type="text" value="https://github.com/zenorocha/clipboard.js.git">
<span class="input-group-button">
<button class="btn" type="button" data-clipboard-demo data-clipboard-target="#foo">
<img class="clippy" src="assets/images/clippy.svg" width="13" alt="Copy to clipboard">
</button>
</span>
</div>
</div>
<pre class="snippet"><code class="html">&lt;!-- Target --&gt;
&lt;input id="foo" value="https://github.com/zenorocha/clipboard.js.git"&gt;
&lt;!-- Trigger --&gt;
&lt;button class="btn" data-clipboard-target="#foo"&gt;
&lt;img src="assets/clippy.svg" alt="Copy to clipboard"&gt;
&lt;/button&gt;</code></pre>
<h3>Cut text from another element</h3>
<p>Additionally, you can define a <code>data-clipboard-action</code> attribute to specify if you want to either <code>copy</code> or <code>cut</code> content.</p>
<p>If you omit this attribute, <code>copy</code> will be used by default.</p>
<div id="example-action" class="example">
<div class="input-group">
<textarea id="bar" cols="62" rows="5" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">Mussum ipsum cacilds, vidis litro abertis. Consetis adipiscings elitis. Pra lá , depois divoltis porris, paradis. Paisis, filhis, espiritis santis. Mé faiz elementum girarzis, nisi eros vermeio, in elementis mé pra quem é amistosis quis leo. Manduma pindureta quium dia nois paga.</textarea>
</div>
<div class="form-actions">
<button class="btn" type="button" data-clipboard-demo data-clipboard-action="cut" data-clipboard-target="#bar">
Cut to clipboard
</button>
</div>
</div>
<pre class="snippet"><code class="html">&lt;!-- Target --&gt;
&lt;textarea id="bar"&gt;Mussum ipsum cacilds...&lt;/textarea&gt;
&lt;!-- Trigger --&gt;
&lt;button class="btn" data-clipboard-action="cut" data-clipboard-target="#bar"&gt;
Cut to clipboard
&lt;/button&gt;</code></pre>
<p>As you may expect, the <code>cut</code> action only works on <code>&lt;input&gt;</code> or <code>&lt;textarea&gt;</code> elements.</p>
<h3>Copy text from attribute</h3>
<p>Truth is, you don't even need another element to copy its content from. You can just include a <code>data-clipboard-text</code> attribute in your trigger element.</p>
<div id="example-text" class="example">
<button class="btn" data-clipboard-demo data-clipboard-action="copy" data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">Copy to clipboard</button>
</div>
<pre class="snippet"><code class="html">&lt;!-- Trigger --&gt;
&lt;button class="btn" data-clipboard-text="Just because you can doesn't mean you should — clipboard.js"&gt;
Copy to clipboard
&lt;/button&gt;</code></pre>
<h1 id="events">Events</h1>
<p>There are cases where you'd like to show some user feedback or capture what has been selected after a copy/cut operation.</p>
<p>That's why we fire custom events such as <code>success</code> and <code>error</code> for you to listen and implement your custom logic.</p>
<pre class="snippet"><code class="js">var clipboard = new ClipboardJS('.btn');
clipboard.on('success', function(e) {
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
e.clearSelection();
});
clipboard.on('error', function(e) {
console.error('Action:', e.action);
console.error('Trigger:', e.trigger);
});</code></pre>
<p>For a live demonstration, just open your console :)</p>
<h1 id="tooltips">Tooltips</h1>
<p>Each application has different design needs, that's why clipboard.js does not include any CSS or built-in tooltip solution.</p>
<p>The tooltips you see on this demo site were built using <a href="https://primer.style/css/components/tooltips">GitHub's Primer</a>. You may want to check that out if you're looking for a similar look and feel.</p>
<h1 id="advanced-usage">Advanced Usage</h1>
<p>If you don't want to modify your HTML, there's a pretty handy imperative API for you to use. All you need to do is declare a function, do your thing, and return a value.</p>
<p>For instance, if you want to dynamically set a <code>target</code>, you'll need to return a Node.</p>
<pre class="snippet"><code class="js">new ClipboardJS('.btn', {
target: function(trigger) {
return trigger.nextElementSibling;
}
});</code></pre>
<p>If you want to dynamically set a <code>text</code>, you'll return a String.</p>
<pre class="snippet"><code class="js">new ClipboardJS('.btn', {
text: function(trigger) {
return trigger.getAttribute('aria-label');
}
});</code></pre>
<p>For use in Bootstrap Modals or with any other library that changes the focus you'll want to set the focused element as the <code>container</code> value.</p>
<pre class="snippet"><code class="js">new ClipboardJS('.btn', {
container: document.getElementById('modal')
});</code></pre>
<p>Also, if you are working with single page apps, you may want to manage the lifecycle of the DOM more precisely. Here's how you clean up the events and objects that we create.</p>
<pre class="snippet"><code class="js">var clipboard = new ClipboardJS('.btn');
clipboard.destroy();</code></pre>
<h1 id="browser-support">Browser Support</h1>
<p>This library relies on both <a href="https://developer.mozilla.org/en-US/docs/Web/API/Selection">Selection</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand">execCommand</a> APIs. The first one is <a href="http://caniuse.com/#search=selection">supported by all browsers</a> while the second one is supported in the following browsers.</p>
<ul class="support">
<li>
<img src="assets/images/chrome.png" width="64" height="64" alt="Chrome logo">
<p>Chrome 42+</p>
</li>
<li>
<img src="assets/images/edge.png" width="64" height="64" alt="Edge logo">
<p>Edge 12+</p>
</li>
<li>
<img src="assets/images/firefox.png" width="64" height="64" alt="Firefox logo">
<p>Firefox 41+</p>
</li>
<li>
<img src="assets/images/ie.png" width="64" height="64" alt="Internet Explorer logo">
<p>IE 9+</p>
</li>
<li>
<img src="assets/images/opera.png" width="64" height="64" alt="Opera logo">
<p>Opera 29+</p>
</li>
<li>
<img src="assets/images/safari.png" width="64" height="64" alt="Safari logo">
<p>Safari 10+</p>
</li>
</ul>
<p>The good news is that clipboard.js gracefully degrades if you need to support older browsers. All you have to do is show a tooltip saying <code>Copied!</code> when <code>success</code> event is called and <code>Press Ctrl+C to copy</code> when <code>error</code> event is called because the text is already selected.</p>
<p>You can also check if clipboard.js is supported or not by running <code>ClipboardJS.isSupported()</code>, that way you can hide copy/cut buttons from the UI.</p>
<h1 id="bonus">Bonus</h1>
<p>A browser extension that adds a "copy to clipboard" button to every code block on <em>GitHub, MDN, Gist, StackOverflow, StackExchange, npm, and even Medium.</em></p>
<p>Install for <a href="https://chrome.google.com/webstore/detail/codecopy/fkbfebkcoelajmhanocgppanfoojcdmg">Chrome</a> and <a href="https://addons.mozilla.org/en-US/firefox/addon/codecopy/">Firefox</a>.</p>
</main>
<footer class="footer gradient text-center">
<p class="credits">
Made with <span class="love"></span> by <a class="footer-link" href="http://zenorocha.com/">Zeno Rocha</a> under <a class="footer-link" href="http://zenorocha.mit-license.org/">MIT license</a>
</p>
<p class="maintainer">
Brought to you by <a class="footer-link" href="http://liferay.com">Liferay</a>
</p>
</footer>
<script src="bower_components/highlightjs/highlight.pack.min.js"></script>
<script src="dist/clipboard.min.js"></script>
<script src="assets/scripts/demos.js"></script>
<script src="assets/scripts/snippets.js"></script>
<script src="assets/scripts/tooltips.js"></script>
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-4114546-44', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>