-
Notifications
You must be signed in to change notification settings - Fork 715
How to handle addEventListener on CSSPseudoElement
?
#12163
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 think that invoking the listener on pseudo-elements before the entire standard dispatch is very weird. I would expect:
It's still kinda weird that non-bubbling events will still bubble from the actual pseudo-element target to the ultimate originating element target. |
Agree, I think the best would be to "Add event.pseudoElement as in KeyframeEffect for Web Animations" as it both covers the use case and doesn't introduce any new confusing constructions, what do you think? |
Yes, that approach can be less confusing. But maybe I would name it |
oh ofc, my naming was just for describing the approach, the naming can be discussed later. |
Uh oh!
There was an error while loading. Please reload this page.
Full context: https://github.com/danielsakhapov/CSSPseudoElementDoc?tab=readme-ov-file#41-addeventlistener-on-a-proxyhandle
Description:
Given that the CSSPseudoElement object acts as a persistent handle, attaching an event listener via addEventListener raises further questions.
This would require some heavy discussions, as it’s already known that authors want to be able to count clicks on ::scroll-marker pseudo elements for analytics purposes.
Potential solutions on example of
::scroll-marker
:addEventListener('click',...)
directly on theCSSPseudoElement
for::scroll-marker
. Clicks physically hitting the marker would continue to dispatch withevent.target
set to the originatingElement
. Developers would have to attach the listener to theElement
and use coordinate-based hit-testing within that listener to infer if the click landed on the marker area.event.target
.CSSPseudoElement
object less useful and doesn't leverage itsEventTarget
inheritance.scrollMarkerPseudoElement.addEventListener('click', counterFunction)
. When a click physically occurs on the scroll marker:::scroll-marker
and invokescounterFunction
(and any other listeners attached directly to this specificCSSPseudoElement
handle).event.target
is set to the originating scrollableElement
. The event then proceeds through the capture and bubble phases relative to theElement
, without re-invokingcounterFunction
during this standard flow.event.target
remains theElement
during the standard, observable event flow.EventTarget
inheritance onCSSPseudoElement
.addEventListener
on the pseudo) is intuitive.click
event, define a new event type likepseudoElementClick
that only fires on theCSSPseudoElement
handle for pseudo elements. Standardclick
events would continue to target theElement
only.click
event dispatch.click
first and be confused why it doesn't work directly on the pseudo-element handle without the special dispatch phase.mouseover
) should interact with pseudo-elements if direct listeners are desired.event.pseudoElement
as inKeyframeEffect
for Web Animations:pseudoElement
parameter as eitherCSSPseudoElement
orstring
toevent
which will be set if the event was fired from the pseudo element. Allowing callingaddEventListener
onCSSPseudoElement
can even be optional here.pseudoElement
is represented asstring
.Recommendation:
Add
event.pseudoElement
as in KeyframeEffect for Web Animations.Justification: This option provides the best balance. It directly enables the desired developer use case (listening for clicks on the scroll marker) with good ergonomics (
addEventListener
on the pseudo element object). Crucially, it achieves this while adhering to the non-negotiable web compatibility requirement thatevent.target
for standard events likeclick
must remain the originatingElement
.The text was updated successfully, but these errors were encountered: