Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Last active December 2, 2022 03:34
Show Gist options
  • Save chriseidhof/b7e2ca227218486e479f5ae07fdc1ecc to your computer and use it in GitHub Desktop.
Save chriseidhof/b7e2ca227218486e479f5ae07fdc1ecc to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// Shared
//
// Created by Chris Eidhof on 05.05.22.
//
import SwiftUI
struct ContentView: View {
@State var sheet = false
var body: some View {
RoundedRectangle(cornerRadius: 5)
.fill(Color.purple)
.onTapGesture {
sheet.toggle()
}
.padding(20)
.frame(maxHeight: .infinity)
.bottomSheet(isActive: $sheet) {
VStack {
Text(String(repeating: "Hello world ", count: 10))
}
}
}
}
extension View {
func bottomSheet<Content: View>(isActive: Binding<Bool>, @ViewBuilder content: @escaping () -> Content) -> some View {
modifier(BottomSheet(isActive: isActive, sheetContent: content))
}
}
struct BottomSheet<C: View>: ViewModifier {
var isActive: Binding<Bool>
@ViewBuilder var sheetContent: () -> C
func body(content: Content) -> some View {
GeometryReader { proxy in
content
.padding(proxy.safeAreaInsets)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.safeAreaInset(edge: .bottom) {
if isActive.wrappedValue {
VStack(spacing: 0) {
sheetContent()
.background(.thinMaterial)
}
.transition(.move(edge: .bottom))
.padding(.bottom, proxy.safeAreaInsets.bottom)
}
}
.edgesIgnoringSafeArea(.all)
}
.animation(.default.speed(0.1), value: isActive.wrappedValue)
}
}
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