-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathHoverButtunStyle.swift
More file actions
30 lines (27 loc) · 946 Bytes
/
HoverButtunStyle.swift
File metadata and controls
30 lines (27 loc) · 946 Bytes
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
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
public init(isHovered: Bool = false, padding: CGFloat = 4, hoverColor: Color = Color.gray.opacity(0.1)) {
self.isHovered = isHovered
self.padding = padding
self.hoverColor = hoverColor
}
public func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding(padding)
.background(
configuration.isPressed
? Color.gray.opacity(0.2)
: isHovered
? hoverColor
: Color.clear
)
.cornerRadius(4)
.onHover { hover in
isHovered = hover
}
}
}