Skip to content

Commit 957dd6b

Browse files
committed
Added copy button feature with styled code block
1 parent fbf02e4 commit 957dd6b

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

examples/Tip_Calculator/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ <h1>Tip Calculator</h1>
3030
<p>Total: ₹<span id="total">0.00</span></p>
3131
<p>Each Pays: ₹<span id="each">0.00</span></p>
3232
</div>
33+
34+
<div class="code-block">
35+
<pre><code>
36+
function calculateTip(bill, percentage) {
37+
return (bill * percentage) / 100;
38+
}
39+
</code></pre>
40+
</div>
3341
</div>
42+
3443
<script src="script.js"></script>
3544
</body>
3645

examples/Tip_Calculator/script.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,26 @@ function calculate() {
5656
resultsDiv.style.background = '#f1f5f9';
5757
}
5858

59+
document.querySelectorAll('pre > code').forEach(codeBlock => {
60+
const button = document.createElement('button');
61+
button.className = 'copy-btn';
62+
button.textContent = 'Copy';
63+
64+
const pre = codeBlock.parentNode;
65+
const wrapper = document.createElement('div');
66+
wrapper.className = 'code-block';
67+
pre.parentNode.insertBefore(wrapper, pre);
68+
wrapper.appendChild(pre);
69+
wrapper.appendChild(button);
70+
71+
button.addEventListener('click', () => {
72+
const text = codeBlock.textContent;
73+
navigator.clipboard.writeText(text).then(() => {
74+
button.textContent = 'Copied!';
75+
setTimeout(() => (button.textContent = 'Copy'), 2000);
76+
});
77+
});
78+
});
79+
5980
// Initial calculation on load
6081
calculate();

examples/Tip_Calculator/style.css

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,43 @@ input[type="number"] {
3333
border: 1px solid #ddd;
3434
}
3535

36+
.code-block {
37+
margin-top: 20px;
38+
}
39+
40+
.code-block pre {
41+
background-color: #f5f5f5;
42+
border: 1px solid #ddd;
43+
border-radius: 8px;
44+
padding: 12px;
45+
overflow-x: auto;
46+
font-size: 14px;
47+
line-height: 1.4;
48+
}
49+
50+
.copy-btn {
51+
position: absolute;
52+
top: 8px;
53+
right: 8px;
54+
background: #4CAF50;
55+
color: white;
56+
border: none;
57+
padding: 5px 10px;
58+
border-radius: 4px;
59+
cursor: pointer;
60+
font-size: 14px;
61+
transition: background 0.3s;
62+
}
63+
64+
.copy-btn:hover {
65+
background: #45a049;
66+
}
67+
68+
.code-block {
69+
position: relative;
70+
}
71+
72+
3673
.tips {
3774
display: flex;
3875
gap: 8px;

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
"main": "index.js",
66
"devDependencies": {
77
"@honkit/honkit-plugin-ga": "^1.0.1",
8-
"cz-conventional-changelog": "^3.3.0",
8+
"cz-conventional-changelog": "^3.0.1",
99
"gitbook-plugin-ace-editor": "github:sumn2u/gitbook-plugin-ace-editor",
1010
"gitbook-plugin-chapter-fold": "^0.0.4",
1111
"gitbook-plugin-edit-link": "^2.0.2",
12-
"gitbook-plugin-exercises": "^3.0.0",
12+
"gitbook-plugin-exercises": "^1.0.0",
1313
"gitbook-plugin-favicon-custom": "^1.0.0",
1414
"gitbook-plugin-hide-published-with": "0.0.1",
1515
"gitbook-plugin-hints": "^1.0.2",
@@ -18,7 +18,7 @@
1818
"gitbook-plugin-sidebar-ad": "github:sumn2u/gitbook-plugin-sidebar-ad",
1919
"gitbook-plugin-sitemap": "^1.2.0",
2020
"gitbook-plugin-theme-creative": "github:sumn2u/gitbook-plugin-theme-creative",
21-
"honkit": "^4.0.4",
21+
"honkit": "^6.1.4",
2222
"honkit-plugin-i18nsettings": "^1.0.0"
2323
},
2424
"scripts": {

0 commit comments

Comments
 (0)