1# \@Extend Decorator: Extension of Built-in Components
2
3
4Apart from [\@Styles](arkts-style.md) used to reuse styles, ArkUI also provides \@Extend, which allows you to add a new attribute feature to a built-in component.
5
6
7> **NOTE**
8>
9> Since API version 9, this decorator is supported in ArkTS widgets.
10>
11> This decorator can be used in atomic services since API version 11.
12
13## How to Use
14
15
16### Syntax
17
18
19```ts
20@Extend(UIComponentName) function functionName { ... }
21```
22
23
24### Rules of Use
25
26- Unlike \@Styles, \@Extend can be defined only globally, that is, outside a component declaration.
27
28> **NOTE**
29> This decorator can be used only in the current file and cannot be exported.
30
31- Unlike \@Styles, \@Extend can encapsulate private attributes, private events, and custom global methods of specified components.
32
33  ```ts
34  // @Extend(Text) supports the private attribute fontColor of the <Text> component.
35  @Extend(Text) function fancy () {
36    .fontColor(Color.Red)
37  }
38  // superFancyText can call the predefined method fancy.
39  @Extend(Text) function superFancyText(size:number) {
40      .fontSize(size)
41      .fancy()
42  }
43  ```
44
45
46- Unlike \@Styles, \@Extend decorated methods support parameters. You can pass in parameters when calling such methods. Regular TypeScript provisions for method parameters apply.
47
48  ```ts
49  // xxx.ets
50  @Extend(Text) function fancy (fontSize: number) {
51    .fontColor(Color.Red)
52    .fontSize(fontSize)
53  }
54
55  @Entry
56  @Component
57  struct FancyUse {
58    build() {
59      Row({ space: 10 }) {
60        Text('Fancy')
61          .fancy(16)
62        Text('Fancy')
63          .fancy(24)
64      }
65    }
66  }
67  ```
68
69- A function can be passed as a parameter in an \@Extend decorated method. The function is used as the handler of an event.
70
71  ```ts
72  @Extend(Text) function makeMeClick(onClick: () => void) {
73    .backgroundColor(Color.Blue)
74    .onClick(onClick)
75  }
76
77  @Entry
78  @Component
79  struct FancyUse {
80    @State label: string = 'Hello World';
81
82    onClickHandler() {
83      this.label = 'Hello ArkUI';
84    }
85
86    build() {
87      Row({ space: 10 }) {
88        Text(`${this.label}`)
89          .makeMeClick(() => {this.onClickHandler()})
90      }
91    }
92  }
93  ```
94
95- A [state variable](arkts-state-management-overview.md) can be passed as a parameter in an \@Extend decorated method. When the state variable changes, the UI is re-rendered.
96
97  ```ts
98  @Extend(Text) function fancy (fontSize: number) {
99    .fontColor(Color.Red)
100    .fontSize(fontSize)
101  }
102
103  @Entry
104  @Component
105  struct FancyUse {
106    @State fontSizeValue: number = 20
107    build() {
108      Row({ space: 10 }) {
109        Text('Fancy')
110          .fancy(this.fontSizeValue)
111          .onClick(() => {
112            this.fontSizeValue = 30
113          })
114      }
115    }
116  }
117  ```
118
119
120## Use Scenarios
121
122The following example declares three **Text** components. The **fontStyle**, **fontWeight**, and **backgroundColor** styles are set for each **Text** component.
123
124
125```ts
126@Entry
127@Component
128struct FancyUse {
129  @State label: string = 'Hello World'
130
131  build() {
132    Row({ space: 10 }) {
133      Text(`${this.label}`)
134        .fontStyle(FontStyle.Italic)
135        .fontWeight(100)
136        .backgroundColor(Color.Blue)
137      Text(`${this.label}`)
138        .fontStyle(FontStyle.Italic)
139        .fontWeight(200)
140        .backgroundColor(Color.Pink)
141      Text(`${this.label}`)
142        .fontStyle(FontStyle.Italic)
143        .fontWeight(300)
144        .backgroundColor(Color.Orange)
145    }.margin('20%')
146  }
147}
148```
149
150\@Extend combines and reuses styles. The following is an example:
151
152
153```ts
154@Extend(Text) function fancyText(weightValue: number, color: Color) {
155  .fontStyle(FontStyle.Italic)
156  .fontWeight(weightValue)
157  .backgroundColor(color)
158}
159```
160
161With the use of \@Extend, the code readability is enhanced.
162
163
164```ts
165@Entry
166@Component
167struct FancyUse {
168  @State label: string = 'Hello World'
169
170  build() {
171    Row({ space: 10 }) {
172      Text(`${this.label}`)
173        .fancyText(100, Color.Blue)
174      Text(`${this.label}`)
175        .fancyText(200, Color.Pink)
176      Text(`${this.label}`)
177        .fancyText(300, Color.Orange)
178    }.margin('20%')
179  }
180}
181```
182