⌘K

Block

Blocks in Nativeblocks represent modular UI components that can be dynamically rendered based on their configuration and properties. Each block is identified by a unique type and is implemented using the INativeBlock protocol.


Example of a Block Workflow

  1. A block is defined with its unique key type and associated properties.
  2. The block is registered in the NativeBlockProvider.
  3. During runtime, the block is retrieved and rendered using the blockView(blockProps:) method.

NativeBlockProvider

A singleton class responsible for managing and providing blocks within the Nativeblocks SDK. The NativeBlockProvider class maintains a registry of blocks that can be dynamically provided based on the block type.

class NativeBlockProvider

INativeBlock

Protocol that defines the behavior for a native block. The INativeBlock protocol is used for creating custom UI components that can be dynamically rendered in the application.

public protocol INativeBlock 

Requirements

blockView(blockProps:​)

Generates the SwiftUI view for the block using the provided properties. Required

func blockView(blockProps: BlockProps) -> any View
Parameters
  • blockProps: The properties and state information 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 

Properties

listItemIndex

The index of the item in the list that the block applies to (if applicable).

public var listItemIndex: Int? = nil

variables

A dictionary of variables that the block can use.

public var variables: [String: NativeVariableModel]? = nil

onVariableChange

Callback function to handle changes to a variable.

public var onVariableChange: ((NativeVariableModel) -> Void)? = nil

actions

A dictionary of actions associated with the block.

public var actions: [String: [NativeActionModel]]? = nil

onHandleAction

Callback function to handle an action.

public var onHandleAction: ((Int, NativeActionModel?, String) -> Void)? = nil
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.

block

The block model representing the current block.

public var block: NativeBlockModel? = nil

onSubBlock

Callback function to render sub-blocks.

public var onSubBlock: (([String: NativeBlockModel], NativeBlockSlotModel, Int) -> AnyView)? = nil
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.

NativeBlockModel

Represents a model for a native block structure used in the application. The NativeBlockModel includes various properties to define its metadata, data relationships, and hierarchical structure. It is hashable and codable, making it suitable for data persistence and comparison.

public struct NativeBlockModel: Hashable, Codable 

Inheritance

Codable, Hashable

Properties

id

The unique identifier of the block.

public var id: String? = nil

parentId

The identifier of the parent block, if any.

public var parentId: String? = nil

slot

Slot information to determine placement within a structure.

public var slot: String? = nil

keyType

The type of the key used for this block.

public var keyType: String? = nil

key

The key associated with this block.

public var key: String? = nil

visibility

Visibility of the block (e.g., visible, hidden).

public var visibility: String? = nil

position

The position of the block, used for ordering within its container.

public var position: Int? = nil

properties

A dictionary of properties associated with this block.

public var properties: [String: NativeBlockPropertyModel]? = nil

data

A dictionary of data models that belong to this block.

public var data: [String: NativeBlockDataModel]? = nil

slots

A dictionary of slots that represent different areas where sub-blocks can be placed.

public var slots: [String: NativeBlockSlotModel]? = nil

subBlocks

A dictionary of sub-blocks contained within this block.

public var subBlocks: [String: NativeBlockModel]? = nil

NativeBlockDataModel

Represents data associated with a native block.

public struct NativeBlockDataModel: Hashable, Codable 

Inheritance

Codable, Hashable

Properties

key

The key for the data entry.

public var key: String

value

The value associated with the key.

public var value: String

type

The type of the data.

public var type: String

NativeBlockPropertyModel

Represents a property model for a native block, including values for different device types.

public struct NativeBlockPropertyModel: Hashable, Codable 

Inheritance

Codable, Hashable

Properties

key

The key associated with the property.

public var key: String

valueMobile

The value for mobile devices.

public var valueMobile: String

valueTablet

The value for tablet devices.

public var valueTablet: String

valueDesktop

The value for desktop devices.

public var valueDesktop: String

type

The type of the property.

public var type: String

NativeBlockSlotModel

Represents a slot model for a native block, indicating a specific placement.

public struct NativeBlockSlotModel: Hashable, Codable 

Inheritance

Codable, Hashable

Properties

slot

The slot identifier.

public var slot: String

Type Alias

BlockIndex

Represents an index used to identify a block in a list.

public typealias BlockIndex = Int