Skip to content

Commit 8978255

Browse files
committed
Add ViewAlias documentation
1 parent 3ddfbd7 commit 8978255

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Sources/OpenSwiftUI/View/ViewAlias.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,48 @@ import OpenGraphShims
1111

1212
// MARK: - ViewAlias
1313

14+
/// A protocol for creating view aliases that can be used to reference source views indirectly.
15+
///
16+
/// ViewAlias allows the creation of placeholder views that will be replaced with actual
17+
/// source views at render time. This enables more flexible view composition patterns
18+
/// and reuse of view hierarchies across different parts of an application.
19+
///
20+
/// Conforming types must provide an empty initializer and will inherit the view
21+
/// implementation from the source view when rendered.
1422
protocol ViewAlias: PrimitiveView {
1523
init()
1624
}
1725

1826
// MARK: - View + ViewAlias Extension
1927

2028
extension View {
29+
/// Defines a view alias that will be replaced with the provided source view.
30+
///
31+
/// Use this method to associate a view alias with a source view. When the
32+
/// alias is used elsewhere in the view hierarchy, it will be replaced with
33+
/// the source view provided here.
34+
///
35+
/// - Parameters:
36+
/// - alias: The view alias type to associate with the source view.
37+
/// - source: A closure that returns the source view to be used when rendering the alias.
38+
/// - Returns: A view that establishes the connection between the alias and source view.
2139
func viewAlias<Alias, Source>(
2240
_ alias: Alias.Type,
2341
_ source: () -> Source
2442
) -> some View where Alias: ViewAlias, Source: View {
2543
modifier(StaticSourceWriter<Alias, Source>(source: source()))
2644
}
2745

46+
/// Conditionally defines a view alias based on a predicate.
47+
///
48+
/// Use this method to associate a view alias with a source view only when
49+
/// a specific condition is met based on the ViewInputPredicate.
50+
///
51+
/// - Parameters:
52+
/// - predicate: The predicate type used to determine if the alias should be applied.
53+
/// - alias: The view alias type to associate with the source view.
54+
/// - source: A closure that returns the source view to be used when rendering the alias.
55+
/// - Returns: A view that conditionally establishes the connection between the alias and source view.
2856
func viewAlias<Predicate, Alias, Source>(
2957
if predicate: Predicate.Type,
3058
_ alias: Alias.Type,
@@ -33,6 +61,16 @@ extension View {
3361
modifier(StaticSourceWriter<Alias, Source>(source: source()).requiring(predicate))
3462
}
3563

64+
/// Defines an optional view alias that will be replaced with the provided source view when it exists.
65+
///
66+
/// Use this method to associate a view alias with an optional source view. When the
67+
/// alias is used elsewhere in the view hierarchy, it will be replaced with the source
68+
/// view if it's non-nil, or not rendered if the source is nil.
69+
///
70+
/// - Parameters:
71+
/// - alias: The view alias type to associate with the optional source view.
72+
/// - source: A closure that returns the optional source view to be used when rendering the alias.
73+
/// - Returns: A view that establishes the conditional connection between the alias and source view.
3674
func optionalViewAlias<Alias, Source>(
3775
_ alias: Alias.Type,
3876
_ source: () -> Source?
@@ -64,6 +102,14 @@ private protocol AnySourceFormula {
64102

65103
// MARK: - OptionalViewAlias
66104

105+
/// A property wrapper that provides conditional access to a view alias based on source availability.
106+
///
107+
/// `OptionalViewAlias` checks whether a source view has been defined for the specified
108+
/// view alias type. If a source exists, the `wrappedValue` provides an instance of the
109+
/// alias; otherwise, it returns `nil`.
110+
///
111+
/// This is useful for conditionally including views in a hierarchy when their source views
112+
/// may or may not be defined.
67113
struct OptionalViewAlias<Alias>: DynamicProperty where Alias: ViewAlias {
68114
static func _makeProperty<Value>(in buffer: inout _DynamicPropertyBuffer, container: _GraphValue<Value>, fieldOffset: Int, inputs: inout _GraphInputs) {
69115
if let source = inputs[SourceInput<Alias>.self].top {
@@ -80,8 +126,10 @@ struct OptionalViewAlias<Alias>: DynamicProperty where Alias: ViewAlias {
80126
}
81127
}
82128

129+
/// Indicates whether a source view has been defined for this alias.
83130
var sourceExists: Bool
84131

132+
/// Returns an instance of the alias if a source view exists, otherwise `nil`.
85133
var wrappedValue: Alias? {
86134
sourceExists ? Alias() : nil
87135
}

0 commit comments

Comments
 (0)