Last active
April 16, 2021 12:29
-
-
Save shadone/b54b86e4bd71252b7116c78ea1a2306b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// test | |
// | |
// Created by Philip Young on 16/04/21. | |
// | |
import SwiftUI | |
struct Item: Identifiable { | |
var id: String { "abc" } | |
var title: String = "Item title" | |
} | |
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))) | |
.onTapGesture { | |
item.wrappedValue = nil | |
} | |
content(unwrapped) | |
.animation(.easeInOut(duration: 0.3)) | |
.transition(.move(edge: .top)) | |
} | |
} | |
}) | |
} | |
} | |
struct MainBackground: View { | |
var body: some View { | |
Color.gray | |
} | |
} | |
struct SheetView: View { | |
@Environment(\.presentationMode) var presentationMode | |
@State var item: Item? = nil | |
var body: some View { | |
ZStack { | |
VStack { | |
Button("Press to add item") { | |
self.item = Item() | |
} | |
Button("Press to dismiss sheet") { | |
presentationMode.wrappedValue.dismiss() | |
} | |
} | |
} | |
.padding(64) | |
.popup(item: self.$item, content: { test in | |
VStack { | |
PopupView(item: test, onDismiss: { | |
self.item = nil | |
}) | |
Spacer() | |
}.cornerRadius(12) | |
}) | |
} | |
} | |
enum Colors: CaseIterable { | |
case one, two, three, four, five | |
var color: Color { | |
switch self { | |
case .one: | |
return Color.red | |
case .two: | |
return Color.blue | |
case .three: | |
return Color.green | |
case .four: | |
return Color.yellow | |
default: | |
return Color.black | |
} | |
} | |
} | |
struct PopupView: View { | |
var item: Item | |
var onDismiss: () -> Void | |
var body: some View { | |
VStack { | |
Text(item.title) | |
HStack { | |
ForEach(Colors.allCases, id: \.self) { color in | |
ZStack { | |
color.color | |
.overlay(Color.white.opacity(0.15)) | |
//.mask(Circle()) | |
.frame(width: 12, height: 12) | |
MainBackground() | |
//.mask(Circle()) | |
.frame(width:12 / 2, height:12 / 2) | |
} | |
.contentShape(Rectangle()) | |
} | |
} | |
Button("Dismiss popup") { | |
self.onDismiss() | |
} | |
} | |
.padding(24) | |
.background(Color.green) | |
.cornerRadius(12) | |
} | |
} | |
struct ContentView: View { | |
@State private var showingSheet = false | |
var body: some View { | |
Button("Show Sheet") { | |
showingSheet.toggle() | |
} | |
.padding(64) | |
.sheet(isPresented: $showingSheet) { | |
SheetView() | |
} | |
} | |
} | |
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