UNPKG

3.45 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3 typeof define === 'function' && define.amd ? define(factory) :
4 (global.nextLevelScroll = factory());
5}(this, (function () { 'use strict';
6
7 var index = {
8 functional: true,
9 render: function render(h, _ref) {
10 var children = _ref.children,
11 data = _ref.data,
12 _ref$props = _ref.props,
13 target = _ref$props.target,
14 scrollFunction = _ref$props.scrollFunction,
15 _ref$props$shouldNavi = _ref$props.shouldNavigate,
16 shouldNavigate = _ref$props$shouldNavi === void 0 ? false : _ref$props$shouldNavi,
17 _ref$props$navigation = _ref$props.navigationType,
18 navigationType = _ref$props$navigation === void 0 ? 'push' : _ref$props$navigation,
19 _ref$props$tag = _ref$props.tag,
20 tag = _ref$props$tag === void 0 ? 'div' : _ref$props$tag,
21 $router = _ref.parent.$router,
22 _ssrNode = _ref._ssrNode;
23
24 var clickFunction = function clickFunction() {
25 var fn = scrollFunction || defaultScrollFunction;
26 fn(target, {
27 shouldNavigate: shouldNavigate,
28 navigationType: navigationType,
29 $router: $router
30 });
31 };
32
33 return h(tag, Object.assign({}, data, {
34 on: {
35 // Ignore scroll function on server side
36 click: _ssrNode || process.server ? undefined : clickFunction
37 }
38 }), children);
39 }
40 };
41
42 var defaultScrollFunction = function defaultScrollFunction(rawTarget, _ref2) {
43 return new Promise(function ($return, $error) {
44 var shouldNavigate, navigationType, $router, target, currentRoute, hash, fullPath, newPath, node;
45 shouldNavigate = _ref2.shouldNavigate, navigationType = _ref2.navigationType, $router = _ref2.$router;
46 return Promise.resolve(new Promise(function ($return, $error) {
47 if (typeof rawTarget === 'function') {
48 return Promise.resolve(rawTarget()).then($return, $error);
49 }
50
51 return $return(rawTarget);
52 })).then(function ($await_4) {
53 try {
54 target = $await_4;
55
56 // If no target given, auto scroll to top
57 if (!target) {
58 window.scroll({
59 top: 0,
60 behavior: 'smooth'
61 });
62
63 if (shouldNavigate && $router) {
64 currentRoute = $router.currentRoute;
65 hash = currentRoute.hash;
66 fullPath = currentRoute.fullPath;
67 newPath = fullPath.replace(hash, '');
68 navigate($router, newPath, navigationType);
69 }
70
71 return $return();
72 }
73
74 node = document.querySelector(target);
75
76 // If target prop is present but the node does not exist, send an error
77 if (!node) {
78 // eslint-disable-next-line no-console
79 return $return(console.error("Could not scroll to ".concat(target)));
80 }
81
82 node.scrollIntoView({
83 behavior: 'smooth'
84 });
85
86 if (shouldNavigate && $router) {
87 navigate($router, target, navigationType);
88 }
89
90 return $return();
91 } catch ($boundEx) {
92 return $error($boundEx);
93 }
94 }, $error);
95 });
96 };
97
98 var navigate = function navigate($router, path, type) {
99 $router[type](path);
100 };
101
102 return index;
103
104})));