Skip to content

Instantly share code, notes, and snippets.

@shadone
Created April 16, 2021 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shadone/1fec3ade4af2966297656bcf8d0841ca to your computer and use it in GitHub Desktop.
Save shadone/1fec3ade4af2966297656bcf8d0841ca to your computer and use it in GitHub Desktop.
import SwiftUI
struct Item: Identifiable {
var id: String { "abc" }
var title: String = "abcfoo"
}
extension View {
func popup<Item, Content>(item: Binding<Item?>, @ViewBuilder content: (Item) -> Content) -> some View where Item: Identifiable, Content: View {
return ZStack(alignment: .top, content: {
self
item.wrappedValue.map { unwrapped in
Group {
Color.black.opacity(0.8)
.transition(AnyTransition.opacity.animation(.easeInOut(duration: 0.15)))
// .tappable {
// item.wrappedValue = nil
// }
content(unwrapped)
.animation(.easeInOut(duration: 0.3))
.transition(.move(edge: .top))
}
}
})
}
}
struct SheetView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button("Press to dismiss") {
presentationMode.wrappedValue.dismiss()
}
.font(.title)
.padding()
.background(Color.black)
}
}
struct ContentView: View {
@State private var showingSheet = false
@State var item: Item? = Item()
var body: some View {
Button("Show Sheet") {
showingSheet.toggle()
}
.sheet(isPresented: $showingSheet) {
VStack {
Text("Hello")
}
.popup(item: self.$item, content: { test in
VStack {
Text(test.title)
}
})
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment