Skip to content

[LiveComponent] New placeholder macro to generate defer/lazy skeleton #1532

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

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function onPreRender(PreRenderEvent $event): void
return;
}

$componentTemplate = $event->getMetadata()->getTemplate();
$event->setTemplate('@LiveComponent/deferred.html.twig');

$variables = $event->getVariables();
Expand All @@ -66,6 +67,8 @@ public function onPreRender(PreRenderEvent $event): void
$variables['loadingTag'] = $mountedComponent->getExtraMetadata('loading-tag');
}

$variables['componentTemplate'] = $componentTemplate;

$event->setVariables($variables);
}

Expand Down
2 changes: 1 addition & 1 deletion src/LiveComponent/src/Test/TestLiveComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(
private UrlGeneratorInterface $router,
) {
$this->client->catchExceptions(false);
$this->data['attributes']['data-live-id'] ??= 'in-a-real-scenario-it-would-already-have-one---provide-one-yourself-if-needed';
$this->data['attributes']['id'] ??= 'in-a-real-scenario-it-would-already-have-one---provide-one-yourself-if-needed';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like I missed this one :)

}

public function render(): RenderedComponent
Expand Down
5 changes: 5 additions & 0 deletions src/LiveComponent/templates/deferred.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
{% block loadingContent %}
{% if loadingTemplate != null %}
{{ include(loadingTemplate) }}
{% else %}
{% from componentTemplate import placeholder %}
{% if placeholder is defined %}
{{ placeholder(__props|default) }}
{% endif %}
{% endif %}
{% endblock %}
</{{ loadingTag }}>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent('deferred_component_with_placeholder', method: 'get')]
final class DeferredComponentWithPlaceholder
{
use DefaultActionTrait;

#[LiveProp]
public int $rows = 6;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div {{ attributes }}>
{# for row in this.rows ... #}
</div>

{% macro placeholder(props) %}
{%- for i in 1..props.rows -%}
<span class="loading-row"></span>
{%- endfor -%}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<twig:deferred_component_with_placeholder rows="2" />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<twig:deferred_component_with_placeholder rows="2" defer />
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ public function testItIncludesGivenTemplateWhileLoadingDeferredComponent(): void
$this->assertStringContainsString('Long awaited data', $div->html());
}

public function testItIncludesComponentTemplateBlockAsPlaceholder(): void
{
$div = $this->browser()
->visit('/render-template/render_deferred_component_with_placeholder')
->assertSuccessful()
->crawler()
->filter('div');

$this->assertSame('<span class="loading-row"></span><span class="loading-row"></span>', trim($div->html()));
}

public function testItDoesNotIncludesPlaceholderWhenRendered(): void
{
$div = $this->browser()
->visit('/render-template/render_component_with_placeholder')
->assertSuccessful()
->crawler();

$this->assertStringNotContainsString('<span class="loading-row">', $div->html());
}

public function testItAllowsToSetCustomLoadingHtmlTag(): void
{
$crawler = $this->browser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function testItUsesKeysToRenderChildrenLiveIds(): void
->assertSuccessful()
->assertHtml()
->assertElementCount('ul li', 3)
// check for the live-id we expect based on the key
// check for the id we expect based on the key
->assertContains('id="live-521026374-the-key0"')
->assertNotContains('key="the-key0"')
->visit($urlWithChangedFingerprints)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\TwigComponent\Tests\Fixtures\User;
use Twig\Environment;
use Twig\Error\RuntimeError;

/**
* @author Kevin Bond <[email protected]>
Expand Down