Skip to content

Commit 6075f6e

Browse files
Merge pull request #20494 from calixteman/simplify_sidebar_resize
Use only one resize observer in the the sidebar
2 parents 171fede + 83fa8e9 commit 6075f6e

File tree

1 file changed

+25
-45
lines changed

1 file changed

+25
-45
lines changed

web/sidebar.js

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class Sidebar {
3636

3737
#isKeyboardResizing = false;
3838

39-
#resizeObserver = null;
39+
#resizeObserver;
40+
41+
#prevX = 0;
4042

4143
/**
4244
* @typedef {Object} SidebarElements
@@ -74,6 +76,20 @@ class Sidebar {
7476
toggleButton.addEventListener("click", this.toggle.bind(this));
7577
this._isOpen = false;
7678
sidebar.hidden = true;
79+
80+
this.#resizeObserver = new ResizeObserver(
81+
([
82+
{
83+
borderBoxSize: [{ inlineSize }],
84+
},
85+
]) => {
86+
if (!isNaN(this.#prevX)) {
87+
this.#prevX += this.#coefficient * (inlineSize - this.#width);
88+
}
89+
this.#setWidth(inlineSize);
90+
}
91+
);
92+
this.#resizeObserver.observe(sidebar);
7793
}
7894

7995
#makeSidebarResizable() {
@@ -84,10 +100,9 @@ class Sidebar {
84100
this._sidebar.classList.remove("resizing");
85101
pointerMoveAC?.abort();
86102
pointerMoveAC = null;
87-
this.#resizeObserver?.disconnect();
88-
this.#resizeObserver = null;
89103
this.#isKeyboardResizing = false;
90104
this.onStopResizing();
105+
this.#prevX = NaN;
91106
};
92107
this.#resizer.addEventListener("pointerdown", e => {
93108
if (pointerMoveAC) {
@@ -97,25 +112,13 @@ class Sidebar {
97112
this.onStartResizing();
98113
const { clientX } = e;
99114
stopEvent(e);
100-
let prevX = clientX;
115+
this.#prevX = clientX;
101116
pointerMoveAC = new AbortController();
102117
const { signal } = pointerMoveAC;
103118
const sidebar = this._sidebar;
104119
sidebar.classList.add("resizing");
105120
const parentStyle = sidebar.parentElement.style;
106121
parentStyle.minWidth = 0;
107-
this.#resizeObserver?.disconnect();
108-
this.#resizeObserver = new ResizeObserver(
109-
([
110-
{
111-
borderBoxSize: [{ inlineSize }],
112-
},
113-
]) => {
114-
prevX += this.#width - inlineSize;
115-
this.#setWidth(inlineSize);
116-
}
117-
);
118-
this.#resizeObserver.observe(sidebar);
119122
window.addEventListener("contextmenu", noContextMenu, { signal });
120123
window.addEventListener(
121124
"pointermove",
@@ -124,7 +127,7 @@ class Sidebar {
124127
return;
125128
}
126129
stopEvent(ev);
127-
sidebarStyle.width = `${Math.round(this.#width + this.#coefficient * (ev.clientX - prevX))}px`;
130+
sidebarStyle.width = `${Math.round(this.#width + this.#coefficient * (ev.clientX - this.#prevX))}px`;
128131
},
129132
{ signal, capture: true }
130133
);
@@ -147,17 +150,6 @@ class Sidebar {
147150
if (!this.#isKeyboardResizing) {
148151
this._sidebar.classList.add("resizing");
149152
this.#isKeyboardResizing = true;
150-
this.#resizeObserver?.disconnect();
151-
this.#resizeObserver = new ResizeObserver(
152-
([
153-
{
154-
borderBoxSize: [{ inlineSize }],
155-
},
156-
]) => {
157-
this.#setWidth(inlineSize);
158-
}
159-
);
160-
this.#resizeObserver.observe(this._sidebar);
161153
this.onStartResizing();
162154
}
163155

@@ -193,24 +185,7 @@ class Sidebar {
193185
* @param {number} newWidth
194186
*/
195187
set width(newWidth) {
196-
if (!this.#resizeObserver) {
197-
this.#resizeObserver = new ResizeObserver(
198-
([
199-
{
200-
borderBoxSize: [{ inlineSize }],
201-
},
202-
]) => {
203-
this.#setWidth(inlineSize);
204-
}
205-
);
206-
this.#resizeObserver.observe(this._sidebar);
207-
}
208188
this._sidebar.style.width = `${newWidth}px`;
209-
clearTimeout(this.#resizeTimeout);
210-
this.#resizeTimeout = setTimeout(() => {
211-
this.#resizeObserver.disconnect();
212-
this.#resizeObserver = null;
213-
}, RESIZE_TIMEOUT);
214189
}
215190

216191
/**
@@ -236,6 +211,11 @@ class Sidebar {
236211
toggle(visibility = !this._isOpen) {
237212
this._sidebar.hidden = !(this._isOpen = visibility);
238213
}
214+
215+
destroy() {
216+
this.#resizeObserver?.disconnect();
217+
this.#resizeObserver = null;
218+
}
239219
}
240220

241221
export { Sidebar };

0 commit comments

Comments
 (0)