@@ -37,16 +37,16 @@ import SwiftUI
37
37
/// </Section>
38
38
/// ```
39
39
///
40
- /// On macOS, ``ListStyle/sidebar`` can have collapsible sections. Use the ``collapsible `` attribute to make a section collapsible.
40
+ /// On macOS, ``ListStyle/sidebar`` can have collapsible sections. Use the ``isExpanded `` attribute to make a section collapsible.
41
41
///
42
42
/// ```html
43
- /// <Section collapsible >
43
+ /// <Section isExpanded={@is_expanded} phx-change="expansion_changed" >
44
44
/// ...
45
45
/// </Section>
46
46
/// ```
47
47
///
48
48
/// ## Attributes
49
- /// * ``collapsible ``
49
+ /// * ``isExpanded ``
50
50
///
51
51
/// ## Children
52
52
/// * `content` - The main body of the section.
@@ -55,34 +55,57 @@ import SwiftUI
55
55
@_documentation ( visibility: public)
56
56
@LiveElement
57
57
struct Section < Root: RootRegistry > : View {
58
- /// Enables this section to be collapsed in sidebar lists on macOS .
58
+ /// Enables this section to be collapsed in sidebar lists.
59
59
@_documentation ( visibility: public)
60
- private var collapsible : Bool = false
60
+ @ChangeTracked ( attribute: . init( name: " isExpanded " ) )
61
+ private var isExpanded : Bool ? = nil
61
62
62
- public var body : some View {
63
- SwiftUI . Section {
64
- let elements = $liveElement. childNodes ( in: " content " , default: true )
65
- . map { ( node) -> ForEachElement in
66
- if let element = node. asElement ( ) ,
67
- let id = element. attributeValue ( for: . init( name: " id " ) )
68
- {
69
- return . keyed( node, id: id)
70
- } else {
71
- return . unkeyed( node)
72
- }
63
+ var content : some View {
64
+ let elements = $liveElement. childNodes ( in: " content " , default: true )
65
+ . map { ( node) -> ForEachElement in
66
+ if let element = node. asElement ( ) ,
67
+ let id = element. attributeValue ( for: . init( name: " id " ) )
68
+ {
69
+ return . keyed( node, id: id)
70
+ } else {
71
+ return . unkeyed( node)
73
72
}
74
- ForEach ( elements) { childNode in
75
- ViewTreeBuilder < Root > . NodeView ( node: childNode. node, context: $liveElement. context. storage)
76
- . trackListItemScrollOffset ( id: childNode. id)
77
73
}
78
- } header: {
79
- $liveElement. children ( in: " header " )
80
- } footer: {
81
- $liveElement. children ( in: " footer " )
74
+ return ForEach ( elements) { childNode in
75
+ ViewTreeBuilder < Root > . NodeView ( node: childNode. node, context: $liveElement. context. storage)
76
+ . trackListItemScrollOffset ( id: childNode. id)
77
+ }
78
+ }
79
+
80
+ var header : some View {
81
+ $liveElement. children ( in: " header " )
82
+ }
83
+
84
+ var footer : some View {
85
+ $liveElement. children ( in: " footer " )
86
+ }
87
+
88
+ public var body : some View {
89
+ if isExpanded != nil ,
90
+ #available( iOS 17 . 0 , macOS 14 . 0 , tvOS 17 . 0 , watchOS 10 . 0 , visionOS 1 . 0 , * )
91
+ {
92
+ SwiftUI . Section ( isExpanded: Binding {
93
+ isExpanded ?? false
94
+ } set: {
95
+ isExpanded = $0
96
+ } ) {
97
+ content
98
+ } header: {
99
+ header
100
+ }
101
+ } else {
102
+ SwiftUI . Section {
103
+ content
104
+ } header: {
105
+ header
106
+ } footer: {
107
+ footer
108
+ }
82
109
}
83
- #if os(macOS)
84
- . collapsible( collapsible)
85
- #endif
86
-
87
110
}
88
111
}
0 commit comments