Skip to content

Commit 4d7f75b

Browse files
Copy to Clipboard: Added support for custom styles (#2789)
1 parent 5943f4c commit 4d7f75b

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

plugins/copy-to-clipboard/index.html

+21-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
1515
<script src="https://www.google-analytics.com/ga.js" async></script>
1616
</head>
17-
<body>
17+
<body class="language-text">
1818

1919
<header data-plugin-header="copy-to-clipboard"></header>
2020

@@ -41,6 +41,26 @@ <h1>Settings</h1>
4141
<p><strong>Warning!</strong> Although possible, you definitely shouldn't add these attributes to the <code class="token tag">html</code> element, because a human-readable text should be placed <em>after</em> the character encoding declaration (<code>&lt;meta charset=&quot;...&quot;&gt;</code>), and the latter <a href="https://html.spec.whatwg.org/multipage/semantics.html#charset">must be</a> serialized completely within the first 512 (in older browsers) or 1024 bytes of the document. Consider using the <code class="token tag">body</code> element or one of its descendants.</p>
4242
</section>
4343

44+
<section>
45+
<h1>Styling</h1>
46+
47+
<p>This plugin supports customizing the style of the copy button. To understand how this is done, let's look at the HTML structure of the copy button:</p>
48+
49+
<pre><code class="language-markup">&lt;button class="copy-to-clipboard-button" type="button" data-copy-state="copy">
50+
&lt;span>Copy&lt;/span>
51+
&lt;/button></code></pre>
52+
53+
<p>The <code>copy-to-clipboard-button</code> class can be used to select the button. The <code>data-copy-state</code> attribute indicates the current state of the plugin with the 3 possible states being:</p>
54+
55+
<ul>
56+
<li><code>data-copy-state="copy"</code> — default state;</li>
57+
<li><code>data-copy-state="copy-error"</code> — the state after failing copying;</li>
58+
<li><code>data-copy-state="copy-success"</code> — the state after successful copying;</li>
59+
</ul>
60+
61+
<p>These 3 states should be conveyed to the user either by different styling or displaying the button text.</p>
62+
</section>
63+
4464
<section>
4565
<h1>Examples</h1>
4666

plugins/copy-to-clipboard/prism-copy-to-clipboard.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,24 @@
114114
var settings = getSettings(element);
115115

116116
var linkCopy = document.createElement('button');
117-
linkCopy.textContent = settings['copy'];
117+
linkCopy.className = 'copy-to-clipboard-button';
118118
linkCopy.setAttribute('type', 'button');
119+
var linkSpan = document.createElement('span');
120+
linkCopy.appendChild(linkSpan);
121+
122+
setState('copy');
119123

120124
registerClipboard(linkCopy, {
121125
getText: function () {
122126
return element.textContent;
123127
},
124128
success: function () {
125-
linkCopy.textContent = settings['copy-success'];
129+
setState('copy-success');
126130

127131
resetText();
128132
},
129133
error: function () {
130-
linkCopy.textContent = settings['copy-error'];
134+
setState('copy-error');
131135

132136
setTimeout(function () {
133137
selectElementText(element);
@@ -140,9 +144,13 @@
140144
return linkCopy;
141145

142146
function resetText() {
143-
setTimeout(function () {
144-
linkCopy.textContent = settings['copy'];
145-
}, settings['copy-timeout']);
147+
setTimeout(function () { setState('copy'); }, settings['copy-timeout']);
148+
}
149+
150+
/** @param {"copy" | "copy-error" | "copy-success"} state */
151+
function setState(state) {
152+
linkSpan.textContent = settings[state];
153+
linkCopy.setAttribute('data-copy-state', state);
146154
}
147155
});
148156
})();

plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)