|
16 | 16 | /// <reference lib="es2018" />
|
17 | 17 | import { Flow as _Flow } from 'Frontend/generated/jar-resources/Flow.js';
|
18 | 18 | import React, { useCallback, useEffect, useReducer, useRef, useState, type ReactNode } from 'react';
|
19 |
| -import { matchRoutes, useBlocker, useLocation, useNavigate, type NavigateOptions, useHref } from 'react-router'; |
| 19 | +import { matchRoutes, useBlocker, useLocation, useNavigate, type NavigateOptions, useHref, type DataRouter } from 'react-router'; |
20 | 20 | import { createPortal } from 'react-dom';
|
21 | 21 |
|
22 | 22 | const flow = new _Flow({
|
@@ -134,6 +134,22 @@ function extractPath(event: MouseEvent): void | string {
|
134 | 134 | return normalizeURL(new URL(anchor.href, anchor.baseURI));
|
135 | 135 | }
|
136 | 136 |
|
| 137 | +export const registerGlobalClickHandler = (router: DataRouter)=> { |
| 138 | + window.addEventListener('click', (event: MouseEvent) => { |
| 139 | + const path = extractPath(event); |
| 140 | + if (!path) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + if (event && event.preventDefault) { |
| 145 | + event.preventDefault(); |
| 146 | + } |
| 147 | + router.navigate(path); |
| 148 | + }); |
| 149 | +}; |
| 150 | + |
| 151 | + |
| 152 | + |
137 | 153 | /**
|
138 | 154 | * Fire 'vaadin-navigated' event to inform components of navigation.
|
139 | 155 | * @param pathname pathname of navigation
|
@@ -342,28 +358,6 @@ function Flow() {
|
342 | 358 | [dispatchPortalAction]
|
343 | 359 | );
|
344 | 360 |
|
345 |
| - const navigateEventHandler = useCallback( |
346 |
| - (event: MouseEvent) => { |
347 |
| - const path = extractPath(event); |
348 |
| - if (!path) { |
349 |
| - return; |
350 |
| - } |
351 |
| - |
352 |
| - if (event && event.preventDefault) { |
353 |
| - event.preventDefault(); |
354 |
| - } |
355 |
| - |
356 |
| - navigated.current = false; |
357 |
| - // When navigation is triggered by click on a link, fromAnchor is set to true |
358 |
| - // in order to get a server round-trip even when navigating to the same URL again |
359 |
| - fromAnchor.current = true; |
360 |
| - navigate(path); |
361 |
| - // Dispatch close event for overlay drawer on click navigation. |
362 |
| - window.dispatchEvent(new CustomEvent('close-overlay-drawer')); |
363 |
| - }, |
364 |
| - [navigate] |
365 |
| - ); |
366 |
| - |
367 | 361 | const vaadinRouterGoEventHandler = useCallback(
|
368 | 362 | (event: CustomEvent<URL>) => {
|
369 | 363 | const url = event.detail;
|
@@ -533,7 +527,6 @@ function Flow() {
|
533 | 527 | if (outlet && outlet !== container.parentNode) {
|
534 | 528 | outlet.append(container);
|
535 | 529 | container.addEventListener('flow-portal-add', addPortalEventHandler as EventListener);
|
536 |
| - window.addEventListener('click', navigateEventHandler); |
537 | 530 | containerRef.current = container;
|
538 | 531 | }
|
539 | 532 | return container.onBeforeEnter?.call(
|
|
0 commit comments