SPB Git

Repository Public MIT

doc-glass-accordion.md 2.9 KB

# GlassAccordion

Identifier glass-accordion
Module SPGlassComponents
Kind native-wrapper — system DisclosureGroup sections; no surface is drawn
Platforms iOS 26, iPadOS 26, macOS 26
Example Examples/Gallery/Containers/GlassAccordionExample.swift

# Minimal usage

swift
@State private var expanded: Set<Question.ID> = []

GlassAccordion(questions, expanded: $expanded) { question in
    Text(question.title)
} content: { question in
    Text(question.answer)
}

# Variants

Mode Behaviour
.multiple (default) Any number of sections can be open.
.single Opening a section closes the others. If the bound set already holds several identifiers, only the first in display order is shown open.

# Parameters

Parameter Type Default Meaning
data RandomAccessCollection of Identifiable — Sections, with stable identifiers.
expanded Binding<Set<ID>> — Identifiers of the open sections.
mode GlassAccordionMode .multiple See Variants.
label @ViewBuilder (Element) -> View — Header of a section.
content @ViewBuilder (Element) -> View — Body of a section, built only while it is open.

The transition rules are in GlassAccordionReducer (setting, toggling, displayed), pure functions the host can reuse to open a section from code.

# Style

Parameter Type Default Meaning
style GlassAccordionStyle .plain .plain: rows separated by hairlines, for an accordion inside a card or a form. .grouped: the rows sit on one opaque themed surface, for an accordion that stands alone.

# States

Expanded and collapsed, per section, controlled by the host through expanded. Identifiers that are not in data are ignored.

# Advanced example

swift
GlassCard {
    GlassAccordion(sections, expanded: $expanded, mode: .single) { section in
        Label(section.title, systemImage: section.symbol)
    } content: { section in
        ForEach(section.rows) { row in LabeledContent(row.name, value: row.value) }
    }
}
.onOpenURL { url in
    if let id = Section.id(from: url) {
        expanded = GlassAccordionReducer.setting(id, expanded: true, in: expanded, mode: .single)
    }
}

# Accessibility

  • DisclosureGroup exposes each header as a control with its expanded or collapsed state, and handles Space, Return and the arrow keys on macOS.
  • With Reduce Motion (real or simulated) sections open and close at once, without the sliding reveal; otherwise the theme's standard animation is used.
  • Dividers are hidden from accessibility.

# Limits

  • The disclosure chevron and its position are the system's.
  • The accordion draws no background; place it in a GlassCard or a Form/List for a surface.
  • All section headers are built eagerly (a VStack); for hundreds of sections use a List of DisclosureGroups instead.
Files main
Menu

Theme