Skip to content

Commit d08a668

Browse files
committed
fix(splitter): fix panes width on resize of the window
1 parent b6249d9 commit d08a668

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/components/splitter/splitter.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export default class IgcSplitterComponent extends EventEmitterMixin<
7575
private _startSize = 'auto';
7676
private _endSize = 'auto';
7777
private _resizeState: SplitterResizeState | null = null;
78+
private _baseResizeObserver?: ResizeObserver;
7879

7980
private readonly _internals = addInternalsController(this, {
8081
initialARIA: {
@@ -262,6 +263,14 @@ export default class IgcSplitterComponent extends EventEmitterMixin<
262263
}
263264
}
264265

266+
public override disconnectedCallback(): void {
267+
super.disconnectedCallback();
268+
if (this._baseResizeObserver) {
269+
this._baseResizeObserver.disconnect();
270+
this._baseResizeObserver = undefined;
271+
}
272+
}
273+
265274
constructor() {
266275
super();
267276

@@ -322,6 +331,10 @@ export default class IgcSplitterComponent extends EventEmitterMixin<
322331

323332
protected override firstUpdated() {
324333
this._initPanes();
334+
this._baseResizeObserver = new ResizeObserver(() =>
335+
this._onContainerResized()
336+
);
337+
this._baseResizeObserver.observe(this._base);
325338
}
326339

327340
//#endregion
@@ -341,6 +354,27 @@ export default class IgcSplitterComponent extends EventEmitterMixin<
341354

342355
//#region Internal API
343356

357+
private _onContainerResized = () => {
358+
window.setTimeout(() => {
359+
const [startSize, endSize] = this._rectSize();
360+
const total = this.getTotalSize();
361+
if (
362+
!this._isPercentageSize('end') &&
363+
!this._isAutoSize('end') &&
364+
startSize + endSize > total
365+
) {
366+
this.endSize = `${total - startSize}px`;
367+
}
368+
if (
369+
!this._isPercentageSize('start') &&
370+
!this._isAutoSize('start') &&
371+
startSize + endSize > total
372+
) {
373+
this.startSize = `${total - endSize}px`;
374+
}
375+
}, 100);
376+
};
377+
344378
private _isPercentageSize(which: 'start' | 'end') {
345379
const targetSize = which === 'start' ? this._startSize : this._endSize;
346380
return !!targetSize && targetSize.indexOf('%') !== -1;
@@ -566,17 +600,22 @@ export default class IgcSplitterComponent extends EventEmitterMixin<
566600
const minProp = isHorizontal ? 'minWidth' : 'minHeight';
567601
const maxProp = isHorizontal ? 'maxWidth' : 'maxHeight';
568602

569-
const sizes = {
603+
const startSizes = {
570604
[minProp]: this.startMinSize ?? 0,
571605
[maxProp]: this.startMaxSize ?? '100%',
572606
};
573607

608+
const endSizes = {
609+
[minProp]: this.endMinSize ?? 0,
610+
[maxProp]: this.endMaxSize ?? '100%',
611+
};
612+
574613
Object.assign(this._startPaneInternalStyles, {
575-
...sizes,
614+
...startSizes,
576615
flex: this._startFlex,
577616
});
578617
Object.assign(this._endPaneInternalStyles, {
579-
...sizes,
618+
...endSizes,
580619
flex: this._endFlex,
581620
});
582621
this.requestUpdate();

0 commit comments

Comments
 (0)