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

Response update card keep shaking when there is only one line of text #1981

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Changes from 3 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
@@ -1,11 +1,11 @@
import { AfterContentChecked, ChangeDetectorRef, Component, ElementRef, Input, ViewChild } from '@angular/core';
import {AfterViewInit, ChangeDetectorRef, Component, ElementRef, Input, ViewChild } from '@angular/core';

@Component({
selector: 'response-update-card',
templateUrl: './response-update-card.component.html',
styleUrls: ['./response-update-card.component.scss']
})
export class ResponseUpdateCardComponent implements AfterContentChecked {
export class ResponseUpdateCardComponent implements AfterViewInit {
@Input() updateDate?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would AfterContentInit be the lifecycle hook we want? It happens once but only once the DOM is initiated and works better for things like ng-content

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking into the lifecycles a bit more, I don't think the code would get the DOM sizes without the DOM being fully drawn, which is why AfterContentChecked is used. We likely would need to update the check instead.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to get too fancy and detect lines of text, but it might just be better to use a fixed threshold like this.

`
heightThreshold = 200;

constructor(private cdr: ChangeDetectorRef) { }

ngAfterContentChecked() {
const size = this.contentElementRef?.nativeElement?.offsetHeight;
const previousUseColumns = this.useColumns;
this.useColumns = size > this.heightThreshold;
if (this.useColumns !== previousUseColumns) {
this.cdr.detectChanges();
}
}
}

`


@ViewChild('responseUpdateContent') contentElementRef: ElementRef;
Expand All @@ -17,7 +17,7 @@ export class ResponseUpdateCardComponent implements AfterContentChecked {

constructor(private cdr: ChangeDetectorRef) { }

ngAfterContentChecked() {
ngAfterViewInit() {
const size = this.contentElementRef?.nativeElement?.offsetHeight;
this.useColumns = size > (this.columnTrigger * this.columnSize);
if (this.useColumns) {
Expand Down
Loading