Infinite Scroll - 1/2
Infinite scroll allows users to continuously load content as they scroll down the page.
Part One of this demo shows how to append new items to the page with a LiveComponent.
So... how does it work?
Click Load More to see another batch appear while the cards already on the page stay in place.
Request the next batch
The current page is a LiveProp. getItems() returns its 12 products, while hasMore() decides whether another batch is available.
Clicking Load More calls the more LiveAction, increments the page number, and renders the component again.
// ... use statements hidden - click to show
#[AsLiveComponent('ProductGrid')]
final class ProductGrid
{
use ComponentToolsTrait;
use DefaultActionTrait;
private const PER_PAGE = 12;
#[LiveProp]
public int $page = 1;
public function __construct(private readonly EmojiCollection $emojis)
{
}
#[LiveAction]
public function more(): void
{
++$this->page;
}
public function hasMore(): bool
{
return \count($this->emojis) > ($this->page * self::PER_PAGE);
}
/**
* @return list<array{id: int, emoji: string, color: string}>
*/
public function getItems(): array
{
$emojis = $this->emojis->paginate($this->page, self::PER_PAGE);
$colors = $this->getColors();
$items = [];
foreach ($emojis as $i => $emoji) {
$items[] = [
'id' => $id = ($this->page - 1) * self::PER_PAGE + $i,
'emoji' => $emoji,
'color' => $colors[$id % \count($colors)],
];
}
return $items;
}
/**
* @return list<string>
*/
public function getColors(): array
{
return [
'#fbf8cc', '#fde4cf', '#ffcfd2',
'#f1c0e8', '#cfbaf0', '#a3c4f3',
'#90dbf4', '#8eecf5', '#98f5e1',
'#b9fbc0', '#b9fbc0', '#ffc9c9',
'#d7ffc9', '#c9fffb',
];
}
}
Keep earlier cards in place
Each batch is wrapped in a ProductGrid_page element marked data-live-ignore. When the page number advances, Twig renders an empty placeholder with the previous page's ID.
LiveComponent matches that placeholder to the existing page element and leaves its cards untouched, so the new batch follows them in the grid.
<div class="ProductGrid" {{ attributes }}>
<div id="results" style="display: flex; gap: 1rem; flex-direction: column;" class="p-6">
<div class="ProductGrid_items">
{% if page > 1 %}
{# 🦊 #}
{# Adding a fake "previous page" div is enough to trick the system #}
{# It must have the same ID than the original page #}
<div class="ProductGrid_page" id="page--{{ page - 1 }}" data-live-ignore="true"></div>
{% endif %}
{# Current page #}
<div class="ProductGrid_page" id="page--{{ page }}" data-live-ignore="true">
{% for item in this.items %}
<article class="ProductGrid_item" style="--color: {{ item.color }};">
<div class="ProductGrid_media">
<svg><use href="#svg-tshirt"/></svg>
<span>{{ item.emoji }}</span>
</div>
<data class="ProductGrid_price" value="{{ item.id }}" data-currency="$">
{{ item.id + 1 }}<small>.99</small>
</data>
</article>
{% endfor %}
</div>
</div>
<div style="display: grid; place-content: center;padding-block: 2rem;">
{% if this.hasMore %}
<button data-action="live#action" data-live-action-param="more" class="ProductGrid_more">
Load More
</button>
{% else %}
<span class="code">The End</span>
{% endif %}
</div>
</div>
</div>