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

API to get top layer elements #9075

Open
sanajaved7 opened this issue Mar 27, 2023 · 7 comments
Open

API to get top layer elements #9075

sanajaved7 opened this issue Mar 27, 2023 · 7 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest

Comments

@sanajaved7
Copy link

The OddBird team is currently building a polyfill for CSS anchor positioning.

To support anchoring top layer elements to each other, it would be very helpful to have a programmatic way to get the order of the elements in the top layer set. This is particularly useful to prevent a top layer target element from anchoring to a succeeding top layer anchor element. See this WPT for an example.

This issue is similar to #8783 but we're interested in all the elements of the top layer set to access their order.

cc: @jgerigmeyer @mfreed7

@annevk
Copy link
Member

annevk commented Mar 27, 2023

This is an interesting suggestion, but the bar for adding an API is higher. We don't add public API surface just for tests either, for instance.

cc @nt1m

@annevk annevk added addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest labels Mar 27, 2023
@nt1m
Copy link
Member

nt1m commented Mar 27, 2023

I'm rather opposed to this, I think something being in the top layer or not should remain an implementation detail of the individual APIs using it.

@sanajaved7
Copy link
Author

@nt1m to clarify a bit further, my thinking is that if browsers are (or will be soon) allowing ways to inspect the top layer and the order of the elements in the top layer, it would be great to have a way to get that same data with something like getTopLayerElements.

@mfreed7
Copy link
Collaborator

mfreed7 commented Mar 27, 2023

I could see non-test use cases also, such as a popover component wanting to make sure it's topmost, and if not, closing and re-showing the popover to put it on top. You wouldn't want to do that unless you knew it was necessary, to avoid flickering and starting animations and such.

One constraint that I think should be a hard constraint, if we add such an API: don't reveal elements in the top layer. You can instead "ask" whether one top layer element is above another, such as:

element1.isAboveInTopLayer(element2);

so that you'd have to already have references to element1 and element2. Otherwise this would be an easy place to accidentally reveal shadow-contained top layer elements, for example.

@Link2Twenty
Copy link

such as a popover component wanting to make sure it's topmost, and if not, closing and re-showing the popover to put it on top.

This is exactly the use case I was looking to solve when I found my way here. Ideally a toast container should be able to listen for a top-layer event on the document, check to see if it is still at the topmost layer and do some action depending on the result.

@emilio
Copy link
Contributor

emilio commented Mar 11, 2024

Something that would perhaps be a bit more consistent with existing APIs would be something like DocumentOrShadowRoot.topmostTopLayerElement (or something, naming TBD), where the API would appropriately re-target much like fullscreenElement or activeElement do.

Would that help with the use case above? Or is there something that really needs "all the elements in the top layer, in order"?

We fire popover toggle events and fullscreen events. I don't recall, does <dialog> fire relevant events too? It seems with that you could track the top layer as well.

@keithamus
Copy link
Contributor

Dialog doesn't fire toggle events (see #9733) but you can observe the open attribute and check .matches(':modal') to determine if it is now on the top layer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest
Development

No branches or pull requests

7 participants