-
Notifications
You must be signed in to change notification settings - Fork 774
[css-animations-2] Add trigger-scope explainer #13296
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
Open
DavMila
wants to merge
1
commit into
w3c:main
Choose a base branch
from
DavMila:trigger-scope
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| # Explainer for trigger-scope | ||
|
|
||
| This explainer is a description of the `trigger-scope` CSS property which provides a | ||
| scoping mechanism for animation trigger-related names. | ||
|
|
||
| ## Summary | ||
| Animation triggers created in CSS are given names through which those triggers | ||
| can be matched with animations. For example `timeline-trigger` declares a | ||
| timeline-based trigger which can then be matched with an animation via the | ||
| `animation-trigger` property. Here is an example: | ||
|
|
||
| ```CSS | ||
| @keyframes expand { | ||
| from { transform: scaleX(1) } | ||
| to { transform: scaleX(2) } | ||
| } | ||
|
|
||
| .source { | ||
| timeline-trigger: --trigger view() contain; | ||
| } | ||
|
|
||
| .target { | ||
| animation: expand .1s linear both; | ||
| animation-trigger: --trigger play-forwards play-backwards; | ||
| } | ||
| ``` | ||
|
|
||
| In the above example, an element with the class `target` attaches its `expand` | ||
| animation to the `timeline-trigger` named `--trigger` allowing the animation to | ||
| be played forward or backward when the appropriate scrolling occurs. | ||
|
|
||
| However, the name `--trigger` is globally visible by default. As such, | ||
| any element in the DOM that references `--trigger` in similar fashion will have | ||
| its animation attached to the same trigger object. This is useful and | ||
| might often match an author's intention but that might not always be the case. | ||
| One example is where an author intends to have elements of a | ||
| certain class behave (independently of each other) as both the `source` and | ||
| `target` in the previous example. Here is an example: | ||
|
|
||
| ```CSS | ||
| @keyframes slide-in { | ||
| from { transform: translateX(-50px) } | ||
| to { transform: translateX(0xp) } | ||
| } | ||
|
|
||
| .section { | ||
| timeline-trigger: --trigger view() contain; | ||
| animation: slide-in .1s linear both; | ||
| animation-trigger: --trigger play-forwards play-backwards; | ||
| } | ||
| ``` | ||
|
|
||
| In this example, the intention is to associate each `section` element with a | ||
| triggered animation which causes it to slide in as it comes into view within its | ||
| containing scroll port. Since the names declared by a `timeline-trigger` | ||
| declaration are globally visible, the `section` elements attach their animations | ||
| to the same trigger object. | ||
|
|
||
| ## trigger-scope | ||
| `trigger-scope` gives an author the ability to scope the name declared by a | ||
| trigger-instantiating property (e.g. `timeline-trigger`) to the subtree of the | ||
| element declaring `trigger-scope`. To fix the previous example the author would | ||
| modify their code to the following: | ||
|
|
||
| ```CSS | ||
| @keyframes slide-in { | ||
| from { transform: translateX(-50px) } | ||
| to { transform: translateX(0px) } | ||
| } | ||
|
|
||
| .section { | ||
| trigger-scope: all; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would probably be better for a canonical example to be explicit about the trigger being scoped here, i.e. |
||
| timeline-trigger: --trigger view() contain; | ||
| animation: slide-in .1s linear both; | ||
| animation-trigger: --trigger play-forwards play-backwards; | ||
| } | ||
| ``` | ||
|
|
||
| Note: it would also suffice to use `trigger-scope: --trigger` in the above example. | ||
|
|
||
| # Details | ||
|
|
||
| `timeline-scope` works similarly to [`anchor-scope`](https://drafts.csswg.org/css-anchor-position-1/#anchor-scope), | ||
| accepting `none` or `all` or a list of dashed idents. It prevents a trigger | ||
| name from being referred to by an element outside the subtree of the | ||
| element declaring `trigger-scope`. It also prevents an element within the | ||
| subtree of the element declaring `trigger-scope` from searching for a trigger | ||
| name outside the subtree. | ||
|
|
||
| `none` is the default and does not restrict the visibility of the trigger names | ||
| in the subtree of the element declaring `trigger-scope`. | ||
|
|
||
| `all` restricts the visibility of all the names in the trigger names in the | ||
| subtree. | ||
|
|
||
| ## Alternatives Considered | ||
|
|
||
| ### Make names scoped to subtree by default. | ||
| With regard to handling dashed ident names declared by CSS properties, two | ||
| models currently exist: the `timeline-scope` model and the `anchor-scope` model. | ||
|
|
||
| The `anchor-scope` model is the same as what is described above for | ||
| `trigger-scope`. The `timeline-scope` model takes the opposite approach: it | ||
| restricts the visibility of names declared by properties like `view-timeline` | ||
| and `scroll-timeline` to the subtree of the element declaring that property | ||
| (`view-timeline` or `scroll-timeline`) by default and uses `timeline-scope` to | ||
| expand the visibility of the name to the include the subtree of the element | ||
| declaring `timeline-scope`. | ||
|
|
||
| In issue [#12581](https://github.com/w3c/csswg-drafts/issues/12581#issuecomment-3206707173) | ||
| the CSS working group decided to have a consistent name scoping model and | ||
| reasoned that `anchor-scope` model was more consistent with the general pattern | ||
| of the visibility of named items in CSS. The working group also resolved to | ||
| switch `timeline-scope` over to the `anchor-scope` model. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be far more compelling to show an example where the trigger source is a different element than the one animating. In theory this sort of use case would be possible with anonymous triggers. E.g. maybe the animating element is a descendant of the trigger, or a sibling to it.