Skip to content

Commit d71ccaa

Browse files
committed
feat: auto copy button code
1 parent b902cb7 commit d71ccaa

File tree

7 files changed

+78
-57
lines changed

7 files changed

+78
-57
lines changed

assets/js/clipboard.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
import Clipboard from 'clipboard';
22

3-
var clipboard = new Clipboard('.btn-clipboard');
3+
var pre = document.getElementsByTagName('pre');
4+
5+
for (var i = 0; i < pre.length; ++ i)
6+
{
7+
var element = pre[i];
8+
element.insertAdjacentHTML('afterbegin', '<button class="btn btn-copy"></button>');
9+
}
10+
11+
var clipboard = new Clipboard('.btn-copy', {
12+
13+
target: function(trigger) {
14+
return trigger.nextElementSibling;
15+
},
16+
17+
});
418

519
clipboard.on('success', function(e) {
20+
621
/*
722
console.info('Action:', e.action);
823
console.info('Text:', e.text);

assets/scss/common/_variables.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ $pink-500: #d32e9d;
2424

2525
$primary: $purple;
2626

27+
$color-btn-bg: $pink-500;
28+
$color-btn-border: darken($pink-500, 5%);
29+
$color-btn-text: $white;
30+
2731
// Options
2832
//
2933
// Quickly modify global styling by enabling or disabling optional features.

assets/scss/components/_buttons.scss

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,44 +50,79 @@ body.dark .toggle-dark {
5050
display: none;
5151
}
5252

53-
.btn-clipboard {
54-
display: none;
53+
pre {
54+
position: relative;
5555
}
5656

57-
@include media-breakpoint-up(md) {
58-
.doks-clipboard {
59-
position: relative;
60-
float: right;
57+
@include media-breakpoint-down(md) {
58+
.btn-copy {
59+
display: none;
6160
}
61+
}
62+
63+
.btn-copy {
64+
transition: opacity 0.3s ease-in-out;
65+
opacity: 0;
66+
position: absolute;
67+
right: 0.25rem;
68+
top: 0.25rem;
69+
z-index: 10;
70+
font-family: $font-family-sans-serif;
71+
font-size: $font-size-sm;
72+
padding: 0.25rem 0.5rem;
73+
color: $color-btn-text;
74+
background-color: $color-btn-bg;
75+
border-color: $color-btn-border;
76+
}
6277

63-
.btn-clipboard {
64-
position: absolute;
65-
top: 1rem;
66-
right: 0.25rem;
67-
z-index: 10;
68-
display: block;
69-
padding: 0.25rem 0.5rem;
70-
font-size: $font-size-sm;
78+
.btn-copy:hover {
79+
color: $color-btn-text;
80+
background-color: lighten($color-btn-bg, 5%);
81+
border-color: lighten($color-btn-border, 15%);
82+
}
83+
84+
.btn-copy:focus {
85+
color: $color-btn-text;
86+
background-color: $color-btn-bg;
87+
border-color: lighten($color-btn-border, 15%);
88+
box-shadow: none;
89+
}
90+
91+
.btn-copy:active,
92+
.btn-copy.active {
93+
color: $color-btn-text;
94+
background-color: $color-btn-bg;
95+
border-color: lighten($color-btn-border, 15%);
96+
}
97+
98+
.btn-copy:active:focus,
99+
.btn-copy.active:focus {
100+
box-shadow: none;
101+
}
102+
103+
@include media-breakpoint-up(md) {
104+
pre:hover .btn-copy {
105+
opacity: 1;
71106
}
72107
}
73108

74-
.copy-status::after {
109+
.btn-copy::after {
75110
content: "Copy";
76111
display: block;
77-
color: $body-color;
112+
color: $color-btn-text;
78113
}
79114

80-
.copy-status:hover::after {
115+
.btn-copy:hover::after {
81116
content: "Copy";
82117
display: block;
83-
color: $pink-500;
118+
color: $color-btn-text;
84119
}
85120

86-
.copy-status:focus::after,
87-
.copy-status:active::after {
121+
.btn-copy:focus::after,
122+
.btn-copy:active::after {
88123
content: "Copied";
89124
display: block;
90-
color: $pink-500;
125+
color: $color-btn-text;
91126
}
92127

93128
.collapsible-sidebar {

config/postcss.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ module.exports = {
1818
'tr',
1919
'th',
2020
'td',
21+
'h5',
2122
...whitelister([
23+
'./assets/scss/components/_buttons.scss',
2224
'./assets/scss/components/_code.scss',
2325
'./assets/scss/components/_syntax.scss',
2426
'./assets/scss/components/_search.scss',

content/docs/prologue/commands.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ toc: true
1919

2020
Create new content for your site:
2121

22-
{{< btn-copy text="npm run create" >}}
23-
2422
```bash
2523
npm run create [path] [flags]
2624
```
@@ -31,8 +29,6 @@ See also the Hugo docs: [hugo new](https://gohugo.io/commands/hugo_new/).
3129

3230
Check scripts, styles, and markdown for errors:
3331

34-
{{< btn-copy text="npm run lint" >}}
35-
3632
```bash
3733
npm run lint
3834
```
@@ -41,8 +37,6 @@ npm run lint
4137

4238
Check scripts for errors:
4339

44-
{{< btn-copy text="npm run lint:scripts" >}}
45-
4640
```bash
4741
npm run lint:scripts [-- --fix]
4842
```
@@ -51,8 +45,6 @@ npm run lint:scripts [-- --fix]
5145

5246
Check styles for errors:
5347

54-
{{< btn-copy text="npm run lint:styles" >}}
55-
5648
```bash
5749
npm run lint:styles [-- --fix]
5850
```
@@ -61,8 +53,6 @@ npm run lint:styles [-- --fix]
6153

6254
Check markdown for errors:
6355

64-
{{< btn-copy text="npm run lint:markdown" >}}
65-
6656
```bash
6757
npm run lint:markdown [-- --fix]
6858
```
@@ -71,8 +61,6 @@ npm run lint:markdown [-- --fix]
7161

7262
Delete temporary directories:
7363

74-
{{< btn-copy text="npm run clean" >}}
75-
7664
```bash
7765
npm run clean
7866
```
@@ -81,8 +69,6 @@ npm run clean
8169

8270
Start local development server:
8371

84-
{{< btn-copy text="npm run start" >}}
85-
8672
```bash
8773
npm run start
8874
```
@@ -91,8 +77,6 @@ npm run start
9177

9278
Build production website:
9379

94-
{{< btn-copy text="npm run build" >}}
95-
9680
```bash
9781
npm run build
9882
```
@@ -101,8 +85,6 @@ npm run build
10185

10286
Build Lambda functions:
10387

104-
{{< btn-copy text="npm run build:functions" >}}
105-
10688
```bash
10789
npm run build:functions
10890
```
@@ -111,8 +93,6 @@ npm run build:functions
11193

11294
Build production website including draft and future content:
11395

114-
{{< btn-copy text="npm run build:preview" >}}
115-
11696
```bash
11797
npm run build:preview
11898
```

content/docs/prologue/quick-start.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,30 @@ Not quite sure? Use the Doks child theme.
3434

3535
#### Doks child theme
3636

37-
{{< btn-copy text="git clone https://github.com/h-enk/doks-child-theme.git my-doks-site" >}}
38-
3937
```bash
4038
git clone https://github.com/h-enk/doks-child-theme.git my-doks-site
4139
```
4240

4341
#### Doks starter theme
4442

45-
{{< btn-copy text="git clone https://github.com/h-enk/doks.git my-doks-site" >}}
46-
4743
```bash
4844
git clone https://github.com/h-enk/doks.git my-doks-site
4945
```
5046

5147
### Change directories
5248

53-
{{< btn-copy text="cd my-doks-site" >}}
54-
5549
```bash
5650
cd my-doks-site
5751
```
5852

5953
### Install dependencies
6054

61-
{{< btn-copy text="npm install" >}}
62-
6355
```bash
6456
npm install
6557
```
6658

6759
### Start development server
6860

69-
{{< btn-copy text="npm run start" >}}
70-
7161
```bash
7262
npm run start
7363
```

layouts/shortcodes/btn-copy.html

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)