• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# SideBarContainer
2
3The **\<SideBarContainer>** component contains a sidebar and content area as its child components. The sidebar is the first child component and can be shown or hidden as needed. The content area is the second child component.
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>  **NOTE**
15>
16>  - Built-in components and custom components are allowed, without support for ([if/else](../../quick-start/arkts-rendering-control-ifelse.md), [ForEach](../../quick-start/arkts-rendering-control-foreach.md), and [LazyForEach](../../quick-start/arkts-rendering-control-lazyforeach.md)) rendering control.
17>  - This component must contain two child components.
18>  - If there are three or more child components, only the first and second child components are displayed. If there is only one child component, the sidebar is displayed, and the content area is blank.
19
20
21## APIs
22
23SideBarContainer( type?: SideBarContainerType )
24
25**Parameters**
26
27| Name| Type| Mandatory| Description|
28| -------- | -------- | -------- | -------- |
29| type | SideBarContainerType | No| Display type of the sidebar.<br>Default value: **SideBarContainerType.Embed**|
30
31## SideBarContainerType
32
33| Name| Description|
34| -------- | -------- |
35| Embed | The sidebar is embedded in the component and displayed side by side with the content area.|
36| Overlay | The sidebar is displayed overlaid on the content area.|
37
38## Attributes
39
40In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
41
42| Name| Type| Description|
43| -------- | -------- | -------- |
44| showSideBar | boolean | Whether to display the sidebar.<br>Default value: **true** |
45| controlButton | [ButtonStyle](#buttonstyle)                            | Attributes of the sidebar control button.|
46| showControlButton | boolean | Whether to display the sidebar control button.<br>Default value: **true**|
47| sideBarWidth | number \| Length<sup>9+</sup> | Width of the sidebar.<br>Default value: **200**<br>Unit: vp<br>**NOTE**<br>A value less than 0 evaluates to the default value.<br>The value must comply with the width constraints. If it is not within the valid range, the value closest to the set one is used.<br>When set, the width of the sidebar takes precedence over that of the sidebar child components. If it is not set, however, the width of the sidebar child component takes precedence.|
48| minSideBarWidth | number \| Length<sup>9+</sup> | Minimum width of the sidebar.<br>Default value: **200**, in vp<br>**NOTE**<br>A value less than 0 evaluates to the default value.<br>The value cannot exceed the width of the sidebar container itself. Otherwise, the width of the sidebar container itself is used.<br>When set, the minimum width of the sidebar takes precedence over that of the sidebar child components. If it is not set, however, the minimum width of the sidebar child component takes precedence.|
49| maxSideBarWidth | number \| Length<sup>9+</sup> | Maximum width of the sidebar.<br>Default value: **280**, in vp<br>**NOTE**<br>A value less than 0 evaluates to the default value.<br>The value cannot exceed the width of the sidebar container itself. Otherwise, the width of the sidebar container itself is used.<br>When set, the maximum width of the sidebar takes precedence over that of the sidebar child components. If it is not set, however, the maximum width of the sidebar child component takes precedence.|
50| autoHide<sup>9+</sup> | boolean | Whether to automatically hide the sidebar when it is dragged to be smaller than the minimum width.<br>Default value: **true**<br>**NOTE**<br>The value is subject to the **minSideBarWidth** attribute method. If it is not set in **minSideBarWidth**, the default value is used.<br>Whether the sidebar should be hidden is determined when it is being dragged. When its width is less than the minimum width, the damping effect is required to trigger hiding (a distance out of range).|
51| sideBarPosition<sup>9+</sup> | SideBarPosition | Position of the sidebar.<br>Default value: **SideBarPosition.Start**|
52
53## ButtonStyle
54
55| Name| Type| Mandatory| Description|
56| -------- | -------- | -------- | -------- |
57| left | number | No| Spacing between the sidebar control button and the left of the container.<br>Default value: **16**<br>Unit: vp|
58| top | number | No| Spacing between the sidebar control button and the top of the container.<br>Default value: **48**<br>Unit: vp|
59| width | number | No| Width of the sidebar control button.<br>Default value: **32**<br>Unit: vp|
60| height | number | No| Height of the sidebar control button.<br>Default value: **32**<br>Unit: vp|
61| icons | {<br>shown: string \| PixelMap \| [Resource](ts-types.md) ,<br>hidden: string \| PixelMap \| [Resource](ts-types.md) ,<br>switching?: string \| PixelMap \| [Resource](ts-types.md) <br>} | No| Icons of the sidebar control button.<br> </p> - **shown**: icon of the control button when the sidebar is shown.<br>**NOTE**<br>When an error occurs during resource obtaining, the default icon is used.<br>- **hidden**: icon of the control button when the sidebar is hidden.<br>- **switching**: icon of the control button when the sidebar is switching between the shown and hidden states.|
62
63## SideBarPosition<sup>9+</sup>
64
65| Name| Description|
66| -------- | -------- |
67| Start | The sidebar is on the left side of the container.|
68| End | The sidebar is on the right side of the container.|
69
70## Events
71
72In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
73
74| Name| Description|
75| -------- | -------- |
76| onChange(callback: (value: boolean) =&gt; void) | Triggered when the status of the sidebar switches between shown and hidden. The value **true** means that the sidebar is displayed, and **false** means the opposite.<br>This event is triggered when any of the following conditions is met:<br>1. The value of **showSideBar** changes.<br>2. The **showSideBar** attribute adapts to behavior changes.<br>3. The **autoHide** API is triggered when the divider is dragged.|
77
78
79## Example
80
81```ts
82// xxx.ets
83@Entry
84@Component
85struct SideBarContainerExample {
86  normalIcon: Resource = $r("app.media.icon")
87  selectedIcon: Resource = $r("app.media.icon")
88  @State arr: number[] = [1, 2, 3]
89  @State current: number = 1
90
91  build() {
92    SideBarContainer(SideBarContainerType.Embed) {
93      Column() {
94        ForEach(this.arr, (item, index) => {
95          Column({ space: 5 }) {
96            Image(this.current === item ? this.selectedIcon : this.normalIcon).width(64).height(64)
97            Text("Index0" + item)
98              .fontSize(25)
99              .fontColor(this.current === item ? '#0A59F7' : '#999')
100              .fontFamily('source-sans-pro,cursive,sans-serif')
101          }
102          .onClick(() => {
103            this.current = item
104          })
105        }, item => item)
106      }.width('100%')
107      .justifyContent(FlexAlign.SpaceEvenly)
108      .backgroundColor('#19000000')
109
110
111      Column() {
112        Text('SideBarContainer content text1').fontSize(25)
113        Text('SideBarContainer content text2').fontSize(25)
114      }
115      .margin({ top: 50, left: 20, right: 30 })
116    }
117    .controlButton({
118      icons: {
119        hidden: $r('app.media.drawer'),
120        shown: $r('app.media.drawer'),
121        switching: $r('app.media.drawer')
122      }
123    })
124    .sideBarWidth(150)
125    .minSideBarWidth(50)
126    .maxSideBarWidth(300)
127    .onChange((value: boolean) => {
128      console.info('status:' + value)
129    })
130  }
131}
132```
133
134![](figures/sidebarcontainer.png)
135