1# Navigation 2 3The **\<Navigation>** component typically functions as the root container of a page and displays the page title, toolbar, and menu based on the attribute settings. 4 5> **NOTE** 6> 7> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version. 8 9 10## Child Components 11 12Supported 13 14 15## APIs 16 17Navigation() 18 19Creates a component that can automatically display the navigation bar, title, and toolbar based on the attribute settings. 20 21## Attributes 22 23| Name | Type | Description | 24| -------------- | ---------------------------------------- | ---------------------------------------- | 25| title | string \| [CustomBuilder](ts-types.md#custombuilder8)<sup>8+</sup> | Page title. | 26| subtitle | string | Subtitle of the page. | 27| menus | Array<NavigationMenuItem> \| [CustomBuilder](ts-types.md#custombuilder8)<sup>8+</sup> | Menu in the upper right corner of the page. | 28| titleMode | NavigationTitleMode | Display mode of the page title bar.<br>Default value: **NavigationTitleMode.Free**| 29| toolBar | object \| [CustomBuilder](ts-types.md#custombuilder8)<sup>8+</sup> | Content of the toolbar.<br>**items**: all items on the toolbar. | 30| hideToolBar | boolean | Whether to hide the toolbar.<br>Default value: **false**<br>**true**: Hide the toolbar.<br>**false**: Show the toolbar.| 31| hideTitleBar | boolean | Whether to hide the title bar.<br>Default value: **false** | 32| hideBackButton | boolean | Whether to hide the back button.<br>Default value: **false** | 33 34## NavigationMenuItem 35 36| Name | Type | Mandatory| Description | 37| ------ | ----------------------- | ---- | ------------------------------ | 38| value | string | Yes | Text of a menu item. | 39| icon | string | No | Icon path of a menu item.| 40| action | () => void | No | Callback invoked when a menu item is selected. | 41 42## object 43 44| Name | Type | Mandatory| Description | 45| ------ | ----------------------- | ---- | ------------------------------ | 46| value | string | Yes | Text of an option on the toolbar. | 47| icon | string | No | Icon path of an option on the toolbar.| 48| action | () => void | No | Callback invoked when an option is selected. | 49 50## NavigationTitleMode 51 52| Name | Description | 53| ---- | ---------------------------------------- | 54| Free | When the content is a scrollable component, the main title shrinks as the content scrolls down (the subtitle fades out with its size remaining unchanged) and restores when the content scrolls up to the top.| 55| Mini | The icon, main title, and subtitle are in mini mode. | 56| Full | The main title and subtitle are in full mode. | 57 58> **NOTE** 59> 60> Currently, the scrollable component can only be **\<List>**. 61 62 63## Events 64 65| Name | Description | 66| ---------------------------------------- | ---------------------------------------- | 67| onTitleModeChange(callback: (titleMode: NavigationTitleMode) => void) | Triggered when **titleMode** is set to **NavigationTitleMode.Free** and the title bar mode changes as content scrolls.| 68 69 70## Example 71 72```ts 73// Example 01 74@Entry 75@Component 76struct NavigationExample { 77 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 78 @State hideBar: boolean = true 79 80 @Builder NavigationTitle() { 81 Column() { 82 Text('title') 83 .width(80) 84 .height(60) 85 .fontColor(Color.Blue) 86 .fontSize(30) 87 } 88 .onClick(() => { 89 console.log("title") 90 }) 91 } 92 93 @Builder NavigationMenus() { 94 Row() { 95 Image('images/add.png') 96 .width(25) 97 .height(25) 98 Image('comment/more.png') 99 .width(25) 100 .height(25) 101 .margin({ left: 30 }) 102 }.width(100) 103 } 104 105 build() { 106 Column() { 107 Navigation() { 108 Search({ value: '', placeholder: "" }).width('85%').margin(26) 109 List({ space: 5, initialIndex: 0 }) { 110 ForEach(this.arr, (item) => { 111 ListItem() { 112 Text('' + item) 113 .width('90%') 114 .height(80) 115 .backgroundColor('#3366CC') 116 .borderRadius(15) 117 .fontSize(16) 118 .textAlign(TextAlign.Center) 119 }.editable(true) 120 }, item => item) 121 } 122 .listDirection(Axis.Vertical) 123 .height(300) 124 .margin({ top: 10, left: 18 }) 125 .width('100%') 126 127 Button(this.hideBar ? "tool bar" : "hide bar") 128 .onClick(() => { 129 this.hideBar = !this.hideBar 130 }) 131 .margin({ left: 135, top: 60 }) 132 } 133 .title(this.NavigationTitle) 134 .subTitle('subtitle') 135 .menus(this.NavigationMenus) 136 .titleMode(NavigationTitleMode.Free) 137 .hideTitleBar(false) 138 .hideBackButton(false) 139 .onTitleModeChange((titleModel: NavigationTitleMode) => { 140 console.log('titleMode') 141 }) 142 .toolBar({ items: [ 143 { value: 'app', icon: 'images/grid.svg', action: () => { 144 console.log("app") 145 } }, 146 { value: 'add', icon: 'images/add.svg', action: () => { 147 console.log("add") 148 } }, 149 { value: 'collect', icon: 'images/collect.svg', action: () => { 150 console.log("collect") 151 } }] }) 152 .hideToolBar(this.hideBar) 153 } 154 } 155} 156``` 157 158 159 160```ts 161// Example 02 162@Entry 163@Component 164struct ToolbarBuilderExample { 165 @State currentIndex: number = 0 166 @State Build: Array<Object> = [ 167 { 168 icon: $r('app.media.ic_public_add'), 169 icon_after: $r('app.media.ic_public_addcolor'), 170 text: 'add', 171 num: 0 172 }, 173 { 174 icon: $r('app.media.ic_public_app'), 175 icon_after: $r('app.media.ic_public_appcolor'), 176 text: 'app', 177 num: 1 178 }, 179 { 180 icon: $r('app.media.ic_public_collect'), 181 icon_after: $r('app.media.ic_public_collectcolor'), 182 text: 'collect', 183 num: 2 184 } 185 ] 186 187 @Builder NavigationToolbar() { 188 Row() { 189 ForEach(this.Build, item => { 190 Column() { 191 Image(this.currentIndex == item.num ? item.icon_after : item.icon) 192 .width(25) 193 .height(25) 194 Text(item.text) 195 .fontColor(this.currentIndex == item.num ? "#ff7500" : "#000000") 196 } 197 .onClick(() => { 198 this.currentIndex = item.num 199 }) 200 .margin({ left: 70 }) 201 }) 202 } 203 } 204 205 build() { 206 Column() { 207 Navigation() { 208 Flex() { 209 } 210 } 211 .toolBar(this.NavigationToolbar) 212 } 213 } 214} 215``` 216 217 218