Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toggle to present and dismiss tooltip on demand #21

Merged
merged 10 commits into from
Apr 26, 2022
11 changes: 6 additions & 5 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 isEnabled: Binding<Bool>
var config: TooltipConfig
var content: TooltipContent


// MARK: - Initialisers

init(config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) {
init(isEnabled: Binding<Bool>, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) {
self.isEnabled = isEnabled
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(isEnabled.wrappedValue ? tooltipBody.transition(.opacity) : nil)
}
}

Expand All @@ -201,7 +202,7 @@ struct Tooltip_Previews: PreviewProvider {


return VStack {
Text("Say...").tooltip(config: config) {
Text("Say...").tooltip(isEnabled: .constant(true), config: config) {
Text("Something nice!")
}
}.previewDevice(.init(stringLiteral: "iPhone 12 mini"))
Expand Down
16 changes: 8 additions & 8 deletions Sources/SwiftUITooltip/TooltipViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@
import SwiftUI

public extension View {
func tooltip<TooltipContent: View>(@ViewBuilder content: @escaping () -> TooltipContent) -> some View {
func tooltip<TooltipContent: View>(isEnabled: Binding<Bool>, @ViewBuilder content: @escaping () -> TooltipContent) -> some View {
bring-shrubbery marked this conversation as resolved.
Show resolved Hide resolved
let config: TooltipConfig = DefaultTooltipConfig.shared

return modifier(TooltipModifier(config: config, content: content))
return modifier(TooltipModifier(isEnabled: isEnabled, config: config, content: content))
bring-shrubbery marked this conversation as resolved.
Show resolved Hide resolved
}

func tooltip<TooltipContent: View>(config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View {
modifier(TooltipModifier(config: config, content: content))
func tooltip<TooltipContent: View>(isEnabled: Binding<Bool>, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View {
modifier(TooltipModifier(isEnabled: isEnabled, config: config, content: content))
}

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

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

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

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