Skip to content

Commit 6ec9876

Browse files
committed
Adds initial code
1 parent 0e37131 commit 6ec9876

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/dist/
2+
/node_modules/

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
11
# vue-default-scroll
22
Provides reasonable default scroll behavior for the vue-rouiter
3+
4+
## What does it do?
5+
By default, the vue-router doesn't scroll to the top of a page when you navigate. This package does the following:
6+
- Scrolls to the top when navigating to a new page when navigating for the first time
7+
- Scrolls to a saved position if navigating back
8+
- Scrolls to the appropriate anchor when navigating to a URL with a hash.
9+
10+
## How do I use it?
11+
12+
1. In a shell / command line:
13+
```
14+
yarn add vue-default-scroll
15+
```
16+
17+
2. In your project
18+
```
19+
import Router from 'vue-router'
20+
import {scrollBehavior} from 'vue-default-scroll'
21+
Vue.use(Router);
22+
23+
export default new Router({
24+
scrollBehavior,
25+
mode: 'history'
26+
}
27+
```

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "vue-default-scroll",
3+
"version": "1.0.0",
4+
"description": "A scroll behavior for the vue-router that does what you would expect -- scrolls to the top, or a hash, or a saved position.",
5+
"main": "/dist/index.js",
6+
"repository": "https://github.com/wakecoder/vue-default-scroll.git",
7+
"author": "wakecoder",
8+
"license": "MIT",
9+
"private": false,
10+
"scripts": {
11+
"build": "tsc"
12+
},
13+
"dependencies": {
14+
"vue-router": "^3.1.3"
15+
},
16+
"devDependencies": {
17+
"typescript": "^3.7.2"
18+
}
19+
}

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Route } from 'vue-router'
2+
export function scrollBehavior(to: Route, from: Route, savedPosition: void | { x: number, y: number }) {
3+
if (savedPosition) {
4+
return savedPosition
5+
} else {
6+
if (to.hash) {
7+
return { selector: to.hash }
8+
} else {
9+
return { x: 0, y: 0 }
10+
}
11+
}
12+
}

tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compileOnSave": true,
3+
"compilerOptions": {
4+
"module": "esnext",
5+
"noImplicitAny": true,
6+
"removeComments": true,
7+
"preserveConstEnums": true,
8+
"sourceMap": false,
9+
"outDir": "dist",
10+
"declaration": true
11+
},
12+
"include": [
13+
"src/**/*"
14+
],
15+
"exclude": [
16+
"node_modules",
17+
"**/*.spec.ts"
18+
]
19+
}

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
typescript@^3.7.2:
6+
version "3.7.2"
7+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.2.tgz#27e489b95fa5909445e9fef5ee48d81697ad18fb"
8+
integrity sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ==
9+
10+
vue-router@^3.1.3:
11+
version "3.1.3"
12+
resolved "https://registry.yarnpkg.com/vue-router/-/vue-router-3.1.3.tgz#e6b14fabc0c0ee9fda0e2cbbda74b350e28e412b"
13+
integrity sha512-8iSa4mGNXBjyuSZFCCO4fiKfvzqk+mhL0lnKuGcQtO1eoj8nq3CmbEG8FwK5QqoqwDgsjsf1GDuisDX4cdb/aQ==

0 commit comments

Comments
 (0)