Skip to content

Commit

Permalink
Add toggle to present and dismiss tooltip on demand (#21)
Browse files Browse the repository at this point in the history
* feat: add isEnabled to toggle tooltip visibility

* refactor: remove implicit animation and add transition modifier

* refactor: remove scale in transition and use opacity only.

* fix: move transition modifier to tooltipBody instead of content

* feat: add transition configuration

* refactor: rename isEnabled to enabled. Shift enabled argument as first argument to remove label requirement.

* refactor: add default true value to enabled argument

* refactor: remove Binding property wrapper on enabled

* feat: add function definitions for tooltip for backwards compatibility

* refactor: remove unnecessary enabled argument
  • Loading branch information
chenium authored Apr 26, 2022
1 parent b1f0d41 commit 9f617c2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public struct ArrowOnlyTooltipConfig: TooltipConfig {
public var animationOffset: CGFloat = 10
public var animationTime: Double = 1

public var transition: AnyTransition = .opacity

public init() {}

public init(side: TooltipSide) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public struct DefaultTooltipConfig: TooltipConfig {
public var animationOffset: CGFloat = 10
public var animationTime: Double = 1

public var transition: AnyTransition = .opacity

public init() {}

public init(side: TooltipSide) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ public protocol TooltipConfig {
var enableAnimation: Bool { get set }
var animationOffset: CGFloat { get set }
var animationTime: Double { get set }

var transition: AnyTransition { get set }
}
9 changes: 5 additions & 4 deletions Sources/SwiftUITooltip/TooltipModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import SwiftUI

struct TooltipModifier<TooltipContent: View>: ViewModifier {
// MARK: - Uninitialised properties

var enabled: Bool
var config: TooltipConfig
var content: TooltipContent


// MARK: - Initialisers

init(config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) {
init(enabled: Bool, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) {
self.enabled = enabled
self.config = config
self.content = content()
}
Expand Down Expand Up @@ -179,7 +181,6 @@ struct TooltipModifier<TooltipContent: View>: ViewModifier {
.overlay(self.arrowView)
}
.offset(x: self.offsetHorizontal(g), y: self.offsetVertical(g))
.animation(.easeInOut)
.onAppear {
self.dispatchAnimation()
}
Expand All @@ -190,7 +191,7 @@ struct TooltipModifier<TooltipContent: View>: ViewModifier {

func body(content: Content) -> some View {
content
.overlay(tooltipBody)
.overlay(enabled ? tooltipBody.transition(config.transition) : nil)
}
}

Expand Down
28 changes: 21 additions & 7 deletions Sources/SwiftUITooltip/TooltipViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,41 @@
import SwiftUI

public extension View {
func tooltip<TooltipContent: View>(@ViewBuilder content: @escaping () -> TooltipContent) -> some View {
func tooltip<TooltipContent: View>(_ enabled: Bool = true, @ViewBuilder content: @escaping () -> TooltipContent) -> some View {
let config: TooltipConfig = DefaultTooltipConfig.shared

return modifier(TooltipModifier(config: config, content: content))
return modifier(TooltipModifier(enabled: enabled, config: config, content: content))
}

func tooltip<TooltipContent: View>(config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View {
modifier(TooltipModifier(config: config, content: content))
func tooltip<TooltipContent: View>(_ enabled: Bool = true, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View {
modifier(TooltipModifier(enabled: enabled, config: config, content: content))
}

func tooltip<TooltipContent: View>(_ side: TooltipSide, @ViewBuilder content: @escaping () -> TooltipContent) -> some View {
func tooltip<TooltipContent: View>(_ enabled: Bool = true, side: TooltipSide, @ViewBuilder content: @escaping () -> TooltipContent) -> some View {
var config = DefaultTooltipConfig.shared
config.side = side

return modifier(TooltipModifier(config: config, content: content))
return modifier(TooltipModifier(enabled: enabled, config: config, content: content))
}

func tooltip<TooltipContent: View>(_ enabled: Bool = true, side: TooltipSide, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View {
var config = config
config.side = side

return modifier(TooltipModifier(enabled: enabled, config: config, content: content))
}

func tooltip<TooltipContent: View>(_ side: TooltipSide, @ViewBuilder content: @escaping () -> TooltipContent) -> some View {
var config = DefaultTooltipConfig.shared
config.side = side

return modifier(TooltipModifier(enabled: true, config: config, content: content))
}

func tooltip<TooltipContent: View>(_ side: TooltipSide, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View {
var config = config
config.side = side

return modifier(TooltipModifier(config: config, content: content))
return modifier(TooltipModifier(enabled: true, config: config, content: content))
}
}

0 comments on commit 9f617c2

Please sign in to comment.