Skip to content

Commit 4f01205

Browse files
committed
Add copy icon to code examples
Fixes nightwatchjs#4350 Add a copy icon to code usage examples for better user experience. * Modify `api/README.md` to include a copy icon for each code example box. * Add a JavaScript function in `api/README.md` to handle the copy action. * Add a CSS class in `api/README.md` for the copy icon styling. * Modify `examples/test-app/index.html` to include a copy icon for each code example box. * Add a JavaScript function in `examples/test-app/index.html` to handle the copy action. * Add a CSS class in `examples/test-app/index.html` for the copy icon styling.
1 parent 32b40a6 commit 4f01205

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

api/README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Public commands api exported by Nightwatch in order to be extended upon from out
33

44
**Example:**
55

6-
```js
6+
<div class="code-example">
7+
<button class="copy-button" onclick="copyToClipboard(this)">Copy</button>
8+
<pre><code>
79
const {Quit: quit} = require('nightwatch/api');
810

911
module.exports = CustomQuit extends Quit {
@@ -13,7 +15,8 @@ module.exports = CustomQuit extends Quit {
1315
await super.command(cb);
1416
}
1517
}
16-
```
18+
</code></pre>
19+
</div>
1720

1821
**This is a work in progress.**
1922

@@ -56,5 +59,34 @@ module.exports = CustomQuit extends Quit {
5659

5760
## Utilities & Debugging:
5861

62+
<script>
63+
function copyToClipboard(button) {
64+
const code = button.nextElementSibling.innerText;
65+
navigator.clipboard.writeText(code).then(() => {
66+
button.innerText = 'Copied!';
67+
setTimeout(() => {
68+
button.innerText = 'Copy';
69+
}, 2000);
70+
});
71+
}
72+
</script>
5973

74+
<style>
75+
.code-example {
76+
position: relative;
77+
}
6078

79+
.copy-button {
80+
position: absolute;
81+
top: 10px;
82+
right: 10px;
83+
background-color: #f5f5f5;
84+
border: 1px solid #ccc;
85+
padding: 5px 10px;
86+
cursor: pointer;
87+
}
88+
89+
.copy-button:hover {
90+
background-color: #e0e0e0;
91+
}
92+
</style>

examples/test-app/index.html

+37-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@
1717
section {
1818
padding: 10px;
1919
}
20+
21+
.code-example {
22+
position: relative;
23+
}
24+
25+
.copy-button {
26+
position: absolute;
27+
top: 10px;
28+
right: 10px;
29+
background-color: #f5f5f5;
30+
border: 1px solid #ccc;
31+
padding: 5px 10px;
32+
cursor: pointer;
33+
}
34+
35+
.copy-button:hover {
36+
background-color: #e0e0e0;
37+
}
2038
</style>
2139

2240
</head>
@@ -74,11 +92,29 @@ <h2>navigate</h2>
7492
<a href="page2.html">Go to Page 2</a>
7593
</section>
7694

95+
<section class="code-example">
96+
<button class="copy-button" onclick="copyToClipboard(this)">Copy</button>
97+
<pre><code>
98+
const exampleCode = 'This is an example code snippet.';
99+
console.log(exampleCode);
100+
</code></pre>
101+
</section>
102+
77103
<script>
78104
document
79105
.querySelector('[data-testid="image-with-random-alt-tag"]')
80106
.setAttribute("alt", "Image Random Alt Text " + Math.random());
107+
108+
function copyToClipboard(button) {
109+
const code = button.nextElementSibling.innerText;
110+
navigator.clipboard.writeText(code).then(() => {
111+
button.innerText = 'Copied!';
112+
setTimeout(() => {
113+
button.innerText = 'Copy';
114+
}, 2000);
115+
});
116+
}
81117
</script>
82118
</body>
83119

84-
</html>
120+
</html>

0 commit comments

Comments
 (0)