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**Atomic service API**: This API can be used in atomic services since API version 11.
26
27**System capability**: SystemCapability.ArkUI.ArkUI.Full
28
29**Parameters**
30
31| Name| Type| Mandatory| Description|
32| -------- | -------- | -------- | -------- |
33| type | [SideBarContainerType](#sidebarcontainertype) | No| Display type of the sidebar.<br>Default value: **SideBarContainerType.Embed**|
34
35## SideBarContainerType
36
37**Atomic service API**: This API can be used in atomic services since API version 11.
38
39**System capability**: SystemCapability.ArkUI.ArkUI.Full
40
41| Name| Description|
42| -------- | -------- |
43| Embed | The sidebar is embedded in the component and displayed side by side with the content area.<br>When the component size is less than the sum of **minContentWidth** and **minSideBarWidth** and **showSideBar** is not set, the sidebar is automatically hidden.<br>If **minSideBarWidth** or **minContentWidth** is not set, the default value will be used for calculation.<br> The user can bring out the sidebar in Overlay mode by clicking the control button.|
44| Overlay | The sidebar is displayed overlaid on the content area.|
45| AUTO<sup>10+</sup> | The sidebar is displayed in Embed mode when the component size is greater than or equal to the sum of **minSideBarWidth** and **minContentWidth** and in Overlay mode otherwise.<br>If **minSideBarWidth** or **minContentWidth** is not set, the default value will be used for calculation. If the calculation result is less than 600 vp, 600 vp will be used as the breakpoint value for mode switching.|
46
47## Attributes
48
49In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
50
51### showSideBar
52
53showSideBar(value: boolean)
54
55Specifies whether to display the sidebar.
56
57Since API version 10, this attribute supports two-way binding through [$$](../../../quick-start/arkts-two-way-sync.md).
58
59**Atomic service API**: This API can be used in atomic services since API version 11.
60
61**System capability**: SystemCapability.ArkUI.ArkUI.Full
62
63**Parameters**
64
65| Name| Type   | Mandatory| Description                                                        |
66| ------ | ------- | ---- | ------------------------------------------------------------ |
67| value  | boolean | Yes  | Whether to display the sidebar.<br>**true**: The sidebar is displayed.<br>**false**: The sidebar is not displayed.<br>Default value: **true**|
68
69### controlButton
70
71controlButton(value: ButtonStyle)
72
73Sets the attributes of the sidebar control button.
74
75**Atomic service API**: This API can be used in atomic services since API version 11.
76
77**System capability**: SystemCapability.ArkUI.ArkUI.Full
78
79**Parameters**
80
81| Name| Type                               | Mandatory| Description                  |
82| ------ | ----------------------------------- | ---- | ---------------------- |
83| value  | [ButtonStyle](#buttonstyle) | Yes  | Attributes of the sidebar control button.|
84
85### showControlButton
86
87showControlButton(value: boolean)
88
89Specifies whether to display the sidebar control button.
90
91**Atomic service API**: This API can be used in atomic services since API version 11.
92
93**System capability**: SystemCapability.ArkUI.ArkUI.Full
94
95**Parameters**
96
97| Name| Type   | Mandatory| Description                                                        |
98| ------ | ------- | ---- | ------------------------------------------------------------ |
99| value  | boolean | Yes  | Whether to display the sidebar control button.<br>**true**: The sidebar control button is displayed.<br>**false**: The sidebar control button is not displayed.<br>Default value: **true**|
100
101### sideBarWidth
102
103sideBarWidth(value: number | Length)
104
105Sets the width of the sidebar. A value less than 0 evaluates to the default value. The value must comply with the width constraints. If it is not within the valid range, the valid value closest to the set one is used.
106
107**Atomic service API**: This API can be used in atomic services since API version 11.
108
109**System capability**: SystemCapability.ArkUI.ArkUI.Full
110
111**Parameters**
112
113| Name| Type                                                        | Mandatory| Description                                                        |
114| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
115| value  | number \| [Length](ts-types.md#length)<sup>9+</sup> | Yes  | Width of the sidebar.<br>Default value: **240vp**<br>Unit: vp<br>**NOTE**<br>In API version 9 and earlier versions, the default value is **200vp**. In API version 10, the default value is **240vp**.|
116
117### minSideBarWidth
118
119minSideBarWidth(value: number | Length)
120
121Sets the minimum width of the sidebar. A value less than 0 evaluates to the default value. The value cannot exceed the width of the sidebar container itself. Otherwise, the width of the sidebar container itself is used.
122
123**minSideBarWidth**, whether it is specified or kept at the default value, takes precedence over **minWidth** of the sidebar child components.
124
125**Atomic service API**: This API can be used in atomic services since API version 11.
126
127**System capability**: SystemCapability.ArkUI.ArkUI.Full
128
129**Parameters**
130
131| Name| Type                                                        | Mandatory| Description                                                        |
132| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
133| value  | number \| [Length](ts-types.md#length)<sup>9+</sup> | Yes  | Minimum width of the sidebar.<br>Default value: **240vp**<br>Unit: vp<br>**NOTE**<br>In API version 9 and earlier versions, the default value is **200vp**. In API version 10, the default value is **240vp**.|
134
135### maxSideBarWidth
136
137maxSideBarWidth(value: number | Length)
138
139Sets the maximum width of the sidebar. A value less than 0 evaluates to the default value. The value cannot exceed the width of the sidebar container itself. Otherwise, the width of the sidebar container itself is used.
140
141**maxSideBarWidth**, whether it is specified or kept at the default value, takes precedence over **maxWidth** of the sidebar child components.
142
143**Atomic service API**: This API can be used in atomic services since API version 11.
144
145**System capability**: SystemCapability.ArkUI.ArkUI.Full
146
147**Parameters**
148
149| Name| Type                                                        | Mandatory| Description                                               |
150| ------ | ------------------------------------------------------------ | ---- | --------------------------------------------------- |
151| value  | number \| [Length](ts-types.md#length)<sup>9+</sup> | Yes  | Maximum width of the sidebar.<br>Default value: **280vp**<br>Unit: vp|
152
153### autoHide<sup>9+</sup>
154
155autoHide(value: boolean)
156
157Specifies whether to automatically hide the sidebar when it is dragged to be smaller than the minimum width. The value is subject to the **minSideBarWidth** attribute method. If it is not set in **minSideBarWidth**, the default value is used.
158
159Whether the sidebar should be hidden is determined when it is being dragged. When it is dragged to be smaller than the minimum width, the damping effect is required to trigger hiding (a distance out of range).
160
161**Atomic service API**: This API can be used in atomic services since API version 11.
162
163**System capability**: SystemCapability.ArkUI.ArkUI.Full
164
165**Parameters**
166
167| Name| Type   | Mandatory| Description                                                        |
168| ------ | ------- | ---- | ------------------------------------------------------------ |
169| value  | boolean | Yes  | Whether to automatically hide the sidebar when it is dragged to be smaller than the minimum width.<br>**true**: The sidebar is automatically hidden.<br>**false**: The sidebar is not automatically hidden.<br>Default value: **true**|
170
171### sideBarPosition<sup>9+</sup>
172
173sideBarPosition(value: SideBarPosition)
174
175Sets the position of the sidebar.
176
177**Atomic service API**: This API can be used in atomic services since API version 11.
178
179**System capability**: SystemCapability.ArkUI.ArkUI.Full
180
181**Parameters**
182
183| Name| Type                                        | Mandatory| Description                                              |
184| ------ | -------------------------------------------- | ---- | -------------------------------------------------- |
185| value  | [SideBarPosition](#sidebarposition9) | Yes  | Position of the sidebar.<br>Default value: **SideBarPosition.Start**|
186
187### divider<sup>10+</sup>
188
189divider(value: DividerStyle | null)
190
191Sets the divider style.
192
193**Atomic service API**: This API can be used in atomic services since API version 11.
194
195**System capability**: SystemCapability.ArkUI.ArkUI.Full
196
197**Parameters**
198
199| Name| Type                                                     | Mandatory| Description                                                        |
200| ------ | --------------------------------------------------------- | ---- | ------------------------------------------------------------ |
201| value  | [DividerStyle](#dividerstyle10) \| null | Yes  | Divider style.<br>- **DividerStyle** (default): The divider is displayed.<br>- **null** or **undefined**: No action is taken, and the divider style remains consistent with the default.<br>**NOTE**<br>In API version 11 and earlier versions, **null** results in the divider not being displayed.|
202
203### minContentWidth<sup>10+</sup>
204
205minContentWidth(value: Dimension)
206
207Sets the minimum content area width of the sidebar container.
208
209If this attribute is set to a value less than 0, the default value **360vp** will be used. If this attribute is not set, the width of the content area can shrink to 0.
210
211In Embed mode, when the component size is increased, only the content area is enlarged;
212
213when the component size is decreased, the content area is shrunk until its width reaches the value defined by **minContentWidth**; if the component size is further decreased, while respecting the **minContentWidth** settings, the sidebar is shrunk
214
215until its width reaches the value defined by **minSideBarWidth**; if the component size is further decreased, then:
216
217- If **autoHide** is set to **false**, while respecting the **minSideBarWidth** and **minContentWidth** settings, the content area has its content clipped.
218- If **autoHide** is set to **true**, the sidebar is hidden first, and then the content area is shrunk. After its width reaches the value defined by **minContentWidth**, the content area has its content clipped.
219
220**minContentWidth**, whether it is specified or kept at the default value, takes precedence over **minSideBarWidth** and **sideBarWidth** of the sidebar.
221
222**Atomic service API**: This API can be used in atomic services since API version 11.
223
224**System capability**: SystemCapability.ArkUI.ArkUI.Full
225
226**Parameters**
227
228| Name| Type                                | Mandatory| Description                                                        |
229| ------ | ------------------------------------ | ---- | ------------------------------------------------------------ |
230| value  | [Dimension](ts-types.md#dimension10) | Yes  | Minimum content area width of the sidebar container.<br>Default value: **360vp**<br>Unit: vp|
231
232## ButtonStyle
233
234**Atomic service API**: This API can be used in atomic services since API version 11.
235
236**System capability**: SystemCapability.ArkUI.ArkUI.Full
237
238| Name| Type| Mandatory| Description|
239| -------- | -------- | -------- | -------- |
240| left | number | No| Spacing between the sidebar control button and the left of the container.<br>Default value: **16vp**<br>Unit: vp|
241| top | number | No| Spacing between the sidebar control button and the top of the container.<br>Default value: **48vp**<br>Unit: vp|
242| width | number | No| Width of the sidebar control button.<br>Default value:<br>API version 9 and earlier versions: **32vp**<br>API version 10 and later versions: **24vp**<br>Unit: vp|
243| height | number | No| Height of the sidebar control button.<br>Default value:<br>API version 9 and earlier versions: **32vp**<br>API version 10 and later versions: **24vp**<br>Unit: vp|
244| 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.|
245
246## SideBarPosition<sup>9+</sup>
247
248Enumerates the positions of the sidebar.
249
250**Atomic service API**: This API can be used in atomic services since API version 11.
251
252**System capability**: SystemCapability.ArkUI.ArkUI.Full
253
254| Name| Description|
255| -------- | -------- |
256| Start | The sidebar is on the left side of the container.|
257| End | The sidebar is on the right side of the container.|
258
259## DividerStyle<sup>10+</sup>
260
261**Atomic service API**: This API can be used in atomic services since API version 11.
262
263**System capability**: SystemCapability.ArkUI.ArkUI.Full
264
265| Name       | Type     | Mandatory| Description                                    |
266| ----------- | ------------- | ---- | ---------------------------------------- |
267| strokeWidth | [Length](ts-types.md#length)        | Yes  | Stroke width of the divider.<br>Default value: **1vp**|
268| color       | [ResourceColor](ts-types.md#resourcecolor) | No  | Color of the divider.<br>Default value: **#000000, 3%**  |
269| startMargin | [Length](ts-types.md#length)        | No  | Distance between the divider and the top of the sidebar.<br>Default value: **0**|
270| endMargin   | [Length](ts-types.md#length)        | No  | Distance between the divider and the bottom of the sidebar.<br>Default value: **0**|
271>  **NOTE**
272>
273>  The settings of the [universal size attributes](ts-universal-attributes-size.md) **width** and **height** do not take effect for the sidebar child component.
274>  The settings do not take effect for the sidebar content area either. By default, the sidebar content area takes up the remaining space of the sidebar container.
275>
276>  If the **showSideBar** attribute is not set, the sidebar's visibility is subject to its size.
277>
278>  - If the size is less than the sum of **minSideBarWidth** and **minContentWidth**, the sidebar is not displayed by default.
279>  - If the size is greater than or equal to the sum of **minSideBarWidth** and **minContentWidth**, the sidebar is displayed by default.
280
281## Events
282
283In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
284
285### onChange
286
287onChange(callback: (value: boolean) =&gt; void)
288
289Triggered when the status of the sidebar switches between shown and hidden.
290
291This event is triggered when any of the following conditions is met:
292
2931. The value of **showSideBar** changes.
294
2952. The **showSideBar** attribute adapts to behavior changes.
296
2973. The **autoHide** API is triggered when the divider is dragged.
298
299**Atomic service API**: This API can be used in atomic services since API version 11.
300
301**System capability**: SystemCapability.ArkUI.ArkUI.Full
302
303**Parameters**
304
305| Name| Type   | Mandatory| Description                         |
306| ------ | ------- | ---- | ----------------------------- |
307| value  | boolean | Yes  | The value **true** means that the sidebar is displayed, and **false** means the opposite.|
308
309
310## Example
311
312```ts
313// xxx.ets
314@Entry
315@Component
316struct SideBarContainerExample {
317  normalIcon: Resource = $r("app.media.icon")
318  selectedIcon: Resource = $r("app.media.icon")
319  @State arr: number[] = [1, 2, 3]
320  @State current: number = 1
321
322  build() {
323    SideBarContainer(SideBarContainerType.Embed) {
324      Column() {
325        ForEach(this.arr, (item: number) => {
326          Column({ space: 5 }) {
327            Image(this.current === item ? this.selectedIcon : this.normalIcon).width(64).height(64)
328            Text("Index0" + item)
329              .fontSize(25)
330              .fontColor(this.current === item ? '#0A59F7' : '#999')
331              .fontFamily('source-sans-pro,cursive,sans-serif')
332          }
333          .onClick(() => {
334            this.current = item
335          })
336        }, (item: string) => item)
337      }.width('100%')
338      .justifyContent(FlexAlign.SpaceEvenly)
339      .backgroundColor('#19000000')
340
341      Column() {
342        Text('SideBarContainer content text1').fontSize(25)
343        Text('SideBarContainer content text2').fontSize(25)
344      }
345      .margin({ top: 50, left: 20, right: 30 })
346    }
347    .controlButton({
348      icons: {
349        hidden: $r('app.media.drawer'),
350        shown: $r('app.media.drawer'),
351        switching: $r('app.media.drawer')
352      }
353    })
354    .sideBarWidth(150)
355    .minSideBarWidth(50)
356    .maxSideBarWidth(300)
357    .minContentWidth(0)
358    .onChange((value: boolean) => {
359      console.info('status:' + value)
360    })
361    .divider({ strokeWidth: '1vp', color: Color.Gray, startMargin: '4vp', endMargin: '4vp' })
362  }
363}
364```
365
366![](figures/sidebarcontainer.png)
367