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
UIViewinstance are referred to as “view-backed”. Elements become view-backed by returning aViewDescriptionvalue from this method.Declaration
Swift
func backingViewDescription(with context: ViewDescriptionContext) -> ViewDescription?Parameters
contextThe context this element is rendered in.
Return Value
An optional
ViewDescription. -
adaptedEnvironment(key:Extension methodvalue: ) Wraps this element in an
AdaptedEnvironmentwith 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
AdaptedEnvironmentwith 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
AdaptedEnvironmentwith 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
Alignedelement with the provided parameters.Declaration
Swift
public func aligned( vertically: Aligned.VerticalAlignment = .center, horizontally: Aligned.HorizontalAlignment = .center ) -> AlignedParameters
verticallyThe vertical alignment. Defaults to
.centered.horizontallyThe horizontal alignment. Defaults to
.centered. -
centered()Extension method
-
if(_:Extension methodthen: ) Returns a new element from the provided
modifyclosure, 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
thenclosure if the provided boolean is true. If the provided boolean is false, theelseclosure 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
modifyclosure 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
thenclosure if the provided boolean is true. If the provided value is nil, theelseclosure 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
mapfunction.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
modifyfunction, 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 ) -> ConstrainedAspectRatioParameters
aspectRatioThe aspect ratio that the content size should match.
contentModeHow 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
wrappedelement, and positions it according to thepositionparameter.See the
Decorateelement for more. -
flowChild(priority:Extension methodkey: ) Wraps the element in a
Flow.Childto allow customizing the item in the flow layout.
-
gridRowChild(key:Extension methodwidth: ) Wraps an element with a
GridRowChildin order to provide meta information that aGridRowcan aply to its layout.Declaration
Parameters
keyA unique identifier for the child.
widthThe 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.isHiddenproperty 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) Keyedallows providing aHashablevalue 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.Childin order to customizeStackLayout.Traitsand the key.Declaration
Swift
public func stackLayoutChild( priority: StackLayout.Child.Priority = .flexible, alignmentGuide: ((ElementDimensions) -> CGFloat)? = nil, key: AnyHashable? = nil ) -> StackLayout.ChildParameters
priorityControls the amount of extra space distributed to this child during underflow and overflow
alignmentGuideAllows for custom alignment of a child along the cross axis.
keyA 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.Childin order to customize theStackLayout.Child.Priority.Declaration
Swift
public func stackLayoutChild( priority: StackLayout.Child.Priority ) -> StackLayout.ChildParameters
priorityControls 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
Transformedelement with the provided 3D transform.Declaration
Swift
public func transformed(_ transform: CATransform3D) -> TransformedParameters
transformThe 3D transform to be applied.
-
transformed(_:Extension method) Wraps the element in an
Transformedelement with the provided 2D transform.Declaration
Swift
public func transformed(_ transform: CGAffineTransform) -> TransformedParameters
transformThe 2D transform to be applied.
-
translated(translateX:Extension methodtranslateY: translateZ: ) Wraps the element in an
Transformedelement that translates the receiver in 3D space.Declaration
Swift
public func translated( translateX: CGFloat = 0, translateY: CGFloat = 0, translateZ: CGFloat = 0 ) -> TransformedParameters
transformXThe X component of the translation.
transformYThe Y component of the translation.
transformZThe Z component of the translation.
-
rotated(by:Extension method) Wraps the element in an
Transformedelement that rotates the receiver in 2D space.Declaration
Swift
public func rotated(by rotationAngle: Measurement<UnitAngle>) -> TransformedParameters
rotateThe angle measurement to rotate the receiver by.
-
scaled(scaleX:Extension methodscaleY: ) Wraps the element in an
Transformedelement that scales the receiver in 2D space.Declaration
Swift
public func scaled( scaleX: CGFloat = 1, scaleY: CGFloat = 1 ) -> TransformedParameters
scaleXThe X axis scale.
scaleYThe 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(containerType:Extension methodlabel: value: identifier: ) Acts as an accessibility container for any subviews where
isAccessibilityElement == true.Declaration
Swift
public func accessibilityContainer( containerType: AccessibilityContainer.ContainerType = .none, label: String? = nil, value: String? = nil, identifier: String? = nil ) -> Element -
accessibilityElement(label:Extension methodvalue: traits: hint: identifier: accessibilityFrameSize: accessibilityFrameCornerStyle: customActions: customContent: userInputLabels: ) Wraps the receiver in an accessibility element with the provided values.
Providing a
nilvalue 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] = [], userInputLabels: [String] = [] ) -> 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 ) -> ElementParameters
onA reference-type trigger object that can be used to trigger accessibility focus via the
focus()function. -
accessibilityShowsLargeContentViewer(display:Extension methodscalesLargeContentImage: largeContentImageInsets: interactionEndedCallback: ) Adds large content viewer support to an individual element. Ensure that you use this in conjunction with accessibilityLargeContentViewerInteractionContainer().
Large content viewer allows users to see a larger version of content when they press and hold on small UI elements. This is particularly useful for users who are low vision. It must only be used if dynamic type is not an option for a given element; it must not be used as a substitute for dynamic type. It’s triggered by a long press gesture and shows an enlarged version of the content in a special overlay. It’s only available when accessibility system type sizes.
Declaration
Swift
public func accessibilityShowsLargeContentViewer( display: Accessibility.LargeContentViewerConfiguration.Display, scalesLargeContentImage: Bool = false, largeContentImageInsets: UIEdgeInsets = .zero, interactionEndedCallback: ((CGPoint) -> Void)? = nil ) -> ElementParameters
titleThe title to display in the large content viewer. Defaults to nil.
imageThe image to display in the large content viewer. Defaults to nil.
scalesLargeContentImageWhether the image should be scaled in the large content viewer. Defaults to false.
largeContentImageInsetsThe insets to apply to the large content image. Defaults to zero insets.
-
accessibilityLargeContentViewerInteractionContainer()Extension methodAdds a large content viewer interaction container to the element. This is used to wrap elements that need to be able to show a large content viewer. Use this in conjunction with accessibilityShowsLargeContentViewer() on elements that need to show a large content viewer. Elements that are wrapped in this container will be able to show a large content viewer and allow a user to swipe through them with one finger and have the HUD update in real time.
Declaration
Swift
public func accessibilityLargeContentViewerInteractionContainer() -> Element
-
onLinkTapped(_:Extension method) Handle links opened in any
AttributedLabelwithin 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
UIMenuControllerediting 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
ScrollViewto 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 ) -> TransitionContainerParameters
onAppearThe transition to use when the element appears. By default, no transition.
onDisappearThe transition to use when the element disappears. By default, no transition.
onLayoutThe 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) -> TransitionContainerParameters
onAppearOrDisappearThe transition to use when the element appears or disappears.
-
overrideUserInterfaceStyle(_:Extension method) Wraps this element in a
UserInterfaceStyleOverridingElementto override its interface style.This method provides a convenient way to override the user interface style (light/dark mode) for any element.
Example:
Label(text: "Always Light Mode") .overrideUserInterfaceStyle(.light)Declaration
Swift
public func overrideUserInterfaceStyle( _ override: UIUserInterfaceStyle = .unspecified ) -> UserInterfaceStyleOverridingElementParameters
overrideThe desired interface style to apply. Defaults to
.unspecified.Return Value
A new element wrapped with the specified interface style override.
View on GitHub
Element Protocol Reference