Skip to content

Commit

Permalink
Adjust attributes names and allow choosing a custom HTML tag
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Sep 26, 2023
1 parent fd97359 commit fea1c7c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
*/
final class AddLiveAttributesSubscriber implements EventSubscriberInterface, ServiceSubscriberInterface
{
private const DEFAULT_LOADING_TAG = 'div';

private const DEFAULT_LOADING_TEMPLATE = null;

public function __construct(
private ComponentStack $componentStack,
private TemplateMap $templateMap,
Expand Down Expand Up @@ -75,14 +79,14 @@ public function onPreRender(PreRenderEvent $event): void
}

$inputProps = $event->getMountedComponent()->getInputProps();
$isDeferred = array_key_exists('defer', $inputProps);
$isDeferred = \array_key_exists('defer', $inputProps);

if ($isDeferred) {
$event->setTemplate('@LiveComponent/deferred.html.twig');

if ($loadingTemplate) {
$variables['loadingTemplate'] = $loadingTemplate;
}
$variables['loadingTag'] = $originalAttributes['loading-tag'] ?? self::DEFAULT_LOADING_TAG;
$variables['loadingTemplate'] = $originalAttributes['loading-template'] ?? self::DEFAULT_LOADING_TEMPLATE;
$attributes = $attributes->without('defer', 'loading-element', 'loading-template');
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/LiveComponent/templates/deferred.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<div {{ attributes }} data-action="live:connect->live#$render">
{% if loadingTemplate is defined and loadingTemplate != null %}
{{ include(loadingTemplate) }}
{% endif %}
</div>
<{{ loadingTag }} {{ attributes }} data-action="live:connect->live#$render">
{% block loadingContent %}
{% if loadingTemplate != null %}
{{ include(loadingTemplate) }}
{% endif %}
{% endblock %}
</{{ loadingTag }}>

0 comments on commit fea1c7c

Please sign in to comment.