navigation
/
1.0.1-alpha.193
navigation 1.0.1-alpha.193
Install from the command line:
Learn more about npm packages
$ npm install @virtualstate/navigation@1.0.1-alpha.193
Install via package.json:
"@virtualstate/navigation": "1.0.1-alpha.193"
About this version
Native JavaScript navigation implementation
const { Navigation } = await import("https://cdn.skypack.dev/@virtualstate/navigation");
Or
import { Navigation } from "https://cdn.skypack.dev/@virtualstate/navigation";
npm i --save @virtualstate/navigation
Or
yarn add @virtualstate/navigation
Then
import { Navigation } from "@virtualstate/navigation";
import { Navigation } from "@virtualstate/navigation";
const navigation = new Navigation();
// Set initial url
navigation.navigate("/");
navigation.navigate("/skipped");
// Use .finished to wait for the transition to complete
await navigation.navigate("/awaited").finished;
import { Navigation } from "@virtualstate/navigation";
const navigation = new Navigation();
navigation.addEventListener("navigate", async ({ destination, preventDefault }) => {
if (new URL(destination.url).pathname === "/disallow") {
preventDefault();
}
});
await navigation.navigate("/allowed").finished; // Resolves
await navigation.navigate("/disallow").finished; // Rejects
import { Navigation } from "@virtualstate/navigation";
import { loadPhotoIntoCache } from "./cache";
const navigation = new Navigation();
navigation.addEventListener("navigate", async ({ destination, intercept }) => {
intercept(loadPhotoIntoCache(destination.url));
});
You can match destination.url
using URLPattern
import {Navigation} from "@virtualstate/navigation";
import {URLPattern} from "urlpattern-polyfill";
const navigation = new Navigation();
navigation.addEventListener("navigate", async ({destination, intercept}) => {
const pattern = new URLPattern({ pathname: "/books/:id" });
const match = pattern.exec(destination.url);
if (match) {
intercept(transition());
}
async function transition() {
console.log("load book", match.pathname.groups.id)
}
});
navigation.navigate("/book/1");
import { Navigation } from "@virtualstate/navigation";
const navigation = new Navigation();
navigation.addEventListener("currententrychange", () => {
console.log({ updatedState: navigation.currentEntry?.getState() });
});
await navigation.updateCurrentEntry({
state: {
items: [
"first",
"second"
],
index: 0
}
}).finished;
await navigation.updateCurrentEntry({
state: {
...navigation.currentEntry.getState(),
index: 1
}
}).finished;
This is a pending development task. The below code will help visually update the window
This can be achieved various ways, but if your application completely utilises
the app history interface, then you can directly use pushState
to immediately
update the window's url.
This does not take into account the browser's native back/forward functionality, which would need to be investigated further.
import { Navigation } from "@virtualstate/navigation";
const navigation = new Navigation();
const origin = typeof location === "undefined" ? "https://example.com" : location.origin;
navigation.addEventListener("currententrychange", () => {
const { currentEntry } = navigation;
if (!currentEntry || !currentEntry.sameDocument) return;
const state = currentEntry.getState() ?? {};
const { pathname } = new URL(currentEntry.url, origin);
if (typeof window !== "undefined" && window.history) {
window.history.pushState(state, state.title, origin)
}
})
Details
- navigation
- virtualstate
- over 1 year ago
- MIT
- 41 dependencies
Assets
- navigation-1.0.1-alpha.193.tgz
Download activity
- Total downloads 0
- Last 30 days 0
- Last week 0
- Today 0