Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Adrien Poly
Adrien Poly

Posted on

     

Animations with Turbolinks and Stimulus

When using CSS animations with Turbolinks, you may experience page blink during page load. We will see how we can easily change that using the life cycle events of a Stimulus controller.

Turbolinks preview

When navigating back, Turbolinks will first display a preview of the page that was stored in the local cache and then replace this preview by the server response. This give the instant page load feeling effect of Turbolinks.

This preview comes from a snapshot taken locally by Turbolinks during the previous visit. Two things must be considered :

  1. The preview has the state of the animations when you left the page.
  2. If your animations are meant to start on page load then they will most probably start when the preview page is displayed

Those are typically two sources of blink effect when using animations with Turbolinks.

The first one because the page preview displays the page with the animations already played but then resets it when the preview is replaced by the real server response.

The second because if the animations start on the page preview then once the page preview is replaced, the animation will be reset. So either the animation is played twice or partially during first load.

Solution

Lets assume a typical controller where you play an animation on an element during after page load.

exportdefaultclassextendsController{connect(){this.element.classList.add("play-animation")}}
Enter fullscreen modeExit fullscreen mode

1) Ignore preview

Adding simple helper can tell you if you are on a preview page on not

getisPreview(){returndocument.documentElement.hasAttribute("data-turbolinks-preview");}
Enter fullscreen modeExit fullscreen mode

So now duringconnect we can easily know if we are in a preview or not. If we are in a preview we shouldn't play the animation as it would most likely be interrupted/play twice

exportdefaultclassextendsController{connect(){if(!this.isPreview){this.element.classList.add('play-animation')}}getisPreview(){returndocument.documentElement.hasAttribute('data-turbolinks-preview')}}
Enter fullscreen modeExit fullscreen mode

2) Tear down before snapshot

The goal is to bring back the page to its initial state before snapshot. For this we will use thedisconnect() function that occurs right before the Turbolinks snapshot and simply remove animations that we may have played.

disconnect(){this.element.classList.remove('play-animation')}
Enter fullscreen modeExit fullscreen mode

So a typical complete solution would look like this

exportdefaultclassextendsController{connect(){if(!this.isPreview){this.element.classList.add('play-animation')}}disconnect(){this.element.classList.remove('play-animation')}getisPreview(){returndocument.documentElement.hasAttribute('data-turbolinks-preview')}}
Enter fullscreen modeExit fullscreen mode

Happy Stimulus 🦄

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
skatkov profile image
Stanislav(Stas) Katkov
holistic Ruby-not-only-Rails developer and aspiring indie developer

Hey Adrien,

Do you know that you can create Pull Request togithub.com/skatkov/awesome-stimulusjs with articles like that ? ;-)

CollapseExpand
 
adrienpoly profile image
Adrien Poly
CTO @Plume_app, doing mostly Rails and Stimulus stuff. Creator of Stimulus-Use.
  • Location
    Bordeaux France
  • Work
    Freelance
  • Joined

Yeah will do thanks for the proposal

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

CTO @Plume_app, doing mostly Rails and Stimulus stuff. Creator of Stimulus-Use.
  • Location
    Bordeaux France
  • Work
    Freelance
  • Joined

More fromAdrien Poly

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp