Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I animate grid box change with angular BrowserAnimationsModule?

To describe my issue, I will start from the roots to explain what I am trying to do, and why I decided to use Grid Box for this, let's start off with two wireframes:

My layout is built up from two containers; the body and the sidebar. Don't think of it as this is the whole website, this is just a component.

The sidebar contains two elements, notes and chat.

enter image description here

Notes & chat elements can be mini-sized, but once it is mini-sized, the second part of the left body container will get wide and take the place that the sidebar used to take at it's bottom space, like in the example below:

enter image description here

So after researching a bit I couldn't find any other solution besides having 2 different components for the second-data part that needs to get wider, or just use a Grid Box, however, I must animate the side bar and the second part of the data with a transition of it's width changing.

There is an angular POC example I have created with Grid Box to achieve what I need without animation:

https://stackblitz.com/edit/angular-ivy-7tucsx?file=src/app/app.component.html

Is it possible to achieve this animation with grid box by just adding the .closed class to my .container like in the example POC?

like image 826
Ben Beri Avatar asked Nov 07 '22 03:11

Ben Beri


People also ask

Can you animate CSS grid?

Using transitions is one way to animate your grid rows and columns. If your design adjusts the grid structure to cater to different viewports, as long as the number of rows and columns remain the same throughout, you'll see the rows and columns animate to their new sizes.

Which animation strategy would you use to apply multiple styles for a transition in Angular?

The keyframes() function in Angular allows you to specify multiple interim styles within a single transition, with an optional offset to define the point in the animation where each style change should occur.

What is the use of Animatechild () function in Angular?

Each time an animation is triggered in Angular, the parent animation has priority and any child animations are blocked. In order for a child animation to run, the parent animation must query each of the elements containing child animations, and run them using this function.


1 Answers

There is a CSS only solution that can help you.

In the snippet, hover the container to make the bottom div expand.

The trick is to use a 3 column grid, and an auxiliar element that grows / shrinks:

In production , the trick element would have an height of 0, and be invisible.

.container {
    display: grid;
    border: solid 1px red;
    grid-template-columns: 1fr auto auto;
    grid-template-rows: 100px 100px 10px;
    transition: all 3s;
    width: 500px;
}


#right {
    background-color: yellow;
    width: 200px;
    grid-column: 2 / span 2;
}

#bottom {
    background-color: lightgreen;
    grid-column: span 2;
}

#trick {
    background-color: tomato;
    grid-column: 2 / 3;
    width: 0px;
    transition: width 3s;
}

.container:hover #trick {
    width: 200px;
}
<div class="container">
    <div id="left">L</div>
    <div id="right">R</div>
    <div id="bottom">B</div>
    <div id="trick">T</div>
</div>
 
like image 165
vals Avatar answered Nov 12 '22 12:11

vals