Advertisement
dartmeadow

Custom Popover NavLink

Dec 18th, 2022
1,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.66 KB | Software | 0 0
  1. //
  2. //  PopoverLink.swift
  3. //
  4. //  Created by Manuel Weiel on 09.07.20.
  5. //
  6. import SwiftUI
  7.  
  8. struct PopoverLink<Label, Destination> : View where Label : View, Destination : View {
  9.     @Environment(\.horizontalSizeClass) var horizontalSizeClass
  10.    
  11.     private let destination: Destination
  12.     private let label: Label
  13.     private var isActive: Binding<Bool>?
  14.     @State private var internalIsActive = false
  15.    
  16.     /// Creates an instance that presents `destination`.
  17.     public init(destination: Destination, @ViewBuilder label: () -> Label) {
  18.         self.destination = destination
  19.         self.label = label()
  20.     }
  21.    
  22.     /// Creates an instance that presents `destination` when active.
  23.     public init(destination: Destination, isActive: Binding<Bool>, @ViewBuilder label: () -> Label) {
  24.         self.destination = destination
  25.         self.label = label()
  26.         self.isActive = isActive
  27.     }
  28.    
  29.     private func popoverButton() -> some View {
  30.         Button {
  31.             (isActive ?? _internalIsActive.projectedValue).wrappedValue = true
  32.         } label: {
  33.             label
  34.         }
  35.     }
  36.    
  37.  
  38. //I assume this would be the code to change from sheet to shared struct view with main menu below:
  39.  
  40.     /// The content and behavior of the view.
  41.     public var body: some View {
  42.         if horizontalSizeClass == .compact {
  43.             popoverButton().sheet(isPresented: (isActive ?? _internalIsActive.projectedValue)) {
  44.                 destination
  45.             }
  46.         } else {
  47.             popoverButton().popover(isPresented: (isActive ?? _internalIsActive.projectedValue)) {
  48.                 destination
  49.             }
  50.         }
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement