Skip to content

Allow applying server-driven client updates within a browser view transition - #24816

Draft
totally-not-ai[bot] wants to merge 18 commits into
mainfrom
feat/wrap-server-updates-in-view-transition
Draft

totally-not-ai[bot] wants to merge 18 commits into
mainfrom
feat/wrap-server-updates-in-view-transition

Conversation

@totally-not-ai

@totally-not-ai totally-not-ai Bot commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

What changed

Adds UI#enableViewTransition(), which requests that the DOM changes produced by the current server response are applied inside a document.startViewTransition() call on the client, so the resulting updates can be animated.

  • Experimental, behind a feature flag: the feature is off unless com.vaadin.experimental.viewTransitions=true is set (for example in src/main/resources/vaadin-featureflags.properties, or from the experimental features tab in Copilot). With the flag off, UI#enableViewTransition() throws a DisabledFeatureException that explains how to enable it. The flag is checked on every call, so no server restart is needed.
  • UI#enableViewTransition() — public opt-in API for the current request.
  • CoreFeatureFlagProvider.VIEW_TRANSITIONS — the new viewTransitions feature flag. Its "more info" link points to the feedback issue Feedback: experimental view transitions for server updates #27688.
  • UIInternals — stores the request per response (isViewTransitionRequested() / setViewTransitionRequested(boolean)).
  • UidlWriter — writes the flag into the response metadata as JsonConstants.META_VIEW_TRANSITION and resets it afterwards, so it only affects a single round trip.
  • MessageHandler.ts — when the server asked for a transition and the browser supports the API, the reactive flush runs inside the document.startViewTransition() update callback; otherwise the changes are applied directly. A skipped transition (e.g. in a hidden tab) is not reported as an error; a failure while applying the changes is reported like one without a transition, also in production mode. Because the browser invokes that callback asynchronously, ending the request, stopping the loading indicator and releasing the response lock also happen inside it, so the client keeps reporting itself as active (e.g. for TestBench's waitForVaadin) and no queued message can flush the changes outside the transition.

Why

There is currently no way to have an arbitrary, server-driven UI change animate — changes are always applied instantly. Making this a per-response opt-in (rather than a global configuration flag) lets an add-on or a specific piece of application logic animate individual updates without affecting the rest of the application. By default the browser applies a simple cross-fade; richer transitions can be defined purely with CSS via view-transition-name and the ::view-transition-* pseudo-elements. Behaviour degrades gracefully: browsers without the View Transitions API (e.g. Safari 17 and Firefox before 144, including the current ESR) just apply the changes as before.

Testing

  • UI tests verify that enableViewTransition() throws a DisabledFeatureException naming the flag when it is off or no service is available, and that it requests a transition when the flag is on, also for a UI without a session.
  • UidlWriterTest verifies the metadata flag is written when requested and that it is reset so it does not leak into the next response.
  • Client tests use an asynchronous startViewTransition stub to verify that the changes are applied, the request ended and the next queued message handled only once the transition runs its update, and that changes are applied directly when no transition is requested or the API is unavailable.
  • Client tests also verify that a message whose changes fail to apply still ends the request exactly once, with and without view transitions, and that a skipped transition does not surface as an unhandled promise rejection.

Use case

A developer has a table of tasks with a "Sort by priority" button. Today, clicking it makes the rows jump to their new positions instantly, which makes it hard to follow where a given row went. They want the reordering to animate, but they do not want to animate every update in the application, and they do not want to reimplement sorting in client-side JavaScript just to get a transition.

They enable the experimental flag in src/main/resources/vaadin-featureflags.properties:

com.vaadin.experimental.viewTransitions=true

Then they opt in for that one round trip and let CSS do the rest:

sortButton.addClickListener(event -> {
    // Only the changes produced by this request are animated
    UI.getCurrent().enableViewTransition();

    tasks.sort(Comparator.comparing(Task::getPriority));
    taskList.removeAll();
    tasks.forEach(task -> {
        TaskRow row = new TaskRow(task);
        // Lets the browser match each row before and after the update
        row.getStyle().set("view-transition-name", "task-" + task.getId());
        taskList.add(row);
    });
});

The browser snapshots the rows before and after the update and animates them into their new positions; no per-component animation code is needed, and browsers without the View Transitions API simply render the reordered list directly.

API Changes

com.vaadin.flow.component.UI

// Added
public void enableViewTransition() // requests that the changes in the current response are applied inside a browser view transition; experimental, throws DisabledFeatureException unless the viewTransitions feature flag is enabled

com.vaadin.experimental.CoreFeatureFlagProvider

// Added
public static final Feature VIEW_TRANSITIONS // "viewTransitions" feature flag

com.vaadin.flow.component.internal.UIInternals

// Added
public boolean isViewTransitionRequested()
public void setViewTransitionRequested(boolean viewTransitionRequested) // reset after the response is written, so it affects a single response

com.vaadin.flow.shared.JsonConstants

// Added
public static final String META_VIEW_TRANSITION // "viewTransition" response metadata key

🤖 Generated with Claude Code

Adds UI#enableViewTransition() so that the DOM changes produced by the
current server response are applied inside a document.startViewTransition()
call on the client, enabling the resulting updates to be animated.

The request is stored per-response in UIInternals, written into the
response metadata by UidlWriter, and reset after the response is built so
it only affects a single round trip. This lets an add-on or specific
application logic opt in to view transitions for individual updates without
a global configuration flag. The client applies the changes within a view
transition only when the browser supports the API, falling back to applying
them directly otherwise.
@Legioth
Legioth marked this pull request as draft June 25, 2026 07:40
private static native void startViewTransition(Command command)
/*-{
$doc.startViewTransition(function() {
command.@com.vaadin.client.Command::execute()();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing $entry

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — wrapped the startViewTransition callback in $entry(...). Since the browser invokes it asynchronously (outside the GWT call stack), $entry is needed so GWT's exception handling and the deferred command scheduler run correctly for code executed within the transition.

totally-not-ai Bot added 2 commits June 25, 2026 07:43
The callback passed to document.startViewTransition() is invoked
asynchronously by the browser, outside the GWT call stack. Wrapping it in
$entry ensures GWT exception handling and the deferred command scheduler
run correctly for code executed within the transition.
Reverts unrelated package-lock.json churn introduced by the build step.
The it-tests (11) job failed in the "Set up Maven" step with HTTP 403 while
downloading the Maven distribution from Maven Central, before any project
code was compiled or tested. This empty commit re-triggers CI to get past
the transient infrastructure failure.
@github-actions

github-actions Bot commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 1 662 files  ± 0   1 753 suites  ±0   1h 47m 54s ⏱️ -19s
14 154 tests +12  14 082 ✅ +12  72 💤 ±0  0 ❌ ±0 
14 590 runs  +12  14 518 ✅ +12  72 💤 ±0  0 ❌ ±0 

Results for commit 1b8c4fa. ± Comparison against base commit 3a9144f.

♻️ This comment has been updated with latest results.

Move the view-transition decision into a single JSNI helper (flushChanges)
so that the only added executable Java line in MessageHandler is the call
itself; the previous approach turned existing, JVM-untestable client lines
into new uncovered lines and dragged down new-code coverage.

Also add GWT tests verifying that server-requested changes are applied
inside a view transition when the browser supports the API, and applied
directly otherwise.
@sonarqubecloud

Copy link
Copy Markdown

@Artur-

Artur- commented Aug 23, 2026

Copy link
Copy Markdown
Member

Did you ever get to try this one out @jouni and did it help? If you describe the use cases for transitions here, then somebody else could also have a look

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Artur- I have expanded the Javadoc of UI.enableViewTransition() to describe the use cases so they are documented alongside the API.

The motivating case is animating an arbitrary, server-driven UI change that would otherwise apply instantly. For example, a feature that fills in a form on the server can call enableViewTransition() before updating the fields, so the new values and the switch from editable to read-only cross-fade into place instead of snapping. Other examples: toggling component visibility, reordering items in a list, or swapping the contents of a layout.

The browser snapshots the affected elements before and after the update and animates between the two states, so no per-component animation code is needed. Because it is opt-in per response, an add-on or a specific piece of logic can animate a single round trip without any global configuration. Leaving the question of real-world results to @jouni.

@jouni

jouni commented Aug 24, 2026

Copy link
Copy Markdown
Member

Did you ever get to try this one out

Yeah, I did a quick test locally back in June. I remember it working as expected.

The use case was about our AI form filler, that we might’ve wanted to use view transitions for the "AI has modified this field value" marker animations, which would’ve allowed us to capture the screenshot of the state of the fields before the AI changed their values, and include the changing of the value in the transitions as well. But we decided to go with the simpler CSS animation approach, and just delay setting the new value to halfway of the animation, so to the user it seems the value changes with the animation, not before it.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@jouni Thanks for confirming it worked when you tried it. Even though the form filler ended up using the simpler CSS-animation approach, this keeps the view-transition path available as an opt-in tool for exactly that kind of case — capturing the before/after snapshot and animating the value change itself — so an add-on or application can reach for it per response when the CSS approach is not enough. No code changes needed based on this; leaving the thread open for further input.

@jouni

jouni commented Aug 24, 2026

Copy link
Copy Markdown
Member

This is still a nice-to-have feature, I would say. Probably non needed or used very often. We could build on it, though, making it easier to create more complex view transitions, instead of just the default crossfade.

@totally-not-ai totally-not-ai Bot changed the title feat: allow applying client updates within a view transition feat: allow applying server-driven client updates within a view transition Aug 24, 2026
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@jouni Agreed it is a nice-to-have building block. Worth noting that more complex transitions than the default cross-fade are already possible on top of this: since it wraps the update in the browser view transition, an app can assign view-transition-name to the relevant elements and style the ::view-transition-* pseudo-elements in CSS to fully customize the animation. I have documented this in the enableViewTransition() Javadoc so the extension point is discoverable. Higher-level helpers for authoring such transitions could indeed be layered on later.

@totally-not-ai totally-not-ai Bot changed the title feat: allow applying server-driven client updates within a view transition feat: allow applying client updates within a view transition Aug 24, 2026
@jouni

jouni commented Aug 24, 2026

Copy link
Copy Markdown
Member

Yes, complex transitions are possible after this change. Just saying that creating those are outside the comfort zone of our target audience, if they can only use CSS for it.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@jouni Fair point — authoring custom transitions in raw CSS is not ideal for a Java-focused audience. This PR intentionally stays at the enabling primitive: it makes server-driven updates run inside a view transition, which is the prerequisite for anything fancier. A Java-friendly API for defining more complex transitions would be a natural follow-up built on top of this, but it is out of scope here to keep the change small and focused. Leaving the thread open so that idea can be tracked.

@totally-not-ai totally-not-ai Bot changed the title feat: allow applying client updates within a view transition Allow applying server-driven client updates within a browser view transition Aug 24, 2026
@sonarqubecloud

Copy link
Copy Markdown

…tes-in-view-transition

The GWT client engine was removed on main in favour of the TypeScript
client, so the view transition handling is ported from the deleted
MessageHandler.java to MessageHandler.ts, with its GWT tests replaced
by web-test-runner tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Memory footprint

Compared with the base commit 3a9144f.

Scenario Metric Base Head Change
Empty UI Heap retained per UI 6,488 B 6,488 B no change
Empty UI Serialized session with one UI 21,484 B 21,511 B $\color{red}\textbf{+27 B}$ (+0.1%)
UI with 200 component rows Heap retained per UI 693,264 B 693,264 B no change
UI with 200 component rows Serialized session with one UI 438,901 B 438,928 B $\color{red}\textbf{+27 B}$ (+0.0%)
UI with 200 form rows added in the latest round trip Heap retained per UI 1,634,944 B 1,634,936 B −8 B (ignored)
UI with 200 form rows added in the latest round trip Serialized session with one UI 1,368,749 B 1,368,776 B $\color{red}\textbf{+27 B}$ (+0.0%)
UI with 200 form rows Heap retained per UI 1,589,984 B 1,589,984 B no change
UI with 200 form rows Serialized session with one UI 1,323,779 B 1,323,806 B $\color{red}\textbf{+27 B}$ (+0.0%)
Scenarios
  • Empty UI (empty-ui): A UI with no components, which is the cost of every UI.
  • UI with 200 component rows (ui-with-200-component-rows): A UI with 200 rows, each a div with a class name and a style, holding a span, an input with a value change listener and a button with a click listener.
  • UI with 200 form rows added in the latest round trip (ui-with-200-form-rows-added-in-latest-round-trip): The UI with 200 form rows, with the rows added in the latest round trip, as when navigating to a view, so that the response kept for resending holds all of them.
  • UI with 200 form rows (ui-with-200-form-rows): A UI with 200 rows, each a layout with a theme and several styles, holding a text field and a combo box bound to a bean with a binder and a button with a click listener. The fields are web components with several properties and a synchronized value, and the combo box has client callable methods, as Vaadin components that load data do.

Measured by UIMemoryFootprintTest. Differences under 10 B are ignored.

totally-not-ai Bot and others added 4 commits October 19, 2026 12:33
document.startViewTransition() runs its update callback asynchronously.
Flushing inside it while ending the request and releasing the response
lock synchronously let the client report itself idle with the old DOM
still on screen, and let a pending message flush the changes outside
the transition. Applying the changes and finishing the message
processing now both happen inside the update callback.

Also name the supported browsers that lack the View Transitions API in
the enableViewTransition() Javadoc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tes-in-view-transition

# Conflicts:
#	flow-client/src/main/frontend/internal/client/communication/MessageHandler.ts
Applying changes could finish processing twice when it threw, ending
an unrelated in-flight request. The promises of a skipped view
transition were also left unhandled, reporting an AbortError.
…tes-in-view-transition

# Conflicts:
#	flow-client/src/main/frontend/internal/client/communication/MessageHandler.ts
#	flow-client/src/test/frontend/internal/client/communication/MessageHandlerTests.ts
The browser swallows what the update callback throws and rejects the
transition's finished promise instead. Logging it with Console.error
hid the failure in production mode, unlike the same failure without a
transition. Report it through Console.reportStacktrace instead.
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

The reported field (UI.pushConfiguration) is existing code that this PR does not touch; it is flagged because the merge from main brought the file into the analysis. Leaving it out to keep this PR focused on view transitions; it is a one-word change that fits better in a separate cleanup.

@jouni

jouni commented Dec 4, 2026

Copy link
Copy Markdown
Member

@totally-not-ai I want to release this feature behind a feature flag.

UI.enableViewTransition() now throws DisabledFeatureException unless
the com.vaadin.experimental.viewTransitions feature flag is enabled,
so the API can ship as experimental. The UidlWriter test now sets the
internal flag directly since it tests response writing, not the flag
check.
@totally-not-ai

totally-not-ai Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

@jouni Done: the feature is now behind the experimental viewTransitions feature flag. It is off by default. To use it, add com.vaadin.experimental.viewTransitions=true to src/main/resources/vaadin-featureflags.properties, or turn it on in Copilot's experimental features tab. With the flag off, UI.enableViewTransition() throws a DisabledFeatureException that says how to enable it, the same way the experimental SSE push transport works. The flag is checked on each call, so no restart is needed. The flag's "more info" link points to this PR for now. If you'd like a tracking issue for feedback instead, I can open one and link it.

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Bundle size

Compared with the base commit 3a9144f.

File Minified Change Brotli Change
sw.js 20,674 B no change 6,138 B no change
VAADIN/build/ApplicationConnection.js 125,407 B $\color{red}\textbf{+356 B}$ (+0.3%) 32,339 B $\color{red}\textbf{+63 B}$ (+0.2%)
VAADIN/build/FlowBootstrap.js 3,191 B no change 1,027 B no change
VAADIN/build/FlowClient.js 10,723 B no change 3,503 B no change
VAADIN/build/frontend.js 409,532 B $\color{red}\textbf{+46 B}$ (+0.0%) 111,748 B no change
VAADIN/build/generated-flow-imports.js 2,945 B no change 1,100 B no change
VAADIN/build/indexhtml.js 47 B no change 51 B no change
VAADIN/build/rolldown-runtime.js 870 B no change 437 B no change
Total 573,389 B $\color{red}\textbf{+402 B}$ (+0.1%) 156,343 B $\color{red}\textbf{+63 B}$ (+0.0%)
Size by module, before minification
Module Change
src/main/frontend/generated/jar-resources/internal/client/communication/MessageHandler.js +859 B
src/main/frontend/generated/vaadin-featureflags.js +55 B

The production bundle of test-prod-bundle, an application with no code of its own: the Flow client, the files Flow generates and the npm packages it brings in. Brotli is what the build precompresses the files with.

@jouni

jouni commented Dec 4, 2026

Copy link
Copy Markdown
Member

If you'd like a tracking issue for feedback instead, I can open one and link it.

@totally-not-ai, yes, let's create a tracking issue for feedback.

@totally-not-ai

totally-not-ai Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

@jouni Opened #27688 as the feedback issue for the experimental feature. The viewTransitions flag's "more info" link now points there instead of this PR.

@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants