Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassComponents/Containers/GlassAccordion.swift8// Module : SPGlassComponents9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import SPGlassCore43import SPGlassTokens44import SPGlassThemes45import SPGlassEffects4647/// How many sections of a ``GlassAccordion`` can be open at once.48public enum GlassAccordionMode: String, CaseIterable, Sendable {49 /// Any number of sections.50 case multiple51 /// At most one section: opening one closes the others.52 case single53}5455/// Surface of a ``GlassAccordion``.56public enum GlassAccordionStyle: String, CaseIterable, Sendable {57 /// Rows separated by hairlines, without a surface: for an accordion placed in a card or a form.58 case plain59 /// The rows sit together on one opaque themed surface: for an accordion that stands alone on a page.60 case grouped61}6263/// The pure rules deciding which sections of an accordion are expanded.64public enum GlassAccordionReducer {65 /// The expanded set after section `id` is opened or closed.66 public static func setting<ID: Hashable>(_ id: ID, expanded isExpanded: Bool, in current: Set<ID>, mode: GlassAccordionMode) -> Set<ID> {67 guard isExpanded else {68 var next = current69 next.remove(id)70 return next71 }72 switch mode {73 case .multiple: return current.union([id])74 case .single: return [id]75 }76 }7778 /// The expanded set after section `id` is toggled.79 public static func toggling<ID: Hashable>(_ id: ID, in current: Set<ID>, mode: GlassAccordionMode) -> Set<ID> {80 setting(id, expanded: !current.contains(id), in: current, mode: mode)81 }8283 /// The set actually displayed: identifiers that no longer exist are ignored and, in single mode, only the84 /// first expanded section in display order stays open.85 public static func displayed<ID: Hashable>(_ current: Set<ID>, order: [ID], mode: GlassAccordionMode) -> Set<ID> {86 let present = order.filter(current.contains)87 switch mode {88 case .multiple: return Set(present)89 case .single: return Set(present.prefix(1))90 }91 }92}9394/// Disclosure sections with controlled expansion.95///96/// ```swift97/// GlassAccordion(questions, expanded: $expanded, mode: .single) { question in98/// Text(question.title)99/// } content: { question in100/// Text(question.answer)101/// }102/// ```103///104/// Each section is a system `DisclosureGroup`, which provides the chevron, the keyboard behaviour and the105/// expanded or collapsed state read by VoiceOver. The set of expanded identifiers belongs to the host.106public struct GlassAccordion<Data: RandomAccessCollection, Label: View, Content: View>: View where Data.Element: Identifiable {107 @GlassThemed private var theme108 @GlassAccessibility private var accessibility109 @Binding private var expanded: Set<Data.Element.ID>110 private let data: Data111 private let mode: GlassAccordionMode112 private let style: GlassAccordionStyle113 private let label: (Data.Element) -> Label114 private let content: (Data.Element) -> Content115116 /// Creates an accordion.117 ///118 /// - Parameters:119 /// - data: the sections, with stable identifiers.120 /// - expanded: identifiers of the open sections.121 /// - mode: `.single` keeps at most one section open.122 /// - style: `.grouped` draws the rows on one opaque surface.123 /// - label: header of a section.124 /// - content: body of a section, built only while it is open.125 public init(126 _ data: Data,127 expanded: Binding<Set<Data.Element.ID>>,128 mode: GlassAccordionMode = .multiple,129 style: GlassAccordionStyle = .plain,130 @ViewBuilder label: @escaping (Data.Element) -> Label,131 @ViewBuilder content: @escaping (Data.Element) -> Content132 ) {133 self.data = data134 self._expanded = expanded135 self.mode = mode136 self.style = style137 self.label = label138 self.content = content139 }140141 public var body: some View {142 switch style {143 case .plain:144 rows145 case .grouped:146 rows147 .padding(.horizontal, theme.spacing.md)148 .padding(.vertical, theme.spacing.xxs)149 .glassOpaqueSurface(in: theme.shape(\.large))150 }151 }152153 private var rows: some View {154 let order = data.map(\.id)155 let displayed = GlassAccordionReducer.displayed(expanded, order: order, mode: mode)156 return VStack(alignment: .leading, spacing: 0) {157 ForEach(data) { item in158 DisclosureGroup(isExpanded: binding(for: item.id, displayed: displayed)) {159 content(item)160 .font(theme.font(.callout))161 .foregroundStyle(theme.color(.textSecondary))162 .frame(maxWidth: .infinity, alignment: .leading)163 .padding(.top, theme.spacing.xxs)164 .padding(.bottom, theme.spacing.xs)165 } label: {166 label(item)167 .font(theme.font(.headline))168 .foregroundStyle(theme.color(.textPrimary))169 .frame(minHeight: 28)170 }171 .padding(.vertical, theme.spacing.sm)172 if item.id != order.last {173 Rectangle()174 .fill(theme.color(.separator).opacity(0.7))175 .frame(height: 1)176 .accessibilityHidden(true)177 }178 }179 }180 .tint(theme.color(.textSecondary))181 }182183 private func binding(for id: Data.Element.ID, displayed: Set<Data.Element.ID>) -> Binding<Bool> {184 Binding {185 displayed.contains(id)186 } set: { isExpanded in187 let next = GlassAccordionReducer.setting(id, expanded: isExpanded, in: displayed, mode: mode)188 // Reduce Motion: the section opens at once, without the sliding reveal.189 var transaction = Transaction(animation: accessibility.reduceMotion ? nil : theme.animation(.standard, reduceMotion: false))190 transaction.disablesAnimations = accessibility.reduceMotion191 withTransaction(transaction) { expanded = next }192 }193 }194}195