02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous Video
Chapter is concluded.Next is

Leaving Animations with ReactCSSTransitionGroup and Sass Nesting

In this final tutorial, we’ll explore the "leave" animation type in ReactCSSTransitionGroup and refine our styling approach using Sass nesting.


1. Understanding the "Leave" Animation

The "leave" animation handles transitions for components that are removed from the DOM. To define a leave animation:

  1. Add the transitionLeaveTimeout to specify the duration of the animation.
  2. Update the CSS to handle the exit styles.

2. Implementing a Reset Button

Add functionality to reset the animation state:

onReset = () => {
    this.setState({
        clicks: 0,
        labels: []
    });
};

<Button onClick={this.onReset}>Reset</Button>

3. Adding Leave Animations in CSS

Define the leave animation styles:

.example-leave {
    opacity: 1;
    transform: translateX(0);
}
.example-leave-active {
    opacity: 0;
    transform: translateX(-100%);
    transition: opacity 500ms ease-out, transform 500ms ease-out;
}

4. Sass Nesting for Cleaner Styles

Utilize Sass nesting to simplify CSS management:

.example {
    &-leave {
        opacity: 1;
        transform: translateX(0);
    }
    &-leave-active {
        opacity: 0;
        transform: translateX(-100%);
        transition: opacity 500ms ease-out, transform 500ms ease-out;
    }
}

Tip: Use the & symbol to reference the parent selector and avoid redundant code.


5. Optimizing Workflow with Sass

  • Nest rules for hierarchical organization.
  • Use variables and mixins for consistent styling.
  • Compile Sass to CSS for browser compatibility.

Key Takeaways

  1. Leave Animation: Create smooth transitions for elements leaving the DOM.
  2. Reset State: Add a reset button to manage component state dynamically.
  3. Sass Nesting: Simplify and optimize your styling process with nested rules.

Congratulations on completing the course! Apply your skills to create unique React components and share them with the community. Keep exploring and mastering new tools to enhance your frontend development journey.

Video Transcript

