UNPKG

v-shared-element

Version:

Declarative shared-element transitions between pages for Vue.js and Nuxt.js

17 lines (14 loc) 6.6 kB
!function(e,t){"use strict"; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise */var n=function(){return n=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},n.apply(this,arguments)};function r(e,t,n,r){return new(n||(n=Promise))((function(i,o){function a(e){try{s(r.next(e))}catch(e){o(e)}}function u(e){try{s(r.throw(e))}catch(e){o(e)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,u)}s((r=r.apply(e,t||[])).next())}))}function i(e,t){var n,r,i,o,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function u(u){return function(s){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,u[0]&&(a=0)),a;)try{if(n=1,r&&(i=2&u[0]?r.return:u[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,u[1])).done)return i;switch(r=0,i&&(u=[2&u[0],i.value]),u[0]){case 0:case 1:i=u;break;case 4:return a.label++,{value:u[1],done:!1};case 5:a.label++,r=u[1],u=[0];continue;case 7:u=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==u[0]&&2!==u[0])){a=0;continue}if(3===u[0]&&(!i||u[1]>i[0]&&u[1]<i[3])){a.label=u[1];break}if(6===u[0]&&a.label<i[1]){a.label=i[1],i=u;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(u);break}i[2]&&a.ops.pop(),a.trys.pop();continue}u=t.call(e,a)}catch(e){u=[6,e],r=0}finally{n=i=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,s])}}}var o={easing:"ease",duration:"300ms",endDuration:"150ms",zIndex:1,compositeOnly:!1,includeChildren:!0,ignoreTransparency:["img"],restrictToViewport:!0,restrictToRoutes:!1};function a(e){e.style.animation="none",e.style.transition="none",e.style.opacity="0"}function u(){return new Promise((function(e){return requestAnimationFrame(e)}))}var s=new Map,l=new Map;function c(e,n,o,c){return r(this,void 0,void 0,(function(){var n,d;return i(this,(function(f){switch(f.label){case 0:return e.dataset.illusoryId=c,s.set(c,{element:e,options:o}),(n=l.get(c))?(d=t.illusory(n.element,e,{element:{includeChildren:o.includeChildren,ignoreTransparency:n.options.ignoreTransparency,processClone:function(e,t){return t>0&&(e instanceof HTMLElement||e instanceof SVGElement)&&e.dataset.illusoryId&&l.has(e.dataset.illusoryId)&&a(e),e}},compositeOnly:n.options.compositeOnly,duration:n.options.duration,zIndex:n.options.zIndex,easing:n.options.easing,relativeTo:[],beforeAnimate:function(e,t){return r(this,void 0,void 0,(function(){return i(this,(function(n){switch(n.label){case 0:return[4,u()];case 1:return n.sent(),t.rect=t.natural.getBoundingClientRect(),t.setStyle("left","".concat(t.rect.left,"px")),t.setStyle("top","".concat(t.rect.top,"px")),t._to(e),[4,u()];case 2:return n.sent(),[2]}}))}))},beforeDetach:function(e,t){if(!(o.includeChildren||!o.endDuration||parseFloat(o.endDuration)<=0))return e.hide(),t.showNatural(),t.setStyle("transition","opacity ".concat(o.endDuration)),t.hide(),t.waitFor("opacity")}}).finished,[4,d]):[2];case 1:return f.sent(),[2]}}))}))}var d=function(e,n){return new t.IllusoryElement(e,n)},f=function(e){return void 0===e&&(e={}),function(t,a,u){return r(void 0,void 0,void 0,(function(){var r,u,s;return i(this,(function(i){if(r=n(n(n({},o),e),a.value),!(u=a.arg))throw new Error("Missing ID on a v-shared-element. For usage see: https://github.com/justintaddei/v-shared-element#readme");return(null===(s=a.value)||void 0===s?void 0:s.$keepSharedElementAlive)&&a.value.$keepSharedElementAlive((function(){c(t,0,r,u)})),c(t,0,r,u),[2]}))}))}},h={install:function(e,n){if(!function(e){return"config"in e&&"globalProperties"in e.config}(e))return e.prototype.$illusory=t.illusory,e.prototype.$createIllusoryElement=d,void e.directive("shared-element",{inserted:f(n)});e.config.globalProperties.$illusory=t.illusory,e.config.globalProperties.$createIllusoryElement=d,e.directive("shared-element",{mounted:f(n)})}},p=function(e,n){var r=function(r,i,o){n.clear();var u=[];e.forEach((function(e,o){if(e.options.restrictToRoutes)if(Array.isArray(e.options.restrictToRoutes)){if(!e.options.restrictToRoutes.includes(r.path))return}else if("function"==typeof e.options.restrictToRoutes&&!e.options.restrictToRoutes(r,i,o))return;if(e.options.restrictToViewport){var a=e.element.getBoundingClientRect();if(!((s=a).bottom>=0&&s.right>=0&&s.top<=window.innerHeight&&s.left<=window.innerWidth))return}var s,l=new t.IllusoryElement(e.element,{includeChildren:e.options.includeChildren,ignoreTransparency:e.options.ignoreTransparency,processClone:function(e,t){return t>0&&(e instanceof HTMLElement||e instanceof SVGElement)&&e.dataset.illusoryId&&n.has(e.dataset.illusoryId)&&u.push(e),e}});n.set(o,{id:o,element:l,options:e.options})})),e.clear(),u.forEach((function(e){a(e)})),o()};return{SharedElementRouteGuard:r,NuxtSharedElementRouteGuard:function(e){e.app.router.beforeEach(r)}}}(s,l),m=p.NuxtSharedElementRouteGuard,y=p.SharedElementRouteGuard;e.NuxtSharedElementRouteGuard=m,e.SharedElementDirective=h,e.SharedElementRouteGuard=y,e.createSharedElementDirective=function(e){return void 0===e&&(e={}),{install:function(t,n){return h.install(t,e)}}},e.sharedElementMixin={data:function(){return{$_vSharedElement_triggers:[],$_vSharedElement_firstRender:!0}},methods:{$keepSharedElementAlive:function(e){this.$data.$_vSharedElement_triggers.push(e)}},activated:function(){if(this.$data.$_vSharedElement_firstRender)return this.$data.$_vSharedElement_firstRender=!1;this.$data.$_vSharedElement_triggers.forEach((function(e){return e()}))}}}(this.window=this.window||{},window);