SPB Git

Repository Public MIT

example-glass-sidebar.txt 2.2 KB 48 lines text

1import SwiftUI2import SPGlassKit34public struct GlassSidebarExample: GlassExample {5    public static let componentID = "glass-sidebar"6    @State private var selection: String? = "inbox"7    @State private var columnVisibility: NavigationSplitViewVisibility = .automatic89    public init() {}1011    private let sections: [GlassSidebarSection<String>] = [12        GlassSidebarSection(id: "mailboxes", title: "Mailboxes", items: [13            GlassSidebarItem(id: "inbox", title: "Inbox", systemImage: "tray", badge: 12, keywords: ["unread"]),14            GlassSidebarItem(id: "drafts", title: "Drafts", systemImage: "doc", badge: 2),15            GlassSidebarItem(id: "sent", title: "Sent", systemImage: "paperplane"),16            GlassSidebarItem(id: "archive", title: "Archive", systemImage: "archivebox"),17        ]),18        GlassSidebarSection(id: "labels", title: "Labels", items: [19            GlassSidebarItem(id: "travel", title: "Voyages et séjours", systemImage: "airplane", keywords: ["travel", "trips"]),20            GlassSidebarItem(id: "receipts", title: "Reçus", systemImage: "receipt", keywords: ["receipts"]),21            GlassSidebarItem(id: "long", title: "A label with a deliberately long name to check wrapping", systemImage: "tag"),22        ]),23    ]2425    public var body: some View {26        GlassSidebar("Mail", sections: sections, selection: $selection, columnVisibility: $columnVisibility, searchPrompt: "Search mailboxes") { id in27            if let id {28                VStack(spacing: 12) {29                    Text(verbatim: title(of: id)).font(.title2)30                    Button(columnVisibility == .detailOnly ? "Show sidebar" : "Hide sidebar") {31                        columnVisibility = columnVisibility == .detailOnly ? .all : .detailOnly32                    }33                }34                .padding()35                .navigationTitle(Text(verbatim: title(of: id)))36            } else {37                ContentUnavailableView("Select a mailbox", systemImage: "sidebar.left")38            }39        }40    }4142    private func title(of id: String) -> String {43        sections.flatMap(\.items).first { $0.id == id }?.title ?? id44    }45}4647#Preview("GlassSidebar") { GlassSidebarExample() }48
Files main
Menu

Theme