@ecopages/browser-router 0.2.0-beta.31 → 0.2.0-beta.33

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.
@@ -1,54 +1,73 @@
1
- import { applyViewTransitionNames, clearViewTransitionNames } from "@ecopages/core/client/view-transitions";
2
- class ViewTransitionManager {
3
- enabled;
4
- constructor(enabled) {
5
- this.enabled = enabled;
6
- }
7
- /**
8
- * Check if the View Transition API is supported
9
- */
10
- isSupported() {
11
- return "startViewTransition" in document;
12
- }
13
- /**
14
- * Execute a callback with view transition if available and enabled.
15
- * Falls back to direct execution if not supported.
16
- *
17
- * Navigation correctness depends on the DOM update callback completing, not on
18
- * the browser finishing the visual transition animation. Awaiting
19
- * `finished` here can leave router transactions artificially in-flight and
20
- * block later navigations during rapid repeated clicks, so the router waits
21
- * for `updateCallbackDone` and lets the animation finish in the background.
22
- * @param callback - The DOM update callback to execute
23
- * @returns Promise that resolves when the DOM update has committed
24
- */
25
- async transition(callback) {
26
- if (!this.enabled || !this.isSupported()) {
27
- await callback();
28
- return;
1
+ /**
2
+ * Manages View Transition API integration
3
+ * @module
4
+ */
5
+ import { applyViewTransitionNames, clearViewTransitionNames } from '@ecopages/core/client/view-transitions';
6
+ /**
7
+ * Service for handling View Transition API during page transitions.
8
+ * Falls back to direct execution if the API is not supported.
9
+ */
10
+ export class ViewTransitionManager {
11
+ enabled;
12
+ constructor(enabled) {
13
+ this.enabled = enabled;
14
+ }
15
+ /**
16
+ * Check if the View Transition API is supported
17
+ */
18
+ isSupported() {
19
+ return 'startViewTransition' in document;
20
+ }
21
+ /**
22
+ * Execute a callback with view transition if available and enabled.
23
+ * Falls back to direct execution if not supported.
24
+ *
25
+ * Navigation correctness depends on the DOM update callback completing, not on
26
+ * the browser finishing the visual transition animation. Awaiting
27
+ * `finished` here can leave router transactions artificially in-flight and
28
+ * block later navigations during rapid repeated clicks, so the router waits
29
+ * for `updateCallbackDone` and lets the animation finish in the background.
30
+ * @param callback - The DOM update callback to execute
31
+ * @returns Promise that resolves when the DOM update has committed
32
+ */
33
+ async transition(callback) {
34
+ if (!this.enabled || !this.isSupported()) {
35
+ await callback();
36
+ return;
37
+ }
38
+ /**
39
+ * Apply view transition names to current elements before starting the transition.
40
+ * This ensures the "old" state is captured correctly.
41
+ */
42
+ applyViewTransitionNames();
43
+ const transition = document.startViewTransition(async () => {
44
+ await callback();
45
+ /**
46
+ * Apply names to the NEW elements after DOM swap and hydration.
47
+ * This ensures the "new" state is captured correctly.
48
+ */
49
+ applyViewTransitionNames();
50
+ });
51
+ void transition.ready.catch((error) => {
52
+ if (error instanceof Error && error.name === 'AbortError') {
53
+ return;
54
+ }
55
+ console.error('[ecopages] View transition failed to start:', error);
56
+ });
57
+ void transition.finished
58
+ .catch((error) => {
59
+ if (error instanceof Error && error.name === 'AbortError') {
60
+ return;
61
+ }
62
+ console.error('[ecopages] View transition lifecycle failed:', error);
63
+ })
64
+ .finally(() => {
65
+ /**
66
+ * Cleanup view transition names and dynamic styles after the browser's
67
+ * animation lifecycle completes.
68
+ */
69
+ clearViewTransitionNames();
70
+ });
71
+ await transition.updateCallbackDone;
29
72
  }
30
- applyViewTransitionNames();
31
- const transition = document.startViewTransition(async () => {
32
- await callback();
33
- applyViewTransitionNames();
34
- });
35
- void transition.ready.catch((error) => {
36
- if (error instanceof Error && error.name === "AbortError") {
37
- return;
38
- }
39
- console.error("[ecopages] View transition failed to start:", error);
40
- });
41
- void transition.finished.catch((error) => {
42
- if (error instanceof Error && error.name === "AbortError") {
43
- return;
44
- }
45
- console.error("[ecopages] View transition lifecycle failed:", error);
46
- }).finally(() => {
47
- clearViewTransitionNames();
48
- });
49
- await transition.updateCallbackDone;
50
- }
51
73
  }
52
- export {
53
- ViewTransitionManager
54
- };
@@ -77,25 +77,6 @@ export interface EcoRouterOptions {
77
77
  export type BrowserRouterNavigateOptions = {
78
78
  direction?: 'forward' | 'back' | 'replace';
79
79
  };
80
- /** Events emitted during the navigation lifecycle */
81
- export interface EcoNavigationEvent {
82
- url: URL;
83
- direction: 'forward' | 'back' | 'replace';
84
- }
85
- /** Event fired before the DOM swap occurs */
86
- export interface EcoBeforeSwapEvent extends EcoNavigationEvent {
87
- newDocument: Document;
88
- reload: () => void;
89
- }
90
- /** Event fired after the DOM swap completes */
91
- export interface EcoAfterSwapEvent extends EcoNavigationEvent {
92
- }
93
- /** Custom event map for navigation lifecycle */
94
- export interface EcoRouterEventMap {
95
- 'eco:before-swap': CustomEvent<EcoBeforeSwapEvent>;
96
- 'eco:after-swap': CustomEvent<EcoAfterSwapEvent>;
97
- 'eco:page-load': CustomEvent<EcoNavigationEvent>;
98
- }
99
80
  /** Default document-level `<html>` attributes synchronized during navigation swaps. */
100
81
  export declare const DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC: string[];
101
82
  /** Default configuration options */
@@ -1,23 +1,22 @@
1
- import { ECO_DOCUMENT_OWNER_ATTRIBUTE } from "@ecopages/core/router/navigation-coordinator";
1
+ import { ECO_DOCUMENT_OWNER_ATTRIBUTE } from '@ecopages/core/router/navigation-coordinator';
2
+ /** Default prefetch configuration */
2
3
  const DEFAULT_PREFETCH_CONFIG = {
3
- strategy: "intent",
4
- delay: 65,
5
- noPrefetchAttribute: "data-eco-no-prefetch",
6
- respectDataSaver: true
4
+ strategy: 'intent',
5
+ delay: 65,
6
+ noPrefetchAttribute: 'data-eco-no-prefetch',
7
+ respectDataSaver: true,
7
8
  };
8
- const DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC = ["lang", "dir", ECO_DOCUMENT_OWNER_ATTRIBUTE];
9
- const DEFAULT_OPTIONS = {
10
- linkSelector: "a[href]",
11
- documentElementAttributesToSync: DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
12
- persistAttribute: "data-eco-persist",
13
- reloadAttribute: "data-eco-reload",
14
- updateHistory: true,
15
- scrollBehavior: "top",
16
- viewTransitions: true,
17
- smoothScroll: false,
18
- prefetch: DEFAULT_PREFETCH_CONFIG
19
- };
20
- export {
21
- DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
22
- DEFAULT_OPTIONS
9
+ /** Default document-level `<html>` attributes synchronized during navigation swaps. */
10
+ export const DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC = ['lang', 'dir', ECO_DOCUMENT_OWNER_ATTRIBUTE];
11
+ /** Default configuration options */
12
+ export const DEFAULT_OPTIONS = {
13
+ linkSelector: 'a[href]',
14
+ documentElementAttributesToSync: DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
15
+ persistAttribute: 'data-eco-persist',
16
+ reloadAttribute: 'data-eco-reload',
17
+ updateHistory: true,
18
+ scrollBehavior: 'top',
19
+ viewTransitions: true,
20
+ smoothScroll: false,
21
+ prefetch: DEFAULT_PREFETCH_CONFIG,
23
22
  };
package/src/index.d.ts CHANGED
@@ -3,6 +3,6 @@
3
3
  * Client-side navigation and view transitions for Ecopages
4
4
  * @module
5
5
  */
6
- export { createRouter, DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS, EcoRouter, syncDocumentElementAttributes, type BrowserRouterNavigateOptions, type EcoAfterSwapEvent, type EcoBeforeSwapEvent, type EcoNavigationEvent, type EcoRouterEventMap, type EcoRouterOptions, } from './client/index.js';
6
+ export { createRouter, DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS, EcoRouter, syncDocumentElementAttributes, type BrowserRouterNavigateOptions, type EcoRouterOptions, } from './client/index.js';
7
7
  export { DomSwapper } from './client/dom/dom-swapper.js';
8
8
  export { ViewTransitionManager } from './client/services/view-transition-manager.js';
package/src/index.js CHANGED
@@ -1,18 +1,8 @@
1
- import {
2
- createRouter,
3
- DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
4
- DEFAULT_OPTIONS,
5
- EcoRouter,
6
- syncDocumentElementAttributes
7
- } from "./client/index.js";
8
- import { DomSwapper } from "./client/dom/dom-swapper.js";
9
- import { ViewTransitionManager } from "./client/services/view-transition-manager.js";
10
- export {
11
- DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
12
- DEFAULT_OPTIONS,
13
- DomSwapper,
14
- EcoRouter,
15
- ViewTransitionManager,
16
- createRouter,
17
- syncDocumentElementAttributes
18
- };
1
+ /**
2
+ * @ecopages/browser-router
3
+ * Client-side navigation and view transitions for Ecopages
4
+ * @module
5
+ */
6
+ export { createRouter, DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS, EcoRouter, syncDocumentElementAttributes, } from './client/index.js';
7
+ export { DomSwapper } from './client/dom/dom-swapper.js';
8
+ export { ViewTransitionManager } from './client/services/view-transition-manager.js';