-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix embedded Live Component rendering (#40)
- Loading branch information
Showing
10 changed files
with
118 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\UX\TwigComponent\Event\PreRenderEvent; | ||
use Symfony\UX\TwigComponent\MountedComponent; | ||
|
||
/** | ||
* @author Nicolas Rigaud <[email protected]> | ||
|
@@ -25,10 +26,42 @@ public function __construct( | |
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
PreRenderEvent::class => 'onPreRender', | ||
PreRenderEvent::class => [ | ||
['inlineRootLiveComponent', 1], | ||
['onPreRender', 0], | ||
], | ||
]; | ||
} | ||
|
||
public function inlineRootLiveComponent(PreRenderEvent $event): void | ||
{ | ||
$request = $this->requestStack->getMainRequest(); | ||
|
||
if (null === $request || !RequestAttributesHelper::isStorybookRequest($request)) { | ||
return; | ||
} | ||
|
||
if (!$event->getMetadata()->get('live', false)) { | ||
// not a live component, skip | ||
return; | ||
} | ||
|
||
$storybookAttributes = RequestAttributesHelper::getStorybookAttributes($request); | ||
|
||
$mounted = $event->getMountedComponent(); | ||
|
||
if ($mounted->hasExtraMetadata('hostTemplate') && $mounted->getExtraMetadata('hostTemplate') === $storybookAttributes->template) { | ||
// Dirty hack here: we are rendering a Live Component in the main story template with the embedded strategy. | ||
// The host template actually doesn't exist, which will cause errors because Live Component will try to use | ||
// it when re-rendering itself. As this is only useful for blocks resolution, we can safely remove this. | ||
// Using reflection because no extension point is available here. | ||
$refl = new \ReflectionProperty(MountedComponent::class, 'extraMetadata'); | ||
$extraMetadata = $refl->getValue($mounted); | ||
unset($extraMetadata['hostTemplate'], $extraMetadata['embeddedTemplateIndex']); | ||
$refl->setValue($mounted, $extraMetadata); | ||
} | ||
} | ||
|
||
public function onPreRender(PreRenderEvent $event): void | ||
{ | ||
$request = $this->requestStack->getMainRequest(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters