UNPKG

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