Adding dynamic components to Jigsaw

Published in PHP, Laravel on Aug 18, 2022

Laravel supports the use
of Dynamic Components in Blade since Laravel 8.

This functionality isn't available in Jigsaw by default, although it's possible to add.

First, you'll want to create a new Blade component in the App\Components namespace called DynamicComponent. This
will extend the DynamicComponent class from Laravel's core, and overload a single method as shown below:

declare(strict_types=1);

namespace App\Components;

use Illuminate\Container\Container;
use Illuminate\View\DynamicComponent as BaseDynamicComponent;
use TightenCo\Jigsaw\View\ComponentTagCompiler;

class DynamicComponent extends BaseDynamicComponent
{
    protected function compiler()
    {
        if (! static::$compiler) {
            static::$compiler = new ComponentTagCompiler(
                Container::getInstance()->make('bladeCompiler')->getClassComponentAliases(),
                Container::getInstance()->make('bladeCompiler')->getClassComponentNamespaces(),
                Container::getInstance()->make('bladeCompiler')
            );
        }

        return static::$compiler;
    }
}

Next, you'll want to register your new dynamic component in the Blade compiler. To do this, add the following to
your blade.php file in the root of your Jigsaw project.

$bladeCompiler->component(\App\Components\DynamicComponent::class);

You should now be able to use your dynamic components as expected. For example:

<x-dynamic-component :component="$component"/>
0
0
0
1