DisclosureGroup
The DisclosureGroup component allows you to organize related content into collapsible sections. This is useful for grouping items in a list, especially when dealing with hierarchical or optional content.
This example demonstrates how to create a top-level disclosure group that can be toggled open or closed, and how to nest additional DisclosureGroup components for sub-sections.
Overview
You will learn how to:
- Use
DisclosureGroupto create expandable sections - Bind the expanded state to local component state
- Nest disclosure groups to represent hierarchical content
- Combine with other controls such as
Toggle,Text, andButton
Example Code
1. Import Dependencies
2. Define Component State
Manage the expanded state and toggle values using useState:
3. Layout UI with Disclosure Groups
The main layout includes a List within a NavigationStack. A Button is provided to toggle the top-level group manually. The DisclosureGroup itself contains multiple child views and a nested DisclosureGroup:
4. Present the View and Exit
Key Concepts
- DisclosureGroup: An expandable view that reveals or hides its content.
- isExpanded: Binds the expanded/collapsed state to a Boolean value.
- onChanged: Callback that triggers when the user expands or collapses the group.
- Nested Groups: You can include one
DisclosureGroupinside another to create a hierarchy. - Integration: Works seamlessly with controls such as
Toggle,Text,Button, etc.
Use Cases
- Grouping settings into categories
- Creating collapsible FAQs or toolboxes
- Displaying nested data like folders, sections, or filters
This pattern provides a clean and user-friendly way to organize complex or optional content within a scrollable list.