00:00It's time for us to wrap up this title, but
00:03before we do that, there's one more animation type
00:05that we want to explore with you,
00:06which is when elements are leaving a component.
00:09And also we're gonna talk a little bit more about SAS
00:11nesting as it's gonna be perfectly aligned to the topic
00:14that we're gonna be working on.
00:16With that said, let's jump in
00:17and let's see the last react CSS transition group animation
00:21of type of leave as we nest a few sassy stuff.
00:26Alright, let's continue.
00:28And Otto add our transition out
00:30or as it's called in the lingo
00:32of this component is the leave timeout.
00:34We already set that time.
00:36How about we'll make it even shorter
00:38and I'm just gonna o to creator some sort of a logic.
00:41So I'm gonna grab one button
00:42that we don't have a hit state on it yet.
00:45How about our discover stuff?
00:47And I'm gonna change that from discover stuff into something
00:50less discoverable such as reset.
00:55So for our reset, I'm gonna o create your on
00:57click on on clock.
01:00And on our on click, I'm gonna just go ahead
01:02and call this on reset and I'm gonna go ahead
01:06and create that on reset method.
01:09And I'm gonna just go ahead
01:10and just start off by making sure that it's bounded
01:12to the same type of logic where it's, this will remain
01:16the same no matter that we're sending it around.
01:19And let's go ahead and also create that method itself.
01:22It has an event, we're really not gonna actually pay
01:24attention to the event, but
01:25what we're actually gonna do is we're gonna set our state
01:28and basically reset our click state to be at zero
01:32and we're gonna reset our labels
01:34to be completely empty as well.
01:36Okay, now, now
01:37that we have this really basic construct created really
01:40quickly, let's go ahead
01:41and also add the CSS And I'm just gonna go ahead
01:44and copy the CSS that we have for our enter
01:46and let's turn that into a leave
01:50and we'll have your Aleve as well.
01:53Now before we even test things out, I'm just gonna make sure
01:55that our opacity will start at zero
01:58and that uh, well all the way around that it will start
02:01with one and that we'll actually reach zero at the end.
02:05Now if I go ahead and save this
02:06and I go back into my, we should now be able to add items
02:11and they're gonna fade in
02:12and it's gonna take them quite a while to fade in.
02:14Let me also double check that I set the speedier not
02:16to five seconds but 50 milliseconds.
02:19So we want to kind of compare to be leaving
02:22to be 50 milliseconds.
02:23It's gonna be a very quick transition out.
02:25And let's go ahead and do this one more time.
02:27So as we're adding the things in,
02:29it's gonna take them five seconds to actually go
02:31through the whole process of animating in.
02:33But anytime we click on the rest,
02:36we'll we'll fix that to be reset.
02:37They should animate out immensely quickly.
02:39Well that was probably too quick.
02:41So let's set that to be 500.
02:43Set that to be 500 and let's do it ease out
02:46'cause it's kind of like the opposite of all the rest.
02:49And last but not least, um, now that we set, oh,
02:52I've been playing around with the values up here,
02:54but I actually wanted to play around
02:55with the values down here.
02:57So that's good and that should do the tricks.
03:01So if we go ahead and give it another shot,
03:03now we sh when we're adding items in still,
03:05they're gonna take a while to be added in
03:08but when we are actually animating them out,
03:10they should actually animate out very quickly now
03:12because the animation was going on at the same time.
03:14So it was a little bit weird
03:16because the item got to zero quicker
03:18and it was removed off of our screen.
03:21Now with that said, I'm gonna also show you a bit about SaaS
03:26before we wrap things up.
03:28By the way, in the formal documentation, the way
03:31that we want to actually approach our items is actually set
03:34them with this full construct.
03:37It's not really mandatory
03:38but it's definitely the way
03:39that officially things are written in.
03:42We haven't really done that
03:43because it's just overkill to type this much.
03:46And if we take a peek at our application now everything will
03:48work the exact way it was working previously only Now it's
03:52more officially done in a cleaner,
03:54but this is a lot of code.
03:56Now one way to prevent this type of like overhead typing
04:00and if I wanna make sure that this will output this way
04:02as well, first of all I wanna show you what nestings are in
04:07in this world now in sas.
04:09Basically if I cut this out
04:10and I literally put it inside as a child, that is all I had
04:14to do to create basically a bind between the two.
04:17But what would happen in this scenario is our item itself
04:20would be converted using SaaS.
04:22Oh sorry, there needs to be a.here.
04:24Our item would be converting using SaaS with a space here
04:27and then create that item.
04:28So actually if we take a look and just trust me
04:31or take a look at your browser, you'll see
04:32that it's gonna actually output as this.
04:35But in our case, what we really want
04:37to happen is we don't want to have any space.
04:39We want it to be without that space.
04:41And to do that we just have to add here and end.
04:43Once we add in that end, basically what's gonna happen,
04:46it's gonna take that name that was above
04:48and then add that without the space.
04:50Now this is gonna, could cut us a lot of our typing
04:53and if you don't care just as much
04:54as I don't care about making sure
04:56that you're gonna have the fully qualified name
04:59but a shorter name.
05:00Another option for you to do is actually concatenate
05:04that naming structure literally by just putting here an end
05:07and then just passing through without that enter again into
05:10that active state as well.
05:12And this is a much cleaner way for me to code, I believe.
05:16And I don't really care to check if it's a double
05:18because if it is already the app appear active,
05:21it's probably one of those app appear items.
05:22It seems overkill to add the both into each other.
05:27Last but not least, let's go ahead
05:28and do this oso for this item.
05:30So again, I'm just gonna go ahead
05:31and copy whatever was there and I'm gonna cut that out.
05:35And if I've done that,
05:36we will have ourselves a much more beautifully constructed
05:41CSS file that is automatically generated with less code.
05:44Now there's a lot more that you could do at SaaS,
05:46but with that we are basically done with this title.
05:50So our fade in should continue working,
05:52adding items should continue working as well
05:55with this long transition that
05:56Takes it five seconds from start all the way to finish.
06:00And our reset is gonna fade out relatively quickly.
06:03And there you go. That is basically
06:07how you create animations and react
06:09and you've even learned a lot more SaaS in the process.
06:13In this lecture we really wrapped everything up
06:15and I wanna congratulate you because we are done.
06:18Um, I'm just really impressed with you
06:21that you've got all the way through here.
06:22And now if you're wondering what should you do next,
06:24there are so many bootstrap components
06:27and I encourage you to start building your
06:29own bootstrap components.
06:30Make it your own. Don't work with a third party library.
06:32Really build out and have your unique library.
06:35You could even share it online.
06:37You could start off with the files we've created together
06:40and upload it and share it.
06:42And I would love to see it.
06:43And with that said, you could email me what your work is,
06:46would love to see it and share it also with other students.
06:48And you could reach me at ben@zerotogeek.com.
06:50If you wanna follow me on Twitter, you could,
06:53although I am non-responsive there
06:55'cause I kind of figured
06:56that I just don't like being on Twitter.
06:58Sorry. With that said, I hope you had a great time.
07:02Please review, comment, and share.
07:04And I'm assuming if you got all the way to this last lecture
07:06that you got something out of this title.
07:09And if you did, please share with others
07:11and tell them what you've got out of it
07:12and who you think should take it.
07:14And if you really, really enjoyed it, then make sure
07:16to search my name, Ben Fowler on the current platform
07:19that you're on, to find dozens of other titles of mine
07:22that could work perfectly with this title.
07:24Thanks again and see you.
07:26Until next time, well, until next time, I'll see you.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!