@@ -58,8 +58,9 @@ class SbbSidebarElement extends SbbOpenCloseBaseElement {
58
58
59
59
/** Returns the SbbSidebarContainerElement where this sidebar is contained. */
60
60
public get container ( ) : SbbSidebarContainerElement | null {
61
- return this . closest ?. ( 'sbb-sidebar-container' ) ;
61
+ return this . _container ;
62
62
}
63
+ private _container : SbbSidebarContainerElement | null = null ;
63
64
64
65
private _language = new SbbLanguageController ( this ) ;
65
66
@@ -73,12 +74,14 @@ class SbbSidebarElement extends SbbOpenCloseBaseElement {
73
74
74
75
public override connectedCallback ( ) : void {
75
76
super . connectedCallback ( ) ;
77
+ this . _container = this . closest ?.( 'sbb-sidebar-container' ) ;
76
78
this . _updateSidebarWidth ( ) ;
77
79
}
78
80
79
81
public override disconnectedCallback ( ) : void {
80
82
super . disconnectedCallback ( ) ;
81
- this . container ?. style . removeProperty ( `--sbb-sidebar-container-${ this . position } -width` ) ;
83
+ this . container ?. style . removeProperty ( this . _buildCssWidthVar ( ) ) ;
84
+ this . _container = null ;
82
85
}
83
86
84
87
protected override willUpdate ( changedProperties : PropertyValues < this> ) : void {
@@ -93,31 +96,38 @@ class SbbSidebarElement extends SbbOpenCloseBaseElement {
93
96
}
94
97
}
95
98
96
- protected override firstUpdated ( changedProperties : PropertyValues < this> ) {
99
+ protected override firstUpdated ( changedProperties : PropertyValues < this> ) : void {
97
100
super . firstUpdated ( changedProperties ) ;
98
101
99
102
this . _updateSidebarWidth ( ) ;
100
103
}
101
104
102
105
/** Opens the sidebar. */
103
- public open ( ) : void {
106
+ public async open ( ) : Promise < void > {
104
107
if ( this . state !== 'closed' || ! this . willOpen . emit ( ) ) {
105
108
return ;
106
109
}
107
110
108
111
const isZeroAnimationDuration = this . _isZeroAnimationDuration ( ) ;
112
+ const isDuringInitialization = ! this . hasUpdated ;
109
113
110
- if ( ! this . hasUpdated || isZeroAnimationDuration ) {
114
+ if ( isDuringInitialization || isZeroAnimationDuration ) {
111
115
this . toggleAttribute ( 'data-skip-animation' , true ) ;
112
116
} else {
113
117
this . state = 'opening' ;
114
118
}
115
119
120
+ if ( isDuringInitialization ) {
121
+ // We have to wait for the first update to be completed
122
+ // in order to have the size of the sidebar ready for the animation.
123
+ await this . updateComplete ;
124
+ }
125
+
116
126
this . opened = true ;
117
127
118
128
// If the animation duration is zero, the animationend event is not always fired reliably.
119
129
// In this case we directly set the `opened` state.
120
- if ( ! this . hasUpdated || isZeroAnimationDuration ) {
130
+ if ( isDuringInitialization || isZeroAnimationDuration ) {
121
131
this . _handleOpening ( ) ;
122
132
}
123
133
}
@@ -183,7 +193,7 @@ class SbbSidebarElement extends SbbOpenCloseBaseElement {
183
193
}
184
194
185
195
if ( oldPosition ) {
186
- container . style . removeProperty ( `--sbb-sidebar-container- ${ oldPosition } -width` ) ;
196
+ container . style . removeProperty ( this . _buildCssWidthVar ( oldPosition ) ) ;
187
197
}
188
198
189
199
const width = this . offsetWidth ?? 0 ;
@@ -192,15 +202,17 @@ class SbbSidebarElement extends SbbOpenCloseBaseElement {
192
202
}
193
203
194
204
const newValue = `${ width } px` ;
195
- const actualValue = getComputedStyle ( container ) . getPropertyValue (
196
- `--sbb-sidebar-container-${ this . position } -width` ,
197
- ) ;
205
+ const actualValue = container . style . getPropertyValue ( this . _buildCssWidthVar ( ) ) ;
198
206
199
207
if ( actualValue === newValue ) {
200
208
return ;
201
209
}
202
210
203
- container . style . setProperty ( `--sbb-sidebar-container-${ this . position } -width` , newValue ) ;
211
+ container . style . setProperty ( this . _buildCssWidthVar ( ) , newValue ) ;
212
+ }
213
+
214
+ private _buildCssWidthVar ( position = this . position ) : string {
215
+ return `--sbb-sidebar-container-${ position } -width` ;
204
216
}
205
217
206
218
private _onAnimationEnd ( event : AnimationEvent ) : void {
0 commit comments