0

I'm using Angular 5, although I don't know if my problem is linked to the version or Angular or to the way I coded my components.

I am creating a component that looks and behave like a table. It consists of two main parts : the toolbar and the table itself. Here is a simplified sample of my template :

<div class="list__table">

<!-- The toolbar -->
  <div class="list-toolbar__wrapper" *ngIf="hasToolbar">
      <div class="list-toolbar">
          <div class="list-toolbar__right-column">
            <div class="list-toolbar__right-column__conditional">
              <toolbar-root [settings]="getToolbarItemsA()" i18nStrings="" [svgIconsPath]="svgIconsPath()"></toolbar-root>
            </div>
            <div class="list-toolbar__right-column__action">
              <toolbar-root [settings]="getToolbarItemsB()" i18nStrings="" [svgIconsPath]="svgIconsPath()"></toolbar-root>
            </div>
          </div>
      </div>
  </div>
  <!-- end of toolbar -->

  <!-- Begining of content -->
  <div>
    <!-- Table head -->
    <div
      class="list__table__header"
      [style.padding-right]="hasScrollbar ? scrollbarWidth : 0">
      <div class="list-row list-row--head">
        <span
          class="list-checkbox list-row__checkbox"
          [ngClass]="{'list-checkbox--checked': areAllRowsSelected, 'list-checkbox--indeterminate': areSomeRowsSelected}"
          *ngIf="hasToolbar"
          (click)="toggleAllChecked()">
            <span class="list-checkbox__box"></span>
            <svg class="list-checkbox__checked-mark">
              <use xlink:href="#NotificationValidation" />
            </svg>
            <svg class="list-checkbox__indeterminate-mark">
              <use xlink:href="#IndeterminateCheckbox" />
            </svg>
          </span>
        <span
          *ngFor="let column of getVisibleColumns()"
          class="list-row__data list-row__data--{{column.size}}"
          (click)="changeSelectedColumnSortingState(column.prop)">
          <div class="list-row__data__text" [ngClass]="{'list-row__data__text--selected' : column.prop == selectedColumn}">
            {{column.name}}
          </div>
        </span>
      </div>
    </div>
    <!-- end of table head -->

    <!-- Content rows -->
    <div class="list__table__content" id="scrollableTableContent">
      <!-- Repeatable rows -->
      <div
        class="list-row"
        [ngClass]="{'list-row--selected': row.isSelected, 'list-row--selectable' : hasToolbar}"
        *ngFor="let row of visibleRows | slice:0:20; index as i"
        (click)="toggleSelectedRow(i)">
          <span
            class="list-checkbox list-row__checkbox"
            *ngIf="hasToolbar"
            [ngClass]="{'list-checkbox--checked': row.isSelected}">
            <span class="list-checkbox__box"></span>
            <svg class="list-checkbox__checked-mark">
              <use xlink:href="#NotificationValidation" />
            </svg>
          </span>
          <span
            class="list-row__data list-row__data--{{column.size}}"
            *ngFor="let column of getVisibleColumns()">
            <div class="list-row__data__text" *ngIf="row[column.prop]">{{row[column.prop]}}</div>
          </span>
      </div>
      <!-- end of repeatable rows -->
    </div>
    <!-- end of content rows -->
  </div>
  <!-- end of content -->
</div>

As you can see, I use *ngIf directive on list-toolbar__wrapper to control the presence of the toolbar in the DOM. hasToolbar is set to true in my component's class like so : public hasToolbar: boolean = true

However, regardless of hasToolbar value, the moment I use *ngIf on my template, I get some weird visual "glitches". My list-row__data on the table head are "blinking" with any action (such as hover, click) on the toolbar, themselves and my content rows. Digging deeper in the problem, I found out what was triggering this behavior, but I don't understand why it does. The toolbar-root component has itself a toolbar-item component, which finally contains my ripple-root component, and its template looks like this :

<div #ripple class="entity-header-ripple" (click)="handleClick()" (mousedown)="handleMouseDown($event)" [style.borderRadius]="getBorderRadius()" [ngClass]="{'entity-header-ripple--clicked': isClicked}">
    <div class="entity-header-ripple__circle" [style.backgroundColor]="color" [style.width.px]="getCircleDiameter()" [style.height.px]="getCircleDiameter()" [style.top.px]="getTopPosition()" [style.left.px]="getLeftPosition()" [ngStyle]="{'transformorigin': 2, '-webkit-transform-origin': 2}"></div>
</div>

When I change every bits of code with [style] or [ngStyle] that are assigned to a method (getBorderRadius(), getCircleDiameter(), getTopPosition(), getLeftPosition()), and give them static values (like '20'), there are no more blinking problems.

I need my toolbar to be conditionnal, and I would like to keep my ripple component as it is or with minor modifications. Overall, I would like to understand what causes the problem in order to solve it.

Stephe
  • 25
  • 6

1 Answers1

0

Problem with function calls in the template is they get called every time change detection runs, ie. any event. You could just use properties instead of methods, or better: Observables / BehaviorSubjects with async pipe which would handle change detection for you allowing you to use OnPush.

<div *ngIf="vars$|async as vars">
    <div [style.top.px]="vars.top"/>
</div>

initialState = {top:0, left:0, etc: 'etc'};
vars$:BehaviorSubject<any> = new BehaviorSubject(initialState);
// Send next object, keep it immutable
this.vars$.next(Object.assign({}, this.vars$.getValue(), {top:5}));
funkizer
  • 4,626
  • 1
  • 18
  • 20