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

Conversation

smnandre
Copy link
Member

Q A
Bug fix? no
New feature? yes
License MIT

When you use defer/lazy component, you can customize the "loading" content in the calling template. But for components that you reuse on multiple pages, it can lead to code duplication.

Moreover, you cannot adapt the size / content depending on the props.

So... introducing the "placeholder" macro.

If you define such macro in your lazy component template, it will be called to generate this temporary content.

{# templates/components/FooBar.html.twig #}

<div {{ attributes }}>

    {# ... #}

    {# your live component code #}

    {# ... #}

</div>


{% macro placeholder(props) %}
    {# This code will only be visible in the "loading" content #}
    <span class="loading-row"></span>
{% endmacro %}

Bonus, you'll get the props of your component as parameters.

So let's say you have a live component which load N comments behind an article.

When you set this prop to your live component, it will be passed to the placeholder macro, allowing you to fill the good amount of space / loading blocs in the HTML

<twig:Comments n="5" />
{# templates/components/Comments.html.twig #}
<div {{ attributes }}
...
</div>

{% macro placeholder(props) %}
    {% for i in 1..props.n %}
        <span class="loading-block"></span>
     {% endfor %}
{% endmacro %}

What do you think ?

@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Feb 22, 2024
@smnandre
Copy link
Member Author

I tried things with blocks (but you'd have to manually "not render it" when not in lazy), with includes (but i wanted to keep code in the same template), so it feel a good balance.

@kbond
Copy link
Member

kbond commented Feb 22, 2024

This is nice! Does rendering loading content traditionally take priority over the macro?

@smnandre
Copy link
Member Author

This is nice! Does rendering loading content traditionally take priority over the macro?

Yes, if you pass a loadingTemplate attribute it takes precedence. And the loadingTag is "around" for both cases.

@kbond
Copy link
Member

kbond commented Feb 22, 2024

So the primary benefits are:

  • not requiring injecting the same loading template for every use
  • allows accessing the passed component props

@smnandre
Copy link
Member Author

Exactly and the last one:

  • "styling" the block in a single place

It's 100% optional and has no cost obviously :)

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

Seems very sensible. Doing this with blocks, in theory, would be cool - but I totally agree that, in practice, it doesn't seem possible in a nice way.

@@ -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 :)

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Feb 26, 2024
@weaverryan weaverryan force-pushed the feat/live-placeholder branch from b4f00da to 2130326 Compare February 26, 2024 19:27
@weaverryan
Copy link
Member

weaverryan commented Feb 26, 2024

Thanks Simon!

@weaverryan weaverryan merged commit 9c7a811 into symfony:2.x Feb 26, 2024
weaverryan added a commit that referenced this pull request Feb 27, 2024
…ndre)

This PR was squashed before being merged into the 2.x branch.

Discussion
----------

[LiveComponent] Add doc about the placeholder macro

Add documentation about  #1532 (placeholder macro)

Commits
-------

a7c2cb3 [LiveComponent] Add doc about the placeholder macro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Reviewed Has been reviewed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants