3

Is there a way to use the same css animation for a different DOM element? I have created the same animation twice, and I was looking for a way to just write it once and make note of which DOM element I am wanting to fade in and out. Now, I am aware of View Child and ElementRef, but I am a little unclear on the implementation. Also, I am trying to look for an implementation that avoids ElementRef due to XSS security concerns. https://angular.io/docs/ts/latest/api/core/index/ElementRef-class.html

Here is my HTML

<div>
    <md-checkbox (change)="toggleFadeOne()">Show</md-checkbox>
    <div fxLayout="row" [@fadeOne]="fadeOne" class="oneToggle">
        <p>

        </p>
    </div>
</div>

<div>
    <md-checkbox (change)="toggleFadeTwo()">Show</md-checkbox>
    <div fxLayout="row" [@fadeTwo]="fadeTwo" class="twoToggle">
    <p>

    </p>
</div>

Here is my css

.oneToggle, .twoToggle {//initial style for el, instead of void
   opacity: 0;
   visibility: hidden;
}

Here is my TypeScript

@Component({
  selector : 'c-select-composite-config-dialog',
  templateUrl: './page.html',
  styleUrls: ['./style.css'],
  animations: [
  trigger('fadeOne', [
    state('in', style({
      'opacity' : '1', 'visibility' : 'visible'
    })),
    state('out', style({
      'opacity' : '0', 'visibility' : 'hidden'
    })),
    transition('* => *', animate(500))
  ]),
  trigger('fadeTwo', [
    state('in', style({
      'opacity' : '1', 'visibility' : 'visible'
    })),
    state('out', style({
      'opacity' : '0', 'visibility' : 'hidden'
    })),
    transition('* => *', animate(500))
  ])
]
})
export class MyComponent {



  private fadeOne : string;
  private fadeTwo : string;

  private toggleFadeOne() {
  if(this.fadeOne === 'out' || this.fadeOne === undefined) {
    this.fadeOne = 'in';
  } else {
    this.fadeOne = 'out'
  }
}

private toggleFadeTwo() {
  if(this.fadeTwo === 'out' || this.fadeTwo === undefined) {
    this.fadeTwo = 'in';
  } else {
    this.fadeTwo = 'out'
  }
}

...
}
0

1 Answer 1

1

I'm pretty sure you could define the anmiation elsewhere, then import it and assign it to your animation property.

Like this:

**import the animation classes**

export static class Animations {
    public sharedAnimation = trigger('fadeOne', [
    state('in', style({
      'opacity' : '1', 'visibility' : 'visible'
    })),
    state('out', style({
      'opacity' : '0', 'visibility' : 'hidden'
    })),
    transition('* => *', animate(500))
  ]),
  trigger('fadeTwo', [
    state('in', style({
      'opacity' : '1', 'visibility' : 'visible'
    })),
    state('out', style({
      'opacity' : '0', 'visibility' : 'hidden'
    })),
    transition('* => *', animate(500))
  ])
]
}

Then I think you could do:

animations: Animations.sharedAnimation
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your response @chrispy. Yes, I am familiar with defining an animation in an external file, but again, the same animation, written twice? There has to be some way to target the one animation to different DOM elements.
I just found this. stackoverflow.com/questions/42068914/… ; this might do it.
Working on a solution using ngClass and keeping the animations as keyframes in external css. Animation state could be kept much the same like I am doing here and toggling the state with a function tied to a checkbox or something.
I am sure there are multiple ways to skin the cat, but using ngClass provides an extensible way to give multiple elements access to the animation through keyframes in external css without accessing the DOM.
@TimConsolazio, how did you set the initial state on your ngClass?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.