Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(database): avoid filter bar flickering #8562

Merged
merged 6 commits into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ export class Overflow extends SignalWatcher(WithDisposable(ShadowlessElement)) {
}
`;

protected frameId: number | undefined = undefined;

protected widthList: number[] = [];

adjustStyle() {
let maxWidth =
this.getBoundingClientRect().width -
this.more.getBoundingClientRect().width;
for (let i = 0; i < this.items.length; i++) {
const width = this.items[i].getBoundingClientRect().width;
maxWidth -= width;
if (maxWidth < 0) {
this.renderCount = i;
return;
}
if (this.frameId) {
cancelAnimationFrame(this.frameId);
}
this.renderCount = this.items.length;

this.frameId = requestAnimationFrame(() => {
this.doAdjustStyle();
});
}

override connectedCallback() {
Expand All @@ -49,6 +48,28 @@ export class Overflow extends SignalWatcher(WithDisposable(ShadowlessElement)) {
});
}

protected doAdjustStyle() {
const moreWidth = this.more.getBoundingClientRect().width;
this.widthList[this.renderCount] = moreWidth;

const containerWidth = this.getBoundingClientRect().width;

let width = 0;
for (let i = 0; i < this.items.length; i++) {
const itemWidth = this.items[i].getBoundingClientRect().width;
// Try to calculate the width occupied by rendering n+1 items;
// if it exceeds the limit, render n items(in i++ round).
const totalWidth =
width + itemWidth + (this.widthList[i + 1] ?? moreWidth);
if (totalWidth > containerWidth) {
this.renderCount = i;
return;
}
width += itemWidth;
}
this.renderCount = this.items.length;
}

override render() {
return html`
${repeat(this.renderItem, (render, index) => {
Expand All @@ -66,9 +87,7 @@ export class Overflow extends SignalWatcher(WithDisposable(ShadowlessElement)) {

protected override updated(_changedProperties: PropertyValues) {
super.updated(_changedProperties);
requestAnimationFrame(() => {
this.adjustStyle();
});
this.adjustStyle();
}

@queryAll(':scope > .component-overflow-item')
Expand Down
Loading