Element
public protocol Element
Conforming types represent a rectangular content area in a two-dimensional layout space.
The ultimate purpose of an element is to provide visual content. This can be done in two ways:
By providing a view description (
ViewDescription
).By providing child elements that will be displayed recursively within the local coordinate space.
A custom element might look something like this:
struct MyElement: Element {
var backgroundColor: UIColor = .red
// Returns a single child element.
var content: ElementContent {
return ElementContent(child: Label(text: "😂"))
}
// Providing a view description means that this element will be
// backed by a UIView instance when displayed in a `BlueprintView`.
func backingViewDescription(with context: ViewDescriptionContext) -> ViewDescription? {
return UIView.describe { config in
config.bind(backgroundColor, to: \.backgroundColor)
}
}
}
-
Returns the content of this element.
Elements generally fall into two types:
- Leaf elements, or elements that have no children. These elements commonly have an intrinsic size, or some
content that can be measured. Leaf elements typically instantiate their content with
ElementContent(measurable:)
or similar. - Container elements: these element have one or more children, which are arranged by a layout implementation.
Container elements typically use methods like
ElementContent(layout:configure:)
to instantiate their content.
Declaration
Swift
var content: ElementContent { get }
- Leaf elements, or elements that have no children. These elements commonly have an intrinsic size, or some
content that can be measured. Leaf elements typically instantiate their content with
-
Returns an (optional) description of the view that should back this element.
In Blueprint, elements that are displayed using a live
UIView
instance are referred to as “view-backed”. Elements become view-backed by returning aViewDescription
value from this method.Declaration
Swift
func backingViewDescription(with context: ViewDescriptionContext) -> ViewDescription?
Parameters
context
The context this element is rendered in.
Return Value
An optional
ViewDescription
. -
adaptedEnvironment(key:
Extension methodvalue: ) Wraps this element in an
AdaptedEnvironment
with the given environment key and value.Declaration
Swift
public func adaptedEnvironment<Key>(key: Key.Type, value: Key.Value) -> Element where Key : EnvironmentKey
-
adaptedEnvironment(keyPath:
Extension methodvalue: ) Wraps this element in an
AdaptedEnvironment
with the given keypath and value.Declaration
Swift
public func adaptedEnvironment<Value>(keyPath: WritableKeyPath<Environment, Value>, value: Value) -> Element
-
adaptedEnvironment(by:
Extension method) Wraps this element in an
AdaptedEnvironment
with the given configuration block.Declaration
Swift
public func adaptedEnvironment(by environmentAdapter: @escaping (inout Environment) -> Void) -> Element
-
aligned(vertically:
Extension methodhorizontally: ) Wraps the element in an
Aligned
element with the provided parameters.Declaration
Swift
public func aligned( vertically: Aligned.VerticalAlignment = .center, horizontally: Aligned.HorizontalAlignment = .center ) -> Aligned
Parameters
vertically
The vertical alignment. Defaults to
.centered
.horizontally
The horizontal alignment. Defaults to
.centered
. -
centered()
Extension method
-
if(_:
Extension methodthen: ) Returns a new element from the provided
modify
closure, if the provided boolean is true. Otherwise, the original element is returned.myElement.if(someBoolean) { element in element.centered() }
Declaration
Swift
public func `if`( _ isTrue: Bool, then: (Self) -> Element ) -> Element
-
if(_:
Extension methodthen: else: ) Returns a new element from the provided
then
closure if the provided boolean is true. If the provided boolean is false, theelse
closure is usedmyElement.if( someBoolean, then: { element in element.aligned(horizontally: .trailing, vertically: .fill) }, else: { element in element.aligned(horizontally: .leading, vertically: .fill) } )
Declaration
Swift
public func `if`( _ isTrue: Bool, then: (Self) -> Element, else: (Self) -> Element ) -> Element
-
if(let:
Extension methodthen: ) Returns a new element from the provided
modify
closure if the provided value is non-nil. Otherwise, the original element is returned.myElement.if(let: someValue) { value, element in element.inset(uniform: someValue.padding) }
Declaration
Swift
public func `if`<Value>( `let` value: Value?, then: (Value, Self) -> Element ) -> Element
-
if(let:
Extension methodthen: else: ) Returns a new element from the provided
then
closure if the provided boolean is true. If the provided value is nil, theelse
closure is usedmyElement.if( let: someValue, then: { value, element in element.inset(uniform: value.padding) }, else: { element in element.inset(uniform: 10) } )
Declaration
Swift
public func `if`<Value>( `let` value: Value?, then: (Value, Self) -> Element, else: (Self) -> Element ) -> Element
-
map(_:
Extension method) Creates and returns a new element by passing the element to the provided
map
function.myElement.map { element in switch myState { case .small: element.inset(uniform: 5) case .medium: element.inset(uniform: 10) case .large: element.inset(uniform: 15) } }
Declaration
Swift
public func map(_ map: (Self) -> Element) -> Element
-
modify(_:
Extension method) Creates and returns a new element by passing the element to the provided
modify
function, which can edit it.myElement.modify { element in switch myState { case .small: element.inset = 5 case .medium: element.inset = 10 case .large: element.inset = 15 } }
Declaration
Swift
public func modify(_ modify: (inout Self) -> Void) -> Element
-
constrainedTo(aspectRatio:
Extension methodcontentMode: ) Constrains the element to the provided aspect ratio.
Declaration
Swift
public func constrainedTo( aspectRatio: AspectRatio, contentMode: ConstrainedAspectRatio.ContentMode = .fitContent ) -> ConstrainedAspectRatio
Parameters
aspectRatio
The aspect ratio that the content size should match.
contentMode
How the content should size itself relative to its parent.
-
constrainedTo(width:
Extension methodheight: ) Constrains the measured size of the element to the provided width and height.
Declaration
Swift
public func constrainedTo( width: ConstrainedSize.Constraint = .unconstrained, height: ConstrainedSize.Constraint = .unconstrained ) -> ConstrainedSize
-
constrainedTo(width:
Extension methodheight: ) Constrains the measured size of the element to the provided width and height.
Declaration
Swift
public func constrainedTo( width: CGFloat, height: CGFloat ) -> ConstrainedSize
-
constrainedTo(size:
Extension method) Constrains the measured size of the element to the provided size.
Declaration
Swift
public func constrainedTo( size: CGSize ) -> ConstrainedSize
-
constrained(to:
Extension method) Constrains the measured size of the element to the provided
SizeConstraint
.Declaration
Swift
public func constrained(to sizeConstraint: SizeConstraint) -> ConstrainedSize
-
decorate(layering:
Extension methodposition: with: ) Places a decoration element behind or in front of the given
wrapped
element, and positions it according to theposition
parameter.See the
Decorate
element for more. -
flowChild(priority:
Extension methodkey: ) Wraps the element in a
Flow.Child
to allow customizing the item in the flow layout.
-
gridRowChild(key:
Extension methodwidth: ) Wraps an element with a
GridRowChild
in order to provide meta information that aGridRow
can aply to its layout.Declaration
Parameters
key
A unique identifier for the child.
width
The sizing for the element.
Return Value
GridRowChild
-
hidden(_:
Extension method) Conditionally hide the wrapped element.
Hidden elements still participate in layout. Hiding sets the
UIView.isHidden
property of the nearest backing view.Note
When an element is hidden, any elements within the wrapped element will be hidden.Declaration
Swift
public func hidden(_ hidden: Bool = true) -> Hidden
-
inset(top:
Extension methodbottom: left: right: ) Insets the element by the given amount on each side.
Declaration
Swift
public func inset( top: CGFloat = 0.0, bottom: CGFloat = 0.0, left: CGFloat = 0.0, right: CGFloat = 0.0 ) -> Inset
-
inset(by:
Extension method) Insets the element by the given amount on each side.
Declaration
Swift
public func inset(by edgeInsets: UIEdgeInsets) -> Inset
-
inset(uniform:
Extension method) Insets the element by the given amount on each side.
Declaration
Swift
public func inset(uniform: CGFloat) -> Inset
-
inset(horizontal:
Extension methodvertical: ) Insets the element by the given amount on each side.
Declaration
Swift
public func inset( horizontal: CGFloat = 0.0, vertical: CGFloat = 0.0 ) -> Inset
-
keyed(_:
Extension method) Keyed
allows providing aHashable
value which is used during view updates to uniquely identify content during the diff process between the old and new element structure.This is useful if you have two elements of the same type at the same depth in the element hierarchy, and you’d like to differentiate between them, eg for appearance transition purposes.
Example
Keying the image returned, so that a transition occurs when changing between a placeholder image and an available photo.
func imageElement() -> Element { if let photo = self.photo { return Image(image: photo) .transition(.fade) .keyed("photo") } else { return Image(image: self.placeholder) .transition(.fade) .keyed("placeholder") } }
Declaration
Swift
public func keyed(_ key: AnyHashable) -> Keyed
-
onAppear(_:
Extension method) Adds a hook that will be called when this element appears.
Callbacks run in depth-first traversal order, with parents before children.
Declaration
Swift
public func onAppear(_ callback: @escaping LifecycleCallback) -> LifecycleObserver
-
onDisappear(_:
Extension method) Adds a hook that will be called when this element disappears.
Callbacks run in depth-first traversal order, with parents before children. There is no guaranteed order between disappearance callbacks and backing views being removed from their superviews.
Declaration
Swift
public func onDisappear(_ callback: @escaping LifecycleCallback) -> LifecycleObserver
-
opacity(_:
Extension method) -
overlayChild(key:
Extension method) Declaration
Swift
public func overlayChild(key: AnyHashable? = nil) -> Overlay.Child
-
stackLayoutChild(priority:
Extension methodalignmentGuide: key: ) Wraps an element with a
StackLayout.Child
in order to customizeStackLayout.Traits
and the key.Declaration
Swift
public func stackLayoutChild( priority: StackLayout.Child.Priority = .flexible, alignmentGuide: ((ElementDimensions) -> CGFloat)? = nil, key: AnyHashable? = nil ) -> StackLayout.Child
Parameters
priority
Controls the amount of extra space distributed to this child during underflow and overflow
alignmentGuide
Allows for custom alignment of a child along the cross axis.
key
A key used to disambiguate children between subsequent updates of the view hierarchy.
Return Value
A wrapper containing this element with additional layout information for the
StackElement
. -
stackLayoutChild(priority:
Extension method) Wraps an element with a
StackLayout.Child
in order to customize theStackLayout.Child.Priority
.Declaration
Swift
public func stackLayoutChild( priority: StackLayout.Child.Priority ) -> StackLayout.Child
Parameters
priority
Controls the amount of extra space distributed to this child during underflow and overflow
Return Value
A wrapper containing this element with additional layout information for the
StackElement
. -
tintAdjustmentMode(_:
Extension method) Conditionally modifies the tint adjustment mode of its wrapped element.
Note
When a tint adjustment mode is applied, any elements within the wrapped element will adopt the parent’s tint adjustment mode.Declaration
Swift
public func tintAdjustmentMode(_ tintAdjustmentMode: UIView.TintAdjustmentMode) -> TintAdjustmentMode
-
transformed(_:
Extension method) Wraps the element in an
Transformed
element with the provided 3D transform.Declaration
Swift
public func transformed(_ transform: CATransform3D) -> Transformed
Parameters
transform
The 3D transform to be applied.
-
transformed(_:
Extension method) Wraps the element in an
Transformed
element with the provided 2D transform.Declaration
Swift
public func transformed(_ transform: CGAffineTransform) -> Transformed
Parameters
transform
The 2D transform to be applied.
-
translated(translateX:
Extension methodtranslateY: translateZ: ) Wraps the element in an
Transformed
element that translates the receiver in 3D space.Declaration
Swift
public func translated( translateX: CGFloat = 0, translateY: CGFloat = 0, translateZ: CGFloat = 0 ) -> Transformed
Parameters
transformX
The X component of the translation.
transformY
The Y component of the translation.
transformZ
The Z component of the translation.
-
rotated(by:
Extension method) Wraps the element in an
Transformed
element that rotates the receiver in 2D space.Declaration
Swift
public func rotated(by rotationAngle: Measurement<UnitAngle>) -> Transformed
Parameters
rotate
The angle measurement to rotate the receiver by.
-
scaled(scaleX:
Extension methodscaleY: ) Wraps the element in an
Transformed
element that scales the receiver in 2D space.Declaration
Swift
public func scaled( scaleX: CGFloat = 1, scaleY: CGFloat = 1 ) -> Transformed
Parameters
scaleX
The X axis scale.
scaleY
The Y axis scale.
-
userInteractionEnabled(_:
Extension method) Conditionally enable user interaction of the wrapped element.
Note
When user interaction is disabled, any elements within the wrapped element will become non-interactive.Declaration
Swift
public func userInteractionEnabled(_ enabled: Bool = true) -> UserInteractionEnabled
-
blockAccessibility(isBlocking:
Extension method) Blocks all accessibility on the element, so that it is is no longer an accessibility element, and its children are hidden from the accessibility system.
Declaration
Swift
public func blockAccessibility(isBlocking: Bool = true) -> AccessibilityBlocker
-
accessibilityContainer(identifier:
Extension method) Acts as an accessibility container for any subviews where
isAccessibilityElement == true
.Declaration
Swift
public func accessibilityContainer(identifier: String? = nil) -> Element
-
accessibilityElement(label:
Extension methodvalue: traits: hint: identifier: accessibilityFrameSize: accessibilityFrameCornerStyle: customActions: customContent: ) Wraps the receiver in an accessibility element with the provided values.
Providing a
nil
value for any of these parameters will result in no resolved value for that accessibility parameter—it does not inherit parameters from the wrapped element’s accessibility configuration.Important
⚠️ This overrides the accessibility of the contained element and all of its children ⚠️Declaration
Swift
public func accessibilityElement( label: String?, value: String?, traits: Set<AccessibilityElement.Trait>, hint: String? = nil, identifier: String? = nil, accessibilityFrameSize: CGSize? = nil, accessibilityFrameCornerStyle: CornerStyle = .square, customActions: [AccessibilityElement.CustomAction] = [], customContent: [AccessibilityElement.CustomContent] = [] ) -> AccessibilityElement
-
deprecated_accessibility(label:
Extension methodvalue: hint: identifier: traits: accessibilityFrameSize: ) Wraps the receiver in an accessibility element with the provided values.
Important
⚠️ This overrides the accessibility of the contained element and all of its children ⚠️
See also
accessibilityElement
Declaration
Swift
@available(*, deprecated, renamed: "accessibilityElement(label:value:traits:hint:identifier:accessibilityFrameSize:﹚") public func deprecated_accessibility( label: String? = nil, value: String? = nil, hint: String? = nil, identifier: String? = nil, traits: Set<AccessibilityElement.Trait> = [], accessibilityFrameSize: CGSize? = nil ) -> AccessibilityElement
-
accessibilityFocus(on:
Extension method) Enables VoiceOver focus to jump to the wrapped element via the trigger.
Declaration
Swift
public func accessibilityFocus( on trigger: AccessibilityFocus.Trigger ) -> Element
Parameters
on
A reference-type trigger object that can be used to trigger accessibility focus via the
focus()
function.
-
onLinkTapped(_:
Extension method) Handle links opened in any
AttributedLabel
within this element using the provided closure.Declaration
Swift
public func onLinkTapped(_ onTap: @escaping (URL) -> Void) -> Element
-
box(background:
Extension methodcorners: cornerCurve: borders: shadow: clipsContent: ) -
editingMenu(show:
Extension methodwith: ) Allows showing the system’s
UIMenuController
editing menu upon long press of the wrapped element.myElement.editingMenu(show: .onLongPress) { EditingMenuItem.copying("A String") EditingMenuItem(.select) { print("Selected!") } }
Declaration
Swift
public func editingMenu( show gesture: EditingMenu.Gesture, @Builder<EditingMenuItem> with items: () -> [EditingMenuItem] ) -> EditingMenu
-
scrollable(_:
Extension methodconfigure: ) Wraps the element in a
ScrollView
to allow it to be scrolled if it takes up more space then is available on screen.Declaration
Swift
public func scrollable( _ contentSize: ScrollView.ContentSize = .fittingHeight, configure: (inout ScrollView) -> Void = { _ in } ) -> ScrollView
-
tappable(onTap:
Extension method) Wraps the element and calls the provided closure when tapped.
Declaration
Swift
public func tappable(onTap: @escaping () -> Void) -> Tappable
-
transition(onAppear:
Extension methodonDisappear: onLayout: ) Wraps the element in a transition container to provide an animated transition.
Declaration
Swift
public func transition( onAppear: VisibilityTransition? = nil, onDisappear: VisibilityTransition? = nil, onLayout: LayoutTransition = .inherited ) -> TransitionContainer
Parameters
onAppear
The transition to use when the element appears. By default, no transition.
onDisappear
The transition to use when the element disappears. By default, no transition.
onLayout
The transition to use when the element’s layout changes. The default value is
.inherited
, which means the element will participate in the same transition as its nearest ancestor with a specified transition. -
transition(_:
Extension method) Wraps the element in a transition container to provide an animated transition when its visibility changes.
Declaration
Swift
public func transition(_ onAppearOrDisappear: VisibilityTransition) -> TransitionContainer
Parameters
onAppearOrDisappear
The transition to use when the element appears or disappears.