// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassComponents/Containers/GlassAccordion.swift // Module : SPGlassComponents // Repository : https://git.spboucher.ai/sp-glasskit // Project Website : https://glass.spboucher.ai // Created : 2026-12-02 // Last Modified : 2026-12-02 // // Author : Simon-Pierre Boucher // Position : Professor of Finance // Institution : Université du Québec en Outaouais (UQO) // Organization : SP Boucher // Website : https://spboucher.ai // Git : https://git.spboucher.ai // // Preferred Contact: contact@spboucher.ai // Development : dev@spboucher.ai // Support : support@spboucher.ai // Security/Admin : admin@spboucher.ai // // Copyright (c) 2026 // Simon-Pierre Boucher // All rights reserved where applicable. This file is governed by the // applicable project license, copyright notices and relevant law. // Existing third-party rights and notices remain unaffected. // // License : MIT // // Do not embed credentials, private keys, access tokens, // certificates or confidential personal information. // // ============================================================================= // END OF SP BOUCHER SOURCE HEADER // ============================================================================= import SwiftUI /// How many sections of a ``GlassAccordion`` can be open at once. public enum GlassAccordionMode: String, CaseIterable, Sendable { /// Any number of sections. case multiple /// At most one section: opening one closes the others. case single } /// Surface of a ``GlassAccordion``. public enum GlassAccordionStyle: String, CaseIterable, Sendable { /// Rows separated by hairlines, without a surface: for an accordion placed in a card or a form. case plain /// The rows sit together on one opaque themed surface: for an accordion that stands alone on a page. case grouped } /// The pure rules deciding which sections of an accordion are expanded. public enum GlassAccordionReducer { /// The expanded set after section `id` is opened or closed. public static func setting(_ id: ID, expanded isExpanded: Bool, in current: Set, mode: GlassAccordionMode) -> Set { guard isExpanded else { var next = current next.remove(id) return next } switch mode { case .multiple: return current.union([id]) case .single: return [id] } } /// The expanded set after section `id` is toggled. public static func toggling(_ id: ID, in current: Set, mode: GlassAccordionMode) -> Set { setting(id, expanded: !current.contains(id), in: current, mode: mode) } /// The set actually displayed: identifiers that no longer exist are ignored and, in single mode, only the /// first expanded section in display order stays open. public static func displayed(_ current: Set, order: [ID], mode: GlassAccordionMode) -> Set { let present = order.filter(current.contains) switch mode { case .multiple: return Set(present) case .single: return Set(present.prefix(1)) } } } /// Disclosure sections with controlled expansion. /// /// ```swift /// GlassAccordion(questions, expanded: $expanded, mode: .single) { question in /// Text(question.title) /// } content: { question in /// Text(question.answer) /// } /// ``` /// /// Each section is a system `DisclosureGroup`, which provides the chevron, the keyboard behaviour and the /// expanded or collapsed state read by VoiceOver. The set of expanded identifiers belongs to the host. public struct GlassAccordion: View where Data.Element: Identifiable { @GlassThemed private var theme @GlassAccessibility private var accessibility @Binding private var expanded: Set private let data: Data private let mode: GlassAccordionMode private let style: GlassAccordionStyle private let label: (Data.Element) -> Label private let content: (Data.Element) -> Content /// Creates an accordion. /// /// - Parameters: /// - data: the sections, with stable identifiers. /// - expanded: identifiers of the open sections. /// - mode: `.single` keeps at most one section open. /// - style: `.grouped` draws the rows on one opaque surface. /// - label: header of a section. /// - content: body of a section, built only while it is open. public init( _ data: Data, expanded: Binding>, mode: GlassAccordionMode = .multiple, style: GlassAccordionStyle = .plain, @ViewBuilder label: @escaping (Data.Element) -> Label, @ViewBuilder content: @escaping (Data.Element) -> Content ) { self.data = data self._expanded = expanded self.mode = mode self.style = style self.label = label self.content = content } public var body: some View { switch style { case .plain: rows case .grouped: rows .padding(.horizontal, theme.spacing.md) .padding(.vertical, theme.spacing.xxs) .glassOpaqueSurface(in: theme.shape(\.large)) } } private var rows: some View { let order = data.map(\.id) let displayed = GlassAccordionReducer.displayed(expanded, order: order, mode: mode) return VStack(alignment: .leading, spacing: 0) { ForEach(data) { item in DisclosureGroup(isExpanded: binding(for: item.id, displayed: displayed)) { content(item) .font(theme.font(.callout)) .foregroundStyle(theme.color(.textSecondary)) .frame(maxWidth: .infinity, alignment: .leading) .padding(.top, theme.spacing.xxs) .padding(.bottom, theme.spacing.xs) } label: { label(item) .font(theme.font(.headline)) .foregroundStyle(theme.color(.textPrimary)) .frame(minHeight: 28) } .padding(.vertical, theme.spacing.sm) if item.id != order.last { Rectangle() .fill(theme.color(.separator).opacity(0.7)) .frame(height: 1) .accessibilityHidden(true) } } } .tint(theme.color(.textSecondary)) } private func binding(for id: Data.Element.ID, displayed: Set) -> Binding { Binding { displayed.contains(id) } set: { isExpanded in let next = GlassAccordionReducer.setting(id, expanded: isExpanded, in: displayed, mode: mode) // Reduce Motion: the section opens at once, without the sliding reveal. var transaction = Transaction(animation: accessibility.reduceMotion ? nil : theme.animation(.standard, reduceMotion: false)) transaction.disablesAnimations = accessibility.reduceMotion withTransaction(transaction) { expanded = next } } } }