amp-list: Support dynamic resizing
I think this is not currently possible, but would be worth double-checking
/cc @choumx — WDYT about this as a feature for amp-list + amp-bind?
EDIT: attempting to summarize the discussion below into specifications for this feature request.
Dynamic resizing needs to cover the following cases:
- Users interacting with the content inside the list can cause the contents to change size:
a) Opening / closing an
<amp-accordion>. b) Class toggles from<amp-bind>. - Using
amp-bindto change thesrcattribute of<amp-list>can cause us to fetch contents that will have a different height from<amp-list>'s current height. - It is possible for
<amp-list>to contain elements that will be loaded later. E.g. collapsible ads.<amp-list>should intelligently adjust its height based on the actual height of its contents. - After applying filters,
<amp-list>might contain fewer elements and would hence need to resize.
Caveats to note:
- Should not resize list when list bottom is in viewport for UX reasons.
- Should make sure to test
<amp-list>in<amp-sidebar>use case. - Should not resize if we scrolled past the
<amp-list>.
Tentative proposed solution(s):
- Set
<amp-list>to behave like layoutCONTAINERafter first load. - Allow automatic resizing when [state] or [src] bindings change, perhaps toggled via a new boolean attribute
resizeOnChange.
are width, height, layout not bindable? seems useful regardless of use-case with amp-list.
I believe width and height are bindable — not sure about layout, though: do you have a use case in mind for that?
This would be super useful to have. We're building a product listing page (an ampStart template) and would like to update the products based on an input select change. Unfortunately, the height of the amp-list element doesn't get updated.
Simple example here https://gurmukh-test.firebaseapp.com/templates/e-commerce/product-listing.amp
@ericlindley-g could this be prioritised?
@aghassemi one wrinkle for this request is that amp-list would have to resize based on the data pulled in, so in some cases this wouldn't be known by the client until the data is rendered by the template
@choumx — @gurmukhp is working on AMP Start templates, and the ability for amp-list to resize when src is updated by amp-bind would be useful there (product listing page with filtering capabilities which vary the number of products displayed via amp-list, using amp-bind). Is this something you've thought about before, or would be easy to fold into the other amp-list work you're looking at? @gurmukhp and team are working on templates that we're hoping to launch soon, and it would be ideal not to have to design around this issue at launch.
Just verified that width and height bindings work in amp-list on a local example. layout currently isn't bindable.
@gurmukhp The amp-list element in that page doesn't appear to have a width/height binding.
@choumx — does this allow amp-list to resize based on the content pulled in via JSON, as opposed to a specific height pre-determined on the client?
For instance: I give users the ability to filter by price and color of a product, but I'm not able to hard-code on the frontend the exact height for each price/color combination. What I would need would be for amp-list to dynamically resize based on the height of the items it pulls in through JSON (which would also accommodate for elements (like text) that change height based on viewport width. Is that currently supported?
Yes, by using amp-state as an intermediary store. Just tested something like this locally:
<amp-state id="myData" [src]="'http://data.com/json?filter=' + myFilterVar">
<script type="application/json">
{
"items": [] // Insert default data here.
}
</script>
</amp-state>
<button on="tap:AMP.setState({myFilterVar: 'red'})">Set filter to 'red'</button>
<!-- "30" is the height per item. -->
<amp-list [state]="myData.items" [height]="myData.items.length * 30"></amp-list>
Probably not immediately obvious for people new to amp-bind. Good candidate for ABE. 😄
Nice! I'm guessing this doesn't work for variable-height items like text (which could wrap and cause new lines depending on viewport width)—is that right? Either way, this is super-useful
If this doesn't work for variable-height items: would it be possible for amp-list to understand the height of its children and resize based on that when amp-bind changes its src?
Correct, we'd have to allow amp-bind to change layout to "container". It'd be a simpler solution than the one above. Seems like a reasonable FR.
Awesome — changing the title of the bug to reflect the request.
This feature would be really useful in the e-commerce template for AmpStart. We have a list of products that are loaded in dynamically. The height of amp-list is dependant on the width of the device so binding using the [height] attribute isn't possible.
Thanks for the feedback.
On second thought, adding a new element-specific action called "resize" might be better than allowing binding to layout property. For one, amp-bind doesn't do anything when the property value doesn't change, e.g. [layout] changing from "container" to "container" even if [src] changes.
<amp-list [src]="myUrlVariable" id="foo" on="change:foo.resize"></amp-list>
An even simpler way is to allow automatic resizing when [state] or [src] bindings change, perhaps toggled via a new boolean attribute:
<amp-list resizeOnChange [src]="myUrlVariable"></amp-list>
Good conversation thread with @aghassemi and @dvoytenko in duped issue: https://github.com/ampproject/amphtml/issues/11535#issuecomment-333971409
Any updates here?
This is a blocker for launching a top site. SImilar to https://github.com/ampproject/amphtml/issues/10230#issuecomment-318026363 they're an ecommerce company and the sidebar is showing categories. Categories show subcategories on click. And when those are shown the sidebar is stuck on overflow: none;
Like https://github.com/ampproject/amphtml/issues/11535 that was dupped 3 weeks ago the logical quick solution is to add overflow: auto, but that's not allowed.
@choumx , for the on-load case, where the user hasn't interacted with the page, what do you think about just queuing the resize and executing it when it can be done with good UX? (i.e. If the user isn't scrolling: if the bottom of amp-list is in the viewport, wait until it isn't anymore, then resize and correct scroll if the viewport is below amp-list. If the bottom of amp-list is not in the viewport, then just resize at will. )
That should work and is probably how resize requests ought to work in the first place.
OOC, is that a valuable use case? I.e. users scrolling past an amp-list and then scrolling back up to it.
Excellent—excited to have a solution for the on-load case! (Though I should double-check: could this conditional resize work with container layout (or a similar notion), so amp-list effectively has container layout, as long as it ensures the good resizing UX described above?)
To answer your question, I'm not aware of cases where users are scrolling past amp-list and then scrolling back up to it—I mention that more as an edge case that I think we'd have to handle if we wanted amp-list to resize automatically (Say, if the user had already visited the page and wanted to scroll quickly down to content they had seen already.)
I think the most likely scenario (the primary use case) is that the resize happens while the user is consuming content above.
FWIW: As a naive though unfamiliar with the implementation path either for this or for the resize on user action we've been talking about: I wonder if the resize that we want to allow on user interaction through bind could just be a rule added to the above ruleset, so the resize logic is all in one place.
The resize logic is part of the runtime and used by other extensions too: amp-ad, amp-facebook-like, amp-imgur, etc. The logic is quite complex -- I'll have to take a closer look.
Would you mind filing another issue for this? Something like "More flexible height-change requests."
Since a relatively simple issue (#11535) was dup'ed into this more involved one, is there a way to handle the overflow use case without all the complexity?
Specifically, AMP sets overflow: hidden !important on the list. While this prevents resizing, so would overflow: scroll. Could there be an option to opt in to overflow scroll in rather than overflow hidden?
@ericlindley-g might be able to give more context, but I believe scrollbars are not allowed since generally they're pretty bad UX:
No user defined element in an AMP document may have a scrollbar.
https://github.com/ampproject/amphtml/blob/master/spec/amp-html-format.md#properties
Have you tried using an overflow button for your use case?