-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathCopyButton.swift
More file actions
39 lines (36 loc) · 1.13 KB
/
CopyButton.swift
File metadata and controls
39 lines (36 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import AppKit
import SwiftUI
public struct CopyButton: View {
public var copy: () -> Void
@State var isCopied = false
public init(copy: @escaping () -> Void) {
self.copy = copy
}
public var body: some View {
Button(action: {
withAnimation(.linear(duration: 0.1)) {
isCopied = true
}
copy()
Task {
try await Task.sleep(nanoseconds: 1_000_000_000)
withAnimation(.linear(duration: 0.1)) {
isCopied = false
}
}
}) {
Image(systemName: isCopied ? "checkmark.circle" : "doc.on.doc")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 14, height: 14)
.frame(width: 20, height: 20, alignment: .center)
.foregroundColor(.secondary)
.background(
.regularMaterial,
in: RoundedRectangle(cornerRadius: 4, style: .circular)
)
.padding(4)
}
.buttonStyle(.borderless)
}
}