-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathHoverButtunStyle.swift
More file actions
40 lines (37 loc) · 1.19 KB
/
HoverButtunStyle.swift
File metadata and controls
40 lines (37 loc) · 1.19 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
40
import SwiftUI
// This is a custom button style that changes its background color when hovered
public struct HoverButtonStyle: ButtonStyle {
@State private var isHovered: Bool
private var padding: CGFloat
private var hoverColor: Color
private var backgroundColor: Color
private var cornerRadius: CGFloat
public init(
isHovered: Bool = false,
padding: CGFloat = 4,
hoverColor: Color = .hoverColor,
backgroundColor: Color = .clear,
cornerRadius: CGFloat = 4
) {
self.isHovered = isHovered
self.padding = padding
self.hoverColor = hoverColor
self.backgroundColor = backgroundColor
self.cornerRadius = cornerRadius
}
public func makeBody(configuration: Configuration) -> some View {
configuration.label
.scaledPadding(padding)
.background(
configuration.isPressed
? Color.gray.opacity(0.2)
: isHovered
? hoverColor
: backgroundColor
)
.cornerRadius(cornerRadius)
.onHover { hover in
isHovered = hover
}
}
}