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?
3 days ago in Node-js by Nidhi
• 14,600 points
35 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 2 days ago by anonymous

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer

How to programmatically send a 404 response with Express/Node?

Hello @kartik, The method is: res.sendStatus(404); Express will send a ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,840 points
3,708 views
0 votes
1 answer

How do I add a custom script to my package.json file that runs a javascript file?

run npm run script1 it works for me READ MORE

answered Jan 10, 2023 in Node-js by Harisudarsan

edited Mar 5 42,040 views
0 votes
1 answer

How to implement a writable stream?

Hello @kartik, to create a writeable stream is ...READ MORE

answered Oct 13, 2020 in Node-js by Niroj
• 82,840 points
1,307 views
0 votes
1 answer

How to handle the swiperight event to trigger custom actions in jQuery Mobile?

To handle the swiperight event and trigger ...READ MORE

answered 2 days ago in Node-js by anonymous
26 views
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 2 days ago in Node-js by anonymous
29 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 2 days ago in Node-js by anonymous
37 views
0 votes
1 answer

How to use host bindings to bind properties to the host element in a component?

In Angular (2+), the @HostBinding() decorator allows ...READ MORE

answered 2 days ago in Node-js by anonymous
36 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 2 days ago in Node-js by anonymous
28 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