ts-container-sidebarcontainer.md 7.5 KB
Newer Older
E
add doc  
ester.zhou 已提交
1 2
# SideBarContainer

E
ester.zhou 已提交
3 4
The **\<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.

5 6 7
>  **NOTE**
>
>  This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
E
add doc  
ester.zhou 已提交
8 9 10 11 12 13


## Child Components

Supported

E
ester.zhou 已提交
14 15 16 17 18 19
>  **NOTE**
>
>  - 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.
>  - This component must contain two child components.
>  - 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.

E
add doc  
ester.zhou 已提交
20 21 22 23 24

## APIs

SideBarContainer( type?: SideBarContainerType )

E
ester.zhou 已提交
25 26 27 28 29 30
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | SideBarContainerType | No| Display type of the sidebar.<br>Default value: **SideBarContainerType.Embed**|

E
ester.zhou 已提交
31
## SideBarContainerType
E
add doc  
ester.zhou 已提交
32

E
ester.zhou 已提交
33 34 35 36
| Name| Description|
| -------- | -------- |
| Embed | The sidebar is embedded in the component and displayed side by side with the content area.|
| Overlay | The sidebar is displayed overlaid on the content area.|
E
add doc  
ester.zhou 已提交
37 38 39

## Attributes

E
ester.zhou 已提交
40 41
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.

E
ester.zhou 已提交
42 43
| Name| Type| Description|
| -------- | -------- | -------- |
E
ester.zhou 已提交
44 45
| showSideBar | boolean | Whether to display the sidebar.<br>Default value: **true** |
| controlButton | [ButtonStyle](#buttonstyle)                            | Attributes of the sidebar control button.|
E
ester.zhou 已提交
46
| showControlButton | boolean | Whether to display the sidebar control button.<br>Default value: **true**|
E
ester.zhou 已提交
47 48 49 50
| 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.|
| 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.|
| 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.|
| 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).|
E
ester.zhou 已提交
51 52 53 54 55
| sideBarPosition<sup>9+</sup> | SideBarPosition | Position of the sidebar.<br>Default value: **SideBarPosition.Start**|

## ButtonStyle

| Name| Type| Mandatory| Description|
E
add doc  
ester.zhou 已提交
56
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
57 58 59 60 61
| left | number | No| Spacing between the sidebar control button and the left of the container.<br>Default value: **16**<br>Unit: vp|
| top | number | No| Spacing between the sidebar control button and the top of the container.<br>Default value: **48**<br>Unit: vp|
| width | number | No| Width of the sidebar control button.<br>Default value: **32**<br>Unit: vp|
| height | number | No| Height of the sidebar control button.<br>Default value: **32**<br>Unit: vp|
| 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.|
E
add doc  
ester.zhou 已提交
62

E
ester.zhou 已提交
63
## SideBarPosition<sup>9+</sup>
E
add doc  
ester.zhou 已提交
64

E
ester.zhou 已提交
65 66 67 68
| Name| Description|
| -------- | -------- |
| Start | The sidebar is on the left side of the container.|
| End | The sidebar is on the right side of the container.|
E
add doc  
ester.zhou 已提交
69 70 71

## Events

E
ester.zhou 已提交
72 73
In addition to the [universal events](ts-universal-events-click.md), the following events are supported.

E
add doc  
ester.zhou 已提交
74 75
| Name| Description|
| -------- | -------- |
E
ester.zhou 已提交
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.|
E
add doc  
ester.zhou 已提交
77 78 79 80


## Example

81 82
```ts
// xxx.ets
E
add doc  
ester.zhou 已提交
83 84 85
@Entry
@Component
struct SideBarContainerExample {
E
ester.zhou 已提交
86
  normalIcon: Resource = $r("app.media.icon")
E
ester.zhou 已提交
87
  selectedIcon: Resource = $r("app.media.icon")
E
add doc  
ester.zhou 已提交
88 89 90 91
  @State arr: number[] = [1, 2, 3]
  @State current: number = 1

  build() {
E
ester.zhou 已提交
92
    SideBarContainer(SideBarContainerType.Embed) {
E
add doc  
ester.zhou 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
      Column() {
        ForEach(this.arr, (item, index) => {
          Column({ space: 5 }) {
            Image(this.current === item ? this.selectedIcon : this.normalIcon).width(64).height(64)
            Text("Index0" + item)
              .fontSize(25)
              .fontColor(this.current === item ? '#0A59F7' : '#999')
              .fontFamily('source-sans-pro,cursive,sans-serif')
          }
          .onClick(() => {
            this.current = item
          })
        }, item => item)
      }.width('100%')
      .justifyContent(FlexAlign.SpaceEvenly)
      .backgroundColor('#19000000')
E
ester.zhou 已提交
109 110 111 112 113 114 115


      Column() {
        Text('SideBarContainer content text1').fontSize(25)
        Text('SideBarContainer content text2').fontSize(25)
      }
      .margin({ top: 50, left: 20, right: 30 })
E
add doc  
ester.zhou 已提交
116
    }
E
ester.zhou 已提交
117 118 119 120 121 122 123
    .controlButton({
      icons: {
        hidden: $r('app.media.drawer'),
        shown: $r('app.media.drawer'),
        switching: $r('app.media.drawer')
      }
    })
E
ester.zhou 已提交
124 125 126
    .sideBarWidth(150)
    .minSideBarWidth(50)
    .maxSideBarWidth(300)
E
add doc  
ester.zhou 已提交
127 128 129 130 131 132 133 134
    .onChange((value: boolean) => {
      console.info('status:' + value)
    })
  }
}
```

![](figures/sidebarcontainer.png)