• 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](#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.<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.|
36| Overlay | The sidebar is displayed overlaid on the content area.|
37| AUTO | The sidebar is displayed in Embed mode when the component size is greater than or equal to the sum of **minSideBarWidth** and **minContentWidth**<br>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.|
38
39## Attributes
40
41In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
42
43| Name| Type| Description|
44| -------- | -------- | -------- |
45| showSideBar | boolean | Whether to display the sidebar.<br>**true**: The sidebar is displayed.<br>**false**: The sidebar is not displayed.<br>Default value: **true**<br>Since API version 10, this attribute supports [$$](../../quick-start/arkts-two-way-sync.md) for two-way binding of variables.|
46| controlButton | [ButtonStyle](#buttonstyle) | Attributes of the sidebar control button.|
47| showControlButton | boolean | 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**|
48| sideBarWidth | number \| [Length](ts-types.md#length)<sup>9+</sup> | 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**.<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 valid value closest to the set one is used.<br>The width of the sidebar, whether it is specified or kept at the default value, takes precedence over that of the sidebar child components.|
49| minSideBarWidth | number \| [Length](ts-types.md#length)<sup>9+</sup> | 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**.<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>**minSideBarWidth**, whether it is specified or kept at the default value, takes precedence over **minWidth** of the sidebar child components.|
50| maxSideBarWidth | number \| [Length](ts-types.md#length)<sup>9+</sup> | Maximum width of the sidebar.<br>Default value: **280vp**<br>Unit: 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>**maxSideBarWidth**, whether it is specified or kept at the default value, takes precedence over **maxWidth** of the sidebar child components.|
51| autoHide<sup>9+</sup> | boolean | 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**<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).|
52| sideBarPosition<sup>9+</sup> | [SideBarPosition](#sidebarposition9) | Position of the sidebar.<br>Default value: **SideBarPosition.Start**|
53| divider<sup>10+</sup>        | [DividerStyle](#dividerstyle10) \| null | Divider style.<br>- **DividerStyle** (default): The divider is displayed.<br>- **null**: The divider is not displayed.|
54| minContentWidth<sup>10+</sup> | [Dimension](ts-types.md#dimension10) | Minimum content area width of the sidebar container.<br>Default value: **360vp**<br>Unit: vp<br>**NOTE**<br>If 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.<br>In Embed mode, when the component size is increased, only the content area is enlarged;<br>when 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<br>until its width reaches the value defined by **minSideBarWidth**; if the component size is further decreased, then:<br>- If **autoHide** is set to **false**, while respecting the **minSideBarWidth** and **minContentWidth** settings, the content area has its content clipped.<br>- 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.<br>**minContentWidth**, whether it is specified or kept at the default value, takes precedence over **minSideBarWidth** and **sideBarWidth** of the sidebar.|
55
56## ButtonStyle
57
58| Name| Type| Mandatory| Description|
59| -------- | -------- | -------- | -------- |
60| left | number | No| Spacing between the sidebar control button and the left of the container.<br>Default value: **16vp**<br>Unit: vp|
61| top | number | No| Spacing between the sidebar control button and the top of the container.<br>Default value: **48vp**<br>Unit: vp|
62| 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|
63| 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|
64| 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.|
65
66## SideBarPosition<sup>9+</sup>
67
68| Name| Description|
69| -------- | -------- |
70| Start | The sidebar is on the left side of the container.|
71| End | The sidebar is on the right side of the container.|
72
73## DividerStyle<sup>10+</sup>
74
75| Name       | Type     | Mandatory| Description                                    |
76| ----------- | ------------- | ---- | ---------------------------------------- |
77| strokeWidth | [Length](ts-types.md#length)        | Yes  | Stroke width of the divider.<br>Default value: **1vp**|
78| color       | [ResourceColor](ts-types.md#resourcecolor) | No  | Color of the divider.<br>Default value: **#000000, 3%**  |
79| startMargin | [Length](ts-types.md#length)        | No  | Distance between the divider and the top of the sidebar.<br>Default value: **0**|
80| endMargin   | [Length](ts-types.md#length)        | No  | Distance between the divider and the bottom of the sidebar.<br>Default value: **0**|
81>  **NOTE**
82>
83>  The settings of the [universal size attributes](ts-universal-attributes-size.md) **width** and **height** do not take effect for the sidebar child component.
84>  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.
85>
86>  If the **showSideBar** attribute is not set, the sidebar's visibility is subject to its size.
87>
88>  - If the size is less than the sum of **minSideBarWidth** and **minContentWidth**, the sidebar is not displayed by default.
89>  - If the size is greater than or equal to the sum of **minSideBarWidth** and **minContentWidth**, the sidebar is displayed by default.
90
91## Events
92
93In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
94
95| Name| Description|
96| -------- | -------- |
97| 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.|
98
99
100## Example
101
102```ts
103// xxx.ets
104@Entry
105@Component
106struct SideBarContainerExample {
107  normalIcon: Resource = $r("app.media.icon")
108  selectedIcon: Resource = $r("app.media.icon")
109  @State arr: number[] = [1, 2, 3]
110  @State current: number = 1
111
112  build() {
113    SideBarContainer(SideBarContainerType.Embed) {
114      Column() {
115        ForEach(this.arr, (item: number) => {
116          Column({ space: 5 }) {
117            Image(this.current === item ? this.selectedIcon : this.normalIcon).width(64).height(64)
118            Text("Index0" + item)
119              .fontSize(25)
120              .fontColor(this.current === item ? '#0A59F7' : '#999')
121              .fontFamily('source-sans-pro,cursive,sans-serif')
122          }
123          .onClick(() => {
124            this.current = item
125          })
126        }, (item: string) => item)
127      }.width('100%')
128      .justifyContent(FlexAlign.SpaceEvenly)
129      .backgroundColor('#19000000')
130
131      Column() {
132        Text('SideBarContainer content text1').fontSize(25)
133        Text('SideBarContainer content text2').fontSize(25)
134      }
135      .margin({ top: 50, left: 20, right: 30 })
136    }
137    .controlButton({
138      icons: {
139        hidden: $r('app.media.drawer'),
140        shown: $r('app.media.drawer'),
141        switching: $r('app.media.drawer')
142      }
143    })
144    .sideBarWidth(150)
145    .minSideBarWidth(50)
146    .maxSideBarWidth(300)
147    .minContentWidth(0)
148    .onChange((value: boolean) => {
149      console.info('status:' + value)
150    })
151    .divider({ strokeWidth: '1vp', color: Color.Gray, startMargin: '4vp', endMargin: '4vp' })
152  }
153}
154```
155
156![](figures/sidebarcontainer.png)
157