1e41f4b71Sopenharmony_ci# stateStyles: Polymorphic Style 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciUnlike \@Styles, which are used to reuse styles only on static pages, stateStyles enables you to set state-specific styles. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci> **NOTE** 7e41f4b71Sopenharmony_ci> 8e41f4b71Sopenharmony_ci> Polymorphic style supports only universal attributes. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## Overview 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_cistateStyles is an attribute method that sets the style based on the internal state of a component. It is similar to a CSS pseudo-class, with different syntax. ArkUI provides the following states: 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci- focused 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci- normal 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci- pressed 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci- disabled 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci- selected<sup>10+</sup> 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci## Application Scenarios 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci### Common Scenarios 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ciThis example shows the most basic application scenario of stateStyles. **Button1** is the first component and **Button2** the second component. When the component is pressed, the black style specified for **pressed** takes effect. When the Tab key is pressed for sequential navigation, **Button1** obtains focus first and is displayed in the pink style specified for **focus**. When **Button 2** is focused, it is displayed in the pink style specified for **focus**, and **Button1** changes to the blue style specified for **normal**. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci```ts 34e41f4b71Sopenharmony_ci@Entry 35e41f4b71Sopenharmony_ci@Component 36e41f4b71Sopenharmony_cistruct StateStylesSample { 37e41f4b71Sopenharmony_ci build() { 38e41f4b71Sopenharmony_ci Column() { 39e41f4b71Sopenharmony_ci Button('Button1') 40e41f4b71Sopenharmony_ci .stateStyles({ 41e41f4b71Sopenharmony_ci focused: { 42e41f4b71Sopenharmony_ci .backgroundColor('#ffffeef0') 43e41f4b71Sopenharmony_ci }, 44e41f4b71Sopenharmony_ci pressed: { 45e41f4b71Sopenharmony_ci .backgroundColor('#ff707070') 46e41f4b71Sopenharmony_ci }, 47e41f4b71Sopenharmony_ci normal: { 48e41f4b71Sopenharmony_ci .backgroundColor('#ff2787d9') 49e41f4b71Sopenharmony_ci } 50e41f4b71Sopenharmony_ci }) 51e41f4b71Sopenharmony_ci .margin(20) 52e41f4b71Sopenharmony_ci Button('Button2') 53e41f4b71Sopenharmony_ci .stateStyles({ 54e41f4b71Sopenharmony_ci focused: { 55e41f4b71Sopenharmony_ci .backgroundColor('#ffffeef0') 56e41f4b71Sopenharmony_ci }, 57e41f4b71Sopenharmony_ci pressed: { 58e41f4b71Sopenharmony_ci .backgroundColor('#ff707070') 59e41f4b71Sopenharmony_ci }, 60e41f4b71Sopenharmony_ci normal: { 61e41f4b71Sopenharmony_ci .backgroundColor('#ff2787d9') 62e41f4b71Sopenharmony_ci } 63e41f4b71Sopenharmony_ci }) 64e41f4b71Sopenharmony_ci }.margin('30%') 65e41f4b71Sopenharmony_ci } 66e41f4b71Sopenharmony_ci} 67e41f4b71Sopenharmony_ci``` 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci **Figure 1** Focused and pressed states 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci### Combined Use of \@Styles and stateStyles 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ciThe following example uses \@Styles to specify different states of stateStyles. 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci```ts 80e41f4b71Sopenharmony_ci@Entry 81e41f4b71Sopenharmony_ci@Component 82e41f4b71Sopenharmony_cistruct MyComponent { 83e41f4b71Sopenharmony_ci @Styles normalStyle() { 84e41f4b71Sopenharmony_ci .backgroundColor(Color.Gray) 85e41f4b71Sopenharmony_ci } 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci @Styles pressedStyle() { 88e41f4b71Sopenharmony_ci .backgroundColor(Color.Red) 89e41f4b71Sopenharmony_ci } 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci build() { 92e41f4b71Sopenharmony_ci Column() { 93e41f4b71Sopenharmony_ci Text('Text1') 94e41f4b71Sopenharmony_ci .fontSize(50) 95e41f4b71Sopenharmony_ci .fontColor(Color.White) 96e41f4b71Sopenharmony_ci .stateStyles({ 97e41f4b71Sopenharmony_ci normal: this.normalStyle, 98e41f4b71Sopenharmony_ci pressed: this.pressedStyle, 99e41f4b71Sopenharmony_ci }) 100e41f4b71Sopenharmony_ci } 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci} 103e41f4b71Sopenharmony_ci``` 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci **Figure 2** Normal and pressed states 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci### Using Regular Variables and State Variables in stateStyles 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_cistateStyles can use **this** to bind regular variables and state variables in a component. 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci```ts 116e41f4b71Sopenharmony_ci@Entry 117e41f4b71Sopenharmony_ci@Component 118e41f4b71Sopenharmony_cistruct CompWithInlineStateStyles { 119e41f4b71Sopenharmony_ci @State focusedColor: Color = Color.Red; 120e41f4b71Sopenharmony_ci normalColor: Color = Color.Green 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci build() { 123e41f4b71Sopenharmony_ci Column() { 124e41f4b71Sopenharmony_ci Button('clickMe').height(100).width(100) 125e41f4b71Sopenharmony_ci .stateStyles({ 126e41f4b71Sopenharmony_ci normal: { 127e41f4b71Sopenharmony_ci .backgroundColor(this.normalColor) 128e41f4b71Sopenharmony_ci }, 129e41f4b71Sopenharmony_ci focused: { 130e41f4b71Sopenharmony_ci .backgroundColor(this.focusedColor) 131e41f4b71Sopenharmony_ci } 132e41f4b71Sopenharmony_ci }) 133e41f4b71Sopenharmony_ci .onClick(() => { 134e41f4b71Sopenharmony_ci this.focusedColor = Color.Pink 135e41f4b71Sopenharmony_ci }) 136e41f4b71Sopenharmony_ci .margin('30%') 137e41f4b71Sopenharmony_ci } 138e41f4b71Sopenharmony_ci } 139e41f4b71Sopenharmony_ci} 140e41f4b71Sopenharmony_ci``` 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ciBy default, the button is displayed in green in the normal state. When you press the Tab key for the first time, the button obtains focus and is displayed in the red style specified for **focus**. After a click event occurs and you press the Tab key again, the button obtains focus and changes to the pink style. 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci **Figure 3** Change of the styles in focused state by a click 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci 148