nuxt icon indicating copy to clipboard operation
nuxt copied to clipboard

Layout transitions (layoutTransition) javascript hooks

Open crutch12 opened this issue 6 years ago â€ĸ 16 comments

What problem does this feature solve?

When I change route page and new page has another layout, I need vue transition hooks, to detect and handle it (in my case - I update store state in beforeLeave/afterEnter hooks) Vue transition hooks: https://vuejs.org/v2/api/#transition

I tried write transition/layoutTransition inside layouts, but it doesn't work. UPD: tried in nuxt.config - same result

layoutTransition: {
  beforeEnter: function () {
    debugger
  }
}

Nuxt version: 2.8.1

What does the proposed changes look like?

Same what you did here: https://github.com/nuxt/nuxt.js/issues/34 But for layouts

This feature request is available on Nuxt community (#c9453)

crutch12 avatar Jul 03 '19 20:07 crutch12

Anybody? :D

crutch12 avatar Jul 15 '19 22:07 crutch12

@crutch12 I'll take a look soon.

galvez avatar Jul 15 '19 22:07 galvez

Still waiting, thx

crutch12 avatar Dec 07 '19 13:12 crutch12

+, it's very important for me too

Sociopacific avatar Dec 08 '19 12:12 Sociopacific

+1 Would be much appreciated

sguillema avatar Dec 30 '19 10:12 sguillema

More specifically, looks like transition is serialized to the template here: https://github.com/nuxt/nuxt.js/blob/0cb2df73a026f4a26fb32cbda33377e8debf4a2f/packages/vue-app/template/index.js#L61

But layout transition is done differently here: https://github.com/nuxt/nuxt.js/blob/0cb2df73a026f4a26fb32cbda33377e8debf4a2f/packages/vue-app/template/App.js#L58

NickBolles avatar Jan 01 '20 17:01 NickBolles

Anybody? :D [2] A year has passed

crutch12 avatar Jul 28 '20 15:07 crutch12

I tried to use function (like pageTransition) in layoutTransition and it didn't work.

SasanFarrokh avatar Aug 15 '20 22:08 SasanFarrokh

As stated in the official documentation, this works as expected.

// nuxt.config.js
{
  pageTransition: {
    name: 'page',
    mode: 'out-in',
    beforeEnter (el) {
      console.log('This works...');
    }
  }
}

But if you try that for a layout, it won't work.

// nuxt.config.js
{
   layoutTransition: {
    name: 'layout',
    mode: 'out-in',
    beforeEnter (el) {
      console.log('This doesnt work...');
    }
}

Layout transition only work with css, at the moment.

// nuxt.config.js
{
   layoutTransition: {
    name: 'layout',
    mode: 'out-in'
  }
}
/* global css */

.layout-enter-active,
.layout-leave-active {
  transition: opacity 0.5s;
}
.layout-enter,
.layout-leave-active {
  opacity: 0;
}

Nuxt version: 2.14.5

ivodolenc avatar Sep 19 '20 16:09 ivodolenc

Any updates on this? In my case i want the page transition hooks in layouts. Because i want the same transition (and other things) when i navigate from the error layout to default layout. Thanks. 🙏

marcoribeiroweb avatar Sep 21 '20 16:09 marcoribeiroweb

I need this too, same case as @marcoribeiroweb Any updates yet?

rutgerbakker95 avatar Oct 01 '20 11:10 rutgerbakker95

@galvez We would appreciate if you could give us some feedback, can we expect a fix soon? 😃

ivodolenc avatar Nov 16 '20 19:11 ivodolenc

Is this still relevant? Can you please answer whether this will be implemented or maybe you have some workaround? This would give us more advanced control over transitions (for example, adding complex transitions with gsap or animejs).

ivodolenc avatar May 27 '21 20:05 ivodolenc

Any news on this? Another year passed and it's not even mentioned in documentation.

Herbata-Sys avatar Oct 14 '21 18:10 Herbata-Sys

hello?!?! â˜šī¸

zangab avatar Feb 18 '22 15:02 zangab

In case anyone is interested in my - kind of - ugly quickfix: I added a container to the app.html that's only purpose is to get in/out of the view on a route change (enter from bottom / exit to top). as there are no js transition hooks for layouts I used the router beforeEach and afterEach guards to kinda rebuild the default transition. the hooks are added via a plugin. in my case, I only use 1 transition for both, pages & layouts. with extensions like vue-meta-router this could also be kind of extended.

<!-- app.html -->
<!DOCTYPE html>
<html {{ HTML_ATTRS }}>
  <head {{ HEAD_ATTRS }}>
    {{ HEAD }}
  </head>
  <body {{ BODY_ATTRS }}>
    {{ APP }}
    <!-- container for transition -->
    <div id="transition"></div>
  </body>
</html>
// transition.client.js
let transitionEl = null

export default function ({ app }) {
  if (!transitionEl) transitionEl = document.getElementById('transition')

  app.router.beforeEach((to, from, next) => {
    transitionEl.classList.add('is-active', 'in')
    next()
  })

  app.router.afterEach((to, from) => {
    // check if active
    if (transitionEl.classList.contains('is-active')) {
       // so this is the ugly part here with 2 timeouts to await the transition.
       // could also be solved via an animation tbh, but this kinda replicates the `done` function
      setTimeout(() => { // first timeout waits for enter-transition (lasts 800ms, that's why timeout = 800)
        transitionEl.classList.add('out')

        setTimeout(() => { // second timeout waits for exit-transition (also 800ms duration)
          transitionEl.classList.remove('is-active', 'in', 'out') // clears stuff so that it starts from bottom again
        }, 800)
      }, 800)
    }
  })
}

zangab avatar Feb 18 '22 18:02 zangab

We are approaching the Nuxt 2 EOL date (June 30, 2024) - see this article for more information. I'm closing this issue as it's marked as a Nuxt 2 related enhancement and it's not a critical issue.

That doesn't mean it might not be relevant for Nuxt 3. If it is, please feel free to open a new issue (or just comment, and I can reopen it). 🙏

Thank you for your understanding and for your contribution to Nuxt! 🎉

danielroe avatar Jun 14 '24 16:06 danielroe