1e41f4b71Sopenharmony_ci# Custom Components
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciThe ArkUI that uses the JavaScript-based web-like development paradigm supports custom components for you to extend existing components. You can encapsulate custom private attributes and events into new components to reuse them multiple times in your project. This also improves code readability. The following is an example:
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci- Building a custom component
8e41f4b71Sopenharmony_ci  
9e41f4b71Sopenharmony_ci  ```html
10e41f4b71Sopenharmony_ci  <!-- comp.hml -->
11e41f4b71Sopenharmony_ci   <div class="item"> 
12e41f4b71Sopenharmony_ci     <text class="title-style">{{title}}</text>
13e41f4b71Sopenharmony_ci     <text class="text-style" onclick="childClicked" focusable="true">Click to view the hidden text.</text>
14e41f4b71Sopenharmony_ci     <text class="text-style" if="{{showObj}}">hello world</text>
15e41f4b71Sopenharmony_ci   </div>
16e41f4b71Sopenharmony_ci  ```
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci  ```css
19e41f4b71Sopenharmony_ci  /* comp.css */
20e41f4b71Sopenharmony_ci   .item { 
21e41f4b71Sopenharmony_ci     width: 700px;  
22e41f4b71Sopenharmony_ci     flex-direction: column;  
23e41f4b71Sopenharmony_ci     height: 300px;  
24e41f4b71Sopenharmony_ci     align-items: center;  
25e41f4b71Sopenharmony_ci     margin-top: 100px; 
26e41f4b71Sopenharmony_ci   }
27e41f4b71Sopenharmony_ci   .text-style {
28e41f4b71Sopenharmony_ci     width: 100%;
29e41f4b71Sopenharmony_ci     text-align: center;
30e41f4b71Sopenharmony_ci     font-weight: 500;
31e41f4b71Sopenharmony_ci     font-family: Courier;
32e41f4b71Sopenharmony_ci     font-size: 36px;
33e41f4b71Sopenharmony_ci   }
34e41f4b71Sopenharmony_ci   .title-style {
35e41f4b71Sopenharmony_ci     font-weight: 500;
36e41f4b71Sopenharmony_ci     font-family: Courier;
37e41f4b71Sopenharmony_ci     font-size: 50px;
38e41f4b71Sopenharmony_ci     color: #483d8b;
39e41f4b71Sopenharmony_ci   }
40e41f4b71Sopenharmony_ci  ```
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci  ```js
43e41f4b71Sopenharmony_ci  // comp.js
44e41f4b71Sopenharmony_ci   export default {
45e41f4b71Sopenharmony_ci     props: {
46e41f4b71Sopenharmony_ci       title: {
47e41f4b71Sopenharmony_ci         default: 'title',
48e41f4b71Sopenharmony_ci       },
49e41f4b71Sopenharmony_ci       showObject: {},
50e41f4b71Sopenharmony_ci     },
51e41f4b71Sopenharmony_ci     data() { 
52e41f4b71Sopenharmony_ci       return {
53e41f4b71Sopenharmony_ci         showObj: this.showObject,
54e41f4b71Sopenharmony_ci       };
55e41f4b71Sopenharmony_ci     }, 
56e41f4b71Sopenharmony_ci     childClicked () { 
57e41f4b71Sopenharmony_ci       this.$emit('eventType1', {text: 'Receive the parameters from the child component.'});
58e41f4b71Sopenharmony_ci       this.showObj = !this.showObj; 
59e41f4b71Sopenharmony_ci     }, 
60e41f4b71Sopenharmony_ci   }
61e41f4b71Sopenharmony_ci  ```
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci- Introducing the custom component
64e41f4b71Sopenharmony_ci  
65e41f4b71Sopenharmony_ci  ```html
66e41f4b71Sopenharmony_ci  <!-- xxx.hml -->
67e41f4b71Sopenharmony_ci   <element name='comp' src='../../common/component/comp.hml'></element> 
68e41f4b71Sopenharmony_ci   <div class="container"> 
69e41f4b71Sopenharmony_ci     <text>Parent component: {{text}}</text>
70e41f4b71Sopenharmony_ci     <comp title="Custom component" show-object="{{isShow}}" @event-type1="textClicked"></comp>
71e41f4b71Sopenharmony_ci   </div>
72e41f4b71Sopenharmony_ci  ```
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci  ```css
75e41f4b71Sopenharmony_ci  /* xxx.css */
76e41f4b71Sopenharmony_ci   .container { 
77e41f4b71Sopenharmony_ci     background-color: #f8f8ff; 
78e41f4b71Sopenharmony_ci     flex: 1; 
79e41f4b71Sopenharmony_ci     flex-direction: column; 
80e41f4b71Sopenharmony_ci     align-content: center;
81e41f4b71Sopenharmony_ci   } 
82e41f4b71Sopenharmony_ci  ```
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci  ```js
85e41f4b71Sopenharmony_ci  // xxx.js
86e41f4b71Sopenharmony_ci   export default { 
87e41f4b71Sopenharmony_ci     data: {
88e41f4b71Sopenharmony_ci       text: 'Start'
89e41f4b71Sopenharmony_ci       isShow: false,
90e41f4b71Sopenharmony_ci     },
91e41f4b71Sopenharmony_ci     textClicked (e) {
92e41f4b71Sopenharmony_ci       this.text = e.detail.text;
93e41f4b71Sopenharmony_ci     },
94e41f4b71Sopenharmony_ci   }
95e41f4b71Sopenharmony_ci  ```
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ciIn this example, the parent component passes the customized attribute through title to the child component, and the child component receives the attribute value in props. The child component passes text to the parent through the bound event, and the passed value is obtained via e.detail. To successfully bind the child component event to the parent component, ensure that the event name meet the requirements for event binding. For details, see [Basic Usage of Custom Components](../reference/apis-arkui/arkui-js/js-components-custom-basic-usage.md). The following figures show how the custom component works.
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci**Figure 1** Running effect
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci![en-us_image_0000001222807816](figures/en-us_image_0000001222807816.png)
103