From 5755c5b0435bd3e57afae0ee39ae91e23b2f7c76 Mon Sep 17 00:00:00 2001 From: Joel Koo Date: Wed, 20 Dec 2023 17:12:25 +0900 Subject: [PATCH] Add hidden property Add Detents annotations --- Sources/BottomSheet/BottomSheet.swift | 5 ++- .../BottomSheet/Detents/DetentDefaults.swift | 1 + .../BottomSheet/Detents/DetentHelpers.swift | 2 + Sources/BottomSheet/Detents/Detents.swift | 37 +++++++++++++++++++ .../Preference Keys/ConfigKey.swift | 2 +- 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Sources/BottomSheet/BottomSheet.swift b/Sources/BottomSheet/BottomSheet.swift index 3bb7cf6..323b964 100644 --- a/Sources/BottomSheet/BottomSheet.swift +++ b/Sources/BottomSheet/BottomSheet.swift @@ -144,7 +144,10 @@ struct SheetPlus: ViewModifier .onPreferenceChange(SheetPlusKey.self) { value in /// Quick hack to prevent the scrollview from resetting the height when keyboard shows up. /// Replace if the root cause has been located. - if value.detents.count == 0 { return } + if value.detents.count == 0 || value.selectedDetent == .hidden { + isPresented = false + return + } sheetConfig = value translation = value.translation diff --git a/Sources/BottomSheet/Detents/DetentDefaults.swift b/Sources/BottomSheet/Detents/DetentDefaults.swift index da8a3aa..f6c2fb1 100644 --- a/Sources/BottomSheet/Detents/DetentDefaults.swift +++ b/Sources/BottomSheet/Detents/DetentDefaults.swift @@ -11,4 +11,5 @@ internal struct PresentationDetentDefaults { static let small: CGFloat = UIScreen.main.bounds.height * 0.2 static let medium: CGFloat = UIScreen.main.bounds.height * 0.5 static let large: CGFloat = UIScreen.main.bounds.height * 0.9 + static let hidden: CGFloat = 0 } diff --git a/Sources/BottomSheet/Detents/DetentHelpers.swift b/Sources/BottomSheet/Detents/DetentHelpers.swift index 2aa2689..94f55ce 100644 --- a/Sources/BottomSheet/Detents/DetentHelpers.swift +++ b/Sources/BottomSheet/Detents/DetentHelpers.swift @@ -20,6 +20,8 @@ internal func detentLimits(detents: Set) -> (min: CGFloat, m return PresentationDetentDefaults.medium case .large: return PresentationDetentDefaults.large + case .hidden: + return PresentationDetentDefaults.hidden case .fraction(let fraction): return UIScreen.main.bounds.height * fraction case .height(let height): diff --git a/Sources/BottomSheet/Detents/Detents.swift b/Sources/BottomSheet/Detents/Detents.swift index 2e2aa16..b6b7a09 100644 --- a/Sources/BottomSheet/Detents/Detents.swift +++ b/Sources/BottomSheet/Detents/Detents.swift @@ -7,11 +7,46 @@ import SwiftUI +/** + An enumeration to represent various form of PresentationDetent + + - `small`: A small sized bottom sheet. `.fraction(0.2)` + - `medium`: A medium sized bottom sheet, `.fraction(0.5)` + - `large`: A large sized bottom sheet. `.fraction(0.9)` + - `hidden`: Hide bottom sheet. + - `fraction`: Relative to screen height. + - `height`: A constant height. + */ + public enum PresentationDetent: Hashable { + /** + .fraction(0.2) + */ case small + + /** + .fraction(0.5) + */ case medium + + /** + .fraction(0.9) + */ case large + + /** + Hide bottom sheet + */ + case hidden + + /** + CGFloat 0 to 1 + */ case fraction(CGFloat) + + /** + A constant height. + */ case height(CGFloat) public var size: CGFloat { @@ -22,6 +57,8 @@ public enum PresentationDetent: Hashable { return PresentationDetentDefaults.medium case .large: return PresentationDetentDefaults.large + case .hidden: + return PresentationDetentDefaults.hidden case .fraction(let fraction): return min( UIScreen.main.bounds.height * fraction, diff --git a/Sources/BottomSheet/Preference Keys/ConfigKey.swift b/Sources/BottomSheet/Preference Keys/ConfigKey.swift index 6837078..c9d2b26 100644 --- a/Sources/BottomSheet/Preference Keys/ConfigKey.swift +++ b/Sources/BottomSheet/Preference Keys/ConfigKey.swift @@ -19,7 +19,7 @@ struct SheetPlusConfig: Equatable { } struct SheetPlusKey: PreferenceKey { - static var defaultValue: SheetPlusConfig = SheetPlusConfig(detents: [], selectedDetent: .constant(.height(.zero)), translation: 0) + static var defaultValue: SheetPlusConfig = SheetPlusConfig(detents: [], selectedDetent: .constant(.hidden), translation: 0) static func reduce(value: inout SheetPlusConfig, nextValue: () -> SheetPlusConfig) { /// This prevents the translation changes to be called whenever the keyboard is triggered.