@esmx/router 3.0.0-rc.24 → 3.0.0-rc.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -43,19 +43,24 @@ function getEventTypeList(eventType: unknown | unknown[]): string[] {
43
43
  /**
44
44
  * Event guard check - determines if the router should handle the navigation
45
45
  *
46
- * Returns false: Let browser handle default behavior (normal link navigation)
47
- * Returns true: Router takes over navigation, prevents default browser behavior
46
+ * Returns !0: Let browser handle default behavior (normal link navigation)
47
+ * Returns 0: Router takes over navigation, prevents default browser behavior
48
48
  *
49
49
  * This function intelligently decides when to let the browser handle clicks
50
50
  * (like Ctrl+click for new tabs) vs when to use SPA routing
51
51
  */
52
- function guardEvent(e: MouseEvent): boolean {
52
+ function guardEvent(e?: Event & Partial<MouseEvent>): true | undefined {
53
+ if (!e) return;
53
54
  // don't redirect with control keys
54
- if (e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) return false;
55
+ if (e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) return;
55
56
  // don't redirect when preventDefault called
56
- if (e.defaultPrevented) return false;
57
+ if (e.defaultPrevented) return;
57
58
  // don't redirect on right click
58
- if (e.button !== undefined && e.button !== 0) return false;
59
+ if (e.button !== undefined && e.button !== 0) return;
60
+ // don't redirect if `target="_blank"`
61
+ // @ts-expect-error getAttribute exists
62
+ const target = e.currentTarget?.getAttribute?.('target') ?? '';
63
+ if (/\b_blank\b/i.test(target)) return;
59
64
  // Prevent default browser navigation to enable SPA routing
60
65
  // Note: this may be a Weex event which doesn't have this method
61
66
  if (e.preventDefault) e.preventDefault();
@@ -107,11 +112,10 @@ function createNavigateFunction(
107
112
  router: Router,
108
113
  props: RouterLinkProps,
109
114
  navigationType: RouterLinkType
110
- ): (e?: MouseEvent) => Promise<void> {
111
- return async (e?: MouseEvent): Promise<void> => {
112
- if (e && !guardEvent(e)) {
113
- return;
114
- }
115
+ ): (e?: Event) => Promise<void> {
116
+ return async (e?: Event): Promise<void> => {
117
+ const eventHandler = props.eventHandler ?? guardEvent;
118
+ if (!eventHandler(e!)) return;
115
119
 
116
120
  await executeNavigation(router, props, navigationType);
117
121
  };
@@ -169,18 +173,17 @@ function computeAttributes(
169
173
  * Create event handlers generator function
170
174
  */
171
175
  function createEventHandlersGenerator(
172
- navigate: (e?: MouseEvent) => Promise<void>,
176
+ navigate: (e: Event) => Promise<void>,
173
177
  eventTypes: string[]
174
178
  ): (
175
179
  nameTransform?: (eventType: string) => string
176
- ) => Record<string, (e: MouseEvent) => Promise<void>> {
180
+ ) => Record<string, (e: Event) => Promise<void>> {
177
181
  return (nameTransform?: (eventType: string) => string) => {
178
- const handlers: Record<string, (e: MouseEvent) => Promise<void>> = {};
182
+ const handlers: Record<string, (e: Event) => Promise<void>> = {};
179
183
 
180
184
  eventTypes.forEach((eventType) => {
181
- const eventName = nameTransform
182
- ? nameTransform(eventType)
183
- : eventType.toLowerCase();
185
+ const eventName =
186
+ nameTransform?.(eventType) ?? eventType.toLowerCase();
184
187
  handlers[eventName] = navigate;
185
188
  });
186
189
 
package/src/router.ts CHANGED
@@ -227,13 +227,13 @@ export class Router {
227
227
  toInput: RouteLocationInput
228
228
  ): Promise<{ promise: Promise<RouteLayerResult>; router: Router }> {
229
229
  const layerOptions: RouteLayerOptions =
230
- (isPlainObject(toInput) ? toInput.layer : null) || {};
230
+ (isPlainObject(toInput) && toInput.layer) || {};
231
231
 
232
232
  const zIndex =
233
233
  layerOptions.zIndex ?? this.parsedOptions.zIndex + LAYER_ID.next();
234
234
 
235
235
  let promiseResolve: (result: RouteLayerResult) => void;
236
- const promise = new Promise<RouteLayerResult>((resolve) => {
236
+ let promise = new Promise<RouteLayerResult>((resolve) => {
237
237
  promiseResolve = resolve;
238
238
  });
239
239
 
@@ -283,7 +283,7 @@ export class Router {
283
283
  router.afterEach((to, from) => {
284
284
  if (layerOptions.shouldClose) {
285
285
  const result = layerOptions.shouldClose(to, from, router);
286
- if (result === false) {
286
+ if (result === true) {
287
287
  router.destroy();
288
288
  promiseResolve({
289
289
  type: 'push',
@@ -297,6 +297,18 @@ export class Router {
297
297
  router.route.state,
298
298
  router.route.url
299
299
  );
300
+ promise = promise.then(async (result) => {
301
+ await this.navigation.backHistoryState();
302
+ return result;
303
+ });
304
+ }
305
+ if (layerOptions.autoPush) {
306
+ promise = promise.then(async (result) => {
307
+ if (result.type === 'push') {
308
+ await this.push(result.route.url.href);
309
+ }
310
+ return result;
311
+ });
300
312
  }
301
313
  return {
302
314
  promise,
package/src/types.ts CHANGED
@@ -299,6 +299,10 @@ export interface RouterLinkAttributes {
299
299
  rel?: string;
300
300
  }
301
301
 
302
+ export type RouterLinkEventHandler = <E extends Event = MouseEvent>(
303
+ event: E
304
+ ) => boolean | void;
305
+
302
306
  /**
303
307
  * Framework-agnostic link configuration interface
304
308
  */
@@ -314,6 +318,7 @@ export interface RouterLinkProps {
314
318
  event?: string | string[];
315
319
  tag?: string;
316
320
  layerOptions?: RouteLayerOptions;
321
+ eventHandler?: RouterLinkEventHandler;
317
322
  }
318
323
 
319
324
  /**
@@ -332,10 +337,10 @@ export interface RouterLinkResolved {
332
337
  attributes: RouterLinkAttributes;
333
338
 
334
339
  // Navigation function
335
- navigate: (e?: MouseEvent) => Promise<void>;
340
+ navigate: (e?: Event) => Promise<void>;
336
341
 
337
342
  // Event handling
338
343
  getEventHandlers: (
339
344
  nameTransform?: (eventType: string) => string
340
- ) => Record<string, (e: MouseEvent) => Promise<void>>;
345
+ ) => Record<string, (e: Event) => Promise<void> | undefined>;
341
346
  }