UNPKG

v-shared-element

Version:

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

294 lines (277 loc) 13.5 kB
'use strict'; var illusory = require('illusory'); /****************************************************************************** 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 __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } var sharedElementMixin = { data: function () { return { $_vSharedElement_triggers: [], $_vSharedElement_firstRender: true }; }, methods: { $keepSharedElementAlive: function (trigger) { this.$data.$_vSharedElement_triggers.push(trigger); } }, activated: function () { if (this.$data.$_vSharedElement_firstRender) return (this.$data.$_vSharedElement_firstRender = false); this.$data.$_vSharedElement_triggers.forEach(function (trigger) { return trigger(); }); } }; var DEFAULT_OPTIONS = { easing: 'ease', duration: '300ms', endDuration: '150ms', zIndex: 1, compositeOnly: false, includeChildren: true, ignoreTransparency: ['img'], restrictToViewport: true, restrictToRoutes: false }; function hideElement(element) { element.style.animation = 'none'; element.style.transition = 'none'; element.style.opacity = '0'; } function withinViewport(rect) { return rect.bottom >= 0 && rect.right >= 0 && rect.top <= window.innerHeight && rect.left <= window.innerWidth; } function createRouteGuard(sharedElementCandidates, sharedElementCache) { var SharedElementRouteGuard = function (to, from, next) { sharedElementCache.clear(); var subSharedElements = []; sharedElementCandidates.forEach(function (candidate, id) { if (candidate.options.restrictToRoutes) { if (Array.isArray(candidate.options.restrictToRoutes)) { if (!candidate.options.restrictToRoutes.includes(to.path)) return; } else if (typeof candidate.options.restrictToRoutes === 'function') { if (!candidate.options.restrictToRoutes(to, from, id)) return; } } if (candidate.options.restrictToViewport) { var bcr = candidate.element.getBoundingClientRect(); if (!withinViewport(bcr)) return; } var element = new illusory.IllusoryElement(candidate.element, { includeChildren: candidate.options.includeChildren, ignoreTransparency: candidate.options.ignoreTransparency, processClone: function (node, depth) { if (depth > 0 && (node instanceof HTMLElement || node instanceof SVGElement) && node.dataset.illusoryId && sharedElementCache.has(node.dataset.illusoryId)) subSharedElements.push(node); return node; } }); sharedElementCache.set(id, { id: id, element: element, options: candidate.options }); }); sharedElementCandidates.clear(); subSharedElements.forEach(function (el) { hideElement(el); }); next(); }; var NuxtSharedElementRouteGuard = function (context) { var router = context.app.router; router.beforeEach(SharedElementRouteGuard); }; return { SharedElementRouteGuard: SharedElementRouteGuard, NuxtSharedElementRouteGuard: NuxtSharedElementRouteGuard }; } function nextFrame() { return new Promise(function (r) { return requestAnimationFrame(r); }); } var sharedElementCandidates = new Map(); var sharedElementCache = new Map(); function trigger(activeElement, vnode, combinedOptions, id) { return __awaiter(this, void 0, void 0, function () { var cachedElement, finished; return __generator(this, function (_a) { switch (_a.label) { case 0: activeElement.dataset.illusoryId = id; sharedElementCandidates.set(id, { element: activeElement, options: combinedOptions }); cachedElement = sharedElementCache.get(id); if (!cachedElement) return [2]; finished = illusory.illusory(cachedElement.element, activeElement, { element: { includeChildren: combinedOptions.includeChildren, ignoreTransparency: cachedElement.options.ignoreTransparency, processClone: function (node, depth) { if (depth > 0 && (node instanceof HTMLElement || node instanceof SVGElement) && node.dataset.illusoryId && sharedElementCache.has(node.dataset.illusoryId)) { hideElement(node); } return node; } }, compositeOnly: cachedElement.options.compositeOnly, duration: cachedElement.options.duration, zIndex: cachedElement.options.zIndex, easing: cachedElement.options.easing, relativeTo: [], beforeAnimate: function (from, to) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, nextFrame()]; case 1: _a.sent(); to.rect = to.natural.getBoundingClientRect(); to.setStyle('left', "".concat(to.rect.left, "px")); to.setStyle('top', "".concat(to.rect.top, "px")); to._to(from); return [4, nextFrame()]; case 2: _a.sent(); return [2]; } }); }); }, beforeDetach: function (from, to) { if (combinedOptions.includeChildren || !combinedOptions.endDuration || parseFloat(combinedOptions.endDuration) <= 0) return; from.hide(); to.showNatural(); to.setStyle('transition', "opacity ".concat(combinedOptions.endDuration)); to.hide(); return to.waitFor('opacity'); } }).finished; return [4, finished]; case 1: _a.sent(); return [2]; } }); }); } var $createIllusoryElement = function (el, opts) { return new illusory.IllusoryElement(el, opts); }; var insertedMounted = function (options) { if (options === void 0) { options = {}; } return function (activeElement, binding, vnode) { return __awaiter(void 0, void 0, void 0, function () { var combinedOptions, id; var _a; return __generator(this, function (_b) { combinedOptions = __assign(__assign(__assign({}, DEFAULT_OPTIONS), options), binding.value); id = binding.arg; if (!id) throw new Error("Missing ID on a v-shared-element. For usage see: https://github.com/justintaddei/v-shared-element#readme"); if ((_a = binding.value) === null || _a === void 0 ? void 0 : _a.$keepSharedElementAlive) binding.value.$keepSharedElementAlive(function () { trigger(activeElement, vnode, combinedOptions, id); }); trigger(activeElement, vnode, combinedOptions, id); return [2]; }); }); }; }; var isVue3 = function (app) { return 'config' in app && 'globalProperties' in app.config; }; var SharedElementDirective = { install: function (app, options) { if (!isVue3(app)) { app.prototype.$illusory = illusory.illusory; app.prototype.$createIllusoryElement = $createIllusoryElement; app.directive('shared-element', { inserted: insertedMounted(options) }); return; } app.config.globalProperties.$illusory = illusory.illusory; app.config.globalProperties.$createIllusoryElement = $createIllusoryElement; app.directive('shared-element', { mounted: insertedMounted(options) }); } }; var createSharedElementDirective = function (options) { if (options === void 0) { options = {}; } return ({ install: function (app, _options) { return SharedElementDirective.install(app, options); } }); }; var _a = createRouteGuard(sharedElementCandidates, sharedElementCache), NuxtSharedElementRouteGuard = _a.NuxtSharedElementRouteGuard, SharedElementRouteGuard = _a.SharedElementRouteGuard; exports.NuxtSharedElementRouteGuard = NuxtSharedElementRouteGuard; exports.SharedElementDirective = SharedElementDirective; exports.SharedElementRouteGuard = SharedElementRouteGuard; exports.createSharedElementDirective = createSharedElementDirective; exports.sharedElementMixin = sharedElementMixin;