Add a navigation blocker to prevent unwanted navigation
Blockers are useful for preventing users from accidentally navigating away when there is unsaved data or ongoing work that would be lost.
The blocker function is called before navigation occurs. Return true
to block navigation, or false to allow it. If blocked, use
Transition.retry to resume navigation later (e.g., after user
confirmation).
Function to evaluate whether navigation should be blocked
Blockers are not guaranteed to be executed upon a navigation event,
for example a hard page unload cannot be intercepted this way. When they
are executed, blockers run in the order they were added until one
returns true or all have been called.
import {
Blocker,
PlatformServiceName,
Transition,
} from '@limetech/lime-web-components';
const navigator = platform.get(PlatformServiceName.Navigator);
let hasUnsavedChanges = false;
const blocker: Blocker = (transition: Transition) => {
if (!hasUnsavedChanges) {
return false;
}
if (confirm('You have unsaved changes. Leave anyway?')) {
hasUnsavedChanges = false;
navigator.removeBlocker(blocker);
transition.retry();
}
return true;
};
navigator.addBlocker(blocker);
// Remember to remove the blocker in `disconnectedCallback()`
navigator.removeBlocker(blocker);
import {
Blocker,
PlatformServiceName,
Transition,
} from '@limetech/lime-web-components';
const navigator = platform.get(PlatformServiceName.Navigator);
let hasUnsavedChanges = false;
const blocker: Blocker = (transition: Transition) => {
if (!hasUnsavedChanges) {
return false;
}
void confirmNavigation(transition);
// Block for now, `retry()` resumes the navigation if the user confirms
return true;
};
async function confirmNavigation(transition: Transition) {
const shouldNavigate = await showConfirmDialog();
if (!shouldNavigate) {
return;
}
hasUnsavedChanges = false;
navigator.removeBlocker(blocker);
transition.retry();
}
// Stands in for the dialog the component would open
async function showConfirmDialog(): Promise<boolean> {
return confirm('You have unsaved changes. Leave anyway?');
}
navigator.addBlocker(blocker);
Create a URL object from location data
Converts location information (path, query, hash) into a full URL object. This is useful for generating links or inspecting what the final URL will be without actually navigating.
Location data to convert to a URL
URL object representing the location
import { PlatformServiceName } from '@limetech/lime-web-components';
const navigator = platform.get(PlatformServiceName.Navigator);
const url = navigator.createUrl({
path: '/products/123',
query: { tab: 'reviews' },
hash: '#review-456',
});
// https://example.com/products/123?tab=reviews#review-456
console.log(url.toString());
console.log(url.pathname); // /products/123
console.log(url.search); // ?tab=reviews
console.log(url.hash); // #review-456
import { h } from '@stencil/core';
import { PlatformServiceName } from '@limetech/lime-web-components';
class ProductLink {
public render() {
const navigator = platform.get(PlatformServiceName.Navigator);
const url = navigator.createUrl({
path: '/products/123',
query: { source: 'list' },
});
return <a href={url.toString()}>View Product</a>;
}
}
Get the current location information
Returns the complete current location including path, query parameters, hash, and state.
Current location object
import { PlatformServiceName } from '@limetech/lime-web-components';
const navigator = platform.get(PlatformServiceName.Navigator);
const location = navigator.getLocation();
console.log('Current path', location.path);
console.log('Query params', location.query);
console.log('Hash', location.hash);
console.log('State', location.state);
Navigate to a new path with optional query parameters
This is a convenience overload for simple navigation scenarios. For more control over navigation (including hash, state, or method), use the LocationChange overload.
The path to navigate to (e.g., '/users/123')
Optionalquery: Record<string, unknown>Optional query parameters to append to the URL
navigate - Triggers a NavigationEvent via EventDispatcher
Navigate to a new location or update the current location
This overload provides full control over all aspects of navigation, including path, query parameters, hash, state, and history management.
By default, the navigation method is automatically determined:
Override this behavior by setting location.method to 'push' or 'replace'.
state defaults to null if not providedLocation data to navigate to or update
navigate - Triggers a NavigationEvent via EventDispatcher
import { PlatformServiceName } from '@limetech/lime-web-components';
const navigator = platform.get(PlatformServiceName.Navigator);
navigator.navigate({ query: { page: 3 } });
import { PlatformServiceName } from '@limetech/lime-web-components';
const navigator = platform.get(PlatformServiceName.Navigator);
navigator.navigate({
path: '/products/123',
state: { fromSearch: true, previousPath: '/search' },
});
import { PlatformServiceName } from '@limetech/lime-web-components';
const navigator = platform.get(PlatformServiceName.Navigator);
// Replace even though the path changes
navigator.navigate({
path: '/products/456',
method: 'replace',
});
// Push even though the path is unchanged
navigator.navigate({
query: { page: 2 },
method: 'push',
});
import { PlatformServiceName } from '@limetech/lime-web-components';
const navigator = platform.get(PlatformServiceName.Navigator);
// Results in /docs/api?version=latest#parameters
navigator.navigate({
path: '/docs/api',
query: { version: 'latest' },
hash: '#parameters',
});
Remove a previously registered navigation blocker
Always remove blockers in disconnectedCallback() to prevent memory
leaks and unintended blocking behavior.
The blocker reference must be the exact same function instance that was passed to addBlocker.
The blocker function to remove
import { Blocker, PlatformServiceName } from '@limetech/lime-web-components';
const navigator = platform.get(PlatformServiceName.Navigator);
const blocker: Blocker = () => false;
navigator.addBlocker(blocker);
// The same function instance has to be passed back in
navigator.removeBlocker(blocker);
Service for navigating within the application
The Navigator enables programmatic navigation to different locations (routes) within the Lime platform.
Navigation can include:
All navigation triggers a NavigationEvent via EventDispatcher, allowing other components to respond to location changes.
Routes must be registered with RouteRegistry before navigation.
Example: Basic navigation
Example: Navigation with query parameters
Example: Advanced navigation with all options
Example: Listening to navigation events