1e41f4b71Sopenharmony_ci# ContentSlot: Representing a Placeholder in Hybrid Development 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **ContentSlot** component is a component designed to render and manage components created on the native layer using C APIs. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciWith support for hybrid development, the **ContentSlot** component is recommended when the container is an ArkTS component and the child component is created on the native side. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## APIs 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci### ContentSlot API 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciContentSlot(content: Content); // You need to use NodeContent provided by ArkUI as the manager. 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 16e41f4b71Sopenharmony_ci| ------- | -------- | ---- | ------------------------------------------------------------ | 17e41f4b71Sopenharmony_ci| content | Content | Yes | Manager of the **ContentSlot** component. Through the APIs provided by the native side, it can register and trigger the attach and detach event callbacks for **ContentSlot**, as well as manage the child components of **ContentSlot**.| 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci```ts 20e41f4b71Sopenharmony_ciabstract class Content { 21e41f4b71Sopenharmony_ci} 22e41f4b71Sopenharmony_ci``` 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci### ContentSlotInterface 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci(content: Content): ContentSlotAttribute; 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ciCalled when content is added to this **ContentSlot** component. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci**Parameters** 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 35e41f4b71Sopenharmony_ci| ------- | -------- | ---- | ------------------------------------------------------------ | 36e41f4b71Sopenharmony_ci| content | Content | Yes | Manager of the **ContentSlot** component. Through the APIs provided by the native side, it can register and trigger the attach and detach event callbacks for **ContentSlot**, as well as manage the child components of **ContentSlot**.| 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci### ContentSlotAttribute 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ciDefines the **ContentSlot** attributes to prevent incorrect recursive use of **ContentSlot**. 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci### Native API 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci| API| Description| 47e41f4b71Sopenharmony_ci| -------- | -------- | 48e41f4b71Sopenharmony_ci|OH_ArkUI_NodeContent_RegisterCallback(ArkUI_NodeContentHandle content, ArkUI_NodeContentCallback callback)|Registers an event with the **Content** manager.| 49e41f4b71Sopenharmony_ci|OH_ArkUI_NodeContentEvent_GetEventType(ArkUI_NodeContentEvent* event)|Obtains the type of the event triggered on the **Content**.| 50e41f4b71Sopenharmony_ci|OH_ArkUI_NodeContent_AddNode(ArkUI_NodeContentHandle content, ArkUI_NodeHandle node)|Adds a child component to **Content**.| 51e41f4b71Sopenharmony_ci|OH_ArkUI_NodeContent_InsertNode(ArkUI_NodeContentHandle content, ArkUI_NodeHandle node, int32_t position)|Inserts a child component into **Content**.| 52e41f4b71Sopenharmony_ci|OH_ArkUI_NodeContent_RemoveNode(ArkUI_NodeContentHandle content, ArkUI_NodeHandle node)|Removes a child component from **Content**.| 53e41f4b71Sopenharmony_ci|OH_ArkUI_GetNodeContentFromNapiValue(napi_env env, napi_value value, ArkUI_NodeContentHandle* content)|Obtains the pointer to **Content** in ArkTS from the native side.| 54e41f4b71Sopenharmony_ci|OH_ArkUI_NodeContentEvent_GetNodeContentHandle(ArkUI_NodeContentEvent* event)|Obtains the **Content** object that triggers the attach and detach events.| 55e41f4b71Sopenharmony_ci|OH_ArkUI_NodeContent_SetUserData(ArkUI_NodeContentHandle content, void* userData)|Sets the custom attributes on **Content**.| 56e41f4b71Sopenharmony_ci|OH_ArkUI_NodeContent_GetUserData(ArkUI_NodeContentHandle content)|Obtains the custom attributes from **Content**.| 57e41f4b71Sopenharmony_ci|typedef enum {<br> NOTE_CONTENT_EVENT_ON_ATTACH_TO_WINDOW = 0,<br> NOTE_CONTENT_EVENT_ON_DETACH_FROM_WINDOW = 1,<br>} ArkUI_NodeContentEventType|Enumerates the event types on **Content**.| 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci## Development and Implementation 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci### Code Implementation in ArkTS 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci```ts 64e41f4b71Sopenharmony_ciimport { nativeNode } from'libNativeNode.so' // so. file implemented by you. 65e41f4b71Sopenharmony_ciimport { NodeContent } from '@kit.ArkUI' 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci@Component 68e41f4b71Sopenharmony_cistruct Parent { 69e41f4b71Sopenharmony_ci private nodeContent: Content = new NodeContent(); 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci aboutToAppear() { 72e41f4b71Sopenharmony_ci // Create a node through the C API and add it to the nodeContent manager. 73e41f4b71Sopenharmony_ci nativeNode.createNativeNode(this.nodeContent); 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci build() { 77e41f4b71Sopenharmony_ci Column() { 78e41f4b71Sopenharmony_ci // Display the native components stored in the nodeContent manager. 79e41f4b71Sopenharmony_ci ContentSlot(this.nodeContent) 80e41f4b71Sopenharmony_ci } 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci} 83e41f4b71Sopenharmony_ci``` 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci### Code Implementation in C 86e41f4b71Sopenharmony_ciFor details about the basic development knowledge of Node-API, see [Getting Started with the NDK](../napi/ndk-development-overview.md). 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ciThis topic only describes how to implement the logic code related to **ContentSlot**. For details about how to create components in C, see section [ArkUI_NativeModule](../reference/apis-arkui/_ark_u_i___native_module.md) in the ArkUI API documents. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci```c++ 91e41f4b71Sopenharmony_ciArkUI_NodeContentHandle nodeContentHandle_ = nullptr; 92e41f4b71Sopenharmony_ciArkUI_NativeNode_API_1 *nodeAPI; 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_cinapi_value Manager::CreateNativeNode(napi_env, napi_callback_info info) { 95e41f4b71Sopenharmony_ci // Solve null pointer and out-of-bounds issues related to Node-API. 96e41f4b71Sopenharmony_ci if ((env == nullptr) || (info == nullptr)) { 97e41f4b71Sopenharmony_ci return nullptr; 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci size_t argc = 1; 101e41f4b71Sopenharmony_ci napi_value args[1] = { nullptr }; 102e41f4b71Sopenharmony_ci if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { 103e41f4b71Sopenharmony_ci OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", "CreateNativeNode napi_get_cb_info failed"); 104e41f4b71Sopenharmony_ci } 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci if (argc != 1) { 107e41f4b71Sopenharmony_ci return nullptr; 108e41f4b71Sopenharmony_ci } 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci // Point nodeContentHandle_ to a nodeContent object passed in from ArkTS. 111e41f4b71Sopenharmony_ci OH_ARKUI_GetNodeContentFromNapiValue(env, args[0], &nodeContentHandle_); 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNode_API_1")); 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci if (nodeAPI != nullptr) { 116e41f4b71Sopenharmony_ci if (nodeAPI->createNode != nullptr && nodeAPI->addChild != nullptr) { 117e41f4b71Sopenharmony_ci ArkUINodeHandle component; 118e41f4b71Sopenharmony_ci // Create components in C. 119e41f4b71Sopenharmony_ci component = CreateNodeHandle(); 120e41f4b71Sopenharmony_ci // Add the component to the nodeContent manager. 121e41f4b71Sopenharmony_ci OH_ArkUI_NodeContent_AddNode(nodeContentHandle_, component); 122e41f4b71Sopenharmony_ci } 123e41f4b71Sopenharmony_ci } 124e41f4b71Sopenharmony_ci} 125e41f4b71Sopenharmony_ci``` 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci#### Registering Attach and Detach Events and Obtaining Corresponding Content Object 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci```c++ 130e41f4b71Sopenharmony_ciauto nodeContentEvent = [](ArkUI_NodeContentEvent *event) { 131e41f4b71Sopenharmony_ci ArkUI_NodeContentHandle content = OH_ArkUI_NodeContentEvent_GetNodeContentHandle(event); 132e41f4b71Sopenharmony_ci // Additional logic required for different contents. 133e41f4b71Sopenharmony_ci if (OH_ArkUINodeContentEvent_GetEventType(event) = NODE_CONTENT_EVENT_ON_ATTACH_TO_WINDOW) { 134e41f4b71Sopenharmony_ci // Logic to be triggered when an attach event occurs on ContentSlot. 135e41f4b71Sopenharmony_ci } else if (OH_ArkUINodeContentEvent_GetEventType(event) = NODE_CONTENT_EVENT_ON_DETACH_FROM_WINDOW) { 136e41f4b71Sopenharmony_ci // Logic to be triggered when a detach event occurs on ContentSlot. 137e41f4b71Sopenharmony_ci }; 138e41f4b71Sopenharmony_ci}; 139e41f4b71Sopenharmony_ci// Register an event with nodeContent. 140e41f4b71Sopenharmony_ciOH_ArkUI_NodeContent_RegisterCallback(nodeContentHandle_, nodeContentEvent); 141e41f4b71Sopenharmony_ci``` 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci#### Adding Child Components 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci```c++ 146e41f4b71Sopenharmony_ciArkUINodeHandle component; 147e41f4b71Sopenharmony_cicomponent = CreateNodeHandle(); 148e41f4b71Sopenharmony_ci// Add the component to the nodeContent manager. 149e41f4b71Sopenharmony_ciOH_ArkUI_NodeContent_AddNode(nodeContentHandle_, component); 150e41f4b71Sopenharmony_ci``` 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci#### Inserting Child Components 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci```c++ 155e41f4b71Sopenharmony_ciArkUINodeHandle component; 156e41f4b71Sopenharmony_cicomponent = CreateNodeHandle(); 157e41f4b71Sopenharmony_ci// Insert a component into the specified position of the nodeContent manager. 158e41f4b71Sopenharmony_ciOH_ArkUI_NodeContent_InsertNode(nodeContentHandle_, component, position); 159e41f4b71Sopenharmony_ci``` 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci#### Removing Child Components 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci```c++ 164e41f4b71Sopenharmony_ci// Remove a component from the nodeContent manager. 165e41f4b71Sopenharmony_ciOH_ArkUI_NodeContent_RemoveNode(nodeContentHandle_, component); 166e41f4b71Sopenharmony_ci``` 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci#### Setting Custom Attributes 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci```c++ 171e41f4b71Sopenharmony_ci// Create the custom data to be defined. 172e41f4b71Sopenharmony_civoid *userData = CreateUserData(); 173e41f4b71Sopenharmony_ciOH_ArkUI_NodeContent_SetUserData(nodeContentHandle_, userData); 174e41f4b71Sopenharmony_ci``` 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci#### Obtaining Custom Attributes 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci``` 179e41f4b71Sopenharmony_civoid *userData = OH_ArkUI_NodeContent_GetUserData(nodeContentHandle_); 180e41f4b71Sopenharmony_ci``` 181