Skip to content

Commit cc6e91e

Browse files
tjguptarubencarvalhoRajdeep ChandraRajdeepc
authored
fix(combobox): remove layout thrash in render (#5538)
* fix(combobox): remove layout thrash in render * perf(sp-combobox): remove layout thrash in render * chore: merge main * chore: add changeset * perf(sp-combobox): incorporate PR feedback * chore: added fallback to the resizeObserver initialization --------- Co-authored-by: Ruben Carvalho <[email protected]> Co-authored-by: Rajdeep Chandra <[email protected]> Co-authored-by: Rajdeep Chandra <[email protected]>
1 parent d5f2909 commit cc6e91e

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

.changeset/every-pigs-chew.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@spectrum-web-components/combobox': patch
3+
---
4+
5+
Replace the use of offsetWidth with a resizeObserver to avoid unecessary, performance-impacting layout reflows.

packages/combobox/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"@spectrum-web-components/picker-button": "1.7.0",
7474
"@spectrum-web-components/popover": "1.7.0",
7575
"@spectrum-web-components/progress-circle": "1.7.0",
76+
"@spectrum-web-components/reactive-controllers": "1.7.0",
7677
"@spectrum-web-components/textfield": "1.7.0"
7778
},
7879
"types": "./src/index.d.ts",

packages/combobox/src/Combobox.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ export class Combobox extends Textfield {
120120

121121
private tooltipEl?: Tooltip;
122122

123+
private resizeObserver?: ResizeObserver | undefined;
124+
125+
@state()
126+
private fieldWidth = 0;
127+
123128
public override focus(): void {
124129
this.focusElement.focus();
125130
}
@@ -459,7 +464,6 @@ export class Combobox extends Textfield {
459464
}
460465

461466
protected override render(): TemplateResult {
462-
const width = (this.input || this).offsetWidth;
463467
if (this.tooltipEl) {
464468
this.tooltipEl.disabled = this.open;
465469
}
@@ -510,7 +514,7 @@ export class Combobox extends Textfield {
510514
this.itemValue
511515
? [this.itemValue]
512516
: []}
513-
style="min-width: ${width}px;"
517+
style="min-width: ${this.fieldWidth}px;"
514518
size=${this.size}
515519
>
516520
${this.overlayOpen
@@ -572,6 +576,13 @@ export class Combobox extends Textfield {
572576
this.focused = false;
573577
}
574578
});
579+
this.resizeObserver = new ResizeObserver(
580+
(entries: ResizeObserverEntry[]) => {
581+
this.fieldWidth = entries[0].borderBoxSize[0].inlineSize;
582+
}
583+
);
584+
585+
this.resizeObserver.observe(this);
575586
}
576587

577588
private _returnItems = (): void => {
@@ -650,6 +661,8 @@ export class Combobox extends Textfield {
650661
public override disconnectedCallback(): void {
651662
this.itemObserver.disconnect();
652663
this.open = false;
664+
this.resizeObserver?.disconnect();
665+
this.resizeObserver = undefined;
653666
super.disconnectedCallback();
654667
}
655668

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6676,6 +6676,7 @@ __metadata:
66766676
"@spectrum-web-components/picker-button": "npm:1.7.0"
66776677
"@spectrum-web-components/popover": "npm:1.7.0"
66786678
"@spectrum-web-components/progress-circle": "npm:1.7.0"
6679+
"@spectrum-web-components/reactive-controllers": "npm:1.7.0"
66796680
"@spectrum-web-components/textfield": "npm:1.7.0"
66806681
languageName: unknown
66816682
linkType: soft

0 commit comments

Comments
 (0)