7
7
8
8
import SwiftUI
9
9
10
- @available ( macOS 11 . 0 , * )
10
+ @available ( macOS 11 . 0 , iOS 14 . 0 , * )
11
11
public struct HoverView < Content> : View where Content: View {
12
12
13
13
// MARK: - Public Wrapped Properties
@@ -24,32 +24,59 @@ public struct HoverView<Content>: View where Content: View {
24
24
25
25
/// The action to perform on click.
26
26
public let action : ( ) -> Void
27
+ /// The action to perform on hover.
28
+ public let onHover : ( Bool ) -> Void
27
29
28
30
// MARK: - Public Body View
29
31
30
32
public var body : some View {
31
33
Button ( action: action) {
32
- content ( hover, clicked) . onHover { hover = $0 }
34
+ content ( hover, clicked) . onHover {
35
+ hover = $0
36
+ onHover ( $0)
37
+ }
33
38
}
34
39
. buttonStyle ( HoverButtonStyle ( pressed: $clicked) )
35
40
}
36
41
37
42
// MARK: - Public Initalizers
38
43
39
- public init ( action: @escaping ( ) -> Void = { } , content c: @escaping ( Bool , Bool ) -> Content ) {
44
+ public init ( action: @escaping ( ) -> Void = { } , onHover : @escaping ( Bool ) -> Void = { _ in } , content c: @escaping ( Bool , Bool ) -> Content ) {
40
45
self . action = action
46
+ self . onHover = onHover
41
47
self . content = c
42
48
}
43
49
44
- public init ( action: @escaping ( ) -> Void = { } , content c: @escaping ( Bool ) -> Content ) {
50
+ public init ( action: @escaping ( ) -> Void = { } , onHover : @escaping ( Bool ) -> Void = { _ in } , content c: @escaping ( Bool ) -> Content ) {
45
51
self . action = action
52
+ self . onHover = onHover
46
53
self . content = { ( hover: Bool , click: Bool ) in
47
54
return c ( hover)
48
55
}
49
56
}
57
+
58
+ // MARK: - Public Methods
59
+
60
+ /**
61
+ Animates the `hover` property of the view.
62
+
63
+ - parameter animation: The Animation to use.
64
+ */
65
+ public func animateHover( _ animation: Animation = . slickEaseOut) -> some View {
66
+ return self . animation ( animation, value: hover)
67
+ }
68
+
69
+ /**
70
+ Animates the `clicked` property of the view.
71
+
72
+ - parameter animation: The Animation to use.
73
+ */
74
+ public func animateClicked( _ animation: Animation = . slickEaseOut) -> some View {
75
+ return self . animation ( animation, value: clicked)
76
+ }
50
77
}
51
78
52
- @available ( macOS 11 . 0 , * )
79
+ @available ( macOS 11 . 0 , iOS 14 . 0 , * )
53
80
fileprivate struct HoverButtonStyle : ButtonStyle {
54
81
let pressed : Binding < Bool >
55
82
func makeBody( configuration: Configuration ) -> some View {
0 commit comments