-
Notifications
You must be signed in to change notification settings - Fork 767
Container Query / Relative query to an anchor #899
Comments
@no-more I've read through the spec and the README for that package and I'm afraid I'm no closer to knowing what it is that you want. Could you provide a practical example of what this would look like as part of the Angular Layout API? |
Thanks for taking time to look at it. The objective is to create "media queries" for container, so instead of taking screen size as a reference to apply a specific rule, the idea is to apply the rule according to a parent container size. An example, I have a reusable component which display a list, and I want to make it responsive by hiding some fields (or changing flex layout) according to the size of the component itself. ngx-responsive already implemented something like this to show/hide some elements according to a container size. Here is their example:
Here it's define an anchor (named So my request is to be able to do the same sort of responsivness with flex-layout directives. |
A syntax proposition :
In this case the inner div layout would be by default This is just an example in order to try to show how it could be used. There might be a simpler syntax to achieve the same result |
Maybe an even easier way would be to use the component itself as reference |
Maybe it could be just much closer syntax to current and by default there would be a single fxContainer (a window). But when providing another container it would become the the size reference for its children, and could be provided with different breakpoints.
|
@no-more just curious as to the reasoning behind using a container for breakpoint detection instead of the viewport as is currently used, can you please provide a real world example where this is used? I do see a possible benefit of having a similar syntax but using the breakpoint detection that currently exists, for example: <div [fxLayout]="[{ value: 'row wrap', breakpoints: ['md', 'gt-lg'] }, { value: 'column', default: true }]">
<div fxFlex="5rem">…</div>
</div> This would allow breakpoint information to be built up in code and then just passed to the property (e.g. |
Hello, Viewport is screen size, now let's say I have a contact list component. And I want to use it in several place. In this example I could have defined that if my container size is About the syntax the simplest will be the best. I don't know if the feasible but something like this would be great :
I think this is quite complete and could be applied for all flex-layout directives. |
And to go even further custom breakpoints could be defined locally because a reusable component might require different breakpoints that the application it's used in. In this case breakpoints configuration could be injected locally, or could be passed as a parameter to the fxContainer:
|
With that particular example the solution would be to have the contact list component always display at almost max width of container (like the first part of your example) and then for the popup restrict the popup container to Though I do see your use case for adapting the same component to display differently when used in a popup that has a smaller container size than the viewport. In my opinion this would be a lower priority feature as there are currently different solutions available to work around these limitations. This library heavily uses and relies on the media query API, and there is not yet any support for performing media queries on elements. One possible way to implement this would be to use ResizeObserver which would essentially give us the ability to perform media queries on elements though a bit of custom processing would be required to turn a media query string into JS code that can utilize element properties. It's currently only shipped in Chrome and Opera with Firefox under development and Edge under consideration. There are polyfills available but they use @CaerusKaru thoughts? |
I've been putting this off for days now trying to come up with some way to justify this. The inherent issue here is that this is a solved problem without an intermediate solution. As @charsleysa mentioned, the perfect solution to this is The performance issues with the polyfills that are out there make them non-viable. If there's some way that I haven't considered to fold this in to the existing API without an enormous performance cost, I would be thrilled to add it to the library. Unfortunately I haven't been able to even think of a possible design so far. I'll keep this issue open in case others have ideas. I'm sorry I don't have better news. |
@CaerusKaru according to caniuse.com the After some more investigation I think using the a simple If the use of the observer is only kept to when this feature is used then it won't have an impact for existing users of the library and anyone wanting to use this feature will understand the potential performance impacts. See this codepen for a really simple |
@charsleysa Excellent job researching this. I'm not crazy about I am willing to do this: we could design an API around That just leaves the implementation and API. I'm also not a fan of the APIs suggested above because they all seem very, very verbose. The closest thing that I liked was @olaf89's |
I'm happy to see this is interesting other people. I'm aware about the resizeObserver issue, after looking in ngx-responsive I can understand how they go around it. So what they did, instead of observing directly the component size, they register to window resize event, and then check if the component size have changed. I think this works fine for 75% of users as most of the time a component will resize due to a screen resize. And at least it cover actual flex-layout behavior with an extra bonus (according to me). It could be a fallback solution that rely on resizeObserver when available, and switch to then otherwise. I'm talking for myself, but that would perfectly fill my needs. About the syntax if I transpose what I've done on ngx-responsive, it could be something like this :
This "just" require to add an other input to existing directives. If this input is an angular component then on event (from one of the observer defined above) use this size for computation instead of screen size. What do you think about it? |
With ngx-responsive and their use of the resize event, from my research I have found two reasons to not use it:
In regards to the syntax, I like what you've proposed though it could possibly be even simpler:
In regards of defining the same input, instead of requiring every directive to have an input you can use Angular's Dependency Injection to inject the instance of |
I like your proposal, its very easy to understand. About the resize event, isn't it already used by flex-layout ? I haven't checked but if flex-layout doesn't use it and use another source of even then we can use the same source. About your first argument, that's true, this solution does not respond to 100% of use case. But I really think it works for 75% of most users' needs. And when available it's still possible to switch to resizeObserver. |
This library uses the media query API (not the resize event) which only triggers when the media query match state changes so a lot more performant. This also means that it can't be used for element resize because the window could be resized multiple times without the media query match state changing. |
Thanks for the enlightenment, I didn't know this API |
So here's where I think I've landed on this. The API would be <div #parent>
<div fxLayout.relative="parent" fxLayout.xs="column" fxLayout="row">
...
</div>
</div> This would allow a terse declaration of the container while still providing us the necessary information to track the element. We would use The final concern is that this wouldn't work on SSR since Angular's DOM provider on the server has no conception of window size (or element size). That would make the elements seem different (but hopefully not radically different) on the server. This would be noted in the docs but is unfortunately a sticking point. |
Hello, some news about this awesome functionality ? |
Hi, any news about this feature? |
Feature Request
What is the desired behavior?
It would be great to go further by implementing Container Query. ngx-responsive already do something similar for adding/removing an element based on media queries or Container Queries (responsive-window feature).
Of course this might require a more complex syntax to be used, but it would be so powerfull when used with components which size varies not only according to screen size but also relative to a container size.
What is the use-case or motivation for the desired behavior?
Really responsive components that could be reused in multiple place with different size
Is there anything else we should know?
I'm quite surprise nobody asked that before. This would be a killer feature.
Thanks
The text was updated successfully, but these errors were encountered: