Use Amp List for showing more content when height is not available
We want the user to be able to load a amp-list of more comments after the initial page load. We don't know the height of these comments because they can be any length. We can't use the overflow button because we already have our own overflow button whose source is updated on amp-bind.
<script type="application/json">
{
"moreCommentsSrc": "/json/comments2.json"
}
</script>
</amp-state>
<amp-list class="mt1" width="auto" height="600" [height]="comments.moreCommentsSrc == '/json/comments2.json' ? 600 : 1000" layout="fixed-height"
src="/json/comments2.json"
[src]="comments.moreCommentsSrc"
>
<template type="amp-mustache">
<div class="flex px1 pt1 depth-{{depth}}">
<amp-img width="44" height="44" class="mr1 flex-none" alt="user" src="/img/ic_account_box_black_48dp_1x.png"></amp-img>
<div class="ampstart-card p1 comment">
<p><span class="user">{{user}}</span> <span class="date">{{datetime}}</span></p>
<p>{{text}}</p>
</div>
</div>
</template>
</amp-list>
<button class="ampstart-btn caps m1 mb3 show" [class]="comments.moreCommentsSrc == '/json/comments2.json' ? 'ampstart-btn caps m1 mb3 show' : 'ampstart-btn caps m1 mb3 hide'" on="tap:AMP.setState({comments: {moreCommentsSrc: '/json/comments3.json'}})">Show more</button>```
/to @ericlindley-g @aghassemi
- @choumx
Thanks @sffriend ! @choumx, I think this is somewhat similar to the request for resizing when updated through amp-bind, except it's done on the first page load. Would it be possible for amp-list to have container layout for its contents, or is that too risky for page jumps on first load?
Sorry for the late response!
Would it be possible for amp-list to have container layout for its contents, or is that too risky for page jumps on first load?
Yea I'd think that's a nonstarter due to content jumping.
We can't use the overflow button because we already have our own overflow button whose source is updated on amp-bind.
Why not? I can imagine having a "view all comments" overflow button and a "next page" pagination button. Not as elegant as having a single button but avoids content jumping.
Thanks @choumx ! Can you add detail on why content jumping is higher risk in this case than in, say, the case where it resizes based on how many items are returned from the server call?
Maybe I'm missing something. The latter requires user action while the former doesn't, right?
Ah, sorry — momentarily forgot how amp-list worked, so you can disregard my question
@choumx The idea of using 2 buttons doesn't help to keep the current Reddit experience the same. Currently the mobile page shows, at the bottom: "More comments (number of comment)" and there is no pagination in place. I think having 2 buttons would be confusing.
@kul3r4 If an amp-list is at the bottom of the page, resizing should be automatic (overflow button won't be displayed).
@sffriend Can you provide more details on your exact use case? E.g. a sample page.
I was looking at this page on mobile, it's true that the amp-list would be at the bottom of the page which makes things easier @sffriend would you like to take a second look at this?
@choumx
If an amp-list is at the bottom of the page, resizing should be automatic (overflow button won't be displayed).
I also read this in the documentation. But I can't get this to actually happen. I have a test page with just an amp-list inside the body tag. But I always get the overflow button. Is there anything specific needed to get this to work?
@devanes Could you share the test page?
@kul3r4
https://bronsvuur.nl/test/test.html
Note that I set the height of each item to a higher value to make the issue more visible. I can remove this and just add more content or reduce the height of the amp-list to get the same result.
The problem is that your list is not below the first viewport. Take a look at this:
- Load the page the first time and then scroll to the bottom. At the bottom there is an example of overflow button and the button doesn't appear
- Then, without scrolling to the top, reload the page and you will see the button appearing
This is because amp-list will automatically take more space if required (and available) but at the same time it has to be below the first viewport to avoid reflow while the page is loading.
We just published a new sample showing how to implement a load more button
@kul3r4 That looks interesting. At the moment I have implemented it in a way where using amp-bind the entire contents of the amp-list is replaced with the new batch combined with a scroll to the top of the list, basically previous/next pagination without reload.
@devanes I see, this was the previous approach in the sample as well, but with this new approach you only load the newer items.
Highly looking forward the <amp-list> to support the scroll event and automatically triggering the request for more items.
@grahamle We are planning to prioritize this work. Please subscribe to https://github.com/ampproject/amphtml/issues/9423 for updates.