Block
Blocks in Nativeblocks are modular SwiftUI components that can be dynamically rendered based on configuration and properties. Each block is identified by a unique type and implemented as a SwiftUI view.
Block Registration & Rendering
- A block is defined with a unique key type and associated properties.
- The block is registered in the NativeBlockProvider.
- At runtime, the block is retrieved and rendered using the blockView(blockProps:) method where
blockPropscontains all context necessary for dynamic rendering and interaction.
blockView(blockProps: BlockProps)
Generates the SwiftUI view for the block using the provided properties.
struct BlockView: View
Parameters
- blockProps: The properties, state, and callbacks needed to show the block.
BlockProps
Represents the properties associated with a native block.
The BlockProps struct is used to pass all the necessary information required to render a block, including variables, actions, and callbacks.
public struct BlockProps {
/// Instance name of NativeblocksManager.
public let instanceName: String
/// The index of the item in the list that the block applies to (if applicable).
public let listItemIndex: Int
/// A function for retrieving a [NativeVariableModel] by its key.
public let onFindVariable: (String) -> NativeVariableModel?
/// Callback function to handle changes to a variable.
public let onVariableChange: (NativeVariableModel) -> Void
/// A function for retrieving a [NativeActionModel] by its event type.
public let onFindAction: (String) -> NativeActionModel?
/// Callback function to handle an action.
/// - Parameters:
/// - listItemIndex: The index of the item in the list that the action applies to.
/// - action: The action model to be executed.
/// - type: The type of action being performed.
public let onHandleAction: (Int, NativeActionModel?, String) -> Void
/// A closure that handles text localization for the block.
/// - Parameter key: The localization key to be translated.
/// - Returns: The localized string if available, or `nil` if no localization exists for the given key.
public let onLocalize: (String) -> String?
/// The block model representing the current block.
public let block: NativeBlockModel?
/// An array representing the hierarchical structure of blocks from root to current block.
/// Each element in the array contains information about a block's position in the overall layout hierarchy.
/// This can be used to understand the block's context and its relationship with parent blocks.
public let hierarchy: [NativeBlockHierarchyModel]
/// Callback function to render sub-blocks.
/// - Parameters:
/// - subBlocks: A dictionary of sub-blocks to be rendered.
/// - subSlot: The slot model that indicates where the sub-blocks should be placed.
/// - itemIndex: The index of the item in the list for which sub-blocks are rendered.
/// - Returns: An `AnyView` containing the rendered sub-blocks.
public let onSubBlock: ([String: NativeBlockModel], NativeBlockSlotModel, Int, Any?) -> AnyView
}
NativeBlockModel
Represents a model for a native block structure in the application.
NativeBlockModel includes metadata, key info, relationships, slot definitions and handles sub-blocks and hierarchical info.
public struct NativeBlockModel: Hashable, Codable
Properties
- id: Unique identifier of the block
- parentId: Identifier of the parent block (optional)
- slot: Slot information for placement
- keyType: The type of key for this block
- key: The key associated
- visibility: Block visibility (e.g. visible, hidden)
- position: Ordering index in container
- properties: Dictionary of additional block configuration
- data: Dictionary of data models for this block
- slots: Dictionary of slots for sub-block placement
- subBlocks: Dictionary of contained sub-blocks
NativeBlockDataModel
Represents data associated with a native block.
public struct NativeBlockDataModel: Hashable, Codable
Properties
- key: The data key
- value: Associated value
- type: Data type
NativeBlockPropertyModel
Represents a property for a native block and provides device-specific values.
public struct NativeBlockPropertyModel: Hashable, Codable
Properties
- key: Property key
- valueMobile: Value for mobile
- valueTablet: Value for tablet
- valueDesktop: Value for desktop
- type: Property type
NativeBlockSlotModel
Represents a slot model for a native block.
public struct NativeBlockSlotModel: Hashable, Codable
Properties
- slot: Slot identifier
NativeBlockHierarchyModel
Represents a block in the hierarchy and its metadata.
public struct NativeBlockHierarchyModel: Codable, Hashable
Properties
- key: Unique key for the block
- position: Position in container
- hierarchyPosition: Index in hierarchy
- keyType: Type of block (e.g., row, box)
- slot: Slot in layout
- scope: Shared view state/properties
Type Alias
BlockIndex
Represents an index used to identify a block in a list.
public typealias BlockIndex = Int