Skip to content

Conversation

DmitiryPotychkin
Copy link
Contributor

Add description component, which truncate long text and display buttons "show more" or "show less"

@codecov
Copy link

codecov bot commented Jan 29, 2021

Codecov Report

Merging #543 (d0db5bf) into main (6175c39) will increase coverage by 0.01%.
The diff coverage is 96.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #543      +/-   ##
==========================================
+ Coverage   85.36%   85.38%   +0.01%     
==========================================
  Files         763      765       +2     
  Lines       15674    15702      +28     
  Branches     1991     1992       +1     
==========================================
+ Hits        13380    13407      +27     
- Misses       2261     2262       +1     
  Partials       33       33              
Impacted Files Coverage Δ
...omponents/src/description/description.component.ts 95.23% <95.23%> (ø)
...s/components/src/description/description.module.ts 100.00% <100.00%> (ø)
projects/components/src/public-api.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6175c39...d0db5bf. Read the comment docs.

@@ -0,0 +1,23 @@
:host {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this doable without :host? we use it in some places, but it's a bad practice and we should try to get rid of it where possible (since the parent is generally responsible for styling the host, and will have greater precedence here)

Copy link
Contributor Author

@DmitiryPotychkin DmitiryPotychkin Jan 29, 2021

Choose a reason for hiding this comment

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

I'm try to do it without :host but for some reason style min-width: 0 for .description not affected parent component. Originally this min-width: 0 added for fix such case as on screenshots.

Without min-width: 0 for host
Screenshot from 2021-01-29 03-04-54
Add min-width: 0
Screenshot from 2021-01-29 03-04-43

anyway, I still work on this component and will try to figure way to fix this without using :host.

Copy link
Contributor

Choose a reason for hiding this comment

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

There's usually alternatives, but always the easiest to find. Don't spend too much time on it - if we need to use :host so be it.

</div>
<div
class="description-button"
*ngIf="eventDescription.offsetWidth < eventDescription.scrollWidth && !this.isDescriptionTextToggled"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also I found this ngIf not recalculate on window resize, I tried to use @ViewChild decorator and several other ways but ref always return undefined, so it's another one issue I working on

Copy link
Contributor

Choose a reason for hiding this comment

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

One option here is to use out layout change directive, which should give you a callback when the layout changes - either from a window resize, or a shift in other components. An example of this can be seen in LabelComponent

}

ngAfterViewInit() {
this.fullDescriptionTextWidth = this.eventDescriptionText.nativeElement.scrollWidth;
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be reassigned in ngOnChanges (or easier, just move it into remeasure and don't persist it) as the description text is based on an input and can change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, so now method look this:

// Called in ngOnChanges
  public remeasure(): void {  
    // This will add class for truncate text for get full width of text (without text wrapping)
    this.isDescriptionTextToggled = false;   
    this.cdRef.detectChanges();

   // Get width of parent element and text full width without text wrapping, compare it
    this.isDescriptionTruncated = 
      this.eventDescriptionContainer.nativeElement.offsetWidth < this.eventDescriptionText.nativeElement.scrollWidth;
    this.cdRef.detectChanges();
  }

}
}

ngAfterViewInit() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing visibility/type declarations

export class DescriptionComponent implements OnChanges, AfterViewInit {
public isInitialized: boolean = false;
public isDescriptionTextToggled: boolean = false;
public readonly isDescriptionTruncated$: Subject<boolean> = new Subject();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there an advantage of using an async boolean here (vs a simple boolean that is reassigned)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original problem was *ngIf don't re-rendered after the change condition, so I decide to use this method. But now I'm use ChangeDetectorRef for this purpose


this.isDescriptionTruncated =
this.eventDescriptionContainer.nativeElement.offsetWidth < this.eventDescriptionText.nativeElement.scrollWidth;
this.cdRef.detectChanges();
Copy link
Contributor

Choose a reason for hiding this comment

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

Only run once, at the end of the remeasure method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will cause bug, value of isDescriptionTruncated will be wrong as we try to get this.eventDescriptionText.nativeElement.scrollWidth before it truncated and DOM changed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm - that's one of the weird parts about using detectChanges explicitly instead of markForCheck (which just invalidates this node in the change detection tree to be picked up by angular's general change detection, which should ensure that the changes settle. Changing that here may be better - could you try that out, and if it's not working we'll go with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I try to use markForCheck method but unfortunately it not work correctly

@DmitiryPotychkin DmitiryPotychkin marked this pull request as ready for review February 2, 2021 11:01
@DmitiryPotychkin DmitiryPotychkin requested a review from a team as a code owner February 2, 2021 11:01
@github-actions

This comment has been minimized.

@anandtiwary anandtiwary merged commit 7aed80d into hypertrace:main Feb 4, 2021
@anandtiwary
Copy link
Contributor

@DmitiryPotychkin we should now use this component at the two locations mentioned in the other PR.

@github-actions
Copy link

github-actions bot commented Feb 4, 2021

Unit Test Results

    4 files  ±0  234 suites  +1   13m 10s ⏱️ - 2m 24s
833 tests +2  833 ✔️ +2  0 💤 ±0  0 ❌ ±0 
837 runs  +2  837 ✔️ +2  0 💤 ±0  0 ❌ ±0 

Results for commit 7aed80d. ± Comparison against base commit 6175c39.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants