UNPKG

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