Skip to content

Commit

Permalink
update docs/types for new features
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Oct 11, 2017
1 parent effb114 commit f0871d1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
20 changes: 19 additions & 1 deletion docs/en/advanced/scroll-behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

When using client-side routing, we may want to scroll to top when navigating to a new route, or preserve the scrolling position of history entries just like real page reload does. `vue-router` allows you to achieve these and even better, allows you to completely customize the scroll behavior on route navigation.

**Note: this feature only works in HTML5 history mode.**
**Note: this feature only works if the browser supports `history.pushState`.**

When creating the router instance, you can provide the `scrollBehavior` function:

Expand Down Expand Up @@ -60,3 +60,21 @@ scrollBehavior (to, from, savedPosition) {
```

We can also use [route meta fields](meta.md) to implement fine-grained scroll behavior control. Check out a full example [here](https://github.com/vuejs/vue-router/blob/dev/examples/scroll-behavior/app.js).

### Async Scrolling

> New in 2.8.0
You can also return a Promise that resolves to the desired position descriptor:

``` js
scrollBehavior (to, from, savedPosition) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ x: 0, y: 0 })
}, 500)
})
}
```

It's possible to hook this up with events from a page-level transition component to make the scroll behavior play nicely with your page transitions, but due to the possible variance and complexity in use cases, we simply provide this primitive to enable specific userland implementations.
9 changes: 7 additions & 2 deletions docs/en/api/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@
Signature:

```
(
type PositionDescriptor =
{ x: number, y: number } |
{ selector: string } |
?{}
type scrollBehaviorHandler = (
to: Route,
from: Route,
savedPosition?: { x: number, y: number }
) => { x: number, y: number } | { selector: string } | ?{}
) => PositionDescriptor | Promise<PositionDescriptor>
```

For more details see [Scroll Behavior](../advanced/scroll-behavior.md).
Expand Down
3 changes: 2 additions & 1 deletion flow/declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ declare type NavigationGuard = (
declare type AfterNavigationHook = (to: Route, from: Route) => any

type Position = { x: number, y: number };
type PositionResult = Position | { selector: string, offset?: Position } | void;

declare type RouterOptions = {
routes?: Array<RouteConfig>;
Expand All @@ -41,7 +42,7 @@ declare type RouterOptions = {
to: Route,
from: Route,
savedPosition: ?Position
) => Position | { selector: string, offset?: Position } | ?{};
) => PositionResult | Promise<PositionResult>;
}

declare type RedirectOption = RawLocation | ((to: Route) => RawLocation)
Expand Down
6 changes: 5 additions & 1 deletion src/util/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ export function handleScroll (

if (typeof shouldScroll.then === 'function') {
shouldScroll.then(shouldScroll => {
scrollToPosition(shouldScroll, position)
scrollToPosition((shouldScroll: any), position)
}).catch(err => {
if (process.env.NODE_ENV !== 'production') {
assert(false, err.toString())
}
})
} else {
scrollToPosition(shouldScroll, position)
Expand Down
3 changes: 2 additions & 1 deletion types/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ declare class VueRouter {
}

type Position = { x: number, y: number };
type PositionResult = Position | { selector: string, offset?: Position } | void;

export interface RouterOptions {
routes?: RouteConfig[];
Expand All @@ -59,7 +60,7 @@ export interface RouterOptions {
to: Route,
from: Route,
savedPosition: Position | void
) => Position | { selector: string, offset?: Position } | void;
) => PositionResult | Promise<PositionResult>;
}

type RoutePropsFunction = (route: Route) => Object;
Expand Down
5 changes: 5 additions & 0 deletions types/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ const router = new VueRouter({
if (savedPosition) {
return savedPosition;
}

return Promise.resolve({
x: 0,
y: 0
})
},
routes: [
{ path: "/", name: "home", component: Home, children: [
Expand Down

0 comments on commit f0871d1

Please sign in to comment.