Skip to content

Commit 2835f22

Browse files
committed
fix: stabilize
1 parent 428eff8 commit 2835f22

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/elements/sidebar/sidebar/sidebar.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ class SbbSidebarElement extends SbbOpenCloseBaseElement {
5858

5959
/** Returns the SbbSidebarContainerElement where this sidebar is contained. */
6060
public get container(): SbbSidebarContainerElement | null {
61-
return this.closest?.('sbb-sidebar-container');
61+
return this._container;
6262
}
63+
private _container: SbbSidebarContainerElement | null = null;
6364

6465
private _language = new SbbLanguageController(this);
6566

@@ -73,12 +74,14 @@ class SbbSidebarElement extends SbbOpenCloseBaseElement {
7374

7475
public override connectedCallback(): void {
7576
super.connectedCallback();
77+
this._container = this.closest?.('sbb-sidebar-container');
7678
this._updateSidebarWidth();
7779
}
7880

7981
public override disconnectedCallback(): void {
8082
super.disconnectedCallback();
81-
this.container?.style.removeProperty(`--sbb-sidebar-container-${this.position}-width`);
83+
this.container?.style.removeProperty(this._buildCssWidthVar());
84+
this._container = null;
8285
}
8386

8487
protected override willUpdate(changedProperties: PropertyValues<this>): void {
@@ -93,31 +96,38 @@ class SbbSidebarElement extends SbbOpenCloseBaseElement {
9396
}
9497
}
9598

96-
protected override firstUpdated(changedProperties: PropertyValues<this>) {
99+
protected override firstUpdated(changedProperties: PropertyValues<this>): void {
97100
super.firstUpdated(changedProperties);
98101

99102
this._updateSidebarWidth();
100103
}
101104

102105
/** Opens the sidebar. */
103-
public open(): void {
106+
public async open(): Promise<void> {
104107
if (this.state !== 'closed' || !this.willOpen.emit()) {
105108
return;
106109
}
107110

108111
const isZeroAnimationDuration = this._isZeroAnimationDuration();
112+
const isDuringInitialization = !this.hasUpdated;
109113

110-
if (!this.hasUpdated || isZeroAnimationDuration) {
114+
if (isDuringInitialization || isZeroAnimationDuration) {
111115
this.toggleAttribute('data-skip-animation', true);
112116
} else {
113117
this.state = 'opening';
114118
}
115119

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+
116126
this.opened = true;
117127

118128
// If the animation duration is zero, the animationend event is not always fired reliably.
119129
// In this case we directly set the `opened` state.
120-
if (!this.hasUpdated || isZeroAnimationDuration) {
130+
if (isDuringInitialization || isZeroAnimationDuration) {
121131
this._handleOpening();
122132
}
123133
}
@@ -183,7 +193,7 @@ class SbbSidebarElement extends SbbOpenCloseBaseElement {
183193
}
184194

185195
if (oldPosition) {
186-
container.style.removeProperty(`--sbb-sidebar-container-${oldPosition}-width`);
196+
container.style.removeProperty(this._buildCssWidthVar(oldPosition));
187197
}
188198

189199
const width = this.offsetWidth ?? 0;
@@ -192,15 +202,17 @@ class SbbSidebarElement extends SbbOpenCloseBaseElement {
192202
}
193203

194204
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());
198206

199207
if (actualValue === newValue) {
200208
return;
201209
}
202210

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`;
204216
}
205217

206218
private _onAnimationEnd(event: AnimationEvent): void {

0 commit comments

Comments
 (0)