How to implement a directive that toggles element visibility with animations

0 votes
Can you tell me How to implement a directive that toggles element visibility with animations?
Apr 9 in Node-js by Nidhi
• 15,620 points
55 views

1 answer to this question.

0 votes

Custom Directive with Angular Animations

1. Define the Animation

import {

  trigger,

  state,

  style,

  transition,

  animate

} from '@angular/animations';

export const toggleAnimation = trigger('toggleVisibility', [

  state('visible', style({ opacity: 1, height: '*' })),

  state('hidden', style({ opacity: 0, height: '0px', overflow: 'hidden' })),

  transition('visible <=> hidden', [animate('300ms ease-in-out')])

]);

2. Create the Directive

import {

  Directive,

  HostBinding,

  Input

} from '@angular/core';

@Directive({

  selector: '[appToggleVisibility]',

  animations: [toggleAnimation]

})

export class ToggleVisibilityDirective {

  @Input('appToggleVisibility') set toggle(state: boolean) {

    this.visibilityState = state ? 'visible' : 'hidden';

  }

  @HostBinding('@toggleVisibility') visibilityState = 'visible';

}

3. Use in HTML

<div [appToggleVisibility]="isShown">

  This content will fade in and out

</div>

<button (click)="isShown = !isShown">Toggle</button>

answered Apr 10 by anonymous

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer

How to implement a pipe that calculates the age from a birthdate input?

To implement a custom pipe that calculates ...READ MORE

answered 3 days ago in Node-js by anonymous
40 views
0 votes
1 answer
0 votes
1 answer

How to implement a service that provides real-time data synchronization across tabs?

To implement real-time data synchronization across browser ...READ MORE

answered 3 days ago in Node-js by anonymous
49 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to develop a directive that restricts user input based on custom conditions?

To create an Angular directive that restricts ...READ MORE

answered Apr 10 in Node-js by anonymous
48 views
0 votes
1 answer

How to dynamically inject components into the DOM using @Component?

You need to use ComponentFactoryResolver and ViewContainerRef. ...READ MORE

answered Apr 10 in Node-js by anonymous
53 views
0 votes
1 answer

How to implement a directive that auto-saves form data periodically?

To create a directive that automatically saves ...READ MORE

answered Apr 10 in Node-js by anonymous
53 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP