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

fix: add global click handler for navigation #20990

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
*/

// Resource loaded from project dependency
export const serverSideRoutes = []
export const serverSideRoutes = [];
export const registerGlobalClickHandler = () => {};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const router = {
}
};

const flowReact : { active: boolean } = {
active: false,
}

// ClickHandler for vaadin-router-go event is copied from vaadin/router click.js
// @ts-ignore
function getAnchorOrigin(anchor) {
Expand All @@ -55,7 +59,7 @@ function normalizeURL(url: URL): void | string {
return '/' + url.href.slice(document.baseURI.length);
}

function extractPath(event: MouseEvent): void | string {
function extractURL(event: MouseEvent): void | URL {
// ignore the click if the default action is prevented
if (event.defaultPrevented) {
return;
Expand Down Expand Up @@ -131,9 +135,45 @@ function extractPath(event: MouseEvent): void | string {
return;
}

return normalizeURL(new URL(anchor.href, anchor.baseURI));
return new URL(anchor.href, anchor.baseURI);
}

function extractPath(event: MouseEvent): void | string {
const url = extractURL(event);
if (!url) {
return;
}
return normalizeURL(url);
}

export const registerGlobalClickHandler = () => {
window.addEventListener('click', (event: MouseEvent) => {
if (flowReact.active) {
return;
}
const url = extractURL(event);
if (!url) {
return;
}
// ignore click if baseURI does not match the document (external)
if (!url.href.startsWith(document.baseURI)) {
return;
}
if (event && event.preventDefault) {
event.preventDefault();
}

// Normalize path against baseURI
const path = url.pathname + url.search + url.hash;
const state = {...window.history.state}
if (state.idx !== undefined) {
state.idx = state.idx + 1;
}
window.history.pushState(state, '', path);
window.dispatchEvent(new PopStateEvent('popstate'));
}, { capture: false });
};

/**
* Fire 'vaadin-navigated' event to inform components of navigation.
* @param pathname pathname of navigation
Expand Down Expand Up @@ -352,7 +392,6 @@ function Flow() {
if (event && event.preventDefault) {
event.preventDefault();
}

navigated.current = false;
// When navigation is triggered by click on a link, fromAnchor is set to true
// in order to get a server round-trip even when navigating to the same URL again
Expand Down Expand Up @@ -417,10 +456,15 @@ function Flow() {
}, [vaadinRouterGoEventHandler, vaadinNavigateEventHandler]);

useEffect(() => {
window.addEventListener('click', navigateEventHandler);
flowReact.active = true;

return () => {
containerRef.current?.parentNode?.removeChild(containerRef.current);
containerRef.current?.removeEventListener('flow-portal-add', addPortalEventHandler as EventListener);
containerRef.current = undefined;
window.removeEventListener('click', navigateEventHandler);
flowReact.active = false;
};
}, []);

Expand Down Expand Up @@ -533,7 +577,6 @@ function Flow() {
if (outlet && outlet !== container.parentNode) {
outlet.append(container);
container.addEventListener('flow-portal-add', addPortalEventHandler as EventListener);
window.addEventListener('click', navigateEventHandler);
containerRef.current = container;
}
return container.onBeforeEnter?.call(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { routes } from "%routesJsImportPath%";
import { registerGlobalClickHandler } from "Frontend/generated/flow/Flow.js";

(window as any).Vaadin ??= {};
(window as any).Vaadin.routesConfig = routes;
registerGlobalClickHandler();

export { routes as forHMROnly };

Expand Down
Loading