-
Notifications
You must be signed in to change notification settings - Fork 715
[css-pseudo] Can we make pseudo-elements first-class citizens in the DOM? #11559
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
Comments
I don't think they can completely become first-class citizens in the DOM. Because in events, the
In some cases you can use I don't think
You don't need to hijack, you can animate directly, this works on browsers: <!DOCTYPE html>
<style>div::before { content: "<::before>" }</style>
<div>text</div>
<script>
new Animation(new KeyframeEffect(
document.querySelector("div"),
[{ color: "blue" }, { color: "red" }],
{
duration: 2000,
direction: "alternate",
iterations: "Infinity",
pseudoElement: "::before",
},
), document.timeline).play();
</script>
It's specced (but seems unimplemented): https://drafts.csswg.org/web-animations-1/#dictdef-getanimationsoptions element.getAnimations({pseudoElement: "::before"}) |
But [
The
Good call. My assumption is that it would return
🤦♂️ Duh, of course … and to say I even created a demo that uses it the very same day I posted this. Sorry, my bad!
My proposal is to allow |
This would add support for document.addEventListener("mousemove", e => console.log(e.target)) When you move the mouse over Turning |
IUC the |
/cc @danielsakhapov as you are working on https://github.com/danielsakhapov/CSSPseudoElementDoc |
Fun with Pseudos
In light of View Transitions I’ve been playing a lot with pseudo-elements trying to do all sorts of things with them from within JavaScript:
Accessing the pseudos themselves is in theory possible through
Element.pseudo()
but in practice it has no browser support1.Some of the other features I listed above are made possible in some APIs through means of the
pseudoElement
option. When working with these it becomes quickly pretty clear that thispseudoElement
is an afterthought that was introduced in order to get it to work quickly, instead of fully adopting the existence ofCSSPseudoElement
. See #4301 for example that chose to add thepseudoElement
option toElement.animate
instead of embracingCSSPseudoElement
.Since then other APIs have also been monkey-patched to accept a
pseudoElement
option:getComputedStyle
in ???Element.getAnimations()
in [view-transitions-2] Add method to get all animations of a ViewTransition object #9908 (comment)So as for the things I want to do with pseudos this is the current state of affairs:
pseudo()
but that has not shipped anywhere.Element.getAnimations()
but that is not specced yet. You can sniff ’m manually, though.getComputedStyle()
withpseudoElt
param.With more and more features leaning on pseudo-elements (select::picker, ::details-content, carousel pseudos, customizable select, etc.) this problem will become more visible to authors.
Making pseudo-elements first-class citizens
Instead of monkey-patching more APIs to accept a
pseudoElement
, I believe it would be nicer if pseudo-elements becamse first-class citizens of the DOM. All the pieces of the puzzle are there, it’s a matter of putting them together:pseudo()
which returns aCSSPseudoElement
Element
from cssom-view also toCSSPseudoElement
CSSPseudoElement
is already defined to include theGeometryUtils
mixin.CSSPseudoElement
include theAnimatable
mixin, similar toElement
.Animatable
mixin from the previous point.pseudoElement
option fromgetAnimations()
(which is safe to do because this is not specced + has not shipped anywhere)getComputedStyle()
withpseudoElt
param, havegetComputedStyle
also accept aCSSPseudoElement
as its first argument.Just a starting point
Most likely I’m overlooking a bunch of things and I’m definitely lacking some nuances to some of the many pseudos that would allow/disallow this – so I’m very much looking forward to input on this one.
Footnotes
Thankfully I didn’t really need the pseudos directly in my experiments. Because I only needed the animations+effects applied to them, I was able to sniff them out through
document.getAnimations()
↩The text was updated successfully, but these errors were encountered: