Skip to content

[css-pseudo-4] Using real DOM elements to proxy pseudo-elements #12575

@noamr

Description

@noamr

... OP Edited ...

This is an alternative idea to the many existing suggestions about improving access to pseudo-elements such as #12163.

The idea is that instead of figuring all kinds of different accessors to pseudo-elements, we would allow the author to create a real-DOM proxy for them, using a regular HTML element.

<menu>
  <>Menu content<>
  <span class=backdrop>
</menu>

<style>
  menu {
    .backdrop { display: pseudo-element(::backdrop); }
  }
</style>

The span element wouldn't render anything on its own, but would rather be a "reference" to a pseudo-element, the backdrop of the menu in this case.
This pseudo element is now available using querySelector or any other DOM APIs, it dispatches events in the normal way, it works with getComputedStyle and getBoundingClientRect(), and so forth. But rendering-wise, and also in terms o fUI events, it represents whatever the corresponding pseudo-element represents.

I think this would alleviate a lot of the issues raised about the pseudo-element specific JS APIs, though there are several things to be mindful around it.

Some notes:

  • We would need a way to address an element that is not the parent, because some pseudo-elements can work on self-closing elements (e.g. img or input).

This could work something like this:

<section>
  <img class=thumb>
  <span class=error>
</section>

<style>
   img.thumb::after { pseudo-name: --error; }
  .error { display: pseudo-element(--error); } 
</style>
  • Some proxy elements would represent multiple pseudo-elements, e.g. view-transition-group(*).
  • There would need to be some JS accessors that reflect which pseudo-element was the target of an event and whether the target pseudo-element exists.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Wednesday Morning

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions