Skip to content

Commit d7480bf

Browse files
committed
👆 HoverView click and action passing
1 parent de48592 commit d7480bf

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

‎Sources/ShinySwiftUI/Views/HoverView.swift‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,52 @@
77

88
import SwiftUI
99

10+
@available(macOS 11.0, *)
1011
public struct HoverView<Content>: View where Content: View {
1112

1213
// MARK: - Public Wrapped Properties
1314

1415
/// The content to display.
15-
@ViewBuilder public let content: (Bool) -> Content
16+
@ViewBuilder public let content: (Bool, Bool) -> Content
1617

1718
/// Whether the view is hovered.
1819
@State public var hover: Bool = false
20+
/// Whether the view is clicked.
21+
@State public var clicked: Bool = false
22+
23+
// MARK: - Public Properties
24+
25+
/// The action to perform on click.
26+
public let action: () -> Void
1927

2028
// MARK: - Public Body View
2129

2230
public var body: some View {
23-
content(hover).onHover { hover = $0 }
31+
Button(action: action) {
32+
content(hover, clicked).onHover { hover = $0 }
33+
}
34+
.buttonStyle(HoverButtonStyle(pressed: $clicked))
2435
}
2536

2637
// MARK: - Public Initalizers
2738

28-
public init(content: @escaping (Bool) -> Content) {
29-
self.content = content
39+
public init(action: @escaping () -> Void = {}, content c: @escaping (Bool, Bool) -> Content) {
40+
self.action = action
41+
self.content = c
42+
}
43+
44+
public init(action: @escaping () -> Void = {}, content c: @escaping (Bool) -> Content) {
45+
self.action = action
46+
self.content = { (hover: Bool, click: Bool) in
47+
return c(hover)
48+
}
49+
}
50+
}
51+
52+
@available(macOS 11.0, *)
53+
fileprivate struct HoverButtonStyle: ButtonStyle {
54+
let pressed: Binding<Bool>
55+
func makeBody(configuration: Configuration) -> some View {
56+
configuration.label.onChange(of: configuration.isPressed) { val in pressed.wrappedValue = val }
3057
}
3158
}

0 commit comments

Comments
 (0)