Monday Snack: Animating display: none
When you use a control flow statement like @if or a structural directive like *ngIf, the element instantly appears and disappears from the DOM. Not always a stylish solution.
But what if you want it to enter and leave with a nice animation?
Then you can write extra CSS to hide it initially, and after a microsecond add a class to the element to start an animation to gracefully show the element and add component logic just to stall the DOM removal until the CSS transition finishes.
But there is another way. Actually, it was already in the Official Baseline since August 2024.
The css way: allow-discrete and @starting-style
To smoothly animate an element being inserted or removed from the DOM, we need two CSS pieces working together:
- @starting-style: This defines the "before" state of an element when it first renders in the DOM so it can animate into view.
- transition-behavior: allow-discrete;: This tells the browser to delay switching the display property (from block to none) until your visual transitions (like opacity or scale) have actually finished.
Clean, pure CSS
Here is how beautifully simple this is now. No complex JavaScript lifecycle hooks, animation modules, or state timers are required.
Template
CSS
Why this matters
By offloading layout animations to native CSS:
- Your TypeScript files stay focused purely on business logic and state management.
- The browser handles these transitions natively on the compositing thread.
- Leveraging baseline web standards means less reliance on framework-specific animation packages that could change over time.
In real-world production apps, maintainability is everything. Every line of JavaScript or TypeScript you write to manage UI states is a line you have to test, debug, and maintain.
Next time you are building a toggled sidebar, a modal, or a dynamic alert banner, skip the complex setups. Give @starting-style a shot.
Have fun!