feat: add View Transitions API support for client-side navigation (#3708)
## Summary - Integrates the [View Transitions API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transition_API) into Fresh's existing partials system - Opt-in via `f-view-transition` attribute, progressive enhancement (falls back gracefully) - Wraps all navigation types (link clicks, popstate, form submissions) in `document.startViewTransition()` ## Usage ```html <body f-client-nav f-view-transition> <!-- your app --> </body> ``` Customize animations with standard CSS: ```css ::view-transition-old(root) { animation: fade-out 0.2s ease-in; } ::view-transition-new(root) { animation: fade-in 0.2s ease-out; } ``` Per-element transitions work via `view-transition-name`: ```css .sidebar { view-transition-name: sidebar; } ``` ## Design decisions - **Opt-in** — only activates when `f-view-transition` attribute is present, can be disabled with `f-view-transition="false"` - **Progressive** — falls back to regular partial updates when the browser doesn't support the View Transitions API - **Minimal** — 35 lines of implementation, integrates into existing partials infrastructure rather than building a parallel navigation system Closes https://github.com/denoland/fresh/issues/3458 --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
cf73d29c3a
commit
400e217c5e
@@ -6,6 +6,7 @@ import {
|
||||
matchesUrl,
|
||||
PartialMode,
|
||||
UrlMatchKind,
|
||||
VIEW_TRANSITION_ATTR,
|
||||
} from "../shared_internal.ts";
|
||||
import {
|
||||
ACTIVE_PARTIALS,
|
||||
@@ -43,6 +44,33 @@ function checkClientNavEnabled(el: HTMLElement) {
|
||||
return setting.getAttribute(CLIENT_NAV_ATTR) !== "false";
|
||||
}
|
||||
|
||||
function isViewTransitionEnabled(): boolean {
|
||||
const setting = document.querySelector(`[${VIEW_TRANSITION_ATTR}]`);
|
||||
if (setting === null) return false;
|
||||
return setting.getAttribute(VIEW_TRANSITION_ATTR) !== "false";
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a DOM update function with the View Transitions API when available
|
||||
* and enabled. Falls back to calling the update directly otherwise.
|
||||
*/
|
||||
function withViewTransition(update: () => Promise<void>): Promise<void> {
|
||||
if (
|
||||
isViewTransitionEnabled() &&
|
||||
// deno-lint-ignore no-explicit-any
|
||||
typeof (document as any).startViewTransition === "function"
|
||||
) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const transition = (document as any).startViewTransition(async () => {
|
||||
await update();
|
||||
});
|
||||
transition.finished.then(resolve, reject);
|
||||
});
|
||||
}
|
||||
return update();
|
||||
}
|
||||
|
||||
// Keep track of history state to apply forward or backward animations
|
||||
let index = history.state?.index || 0;
|
||||
if (!history.state) {
|
||||
@@ -132,8 +160,10 @@ document.addEventListener("click", async (e) => {
|
||||
partial ? partial : nextUrl.href,
|
||||
location.href,
|
||||
);
|
||||
await fetchPartials(nextUrl, partialUrl, true);
|
||||
updateLinks(nextUrl);
|
||||
await withViewTransition(async () => {
|
||||
await fetchPartials(nextUrl, partialUrl, true);
|
||||
updateLinks(nextUrl);
|
||||
});
|
||||
scrollTo({ left: 0, top: 0, behavior: "instant" });
|
||||
} finally {
|
||||
if (indicator !== undefined) {
|
||||
@@ -192,8 +222,10 @@ addEventListener("popstate", async (e) => {
|
||||
|
||||
const url = new URL(location.href, location.origin);
|
||||
try {
|
||||
await fetchPartials(url, url, true);
|
||||
updateLinks(url);
|
||||
await withViewTransition(async () => {
|
||||
await fetchPartials(url, url, true);
|
||||
updateLinks(url);
|
||||
});
|
||||
scrollTo({
|
||||
left: state.scrollX ?? 0,
|
||||
top: state.scrollY ?? 0,
|
||||
@@ -263,7 +295,9 @@ document.addEventListener("submit", async (e) => {
|
||||
init = { body: new FormData(el, e.submitter), method: lowerMethod };
|
||||
}
|
||||
|
||||
await fetchPartials(actionUrl, partialUrl, true, init);
|
||||
await withViewTransition(async () => {
|
||||
await fetchPartials(actionUrl, partialUrl, true, init);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ export const DATA_CURRENT = "data-current";
|
||||
export const DATA_ANCESTOR = "data-ancestor";
|
||||
export const DATA_FRESH_KEY = "data-frsh-key";
|
||||
export const CLIENT_NAV_ATTR = "f-client-nav";
|
||||
export const VIEW_TRANSITION_ATTR = "f-view-transition";
|
||||
|
||||
export const enum OptionsType {
|
||||
ATTR = "attr",
|
||||
|
||||
Reference in New Issue
Block a user