-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathViewExtension.swift
More file actions
76 lines (62 loc) · 2.65 KB
/
ViewExtension.swift
File metadata and controls
76 lines (62 loc) · 2.65 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import SwiftUI
let ITEM_SELECTED_COLOR = Color("ItemSelectedColor")
struct HoverBackgroundModifier: ViewModifier {
var isHovered: Bool
func body(content: Content) -> some View {
content
.background(isHovered ? ITEM_SELECTED_COLOR : Color.clear)
}
}
struct HoverRadiusBackgroundModifier: ViewModifier {
var isHovered: Bool
var hoverColor: Color?
var cornerRadius: CGFloat = 0
var showBorder: Bool = false
var borderColor: Color = .white.opacity(0.07)
var borderWidth: CGFloat = 1
func body(content: Content) -> some View {
content
.background(
RoundedRectangle(cornerRadius: cornerRadius)
.fill(isHovered ? hoverColor ?? ITEM_SELECTED_COLOR : Color.clear)
)
.overlay(
Group {
if isHovered && showBorder {
RoundedRectangle(cornerRadius: cornerRadius)
.stroke(borderColor, lineWidth: borderWidth)
}
}
)
}
}
struct HoverForegroundModifier: ViewModifier {
var isHovered: Bool
var defaultColor: Color
func body(content: Content) -> some View {
content.foregroundColor(isHovered ? Color.white : defaultColor)
}
}
extension View {
public func hoverBackground(isHovered: Bool) -> some View {
self.modifier(HoverBackgroundModifier(isHovered: isHovered))
}
public func hoverRadiusBackground(isHovered: Bool, cornerRadius: CGFloat) -> some View {
self.modifier(HoverRadiusBackgroundModifier(isHovered: isHovered, cornerRadius: cornerRadius))
}
public func hoverRadiusBackground(isHovered: Bool, hoverColor: Color?, cornerRadius: CGFloat) -> some View {
self.modifier(HoverRadiusBackgroundModifier(isHovered: isHovered, hoverColor: hoverColor, cornerRadius: cornerRadius))
}
public func hoverRadiusBackground(isHovered: Bool, hoverColor: Color?, cornerRadius: CGFloat, showBorder: Bool) -> some View {
self.modifier(HoverRadiusBackgroundModifier(isHovered: isHovered, hoverColor: hoverColor, cornerRadius: cornerRadius, showBorder: showBorder))
}
public func hoverForeground(isHovered: Bool, defaultColor: Color) -> some View {
self.modifier(HoverForegroundModifier(isHovered: isHovered, defaultColor: defaultColor))
}
public func hoverPrimaryForeground(isHovered: Bool) -> some View {
self.hoverForeground(isHovered: isHovered, defaultColor: .primary)
}
public func hoverSecondaryForeground(isHovered: Bool) -> some View {
self.hoverForeground(isHovered: isHovered, defaultColor: .secondary)
}
}