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

[css-contain] Similar to window.matchMedia(), Container Queries should have a similar method #6205

Open
clairebaire opened this issue Apr 11, 2021 · 14 comments

Comments

@clairebaire
Copy link

CSS Media Queries have a method on the Window interface (window.matchMedia()) giving a script the ability to listen for media query changes. I propose that CSS Container Queries has the same functionality. However, I think this introduces some complexity, and therefore I'd like to present two thoughts for consideration:

Proposal 1

A new method on the HTMLElement interface possibly called matchContainer() or something similar. Reason it wouldn't be on the Window is due to the nature of container queries - not being based upon viewport size, but size of the container. However, this should be influenced by the question still pending, which is what is the default behavior of a container query when there is no contain property on any parent element.

Proposal 2

A new Observer of sorts, similar to ResizeObserver or MutationObserver where it's a bit more clear that it is listening for any kind of container change, and that it relays the container as a member of a ContainerQueryList. This would make it not pollute the HTMLElement interface, and instead be a DOM method. This would solely be used to identify when a container query is activated, and which items are affected by that activation.

Admittedly, the second proposal seems very similar to what ResizeObserver does already, but I feel it misses the connection that CSS Contain Level 3 is attempting to solve. This could be something that might be added to ResizeObserver, but might also be its own API.

Thoughts?

@andruud
Copy link
Member

andruud commented May 3, 2021

but I feel it misses the connection that CSS Contain Level 3 is attempting to solve

Why isn't the existing ResizeObserver (as-is) good enough to listen for resize events on containers?

@mirisuzanne mirisuzanne moved this from To do to In progress in Container Queries [css-contain] Sep 21, 2021
@astearns astearns moved this from Second to Contain in EUR Sep 29 2021 TPAC Meeting Sep 27, 2021
@css-meeting-bot
Copy link
Member

The CSS Working Group just discussed Similar to window.matchMedia(), Container Queries should have a similar method, and agreed to the following:

  • RESOLVED: Add a matchContainer method as proposed in issue. Keep issue open to decide if need new observer or if we can reuse the existing
The full IRC log of that discussion <dael> Topic: Similar to window.matchMedia(), Container Queries should have a similar method
<dael> github: https://github.com//issues/6205
<dael> miriam: Idea is have something similar for container queries. Claire (sp?) has provided a few options. Would be great to resolve on one. Or something else
<dael> astearns: 3 options: new method. new observer. reuse resize observer
<dael> fantasai: Possible use cases for both approaches. Could start with defining matchContainer since that's easy
<dael> astearns: You're thinking we would need matchContainer and either new observer or reuse resize?
<dael> fantasai: Yeah
<dael> astearns: Anyone with opinions about adding matchContainer?
<dael> TabAtkins: This would allow style queries in the matchContainer since they're under container query umbrella?
<dael> fantasai: Yeah
<dael> TabAtkins: Nice. Good
<dael> astearns: Prop: Add a matchContainer method as proposed in issue. Keep issue open to decide if need new observer or if we can reuse the existing
<dael> astearns: Objections?
<dael> RESOLVED: Add a matchContainer method as proposed in issue. Keep issue open to decide if need new observer or if we can reuse the existing
<fremy> FWIW I will not join the second call, I am going to bed, I think most issues I was involved in have been discussed already, or I left a comment with my thoughts. Talk to you all next week!

@jogibear9988
Copy link

Is there anything new about this? We created a Designer with CSS parsing support (https://github.com/node-projects/web-component-designer) where we would need the "matchContainer" API to find the mathcing container queries

@seedee
Copy link

seedee commented Jun 6, 2023

Wouldn't just checking a container element's offsetWidth when the window resizes be functionally the same as container query?

@jogibear9988
Copy link

Wouldn't just checking a container element's offsetWidth when the window resizes be functionally the same as container query?

no? container queries could not only check for a size....

@aradalvand
Copy link

I expected this to have already been implemented, such a shame.

@Loque-
Copy link

Loque- commented Aug 18, 2023

Just wanted to add that I ran into a use case where I wanted to be able to do;

window.matchContainer('(aspect-ratio < 16 / 9)')

Would have loved to be able to do this instead of writing some custom ResizeObserver which could potentially lead to performance issues if not managed mindful of re-paints for calculating/retrieving values.

@xamir82
Copy link

xamir82 commented Dec 13, 2023

No movement on this yet? No offense but do we have to wait another 10 years for something as basic as this again?

@robrez
Copy link

robrez commented Feb 28, 2024

Some experimenting with learning about container-query changes via JS

  • MutationObserver
    • Not possible, CSS cannot cause mutations to be observed; Thought perhaps pseudo-elements might work.. they don't.
  • IntersectionObserver
  • ResizeObserver
    • Viable by toggling any css property. I'm unsure if the CSS engine will always run prior to the resizeObserver callback
    • Example: https://codepen.io/robrez/pen/yLrLyKE
    • Note that many would just use the ResizeObserver w/o any interaction w/ an underlying container query css policy... but the point of this thread is to have something similar to mediaMatch. The things you can do in CSS might be tricky to otherwise detect in JS
    • Note also that Element.matches seems like a reasonable instictive "let me try this"... but it will not tolerate any container query

@jogibear9988
Copy link

jogibear9988 commented May 18, 2024

Browser Bugs:

https://issues.chromium.org/issues/40887402
https://bugzilla.mozilla.org/show_bug.cgi?id=1897594
https://bugs.webkit.org/show_bug.cgi?id=274347

@jogibear9988
Copy link

@rejhgadellaa
Copy link

I just ran into a case where I'd like to watch changes to a css variable using container style queries:

const match = document.body.matchContainer( 'style( --theme: dark )' );
match.addEventListener( () => { /* .... */ } );

@lukewarlow
Copy link
Member

lukewarlow commented Jun 19, 2024

It would be good to get clarification on if this is intended to be in HTMLElement or Element? I would imagine Element so it works with SVG and MathML?

Edit: the spec note itself says Elements but this issue and some of the browser issues say HTMLElement

@jogibear9988
Copy link

For sure it needs to be Element. Every Node wich is styleable via CSS and respects ContainerQueries should have this method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests