Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[css-view-transitions-1] Defer restoring persisted state until after capturing old state #9676

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

noamr
Copy link
Collaborator

@noamr noamr commented Dec 4, 2023

This fixes an issue that the spec restores the persisted state (e.g. scroll position) too eagerly, e.g. when calling document.startViewTransition inside a popstate handler, not giving the UA an opportunity to first capture the old state.

This exports an algorithm that should be called from the HTML spec when ready to automatically restore persisted state, e.g. after popstate. If the algorithm returns true, proceed as normal, otherwise the state will be restored after capturing the old state.

@noamr noamr requested a review from vmpstr December 4, 2023 20:49
noamr added a commit to noamr/html that referenced this pull request Dec 15, 2023
When starting a view transition right before state restoration,
e.g. `addEventListener("popstate", () => startViewTransition(() => {...}))`,
according to the current spec the scroll position would be restored
before the `startViewTransition` callback is called, which means that
it would be captured as part of the old state, even though it belongs
to the new state.

This change makes it so that we defer persisted state restoration
until after the old state is captured.

Together with w3c/csswg-drafts#9676
@@ -47,6 +47,9 @@ spec:css-display-4; type: dfn; text:invisible;
urlPrefix: https://wicg.github.io/navigation-api/; type: interface;
text: NavigateEvent
text: signal; for: NavigateEvent; url: #ref-for-dom-navigateevent-signal①
urlPrefix: https://html.spec.whatwg.org/multipage/; spec: html; type: dfn;
text: restore persisted state; url: #restore-persisted-state
text: latest entry; url: #latest-entry
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of poking into HTML's externals, try to get whatwg/html#9996 to export the appropriate parts.

(I'm a bit scared of exporting "latest entry" though, since it's a pretty low-level detail... document's node navigable's active session history entry would be a more public API. Maybe it's the same thing in this case?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

Copy link
Member

@khushalsagar khushalsagar left a comment

Choose a reason for hiding this comment

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

Do you have the html spec PR which will use this? Would help to review that together with this.

1. "`pending`".
1. "`immediate`".

Note: this is used to defer automatic restoration of persisted state, e.g. scroll position,
Copy link
Member

Choose a reason for hiding this comment

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

Can you add what immediate implies as well?

@@ -1252,10 +1267,16 @@ urlPrefix: https://wicg.github.io/navigation-api/; type: interface;
If failure is returned, then [=skip the view transition=] for |transition| with an "{{InvalidStateError}}" {{DOMException}} in |transition|'s [=relevant Realm=],
and return.

1. [=Resolve persisted state restoration=] for |transition|.
Copy link
Member

Choose a reason for hiding this comment

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

We should do this in the skip case as well.

@@ -1094,6 +1097,18 @@ urlPrefix: https://wicg.github.io/navigation-api/; type: interface;
Initially null.

Note: this is used for cross-document view transitions.

: <dfn>persisted state restoration</dfn>
:: One of the following, initially "`defer`":
Copy link
Member

Choose a reason for hiding this comment

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

Do we have scroll restoration in the spec for cross-document navigations?

@noamr
Copy link
Collaborator Author

noamr commented Jan 11, 2024

Do you have the html spec PR which will use this? Would help to review that together with this.

whatwg/html#9996

@@ -1940,6 +1978,7 @@ Changes from <a href="https://www.w3.org/TR/2023/WD-css-view-transitions-1-20230
* Copy `color-scheme` from DOM element to ''::view-transition-group()''. See <a href="https://github.com/w3c/csswg-drafts/issues/9276">issue 9276</a>.
* Expose [=auto-skip view transition=] for a {{Document}}, to allow having outbound cross-document transitions preceed programmatic view transiitons. see <a href="https://github.com/w3c/csswg-drafts/issues/9512">issue 9512</a>.
* Add a note about why 'view-transition-name' should be animatable.
* Export an algorithm and add mechanism to defer restoring scroll/persisted state.
Copy link
Member

Choose a reason for hiding this comment

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

Is there a link to an html or csswg issue where this was discussed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants