Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose listener function as new API #300

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ in the `Link` component.
</script>
```

#### `listen`

A function that allows you to listen to path changes made from `navigate`, `Link` and browser back button.

The argument supplied must be a function that receives a Location object. Use the returned function to unsubscribe the listener.

This function is meant to be used outside a Svelte component. The alternative function is `useLocation`, `useRouter` and `useHistory` where it uses Svelte context to retrieve location, router or history object.

```js
import { listen as addListener } from 'svelte-routing';

function init() {
addListener(({ location }) => {
console.log(location.pathname);
})
}
```

#### `link`

An action used on anchor tags to navigate around the application. You can add an
Expand Down
4 changes: 2 additions & 2 deletions src/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ const createMemorySource = (initialPathname = "/") => {
const globalHistory = createHistory(
canUseDOM() ? window : createMemorySource()
);
const { navigate } = globalHistory;
const { navigate, listen } = globalHistory;

export { globalHistory, navigate, createHistory, createMemorySource };
export { globalHistory, navigate, listen, createHistory, createMemorySource };
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export { default as Route } from "./Route.svelte";
export { default as Router } from "./Router.svelte";
export { link, links } from "./actions.js";
export * from "./contexts.js";
export { navigate } from "./history.js";
export { navigate, listen } from "./history.js";
10 changes: 10 additions & 0 deletions types/functions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ export const navigate: (
blurActiveElement?: boolean;
}
) => void;

export const listen: (
listener: (data: {
location: Location & {
state: any,
key: string
},
action: "POP"|"PUSH"
}) => void
) => () => void;
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export { Route } from "./Route";
export { Router } from "./Router";
export { link, links } from "./actions";
export * from "./contexts";
export { navigate } from "./functions";
export { navigate, listen } from "./functions";