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 encapsulate private attributes, private events, and custom global methods of specified components. 27 28 ```ts 29 // @Extend(Text) supports the private attribute fontColor of the <Text> component. 30 @Extend(Text) function fancy () { 31 .fontColor(Color.Red) 32 } 33 // superFancyText can call the predefined method fancy. 34 @Extend(Text) function superFancyText(size:number) { 35 .fontSize(size) 36 .fancy() 37 } 38 ``` 39 40 41- Unlike \@Styles, \@Extend decorated methods support parameters. You can pass in parameters when calling such methods. Regular TypeScript provisions for method parameters apply. 42 43 ```ts 44 // xxx.ets 45 @Extend(Text) function fancy (fontSize: number) { 46 .fontColor(Color.Red) 47 .fontSize(fontSize) 48 } 49 50 @Entry 51 @Component 52 struct FancyUse { 53 build() { 54 Row({ space: 10 }) { 55 Text('Fancy') 56 .fancy(16) 57 Text('Fancy') 58 .fancy(24) 59 } 60 } 61 } 62 ``` 63 64- A function can be passed as a parameter in an \@Extend decorated method. The function is used as the handler of an event. 65 66 ```ts 67 @Extend(Text) function makeMeClick(onClick: () => void) { 68 .backgroundColor(Color.Blue) 69 .onClick(onClick) 70 } 71 72 @Entry 73 @Component 74 struct FancyUse { 75 @State label: string = 'Hello World'; 76 77 onClickHandler() { 78 this.label = 'Hello ArkUI'; 79 } 80 81 build() { 82 Row({ space: 10 }) { 83 Text(`${this.label}`) 84 .makeMeClick(() => {this.onClickHandler()}) 85 } 86 } 87 } 88 ``` 89 90- 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. 91 92 ```ts 93 @Extend(Text) function fancy (fontSize: number) { 94 .fontColor(Color.Red) 95 .fontSize(fontSize) 96 } 97 98 @Entry 99 @Component 100 struct FancyUse { 101 @State fontSizeValue: number = 20 102 build() { 103 Row({ space: 10 }) { 104 Text('Fancy') 105 .fancy(this.fontSizeValue) 106 .onClick(() => { 107 this.fontSizeValue = 30 108 }) 109 } 110 } 111 } 112 ``` 113 114 115## Constraints 116 117- Unlike \@Styles, \@Extend can be defined only globally, that is, outside a component declaration. 118 119> **NOTE** 120> 121> This decorator can be used only in the current file and cannot be exported. 122> 123> To the export the decorator, you are advised to use [AttributeModifier](../ui/arkts-user-defined-extension-attributeModifier.md). 124 125[Negative Example] 126 127```ts 128@Entry 129@Component 130struct FancyUse { 131 // Incorrect format. @Extend can be defined only globally, but not inside a component. 132 @Extend(Text) function fancy (fontSize: number) { 133 .fontSize(fontSize) 134 } 135 136 build() { 137 Row({ space: 10 }) { 138 Text('Fancy') 139 .fancy(16) 140 } 141 } 142} 143``` 144 145[Positive Example] 146 147```ts 148// Correct format. 149@Extend(Text) function fancy (fontSize: number) { 150 .fontSize(fontSize) 151} 152 153@Entry 154@Component 155struct FancyUse { 156 157 build() { 158 Row({ space: 10 }) { 159 Text('Fancy') 160 .fancy(16) 161 } 162 } 163} 164``` 165 166 167## Use Scenarios 168 169The following example declares three **Text** components. The **fontStyle**, **fontWeight**, and **backgroundColor** styles are set for each **Text** component. 170 171 172```ts 173@Entry 174@Component 175struct FancyUse { 176 @State label: string = 'Hello World' 177 178 build() { 179 Row({ space: 10 }) { 180 Text(`${this.label}`) 181 .fontStyle(FontStyle.Italic) 182 .fontWeight(100) 183 .backgroundColor(Color.Blue) 184 Text(`${this.label}`) 185 .fontStyle(FontStyle.Italic) 186 .fontWeight(200) 187 .backgroundColor(Color.Pink) 188 Text(`${this.label}`) 189 .fontStyle(FontStyle.Italic) 190 .fontWeight(300) 191 .backgroundColor(Color.Orange) 192 }.margin('20%') 193 } 194} 195``` 196 197\@Extend combines and reuses styles. The following is an example: 198 199 200```ts 201@Extend(Text) function fancyText(weightValue: number, color: Color) { 202 .fontStyle(FontStyle.Italic) 203 .fontWeight(weightValue) 204 .backgroundColor(color) 205} 206``` 207 208With the use of \@Extend, the code readability is enhanced. 209 210 211```ts 212@Entry 213@Component 214struct FancyUse { 215 @State label: string = 'Hello World' 216 217 build() { 218 Row({ space: 10 }) { 219 Text(`${this.label}`) 220 .fancyText(100, Color.Blue) 221 Text(`${this.label}`) 222 .fancyText(200, Color.Pink) 223 Text(`${this.label}`) 224 .fancyText(300, Color.Orange) 225 }.margin('20%') 226 } 227} 228``` 229